xref: /aosp_15_r20/external/perfetto/ui/src/plugins/dev.perfetto.RecordTrace/trace_config_utils.ts (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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 {EnableTracingRequest, TraceConfig} 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      EnableTracingRequest.decode(enableTracingRequest);
25    if (!enableTracingObject.traceConfig) return undefined;
26    return 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 extractDurationFromTraceConfig(traceConfigProto: Uint8Array) {
35  try {
36    return TraceConfig.decode(traceConfigProto).durationMs;
37  } catch (e) {
38    // This catch is for possible proto encoding/decoding issues.
39    return undefined;
40  }
41}
42
43export function browserSupportsPerfettoConfig(): boolean {
44  const minimumChromeVersion = '91.0.4448.0';
45  const runningVersion = String(
46    (/Chrome\/(([0-9]+\.?){4})/.exec(navigator.userAgent) || [, 0])[1],
47  );
48
49  if (!runningVersion) return false;
50
51  const minVerArray = minimumChromeVersion.split('.').map(Number);
52  const runVerArray = runningVersion.split('.').map(Number);
53
54  for (let index = 0; index < minVerArray.length; index++) {
55    if (runVerArray[index] === minVerArray[index]) continue;
56    return runVerArray[index] > minVerArray[index];
57  }
58  return true; // Exact version match.
59}
60
61export function hasSystemDataSourceConfig(config: TraceConfig): boolean {
62  for (const ds of config.dataSources) {
63    if (!(ds.config?.name ?? '').startsWith('org.chromium.')) {
64      return true;
65    }
66  }
67  return false;
68}
69