xref: /aosp_15_r20/development/tools/winscope/src/parsers/input_method/legacy/parser_input_method_clients.ts (revision 90c8c64db3049935a07c6143d7fd006e26f8ecca)
1/*
2 * Copyright (C) 2022 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 {Timestamp} from 'common/time';
19import {HierarchyTreeClientsFactory} from 'parsers/input_method/hierarchy_tree_clients_factory';
20import {AbstractParser} from 'parsers/legacy/abstract_parser';
21import {TamperedMessageType} from 'parsers/tampered_message_type';
22import root from 'protos/ime/udc/json';
23import {android} from 'protos/ime/udc/static';
24import {TraceType} from 'trace/trace_type';
25import {HierarchyTreeNode} from 'trace/tree_node/hierarchy_tree_node';
26
27class ParserInputMethodClients extends AbstractParser<HierarchyTreeNode> {
28  private static readonly MAGIC_NUMBER = [
29    0x09, 0x49, 0x4d, 0x43, 0x54, 0x52, 0x41, 0x43, 0x45,
30  ]; // .IMCTRACE
31
32  private static readonly InputMethodClientsTraceFileProto =
33    TamperedMessageType.tamper(
34      root.lookupType(
35        'android.view.inputmethod.InputMethodClientsTraceFileProto',
36      ),
37    );
38  private static readonly ENTRY_FIELD =
39    ParserInputMethodClients.InputMethodClientsTraceFileProto.fields['entry'];
40  private static readonly CLIENT_FIELD = assertDefined(
41    ParserInputMethodClients.ENTRY_FIELD.tamperedMessageType,
42  ).fields['client'];
43  private static readonly HIERARCHY_TREE_FACTORY =
44    new HierarchyTreeClientsFactory(
45      ParserInputMethodClients.ENTRY_FIELD,
46      ParserInputMethodClients.CLIENT_FIELD,
47    );
48
49  private realToBootTimeOffsetNs: bigint | undefined;
50
51  override getTraceType(): TraceType {
52    return TraceType.INPUT_METHOD_CLIENTS;
53  }
54
55  override getMagicNumber(): number[] {
56    return ParserInputMethodClients.MAGIC_NUMBER;
57  }
58
59  override getRealToBootTimeOffsetNs(): bigint | undefined {
60    return this.realToBootTimeOffsetNs;
61  }
62
63  override getRealToMonotonicTimeOffsetNs(): bigint | undefined {
64    return undefined;
65  }
66
67  override decodeTrace(
68    buffer: Uint8Array,
69  ): android.view.inputmethod.IInputMethodClientsTraceProto[] {
70    const decoded =
71      ParserInputMethodClients.InputMethodClientsTraceFileProto.decode(
72        buffer,
73      ) as android.view.inputmethod.IInputMethodClientsTraceFileProto;
74    const timeOffset = BigInt(
75      decoded.realToElapsedTimeOffsetNanos?.toString() ?? '0',
76    );
77    this.realToBootTimeOffsetNs = timeOffset !== 0n ? timeOffset : undefined;
78    return decoded.entry ?? [];
79  }
80
81  protected override getTimestamp(
82    entry: android.view.inputmethod.IInputMethodClientsTraceProto,
83  ): Timestamp {
84    return this.timestampConverter.makeTimestampFromBootTimeNs(
85      BigInt(assertDefined(entry.elapsedRealtimeNanos).toString()),
86    );
87  }
88
89  override processDecodedEntry(
90    index: number,
91    entry: android.view.inputmethod.IInputMethodClientsTraceProto,
92  ): HierarchyTreeNode {
93    if (
94      entry.elapsedRealtimeNanos === undefined ||
95      entry.elapsedRealtimeNanos === null
96    ) {
97      throw new Error('Missing elapsedRealtimeNanos on IME Clients entry');
98    }
99
100    return ParserInputMethodClients.HIERARCHY_TREE_FACTORY.makeHierarchyTree(
101      entry,
102    );
103  }
104}
105
106export {ParserInputMethodClients};
107