5ed6d37fab0391236b7014994ff8b93817601361
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Base / Main.js
1 /*
2  * Copyright (C) 2013-2014 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 WebInspector.ContentViewCookieType = {
27     ApplicationCache: "application-cache",
28     CookieStorage: "cookie-storage",
29     Database: "database",
30     DatabaseTable: "database-table",
31     DOMStorage: "dom-storage",
32     Resource: "resource", // includes Frame too.
33     Timelines: "timelines"
34 };
35
36 WebInspector.DebuggableType = {
37     Web: "web",
38     JavaScript: "javascript"
39 };
40
41 WebInspector.SelectedSidebarPanelCookieKey = "selected-sidebar-panel";
42 WebInspector.TypeIdentifierCookieKey = "represented-object-type";
43
44 WebInspector.StateRestorationType = {
45     Load: "state-restoration-load",
46     Navigation: "state-restoration-navigation",
47     Delayed: "state-restoration-delayed",
48 };
49
50 WebInspector.loaded = function()
51 {
52     // Initialize WebSocket to communication.
53     this._initializeWebSocketIfNeeded();
54
55     this.debuggableType = InspectorFrontendHost.debuggableType() === "web" ? WebInspector.DebuggableType.Web : WebInspector.DebuggableType.JavaScript;
56     this.hasExtraDomains = false;
57
58     // Register observers for events from the InspectorBackend.
59     if (InspectorBackend.registerInspectorDispatcher)
60         InspectorBackend.registerInspectorDispatcher(new WebInspector.InspectorObserver);
61     if (InspectorBackend.registerPageDispatcher)
62         InspectorBackend.registerPageDispatcher(new WebInspector.PageObserver);
63     if (InspectorBackend.registerConsoleDispatcher)
64         InspectorBackend.registerConsoleDispatcher(new WebInspector.ConsoleObserver);
65     if (InspectorBackend.registerNetworkDispatcher)
66         InspectorBackend.registerNetworkDispatcher(new WebInspector.NetworkObserver);
67     if (InspectorBackend.registerDOMDispatcher)
68         InspectorBackend.registerDOMDispatcher(new WebInspector.DOMObserver);
69     if (InspectorBackend.registerDebuggerDispatcher)
70         InspectorBackend.registerDebuggerDispatcher(new WebInspector.DebuggerObserver);
71     if (InspectorBackend.registerDatabaseDispatcher)
72         InspectorBackend.registerDatabaseDispatcher(new WebInspector.DatabaseObserver);
73     if (InspectorBackend.registerDOMStorageDispatcher)
74         InspectorBackend.registerDOMStorageDispatcher(new WebInspector.DOMStorageObserver);
75     if (InspectorBackend.registerApplicationCacheDispatcher)
76         InspectorBackend.registerApplicationCacheDispatcher(new WebInspector.ApplicationCacheObserver);
77     if (InspectorBackend.registerTimelineDispatcher)
78         InspectorBackend.registerTimelineDispatcher(new WebInspector.TimelineObserver);
79     if (InspectorBackend.registerCSSDispatcher)
80         InspectorBackend.registerCSSDispatcher(new WebInspector.CSSObserver);
81     if (InspectorBackend.registerLayerTreeDispatcher)
82         InspectorBackend.registerLayerTreeDispatcher(new WebInspector.LayerTreeObserver);
83     if (InspectorBackend.registerRuntimeDispatcher)
84         InspectorBackend.registerRuntimeDispatcher(new WebInspector.RuntimeObserver);
85     if (InspectorBackend.registerReplayDispatcher)
86         InspectorBackend.registerReplayDispatcher(new WebInspector.ReplayObserver);
87
88     // Enable agents.
89     if (window.InspectorAgent)
90         InspectorAgent.enable();
91
92     // Perform one-time tasks.
93     WebInspector.CSSCompletions.requestCSSCompletions();
94     this._generateDisclosureTriangleImages();
95
96     // Listen for the ProvisionalLoadStarted event before registering for events so our code gets called before any managers or sidebars.
97     // This lets us save a state cookie before any managers or sidebars do any resets that would affect state (namely TimelineManager).
98     WebInspector.Frame.addEventListener(WebInspector.Frame.Event.ProvisionalLoadStarted, this._provisionalLoadStarted, this);
99
100     // Create the singleton managers next, before the user interface elements, so the user interface can register
101     // as event listeners on these managers.
102     this.branchManager = new WebInspector.BranchManager;
103     this.frameResourceManager = new WebInspector.FrameResourceManager;
104     this.storageManager = new WebInspector.StorageManager;
105     this.domTreeManager = new WebInspector.DOMTreeManager;
106     this.cssStyleManager = new WebInspector.CSSStyleManager;
107     this.logManager = new WebInspector.LogManager;
108     this.issueManager = new WebInspector.IssueManager;
109     this.analyzerManager = new WebInspector.AnalyzerManager;
110     this.runtimeManager = new WebInspector.RuntimeManager;
111     this.applicationCacheManager = new WebInspector.ApplicationCacheManager;
112     this.timelineManager = new WebInspector.TimelineManager;
113     this.debuggerManager = new WebInspector.DebuggerManager;
114     this.sourceMapManager = new WebInspector.SourceMapManager;
115     this.layerTreeManager = new WebInspector.LayerTreeManager;
116     this.dashboardManager = new WebInspector.DashboardManager;
117     this.probeManager = new WebInspector.ProbeManager;
118     this.replayManager = new WebInspector.ReplayManager;
119
120     // Enable the Console Agent after creating the singleton managers.
121     if (window.ConsoleAgent)
122         ConsoleAgent.enable();
123
124     // Tell the backend we are initialized after all our initialization messages have been sent.
125     setTimeout(function() {
126         // COMPATIBILITY (iOS 8): Inspector.initialized did not exist yet.
127         if (window.InspectorAgent && InspectorAgent.initialized)
128             InspectorAgent.initialized();
129     }, 0);
130
131     // Register for events.
132     this.replayManager.addEventListener(WebInspector.ReplayManager.Event.CaptureStarted, this._captureDidStart, this);
133     this.debuggerManager.addEventListener(WebInspector.DebuggerManager.Event.Paused, this._debuggerDidPause, this);
134     this.debuggerManager.addEventListener(WebInspector.DebuggerManager.Event.Resumed, this._debuggerDidResume, this);
135     this.domTreeManager.addEventListener(WebInspector.DOMTreeManager.Event.InspectModeStateChanged, this._inspectModeStateChanged, this);
136     this.domTreeManager.addEventListener(WebInspector.DOMTreeManager.Event.DOMNodeWasInspected, this._domNodeWasInspected, this);
137     this.storageManager.addEventListener(WebInspector.StorageManager.Event.DOMStorageObjectWasInspected, this._storageWasInspected, this);
138     this.storageManager.addEventListener(WebInspector.StorageManager.Event.DatabaseWasInspected, this._storageWasInspected, this);
139     this.frameResourceManager.addEventListener(WebInspector.FrameResourceManager.Event.MainFrameDidChange, this._mainFrameDidChange, this);
140     this.frameResourceManager.addEventListener(WebInspector.FrameResourceManager.Event.FrameWasAdded, this._frameWasAdded, this);
141
142     WebInspector.Frame.addEventListener(WebInspector.Frame.Event.MainResourceDidChange, this._mainResourceDidChange, this);
143
144     document.addEventListener("DOMContentLoaded", this.contentLoaded.bind(this));
145
146     // Create settings.
147     this._showingSplitConsoleSetting = new WebInspector.Setting("showing-split-console", false);
148     this._splitConsoleHeightSetting = new WebInspector.Setting("split-console-height", 150);
149
150     this._openTabsSetting = new WebInspector.Setting("open-tab-types", ["elements", "network", "resources", "timeline", "debugger", "storage", "console"]);
151     this._selectedTabIndexSetting = new WebInspector.Setting("selected-tab-index", 0);
152
153     this.showShadowDOMSetting = new WebInspector.Setting("show-shadow-dom", false);
154     this.showReplayInterfaceSetting = new WebInspector.Setting("show-web-replay", false);
155
156     // COMPATIBILITY (iOS 8): Page.enableTypeProfiler did not exist.
157     this.showJavaScriptTypeInformationSetting = new WebInspector.Setting("show-javascript-type-information", false);
158     if (this.showJavaScriptTypeInformationSetting.value && window.RuntimeAgent && RuntimeAgent.enableTypeProfiler)
159         RuntimeAgent.enableTypeProfiler();
160
161     // COMPATIBILITY (iOS 8): Page.setShowPaintRects did not exist.
162     this.showPaintRectsSetting = new WebInspector.Setting("show-paint-rects", false);
163     if (this.showPaintRectsSetting.value && window.PageAgent && PageAgent.setShowPaintRects)
164         PageAgent.setShowPaintRects(true);
165
166     this.mouseCoords = {
167         x: 0,
168         y: 0
169     };
170
171     this._windowKeydownListeners = [];
172 };
173
174 WebInspector.contentLoaded = function()
175 {
176     // Register for global events.
177     document.addEventListener("beforecopy", this._beforecopy.bind(this));
178     document.addEventListener("copy", this._copy.bind(this));
179
180     document.addEventListener("click", this._mouseWasClicked.bind(this));
181     document.addEventListener("dragover", this._dragOver.bind(this));
182     document.addEventListener("focus", WebInspector._focusChanged.bind(this), true);
183
184     window.addEventListener("focus", this._windowFocused.bind(this));
185     window.addEventListener("blur", this._windowBlurred.bind(this));
186     window.addEventListener("resize", this._windowResized.bind(this));
187     window.addEventListener("keydown", this._windowKeyDown.bind(this));
188     window.addEventListener("keyup", this._windowKeyUp.bind(this));
189     window.addEventListener("mousemove", this._mouseMoved.bind(this), true);
190     window.addEventListener("pagehide", this._pageHidden.bind(this));
191
192     // Add platform style classes so the UI can be tweaked per-platform.
193     document.body.classList.add(WebInspector.Platform.name + "-platform");
194     if (WebInspector.Platform.isNightlyBuild)
195         document.body.classList.add("nightly-build");
196
197     if (WebInspector.Platform.name === "mac") {
198         if (WebInspector.Platform.version.release >= 11)
199             document.body.classList.add("latest-mac");
200         else
201             document.body.classList.add("legacy-mac");
202     }
203
204     document.body.classList.add(this.debuggableType);
205
206     // Create the user interface elements.
207     this.toolbar = new WebInspector.Toolbar(document.getElementById("toolbar"), null, true);
208     this.toolbar.displayMode = WebInspector.Toolbar.DisplayMode.IconOnly;
209     this.toolbar.sizeMode = WebInspector.Toolbar.SizeMode.Small;
210
211     this.tabBar = new WebInspector.TabBar(document.getElementById("tab-bar"));
212     this.tabBar.addEventListener(WebInspector.TabBar.Event.NewTabItemClicked, this._newTabItemClicked, this);
213
214     var contentElement = document.getElementById("content");
215     contentElement.setAttribute("role", "main");
216     contentElement.setAttribute("aria-label", WebInspector.UIString("Content"));
217
218     this.splitContentBrowser = new WebInspector.ContentBrowser(document.getElementById("split-content-browser"), this, true);
219     this.splitContentBrowser.navigationBar.element.addEventListener("mousedown", this._consoleResizerMouseDown.bind(this));
220
221     this.quickConsole = new WebInspector.QuickConsole(document.getElementById("quick-console"));
222     this.quickConsole.addEventListener(WebInspector.QuickConsole.Event.DidResize, this._quickConsoleDidResize, this);
223
224     this._consoleRepresentedObject = new WebInspector.LogObject;
225     this._consoleTreeElement = new WebInspector.LogTreeElement(this._consoleRepresentedObject);
226     this.consoleContentView = WebInspector.splitContentBrowser.contentViewForRepresentedObject(this._consoleRepresentedObject);
227     this.consoleLogViewController = this.consoleContentView.logViewController;
228
229     // FIXME: The sidebars should be flipped in RTL languages.
230     this.navigationSidebar = new WebInspector.Sidebar(document.getElementById("navigation-sidebar"), WebInspector.Sidebar.Sides.Left);
231     this.navigationSidebar.addEventListener(WebInspector.Sidebar.Event.WidthDidChange, this._sidebarWidthDidChange, this);
232
233     this.detailsSidebar = new WebInspector.Sidebar(document.getElementById("details-sidebar"), WebInspector.Sidebar.Sides.Right, null, null, WebInspector.UIString("Details"), true);
234     this.detailsSidebar.addEventListener(WebInspector.Sidebar.Event.WidthDidChange, this._sidebarWidthDidChange, this);
235
236     this.searchKeyboardShortcut = new WebInspector.KeyboardShortcut(WebInspector.KeyboardShortcut.Modifier.CommandOrControl | WebInspector.KeyboardShortcut.Modifier.Shift, "F", this._focusSearchField.bind(this));
237
238     this.navigationSidebarKeyboardShortcut = new WebInspector.KeyboardShortcut(WebInspector.KeyboardShortcut.Modifier.CommandOrControl, "0", this.toggleNavigationSidebar.bind(this));
239     this.detailsSidebarKeyboardShortcut = new WebInspector.KeyboardShortcut(WebInspector.KeyboardShortcut.Modifier.CommandOrControl | WebInspector.KeyboardShortcut.Modifier.Option, "0", this.toggleDetailsSidebar.bind(this));
240
241     this.tabBrowser = new WebInspector.TabBrowser(document.getElementById("tab-browser"), this.tabBar, this.navigationSidebar, this.detailsSidebar);
242     this.tabBrowser.addEventListener(WebInspector.TabBrowser.Event.SelectedTabContentViewDidChange, this._tabBrowserSelectedTabContentViewDidChange, this);
243
244     this.tabBar.addEventListener(WebInspector.TabBar.Event.TabBarItemAdded, this._updateNewTabButtonState, this);
245     this.tabBar.addEventListener(WebInspector.TabBar.Event.TabBarItemRemoved, this._updateNewTabButtonState, this);
246
247     this._reloadPageKeyboardShortcut = new WebInspector.KeyboardShortcut(WebInspector.KeyboardShortcut.Modifier.CommandOrControl, "R", this._reloadPage.bind(this));
248     this._reloadPageIgnoringCacheKeyboardShortcut = new WebInspector.KeyboardShortcut(WebInspector.KeyboardShortcut.Modifier.CommandOrControl | WebInspector.KeyboardShortcut.Modifier.Shift, "R", this._reloadPageIgnoringCache.bind(this));
249
250     this._consoleTabKeyboardShortcut = new WebInspector.KeyboardShortcut(WebInspector.KeyboardShortcut.Modifier.Option | WebInspector.KeyboardShortcut.Modifier.CommandOrControl, "C", this._showConsoleTab.bind(this));
251     this._quickConsoleKeyboardShortcut = new WebInspector.KeyboardShortcut(WebInspector.KeyboardShortcut.Modifier.Control, WebInspector.KeyboardShortcut.Key.Apostrophe, this._focusConsolePrompt.bind(this));
252
253     this._inspectModeKeyboardShortcut = new WebInspector.KeyboardShortcut(WebInspector.KeyboardShortcut.Modifier.CommandOrControl | WebInspector.KeyboardShortcut.Modifier.Shift, "C", this._toggleInspectMode.bind(this));
254
255     this._undoKeyboardShortcut = new WebInspector.KeyboardShortcut(WebInspector.KeyboardShortcut.Modifier.CommandOrControl, "Z", this._undoKeyboardShortcut.bind(this));
256     this._redoKeyboardShortcut = new WebInspector.KeyboardShortcut(WebInspector.KeyboardShortcut.Modifier.CommandOrControl | WebInspector.KeyboardShortcut.Modifier.Shift, "Z", this._redoKeyboardShortcut.bind(this));
257     this._undoKeyboardShortcut.implicitlyPreventsDefault = this._redoKeyboardShortcut.implicitlyPreventsDefault = false;
258
259     this.toggleBreakpointsKeyboardShortcut = new WebInspector.KeyboardShortcut(WebInspector.KeyboardShortcut.Modifier.CommandOrControl, "Y", this.debuggerToggleBreakpoints.bind(this));
260     this.pauseOrResumeKeyboardShortcut = new WebInspector.KeyboardShortcut(WebInspector.KeyboardShortcut.Modifier.Control | WebInspector.KeyboardShortcut.Modifier.CommandOrControl, "Y", this.debuggerPauseResumeToggle.bind(this));
261     this.stepOverKeyboardShortcut = new WebInspector.KeyboardShortcut(null, WebInspector.KeyboardShortcut.Key.F6, this.debuggerStepOver.bind(this));
262     this.stepIntoKeyboardShortcut = new WebInspector.KeyboardShortcut(null, WebInspector.KeyboardShortcut.Key.F7, this.debuggerStepInto.bind(this));
263     this.stepOutKeyboardShortcut = new WebInspector.KeyboardShortcut(null, WebInspector.KeyboardShortcut.Key.F8, this.debuggerStepOut.bind(this));
264
265     this.pauseOrResumeAlternateKeyboardShortcut = new WebInspector.KeyboardShortcut(WebInspector.KeyboardShortcut.Modifier.CommandOrControl, WebInspector.KeyboardShortcut.Key.Backslash, this.debuggerPauseResumeToggle.bind(this));
266     this.stepOverAlternateKeyboardShortcut = new WebInspector.KeyboardShortcut(WebInspector.KeyboardShortcut.Modifier.CommandOrControl, WebInspector.KeyboardShortcut.Key.SingleQuote, this.debuggerStepOver.bind(this));
267     this.stepIntoAlternateKeyboardShortcut = new WebInspector.KeyboardShortcut(WebInspector.KeyboardShortcut.Modifier.CommandOrControl, WebInspector.KeyboardShortcut.Key.Semicolon, this.debuggerStepInto.bind(this));
268     this.stepOutAlternateKeyboardShortcut = new WebInspector.KeyboardShortcut(WebInspector.KeyboardShortcut.Modifier.Shift | WebInspector.KeyboardShortcut.Modifier.CommandOrControl, WebInspector.KeyboardShortcut.Key.Semicolon, this.debuggerStepOut.bind(this));
269
270     this._closeToolbarButton = new WebInspector.ControlToolbarItem("dock-close", WebInspector.UIString("Close"), "Images/Close.svg", 16, 14);
271     this._closeToolbarButton.addEventListener(WebInspector.ButtonNavigationItem.Event.Clicked, this.close, this);
272
273     this._undockToolbarButton = new WebInspector.ButtonToolbarItem("undock", WebInspector.UIString("Detach into separate window"), null, "Images/Undock.svg");
274     this._undockToolbarButton.addEventListener(WebInspector.ButtonNavigationItem.Event.Clicked, this._undock, this);
275
276     this._dockRightToolbarButton = new WebInspector.ButtonToolbarItem("dock-right", WebInspector.UIString("Dock to right of window"), null, "Images/DockRight.svg");
277     this._dockRightToolbarButton.addEventListener(WebInspector.ButtonNavigationItem.Event.Clicked, this._dockRight, this);
278
279     this._dockBottomToolbarButton = new WebInspector.ButtonToolbarItem("dock-bottom", WebInspector.UIString("Dock to bottom of window"), null, "Images/DockBottom.svg");
280     this._dockBottomToolbarButton.addEventListener(WebInspector.ButtonNavigationItem.Event.Clicked, this._dockBottom, this);
281
282     var toolTip;
283     if (WebInspector.debuggableType === WebInspector.DebuggableType.JavaScript)
284         toolTip = WebInspector.UIString("Restart (%s)").format(this._reloadPageKeyboardShortcut.displayName);
285     else
286         toolTip = WebInspector.UIString("Reload page (%s)\nReload ignoring cache (%s)").format(this._reloadPageKeyboardShortcut.displayName, this._reloadPageIgnoringCacheKeyboardShortcut.displayName);
287
288     this._reloadToolbarButton = new WebInspector.ButtonToolbarItem("reload", toolTip, null, "Images/ReloadToolbar.svg");
289     this._reloadToolbarButton.addEventListener(WebInspector.ButtonNavigationItem.Event.Clicked, this._reloadPageClicked, this);
290
291     this._downloadToolbarButton = new WebInspector.ButtonToolbarItem("download", WebInspector.UIString("Download Web Archive"), null, "Images/DownloadArrow.svg");
292     this._downloadToolbarButton.addEventListener(WebInspector.ButtonNavigationItem.Event.Clicked, this._downloadWebArchive, this);
293
294     this._updateReloadToolbarButton();
295     this._updateDownloadToolbarButton();
296
297     // The toolbar button for node inspection.
298     if (this.debuggableType === WebInspector.DebuggableType.Web) {
299         var toolTip = WebInspector.UIString("Enable point to inspect mode (%s)").format(WebInspector._inspectModeKeyboardShortcut.displayName);
300         var activatedToolTip = WebInspector.UIString("Disable point to inspect mode (%s)").format(WebInspector._inspectModeKeyboardShortcut.displayName);
301         this._inspectModeToolbarButton = new WebInspector.ActivateButtonToolbarItem("inspect", toolTip, activatedToolTip, null, "Images/Crosshair.svg");
302         this._inspectModeToolbarButton.addEventListener(WebInspector.ButtonNavigationItem.Event.Clicked, this._toggleInspectMode, this);
303     }
304
305     this._dashboardContainer = new WebInspector.DashboardContainerView;
306     this._dashboardContainer.showDashboardViewForRepresentedObject(this.dashboardManager.dashboards.default);
307
308     this._searchToolbarItem = new WebInspector.SearchBar("inspector-search", WebInspector.UIString("Search"), null, true);
309     this._searchToolbarItem.addEventListener(WebInspector.SearchBar.Event.TextChanged, this._searchTextDidChange, this);
310
311     this.toolbar.addToolbarItem(this._closeToolbarButton, WebInspector.Toolbar.Section.Control);
312
313     this.toolbar.addToolbarItem(this._undockToolbarButton, WebInspector.Toolbar.Section.Left);
314     this.toolbar.addToolbarItem(this._dockRightToolbarButton, WebInspector.Toolbar.Section.Left);
315     this.toolbar.addToolbarItem(this._dockBottomToolbarButton, WebInspector.Toolbar.Section.Left);
316
317     this.toolbar.addToolbarItem(this._reloadToolbarButton, WebInspector.Toolbar.Section.CenterLeft);
318     this.toolbar.addToolbarItem(this._downloadToolbarButton, WebInspector.Toolbar.Section.CenterLeft);
319
320     this.toolbar.addToolbarItem(this._dashboardContainer.toolbarItem, WebInspector.Toolbar.Section.Center);
321
322     if (this._inspectModeToolbarButton)
323         this.toolbar.addToolbarItem(this._inspectModeToolbarButton, WebInspector.Toolbar.Section.CenterRight);
324
325     this.toolbar.addToolbarItem(this._searchToolbarItem, WebInspector.Toolbar.Section.Right);
326
327     this.resourceDetailsSidebarPanel = new WebInspector.ResourceDetailsSidebarPanel;
328     this.domNodeDetailsSidebarPanel = new WebInspector.DOMNodeDetailsSidebarPanel;
329     this.cssStyleDetailsSidebarPanel = new WebInspector.CSSStyleDetailsSidebarPanel;
330     this.applicationCacheDetailsSidebarPanel = new WebInspector.ApplicationCacheDetailsSidebarPanel;
331     this.scopeChainDetailsSidebarPanel = new WebInspector.ScopeChainDetailsSidebarPanel;
332     this.probeDetailsSidebarPanel = new WebInspector.ProbeDetailsSidebarPanel;
333
334     if (window.LayerTreeAgent)
335         this.layerTreeDetailsSidebarPanel = new WebInspector.LayerTreeDetailsSidebarPanel;
336
337     this.modifierKeys = {altKey: false, metaKey: false, shiftKey: false};
338
339     this.toolbar.element.addEventListener("mousedown", this._toolbarMouseDown.bind(this));
340     document.getElementById("docked-resizer").addEventListener("mousedown", this._dockedResizerMouseDown.bind(this));
341
342     this._dockingAvailable = false;
343
344     this._updateDockNavigationItems();
345     this._updateToolbarHeight();
346
347     this._pendingOpenTabs = [];
348
349     let openTabTypes = this._openTabsSetting.value;
350
351     for (let i = 0; i < openTabTypes.length; ++i) {
352         let tabType = openTabTypes[i];
353         if (!this.isTabTypeAllowed(tabType)) {
354             this._pendingOpenTabs.push({tabType, index: i});
355             continue;
356         }
357
358         let tabContentView = this._tabContentViewForType(tabType);
359         if (!tabContentView)
360             continue;
361         this.tabBrowser.addTabForContentView(tabContentView, true);
362     }
363
364     this._restoreCookieForOpenTabs(WebInspector.StateRestorationType.Load);
365
366     this.tabBar.selectedTabBarItem = this._selectedTabIndexSetting.value;
367
368     if (!this.tabBar.selectedTabBarItem)
369         this.tabBar.selectedTabBarItem = 0;
370
371     // Listen to the events after restoring the saved tabs to avoid recursion.
372     this.tabBar.addEventListener(WebInspector.TabBar.Event.TabBarItemAdded, this._rememberOpenTabs, this);
373     this.tabBar.addEventListener(WebInspector.TabBar.Event.TabBarItemRemoved, this._rememberOpenTabs, this);
374     this.tabBar.addEventListener(WebInspector.TabBar.Event.TabBarItemsReordered, this._rememberOpenTabs, this);
375
376     // Signal that the frontend is now ready to receive messages.
377     InspectorFrontendAPI.loadCompleted();
378
379     // Tell the InspectorFrontendHost we loaded, which causes the window to display
380     // and pending InspectorFrontendAPI commands to be sent.
381     InspectorFrontendHost.loaded();
382
383     this._updateSplitConsoleHeight(this._splitConsoleHeightSetting.value);
384
385     if (this._showingSplitConsoleSetting.value)
386         this.showSplitConsole();
387
388     this._contentLoaded = true;
389
390     this.runBootstrapOperations();
391 };
392
393 WebInspector.isTabTypeAllowed = function(tabType)
394 {
395     switch (tabType) {
396     case WebInspector.ElementsTabContentView.Type:
397         return !!window.DOMAgent;
398     case WebInspector.NetworkTabContentView.Type:
399         return !!window.NetworkAgent && !!window.PageAgent;
400     case WebInspector.StorageTabContentView.Type:
401         return !!window.DOMStorageAgent || !!window.DatabaseAgent || !!window.IndexedDBAgent;
402     case WebInspector.TimelineTabContentView.Type:
403         return !!window.TimelineAgent;
404     }
405
406     return true;
407 };
408
409 WebInspector._tabContentViewForType = function(tabType)
410 {
411     switch (tabType) {
412     case WebInspector.ConsoleTabContentView.Type:
413         return new WebInspector.ConsoleTabContentView;
414     case WebInspector.DebuggerTabContentView.Type:
415         return new WebInspector.DebuggerTabContentView;
416     case WebInspector.ElementsTabContentView.Type:
417         return new WebInspector.ElementsTabContentView;
418     case WebInspector.NetworkTabContentView.Type:
419         return new WebInspector.NetworkTabContentView;
420     case WebInspector.NewTabContentView.Type:
421         return new WebInspector.NewTabContentView;
422     case WebInspector.ResourcesTabContentView.Type:
423         return new WebInspector.ResourcesTabContentView;
424     case WebInspector.SearchTabContentView.Type:
425         return new WebInspector.SearchTabContentView;
426     case WebInspector.StorageTabContentView.Type:
427         return new WebInspector.StorageTabContentView;
428     case WebInspector.TimelineTabContentView.Type:
429         return new WebInspector.TimelineTabContentView;
430     default:
431         console.error("Unknown tab type", tabType);
432     }
433
434     return null;
435 };
436
437 WebInspector._rememberOpenTabs = function()
438 {
439     var openTabs = [];
440
441     for (var tabBarItem of this.tabBar.tabBarItems) {
442         var tabContentView = tabBarItem.representedObject;
443         if (!(tabContentView instanceof WebInspector.TabContentView))
444             continue;
445         if (tabContentView instanceof WebInspector.SettingsTabContentView || tabContentView instanceof WebInspector.NewTabContentView)
446             continue;
447         console.assert(tabContentView.type, "Tab type can't be null, undefined, or empty string", tabContentView.type, tabContentView);
448         openTabs.push(tabContentView.type);
449     }
450
451     // Keep currently unsupported tabs in the setting at their previous index.
452     for (let {tabType, index} of this._pendingOpenTabs)
453         openTabs.insertAtIndex(tabType, index);
454
455     this._openTabsSetting.value = openTabs;
456 };
457
458 WebInspector._updateNewTabButtonState = function(event)
459 {
460     var newTabAllowed = this.isNewTabWithTypeAllowed(WebInspector.ConsoleTabContentView.Type) || this.isNewTabWithTypeAllowed(WebInspector.ElementsTabContentView.Type)
461         || this.isNewTabWithTypeAllowed(WebInspector.ResourcesTabContentView.Type) || this.isNewTabWithTypeAllowed(WebInspector.StorageTabContentView.Type)
462         || this.isNewTabWithTypeAllowed(WebInspector.TimelineTabContentView.Type) || this.isNewTabWithTypeAllowed(WebInspector.DebuggerTabContentView.Type)
463         || this.isNewTabWithTypeAllowed(WebInspector.NetworkTabContentView.Type);
464     this.tabBar.newTabItem.disabled = !newTabAllowed;
465 };
466
467 WebInspector._newTabItemClicked = function(event)
468 {
469     var tabContentView = this.tabBrowser.bestTabContentViewForClass(WebInspector.NewTabContentView);
470     if (!tabContentView)
471         tabContentView = new WebInspector.NewTabContentView;
472     this.tabBrowser.showTabForContentView(tabContentView);
473 };
474
475 WebInspector.isNewTabWithTypeAllowed = function(tabType)
476 {
477     if (!this.isTabTypeAllowed(tabType))
478         return false;
479
480     // Only allow one tab per class for now.
481     for (var tabBarItem of this.tabBar.tabBarItems) {
482         var tabContentView = tabBarItem.representedObject;
483         if (!(tabContentView instanceof WebInspector.TabContentView))
484             continue;
485         if (tabContentView.type === tabType)
486             return false;
487     }
488
489     return true;
490 };
491
492 WebInspector.createNewTab = function(tabType, newTabContentViewToReplace)
493 {
494     console.assert(this.isNewTabWithTypeAllowed(tabType));
495
496     var tabContentView = this._tabContentViewForType(tabType);
497
498     if (newTabContentViewToReplace) {
499         var insertionIndex = this.tabBar.tabBarItems.indexOf(newTabContentViewToReplace.tabBarItem);
500         this.tabBrowser.closeTabForContentView(newTabContentViewToReplace, true);
501         this.tabBrowser.showTabForContentView(tabContentView, true, insertionIndex);
502         return;
503     }
504
505     this.tabBrowser.showTabForContentView(tabContentView);
506 };
507
508 WebInspector.activateExtraDomains = function(domains)
509 {
510     this.hasExtraDomains = true;
511
512     for (var domain of domains) {
513         var agent = InspectorBackend.activateDomain(domain);
514         if (agent.enable)
515             agent.enable();
516     }
517
518     this.notifications.dispatchEventToListeners(WebInspector.Notification.ExtraDomainsActivated, {"domains": domains});
519
520     WebInspector.CSSCompletions.requestCSSCompletions();
521
522     this._updateReloadToolbarButton();
523     this._updateDownloadToolbarButton();
524
525     let stillPendingOpenTabs = [];
526     for (let {tabType, index} of this._pendingOpenTabs) {
527         if (!this.isTabTypeAllowed(tabType)) {
528             stillPendingOpenTabs.push({tabType, index});
529             continue;
530         }
531
532         let tabContentView = this._tabContentViewForType(tabType);
533         if (!tabContentView)
534             continue;
535
536         this.tabBrowser.addTabForContentView(tabContentView, true, index);
537
538         tabContentView.restoreStateFromCookie(WebInspector.StateRestorationType.Load);
539     }
540
541     this._pendingOpenTabs = stillPendingOpenTabs;
542
543     this._updateNewTabButtonState();
544 };
545
546 WebInspector.contentBrowserTreeElementForRepresentedObject = function(contentBrowser, representedObject)
547 {
548     // The console does not have a sidebar, so return a tree element here so something is shown.
549     if (representedObject === this._consoleRepresentedObject)
550         return this._consoleTreeElement;
551     return null;
552 };
553
554 WebInspector.updateWindowTitle = function()
555 {
556     var mainFrame = this.frameResourceManager.mainFrame;
557     console.assert(mainFrame);
558
559     var urlComponents = mainFrame.mainResource.urlComponents;
560
561     var lastPathComponent;
562     try {
563         lastPathComponent = decodeURIComponent(urlComponents.lastPathComponent || "");
564     } catch (e) {
565         lastPathComponent = urlComponents.lastPathComponent;
566     }
567
568     // Build a title based on the URL components.
569     if (urlComponents.host && lastPathComponent)
570         var title = this.displayNameForHost(urlComponents.host) + " \u2014 " + lastPathComponent;
571     else if (urlComponents.host)
572         var title = this.displayNameForHost(urlComponents.host);
573     else if (lastPathComponent)
574         var title = lastPathComponent;
575     else
576         var title = mainFrame.url;
577
578     // The name "inspectedURLChanged" sounds like the whole URL is required, however this is only
579     // used for updating the window title and it can be any string.
580     InspectorFrontendHost.inspectedURLChanged(title);
581 };
582
583 WebInspector.updateDockingAvailability = function(available)
584 {
585     this._dockingAvailable = available;
586
587     this._updateDockNavigationItems();
588 };
589
590 WebInspector.updateDockedState = function(side)
591 {
592     if (this._dockSide === side)
593         return;
594
595     this._dockSide = side;
596
597     this.docked = side !== "undocked";
598
599     this._ignoreToolbarModeDidChangeEvents = true;
600
601     if (side === "bottom") {
602         document.body.classList.add("docked", "bottom");
603         document.body.classList.remove("window-inactive", "right");
604     } else if (side === "right") {
605         document.body.classList.add("docked", "right");
606         document.body.classList.remove("window-inactive", "bottom");
607     } else
608         document.body.classList.remove("docked", "right", "bottom");
609
610     this._ignoreToolbarModeDidChangeEvents = false;
611
612     this._updateDockNavigationItems();
613 };
614
615 WebInspector.handlePossibleLinkClick = function(event, frame, alwaysOpenExternally)
616 {
617     var anchorElement = event.target.enclosingNodeOrSelfWithNodeName("a");
618     if (!anchorElement || !anchorElement.href)
619         return false;
620
621     if (WebInspector.isBeingEdited(anchorElement)) {
622         // Don't follow the link when it is being edited.
623         return false;
624     }
625
626     // Prevent the link from navigating, since we don't do any navigation by following links normally.
627     event.preventDefault();
628     event.stopPropagation();
629
630     this.openURL(anchorElement.href, frame, false, anchorElement.lineNumber);
631
632     return true;
633 };
634
635 WebInspector.openURL = function(url, frame, alwaysOpenExternally, lineNumber)
636 {
637     console.assert(url);
638     if (!url)
639         return;
640
641     // If alwaysOpenExternally is not defined, base it off the command/meta key for the current event.
642     if (alwaysOpenExternally === undefined || alwaysOpenExternally === null)
643         alwaysOpenExternally = window.event ? window.event.metaKey : false;
644
645     if (alwaysOpenExternally) {
646         InspectorFrontendHost.openInNewTab(url);
647         return;
648     }
649
650     var searchChildFrames = false;
651     if (!frame) {
652         frame = this.frameResourceManager.mainFrame;
653         searchChildFrames = true;
654     }
655
656     console.assert(frame);
657
658     // WebInspector.Frame.resourceForURL does not check the main resource, only sub-resources. So check both.
659     var resource = frame.url === url ? frame.mainResource : frame.resourceForURL(url, searchChildFrames);
660     if (resource) {
661         var position = new WebInspector.SourceCodePosition(lineNumber, 0);
662         this.showSourceCode(resource, position);
663         return;
664     }
665
666     InspectorFrontendHost.openInNewTab(url);
667 };
668
669 WebInspector.close = function()
670 {
671     if (this._isClosing)
672         return;
673
674     this._isClosing = true;
675
676     InspectorFrontendHost.closeWindow();
677 };
678
679 WebInspector.saveDataToFile = function(saveData, forceSaveAs)
680 {
681     console.assert(saveData);
682     if (!saveData)
683         return;
684
685     if (typeof saveData.customSaveHandler === "function") {
686         saveData.customSaveHandler(forceSaveAs);
687         return;
688     }
689
690     console.assert(saveData.url);
691     console.assert(typeof saveData.content === "string");
692     if (!saveData.url || typeof saveData.content !== "string")
693         return;
694
695     InspectorFrontendHost.save(saveData.url, saveData.content, false, forceSaveAs || saveData.forceSaveAs);
696 };
697
698 WebInspector.isConsoleFocused = function()
699 {
700     return this.quickConsole.prompt.focused;
701 };
702
703 WebInspector.isShowingSplitConsole = function()
704 {
705     return !this.splitContentBrowser.element.classList.contains("hidden");
706 };
707
708 WebInspector.doesCurrentTabSupportSplitContentBrowser = function()
709 {
710     var currentContentView = this.tabBrowser.selectedTabContentView;
711     return !currentContentView || currentContentView.supportsSplitContentBrowser;
712 };
713
714 WebInspector.toggleSplitConsole = function()
715 {
716     if (!this.doesCurrentTabSupportSplitContentBrowser()) {
717         this.showConsoleTab();
718         return;
719     }
720
721     if (this.isShowingSplitConsole())
722         this.hideSplitConsole();
723     else
724         this.showSplitConsole();
725 };
726
727 WebInspector.showSplitConsole = function()
728 {
729     if (!this.doesCurrentTabSupportSplitContentBrowser()) {
730         this.showConsoleTab();
731         return;
732     }
733
734     this.splitContentBrowser.element.classList.remove("hidden");
735
736     this._showingSplitConsoleSetting.value = true;
737
738     if (this.splitContentBrowser.currentContentView !== this.consoleContentView) {
739         // Be sure to close the view in the tab content browser before showing it in the
740         // split content browser. We can only show a content view in one browser at a time.
741         if (this.consoleContentView.parentContainer)
742             this.consoleContentView.parentContainer.closeContentView(this.consoleContentView);
743         this.splitContentBrowser.showContentView(this.consoleContentView);
744     } else {
745         // This causes the view to know it was shown and focus the prompt.
746         this.splitContentBrowser.shown();
747     }
748
749     this.quickConsole.consoleLogVisibilityChanged(true);
750 };
751
752 WebInspector.hideSplitConsole = function()
753 {
754     if (!this.isShowingSplitConsole())
755         return;
756
757     this.splitContentBrowser.element.classList.add("hidden");
758
759     this._showingSplitConsoleSetting.value = false;
760
761     // This causes the view to know it was hidden.
762     this.splitContentBrowser.hidden();
763
764     this.quickConsole.consoleLogVisibilityChanged(false);
765 };
766
767 WebInspector.showConsoleTab = function(requestedScope)
768 {
769     this.hideSplitConsole();
770
771     var scope = requestedScope || WebInspector.LogContentView.Scopes.All;
772
773     // If the requested scope is already selected and the console is showing, then switch back to All.
774     if (this.isShowingConsoleTab() && this.consoleContentView.scopeBar.item(scope).selected)
775         scope = WebInspector.LogContentView.Scopes.All;
776
777     if (requestedScope || !this.consoleContentView.scopeBar.selectedItems.length)
778         this.consoleContentView.scopeBar.item(scope).selected = true;
779
780     this.showRepresentedObject(this._consoleRepresentedObject);
781
782     console.assert(this.isShowingConsoleTab());
783 };
784
785 WebInspector.isShowingConsoleTab = function()
786 {
787     return this.tabBrowser.selectedTabContentView instanceof WebInspector.ConsoleTabContentView;
788 };
789
790 WebInspector.showElementsTab = function()
791 {
792     var tabContentView = this.tabBrowser.bestTabContentViewForClass(WebInspector.ElementsTabContentView);
793     if (!tabContentView)
794         tabContentView = new WebInspector.ElementsTabContentView;
795     this.tabBrowser.showTabForContentView(tabContentView);
796 };
797
798 WebInspector.showDebuggerTab = function(breakpointToSelect, showScopeChainDetailsSidebarPanel)
799 {
800     var tabContentView = this.tabBrowser.bestTabContentViewForClass(WebInspector.DebuggerTabContentView);
801     if (!tabContentView)
802         tabContentView = new WebInspector.DebuggerTabContentView;
803
804     if (breakpointToSelect instanceof WebInspector.Breakpoint)
805         tabContentView.revealAndSelectBreakpoint(breakpointToSelect);
806
807     if (showScopeChainDetailsSidebarPanel)
808         tabContentView.showScopeChainDetailsSidebarPanel();
809
810     this.tabBrowser.showTabForContentView(tabContentView);
811 };
812
813 WebInspector.isShowingDebuggerTab = function()
814 {
815     return this.tabBrowser.selectedTabContentView instanceof WebInspector.DebuggerTabContentView;
816 };
817
818 WebInspector.showResourcesTab = function()
819 {
820     var tabContentView = this.tabBrowser.bestTabContentViewForClass(WebInspector.ResourcesTabContentView);
821     if (!tabContentView)
822         tabContentView = new WebInspector.ResourcesTabContentView;
823     this.tabBrowser.showTabForContentView(tabContentView);
824 };
825
826 WebInspector.showStorageTab = function()
827 {
828     var tabContentView = this.tabBrowser.bestTabContentViewForClass(WebInspector.StorageTabContentView);
829     if (!tabContentView)
830         tabContentView = new WebInspector.StorageTabContentView;
831     this.tabBrowser.showTabForContentView(tabContentView);
832 };
833
834 WebInspector.showNetworkTab = function()
835 {
836     var tabContentView = this.tabBrowser.bestTabContentViewForClass(WebInspector.NetworkTabContentView);
837     if (!tabContentView)
838         tabContentView = new WebInspector.NetworkTabContentView;
839     this.tabBrowser.showTabForContentView(tabContentView);
840 };
841
842 WebInspector.showTimelineTab = function()
843 {
844     var tabContentView = this.tabBrowser.bestTabContentViewForClass(WebInspector.TimelineTabContentView);
845     if (!tabContentView)
846         tabContentView = new WebInspector.TimelineTabContentView;
847     this.tabBrowser.showTabForContentView(tabContentView);
848 };
849
850 WebInspector.UIString = function(string, vararg)
851 {
852     if (WebInspector.dontLocalizeUserInterface)
853         return string;
854
855     if (window.localizedStrings && string in window.localizedStrings)
856         return window.localizedStrings[string];
857
858     if (!this._missingLocalizedStrings)
859         this._missingLocalizedStrings = {};
860
861     if (!(string in this._missingLocalizedStrings)) {
862         console.error("Localized string \"" + string + "\" was not found.");
863         this._missingLocalizedStrings[string] = true;
864     }
865
866     return "LOCALIZED STRING NOT FOUND";
867 };
868
869 WebInspector.restoreFocusFromElement = function(element)
870 {
871     if (element && element.isSelfOrAncestor(this.currentFocusElement))
872         this.previousFocusElement.focus();
873 };
874
875 WebInspector.toggleNavigationSidebar = function(event)
876 {
877     if (!this.navigationSidebar.collapsed || !this.navigationSidebar.sidebarPanels.length) {
878         this.navigationSidebar.collapsed = true;
879         return;
880     }
881
882     if (!this.navigationSidebar.selectedSidebarPanel)
883         this.navigationSidebar.selectedSidebarPanel = this.navigationSidebar.sidebarPanels[0];
884     this.navigationSidebar.collapsed = false;
885 };
886
887 WebInspector.toggleDetailsSidebar = function(event)
888 {
889     if (!this.detailsSidebar.collapsed || !this.detailsSidebar.sidebarPanels.length) {
890         this.detailsSidebar.collapsed = true;
891         return;
892     }
893
894     if (!this.detailsSidebar.selectedSidebarPanel)
895         this.detailsSidebar.selectedSidebarPanel = this.detailsSidebar.sidebarPanels[0];
896     this.detailsSidebar.collapsed = false;
897 };
898
899 WebInspector.tabContentViewClassForRepresentedObject = function(representedObject)
900 {
901     if (representedObject instanceof WebInspector.DOMTree)
902         return WebInspector.ElementsTabContentView;
903
904     if (representedObject instanceof WebInspector.TimelineRecording)
905         return WebInspector.TimelineTabContentView;
906
907     // We only support one console tab right now. So this isn't an instanceof check.
908     if (representedObject === this._consoleRepresentedObject)
909         return WebInspector.ConsoleTabContentView;
910
911     if (WebInspector.debuggerManager.paused) {
912         if (representedObject instanceof WebInspector.Script)
913             return WebInspector.DebuggerTabContentView;
914
915         if (representedObject instanceof WebInspector.Resource && (representedObject.type === WebInspector.Resource.Type.Document || representedObject.type === WebInspector.Resource.Type.Script))
916             return WebInspector.DebuggerTabContentView;
917     }
918
919     if (representedObject instanceof WebInspector.Frame || representedObject instanceof WebInspector.Resource || representedObject instanceof WebInspector.Script)
920         return WebInspector.ResourcesTabContentView;
921
922     // FIXME: Move Content Flows to the Elements tab?
923     if (representedObject instanceof WebInspector.ContentFlow)
924         return WebInspector.ResourcesTabContentView;
925
926     // FIXME: Move these to a Storage tab.
927     if (representedObject instanceof WebInspector.DOMStorageObject || representedObject instanceof WebInspector.CookieStorageObject ||
928         representedObject instanceof WebInspector.DatabaseTableObject || representedObject instanceof WebInspector.DatabaseObject ||
929         representedObject instanceof WebInspector.ApplicationCacheFrame || representedObject instanceof WebInspector.IndexedDatabaseObjectStore ||
930         representedObject instanceof WebInspector.IndexedDatabaseObjectStoreIndex)
931         return WebInspector.ResourcesTabContentView;
932
933     return null;
934 };
935
936 WebInspector.tabContentViewForRepresentedObject = function(representedObject)
937 {
938     var tabContentView = this.tabBrowser.bestTabContentViewForRepresentedObject(representedObject);
939     if (tabContentView)
940         return tabContentView;
941
942     var tabContentViewClass = this.tabContentViewClassForRepresentedObject(representedObject);
943     if (!tabContentViewClass) {
944         console.error("Unknown representedObject, couldn't create TabContentView.", representedObject);
945         return null;
946     }
947
948     tabContentView = new tabContentViewClass;
949
950     this.tabBrowser.addTabForContentView(tabContentView);
951
952     return tabContentView;
953 };
954
955 WebInspector.showRepresentedObject = function(representedObject, cookie)
956 {
957     var tabContentView = this.tabContentViewForRepresentedObject(representedObject);
958     console.assert(tabContentView);
959     if (!tabContentView)
960         return;
961
962     this.tabBrowser.showTabForContentView(tabContentView);
963     tabContentView.showRepresentedObject(representedObject, cookie);
964 };
965
966 WebInspector.showMainFrameDOMTree = function(nodeToSelect)
967 {
968     console.assert(WebInspector.frameResourceManager.mainFrame);
969     if (!WebInspector.frameResourceManager.mainFrame)
970         return;
971     this.showRepresentedObject(WebInspector.frameResourceManager.mainFrame.domTree, {nodeToSelect});
972 };
973
974 WebInspector.showContentFlowDOMTree = function(contentFlow, nodeToSelect)
975 {
976     this.showRepresentedObject(contentFlow, {nodeToSelect});
977 };
978
979 WebInspector.showSourceCodeForFrame = function(frameIdentifier)
980 {
981     var frame = WebInspector.frameResourceManager.frameForIdentifier(frameIdentifier);
982     if (!frame) {
983         this._frameIdentifierToShowSourceCodeWhenAvailable = frameIdentifier;
984         return;
985     }
986
987     this._frameIdentifierToShowSourceCodeWhenAvailable = undefined;
988
989     this.showRepresentedObject(frame);
990 };
991
992 WebInspector.showSourceCode = function(sourceCode, positionToReveal, textRangeToSelect, forceUnformatted)
993 {
994     console.assert(!positionToReveal || positionToReveal instanceof WebInspector.SourceCodePosition, positionToReveal);
995     var representedObject = sourceCode;
996
997     if (representedObject instanceof WebInspector.Script) {
998         // A script represented by a resource should always show the resource.
999         representedObject = representedObject.resource || representedObject;
1000     }
1001
1002     var cookie = positionToReveal ? {lineNumber: positionToReveal.lineNumber, columnNumber: positionToReveal.columnNumber} : {};
1003     this.showRepresentedObject(representedObject, cookie);
1004 };
1005
1006 WebInspector.showSourceCodeLocation = function(sourceCodeLocation)
1007 {
1008     this.showSourceCode(sourceCodeLocation.displaySourceCode, sourceCodeLocation.displayPosition());
1009 };
1010
1011 WebInspector.showOriginalUnformattedSourceCodeLocation = function(sourceCodeLocation)
1012 {
1013     this.showSourceCode(sourceCodeLocation.sourceCode, sourceCodeLocation.position(), null, true);
1014 };
1015
1016 WebInspector.showOriginalOrFormattedSourceCodeLocation = function(sourceCodeLocation)
1017 {
1018     this.showSourceCode(sourceCodeLocation.sourceCode, sourceCodeLocation.formattedPosition());
1019 };
1020
1021 WebInspector.showOriginalOrFormattedSourceCodeTextRange = function(sourceCodeTextRange)
1022 {
1023     var textRangeToSelect = sourceCodeTextRange.formattedTextRange;
1024     this.showSourceCode(sourceCodeTextRange.sourceCode, textRangeToSelect.startPosition(), textRangeToSelect);
1025 };
1026
1027 WebInspector.showResourceRequest = function(resource)
1028 {
1029     this.showRepresentedObject(resource, {[WebInspector.ResourceClusterContentView.ContentViewIdentifierCookieKey]: WebInspector.ResourceClusterContentView.RequestIdentifier});
1030 };
1031
1032 WebInspector.debuggerToggleBreakpoints = function(event)
1033 {
1034     WebInspector.debuggerManager.breakpointsEnabled = !WebInspector.debuggerManager.breakpointsEnabled;
1035 };
1036
1037 WebInspector.debuggerPauseResumeToggle = function(event)
1038 {
1039     if (WebInspector.debuggerManager.paused)
1040         WebInspector.debuggerManager.resume();
1041     else
1042         WebInspector.debuggerManager.pause();
1043 };
1044
1045 WebInspector.debuggerStepOver = function(event)
1046 {
1047     WebInspector.debuggerManager.stepOver();
1048 };
1049
1050 WebInspector.debuggerStepInto = function(event)
1051 {
1052     WebInspector.debuggerManager.stepInto();
1053 };
1054
1055 WebInspector.debuggerStepOut = function(event)
1056 {
1057     WebInspector.debuggerManager.stepOut();
1058 };
1059
1060 WebInspector._searchTextDidChange = function(event)
1061 {
1062     var tabContentView = this.tabBrowser.bestTabContentViewForClass(WebInspector.SearchTabContentView);
1063     if (!tabContentView)
1064         tabContentView = new WebInspector.SearchTabContentView;
1065
1066     var searchQuery = this._searchToolbarItem.text;
1067     this._searchToolbarItem.text = "";
1068
1069     this.tabBrowser.showTabForContentView(tabContentView);
1070
1071     tabContentView.performSearch(searchQuery);
1072 };
1073
1074 WebInspector._focusSearchField = function(event)
1075 {
1076     if (this.tabBrowser.selectedTabContentView instanceof WebInspector.SearchTabContentView) {
1077         this.tabBrowser.selectedTabContentView.focusSearchField();
1078         return;
1079     }
1080
1081     this._searchToolbarItem.focus();
1082 };
1083
1084 WebInspector._focusChanged = function(event)
1085 {
1086     // Make a caret selection inside the focused element if there isn't a range selection and there isn't already
1087     // a caret selection inside. This is needed (at least) to remove caret from console when focus is moved.
1088     // The selection change should not apply to text fields and text areas either.
1089
1090     if (WebInspector.isEventTargetAnEditableField(event))
1091         return;
1092
1093     var selection = window.getSelection();
1094     if (!selection.isCollapsed)
1095         return;
1096
1097     var element = event.target;
1098
1099     if (element !== this.currentFocusElement) {
1100         this.previousFocusElement = this.currentFocusElement;
1101         this.currentFocusElement = element;
1102     }
1103
1104     if (element.isInsertionCaretInside())
1105         return;
1106
1107     var selectionRange = element.ownerDocument.createRange();
1108     selectionRange.setStart(element, 0);
1109     selectionRange.setEnd(element, 0);
1110
1111     selection.removeAllRanges();
1112     selection.addRange(selectionRange);
1113 };
1114
1115 WebInspector._mouseWasClicked = function(event)
1116 {
1117     this.handlePossibleLinkClick(event);
1118 };
1119
1120 WebInspector._dragOver = function(event)
1121 {
1122     // Do nothing if another event listener handled the event already.
1123     if (event.defaultPrevented)
1124         return;
1125
1126     // Allow dropping into editable areas.
1127     if (WebInspector.isEventTargetAnEditableField(event))
1128         return;
1129
1130     // Prevent the drop from being accepted.
1131     event.dataTransfer.dropEffect = "none";
1132     event.preventDefault();
1133 };
1134
1135 WebInspector._captureDidStart = function(event)
1136 {
1137     this._dashboardContainer.showDashboardViewForRepresentedObject(this.dashboardManager.dashboards.replay);
1138 };
1139
1140 WebInspector._debuggerDidPause = function(event)
1141 {
1142     this.showDebuggerTab(null, true);
1143
1144     this._dashboardContainer.showDashboardViewForRepresentedObject(this.dashboardManager.dashboards.debugger);
1145
1146     InspectorFrontendHost.bringToFront();
1147 };
1148
1149 WebInspector._debuggerDidResume = function(event)
1150 {
1151     this._dashboardContainer.closeDashboardViewForRepresentedObject(this.dashboardManager.dashboards.debugger);
1152 };
1153
1154 WebInspector._frameWasAdded = function(event)
1155 {
1156     if (!this._frameIdentifierToShowSourceCodeWhenAvailable)
1157         return;
1158
1159     var frame = event.data.frame;
1160     if (frame.id !== this._frameIdentifierToShowSourceCodeWhenAvailable)
1161         return;
1162
1163     function delayedWork()
1164     {
1165         this.showSourceCodeForFrame(frame.id);
1166     }
1167
1168     // Delay showing the frame since FrameWasAdded is called before MainFrameChanged.
1169     // Calling showSourceCodeForFrame before MainFrameChanged will show the frame then close it.
1170     setTimeout(delayedWork.bind(this));
1171 };
1172
1173 WebInspector._mainFrameDidChange = function(event)
1174 {
1175     this._updateDownloadToolbarButton();
1176
1177     this.updateWindowTitle();
1178 };
1179
1180 WebInspector._mainResourceDidChange = function(event)
1181 {
1182     if (!event.target.isMainFrame())
1183         return;
1184
1185     this._inProvisionalLoad = false;
1186
1187     // Run cookie restoration after we are sure all of the Tabs and NavigationSidebarPanels
1188     // have updated with respect to the main resource change.
1189     setTimeout(this._restoreCookieForOpenTabs.bind(this, WebInspector.StateRestorationType.Navigation));
1190
1191     this._updateDownloadToolbarButton();
1192
1193     this.updateWindowTitle();
1194 };
1195
1196 WebInspector._provisionalLoadStarted = function(event)
1197 {
1198     if (!event.target.isMainFrame())
1199         return;
1200
1201     this._saveCookieForOpenTabs();
1202
1203     this._inProvisionalLoad = true;
1204 };
1205
1206 WebInspector._restoreCookieForOpenTabs = function(restorationType)
1207 {
1208     for (var tabBarItem of this.tabBar.tabBarItems) {
1209         var tabContentView = tabBarItem.representedObject;
1210         if (!(tabContentView instanceof WebInspector.TabContentView))
1211             continue;
1212         tabContentView.restoreStateFromCookie(restorationType);
1213     }
1214 };
1215
1216 WebInspector._saveCookieForOpenTabs = function()
1217 {
1218     for (var tabBarItem of this.tabBar.tabBarItems) {
1219         var tabContentView = tabBarItem.representedObject;
1220         if (!(tabContentView instanceof WebInspector.TabContentView))
1221             continue;
1222         tabContentView.saveStateToCookie();
1223     }
1224 };
1225
1226 WebInspector._windowFocused = function(event)
1227 {
1228     if (event.target.document.nodeType !== Node.DOCUMENT_NODE)
1229         return;
1230
1231     // FIXME: We should use the :window-inactive pseudo class once https://webkit.org/b/38927 is fixed.
1232     document.body.classList.remove(this.docked ? "window-docked-inactive" : "window-inactive");
1233 };
1234
1235 WebInspector._windowBlurred = function(event)
1236 {
1237     if (event.target.document.nodeType !== Node.DOCUMENT_NODE)
1238         return;
1239
1240     // FIXME: We should use the :window-inactive pseudo class once https://webkit.org/b/38927 is fixed.
1241     document.body.classList.add(this.docked ? "window-docked-inactive" : "window-inactive");
1242 };
1243
1244 WebInspector._windowResized = function(event)
1245 {
1246     this.toolbar.updateLayout();
1247     this._tabBrowserSizeDidChange();
1248 };
1249
1250 WebInspector._updateModifierKeys = function(event)
1251 {
1252     var didChange = this.modifierKeys.altKey !== event.altKey || this.modifierKeys.metaKey !== event.metaKey || this.modifierKeys.shiftKey !== event.shiftKey;
1253
1254     this.modifierKeys = {altKey: event.altKey, metaKey: event.metaKey, shiftKey: event.shiftKey};
1255
1256     if (didChange)
1257         this.notifications.dispatchEventToListeners(WebInspector.Notification.GlobalModifierKeysDidChange, event);
1258 };
1259
1260 WebInspector._windowKeyDown = function(event)
1261 {
1262     this._updateModifierKeys(event);
1263 };
1264
1265 WebInspector._windowKeyUp = function(event)
1266 {
1267     this._updateModifierKeys(event);
1268 };
1269
1270 WebInspector._mouseMoved = function(event)
1271 {
1272     this._updateModifierKeys(event);
1273     this.mouseCoords = {
1274         x: event.pageX,
1275         y: event.pageY
1276     };
1277 };
1278
1279 WebInspector._pageHidden = function(event)
1280 {
1281     this._saveCookieForOpenTabs();
1282 };
1283
1284 WebInspector._undock = function(event)
1285 {
1286     InspectorFrontendHost.requestSetDockSide("undocked");
1287 };
1288
1289 WebInspector._dockBottom = function(event)
1290 {
1291     InspectorFrontendHost.requestSetDockSide("bottom");
1292 };
1293
1294 WebInspector._dockRight = function(event)
1295 {
1296     InspectorFrontendHost.requestSetDockSide("right");
1297 };
1298
1299 WebInspector._updateDockNavigationItems = function()
1300 {
1301     if (this._dockingAvailable || this.docked) {
1302         this._closeToolbarButton.hidden = !this.docked;
1303         this._undockToolbarButton.hidden = this._dockSide === "undocked";
1304         this._dockBottomToolbarButton.hidden = this._dockSide === "bottom";
1305         this._dockRightToolbarButton.hidden = this._dockSide === "right";
1306     } else {
1307         this._closeToolbarButton.hidden = true;
1308         this._undockToolbarButton.hidden = true;
1309         this._dockBottomToolbarButton.hidden = true;
1310         this._dockRightToolbarButton.hidden = true;
1311     }
1312 };
1313
1314 WebInspector._tabBrowserSizeDidChange = function()
1315 {
1316     this.tabBrowser.updateLayout();
1317     this.splitContentBrowser.updateLayout();
1318     this.quickConsole.updateLayout();
1319 };
1320
1321 WebInspector._quickConsoleDidResize = function(event)
1322 {
1323     this.tabBrowser.updateLayout();
1324 };
1325
1326 WebInspector._sidebarWidthDidChange = function(event)
1327 {
1328     this._tabBrowserSizeDidChange();
1329 };
1330
1331 WebInspector._updateToolbarHeight = function()
1332 {
1333     if (WebInspector.Platform.name === "mac" && WebInspector.Platform.version.release < 10)
1334         InspectorFrontendHost.setToolbarHeight(this.toolbar.element.offsetHeight);
1335 };
1336
1337 WebInspector._tabBrowserSelectedTabContentViewDidChange = function(event)
1338 {
1339     if (this.tabBar.selectedTabBarItem)
1340         this._selectedTabIndexSetting.value = this.tabBar.tabBarItems.indexOf(this.tabBar.selectedTabBarItem);
1341
1342     if (!this.doesCurrentTabSupportSplitContentBrowser())
1343         this.hideSplitConsole();
1344
1345     if (!this.isShowingSplitConsole())
1346         this.quickConsole.consoleLogVisibilityChanged(this.isShowingConsoleTab());
1347 };
1348
1349 WebInspector._initializeWebSocketIfNeeded = function()
1350 {
1351     if (!InspectorFrontendHost.initializeWebSocket)
1352         return;
1353
1354     var queryParams = parseLocationQueryParameters();
1355
1356     if ("ws" in queryParams)
1357         var url = "ws://" + queryParams.ws;
1358     else if ("page" in queryParams) {
1359         var page = queryParams.page;
1360         var host = "host" in queryParams ? queryParams.host : window.location.host;
1361         var url = "ws://" + host + "/devtools/page/" + page;
1362     }
1363
1364     if (!url)
1365         return;
1366
1367     InspectorFrontendHost.initializeWebSocket(url);
1368 };
1369
1370 WebInspector._updateSplitConsoleHeight = function(height)
1371 {
1372     const minimumHeight = 64;
1373     const maximumHeight = window.innerHeight * 0.55;
1374
1375     height = Math.max(minimumHeight, Math.min(height, maximumHeight));
1376
1377     this.splitContentBrowser.element.style.height = height + "px";
1378 };
1379
1380 WebInspector._consoleResizerMouseDown = function(event)
1381 {
1382     if (event.button !== 0 || event.ctrlKey)
1383         return;
1384
1385     // Only start dragging if the target is one of the elements that we expect.
1386     if (!event.target.classList.contains("navigation-bar") && !event.target.classList.contains("flexible-space"))
1387         return;
1388
1389     var resizerElement = event.target;
1390     var mouseOffset = resizerElement.offsetHeight - (event.pageY - resizerElement.totalOffsetTop);
1391
1392     function dockedResizerDrag(event)
1393     {
1394         if (event.button !== 0)
1395             return;
1396
1397         var height = window.innerHeight - event.pageY - mouseOffset;
1398
1399         this._splitConsoleHeightSetting.value = height;
1400
1401         this._updateSplitConsoleHeight(height);
1402
1403         this.quickConsole.dispatchEventToListeners(WebInspector.QuickConsole.Event.DidResize);
1404     }
1405
1406     function dockedResizerDragEnd(event)
1407     {
1408         if (event.button !== 0)
1409             return;
1410
1411         this.elementDragEnd(event);
1412     }
1413
1414     this.elementDragStart(resizerElement, dockedResizerDrag.bind(this), dockedResizerDragEnd.bind(this), event, "row-resize");
1415 };
1416
1417 WebInspector._toolbarMouseDown = function(event)
1418 {
1419     if (event.ctrlKey)
1420         return;
1421
1422     if (this._dockSide === "right")
1423         return;
1424
1425     if (this.docked)
1426         this._dockedResizerMouseDown(event);
1427     else
1428         this._moveWindowMouseDown(event);
1429 };
1430
1431 WebInspector._dockedResizerMouseDown = function(event)
1432 {
1433     if (event.button !== 0 || event.ctrlKey)
1434         return;
1435
1436     if (!this.docked)
1437         return;
1438
1439     // Only start dragging if the target is one of the elements that we expect.
1440     if (event.target.id !== "docked-resizer" && !event.target.classList.contains("toolbar") &&
1441         !event.target.classList.contains("flexible-space") && !event.target.classList.contains("item-section"))
1442         return;
1443
1444     var windowProperty = this._dockSide === "bottom" ? "innerHeight" : "innerWidth";
1445     var eventScreenProperty = this._dockSide === "bottom" ? "screenY" : "screenX";
1446     var eventClientProperty = this._dockSide === "bottom" ? "clientY" : "clientX";
1447
1448     var resizerElement = event.target;
1449     var firstClientPosition = event[eventClientProperty];
1450     var lastScreenPosition = event[eventScreenProperty];
1451
1452     function dockedResizerDrag(event)
1453     {
1454         if (event.button !== 0)
1455             return;
1456
1457         var position = event[eventScreenProperty];
1458         var delta = position - lastScreenPosition;
1459         var clientPosition = event[eventClientProperty];
1460
1461         lastScreenPosition = position;
1462
1463         // If delta is positive the docked Inspector size is decreasing, in which case the cursor client position
1464         // with respect to the target cannot be less than the first mouse down position within the target.
1465         if (delta > 0 && clientPosition < firstClientPosition)
1466             return;
1467
1468         // If delta is negative the docked Inspector size is increasing, in which case the cursor client position
1469         // with respect to the target cannot be greater than the first mouse down position within the target.
1470         if (delta < 0 && clientPosition > firstClientPosition)
1471             return;
1472
1473         var dimension = Math.max(0, window[windowProperty] - delta);
1474
1475         if (this._dockSide === "bottom")
1476             InspectorFrontendHost.setAttachedWindowHeight(dimension);
1477         else
1478             InspectorFrontendHost.setAttachedWindowWidth(dimension);
1479     }
1480
1481     function dockedResizerDragEnd(event)
1482     {
1483         if (event.button !== 0)
1484             return;
1485
1486         WebInspector.elementDragEnd(event);
1487     }
1488
1489     WebInspector.elementDragStart(resizerElement, dockedResizerDrag.bind(this), dockedResizerDragEnd.bind(this), event, this._dockSide === "bottom" ? "row-resize" : "col-resize");
1490 };
1491
1492 WebInspector._moveWindowMouseDown = function(event)
1493 {
1494     console.assert(!this.docked);
1495
1496     if (event.button !== 0 || event.ctrlKey)
1497         return;
1498
1499     // Only start dragging if the target is one of the elements that we expect.
1500     if (!event.target.classList.contains("toolbar") && !event.target.classList.contains("flexible-space") &&
1501         !event.target.classList.contains("item-section"))
1502         return;
1503
1504     if (WebInspector.Platform.name === "mac") {
1505         // New Mac releases can start a window drag.
1506         if (WebInspector.Platform.version.release >= 11) {
1507             InspectorFrontendHost.startWindowDrag();
1508             event.preventDefault();
1509             return;
1510         }
1511
1512         // Ignore dragging on the top of the toolbar on Mac if the system handles it.
1513         if (WebInspector.Platform.version.release === 10) {
1514             const windowDragHandledTitleBarHeight = 22;
1515             if (event.pageY < windowDragHandledTitleBarHeight) {
1516                 event.preventDefault();
1517                 return;
1518             }
1519         }
1520     }
1521
1522     var lastScreenX = event.screenX;
1523     var lastScreenY = event.screenY;
1524
1525     function toolbarDrag(event)
1526     {
1527         if (event.button !== 0)
1528             return;
1529
1530         var x = event.screenX - lastScreenX;
1531         var y = event.screenY - lastScreenY;
1532
1533         InspectorFrontendHost.moveWindowBy(x, y);
1534
1535         lastScreenX = event.screenX;
1536         lastScreenY = event.screenY;
1537     }
1538
1539     function toolbarDragEnd(event)
1540     {
1541         if (event.button !== 0)
1542             return;
1543
1544         WebInspector.elementDragEnd(event);
1545     }
1546
1547     WebInspector.elementDragStart(event.target, toolbarDrag, toolbarDragEnd, event, "default");
1548 };
1549
1550 WebInspector._storageWasInspected = function(event)
1551 {
1552     this.showStorageTab();
1553 };
1554
1555 WebInspector._domNodeWasInspected = function(event)
1556 {
1557     this.domTreeManager.highlightDOMNodeForTwoSeconds(event.data.node.id);
1558
1559     InspectorFrontendHost.bringToFront();
1560
1561     this.showElementsTab();
1562     this.showMainFrameDOMTree(event.data.node);
1563 };
1564
1565 WebInspector._inspectModeStateChanged = function(event)
1566 {
1567     this._inspectModeToolbarButton.activated = this.domTreeManager.inspectModeEnabled;
1568 };
1569
1570 WebInspector._toggleInspectMode = function(event)
1571 {
1572     this.domTreeManager.inspectModeEnabled = !this.domTreeManager.inspectModeEnabled;
1573 };
1574
1575 WebInspector._downloadWebArchive = function(event)
1576 {
1577     this.archiveMainFrame();
1578 };
1579
1580 WebInspector._reloadPage = function(event)
1581 {
1582     PageAgent.reload();
1583 };
1584
1585 WebInspector._reloadPageClicked = function(event)
1586 {
1587     // Ignore cache when the shift key is pressed.
1588     PageAgent.reload(window.event ? window.event.shiftKey : false);
1589 };
1590
1591 WebInspector._reloadPageIgnoringCache = function(event)
1592 {
1593     PageAgent.reload(true);
1594 };
1595
1596 WebInspector._updateReloadToolbarButton = function()
1597 {
1598     if (!window.PageAgent) {
1599         this._reloadToolbarButton.hidden = true;
1600         return;
1601     }
1602
1603     this._reloadToolbarButton.hidden = false;
1604 };
1605
1606 WebInspector._updateDownloadToolbarButton = function()
1607 {
1608     // COMPATIBILITY (iOS 7): Page.archive did not exist yet.
1609     if (!window.PageAgent || !PageAgent.archive || this.debuggableType !== WebInspector.DebuggableType.Web) {
1610         this._downloadToolbarButton.hidden = true;
1611         return;
1612     }
1613
1614     if (this._downloadingPage) {
1615         this._downloadToolbarButton.enabled = false;
1616         return;
1617     }
1618
1619     this._downloadToolbarButton.enabled = this.canArchiveMainFrame();
1620 };
1621
1622 WebInspector._toggleInspectMode = function(event)
1623 {
1624     this.domTreeManager.inspectModeEnabled = !this.domTreeManager.inspectModeEnabled;
1625 };
1626
1627 WebInspector._showConsoleTab = function(event)
1628 {
1629     this.showConsoleTab();
1630 };
1631
1632 WebInspector._focusConsolePrompt = function(event)
1633 {
1634     this.quickConsole.prompt.focus();
1635 };
1636
1637 WebInspector._focusedContentView = function()
1638 {
1639     if (this.tabBrowser.element.isSelfOrAncestor(this.currentFocusElement)) {
1640         var tabContentView = this.tabBrowser.selectedTabContentView;
1641         if (tabContentView instanceof WebInspector.ContentBrowserTabContentView)
1642             return tabContentView.contentBrowser.currentContentView;
1643         return tabContentView;
1644     }
1645     if (this.splitContentBrowser.element.isSelfOrAncestor(this.currentFocusElement))
1646         return  this.splitContentBrowser.currentContentView;
1647     return null;
1648 };
1649
1650 WebInspector._beforecopy = function(event)
1651 {
1652     var selection = window.getSelection();
1653
1654     // If there is no selection, see if the focused element or focused ContentView can handle the copy event.
1655     if (selection.isCollapsed && !WebInspector.isEventTargetAnEditableField(event)) {
1656         var focusedCopyHandler = this.currentFocusElement && this.currentFocusElement.copyHandler;
1657         if (focusedCopyHandler && typeof focusedCopyHandler.handleBeforeCopyEvent === "function") {
1658             focusedCopyHandler.handleBeforeCopyEvent(event);
1659             if (event.defaultPrevented)
1660                 return;
1661         }
1662
1663         var focusedContentView = this._focusedContentView();
1664         if (focusedContentView && typeof focusedContentView.handleCopyEvent === "function") {
1665             event.preventDefault();
1666             return;
1667         }
1668
1669         return;
1670     }
1671
1672     if (selection.isCollapsed)
1673         return;
1674
1675     // Say we can handle it (by preventing default) to remove word break characters.
1676     event.preventDefault();
1677 };
1678
1679 WebInspector._copy = function(event)
1680 {
1681     var selection = window.getSelection();
1682
1683     // If there is no selection, pass the copy event on to the focused element or focused ContentView.
1684     if (selection.isCollapsed && !WebInspector.isEventTargetAnEditableField(event)) {
1685         var focusedCopyHandler = this.currentFocusElement && this.currentFocusElement.copyHandler;
1686         if (focusedCopyHandler && typeof focusedCopyHandler.handleCopyEvent === "function") {
1687             focusedCopyHandler.handleCopyEvent(event);
1688             if (event.defaultPrevented)
1689                 return;
1690         }
1691
1692         var focusedContentView = this._focusedContentView();
1693         if (focusedContentView && typeof focusedContentView.handleCopyEvent === "function") {
1694             focusedContentView.handleCopyEvent(event);
1695             return;
1696         }
1697
1698         return;
1699     }
1700
1701     if (selection.isCollapsed)
1702         return;
1703
1704     // Remove word break characters from the selection before putting it on the pasteboard.
1705     var selectionString = selection.toString().removeWordBreakCharacters();
1706     event.clipboardData.setData("text/plain", selectionString);
1707     event.preventDefault();
1708 };
1709
1710 WebInspector._generateDisclosureTriangleImages = function()
1711 {
1712     var specifications = {};
1713     specifications["normal"] = {fillColor: [140, 140, 140, 1]};
1714     specifications["normal-active"] = {fillColor: [128, 128, 128, 1]};
1715
1716     generateColoredImagesForCSS("Images/DisclosureTriangleSmallOpen.svg", specifications, 13, 13, "disclosure-triangle-small-open-");
1717     generateColoredImagesForCSS("Images/DisclosureTriangleSmallClosed.svg", specifications, 13, 13, "disclosure-triangle-small-closed-");
1718
1719     specifications["selected"] = {fillColor: [255, 255, 255, 1]};
1720
1721     generateColoredImagesForCSS("Images/DisclosureTriangleTinyOpen.svg", specifications, 8, 8, "disclosure-triangle-tiny-open-");
1722     generateColoredImagesForCSS("Images/DisclosureTriangleTinyClosed.svg", specifications, 8, 8, "disclosure-triangle-tiny-closed-");
1723 };
1724
1725 WebInspector.elementDragStart = function(element, dividerDrag, elementDragEnd, event, cursor, eventTarget)
1726 {
1727     if (WebInspector._elementDraggingEventListener || WebInspector._elementEndDraggingEventListener)
1728         WebInspector.elementDragEnd(event);
1729
1730     if (element) {
1731         // Install glass pane
1732         if (WebInspector._elementDraggingGlassPane)
1733             WebInspector._elementDraggingGlassPane.remove();
1734
1735         var glassPane = document.createElement("div");
1736         glassPane.style.cssText = "position:absolute;top:0;bottom:0;left:0;right:0;opacity:0;z-index:1";
1737         glassPane.id = "glass-pane-for-drag";
1738         element.ownerDocument.body.appendChild(glassPane);
1739         WebInspector._elementDraggingGlassPane = glassPane;
1740     }
1741
1742     WebInspector._elementDraggingEventListener = dividerDrag;
1743     WebInspector._elementEndDraggingEventListener = elementDragEnd;
1744
1745     var targetDocument = event.target.ownerDocument;
1746
1747     WebInspector._elementDraggingEventTarget = eventTarget || targetDocument;
1748     WebInspector._elementDraggingEventTarget.addEventListener("mousemove", dividerDrag, true);
1749     WebInspector._elementDraggingEventTarget.addEventListener("mouseup", elementDragEnd, true);
1750
1751     targetDocument.body.style.cursor = cursor;
1752
1753     event.preventDefault();
1754 };
1755
1756 WebInspector.elementDragEnd = function(event)
1757 {
1758     WebInspector._elementDraggingEventTarget.removeEventListener("mousemove", WebInspector._elementDraggingEventListener, true);
1759     WebInspector._elementDraggingEventTarget.removeEventListener("mouseup", WebInspector._elementEndDraggingEventListener, true);
1760
1761     event.target.ownerDocument.body.style.removeProperty("cursor");
1762
1763     if (WebInspector._elementDraggingGlassPane)
1764         WebInspector._elementDraggingGlassPane.remove();
1765
1766     delete WebInspector._elementDraggingGlassPane;
1767     delete WebInspector._elementDraggingEventTarget;
1768     delete WebInspector._elementDraggingEventListener;
1769     delete WebInspector._elementEndDraggingEventListener;
1770
1771     event.preventDefault();
1772 };
1773
1774 WebInspector.createMessageTextView = function(message, isError)
1775 {
1776     var messageElement = document.createElement("div");
1777     messageElement.className = "message-text-view";
1778     if (isError)
1779         messageElement.classList.add("error");
1780
1781     messageElement.textContent = message;
1782
1783     return messageElement;
1784 };
1785
1786 WebInspector.createGoToArrowButton = function()
1787 {
1788     if (!WebInspector._generatedGoToArrowButtonImages) {
1789         WebInspector._generatedGoToArrowButtonImages = true;
1790
1791         var specifications = {};
1792         specifications["go-to-arrow-normal"] = {fillColor: [0, 0, 0, 0.5]};
1793         specifications["go-to-arrow-normal-active"] = {fillColor: [0, 0, 0, 0.7]};
1794         specifications["go-to-arrow-selected"] = {fillColor: [255, 255, 255, 0.8]};
1795         specifications["go-to-arrow-selected-active"] = {fillColor: [255, 255, 255, 1]};
1796
1797         generateColoredImagesForCSS("Images/GoToArrow.svg", specifications, 10, 10);
1798     }
1799
1800     function stopPropagation(event)
1801     {
1802         event.stopPropagation()
1803     }
1804
1805     var button = document.createElement("button");
1806     button.addEventListener("mousedown", stopPropagation, true);
1807     button.className = "go-to-arrow";
1808     button.tabIndex = -1;
1809     return button;
1810 };
1811
1812 WebInspector.createSourceCodeLocationLink = function(sourceCodeLocation, dontFloat, useGoToArrowButton)
1813 {
1814     console.assert(sourceCodeLocation);
1815     if (!sourceCodeLocation)
1816         return null;
1817
1818     var linkElement = document.createElement("a");
1819     linkElement.className = "go-to-link";
1820     WebInspector.linkifyElement(linkElement, sourceCodeLocation);
1821     sourceCodeLocation.populateLiveDisplayLocationTooltip(linkElement);
1822
1823     if (useGoToArrowButton)
1824         linkElement.appendChild(WebInspector.createGoToArrowButton());
1825     else
1826         sourceCodeLocation.populateLiveDisplayLocationString(linkElement, "textContent");
1827
1828     if (dontFloat)
1829         linkElement.classList.add("dont-float");
1830
1831     return linkElement;
1832 };
1833
1834 WebInspector.linkifyLocation = function(url, lineNumber, columnNumber, className)
1835 {
1836     var sourceCode = WebInspector.sourceCodeForURL(url);
1837
1838     if (!sourceCode) {
1839         var anchor = document.createElement("a");
1840         anchor.href  = url;
1841         anchor.lineNumber = lineNumber;
1842         if (className)
1843             anchor.className = className;
1844         anchor.append(WebInspector.displayNameForURL(url) + ":" + lineNumber);
1845         return anchor;
1846     }
1847
1848     var sourceCodeLocation = sourceCode.createSourceCodeLocation(lineNumber, columnNumber);
1849     var linkElement = WebInspector.createSourceCodeLocationLink(sourceCodeLocation, true);
1850     if (className)
1851         linkElement.classList.add(className);
1852     return linkElement;
1853 };
1854
1855 WebInspector.linkifyElement = function(linkElement, sourceCodeLocation) {
1856     console.assert(sourceCodeLocation);
1857
1858     function showSourceCodeLocation(event)
1859     {
1860         event.stopPropagation();
1861         event.preventDefault();
1862
1863         if (event.metaKey)
1864             this.showOriginalUnformattedSourceCodeLocation(sourceCodeLocation);
1865         else
1866             this.showSourceCodeLocation(sourceCodeLocation);
1867     }
1868
1869     linkElement.addEventListener("click", showSourceCodeLocation.bind(this));
1870 };
1871
1872 WebInspector.sourceCodeForURL = function(url) {
1873     var sourceCode = WebInspector.frameResourceManager.resourceForURL(url);
1874     if (!sourceCode) {
1875         sourceCode = WebInspector.debuggerManager.scriptsForURL(url)[0];
1876         if (sourceCode)
1877             sourceCode = sourceCode.resource || sourceCode;
1878     }
1879     return sourceCode || null;
1880 };
1881
1882 WebInspector.linkifyURLAsNode = function(url, linkText, classes)
1883 {
1884     if (!linkText)
1885         linkText = url;
1886
1887     classes = (classes ? classes + " " : "");
1888
1889     var a = document.createElement("a");
1890     a.href = url;
1891     a.className = classes;
1892
1893     a.textContent = linkText;
1894     a.style.maxWidth = "100%";
1895
1896     return a;
1897 };
1898
1899 WebInspector.linkifyStringAsFragmentWithCustomLinkifier = function(string, linkifier)
1900 {
1901     var container = document.createDocumentFragment();
1902     var linkStringRegEx = /(?:[a-zA-Z][a-zA-Z0-9+.-]{2,}:\/\/|www\.)[\w$\-_+*'=\|\/\\(){}[\]%@&#~,:;.!?]{2,}[\w$\-_+*=\|\/\\({%@&#~]/;
1903     var lineColumnRegEx = /:(\d+)(:(\d+))?$/;
1904
1905     while (string) {
1906         var linkString = linkStringRegEx.exec(string);
1907         if (!linkString)
1908             break;
1909
1910         linkString = linkString[0];
1911         var linkIndex = string.indexOf(linkString);
1912         var nonLink = string.substring(0, linkIndex);
1913         container.append(nonLink);
1914
1915         var title = linkString;
1916         var realURL = (linkString.startsWith("www.") ? "http://" + linkString : linkString);
1917         var lineColumnMatch = lineColumnRegEx.exec(realURL);
1918         if (lineColumnMatch)
1919             realURL = realURL.substring(0, realURL.length - lineColumnMatch[0].length);
1920
1921         var linkNode = linkifier(title, realURL, lineColumnMatch ? lineColumnMatch[1] : undefined);
1922         container.appendChild(linkNode);
1923         string = string.substring(linkIndex + linkString.length, string.length);
1924     }
1925
1926     if (string)
1927         container.append(string);
1928
1929     return container;
1930 };
1931
1932 WebInspector.linkifyStringAsFragment = function(string)
1933 {
1934     function linkifier(title, url, lineNumber)
1935     {
1936         var urlNode = WebInspector.linkifyURLAsNode(url, title, undefined);
1937         if (lineNumber !== undefined)
1938             urlNode.lineNumber = lineNumber;
1939
1940         return urlNode;
1941     }
1942
1943     return WebInspector.linkifyStringAsFragmentWithCustomLinkifier(string, linkifier);
1944 };
1945
1946 WebInspector._undoKeyboardShortcut = function(event)
1947 {
1948     if (!this.isEditingAnyField() && !this.isEventTargetAnEditableField(event)) {
1949         this.undo();
1950         event.preventDefault();
1951     }
1952 };
1953
1954 WebInspector._redoKeyboardShortcut = function(event)
1955 {
1956     if (!this.isEditingAnyField() && !this.isEventTargetAnEditableField(event)) {
1957         this.redo();
1958         event.preventDefault();
1959     }
1960 };
1961
1962 WebInspector.undo = function()
1963 {
1964     DOMAgent.undo();
1965 };
1966
1967 WebInspector.redo = function()
1968 {
1969     DOMAgent.redo();
1970 };
1971
1972 WebInspector.highlightRangesWithStyleClass = function(element, resultRanges, styleClass, changes)
1973 {
1974     changes = changes || [];
1975     var highlightNodes = [];
1976     var lineText = element.textContent;
1977     var ownerDocument = element.ownerDocument;
1978     var textNodeSnapshot = ownerDocument.evaluate(".//text()", element, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
1979
1980     var snapshotLength = textNodeSnapshot.snapshotLength;
1981     if (snapshotLength === 0)
1982         return highlightNodes;
1983
1984     var nodeRanges = [];
1985     var rangeEndOffset = 0;
1986     for (var i = 0; i < snapshotLength; ++i) {
1987         var range = {};
1988         range.offset = rangeEndOffset;
1989         range.length = textNodeSnapshot.snapshotItem(i).textContent.length;
1990         rangeEndOffset = range.offset + range.length;
1991         nodeRanges.push(range);
1992     }
1993
1994     var startIndex = 0;
1995     for (var i = 0; i < resultRanges.length; ++i) {
1996         var startOffset = resultRanges[i].offset;
1997         var endOffset = startOffset + resultRanges[i].length;
1998
1999         while (startIndex < snapshotLength && nodeRanges[startIndex].offset + nodeRanges[startIndex].length <= startOffset)
2000             startIndex++;
2001         var endIndex = startIndex;
2002         while (endIndex < snapshotLength && nodeRanges[endIndex].offset + nodeRanges[endIndex].length < endOffset)
2003             endIndex++;
2004         if (endIndex === snapshotLength)
2005             break;
2006
2007         var highlightNode = ownerDocument.createElement("span");
2008         highlightNode.className = styleClass;
2009         highlightNode.textContent = lineText.substring(startOffset, endOffset);
2010
2011         var lastTextNode = textNodeSnapshot.snapshotItem(endIndex);
2012         var lastText = lastTextNode.textContent;
2013         lastTextNode.textContent = lastText.substring(endOffset - nodeRanges[endIndex].offset);
2014         changes.push({ node: lastTextNode, type: "changed", oldText: lastText, newText: lastTextNode.textContent });
2015
2016         if (startIndex === endIndex) {
2017             lastTextNode.parentElement.insertBefore(highlightNode, lastTextNode);
2018             changes.push({ node: highlightNode, type: "added", nextSibling: lastTextNode, parent: lastTextNode.parentElement });
2019             highlightNodes.push(highlightNode);
2020
2021             var prefixNode = ownerDocument.createTextNode(lastText.substring(0, startOffset - nodeRanges[startIndex].offset));
2022             lastTextNode.parentElement.insertBefore(prefixNode, highlightNode);
2023             changes.push({ node: prefixNode, type: "added", nextSibling: highlightNode, parent: lastTextNode.parentElement });
2024         } else {
2025             var firstTextNode = textNodeSnapshot.snapshotItem(startIndex);
2026             var firstText = firstTextNode.textContent;
2027             var anchorElement = firstTextNode.nextSibling;
2028
2029             firstTextNode.parentElement.insertBefore(highlightNode, anchorElement);
2030             changes.push({ node: highlightNode, type: "added", nextSibling: anchorElement, parent: firstTextNode.parentElement });
2031             highlightNodes.push(highlightNode);
2032
2033             firstTextNode.textContent = firstText.substring(0, startOffset - nodeRanges[startIndex].offset);
2034             changes.push({ node: firstTextNode, type: "changed", oldText: firstText, newText: firstTextNode.textContent });
2035
2036             for (var j = startIndex + 1; j < endIndex; j++) {
2037                 var textNode = textNodeSnapshot.snapshotItem(j);
2038                 var text = textNode.textContent;
2039                 textNode.textContent = "";
2040                 changes.push({ node: textNode, type: "changed", oldText: text, newText: textNode.textContent });
2041             }
2042         }
2043         startIndex = endIndex;
2044         nodeRanges[startIndex].offset = endOffset;
2045         nodeRanges[startIndex].length = lastTextNode.textContent.length;
2046
2047     }
2048     return highlightNodes;
2049 };
2050
2051 WebInspector.revertDomChanges = function(domChanges)
2052 {
2053     for (var i = domChanges.length - 1; i >= 0; --i) {
2054         var entry = domChanges[i];
2055         switch (entry.type) {
2056         case "added":
2057             entry.node.remove();
2058             break;
2059         case "changed":
2060             entry.node.textContent = entry.oldText;
2061             break;
2062         }
2063     }
2064 };
2065
2066 WebInspector.archiveMainFrame = function()
2067 {
2068     this._downloadingPage = true;
2069     this._updateDownloadToolbarButton();
2070
2071     PageAgent.archive(function(error, data) {
2072         this._downloadingPage = false;
2073         this._updateDownloadToolbarButton();
2074
2075         if (error)
2076             return;
2077
2078         var mainFrame = WebInspector.frameResourceManager.mainFrame;
2079         var archiveName = mainFrame.mainResource.urlComponents.host || mainFrame.mainResource.displayName || "Archive";
2080         var url = "web-inspector:///" + encodeURI(archiveName) + ".webarchive";
2081
2082         InspectorFrontendHost.save(url, data, true, true);
2083     }.bind(this));
2084 };
2085
2086 WebInspector.canArchiveMainFrame = function()
2087 {
2088     // COMPATIBILITY (iOS 7): Page.archive did not exist yet.
2089     if (!PageAgent.archive || this.debuggableType !== WebInspector.DebuggableType.Web)
2090         return false;
2091
2092     if (!WebInspector.frameResourceManager.mainFrame || !WebInspector.frameResourceManager.mainFrame.mainResource)
2093         return;
2094
2095     return WebInspector.Resource.typeFromMIMEType(WebInspector.frameResourceManager.mainFrame.mainResource.mimeType) === WebInspector.Resource.Type.Document;
2096 };
2097
2098 WebInspector.addWindowKeydownListener = function(listener)
2099 {
2100     if (typeof listener.handleKeydownEvent !== "function")
2101         return;
2102
2103     this._windowKeydownListeners.push(listener);
2104
2105     this._updateWindowKeydownListener();
2106 };
2107
2108 WebInspector.removeWindowKeydownListener = function(listener)
2109 {
2110     this._windowKeydownListeners.remove(listener);
2111
2112     this._updateWindowKeydownListener();
2113 };
2114
2115 WebInspector._updateWindowKeydownListener = function()
2116 {
2117     if (this._windowKeydownListeners.length > 0)
2118         window.addEventListener("keydown", WebInspector._sharedWindowKeydownListener, true);
2119     else
2120         window.removeEventListener("keydown", WebInspector._sharedWindowKeydownListener, true);
2121 };
2122
2123 WebInspector._sharedWindowKeydownListener = function(event)
2124 {
2125     for (var i = WebInspector._windowKeydownListeners.length - 1; i >= 0; --i) {
2126         if (WebInspector._windowKeydownListeners[i].handleKeydownEvent(event)) {
2127             event.stopImmediatePropagation();
2128             event.preventDefault();
2129             break;
2130         }
2131     }
2132 };