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/** 8 * @fileoverview A helper object used by the chrome://metrics-internals page to 9 * interact with the browser. 10 */ 11 12/** 13 * A pair of strings representing a row in a summary table. For example, |key| 14 * could be "Platform", with |value| being "Android". 15 */ 16export interface KeyValue { 17 key: string; 18 value: string; 19} 20 21/** 22 * An individual event that occurred on a log. Optionally, this may include a 23 * message. For example, for a "Trimmed" event, the message could be "Log size 24 * too large". 25 */ 26export interface LogEvent { 27 event: string; 28 timestampMs: number; 29 message?: string; 30} 31 32/** 33 * A log and its data, including the events that occurred throughout its 34 * lifetime. The |type| field is only set for UMA logs (i.e., ongoing, 35 * independent, or stability). The |compressed_data| field (i.e., its proto 36 * data) is only set when exporting. 37 * TODO(crbug.com/40238818): Change name of |type| to something else, since it is 38 * confusing and can be mistaken for |logType| in LogData (UMA or UKM). 39 */ 40export interface Log { 41 type?: string; 42 hash: string; 43 timestamp: string; 44 data?: string; 45 size: number; 46 events: LogEvent[]; 47} 48 49/** 50 * A list of logs, as well as their type (UMA or UKM). 51 */ 52export interface LogData { 53 logType: string; 54 logs: Log[]; 55} 56 57/** 58 * A study or group name along with its hex hash. 59 */ 60export interface HashNamed { 61 // `undefined` if we only know the hash. 62 name: string|undefined; 63 hash: string; 64} 65 66/** 67 * A Field Trial Group. 68 */ 69export interface Group extends HashNamed { 70 forceEnabled: boolean; 71 enabled: boolean; 72} 73 74/** 75 * A Field Trial. 76 */ 77export interface Trial extends HashNamed { 78 groups: Group[]; 79} 80 81/** 82 * Maps some hashes to their study/group names. 83 */ 84export interface HashNameMap { 85 [hash: string]: string; 86} 87 88 89/** 90 * State of all field trials. 91 */ 92export interface FieldTrialState { 93 trials: Trial[]; 94 restartRequired: boolean; 95} 96 97export interface MetricsInternalsBrowserProxy { 98 /** 99 * Gets UMA log data. |includeLogProtoData| determines whether or not the 100 * fetched data should also include the protos of the logs. 101 */ 102 getUmaLogData(includeLogProtoData: boolean): Promise<string>; 103 104 /** 105 * Fetches a summary of variations info. 106 */ 107 fetchVariationsSummary(): Promise<KeyValue[]>; 108 109 /** 110 * Fetches a summary of UMA info. 111 */ 112 fetchUmaSummary(): Promise<KeyValue[]>; 113 114 /** 115 * Fetches whether the logs observer being used is owned by the metrics 116 * service or is owned by the page. 117 */ 118 isUsingMetricsServiceObserver(): Promise<boolean>; 119 120 /** 121 * Overrides the enroll state of a field trial which will be realized after a 122 * restart. 123 */ 124 setTrialEnrollState( 125 trialHash: string, groupHash: string, 126 forceEnable: boolean): Promise<boolean>; 127 128 /** 129 * Fetches the current state of the field trials. 130 */ 131 fetchTrialState(): Promise<FieldTrialState>; 132 133 /** 134 * Given a trial name, group name, or combination with a [/.-:] separator, 135 * returns any name hashes associated with that trial or group. 136 */ 137 lookupTrialOrGroupName(name: string): Promise<HashNameMap>; 138 139 /** 140 * Restarts the browser. 141 */ 142 restart(): Promise<void>; 143} 144 145export class MetricsInternalsBrowserProxyImpl implements 146 MetricsInternalsBrowserProxy { 147 getUmaLogData(includeLogProtoData: boolean): Promise<string> { 148 return sendWithPromise('fetchUmaLogsData', includeLogProtoData); 149 } 150 151 fetchVariationsSummary(): Promise<KeyValue[]> { 152 return sendWithPromise('fetchVariationsSummary'); 153 } 154 155 fetchUmaSummary(): Promise<KeyValue[]> { 156 return sendWithPromise('fetchUmaSummary'); 157 } 158 159 isUsingMetricsServiceObserver(): Promise<boolean> { 160 return sendWithPromise('isUsingMetricsServiceObserver'); 161 } 162 163 setTrialEnrollState( 164 trialHash: string, groupHash: string, 165 forceEnable: boolean): Promise<boolean> { 166 return sendWithPromise( 167 'setTrialEnrollState', trialHash, groupHash, forceEnable); 168 } 169 170 fetchTrialState(): Promise<FieldTrialState> { 171 return sendWithPromise('fetchTrialState'); 172 } 173 174 lookupTrialOrGroupName(name: string): Promise<HashNameMap> { 175 return sendWithPromise('lookupTrialOrGroupName', name); 176 } 177 178 restart(): Promise<void> { 179 return sendWithPromise('restart'); 180 } 181 182 static getInstance(): MetricsInternalsBrowserProxy { 183 return instance || (instance = new MetricsInternalsBrowserProxyImpl()); 184 } 185 186 static setInstance(obj: MetricsInternalsBrowserProxy) { 187 instance = obj; 188 } 189} 190 191let instance: MetricsInternalsBrowserProxy|null = null; 192