forked from google/perfetto
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
500 lines (453 loc) · 17.4 KB
/
index.ts
File metadata and controls
500 lines (453 loc) · 17.4 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// Copyright (C) 2018 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Keep this import first.
import z from 'zod';
import '../base/disposable_polyfill';
import '../base/static_initializers';
import NON_CORE_PLUGINS from '../gen/all_plugins';
import CORE_PLUGINS from '../gen/all_core_plugins';
import m from 'mithril';
import {defer} from '../base/deferred';
import {addErrorHandler, reportError} from '../base/logging';
import {featureFlags} from '../core/feature_flags';
import {initLiveReload} from '../core/live_reload';
import {raf} from '../core/raf_scheduler';
import {initWasm} from '../trace_processor/wasm_engine_proxy';
import {UiMain} from './ui_main';
import {registerDebugGlobals} from './debug';
import {maybeShowErrorDialog} from './error_dialog';
import {installFileDropHandler} from './file_drop_handler';
import {tryLoadIsInternalUserScript} from './is_internal_user_script_loader';
import {HomePage} from './home_page';
import {postMessageHandler} from './post_message_handler';
import {Route, Router} from '../core/router';
import {checkHttpRpcConnection} from './rpc_http_dialog';
import {maybeOpenTraceFromRoute} from './trace_url_handler';
import {renderViewerPage} from './viewer_page/viewer_page';
import {HttpRpcEngine} from '../trace_processor/http_rpc_engine';
import {setDefaultOwnerFunction, showModal} from '../widgets/modal';
import {IdleDetector} from './idle_detector';
import {IdleDetectorWindow} from './idle_detector_interface';
import {AppImpl} from '../core/app_impl';
import {addLegacyTableTab} from '../components/details/sql_table_tab';
import {configureExtensions} from '../components/extensions';
import {
addDebugCounterTrack,
addDebugSliceTrack,
} from '../components/tracks/debug_tracks';
import {addVisualizedArgTracks} from '../components/tracks/visualized_args_tracks';
import {addQueryResultsTab} from '../components/query_table/query_result_tab';
import {assetSrc, initAssets} from '../base/assets';
import {
PERFETTO_SETTINGS_STORAGE_KEY,
SettingsManagerImpl,
} from '../core/settings_manager';
import {LocalStorage} from '../core/local_storage';
import {DurationPrecision, TimestampFormat} from '../public/timeline';
import {timezoneOffsetMap} from '../base/time';
import {ThemeProvider} from './theme_provider';
import {OverlayContainer} from '../widgets/overlay_container';
import {JsonSettingsEditor} from '../components/json_settings_editor';
import {
CommandInvocation,
commandInvocationArraySchema,
} from '../core/command_manager';
import {HotkeyConfig, HotkeyContext} from '../widgets/hotkey_context';
import {embedderContext} from '../core/embedder';
const CSP_WS_PERMISSIVE_PORT = featureFlags.register({
id: 'cspAllowAnyWebsocketPort',
name: 'Relax Content Security Policy for 127.0.0.1:*',
description:
'Allows simultaneous usage of several trace_processor_shell ' +
'-D --http-port 1234 by opening ' +
'https://ui.perfetto.dev/#!/?rpc_port=1234',
defaultValue: false,
});
function routeChange(route: Route) {
raf.scheduleFullRedraw(() => {
if (route.fragment) {
// This needs to happen after the next redraw call. It's not enough
// to use setTimeout(..., 0); since that may occur before the
// redraw scheduled above.
const e = document.getElementById(route.fragment);
if (e) {
e.scrollIntoView();
}
}
});
maybeOpenTraceFromRoute(route);
}
function setupContentSecurityPolicy() {
// Note: self and sha-xxx must be quoted, urls data: and blob: must not.
let rpcPolicy = [
'http://127.0.0.1:9001', // For trace_processor_shell --httpd.
'ws://127.0.0.1:9001', // Ditto, for the websocket RPC.
'ws://127.0.0.1:9167', // For Web Device Proxy.
];
if (CSP_WS_PERMISSIVE_PORT.get()) {
const route = Router.parseUrl(window.location.href);
if (/^\d+$/.exec(route.args.rpc_port ?? '')) {
rpcPolicy = [
`http://127.0.0.1:${route.args.rpc_port}`,
`ws://127.0.0.1:${route.args.rpc_port}`,
];
}
}
const policy = {
'default-src': [
`'self'`,
// Google Tag Manager bootstrap.
`'sha256-LirUKeorCU4uRNtNzr8tlB11uy8rzrdmqHCX38JSwHY='`,
],
'script-src': [
`'self'`,
// TODO(b/201596551): this is required for Wasm after crrev.com/c/3179051
// and should be replaced with 'wasm-unsafe-eval'.
`'unsafe-eval'`,
'https://*.google.com',
'https://*.googleusercontent.com',
'https://www.googletagmanager.com',
'https://*.google-analytics.com',
],
'object-src': ['none'],
'connect-src': [
`'self'`,
'ws://127.0.0.1:8037', // For the adb websocket server.
'https://*.google-analytics.com',
'https://*.googleapis.com', // For Google Cloud Storage fetches.
'blob:',
'data:',
].concat(rpcPolicy),
'img-src': [
`'self'`,
'data:',
'blob:',
'https://*.google-analytics.com',
'https://www.googletagmanager.com',
'https://*.googleapis.com',
],
'style-src': [`'self'`, `'unsafe-inline'`],
'navigate-to': ['https://*.perfetto.dev', 'self'],
};
// Let embedders revise and install the policy as appropriate for their needs
if (embedderContext?.setContentSecurityPolicy) {
embedderContext.setContentSecurityPolicy(policy);
return;
}
const meta = document.createElement('meta');
meta.httpEquiv = 'Content-Security-Policy';
let policyStr = '';
for (const [key, list] of Object.entries(policy)) {
policyStr += `${key} ${list.join(' ')}; `;
}
meta.content = policyStr;
document.head.appendChild(meta);
}
function main() {
// Setup content security policy before anything else.
setupContentSecurityPolicy();
initAssets();
// Create settings Manager
const settingsManager = new SettingsManagerImpl(
new LocalStorage(PERFETTO_SETTINGS_STORAGE_KEY),
);
// Initialize core settings...
const timestampFormatSetting = settingsManager.register({
id: 'timestampFormat',
name: 'Timestamp format',
description: 'The format of timestamps throughout Perfetto.',
schema: z.nativeEnum(TimestampFormat),
defaultValue: TimestampFormat.Timecode,
});
const timezoneOverrideSetting = settingsManager.register({
id: 'timezoneOverride',
name: 'Timezone Override',
description:
"When 'Timestamp Format' is set to 'CustomTimezone', this setting controls which timezone is used.",
schema: z.enum(Object.keys(timezoneOffsetMap) as [string, ...string[]]),
defaultValue: '(UTC+00:00) London, Dublin, Lisbon, Casablanca', // UTC by default.
});
const durationPrecisionSetting = settingsManager.register({
id: 'durationPrecision',
name: 'Duration precision',
description: 'The precision of durations throughout Perfetto.',
schema: z.nativeEnum(DurationPrecision),
defaultValue: DurationPrecision.Full,
});
const analyticsSetting = settingsManager.register({
id: 'analyticsEnable',
name: 'Enable UI telemetry',
description: `
This setting controls whether the Perfetto UI logs coarse-grained
information about your usage of the UI and any errors encountered. This
information helps us understand how the UI is being used and allows us to
better prioritise features and fix bugs. If this option is disabled,
no information will be logged.
Note: even if this option is enabled, information about the *contents* of
traces is *not* logged.
Note: this setting only has an effect on the ui.perfetto.dev and localhost
origins: all other origins do not log telemetry even if this option is
enabled.
`,
schema: z.boolean(),
defaultValue: true,
requiresReload: true,
});
const startupCommandsEditor = new JsonSettingsEditor<CommandInvocation[]>({
schema: commandInvocationArraySchema,
});
const startupCommandsSetting = settingsManager.register({
id: 'startupCommands',
name: 'Startup Commands',
description: `
Commands to run automatically after a trace loads and any saved state is
restored. These commands execute as if a user manually invoked them after
the trace is fully ready, making them ideal for automating common
post-load actions like running queries, expanding tracks, or setting up
custom views.
`,
schema: commandInvocationArraySchema,
defaultValue: [],
render: (setting) => startupCommandsEditor.render(setting),
});
const enforceStartupCommandAllowlistSetting = settingsManager.register({
id: 'enforceStartupCommandAllowlist',
name: 'Enforce Startup Command Allowlist',
description: `
When enabled, only commands in the predefined allowlist can be executed
as startup commands. When disabled, all startup commands will be
executed without filtering.
The command allowlist encodes the set of commands which Perfetto UI
maintainers expect to maintain backwards compatibility for the forseeable\
future.
WARNING: if this setting is disabled, any command outside the allowlist
has *no* backwards compatibility guarantees and is can change without
warning at any time.
`,
schema: z.boolean(),
defaultValue: true,
});
AppImpl.initialize({
initialRouteArgs: Router.parseUrl(window.location.href).args,
settingsManager,
timestampFormatSetting,
durationPrecisionSetting,
timezoneOverrideSetting,
analyticsSetting,
startupCommandsSetting,
enforceStartupCommandAllowlistSetting,
});
setDefaultOwnerFunction(() => AppImpl.instance.trace ?? AppImpl.instance);
// Load the css. The load is asynchronous and the CSS is not ready by the time
// appendChild returns.
const cssLoadPromise = defer<void>();
const css = document.createElement('link');
css.rel = 'stylesheet';
css.href = assetSrc('perfetto.css');
css.onload = () => cssLoadPromise.resolve();
css.onerror = (err) => cssLoadPromise.reject(err);
const favicon = document.head.querySelector('#favicon');
if (favicon instanceof HTMLLinkElement) {
favicon.href = assetSrc('assets/favicon.png');
}
document.head.append(css);
// Load the script to detect if this is a Googler (see comments on globals.ts)
// and initialize GA after that (or after a timeout if something goes wrong).
const app = AppImpl.instance;
tryLoadIsInternalUserScript(app).then(() => {
app.analytics.initialize(app.isInternalUser);
app.notifyOnExtrasLoadingCompleted();
});
if (embedderContext?.suppressErrorHandling !== true) {
// Route errors to both the UI bugreport dialog and Analytics (if enabled).
addErrorHandler(maybeShowErrorDialog);
addErrorHandler((e) => AppImpl.instance.analytics.logError(e));
// Add Error handlers for JS error and for uncaught exceptions in promises.
window.addEventListener('error', (e) => reportError(e));
window.addEventListener('unhandledrejection', (e) => reportError(e));
}
initWasm();
AppImpl.instance.serviceWorkerController.install();
// Put debug variables in the global scope for better debugging.
registerDebugGlobals();
// Prevent pinch zoom.
document.body.addEventListener(
'wheel',
(e: MouseEvent) => {
if (e.ctrlKey) e.preventDefault();
},
{passive: false},
);
cssLoadPromise.then(() => onCssLoaded());
if (AppImpl.instance.testingMode) {
document.body.classList.add('testing');
}
(window as {} as IdleDetectorWindow).waitForPerfettoIdle = (ms?: number) => {
return new IdleDetector().waitForPerfettoIdle(ms);
};
}
function onCssLoaded() {
if (!embedderContext?.suppressMainUi) {
// Clear all the contents of the initial page (e.g. the <pre> error message)
// And replace it with the root <main> element which will be used by mithril.
document.body.innerHTML = '';
document.body.classList.add('perfetto');
}
const pages = AppImpl.instance.pages;
pages.registerPage({route: '/', render: () => m(HomePage)});
pages.registerPage({
route: '/viewer',
renderPage: ({trace}) => renderViewerPage(trace),
});
const router = new Router();
router.onRouteChanged = routeChange;
const themeSetting = AppImpl.instance.settings.register({
id: 'theme',
name: '[Experimental] UI Theme',
description: 'Warning: Dark mode is not fully supported yet.',
schema: z.enum(['dark', 'light']),
defaultValue: 'light',
});
AppImpl.instance.settings.register({
id: 'track-height-min-px',
name: 'Track Height',
description:
'Minimum height of tracks in the trace viewer page, in pixels.',
schema: z.number().int().min(18),
defaultValue: 18,
});
// Add command to toggle the theme.
AppImpl.instance.commands.registerCommand({
id: 'dev.perfetto.ToggleTheme',
name: '[Experimental] Toggle UI Theme',
callback: () => {
const currentTheme = themeSetting.get();
themeSetting.set(currentTheme === 'dark' ? 'light' : 'dark');
},
});
if (!embedderContext?.suppressMainUi) {
// Mount the main mithril component. This also forces a sync render pass.
raf.mount(document.body, {
view: () => {
const app = AppImpl.instance.trace ?? AppImpl.instance;
const commands = app.commands;
const hotkeys: HotkeyConfig[] = [];
for (const {id, defaultHotkey} of commands.commands) {
if (defaultHotkey) {
hotkeys.push({
callback: () => commands.runCommand(id),
hotkey: defaultHotkey,
});
}
}
return m(
ThemeProvider,
{theme: themeSetting.get() as 'dark' | 'light'},
[
m(HotkeyContext, {hotkeys, fillHeight: true, autoFocus: true}, [
m(OverlayContainer, {fillParent: true}, [
m(UiMain, {key: themeSetting.get()}),
]),
]),
],
);
},
});
}
if (
(location.origin.startsWith('http://localhost:') ||
location.origin.startsWith('http://127.0.0.1:')) &&
!AppImpl.instance.embeddedMode &&
!AppImpl.instance.testingMode
) {
initLiveReload();
}
// Will update the chip on the sidebar footer that notifies that the RPC is
// connected. Has no effect on the controller (which will repeat this check
// before creating a new engine).
// Don't auto-open any trace URLs until we get a response here because we may
// accidentially clober the state of an open trace processor instance
// otherwise.
maybeChangeRpcPortFromFragment();
const source = AppImpl.instance.trace?.traceInfo.source;
const port =
(source?.type === 'HTTP_RPC' && source?.port) ||
HttpRpcEngine.defaultRpcPort;
checkHttpRpcConnection(port).then(() => {
const route = Router.parseUrl(window.location.href);
if (!AppImpl.instance.embeddedMode) {
installFileDropHandler();
}
// Don't allow postMessage or opening trace from route when the user says
// that they want to reuse the already loaded trace in trace processor.
const traceSource = AppImpl.instance.trace?.traceInfo.source;
if (traceSource && traceSource.type === 'HTTP_RPC') {
return;
}
// Add support for opening traces from postMessage().
window.addEventListener('message', postMessageHandler, {passive: true});
// Handles the initial ?local_cache_key=123 or ?s=permalink or ?url=...
// cases.
routeChange(route);
});
// Initialize plugins, now that we are ready to go.
const pluginManager = AppImpl.instance.plugins;
CORE_PLUGINS.forEach((p) => pluginManager.registerPlugin(p));
NON_CORE_PLUGINS.forEach((p) => pluginManager.registerPlugin(p));
const route = Router.parseUrl(window.location.href);
const overrides = (route.args.enablePlugins ?? '').split(',');
pluginManager.activatePlugins(overrides);
}
// If the URL is /#!?rpc_port=1234, change the default RPC port.
// For security reasons, this requires toggling a flag. Detect this and tell the
// user what to do in this case.
function maybeChangeRpcPortFromFragment() {
const route = Router.parseUrl(window.location.href);
if (route.args.rpc_port !== undefined) {
if (!CSP_WS_PERMISSIVE_PORT.get()) {
showModal({
title: 'Using a different port requires a flag change',
content: m(
'div',
m(
'span',
'For security reasons before connecting to a non-standard ' +
'TraceProcessor port you need to manually enable the flag to ' +
'relax the Content Security Policy and restart the UI.',
),
),
buttons: [
{
text: 'Take me to the flags page',
primary: true,
action: () => Router.navigate('#!/flags/cspAllowAnyWebsocketPort'),
},
],
});
} else {
HttpRpcEngine.defaultRpcPort = route.args.rpc_port;
}
}
}
// TODO(primiano): this injection is to break a cirular dependency. See
// comment in sql_table_tab_interface.ts. Remove once we add an extension
// point for context menus.
configureExtensions({
addDebugCounterTrack,
addDebugSliceTrack,
addVisualizedArgTracks,
addLegacySqlTableTab: addLegacyTableTab,
addQueryResultsTab,
});
main();