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 {WinscopeEvent} from 'messaging/winscope_event';
19import {EmitEvent} from 'messaging/winscope_event_emitter';
20import {Trace} from 'trace/trace';
21import {Traces} from 'trace/traces';
22import {TraceType} from 'trace/trace_type';
23import {PropertyTreeNode} from 'trace/tree_node/property_tree_node';
24import {TimestampClickDetail, ViewerEvents} from 'viewers/common/viewer_events';
25import {View, Viewer, ViewType} from 'viewers/viewer';
26import {Presenter} from './presenter';
27import {UiData} from './ui_data';
28
29class ViewerProtoLog implements Viewer {
30  static readonly DEPENDENCIES: TraceType[] = [TraceType.PROTO_LOG];
31
32  private readonly trace: Trace<PropertyTreeNode>;
33  private readonly htmlElement: HTMLElement;
34  private readonly presenter: Presenter;
35  private readonly view: View;
36
37  constructor(trace: Trace<PropertyTreeNode>, traces: Traces) {
38    this.trace = trace;
39    this.htmlElement = document.createElement('viewer-protolog');
40
41    this.presenter = new Presenter(trace, (data: UiData) => {
42      (this.htmlElement as any).inputData = data;
43    });
44
45    this.htmlElement.addEventListener(
46      ViewerEvents.LogLevelsFilterChanged,
47      (event) => {
48        this.presenter.onLogLevelsFilterChanged((event as CustomEvent).detail);
49      },
50    );
51    this.htmlElement.addEventListener(
52      ViewerEvents.TagsFilterChanged,
53      (event) => {
54        this.presenter.onTagsFilterChanged((event as CustomEvent).detail);
55      },
56    );
57    this.htmlElement.addEventListener(
58      ViewerEvents.SourceFilesFilterChanged,
59      (event) => {
60        this.presenter.onSourceFilesFilterChanged(
61          (event as CustomEvent).detail,
62        );
63      },
64    );
65    this.htmlElement.addEventListener(
66      ViewerEvents.SearchStringFilterChanged,
67      (event) => {
68        this.presenter.onSearchStringFilterChanged(
69          (event as CustomEvent).detail,
70        );
71      },
72    );
73    this.htmlElement.addEventListener(ViewerEvents.LogClicked, (event) => {
74      this.presenter.onMessageClicked((event as CustomEvent).detail);
75    });
76    this.htmlElement.addEventListener(
77      ViewerEvents.TimestampClick,
78      async (event) => {
79        const detail: TimestampClickDetail = (event as CustomEvent).detail;
80        await this.presenter.onLogTimestampClicked(assertDefined(detail.index));
81      },
82    );
83
84    this.view = new View(
85      ViewType.TAB,
86      this.getTraces(),
87      this.htmlElement,
88      'ProtoLog',
89    );
90  }
91
92  async onWinscopeEvent(event: WinscopeEvent) {
93    await this.presenter.onAppEvent(event);
94  }
95
96  setEmitEvent(callback: EmitEvent) {
97    this.presenter.setEmitEvent(callback);
98  }
99
100  getViews(): View[] {
101    return [this.view];
102  }
103
104  getTraces(): Array<Trace<PropertyTreeNode>> {
105    return [this.trace];
106  }
107}
108
109export {ViewerProtoLog};
110