1// Copyright 2022 The Chromium Authors 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5import {sendWithPromise} from 'chrome://resources/js/cr.js'; 6 7// <if expr="structured_metrics_enabled"> 8import type {StructuredMetricEvent, StructuredMetricsSummary} from './structured/structured_utils.js'; 9// </if> 10 11/** 12 * @fileoverview A helper object used by the chrome://metrics-internals page to 13 * interact with the browser. 14 */ 15 16/** 17 * A pair of strings representing a row in a summary table. For example, |key| 18 * could be "Platform", with |value| being "Android". 19 */ 20export interface KeyValue { 21 key: string; 22 value: string; 23} 24 25/** 26 * An individual event that occurred on a log. Optionally, this may include a 27 * message. For example, for a "Trimmed" event, the message could be "Log size 28 * too large". 29 */ 30export interface LogEvent { 31 event: string; 32 timestampMs: number; 33 message?: string; 34} 35 36/** 37 * A log and its data, including the events that occurred throughout its 38 * lifetime. The |type| field is only set for UMA logs (i.e., ongoing, 39 * independent, or stability). The |compressed_data| field (i.e., its proto 40 * data) is only set when exporting. 41 * TODO(crbug/1363747): Change name of |type| to something else, since it is 42 * confusing and can be mistaken for |logType| in LogData (UMA or UKM). 43 */ 44export interface Log { 45 type?: string; 46 hash: string; 47 timestamp: string; 48 data?: string; 49 size: number; 50 events: LogEvent[]; 51} 52 53/** 54 * A list of logs, as well as their type (UMA or UKM). 55 */ 56export interface LogData { 57 logType: string; 58 logs: Log[]; 59} 60 61export interface MetricsInternalsBrowserProxy { 62 /** 63 * Gets UMA log data. |includeLogProtoData| determines whether or not the 64 * fetched data should also include the protos of the logs. 65 */ 66 getUmaLogData(includeLogProtoData: boolean): Promise<string>; 67 68 /** 69 * Fetches a summary of variations info. 70 */ 71 fetchVariationsSummary(): Promise<KeyValue[]>; 72 73 /** 74 * Fetches a summary of UMA info. 75 */ 76 fetchUmaSummary(): Promise<KeyValue[]>; 77 78 /** 79 * Fetches whether the logs observer being used is owned by the metrics 80 * service or is owned by the page. 81 */ 82 isUsingMetricsServiceObserver(): Promise<boolean>; 83 84 // <if expr="structured_metrics_enabled"> 85 /** 86 * Fetches recorded events from Structured Metrics Service. 87 */ 88 fetchStructuredMetricsEvents(): Promise<StructuredMetricEvent[]>; 89 90 /** 91 * Fetches a summary of the Structured Metrics Service. 92 */ 93 fetchStructuredMetricsSummary(): Promise<StructuredMetricsSummary>; 94 // </if> 95} 96 97export class MetricsInternalsBrowserProxyImpl implements 98 MetricsInternalsBrowserProxy { 99 getUmaLogData(includeLogProtoData: boolean): Promise<string> { 100 return sendWithPromise('fetchUmaLogsData', includeLogProtoData); 101 } 102 103 fetchVariationsSummary(): Promise<KeyValue[]> { 104 return sendWithPromise('fetchVariationsSummary'); 105 } 106 107 fetchUmaSummary(): Promise<KeyValue[]> { 108 return sendWithPromise('fetchUmaSummary'); 109 } 110 111 isUsingMetricsServiceObserver(): Promise<boolean> { 112 return sendWithPromise('isUsingMetricsServiceObserver'); 113 } 114 115 // <if expr="structured_metrics_enabled"> 116 fetchStructuredMetricsEvents(): Promise<StructuredMetricEvent[]> { 117 return sendWithPromise('fetchStructuredMetricsEvents'); 118 } 119 120 fetchStructuredMetricsSummary(): Promise<StructuredMetricsSummary> { 121 return sendWithPromise('fetchStructuredMetricsSummary'); 122 } 123 // </if> 124 125 static getInstance(): MetricsInternalsBrowserProxy { 126 return instance || (instance = new MetricsInternalsBrowserProxyImpl()); 127 } 128 129 static setInstance(obj: MetricsInternalsBrowserProxy) { 130 instance = obj; 131 } 132} 133 134let instance: MetricsInternalsBrowserProxy|null = null; 135