1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
content / browser / resources / traces_internals / trace_report.ts [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'chrome://resources/cr_elements/cr_icon_button/cr_icon_button.js';
import 'chrome://resources/cr_elements/icons.html.js';
import './icons.html.js';
import {assert} from 'chrome://resources/js/assert.js';
import {CrLitElement} from 'chrome://resources/lit/v3_0/lit.rollup.js';
import type {BigBuffer} from 'chrome://resources/mojo/mojo/public/mojom/base/big_buffer.mojom-webui.js';
import type {Time} from 'chrome://resources/mojo/mojo/public/mojom/base/time.mojom-webui.js';
import {getCss} from './trace_report.css.js';
import {getHtml} from './trace_report.html.js';
import type {ClientTraceReport} from './trace_report.mojom-webui.js';
import {ReportUploadState, SkipUploadReason} from './trace_report.mojom-webui.js';
import {TraceReportBrowserProxy} from './trace_report_browser_proxy.js';
import {Notification, NotificationType} from './trace_report_list.js';
// Create the temporary element here to hold the data to download the trace
// since it is only obtained after downloadData_ is called. This way we can
// perform a download directly in JS without touching the element that
// triggers the action. Initiate download a resource identified by |url| into
// |filename|.
function downloadUrl(fileName: string, url: string): void {
const a = document.createElement('a');
a.href = url;
a.download = fileName;
a.click();
}
export class TraceReportElement extends CrLitElement {
static get is() {
return 'trace-report';
}
static override get styles() {
return getCss();
}
override render() {
return getHtml.bind(this)();
}
static override get properties() {
return {
trace: {type: Object},
isLoading: {type: Boolean},
};
}
private traceReportProxy_: TraceReportBrowserProxy =
TraceReportBrowserProxy.getInstance();
protected trace: ClientTraceReport = {
// Dummy ClientTraceReport
uuid: {
high: 0n,
low: 0n,
},
creationTime: {internalValue: 0n},
scenarioName: '',
uploadRuleName: '',
totalSize: 0n,
uploadState: ReportUploadState.kNotUploaded,
uploadTime: {internalValue: 0n},
skipReason: SkipUploadReason.kNoSkip,
hasTraceContent: false,
};
protected isLoading_: boolean = false;
protected onCopyUuidClick_(): void {
// Get the text field
navigator.clipboard.writeText(this.getTokenAsString_());
}
protected getTraceSize_(): string {
if (this.trace.totalSize < 1) {
return '0 Bytes';
}
let displayedSize = Number(this.trace.totalSize);
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
let i = 0;
for (i; displayedSize >= k && i < 3; i++) {
displayedSize /= k;
}
return `${displayedSize.toFixed(2)} ${sizes[i]}`;
}
protected getSkipReason_(): string {
// Keep this in sync with the values of SkipUploadReason in
// tracereport.mojom
const skipReasonMap: string[] = [
'None',
'Size limit exceeded',
'Not anonymized',
'Scenario quota exceeded',
'Upload timed out',
];
return skipReasonMap[this.trace.skipReason] ??
'Could not get the skip reason';
}
protected onCopyScenarioClick_(): void {
// Get the text field
navigator.clipboard.writeText(this.trace.scenarioName);
}
protected onCopyUploadRuleClick_(): void {
// Get the text field
navigator.clipboard.writeText(this.trace.uploadRuleName);
}
protected isManualUploadPermitted_(): boolean {
return this.trace.skipReason !== SkipUploadReason.kNotAnonymized;
}
protected dateToString_(mojoTime: Time): string {
// The JS Date() is based off of the number of milliseconds since
// the UNIX epoch (1970-01-01 00::00:00 UTC), while |internalValue|
// of the base::Time (represented in mojom.Time) represents the
// number of microseconds since the Windows FILETIME epoch
// (1601-01-01 00:00:00 UTC). This computes the final JS time by
// computing the epoch delta and the conversion from microseconds to
// milliseconds.
const windowsEpoch = Date.UTC(1601, 0, 1, 0, 0, 0, 0);
const unixEpoch = Date.UTC(1970, 0, 1, 0, 0, 0, 0);
// |epochDeltaInMs| equals to
// base::Time::kTimeTToMicrosecondsOffset.
const epochDeltaInMs = unixEpoch - windowsEpoch;
const timeInMs = Number(mojoTime.internalValue) / 1000;
// Define the format in which the date string is going to be displayed.
return new Date(timeInMs - epochDeltaInMs)
.toLocaleString(
/*locales=*/ undefined, {
hour: 'numeric',
minute: 'numeric',
month: 'short',
day: 'numeric',
year: 'numeric',
hour12: true,
});
}
protected async onDownloadTraceClick_(): Promise<void> {
this.isLoading_ = true;
const {trace} =
await this.traceReportProxy_.handler.downloadTrace(this.trace.uuid);
if (trace !== null) {
this.downloadData_(`${this.getTokenAsString_()}.gz`, trace);
} else {
this.dispatchToast_(`Failed to download trace ${this.getTokenAsString_()}.`);
}
this.isLoading_ = false;
}
private downloadData_(fileName: string, data: BigBuffer): void {
if (data.invalidBuffer) {
this.dispatchToast_(
`Invalid buffer received for ${this.getTokenAsString_()}.`);
return;
}
try {
let bytes: Uint8Array;
if (Array.isArray(data.bytes)) {
bytes = new Uint8Array(data.bytes);
} else {
assert(!!data.sharedMemory, 'sharedMemory must be defined here');
const sharedMemory = data.sharedMemory!;
const {buffer, result} =
sharedMemory.bufferHandle.mapBuffer(0, sharedMemory.size);
assert(result === Mojo.RESULT_OK, 'Could not map buffer');
bytes = new Uint8Array(buffer);
}
const url = URL.createObjectURL(
new Blob([bytes], {type: 'application/octet-stream'}));
downloadUrl(fileName, url);
} catch (e) {
this.dispatchToast_(
`Unable to create blob from trace data for ${this.getTokenAsString_()}.`);
}
}
protected async onDeleteTraceClick_(): Promise<void> {
this.isLoading_ = true;
const {success} =
await this.traceReportProxy_.handler.deleteSingleTrace(this.trace.uuid);
if (!success) {
this.dispatchToast_(`Failed to delete ${this.getTokenAsString_()}.`);
} else {
this.dispatchReloadRequest_();
}
this.isLoading_ = false;
}
protected async onUploadTraceClick_(): Promise<void> {
this.isLoading_ = true;
const {success} =
await this.traceReportProxy_.handler.userUploadSingleTrace(
this.trace.uuid);
if (!success) {
this.dispatchToast_(`Failed to upload trace ${this.getTokenAsString_()}.`);
} else {
this.dispatchReloadRequest_();
}
this.isLoading_ = false;
}
protected uploadStateEqual_(state: ReportUploadState): boolean {
return this.trace.uploadState === state;
}
protected getTokenAsString_(): string {
return `${this.trace.uuid.high.toString(16)}-${
this.trace.uuid.low.toString(16)}`;
}
private dispatchToast_(message: string): void {
this.dispatchEvent(new CustomEvent('show-toast', {
bubbles: true,
composed: true,
detail: new Notification(NotificationType.ERROR, message),
}));
}
protected isDownloadDisabled_(): boolean {
return this.isLoading_ || !this.trace.hasTraceContent;
}
protected getDownloadTooltip_(): string {
return this.trace.hasTraceContent ? 'Download Trace' : 'Trace expired';
}
private dispatchReloadRequest_(): void {
this.fire('refresh-traces-request');
}
protected getStateCssClass_(): string {
switch (this.trace.uploadState) {
case ReportUploadState.kNotUploaded:
return 'state-default';
case ReportUploadState.kPending:
case ReportUploadState.kPending_UserRequested:
return 'state-pending';
case ReportUploadState.kUploaded:
return 'state-success';
default:
return '';
}
}
protected getStateText_(): string {
switch (this.trace.uploadState) {
case ReportUploadState.kNotUploaded:
return `Skip reason: ${this.getSkipReason_()}`;
case ReportUploadState.kPending:
return 'Pending upload';
case ReportUploadState.kPending_UserRequested:
return 'Pending upload: User requested';
case ReportUploadState.kUploaded:
return `Uploaded: ${this.dateToString_(this.trace.uploadTime)}`;
default:
return '';
}
}
}
declare global {
interface HTMLElementTagNameMap {
'trace-report': TraceReportElement;
}
}
customElements.define(TraceReportElement.is, TraceReportElement);