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 {AbstractParser} from 'parsers/legacy/abstract_parser';
20import {RectsComputation} from 'parsers/window_manager/computations/rects_computation';
21import {WmCustomQueryUtils} from 'parsers/window_manager/custom_query_utils';
22import {HierarchyTreeBuilderWm} from 'parsers/window_manager/hierarchy_tree_builder_wm';
23import {PropertiesProviderFactory} from 'parsers/window_manager/properties_provider_factory';
24import {com} from 'protos/windowmanager/udc/static';
25import {
26  CustomQueryParserResultTypeMap,
27  CustomQueryType,
28  VisitableParserCustomQuery,
29} from 'trace/custom_query';
30import {EntriesRange} from 'trace/index_types';
31import {TraceType} from 'trace/trace_type';
32import {HierarchyTreeNode} from 'trace/tree_node/hierarchy_tree_node';
33import {PropertiesProvider} from 'trace/tree_node/properties_provider';
34import {TAMPERED_PROTOS_UDC} from './tampered_protos_udc';
35
36class ParserWindowManagerDump extends AbstractParser {
37  private readonly factory = new PropertiesProviderFactory(TAMPERED_PROTOS_UDC);
38
39  override getTraceType(): TraceType {
40    return TraceType.WINDOW_MANAGER;
41  }
42
43  override getMagicNumber(): undefined {
44    return undefined;
45  }
46
47  override getRealToBootTimeOffsetNs(): bigint | undefined {
48    return undefined;
49  }
50
51  override getRealToMonotonicTimeOffsetNs(): bigint | undefined {
52    return undefined;
53  }
54
55  override decodeTrace(
56    buffer: Uint8Array,
57  ): com.android.server.wm.IWindowManagerServiceDumpProto[] {
58    const entryProto = assertDefined(
59      TAMPERED_PROTOS_UDC.windowManagerServiceField.tamperedMessageType,
60    ).decode(buffer) as com.android.server.wm.IWindowManagerServiceDumpProto;
61
62    // This parser is prone to accepting invalid inputs because it lacks a magic
63    // number. Let's reduce the chances of accepting invalid inputs by making
64    // sure that a trace entry can actually be created from the decoded proto.
65    // If the trace entry creation fails, an exception is thrown and the parser
66    // will be considered unsuited for this input data.
67    this.processDecodedEntry(0, entryProto);
68
69    return [entryProto];
70  }
71
72  protected override getTimestamp(
73    entryProto: com.android.server.wm.IWindowManagerServiceDumpProto,
74  ): Timestamp {
75    return this.timestampConverter.makeZeroTimestamp();
76  }
77
78  override processDecodedEntry(
79    index: number,
80    entryProto: com.android.server.wm.IWindowManagerServiceDumpProto,
81  ): HierarchyTreeNode {
82    return this.makeHierarchyTree(entryProto);
83  }
84
85  private makeHierarchyTree(
86    entryProto: com.android.server.wm.IWindowManagerServiceDumpProto,
87  ): HierarchyTreeNode {
88    const containers: PropertiesProvider[] =
89      this.factory.makeContainerProperties(assertDefined(entryProto));
90
91    const entry = this.factory.makeEntryProperties(entryProto);
92
93    return new HierarchyTreeBuilderWm()
94      .setRoot(entry)
95      .setChildren(containers)
96      .setComputations([new RectsComputation()])
97      .build();
98  }
99
100  override customQuery<Q extends CustomQueryType>(
101    type: Q,
102    entriesRange: EntriesRange,
103  ): Promise<CustomQueryParserResultTypeMap[Q]> {
104    return new VisitableParserCustomQuery(type)
105      .visit(CustomQueryType.WM_WINDOWS_TOKEN_AND_TITLE, () => {
106        const result: CustomQueryParserResultTypeMap[CustomQueryType.WM_WINDOWS_TOKEN_AND_TITLE] =
107          [];
108        this.decodedEntries
109          .slice(entriesRange.start, entriesRange.end)
110          .forEach((windowManagerServiceDumpProto) => {
111            WmCustomQueryUtils.parseWindowsTokenAndTitle(
112              windowManagerServiceDumpProto?.rootWindowContainer,
113              result,
114            );
115          });
116        return Promise.resolve(result);
117      })
118      .getResult();
119  }
120}
121
122export {ParserWindowManagerDump};
123