1/* 2 * Copyright (C) 2025 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 {Store} from 'common/store/store'; 19import {TimestampConverter} from 'common/time/timestamp_converter'; 20import {WinscopeEvent} from 'messaging/winscope_event'; 21import {EmitEvent} from 'messaging/winscope_event_emitter'; 22import {Trace} from 'trace/trace'; 23import {Traces} from 'trace/traces'; 24import {TRACE_INFO} from 'trace/trace_info'; 25import {TraceType} from 'trace/trace_type'; 26import {ViewerComponent} from './components/viewer_component'; 27import {View, Viewer, ViewType} from './viewer'; 28 29interface Presenter { 30 onAppEvent(event: WinscopeEvent): Promise<void>; 31 setEmitEvent(callback: EmitEvent): void; 32 addEventListeners(htmlElement: HTMLElement): void; 33} 34 35export abstract class AbstractViewer<T extends object> implements Viewer { 36 protected readonly trace: Trace<T> | undefined; 37 protected readonly htmlElement: HTMLElement; 38 protected readonly presenter: Presenter; 39 private readonly view: View; 40 41 constructor( 42 trace: Trace<T> | undefined, 43 traces: Traces, 44 componentSelector: string, 45 store: Store, 46 timestampConverter?: TimestampConverter, 47 ) { 48 this.trace = trace; 49 this.htmlElement = document.createElement(componentSelector); 50 (this.htmlElement as unknown as ViewerComponent<T>).store = store; 51 this.presenter = this.initializePresenter( 52 trace, 53 traces, 54 store, 55 timestampConverter, 56 ); 57 this.presenter.addEventListeners(this.htmlElement); 58 this.view = new View( 59 this.getViewType(), 60 this.getTraces(), 61 this.htmlElement, 62 TRACE_INFO[this.getTraceTypeForViewTitle()].name, 63 ); 64 } 65 66 setEmitEvent(callback: EmitEvent) { 67 this.presenter.setEmitEvent(callback); 68 } 69 70 async onWinscopeEvent(event: WinscopeEvent) { 71 await this.presenter.onAppEvent(event); 72 } 73 74 getViews(): View[] { 75 return [this.view]; 76 } 77 78 getTraces(): Array<Trace<object>> { 79 return [assertDefined(this.trace)]; 80 } 81 82 protected getTraceTypeForViewTitle(): TraceType { 83 return assertDefined(this.trace).type; 84 } 85 86 protected getViewType(): ViewType { 87 return ViewType.TRACE_TAB; 88 } 89 90 protected abstract initializePresenter( 91 trace: Trace<T> | undefined, 92 traces: Traces, 93 store: Store, 94 timestampConverter?: TimestampConverter, 95 ): Presenter; 96} 97