1/* 2 * Copyright (C) 2023 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 {ParserTimestampConverter} from 'common/timestamp_converter'; 19import {ParsingUtils} from 'parsers/legacy/parsing_utils'; 20import {com} from 'protos/viewcapture/udc/static'; 21import {Parser} from 'trace/parser'; 22import {TraceFile} from 'trace/trace_file'; 23import {TraceType} from 'trace/trace_type'; 24import {HierarchyTreeNode} from 'trace/tree_node/hierarchy_tree_node'; 25import {ParserViewCaptureWindow} from './parser_view_capture_window'; 26import {ExportedData} from './tampered_protos'; 27 28export class ParserViewCapture { 29 private readonly windowParsers: Array<Parser<HierarchyTreeNode>> = []; 30 31 constructor( 32 private readonly traceFile: TraceFile, 33 private readonly timestampConverter: ParserTimestampConverter, 34 ) {} 35 36 async parse() { 37 const traceBuffer = new Uint8Array(await this.traceFile.file.arrayBuffer()); 38 ParsingUtils.throwIfMagicNumberDoesNotMatch( 39 traceBuffer, 40 ParserViewCapture.MAGIC_NUMBER, 41 ); 42 43 const exportedData = ExportedData.decode( 44 traceBuffer, 45 ) as com.android.app.viewcapture.data.IExportedData; 46 47 const realToBootTimeOffsetNs = BigInt( 48 assertDefined(exportedData.realToElapsedTimeOffsetNanos).toString(), 49 ); 50 51 exportedData.windowData?.forEach( 52 (windowData: com.android.app.viewcapture.data.IWindowData) => 53 this.windowParsers.push( 54 new ParserViewCaptureWindow( 55 [this.traceFile.getDescriptor()], 56 windowData.frameData ?? [], 57 realToBootTimeOffsetNs, 58 assertDefined(exportedData.package), 59 assertDefined(windowData.title), 60 assertDefined(exportedData.classname), 61 this.timestampConverter, 62 ), 63 ), 64 ); 65 } 66 67 getTraceType(): TraceType { 68 return TraceType.VIEW_CAPTURE; 69 } 70 71 getWindowParsers(): Array<Parser<HierarchyTreeNode>> { 72 return this.windowParsers; 73 } 74 75 private static readonly MAGIC_NUMBER = [ 76 0x9, 0x78, 0x65, 0x90, 0x65, 0x73, 0x82, 0x65, 0x68, 77 ]; 78} 79