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 {Timestamp} from 'common/time'; 19import {HierarchyTreeManagerServiceFactory} from 'parsers/input_method/hierarchy_tree_manager_service_factory'; 20import {AbstractParser} from 'parsers/legacy/abstract_parser'; 21import {TamperedMessageType} from 'parsers/tampered_message_type'; 22import root from 'protos/ime/udc/json'; 23import {android} from 'protos/ime/udc/static'; 24import {TraceType} from 'trace/trace_type'; 25import {HierarchyTreeNode} from 'trace/tree_node/hierarchy_tree_node'; 26 27class ParserInputMethodManagerService extends AbstractParser<HierarchyTreeNode> { 28 private static readonly MAGIC_NUMBER = [ 29 0x09, 0x49, 0x4d, 0x4d, 0x54, 0x52, 0x41, 0x43, 0x45, 30 ]; // .IMMTRACE 31 32 private static readonly InputMethodManagerServiceTraceFileProto = 33 TamperedMessageType.tamper( 34 root.lookupType( 35 'android.view.inputmethod.InputMethodManagerServiceTraceFileProto', 36 ), 37 ); 38 private static readonly ENTRY_FIELD = 39 ParserInputMethodManagerService.InputMethodManagerServiceTraceFileProto 40 .fields['entry']; 41 private static readonly MANAGER_SERVICE_FIELD = assertDefined( 42 ParserInputMethodManagerService.ENTRY_FIELD.tamperedMessageType, 43 ).fields['inputMethodManagerService']; 44 private static readonly HIERARCHY_TREE_FACTORY = 45 new HierarchyTreeManagerServiceFactory( 46 ParserInputMethodManagerService.ENTRY_FIELD, 47 ParserInputMethodManagerService.MANAGER_SERVICE_FIELD, 48 ); 49 50 private realToBootTimeOffsetNs: bigint | undefined; 51 52 override getTraceType(): TraceType { 53 return TraceType.INPUT_METHOD_MANAGER_SERVICE; 54 } 55 56 override getMagicNumber(): number[] { 57 return ParserInputMethodManagerService.MAGIC_NUMBER; 58 } 59 60 override getRealToBootTimeOffsetNs(): bigint | undefined { 61 return this.realToBootTimeOffsetNs; 62 } 63 64 override getRealToMonotonicTimeOffsetNs(): bigint | undefined { 65 return undefined; 66 } 67 68 override decodeTrace( 69 buffer: Uint8Array, 70 ): android.view.inputmethod.IInputMethodManagerServiceTraceProto[] { 71 const decoded = 72 ParserInputMethodManagerService.InputMethodManagerServiceTraceFileProto.decode( 73 buffer, 74 ) as android.view.inputmethod.IInputMethodManagerServiceTraceFileProto; 75 const timeOffset = BigInt( 76 decoded.realToElapsedTimeOffsetNanos?.toString() ?? '0', 77 ); 78 this.realToBootTimeOffsetNs = timeOffset !== 0n ? timeOffset : undefined; 79 return decoded.entry ?? []; 80 } 81 82 protected override getTimestamp( 83 entry: android.view.inputmethod.IInputMethodManagerServiceTraceProto, 84 ): Timestamp { 85 return this.timestampConverter.makeTimestampFromBootTimeNs( 86 BigInt(assertDefined(entry.elapsedRealtimeNanos).toString()), 87 ); 88 } 89 90 protected override processDecodedEntry( 91 index: number, 92 entry: android.view.inputmethod.IInputMethodManagerServiceTraceProto, 93 ): HierarchyTreeNode { 94 if ( 95 entry.elapsedRealtimeNanos === undefined || 96 entry.elapsedRealtimeNanos === null 97 ) { 98 throw Error('Missing elapsedRealtimeNanos on entry'); 99 } 100 return ParserInputMethodManagerService.HIERARCHY_TREE_FACTORY.makeHierarchyTree( 101 entry, 102 ); 103 } 104} 105 106export {ParserInputMethodManagerService}; 107