2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * @extends {WebInspector.Object}
35 WebInspector.WorkerManager = function()
37 this._workerIdToWindow = {};
38 InspectorBackend.registerWorkerDispatcher(new WebInspector.WorkerDispatcher(this));
41 WebInspector.WorkerManager.isWorkerFrontend = function()
43 return !!WebInspector.queryParamsObject["dedicatedWorkerId"] ||
44 !!WebInspector.queryParamsObject["isSharedWorker"];
47 WebInspector.WorkerManager.loaded = function()
49 var workerId = WebInspector.queryParamsObject["dedicatedWorkerId"];
51 WebInspector.WorkerManager._initializeDedicatedWorkerFrontend(workerId);
53 WebInspector.workerManager = new WebInspector.WorkerManager();
56 WebInspector.WorkerManager.loadCompleted = function()
58 // Make sure script execution of dedicated worker is resumed and then paused
59 // on the first script statement in case we autoattached to it.
60 if (WebInspector.queryParamsObject["workerPaused"]) {
61 DebuggerAgent.pause();
62 RuntimeAgent.run(calculateTitle);
63 } else if (WebInspector.WorkerManager.isWorkerFrontend())
66 function calculateTitle()
68 WebInspector.WorkerManager._calculateWorkerInspectorTitle();
71 WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.MainFrameNavigated, WebInspector.workerManager._mainFrameNavigated, WebInspector.workerManager);
74 WebInspector.WorkerManager._initializeDedicatedWorkerFrontend = function(workerId)
76 function receiveMessage(event)
78 var message = event.data;
79 InspectorBackend.dispatch(message);
81 window.addEventListener("message", receiveMessage, true);
84 InspectorBackend.sendMessageObjectToBackend = function(message)
86 window.opener.postMessage({workerId: workerId, command: "sendMessageToBackend", message: message}, "*");
89 InspectorFrontendHost.loaded = function()
91 window.opener.postMessage({workerId: workerId, command: "loaded"}, "*");
95 WebInspector.WorkerManager._calculateWorkerInspectorTitle = function()
97 var expression = "location.href";
98 if (WebInspector.queryParamsObject["isSharedWorker"])
99 expression += " + (this.name ? ' (' + this.name + ')' : '')";
100 RuntimeAgent.evaluate.invoke({expression:expression, doNotPauseOnExceptions:true, returnByValue: true}, evalCallback.bind(this));
101 function evalCallback(error, result, wasThrown)
103 if (error || wasThrown) {
104 console.error(error);
107 InspectorFrontendHost.inspectedURLChanged(result.value);
111 WebInspector.WorkerManager.Events = {
112 WorkerAdded: "worker-added",
113 WorkerRemoved: "worker-removed",
114 WorkersCleared: "workers-cleared",
117 WebInspector.WorkerManager.prototype = {
118 _workerCreated: function(workerId, url, inspectorConnected)
120 if (inspectorConnected)
121 this._openInspectorWindow(workerId, true);
122 this.dispatchEventToListeners(WebInspector.WorkerManager.Events.WorkerAdded, {workerId: workerId, url: url, inspectorConnected: inspectorConnected});
125 _workerTerminated: function(workerId)
127 this.closeWorkerInspector(workerId);
128 this.dispatchEventToListeners(WebInspector.WorkerManager.Events.WorkerRemoved, workerId);
131 _sendMessageToWorkerInspector: function(workerId, message)
133 var workerInspectorWindow = this._workerIdToWindow[workerId];
134 if (workerInspectorWindow)
135 workerInspectorWindow.postMessage(message, "*");
138 openWorkerInspector: function(workerId)
140 var existingInspector = this._workerIdToWindow[workerId];
141 if (existingInspector) {
142 existingInspector.focus();
146 this._openInspectorWindow(workerId, false);
147 WorkerAgent.connectToWorker(workerId);
150 _openInspectorWindow: function(workerId, workerIsPaused)
152 var url = window.location.href + "&dedicatedWorkerId=" + workerId;
154 url += "&workerPaused=true";
155 url = url.replace("docked=true&", "");
156 // Set location=0 just to make sure the front-end will be opened in a separate window, not in new tab.
157 var workerInspectorWindow = window.open(url, undefined, "location=0");
158 this._workerIdToWindow[workerId] = workerInspectorWindow;
159 workerInspectorWindow.addEventListener("beforeunload", this._workerInspectorClosing.bind(this, workerId), true);
161 // Listen to beforeunload in detached state and to the InspectorClosing event in case of attached inspector.
162 window.addEventListener("beforeunload", this._pageInspectorClosing.bind(this), true);
163 WebInspector.notifications.addEventListener(WebInspector.Events.InspectorClosing, this._pageInspectorClosing, this);
166 closeWorkerInspector: function(workerId)
168 var workerInspectorWindow = this._workerIdToWindow[workerId];
169 if (workerInspectorWindow)
170 workerInspectorWindow.close();
173 _mainFrameNavigated: function(event)
175 for (var workerId in this._workerIdToWindow)
176 this.closeWorkerInspector(workerId);
177 this.dispatchEventToListeners(WebInspector.WorkerManager.Events.WorkersCleared);
180 _pageInspectorClosing: function()
182 this._ignoreWorkerInspectorClosing = true;
183 for (var workerId in this._workerIdToWindow) {
184 this._workerIdToWindow[workerId].close();
185 WorkerAgent.disconnectFromWorker(parseInt(workerId, 10));
189 _workerInspectorClosing: function(workerId, event)
191 if (this._ignoreWorkerInspectorClosing)
193 delete this._workerIdToWindow[workerId];
194 WorkerAgent.disconnectFromWorker(workerId);
197 _disconnectedFromWorker: function()
201 WebInspector.debuggerModel.removeEventListener(WebInspector.DebuggerModel.Events.GlobalObjectCleared, screen.hide, screen);
203 var screen = new WebInspector.WorkerTerminatedScreen();
204 WebInspector.debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.GlobalObjectCleared, screen.hide, screen);
205 screen.show(onHide.bind(this));
209 WebInspector.WorkerManager.prototype.__proto__ = WebInspector.Object.prototype;
213 * @implements {WorkerAgent.Dispatcher}
215 WebInspector.WorkerDispatcher = function(workerManager)
217 this._workerManager = workerManager;
218 window.addEventListener("message", this._receiveMessage.bind(this), true);
221 WebInspector.WorkerDispatcher.prototype = {
222 _receiveMessage: function(event)
224 var workerId = event.data["workerId"];
225 workerId = parseInt(workerId, 10);
226 var command = event.data.command;
227 var message = event.data.message;
229 if (command == "sendMessageToBackend")
230 WorkerAgent.sendMessageToWorker(workerId, message);
233 workerCreated: function(workerId, url, inspectorConnected)
235 this._workerManager._workerCreated(workerId, url, inspectorConnected);
238 workerTerminated: function(workerId)
240 this._workerManager._workerTerminated(workerId);
243 dispatchMessageFromWorker: function(workerId, message)
245 this._workerManager._sendMessageToWorkerInspector(workerId, message);
248 disconnectedFromWorker: function()
250 this._workerManager._disconnectedFromWorker();
256 * @extends {WebInspector.HelpScreen}
258 WebInspector.WorkerTerminatedScreen = function()
260 WebInspector.HelpScreen.call(this, WebInspector.UIString("Inspected worker terminated"));
261 var p = this.contentElement.createChild("p");
262 p.addStyleClass("help-section");
263 p.textContent = WebInspector.UIString("Inspected worker has terminated. Once it restarts we will attach to it automatically.");
266 WebInspector.WorkerTerminatedScreen.prototype.__proto__ = WebInspector.HelpScreen.prototype;