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 {TraceConfig} from '../common/protos'; 16import {perfetto} from '../gen/protos'; 17 18// In this file are contained a few functions to simplify the proto parsing. 19 20export function extractTraceConfig(enableTracingRequest: Uint8Array): 21 Uint8Array|undefined { 22 try { 23 const enableTracingObject = 24 perfetto.protos.EnableTracingRequest.decode(enableTracingRequest); 25 if (!enableTracingObject.traceConfig) return undefined; 26 return perfetto.protos.TraceConfig.encode(enableTracingObject.traceConfig) 27 .finish(); 28 } catch (e) { // 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 extractDurationFromTraceConfig(traceConfigProto: Uint8Array) { 35 try { 36 return perfetto.protos.TraceConfig.decode(traceConfigProto).durationMs; 37 } catch (e) { // This catch is for possible proto encoding/decoding issues. 38 return undefined; 39 } 40} 41 42export function browserSupportsPerfettoConfig(): boolean { 43 const minimumChromeVersion = '91.0.4448.0'; 44 const runningVersion = String( 45 (/Chrome\/(([0-9]+\.?){4})/.exec(navigator.userAgent) || [, 0])[1]); 46 47 if (!runningVersion) return false; 48 49 const minVerArray = minimumChromeVersion.split('.').map(Number); 50 const runVerArray = runningVersion.split('.').map(Number); 51 52 for (let index = 0; index < minVerArray.length; index++) { 53 if (runVerArray[index] === minVerArray[index]) continue; 54 return runVerArray[index] > minVerArray[index]; 55 } 56 return true; // Exact version match. 57} 58 59export function hasSystemDataSourceConfig(config: TraceConfig): boolean { 60 for (const ds of config.dataSources) { 61 if (ds.config && ds.config.name && 62 !ds.config.name.startsWith('org.chromium.')) { 63 return true; 64 } 65 } 66 return false; 67} 68