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 {HierarchyTreeClientsFactory} from 'parsers/input_method/hierarchy_tree_clients_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 ParserInputMethodClients extends AbstractParser<HierarchyTreeNode> {
31  private static readonly Wrapper = TamperedMessageType.tamper(
32    root.lookupType('perfetto.protos.Wrapper'),
33  );
34  private static readonly ENTRY_FIELD =
35    ParserInputMethodClients.Wrapper.fields['inputmethodClients'];
36  private static readonly CLIENT_FIELD = assertDefined(
37    ParserInputMethodClients.ENTRY_FIELD.tamperedMessageType,
38  ).fields['client'];
39  private static readonly HIERARCHY_TREE_FACTORY =
40    new HierarchyTreeClientsFactory(
41      ParserInputMethodClients.ENTRY_FIELD,
42      ParserInputMethodClients.CLIENT_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(ParserInputMethodClients.ENTRY_FIELD.tamperedMessageType),
56    );
57  }
58
59  override getTraceType(): TraceType {
60    return TraceType.INPUT_METHOD_CLIENTS;
61  }
62
63  override async getEntry(index: number): Promise<HierarchyTreeNode> {
64    let entryProto = await Utils.queryEntry(
65      this.traceProcessor,
66      this.getTableName(),
67      this.entryIndexToRowIdMap,
68      index,
69    );
70    entryProto = this.protoTransformer.transform(entryProto);
71    return ParserInputMethodClients.HIERARCHY_TREE_FACTORY.makeHierarchyTree(
72      entryProto,
73    );
74  }
75
76  protected override getStdLibModuleName(): string | undefined {
77    return 'android.winscope.inputmethod';
78  }
79
80  protected override getTableName(): string {
81    return 'android_inputmethod_clients';
82  }
83}
84