1/*
2 * Copyright (C) 2024 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
17import {assertDefined} from 'common/assert_utils';
18import {ParserTimestampConverter} from 'common/timestamp_converter';
19import {HierarchyTreeManagerServiceFactory} from 'parsers/input_method/hierarchy_tree_manager_service_factory';
20import {AbstractParser} from 'parsers/perfetto/abstract_parser';
21import {FakeProtoTransformer} from 'parsers/perfetto/fake_proto_transformer';
22import {Utils} from 'parsers/perfetto/utils';
23import {TamperedMessageType} from 'parsers/tampered_message_type';
24import root from 'protos/ime/latest/json';
25import {TraceFile} from 'trace/trace_file';
26import {TraceType} from 'trace/trace_type';
27import {HierarchyTreeNode} from 'trace/tree_node/hierarchy_tree_node';
28import {WasmEngineProxy} from 'trace_processor/wasm_engine_proxy';
29
30export class ParserInputMethodManagerService extends AbstractParser<HierarchyTreeNode> {
31  private static readonly Wrapper = TamperedMessageType.tamper(
32    root.lookupType('perfetto.protos.Wrapper'),
33  );
34  private static readonly ENTRY_FIELD =
35    ParserInputMethodManagerService.Wrapper.fields['inputmethodManagerService'];
36  private static readonly MANAGER_SERVICE_FIELD = assertDefined(
37    ParserInputMethodManagerService.ENTRY_FIELD.tamperedMessageType,
38  ).fields['inputMethodManagerService'];
39  private static readonly HIERARCHY_TREE_FACTORY =
40    new HierarchyTreeManagerServiceFactory(
41      ParserInputMethodManagerService.ENTRY_FIELD,
42      ParserInputMethodManagerService.MANAGER_SERVICE_FIELD,
43    );
44
45  private protoTransformer: FakeProtoTransformer;
46
47  constructor(
48    traceFile: TraceFile,
49    traceProcessor: WasmEngineProxy,
50    timestampConverter: ParserTimestampConverter,
51  ) {
52    super(traceFile, traceProcessor, timestampConverter);
53
54    this.protoTransformer = new FakeProtoTransformer(
55      assertDefined(
56        ParserInputMethodManagerService.ENTRY_FIELD.tamperedMessageType,
57      ),
58    );
59  }
60
61  override getTraceType(): TraceType {
62    return TraceType.INPUT_METHOD_MANAGER_SERVICE;
63  }
64
65  override async getEntry(index: number): Promise<HierarchyTreeNode> {
66    let entryProto = await Utils.queryEntry(
67      this.traceProcessor,
68      this.getTableName(),
69      this.entryIndexToRowIdMap,
70      index,
71    );
72    entryProto = this.protoTransformer.transform(entryProto);
73    return ParserInputMethodManagerService.HIERARCHY_TREE_FACTORY.makeHierarchyTree(
74      entryProto,
75    );
76  }
77
78  protected override getStdLibModuleName(): string | undefined {
79    return 'android.winscope.inputmethod';
80  }
81
82  protected override getTableName(): string {
83    return 'android_inputmethod_manager_service';
84  }
85}
86