forked from catapult-project/catapult
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
394 lines (333 loc) · 12.1 KB
/
main.js
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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/**
* Dictionary of constants (Initialized soon after loading by data from browser,
* updated on load log). The *Types dictionaries map strings to numeric IDs,
* while the *TypeNames are the other way around.
*/
let EventType = null;
let EventTypeNames = null;
let EventPhase = null;
let EventSourceType = null;
let EventSourceTypeNames = null;
let ClientInfo = null;
let NetError = null;
let QuicError = null;
let QuicRstStreamError = null;
let LoadFlag = null;
let CertStatusFlag = null;
let LoadState = null;
let AddressFamily = null;
let DataReductionProxyBypassEventType = null;
let DataReductionProxyBypassActionType = null;
/**
* Dictionary of all constants, used for saving log files.
*/
let Constants = null;
/**
* Object to communicate between the renderer and the browser.
* @type {!BrowserBridge}
*/
let g_browser = null;
/**
* This class is the root view object of the page. It owns all the other
* views, and manages switching between them. It is also responsible for
* initializing the views and the BrowserBridge.
*/
const MainView = (function() {
'use strict';
// We inherit from WindowView
const superClass = WindowView;
/**
* Main entry point. Called once the page has loaded.
* @constructor
*/
function MainView() {
assertFirstConstructorCall(MainView);
if (hasTouchScreen()) {
document.body.classList.add('touch');
}
// This must be initialized before the tabs, so they can register as
// observers.
g_browser = BrowserBridge.getInstance();
// This must be the first constants observer, so other constants observers
// can safely use the globals, rather than depending on walking through
// the constants themselves.
g_browser.addConstantsObserver(new ConstantsObserver());
// Create the tab switcher.
this.initTabs_();
// Cut out a small vertical strip at the top of the window, to display
// a high level status (i.e. if we are displaying a log file or not).
// Below it we will position the main tabs and their content area.
this.topBarView_ = TopBarView.getInstance(this);
const verticalSplitView =
new VerticalSplitView(this.topBarView_, this.tabSwitcher_);
superClass.call(this, verticalSplitView);
// Trigger initial layout.
this.resetGeometry();
window.onhashchange = this.onUrlHashChange_.bind(this);
// Select the initial view based on the current URL.
window.onhashchange();
// No log file loaded yet so set the status bar to that state.
this.topBarView_.switchToSubView('loaded').setFileName(
'No log to display.');
// TODO(rayraymond): Follow-up is to completely remove all code from
// g_browser that interacts with sending/receiving messages from
// browser.
g_browser.disable();
}
cr.addSingletonGetter(MainView);
// Tracks if we're viewing a loaded log file, so views can behave
// appropriately. Global so safe to call during construction.
let isViewingLoadedLog = false;
MainView.isViewingLoadedLog = function() {
return isViewingLoadedLog;
};
MainView.prototype = {
// Inherit the superclass's methods.
__proto__: superClass.prototype,
// This is exposed both so the log import/export code can enumerate all the
// tabs, and for testing.
tabSwitcher() {
return this.tabSwitcher_;
},
/**
* Prevents receiving/sending events to/from the browser, so loaded data
* will not be mixed with current Chrome state. Also hides any interactive
* HTML elements that send messages to the browser. Cannot be undone
* without reloading the page. Must be called before passing loaded data
* to the individual views.
*
* @param {string} opt_fileName The name of the log file that has been
* loaded, if we're loading a log file.
*/
onLoadLog(opt_fileName) {
isViewingLoadedLog = true;
if (opt_fileName !== undefined) {
// If there's a file name, a log file was loaded, so display the
// file's name in the status bar.
this.topBarView_.switchToSubView('loaded').setFileName(opt_fileName);
}
},
switchToViewOnlyMode() {
// Since this won't be dumped to a file, we don't want to remove
// cookies and credentials.
log_util.createLogDumpAsync('', log_util.loadLogFile, false);
},
initTabs_() {
this.tabIdToHash_ = {};
this.hashToTabId_ = {};
this.tabSwitcher_ = new TabSwitcherView(this.onTabSwitched_.bind(this));
// Helper function to add a tab given the class for a view singleton.
const addTab = function(viewClass) {
const tabId = viewClass.TAB_ID;
const tabHash = viewClass.TAB_HASH;
const tabName = viewClass.TAB_NAME;
const view = viewClass.getInstance();
if (!tabId || !view || !tabHash || !tabName) {
throw Error('Invalid view class for tab');
}
if (tabHash.charAt(0) !== '#') {
throw Error('Tab hashes must start with a #');
}
this.tabSwitcher_.addTab(tabId, view, tabName, tabHash);
this.tabIdToHash_[tabId] = tabHash;
this.hashToTabId_[tabHash] = tabId;
}.bind(this);
// Populate the main tabs. Even tabs that don't contain information for
// the running OS should be created, so they can load log dumps from other
// OSes.
addTab(ImportView);
addTab(ProxyView);
addTab(EventsView);
addTab(TimelineView);
addTab(DnsView);
addTab(SocketsView);
addTab(AltSvcView);
addTab(SpdyView);
addTab(QuicView);
addTab(ReportingView);
addTab(HttpCacheView);
addTab(ModulesView);
addTab(BandwidthView);
addTab(PrerenderView);
addTab(CrosView);
// Tab links start off hidden (besides import) since a log file has not
// been loaded yet. This must be done after all the tabs are added so
// that the width of the tab-list div is correctly styled.
for (const tabId in this.tabSwitcher_.getAllTabViews()) {
if (tabId !== ImportView.TAB_ID) {
this.tabSwitcher_.showTabLink(tabId, false);
}
}
this.tabSwitcher_.showTabLink(CrosView.TAB_ID, cr.isChromeOS);
},
/**
* This function is called by the tab switcher when the current tab has been
* changed. It will update the current URL to reflect the new active tab,
* so the back can be used to return to previous view.
*/
onTabSwitched_(oldTabId, newTabId) {
// Update data needed by newly active tab, as it may be
// significantly out of date.
if (g_browser) {
g_browser.checkForUpdatedInfo();
}
// Change the URL to match the new tab.
const newTabHash = this.tabIdToHash_[newTabId];
const parsed = parseUrlHash_(window.location.hash);
if (parsed.tabHash !== newTabHash) {
window.location.hash = newTabHash;
}
},
onUrlHashChange_() {
const parsed = parseUrlHash_(window.location.hash);
if (!parsed) {
return;
}
if (!parsed.tabHash) {
// Default to the import tab.
parsed.tabHash = ImportView.TAB_HASH;
}
const tabId = this.hashToTabId_[parsed.tabHash];
if (tabId) {
this.tabSwitcher_.switchToTab(tabId);
if (parsed.parameters) {
const view = this.tabSwitcher_.getTabView(tabId);
view.setParameters(parsed.parameters);
}
}
},
};
/**
* Takes the current hash in form of "#tab¶m1=value1¶m2=value2&..."
* and parses it into a dictionary.
*
* Parameters and values are decoded with decodeURIComponent().
*/
function parseUrlHash_(hash) {
const parameters = hash.split('&');
let tabHash = parameters[0];
if (tabHash === '' || tabHash === '#') {
tabHash = undefined;
}
// Split each string except the first around the '='.
let paramDict = null;
for (let i = 1; i < parameters.length; i++) {
const paramStrings = parameters[i].split('=');
if (paramStrings.length !== 2) {
continue;
}
if (paramDict === null) {
paramDict = {};
}
const key = decodeURIComponent(paramStrings[0]);
const value = decodeURIComponent(paramStrings[1]);
paramDict[key] = value;
}
return {tabHash, parameters: paramDict};
}
return MainView;
})();
function ConstantsObserver() {}
/**
* Loads all constants from |constants|. On failure, global dictionaries are
* not modifed.
* @param {Object} receivedConstants The map of received constants.
*/
ConstantsObserver.prototype.onReceivedConstants = function(receivedConstants) {
if (!areValidConstants(receivedConstants)) {
return;
}
Constants = receivedConstants;
EventType = Constants.logEventTypes;
EventTypeNames = makeInverseMap(EventType);
EventPhase = Constants.logEventPhase;
EventSourceType = Constants.logSourceType;
EventSourceTypeNames = makeInverseMap(EventSourceType);
ClientInfo = Constants.clientInfo;
LoadFlag = Constants.loadFlag;
NetError = Constants.netError;
QuicError = Constants.quicError;
QuicRstStreamError = Constants.quicRstStreamError;
AddressFamily = Constants.addressFamily;
LoadState = Constants.loadState;
DataReductionProxyBypassEventType =
Constants.dataReductionProxyBypassEventType;
DataReductionProxyBypassActionType =
Constants.dataReductionProxyBypassActionType;
// certStatusFlag may not be present when loading old log Files
if (typeof(Constants.certStatusFlag) === 'object') {
CertStatusFlag = Constants.certStatusFlag;
} else {
CertStatusFlag = {};
}
timeutil.setTimeTickOffset(Constants.timeTickOffset);
};
function isNetLogNumber(value) {
return (typeof(value) === 'number') ||
(typeof(value) === 'string' && !isNaN(parseInt(value)));
}
/**
* Returns true if it's given a valid-looking constants object.
* @param {Object} receivedConstants The received map of constants.
* @return {boolean} True if the |receivedConstants| object appears valid.
*/
function areValidConstants(receivedConstants) {
return typeof(receivedConstants) === 'object' &&
typeof(receivedConstants.logEventTypes) === 'object' &&
typeof(receivedConstants.clientInfo) === 'object' &&
typeof(receivedConstants.logEventPhase) === 'object' &&
typeof(receivedConstants.logSourceType) === 'object' &&
typeof(receivedConstants.loadFlag) === 'object' &&
typeof(receivedConstants.netError) === 'object' &&
typeof(receivedConstants.addressFamily) === 'object' &&
isNetLogNumber(receivedConstants.timeTickOffset) &&
typeof(receivedConstants.logFormatVersion) === 'number';
}
/**
* Returns the name for netError.
*
* Example: netErrorToString(-105) should return
* "ERR_NAME_NOT_RESOLVED".
* @param {number} netError The net error code.
* @return {string} The name of the given error.
*/
function netErrorToString(netError) {
return getKeyWithValue(NetError, netError);
}
/**
* Returns the name for quicError.
*
* Example: quicErrorToString(25) should return
* "TIMED_OUT".
* @param {number} quicError The QUIC error code.
* @return {string} The name of the given error.
*/
function quicErrorToString(quicError) {
return getKeyWithValue(QuicError, quicError);
}
/**
* Returns the name for quicRstStreamError.
*
* Example: quicRstStreamErrorToString(3) should return
* "BAD_APPLICATION_PAYLOAD".
* @param {number} quicRstStreamError The QUIC RST_STREAM error code.
* @return {string} The name of the given error.
*/
function quicRstStreamErrorToString(quicRstStreamError) {
return getKeyWithValue(QuicRstStreamError, quicRstStreamError);
}
/**
* Returns a string representation of |family|.
* @param {number} family An AddressFamily
* @return {string} A representation of the given family.
*/
function addressFamilyToString(family) {
const str = getKeyWithValue(AddressFamily, family);
// All the address family start with ADDRESS_FAMILY_*.
// Strip that prefix since it is redundant and only clutters the output.
return str.replace(/^ADDRESS_FAMILY_/, '');
}