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.
31 WebInspector.WorkerManager = function()
33 this._workerIdToWindow = {};
34 InspectorBackend.registerWorkerDispatcher(new WebInspector.DedicatedWorkerMessageForwarder(this));
37 WebInspector.WorkerManager.isWorkerFrontend = function()
39 return !!WebInspector.queryParamsObject["dedicatedWorkerId"] ||
40 !!WebInspector.queryParamsObject["isSharedWorker"];
43 WebInspector.WorkerManager.loaded = function()
45 var workerId = WebInspector.queryParamsObject["dedicatedWorkerId"];
47 WebInspector.workerManager = new WebInspector.WorkerManager();
51 function receiveMessage(event)
53 var message = event.data;
54 InspectorBackend.dispatch(message);
56 window.addEventListener("message", receiveMessage, true);
59 InspectorBackend.sendMessageObjectToBackend = function(message)
61 window.opener.postMessage({workerId: workerId, command: "sendMessageToBackend", message: message}, "*");
64 InspectorFrontendHost.loaded = function()
66 window.opener.postMessage({workerId: workerId, command: "loaded"}, "*");
70 WebInspector.WorkerManager.Events = {
71 WorkerAdded: "worker-added",
72 WorkerRemoved: "worker-removed",
73 WorkersCleared: "workers-cleared",
74 WorkerInspectorClosed: "worker-inspector-closed"
77 WebInspector.WorkerManager.prototype = {
78 _workerCreated: function(workerId, url, inspectorConnected)
80 if (inspectorConnected)
81 this._openInspectorWindow(workerId);
82 this.dispatchEventToListeners(WebInspector.WorkerManager.Events.WorkerAdded, {workerId: workerId, url: url, inspectorConnected: inspectorConnected});
85 _workerTerminated: function(workerId)
87 this.closeWorkerInspector(workerId);
88 this.dispatchEventToListeners(WebInspector.WorkerManager.Events.WorkerRemoved, workerId);
91 _sendMessageToWorkerInspector: function(workerId, message)
93 var workerInspectorWindow = this._workerIdToWindow[workerId];
94 if (workerInspectorWindow)
95 workerInspectorWindow.postMessage(message, "*");
98 openWorkerInspector: function(workerId)
100 this._openInspectorWindow(workerId);
101 WorkerAgent.connectToWorker(workerId);
104 _openInspectorWindow: function(workerId)
106 var url = location.href + "&dedicatedWorkerId=" + workerId;
107 url = url.replace("docked=true&", "");
108 // Set location=0 just to make sure the front-end will be opened in a separate window, not in new tab.
109 var workerInspectorWindow = window.open(url, undefined, "location=0");
110 this._workerIdToWindow[workerId] = workerInspectorWindow;
111 workerInspectorWindow.addEventListener("beforeunload", this._workerInspectorClosing.bind(this, workerId), true);
113 // Listen to beforeunload in detached state and to the InspectorClosing event in case of attached inspector.
114 window.addEventListener("beforeunload", this._pageInspectorClosing.bind(this), true);
115 WebInspector.notifications.addEventListener(WebInspector.Events.InspectorClosing, this._pageInspectorClosing, this);
118 closeWorkerInspector: function(workerId)
120 var workerInspectorWindow = this._workerIdToWindow[workerId];
121 if (workerInspectorWindow)
122 workerInspectorWindow.close();
127 for (var workerId in this._workerIdToWindow)
128 this.closeWorkerInspector(workerId);
129 this.dispatchEventToListeners(WebInspector.WorkerManager.Events.WorkersCleared);
132 _pageInspectorClosing: function()
134 this._ignoreWorkerInspectorClosing = true;
135 for (var workerId in this._workerIdToWindow) {
136 this._workerIdToWindow[workerId].close();
137 WorkerAgent.disconnectFromWorker(workerId);
141 _workerInspectorClosing: function(workerId, event)
143 if (this._ignoreWorkerInspectorClosing)
145 delete this._workerIdToWindow[workerId];
146 WorkerAgent.disconnectFromWorker(workerId);
147 this.dispatchEventToListeners(WebInspector.WorkerManager.Events.WorkerInspectorClosed, workerId);
151 WebInspector.WorkerManager.prototype.__proto__ = WebInspector.Object.prototype;
153 WebInspector.DedicatedWorkerMessageForwarder = function(workerManager)
155 this._workerManager = workerManager;
156 window.addEventListener("message", this._receiveMessage.bind(this), true);
159 WebInspector.DedicatedWorkerMessageForwarder.prototype = {
160 _receiveMessage: function(event)
162 var workerId = event.data.workerId;
163 workerId = parseInt(workerId);
164 var command = event.data.command;
165 var message = event.data.message;
167 if (command == "sendMessageToBackend")
168 WorkerAgent.sendMessageToWorker(workerId, message);
171 workerCreated: function(workerId, url, inspectorConnected)
173 this._workerManager._workerCreated(workerId, url, inspectorConnected);
176 workerTerminated: function(workerId)
178 this._workerManager._workerTerminated(workerId);
181 dispatchMessageFromWorker: function(workerId, message)
183 this._workerManager._sendMessageToWorkerInspector(workerId, message);