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 {Timestamp, TimestampType} from 'trace/timestamp'; 18import {TraceFile} from 'trace/trace_file'; 19import {TraceType} from 'trace/trace_type'; 20import {AbstractParser} from './abstract_parser'; 21import {AccessibilityTraceFileProto} from './proto_types'; 22 23class ParserAccessibility extends AbstractParser { 24 constructor(trace: TraceFile) { 25 super(trace); 26 this.realToElapsedTimeOffsetNs = undefined; 27 } 28 29 override getTraceType(): TraceType { 30 return TraceType.ACCESSIBILITY; 31 } 32 33 override getMagicNumber(): number[] { 34 return ParserAccessibility.MAGIC_NUMBER; 35 } 36 37 override decodeTrace(buffer: Uint8Array): any[] { 38 const decoded = AccessibilityTraceFileProto.decode(buffer) as any; 39 if (Object.prototype.hasOwnProperty.call(decoded, 'realToElapsedTimeOffsetNanos')) { 40 this.realToElapsedTimeOffsetNs = BigInt(decoded.realToElapsedTimeOffsetNanos); 41 } else { 42 this.realToElapsedTimeOffsetNs = undefined; 43 } 44 return decoded.entry; 45 } 46 47 override getTimestamp(type: TimestampType, entryProto: any): undefined | Timestamp { 48 if (type === TimestampType.ELAPSED) { 49 return new Timestamp(type, BigInt(entryProto.elapsedRealtimeNanos)); 50 } else if (type === TimestampType.REAL && this.realToElapsedTimeOffsetNs !== undefined) { 51 return new Timestamp( 52 type, 53 this.realToElapsedTimeOffsetNs + BigInt(entryProto.elapsedRealtimeNanos) 54 ); 55 } 56 return undefined; 57 } 58 59 override processDecodedEntry(index: number, timestampType: TimestampType, entryProto: any): any { 60 return entryProto; 61 } 62 63 private realToElapsedTimeOffsetNs: undefined | bigint; 64 private static readonly MAGIC_NUMBER = [0x09, 0x41, 0x31, 0x31, 0x59, 0x54, 0x52, 0x41, 0x43]; // .A11YTRAC 65} 66 67export {ParserAccessibility}; 68