1// Copyright (C) 2019 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15import protos from '../protos'; 16 17// In this file are contained a few functions to simplify the proto parsing. 18 19export function extractTraceConfig( 20 enableTracingRequest: Uint8Array, 21): Uint8Array | undefined { 22 try { 23 const enableTracingObject = 24 protos.EnableTracingRequest.decode(enableTracingRequest); 25 if (!enableTracingObject.traceConfig) return undefined; 26 return protos.TraceConfig.encode(enableTracingObject.traceConfig).finish(); 27 } catch (e) { 28 // This catch is for possible proto encoding/decoding issues. 29 console.error('Error extracting the config: ', e.message); 30 return undefined; 31 } 32} 33 34export function browserSupportsPerfettoConfig(): boolean { 35 const minimumChromeVersion = '91.0.4448.0'; 36 const runningVersion = String( 37 (/Chrome\/(([0-9]+\.?){4})/.exec(navigator.userAgent) || [, 0])[1], 38 ); 39 40 if (!runningVersion) return false; 41 42 const minVerArray = minimumChromeVersion.split('.').map(Number); 43 const runVerArray = runningVersion.split('.').map(Number); 44 45 for (let index = 0; index < minVerArray.length; index++) { 46 if (runVerArray[index] === minVerArray[index]) continue; 47 return runVerArray[index] > minVerArray[index]; 48 } 49 return true; // Exact version match. 50} 51 52export function hasSystemDataSourceConfig(config: protos.TraceConfig): boolean { 53 for (const ds of config.dataSources) { 54 if (!(ds.config?.name ?? '').startsWith('org.chromium.')) { 55 return true; 56 } 57 } 58 return false; 59} 60