/* * Copyright (C) 2023 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import {RelativeEntryIndex, TraceEntryEager} from './trace'; export enum CustomQueryType { SF_LAYERS_ID_AND_NAME, VIEW_CAPTURE_METADATA, VSYNCID, WM_WINDOWS_TOKEN_AND_TITLE, } export class ProcessParserResult { static [CustomQueryType.SF_LAYERS_ID_AND_NAME]( parserResult: CustomQueryParserResultTypeMap[CustomQueryType.SF_LAYERS_ID_AND_NAME], ): CustomQueryResultTypeMap[CustomQueryType.SF_LAYERS_ID_AND_NAME] { return parserResult; } static [CustomQueryType.VIEW_CAPTURE_METADATA]( parserResult: CustomQueryParserResultTypeMap[CustomQueryType.VIEW_CAPTURE_METADATA], ): CustomQueryResultTypeMap[CustomQueryType.VIEW_CAPTURE_METADATA] { return parserResult; } static [CustomQueryType.VSYNCID]( parserResult: CustomQueryParserResultTypeMap[CustomQueryType.VSYNCID], makeTraceEntry: ( index: RelativeEntryIndex, vsyncId: bigint, ) => TraceEntryEager, ): CustomQueryResultTypeMap[CustomQueryType.VSYNCID] { return parserResult.map((vsyncId, index) => { return makeTraceEntry(index, vsyncId); }); } static [CustomQueryType.WM_WINDOWS_TOKEN_AND_TITLE]( parserResult: CustomQueryParserResultTypeMap[CustomQueryType.WM_WINDOWS_TOKEN_AND_TITLE], ): CustomQueryResultTypeMap[CustomQueryType.WM_WINDOWS_TOKEN_AND_TITLE] { return parserResult; } } export interface CustomQueryParamTypeMap { [CustomQueryType.SF_LAYERS_ID_AND_NAME]: never; [CustomQueryType.VIEW_CAPTURE_METADATA]: never; [CustomQueryType.VSYNCID]: never; [CustomQueryType.WM_WINDOWS_TOKEN_AND_TITLE]: never; } export interface CustomQueryParserResultTypeMap { [CustomQueryType.SF_LAYERS_ID_AND_NAME]: Array<{id: number; name: string}>; [CustomQueryType.VIEW_CAPTURE_METADATA]: { packageName: string; windowName: string; }; [CustomQueryType.VSYNCID]: Array; [CustomQueryType.WM_WINDOWS_TOKEN_AND_TITLE]: Array<{ token: string; title: string; }>; } export interface CustomQueryResultTypeMap { [CustomQueryType.SF_LAYERS_ID_AND_NAME]: Array<{id: number; name: string}>; [CustomQueryType.VIEW_CAPTURE_METADATA]: { packageName: string; windowName: string; }; [CustomQueryType.VSYNCID]: Array>; [CustomQueryType.WM_WINDOWS_TOKEN_AND_TITLE]: Array<{ token: string; title: string; }>; } export class VisitableParserCustomQuery { private readonly type: CustomQueryType; private result: Promise | undefined; constructor(type: Q) { this.type = type; } visit( type: R, visitor: () => Promise, ): VisitableParserCustomQuery { if (type !== this.type) { return this; } this.result = visitor() as Promise; return this; } getResult(): Promise { if (this.result === undefined) { throw new Error( `No result available. Looks like custom query (type: ${this.type}) is not implemented!`, ); } return this.result; } }