1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "src/trace_processor/importers/proto/winscope/shell_transitions_parser.h"
18 #include "src/trace_processor/importers/proto/winscope/shell_transitions_tracker.h"
19 
20 #include "perfetto/ext/base/base64.h"
21 #include "protos/perfetto/trace/android/shell_transition.pbzero.h"
22 #include "src/trace_processor/importers/common/args_tracker.h"
23 #include "src/trace_processor/importers/proto/args_parser.h"
24 #include "src/trace_processor/storage/trace_storage.h"
25 #include "src/trace_processor/types/trace_processor_context.h"
26 #include "src/trace_processor/util/winscope_proto_mapping.h"
27 
28 namespace perfetto {
29 namespace trace_processor {
30 
ShellTransitionsParser(TraceProcessorContext * context)31 ShellTransitionsParser::ShellTransitionsParser(TraceProcessorContext* context)
32     : context_(context), args_parser_{*context->descriptor_pool_} {}
33 
ParseTransition(protozero::ConstBytes blob)34 void ShellTransitionsParser::ParseTransition(protozero::ConstBytes blob) {
35   protos::pbzero::ShellTransition::Decoder transition(blob);
36 
37   auto row_id =
38       ShellTransitionsTracker::GetOrCreate(context_)->InternTransition(
39           transition.id());
40 
41   auto* window_manager_shell_transitions_table =
42       context_->storage->mutable_window_manager_shell_transitions_table();
43   auto row = window_manager_shell_transitions_table->FindById(row_id).value();
44 
45   if (transition.has_dispatch_time_ns()) {
46     row.set_ts(transition.dispatch_time_ns());
47   }
48 
49   auto base64_proto = context_->storage->mutable_string_pool()->InternString(
50       base::StringView(base::Base64Encode(blob.data, blob.size)));
51   row.set_base64_proto(base64_proto);
52   row.set_base64_proto_id(base64_proto.raw_id());
53   auto inserter = context_->args_tracker->AddArgsTo(row_id);
54   ArgsParser writer(/*timestamp=*/0, inserter, *context_->storage.get());
55   base::Status status = args_parser_.ParseMessage(
56       blob,
57       *util::winscope_proto_mapping::GetProtoName(
58           tables::WindowManagerShellTransitionsTable::Name()),
59       nullptr /* parse all fields */, writer);
60 
61   if (!status.ok()) {
62     context_->storage->IncrementStats(
63         stats::winscope_shell_transitions_parse_errors);
64   }
65 }
66 
ParseHandlerMappings(protozero::ConstBytes blob)67 void ShellTransitionsParser::ParseHandlerMappings(protozero::ConstBytes blob) {
68   auto* shell_handlers_table =
69       context_->storage
70           ->mutable_window_manager_shell_transition_handlers_table();
71 
72   protos::pbzero::ShellHandlerMappings::Decoder handler_mappings(blob);
73   for (auto it = handler_mappings.mapping(); it; ++it) {
74     protos::pbzero::ShellHandlerMapping::Decoder mapping(it.field().as_bytes());
75 
76     tables::WindowManagerShellTransitionHandlersTable::Row row;
77     row.handler_id = mapping.id();
78     row.handler_name = context_->storage->InternString(
79         base::StringView(mapping.name().ToStdString()));
80     row.base64_proto = context_->storage->mutable_string_pool()->InternString(
81         base::StringView(base::Base64Encode(blob.data, blob.size)));
82     row.base64_proto_id = row.base64_proto.raw_id();
83     shell_handlers_table->Insert(row);
84   }
85 }
86 
87 }  // namespace trace_processor
88 }  // namespace perfetto
89