Web Inspector: convert workers inspection into capability
[WebKit-https.git] / Source / WebCore / inspector / front-end / inspector.js
1 /*
2  * Copyright (C) 2006, 2007, 2008 Apple Inc.  All rights reserved.
3  * Copyright (C) 2007 Matt Lilek (pewtermoose@gmail.com).
4  * Copyright (C) 2009 Joseph Pecoraro
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1.  Redistributions of source code must retain the above copyright
11  *     notice, this list of conditions and the following disclaimer.
12  * 2.  Redistributions in binary form must reproduce the above copyright
13  *     notice, this list of conditions and the following disclaimer in the
14  *     documentation and/or other materials provided with the distribution.
15  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
16  *     its contributors may be used to endorse or promote products derived
17  *     from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
20  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 var WebInspector = {
32     _panelDescriptors: function()
33     {
34         this.panels = {};
35         WebInspector.inspectorView = new WebInspector.InspectorView();
36         var parentElement = document.getElementById("main");
37         WebInspector.inspectorView.show(parentElement);
38         WebInspector.inspectorView.addEventListener(WebInspector.InspectorView.Events.PanelSelected, this._panelSelected, this);
39
40         var elements = new WebInspector.ElementsPanelDescriptor();
41         var resources = new WebInspector.PanelDescriptor("resources", WebInspector.UIString("Resources"), "ResourcesPanel", "ResourcesPanel.js");
42         var network = new WebInspector.NetworkPanelDescriptor();
43         var scripts = new WebInspector.ScriptsPanelDescriptor();
44         var timeline = new WebInspector.TimelinePanelDescriptor();
45         var profiles = new WebInspector.ProfilesPanelDescriptor();
46         var audits = new WebInspector.PanelDescriptor("audits", WebInspector.UIString("Audits"), "AuditsPanel", "AuditsPanel.js");
47         var console = new WebInspector.PanelDescriptor("console", WebInspector.UIString("Console"), "ConsolePanel");
48         var allDescriptors = [elements, resources, network, scripts, timeline, profiles, audits, console];
49         var allProfilers = [profiles];
50         if (WebInspector.experimentsSettings.separateProfilers.isEnabled()) {
51             allProfilers = [];
52             allProfilers.push(new WebInspector.PanelDescriptor("cpu-profiler", WebInspector.UIString("CPU Profiler"), "CPUProfilerPanel", "ProfilesPanel.js"));
53             if (!WebInspector.WorkerManager.isWorkerFrontend())
54                 allProfilers.push(new WebInspector.PanelDescriptor("css-profiler", WebInspector.UIString("CSS Profiler"), "CSSSelectorProfilerPanel", "ProfilesPanel.js"));
55             if (Capabilities.heapProfilerPresent)
56                 allProfilers.push(new WebInspector.PanelDescriptor("heap-profiler", WebInspector.UIString("Heap Profiler"), "HeapProfilerPanel", "ProfilesPanel.js"));
57             if (!WebInspector.WorkerManager.isWorkerFrontend() && WebInspector.experimentsSettings.canvasInspection.isEnabled())
58                 allProfilers.push(new WebInspector.PanelDescriptor("canvas-profiler", WebInspector.UIString("Canvas Profiler"), "CanvasProfilerPanel", "ProfilesPanel.js"));
59             if (!WebInspector.WorkerManager.isWorkerFrontend() && WebInspector.experimentsSettings.nativeMemorySnapshots.isEnabled()) {
60                 allProfilers.push(new WebInspector.PanelDescriptor("memory-chart-profiler", WebInspector.UIString("Memory Distribution"), "MemoryChartProfilerPanel", "ProfilesPanel.js"));
61                 allProfilers.push(new WebInspector.PanelDescriptor("memory-snapshot-profiler", WebInspector.UIString("Memory Snapshots"), "NativeMemoryProfilerPanel", "ProfilesPanel.js"));
62             }
63             Array.prototype.splice.bind(allDescriptors, allDescriptors.indexOf(profiles), 1).apply(null, allProfilers);
64         }
65
66         var panelDescriptors = [];
67         if (WebInspector.WorkerManager.isWorkerFrontend()) {
68             panelDescriptors.push(scripts);
69             panelDescriptors.push(timeline);
70             panelDescriptors = panelDescriptors.concat(allProfilers);
71             panelDescriptors.push(console);
72             return panelDescriptors;
73         }
74         var hiddenPanels = InspectorFrontendHost.hiddenPanels();
75         for (var i = 0; i < allDescriptors.length; ++i) {
76             if (hiddenPanels.indexOf(allDescriptors[i].name()) === -1)
77                 panelDescriptors.push(allDescriptors[i]);
78         }
79         return panelDescriptors;
80     },
81
82     _panelSelected: function()
83     {
84         this._toggleConsoleButton.setEnabled(WebInspector.inspectorView.currentPanel().name !== "console");
85     },
86
87     _createGlobalStatusBarItems: function()
88     {
89         var bottomStatusBarContainer = document.getElementById("bottom-status-bar-container");
90
91         // Create main dock button and options.
92         var mainStatusBar = document.getElementById("main-status-bar");
93         mainStatusBar.insertBefore(this.dockController.element, bottomStatusBarContainer);
94
95         this._toggleConsoleButton = new WebInspector.StatusBarButton(WebInspector.UIString("Show console."), "console-status-bar-item");
96         this._toggleConsoleButton.addEventListener("click", this._toggleConsoleButtonClicked.bind(this), false);
97         mainStatusBar.insertBefore(this._toggleConsoleButton.element, bottomStatusBarContainer);
98
99         if (!WebInspector.WorkerManager.isWorkerFrontend()) {
100             this._nodeSearchButton = new WebInspector.StatusBarButton(WebInspector.UIString("Select an element in the page to inspect it."), "node-search-status-bar-item");
101             this._nodeSearchButton.addEventListener("click", this.toggleSearchingForNode, this);
102             mainStatusBar.insertBefore(this._nodeSearchButton.element, bottomStatusBarContainer);
103         }
104
105         mainStatusBar.appendChild(this.settingsController.statusBarItem);
106     },
107
108     _toggleConsoleButtonClicked: function()
109     {
110         if (!this._toggleConsoleButton.enabled())
111             return;
112
113         this._toggleConsoleButton.toggled = !this._toggleConsoleButton.toggled;
114
115         var animationType = window.event && window.event.shiftKey ? WebInspector.Drawer.AnimationType.Slow : WebInspector.Drawer.AnimationType.Normal;
116         if (this._toggleConsoleButton.toggled) {
117             this._toggleConsoleButton.title = WebInspector.UIString("Hide console.");
118             this.drawer.show(this.consoleView, animationType);
119             this._consoleWasShown = true;
120         } else {
121             this._toggleConsoleButton.title = WebInspector.UIString("Show console.");
122             this.drawer.hide(animationType);
123             delete this._consoleWasShown;
124         }
125     },
126
127     /**
128      * @param {Element} statusBarElement
129      * @param {WebInspector.View} view
130      * @param {function()=} onclose
131      */
132     showViewInDrawer: function(statusBarElement, view, onclose)
133     {
134         this._toggleConsoleButton.title = WebInspector.UIString("Hide console.");
135         this._toggleConsoleButton.toggled = false;
136         this._closePreviousDrawerView();
137
138         var drawerStatusBarHeader = document.createElement("div");
139         drawerStatusBarHeader.className = "drawer-header status-bar-item";
140         drawerStatusBarHeader.appendChild(statusBarElement);
141         drawerStatusBarHeader.onclose = onclose;
142
143         var closeButton = drawerStatusBarHeader.createChild("span");
144         closeButton.textContent = WebInspector.UIString("\u00D7");
145         closeButton.addStyleClass("drawer-header-close-button");
146         closeButton.addEventListener("click", this.closeViewInDrawer.bind(this), false);
147
148         var panelStatusBar = document.getElementById("panel-status-bar");
149         var drawerViewAnchor = document.getElementById("drawer-view-anchor");
150         panelStatusBar.insertBefore(drawerStatusBarHeader, drawerViewAnchor);
151         this._drawerStatusBarHeader = drawerStatusBarHeader;
152         this.drawer.show(view, WebInspector.Drawer.AnimationType.Immediately);
153     },
154
155     closeViewInDrawer: function()
156     {
157         if (this._drawerStatusBarHeader) {
158             this._closePreviousDrawerView();
159
160             // Once drawer is closed console should be shown if it was shown before current view replaced it in drawer. 
161             if (!this._consoleWasShown)
162                 this.drawer.hide(WebInspector.Drawer.AnimationType.Immediately);
163             else
164                 this._toggleConsoleButtonClicked();
165         }
166     },
167
168     _closePreviousDrawerView: function()
169     {
170         if (this._drawerStatusBarHeader) {
171             this._drawerStatusBarHeader.parentElement.removeChild(this._drawerStatusBarHeader);
172             if (this._drawerStatusBarHeader.onclose)
173                 this._drawerStatusBarHeader.onclose();
174             delete this._drawerStatusBarHeader;
175         }
176     },
177
178     _updateErrorAndWarningCounts: function()
179     {
180         var errorWarningElement = document.getElementById("error-warning-count");
181         if (!errorWarningElement)
182             return;
183
184         var errors = WebInspector.console.errors;
185         var warnings = WebInspector.console.warnings;
186         if (!errors && !warnings) {
187             errorWarningElement.addStyleClass("hidden");
188             return;
189         }
190
191         errorWarningElement.removeStyleClass("hidden");
192
193         errorWarningElement.removeChildren();
194
195         if (errors) {
196             var errorImageElement = document.createElement("img");
197             errorImageElement.id = "error-count-img";
198             errorWarningElement.appendChild(errorImageElement);
199             var errorElement = document.createElement("span");
200             errorElement.id = "error-count";
201             errorElement.textContent = errors;
202             errorWarningElement.appendChild(errorElement);
203         }
204
205         if (warnings) {
206             var warningsImageElement = document.createElement("img");
207             warningsImageElement.id = "warning-count-img";
208             errorWarningElement.appendChild(warningsImageElement);
209             var warningsElement = document.createElement("span");
210             warningsElement.id = "warning-count";
211             warningsElement.textContent = warnings;
212             errorWarningElement.appendChild(warningsElement);
213         }
214
215         if (errors) {
216             if (warnings) {
217                 if (errors == 1) {
218                     if (warnings == 1)
219                         errorWarningElement.title = WebInspector.UIString("%d error, %d warning", errors, warnings);
220                     else
221                         errorWarningElement.title = WebInspector.UIString("%d error, %d warnings", errors, warnings);
222                 } else if (warnings == 1)
223                     errorWarningElement.title = WebInspector.UIString("%d errors, %d warning", errors, warnings);
224                 else
225                     errorWarningElement.title = WebInspector.UIString("%d errors, %d warnings", errors, warnings);
226             } else if (errors == 1)
227                 errorWarningElement.title = WebInspector.UIString("%d error", errors);
228             else
229                 errorWarningElement.title = WebInspector.UIString("%d errors", errors);
230         } else if (warnings == 1)
231             errorWarningElement.title = WebInspector.UIString("%d warning", warnings);
232         else if (warnings)
233             errorWarningElement.title = WebInspector.UIString("%d warnings", warnings);
234         else
235             errorWarningElement.title = null;
236     },
237
238     get inspectedPageDomain()
239     {
240         var parsedURL = WebInspector.inspectedPageURL && WebInspector.inspectedPageURL.asParsedURL();
241         return parsedURL ? parsedURL.host : "";
242     },
243
244     _initializeCapability: function(name, callback, error, result)
245     {
246         Capabilities[name] = result;
247         if (callback)
248             callback();
249     },
250
251     _zoomIn: function()
252     {
253         this._zoomLevel = Math.min(this._zoomLevel + 1, WebInspector.Zoom.Table.length - WebInspector.Zoom.DefaultOffset - 1);
254         this._requestZoom();
255     },
256
257     _zoomOut: function()
258     {
259         this._zoomLevel = Math.max(this._zoomLevel - 1, -WebInspector.Zoom.DefaultOffset);
260         this._requestZoom();
261     },
262
263     _resetZoom: function()
264     {
265         this._zoomLevel = 0;
266         this._requestZoom();
267     },
268
269     _requestZoom: function()
270     {
271         WebInspector.settings.zoomLevel.set(this._zoomLevel);
272         // For backwards compatibility, zoomLevel takes integers (with 0 being default zoom).
273         var index = this._zoomLevel + WebInspector.Zoom.DefaultOffset;
274         index = Math.min(WebInspector.Zoom.Table.length - 1, index);
275         index = Math.max(0, index);
276         InspectorFrontendHost.setZoomFactor(WebInspector.Zoom.Table[index]);
277     },
278
279     toggleSearchingForNode: function()
280     {
281         var enabled = !this._nodeSearchButton.toggled;
282         /**
283          * @param {?Protocol.Error} error
284          */
285         function callback(error)
286         {
287             if (!error)
288                 this._nodeSearchButton.toggled = enabled;
289         }
290         WebInspector.domAgent.setInspectModeEnabled(enabled, callback.bind(this));
291     },
292
293     _debuggerPaused: function()
294     {
295         // Create scripts panel upon demand.
296         WebInspector.panel("scripts");
297     }
298 }
299
300 WebInspector.Events = {
301     InspectorLoaded: "InspectorLoaded",
302     InspectorClosing: "InspectorClosing"
303 }
304
305 {(function parseQueryParameters()
306 {
307     WebInspector.queryParamsObject = {};
308     var queryParams = window.location.search;
309     if (!queryParams)
310         return;
311     var params = queryParams.substring(1).split("&");
312     for (var i = 0; i < params.length; ++i) {
313         var pair = params[i].split("=");
314         WebInspector.queryParamsObject[pair[0]] = pair[1];
315     }
316 })();}
317
318 WebInspector.suggestReload = function()
319 {
320     if (window.confirm(WebInspector.UIString("It is recommended to restart inspector after making these changes. Would you like to restart it?")))
321         this.reload();
322 }
323
324 WebInspector.reload = function()
325 {
326     var queryParams = window.location.search;
327     var url = window.location.href;
328     url = url.substring(0, url.length - queryParams.length);
329     var queryParamsObject = {};
330     for (var name in WebInspector.queryParamsObject)
331         queryParamsObject[name] = WebInspector.queryParamsObject[name];
332     if (this.dockController)
333         queryParamsObject["dockSide"] = this.dockController.dockSide();
334     var names = Object.keys(queryParamsObject);
335     for (var i = 0; i < names.length; ++i)
336         url += (i ? "&" : "?") + names[i] + "=" + queryParamsObject[names[i]];
337
338     InspectorBackend.disconnect();
339     document.location = url;
340 }
341
342 WebInspector.loaded = function()
343 {
344     InspectorBackend.loadFromJSONIfNeeded("../Inspector.json");
345     WebInspector.dockController = new WebInspector.DockController();
346
347     if (WebInspector.WorkerManager.isDedicatedWorkerFrontend()) {
348         // Do not create socket for the worker front-end.
349         WebInspector.doLoadedDone();
350         return;
351     }
352
353     var ws;
354     if ("ws" in WebInspector.queryParamsObject)
355         ws = "ws://" + WebInspector.queryParamsObject.ws;
356     else if ("page" in WebInspector.queryParamsObject) {
357         var page = WebInspector.queryParamsObject.page;
358         var host = "host" in WebInspector.queryParamsObject ? WebInspector.queryParamsObject.host : window.location.host;
359         ws = "ws://" + host + "/devtools/page/" + page;
360     }
361
362     if (ws) {
363         WebInspector.socket = new WebSocket(ws);
364         WebInspector.socket.onmessage = function(message) { InspectorBackend.dispatch(message.data); }
365         WebInspector.socket.onerror = function(error) { console.error(error); }
366         WebInspector.socket.onopen = function() {
367             InspectorFrontendHost.sendMessageToBackend = WebInspector.socket.send.bind(WebInspector.socket);
368             WebInspector.doLoadedDone();
369         }
370         WebInspector.socket.onclose = function() {
371             if (!WebInspector.socket._detachReason)
372                 (new WebInspector.RemoteDebuggingTerminatedScreen("websocket_closed")).showModal();
373         }
374         return;
375     }
376
377     WebInspector.doLoadedDone();
378
379     // In case of loading as a web page with no bindings / harness, kick off initialization manually.
380     if (InspectorFrontendHost.isStub) {
381         InspectorFrontendAPI.dispatchQueryParameters();
382         WebInspector._doLoadedDoneWithCapabilities();
383     }
384 }
385
386 WebInspector.doLoadedDone = function()
387 {
388     // Install styles and themes
389     WebInspector.installPortStyles();
390     if (WebInspector.socket)
391         document.body.addStyleClass("remote");
392
393     if (WebInspector.queryParamsObject.toolbarColor && WebInspector.queryParamsObject.textColor)
394         WebInspector.setToolbarColors(WebInspector.queryParamsObject.toolbarColor, WebInspector.queryParamsObject.textColor);
395
396     InspectorFrontendHost.loaded();
397     WebInspector.WorkerManager.loaded();
398
399     DebuggerAgent.causesRecompilation(WebInspector._initializeCapability.bind(WebInspector, "debuggerCausesRecompilation", null));
400     DebuggerAgent.supportsSeparateScriptCompilationAndExecution(WebInspector._initializeCapability.bind(WebInspector, "separateScriptCompilationAndExecutionEnabled", null));
401     ProfilerAgent.causesRecompilation(WebInspector._initializeCapability.bind(WebInspector, "profilerCausesRecompilation", null));
402     ProfilerAgent.isSampling(WebInspector._initializeCapability.bind(WebInspector, "samplingCPUProfiler", null));
403     HeapProfilerAgent.hasHeapProfiler(WebInspector._initializeCapability.bind(WebInspector, "heapProfilerPresent", null));
404     TimelineAgent.supportsFrameInstrumentation(WebInspector._initializeCapability.bind(WebInspector, "timelineSupportsFrameInstrumentation", null));
405     TimelineAgent.canMonitorMainThread(WebInspector._initializeCapability.bind(WebInspector, "timelineCanMonitorMainThread", null));
406     PageAgent.canShowDebugBorders(WebInspector._initializeCapability.bind(WebInspector, "canShowDebugBorders", null));
407     PageAgent.canShowFPSCounter(WebInspector._initializeCapability.bind(WebInspector, "canShowFPSCounter", null));
408     PageAgent.canContinuouslyPaint(WebInspector._initializeCapability.bind(WebInspector, "canContinuouslyPaint", null));
409     PageAgent.canOverrideDeviceMetrics(WebInspector._initializeCapability.bind(WebInspector, "canOverrideDeviceMetrics", null));
410     PageAgent.canOverrideGeolocation(WebInspector._initializeCapability.bind(WebInspector, "canOverrideGeolocation", null));
411     WorkerAgent.canInspectWorkers(WebInspector._initializeCapability.bind(WebInspector, "canInspectWorkers", null));
412     PageAgent.canOverrideDeviceOrientation(WebInspector._initializeCapability.bind(WebInspector, "canOverrideDeviceOrientation", WebInspector._doLoadedDoneWithCapabilities.bind(WebInspector)));
413 }
414
415 WebInspector._doLoadedDoneWithCapabilities = function()
416 {
417     new WebInspector.VersionController().updateVersion();
418
419     WebInspector.shortcutsScreen = new WebInspector.ShortcutsScreen();
420     this._registerShortcuts();
421
422     // set order of some sections explicitly
423     WebInspector.shortcutsScreen.section(WebInspector.UIString("Console"));
424     WebInspector.shortcutsScreen.section(WebInspector.UIString("Elements Panel"));
425
426     var panelDescriptors = this._panelDescriptors();
427     for (var i = 0; i < panelDescriptors.length; ++i)
428         panelDescriptors[i].registerShortcuts();
429
430     this.console = new WebInspector.ConsoleModel();
431     this.console.addEventListener(WebInspector.ConsoleModel.Events.ConsoleCleared, this._updateErrorAndWarningCounts, this);
432     this.console.addEventListener(WebInspector.ConsoleModel.Events.MessageAdded, this._updateErrorAndWarningCounts, this);
433     this.console.addEventListener(WebInspector.ConsoleModel.Events.RepeatCountUpdated, this._updateErrorAndWarningCounts, this);
434
435     WebInspector.CSSMetadata.requestCSSShorthandData();
436
437     this.drawer = new WebInspector.Drawer();
438
439     this.networkManager = new WebInspector.NetworkManager();
440     this.resourceTreeModel = new WebInspector.ResourceTreeModel(this.networkManager);
441     this.debuggerModel = new WebInspector.DebuggerModel();
442     this.debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.DebuggerPaused, this._debuggerPaused, this);
443     this.networkLog = new WebInspector.NetworkLog();
444     this.domAgent = new WebInspector.DOMAgent();
445     this.runtimeModel = new WebInspector.RuntimeModel(this.resourceTreeModel);
446
447     this.consoleView = new WebInspector.ConsoleView(WebInspector.WorkerManager.isWorkerFrontend());
448
449     InspectorBackend.registerInspectorDispatcher(this);
450
451     this.isolatedFileSystemManager = new WebInspector.IsolatedFileSystemManager();
452     this.isolatedFileSystemDispatcher = new WebInspector.IsolatedFileSystemDispatcher(this.isolatedFileSystemManager);
453     this.fileMapping = new WebInspector.FileMapping();
454     this.workspace = new WebInspector.Workspace(this.fileMapping, this.isolatedFileSystemManager.mapping());
455
456     this.cssModel = new WebInspector.CSSStyleModel(this.workspace);
457     this.timelineManager = new WebInspector.TimelineManager();
458     this.userAgentSupport = new WebInspector.UserAgentSupport();
459
460     this.searchController = new WebInspector.SearchController();
461     this.advancedSearchController = new WebInspector.AdvancedSearchController();
462     this.settingsController = new WebInspector.SettingsController();
463
464     this.domBreakpointsSidebarPane = new WebInspector.DOMBreakpointsSidebarPane();
465
466     this._zoomLevel = WebInspector.settings.zoomLevel.get();
467     if (this._zoomLevel)
468         this._requestZoom();
469
470     var autoselectPanel = WebInspector.UIString("a panel chosen automatically");
471     var openAnchorLocationSetting = WebInspector.settings.createSetting("openLinkHandler", autoselectPanel);
472     this.openAnchorLocationRegistry = new WebInspector.HandlerRegistry(openAnchorLocationSetting);
473     this.openAnchorLocationRegistry.registerHandler(autoselectPanel, function() { return false; });
474
475     this.workspaceController = new WebInspector.WorkspaceController(this.workspace);
476
477     this.fileSystemWorkspaceProvider = new WebInspector.FileSystemWorkspaceProvider(this.isolatedFileSystemManager, this.workspace);
478
479     this.networkWorkspaceProvider = new WebInspector.SimpleWorkspaceProvider(this.workspace, WebInspector.projectTypes.Network);
480     new WebInspector.NetworkUISourceCodeProvider(this.networkWorkspaceProvider, this.workspace);
481
482     this.breakpointManager = new WebInspector.BreakpointManager(WebInspector.settings.breakpoints, this.debuggerModel, this.workspace);
483
484     this.scriptSnippetModel = new WebInspector.ScriptSnippetModel(this.workspace);
485
486     new WebInspector.DebuggerScriptMapping(this.workspace, this.networkWorkspaceProvider);
487     this.liveEditSupport = new WebInspector.LiveEditSupport(this.workspace);
488     this.styleContentBinding = new WebInspector.StyleContentBinding(this.cssModel, this.workspace);
489     new WebInspector.StylesSourceMapping(this.cssModel, this.workspace);
490     if (WebInspector.experimentsSettings.sass.isEnabled())
491         new WebInspector.SASSSourceMapping(this.cssModel, this.workspace, this.networkWorkspaceProvider);
492
493     new WebInspector.PresentationConsoleMessageHelper(this.workspace);
494
495     this._createGlobalStatusBarItems();
496
497     this.toolbar = new WebInspector.Toolbar();
498     WebInspector.startBatchUpdate();
499     for (var i = 0; i < panelDescriptors.length; ++i)
500         WebInspector.inspectorView.addPanel(panelDescriptors[i]);
501     WebInspector.endBatchUpdate();
502
503     this.addMainEventListeners(document);
504
505     window.addEventListener("resize", this.windowResize.bind(this), true);
506
507     var errorWarningCount = document.getElementById("error-warning-count");
508     errorWarningCount.addEventListener("click", this.showConsole.bind(this), false);
509     this._updateErrorAndWarningCounts();
510
511     this.extensionServer.initExtensions();
512
513     this.console.enableAgent();
514
515     function showInitialPanel()
516     {
517         if (!WebInspector.inspectorView.currentPanel())
518             WebInspector.showPanel(WebInspector.settings.lastActivePanel.get());
519     }
520
521     InspectorAgent.enable(showInitialPanel);
522     this.databaseModel = new WebInspector.DatabaseModel();
523     this.domStorageModel = new WebInspector.DOMStorageModel();
524
525     if (!Capabilities.profilerCausesRecompilation || WebInspector.settings.profilerEnabled.get())
526         ProfilerAgent.enable();
527
528     if (WebInspector.settings.showPaintRects.get())
529         PageAgent.setShowPaintRects(true);
530
531     if (WebInspector.settings.showDebugBorders.get())
532         PageAgent.setShowDebugBorders(true);
533
534     if (WebInspector.settings.continuousPainting.get())
535         PageAgent.setContinuousPaintingEnabled(true);
536
537     if (WebInspector.settings.javaScriptDisabled.get())
538         PageAgent.setScriptExecutionDisabled(true);
539
540     if (WebInspector.settings.showFPSCounter.get())
541         PageAgent.setShowFPSCounter(true);
542
543     this.domAgent._emulateTouchEventsChanged();
544
545     WebInspector.WorkerManager.loadCompleted();
546     InspectorFrontendAPI.loadCompleted();
547
548     WebInspector.notifications.dispatchEventToListeners(WebInspector.Events.InspectorLoaded);
549 }
550
551 var windowLoaded = function()
552 {
553     var localizedStringsURL = InspectorFrontendHost.localizedStringsURL();
554     if (localizedStringsURL) {
555         var localizedStringsScriptElement = document.createElement("script");
556         localizedStringsScriptElement.addEventListener("load", WebInspector.loaded.bind(WebInspector), false);
557         localizedStringsScriptElement.type = "text/javascript";
558         localizedStringsScriptElement.src = localizedStringsURL;
559         document.head.appendChild(localizedStringsScriptElement);
560     } else
561         WebInspector.loaded();
562
563     window.removeEventListener("DOMContentLoaded", windowLoaded, false);
564     delete windowLoaded;
565 };
566
567 window.addEventListener("DOMContentLoaded", windowLoaded, false);
568
569 // We'd like to enforce asynchronous interaction between the inspector controller and the frontend.
570 // It is needed to prevent re-entering the backend code.
571 // Also, native dispatches do not guarantee setTimeouts to be serialized, so we
572 // enforce serialization using 'messagesToDispatch' queue. It is also important that JSC debugger
573 // tests require that each command was dispatch within individual timeout callback, so we don't batch them.
574
575 var messagesToDispatch = [];
576
577 WebInspector.dispatchQueueIsEmpty = function() {
578     return messagesToDispatch.length == 0;
579 }
580
581 WebInspector.dispatch = function(message) {
582     messagesToDispatch.push(message);
583     setTimeout(function() {
584         InspectorBackend.dispatch(messagesToDispatch.shift());
585     }, 0);
586 }
587
588 WebInspector.windowResize = function(event)
589 {
590     if (WebInspector.inspectorView)
591         WebInspector.inspectorView.doResize();
592     if (WebInspector.drawer)
593         WebInspector.drawer.resize();
594     if (WebInspector.toolbar)
595         WebInspector.toolbar.resize();
596     if (WebInspector.settingsController)
597         WebInspector.settingsController.resize();
598 }
599
600 WebInspector.setDockingUnavailable = function(unavailable)
601 {
602     if (this.dockController)
603         this.dockController.setDockingUnavailable(unavailable);
604 }
605
606 WebInspector.close = function(event)
607 {
608     if (this._isClosing)
609         return;
610     this._isClosing = true;
611     this.notifications.dispatchEventToListeners(WebInspector.Events.InspectorClosing);
612     InspectorFrontendHost.closeWindow();
613 }
614
615 WebInspector.documentClick = function(event)
616 {
617     var anchor = event.target.enclosingNodeOrSelfWithNodeName("a");
618     if (!anchor || (anchor.target === "_blank"))
619         return;
620
621     // Prevent the link from navigating, since we don't do any navigation by following links normally.
622     event.consume(true);
623
624     function followLink()
625     {
626         if (WebInspector.isBeingEdited(event.target) || WebInspector._showAnchorLocation(anchor))
627             return;
628
629         const profileMatch = WebInspector.ProfilesPanelDescriptor.ProfileURLRegExp.exec(anchor.href);
630         if (profileMatch) {
631             WebInspector.showPanel("profiles").showProfile(profileMatch[1], profileMatch[2]);
632             return;
633         }
634
635         var parsedURL = anchor.href.asParsedURL();
636         if (parsedURL && parsedURL.scheme === "webkit-link-action") {
637             if (parsedURL.host === "show-panel") {
638                 var panel = parsedURL.path.substring(1);
639                 if (WebInspector.panel(panel))
640                     WebInspector.showPanel(panel);
641             }
642             return;
643         }
644
645         InspectorFrontendHost.openInNewTab(anchor.href);
646     }
647
648     if (WebInspector.followLinkTimeout)
649         clearTimeout(WebInspector.followLinkTimeout);
650
651     if (anchor.preventFollowOnDoubleClick) {
652         // Start a timeout if this is the first click, if the timeout is canceled
653         // before it fires, then a double clicked happened or another link was clicked.
654         if (event.detail === 1)
655             WebInspector.followLinkTimeout = setTimeout(followLink, 333);
656         return;
657     }
658
659     followLink();
660 }
661
662 WebInspector.openResource = function(resourceURL, inResourcesPanel)
663 {
664     var resource = WebInspector.resourceForURL(resourceURL);
665     if (inResourcesPanel && resource)
666         WebInspector.showPanel("resources").showResource(resource);
667     else
668         InspectorFrontendHost.openInNewTab(resourceURL);
669 }
670
671 WebInspector._registerShortcuts = function()
672 {
673     var shortcut = WebInspector.KeyboardShortcut;
674     var section = WebInspector.shortcutsScreen.section(WebInspector.UIString("All Panels"));
675     var keys = [
676         shortcut.makeDescriptor("[", shortcut.Modifiers.CtrlOrMeta),
677         shortcut.makeDescriptor("]", shortcut.Modifiers.CtrlOrMeta)
678     ];
679     section.addRelatedKeys(keys, WebInspector.UIString("Go to the panel to the left/right"));
680
681     var keys = [
682         shortcut.makeDescriptor("[", shortcut.Modifiers.CtrlOrMeta | shortcut.Modifiers.Alt),
683         shortcut.makeDescriptor("]", shortcut.Modifiers.CtrlOrMeta | shortcut.Modifiers.Alt)
684     ];
685     section.addRelatedKeys(keys, WebInspector.UIString("Go back/forward in panel history"));
686
687     section.addKey(shortcut.makeDescriptor(shortcut.Keys.Esc), WebInspector.UIString("Toggle console"));
688     section.addKey(shortcut.makeDescriptor("f", shortcut.Modifiers.CtrlOrMeta), WebInspector.UIString("Search"));
689
690     var advancedSearchShortcut = WebInspector.AdvancedSearchController.createShortcut();
691     section.addKey(advancedSearchShortcut, WebInspector.UIString("Search across all sources"));
692
693     var openResourceShortcut = WebInspector.KeyboardShortcut.makeDescriptor("o", WebInspector.KeyboardShortcut.Modifiers.CtrlOrMeta);
694     section.addKey(openResourceShortcut, WebInspector.UIString("Go to source"));
695
696     if (WebInspector.isMac()) {
697         keys = [
698             shortcut.makeDescriptor("g", shortcut.Modifiers.Meta),
699             shortcut.makeDescriptor("g", shortcut.Modifiers.Meta | shortcut.Modifiers.Shift)
700         ];
701         section.addRelatedKeys(keys, WebInspector.UIString("Find next/previous"));
702     }
703
704     var goToShortcut = WebInspector.GoToLineDialog.createShortcut();
705     section.addKey(goToShortcut, WebInspector.UIString("Go to line"));
706 }
707
708 /**
709  * @param {KeyboardEvent} event
710  */
711 WebInspector.documentKeyDown = function(event)
712 {
713     const helpKey = WebInspector.isMac() ? "U+003F" : "U+00BF"; // "?" for both platforms
714
715     if (event.keyIdentifier === "F1" ||
716         (event.keyIdentifier === helpKey && event.shiftKey && (!WebInspector.isBeingEdited(event.target) || event.metaKey))) {
717         this.settingsController.showSettingsScreen(WebInspector.SettingsScreen.Tabs.Shortcuts);
718         event.consume(true);
719         return;
720     }
721
722     if (WebInspector.currentFocusElement() && WebInspector.currentFocusElement().handleKeyEvent) {
723         WebInspector.currentFocusElement().handleKeyEvent(event);
724         if (event.handled) {
725             event.consume(true);
726             return;
727         }
728     }
729
730     if (WebInspector.inspectorView.currentPanel()) {
731         WebInspector.inspectorView.currentPanel().handleShortcut(event);
732         if (event.handled) {
733             event.consume(true);
734             return;
735         }
736     }
737
738     if (WebInspector.searchController.handleShortcut(event))
739         return;
740     if (WebInspector.advancedSearchController.handleShortcut(event))
741         return;
742
743     switch (event.keyIdentifier) {
744         case "U+004F": // O key
745             if (!event.shiftKey && !event.altKey && WebInspector.KeyboardShortcut.eventHasCtrlOrMeta(event)) {
746                 WebInspector.showPanel("scripts").showGoToSourceDialog();
747                 event.consume(true);
748             }
749             break;
750         case "U+0052": // R key
751             if (WebInspector.KeyboardShortcut.eventHasCtrlOrMeta(event)) {
752                 PageAgent.reload(event.shiftKey);
753                 event.consume(true);
754             }
755             if (window.DEBUG && event.altKey) {
756                 WebInspector.reload();
757                 return;
758             }
759             break;
760         case "F5":
761             if (!WebInspector.isMac()) {
762                 PageAgent.reload(event.ctrlKey || event.shiftKey);
763                 event.consume(true);
764             }
765             break;
766     }
767
768     var isValidZoomShortcut = WebInspector.KeyboardShortcut.eventHasCtrlOrMeta(event) &&
769         !event.altKey &&
770         !InspectorFrontendHost.isStub;
771     switch (event.keyCode) {
772         case 107: // +
773         case 187: // +
774             if (isValidZoomShortcut) {
775                 WebInspector._zoomIn();
776                 event.consume(true);
777             }
778             break;
779         case 109: // -
780         case 189: // -
781             if (isValidZoomShortcut) {
782                 WebInspector._zoomOut();
783                 event.consume(true);
784             }
785             break;
786         case 48: // 0
787             // Zoom reset shortcut does not allow "Shift" when handled by the browser.
788             if (isValidZoomShortcut && !event.shiftKey) {
789                 WebInspector._resetZoom();
790                 event.consume(true);
791             }
792             break;
793     }
794
795     // Cmd/Control + Shift + C should be a shortcut to clicking the Node Search Button.
796     // This shortcut matches Firebug.
797     if (event.keyIdentifier === "U+0043") { // C key
798         if (WebInspector.isMac())
799             var isNodeSearchKey = event.metaKey && !event.ctrlKey && !event.altKey && event.shiftKey;
800         else
801             var isNodeSearchKey = event.ctrlKey && !event.metaKey && !event.altKey && event.shiftKey;
802
803         if (isNodeSearchKey) {
804             this.toggleSearchingForNode();
805             event.consume(true);
806             return;
807         }
808         return;
809     }
810 }
811
812 WebInspector.postDocumentKeyDown = function(event)
813 {
814     if (event.handled)
815         return;
816
817     if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Esc.code) {
818         // If drawer is open with some view other than console then close it.
819         if (!this._toggleConsoleButton.toggled && WebInspector.drawer.visible)
820             this.closeViewInDrawer();
821         else
822             this._toggleConsoleButtonClicked();
823     }
824 }
825
826 WebInspector.documentCanCopy = function(event)
827 {
828     if (WebInspector.inspectorView.currentPanel() && WebInspector.inspectorView.currentPanel().handleCopyEvent)
829         event.preventDefault();
830 }
831
832 WebInspector.documentCopy = function(event)
833 {
834     if (WebInspector.inspectorView.currentPanel() && WebInspector.inspectorView.currentPanel().handleCopyEvent)
835         WebInspector.inspectorView.currentPanel().handleCopyEvent(event);
836     WebInspector.documentCopyEventFired(event);
837 }
838
839 WebInspector.documentCopyEventFired = function(event)
840 {
841 }
842
843 WebInspector.contextMenuEventFired = function(event)
844 {
845     if (event.handled || event.target.hasStyleClass("popup-glasspane"))
846         event.preventDefault();
847 }
848
849 WebInspector.showConsole = function()
850 {
851     if (WebInspector._toggleConsoleButton && !WebInspector._toggleConsoleButton.toggled) {
852         if (WebInspector.drawer.visible)
853             this._closePreviousDrawerView();
854         WebInspector._toggleConsoleButtonClicked();
855     }
856 }
857
858 WebInspector.showPanel = function(panel)
859 {
860     return WebInspector.inspectorView.showPanel(panel);
861 }
862
863 WebInspector.panel = function(panel)
864 {
865     return WebInspector.inspectorView.panel(panel);
866 }
867
868 WebInspector.bringToFront = function()
869 {
870     InspectorFrontendHost.bringToFront();
871 }
872
873 /**
874  * @param {string=} messageLevel
875  * @param {boolean=} showConsole
876  */
877 WebInspector.log = function(message, messageLevel, showConsole)
878 {
879     // remember 'this' for setInterval() callback
880     var self = this;
881
882     // return indication if we can actually log a message
883     function isLogAvailable()
884     {
885         return WebInspector.ConsoleMessage && WebInspector.RemoteObject && self.console;
886     }
887
888     // flush the queue of pending messages
889     function flushQueue()
890     {
891         var queued = WebInspector.log.queued;
892         if (!queued)
893             return;
894
895         for (var i = 0; i < queued.length; ++i)
896             logMessage(queued[i]);
897
898         delete WebInspector.log.queued;
899     }
900
901     // flush the queue if it console is available
902     // - this function is run on an interval
903     function flushQueueIfAvailable()
904     {
905         if (!isLogAvailable())
906             return;
907
908         clearInterval(WebInspector.log.interval);
909         delete WebInspector.log.interval;
910
911         flushQueue();
912     }
913
914     // actually log the message
915     function logMessage(message)
916     {
917         // post the message
918         var msg = WebInspector.ConsoleMessage.create(
919             WebInspector.ConsoleMessage.MessageSource.Other,
920             messageLevel || WebInspector.ConsoleMessage.MessageLevel.Debug,
921             message);
922
923         self.console.addMessage(msg);
924         if (showConsole)
925             WebInspector.showConsole();
926     }
927
928     // if we can't log the message, queue it
929     if (!isLogAvailable()) {
930         if (!WebInspector.log.queued)
931             WebInspector.log.queued = [];
932
933         WebInspector.log.queued.push(message);
934
935         if (!WebInspector.log.interval)
936             WebInspector.log.interval = setInterval(flushQueueIfAvailable, 1000);
937
938         return;
939     }
940
941     // flush the pending queue if any
942     flushQueue();
943
944     // log the message
945     logMessage(message);
946 }
947
948 WebInspector.showErrorMessage = function(error)
949 {
950     WebInspector.log(error, WebInspector.ConsoleMessage.MessageLevel.Error, true);
951 }
952
953 // Inspector.inspect protocol event
954 WebInspector.inspect = function(payload, hints)
955 {
956     var object = WebInspector.RemoteObject.fromPayload(payload);
957     if (object.subtype === "node") {
958         function callback(nodeId)
959         {
960             WebInspector._updateFocusedNode(nodeId);
961             object.release();
962         }
963         object.pushNodeToFrontend(callback);
964         return;
965     }
966
967     if (hints.databaseId)
968         WebInspector.showPanel("resources").selectDatabase(WebInspector.databaseModel.databaseForId(hints.databaseId));
969     else if (hints.domStorageId)
970         WebInspector.showPanel("resources").selectDOMStorage(WebInspector.domStorageModel.storageForId(hints.domStorageId));
971
972     object.release();
973 }
974
975 // Inspector.detached protocol event
976 WebInspector.detached = function(reason)
977 {
978     WebInspector.socket._detachReason = reason;
979     (new WebInspector.RemoteDebuggingTerminatedScreen(reason)).showModal();
980 }
981
982 WebInspector._updateFocusedNode = function(nodeId)
983 {
984     if (WebInspector._nodeSearchButton.toggled) {
985         InspectorFrontendHost.bringToFront();
986         WebInspector._nodeSearchButton.toggled = false;
987     }
988     WebInspector.showPanel("elements").revealAndSelectNode(nodeId);
989 }
990
991 WebInspector._showAnchorLocation = function(anchor)
992 {
993     if (WebInspector.openAnchorLocationRegistry.dispatch({ url: anchor.href, lineNumber: anchor.lineNumber}))
994         return true;
995     var preferredPanel = this.panels[anchor.preferredPanel];
996     if (preferredPanel && WebInspector._showAnchorLocationInPanel(anchor, preferredPanel))
997         return true;
998     if (WebInspector._showAnchorLocationInPanel(anchor, this.panel("scripts")))
999         return true;
1000     if (WebInspector._showAnchorLocationInPanel(anchor, this.panel("resources")))
1001         return true;
1002     if (WebInspector._showAnchorLocationInPanel(anchor, this.panel("network")))
1003         return true;
1004     return false;
1005 }
1006
1007 WebInspector._showAnchorLocationInPanel = function(anchor, panel)
1008 {
1009     if (!panel || !panel.canShowAnchorLocation(anchor))
1010         return false;
1011
1012     // FIXME: support webkit-html-external-link links here.
1013     if (anchor.hasStyleClass("webkit-html-external-link")) {
1014         anchor.removeStyleClass("webkit-html-external-link");
1015         anchor.addStyleClass("webkit-html-resource-link");
1016     }
1017
1018     WebInspector.inspectorView.showPanelForAnchorNavigation(panel);
1019     panel.showAnchorLocation(anchor);
1020     return true;
1021 }
1022
1023 WebInspector.evaluateInConsole = function(expression, showResultOnly)
1024 {
1025     this.showConsole();
1026     this.consoleView.evaluateUsingTextPrompt(expression, showResultOnly);
1027 }
1028
1029 WebInspector.addMainEventListeners = function(doc)
1030 {
1031     doc.addEventListener("keydown", this.documentKeyDown.bind(this), true);
1032     doc.addEventListener("keydown", this.postDocumentKeyDown.bind(this), false);
1033     doc.addEventListener("beforecopy", this.documentCanCopy.bind(this), true);
1034     doc.addEventListener("copy", this.documentCopy.bind(this), true);
1035     doc.addEventListener("contextmenu", this.contextMenuEventFired.bind(this), true);
1036     doc.addEventListener("click", this.documentClick.bind(this), true);
1037 }
1038
1039 WebInspector.Zoom = {
1040     Table: [0.25, 0.33, 0.5, 0.66, 0.75, 0.9, 1, 1.1, 1.25, 1.5, 1.75, 2, 2.5, 3, 4, 5],
1041     DefaultOffset: 6
1042 }