tizen beta release
[profile/ivi/webkit-efl.git] / debian / tmp / usr / share / ewebkit-0 / webinspector / WorkerManager.js
1 /*
2  * Copyright (C) 2011 Google 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 are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 /**
32  * @constructor
33  * @extends {WebInspector.Object}
34  */
35 WebInspector.WorkerManager = function()
36 {
37     this._workerIdToWindow = {};
38     InspectorBackend.registerWorkerDispatcher(new WebInspector.WorkerDispatcher(this));
39 }
40
41 WebInspector.WorkerManager.isWorkerFrontend = function()
42 {
43     return !!WebInspector.queryParamsObject["dedicatedWorkerId"] ||
44            !!WebInspector.queryParamsObject["isSharedWorker"];
45 }
46
47 WebInspector.WorkerManager.loaded = function()
48 {
49     var workerId = WebInspector.queryParamsObject["dedicatedWorkerId"];
50     if (workerId)
51         WebInspector.WorkerManager._initializeDedicatedWorkerFrontend(workerId);
52     else
53         WebInspector.workerManager = new WebInspector.WorkerManager();
54 }
55
56 WebInspector.WorkerManager.loadCompleted = function()
57 {
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())
64         calculateTitle();
65
66     function calculateTitle()
67     {
68         WebInspector.WorkerManager._calculateWorkerInspectorTitle();
69     }
70
71     WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.MainFrameNavigated, WebInspector.workerManager._mainFrameNavigated, WebInspector.workerManager);
72 }
73
74 WebInspector.WorkerManager._initializeDedicatedWorkerFrontend = function(workerId)
75 {
76     function receiveMessage(event)
77     {
78         var message = event.data;
79         InspectorBackend.dispatch(message);
80     }
81     window.addEventListener("message", receiveMessage, true);
82
83
84     InspectorBackend.sendMessageObjectToBackend = function(message)
85     {
86         window.opener.postMessage({workerId: workerId, command: "sendMessageToBackend", message: message}, "*");
87     }
88
89     InspectorFrontendHost.loaded = function()
90     {
91         window.opener.postMessage({workerId: workerId, command: "loaded"}, "*");
92     }
93 }
94
95 WebInspector.WorkerManager._calculateWorkerInspectorTitle = function()
96 {
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)
102     {
103         if (error || wasThrown) {
104             console.error(error);
105             return;
106         }
107         InspectorFrontendHost.inspectedURLChanged(result.value);
108     }
109 }
110
111 WebInspector.WorkerManager.Events = {
112     WorkerAdded: "worker-added",
113     WorkerRemoved: "worker-removed",
114     WorkersCleared: "workers-cleared",
115 }
116
117 WebInspector.WorkerManager.prototype = {
118     _workerCreated: function(workerId, url, inspectorConnected)
119      {
120         if (inspectorConnected)
121             this._openInspectorWindow(workerId, true);
122         this.dispatchEventToListeners(WebInspector.WorkerManager.Events.WorkerAdded, {workerId: workerId, url: url, inspectorConnected: inspectorConnected});
123      },
124
125     _workerTerminated: function(workerId)
126      {
127         this.closeWorkerInspector(workerId);
128         this.dispatchEventToListeners(WebInspector.WorkerManager.Events.WorkerRemoved, workerId);
129      },
130
131     _sendMessageToWorkerInspector: function(workerId, message)
132     {
133         var workerInspectorWindow = this._workerIdToWindow[workerId];
134         if (workerInspectorWindow)
135             workerInspectorWindow.postMessage(message, "*");
136     },
137
138     openWorkerInspector: function(workerId)
139     {
140         var existingInspector = this._workerIdToWindow[workerId];
141         if (existingInspector) {
142             existingInspector.focus();
143             return;
144         }
145
146         this._openInspectorWindow(workerId, false);
147         WorkerAgent.connectToWorker(workerId);
148     },
149
150     _openInspectorWindow: function(workerId, workerIsPaused)
151     {
152         var url = window.location.href + "&dedicatedWorkerId=" + workerId;
153         if (workerIsPaused)
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);
160
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);
164     },
165
166     closeWorkerInspector: function(workerId)
167     {
168         var workerInspectorWindow = this._workerIdToWindow[workerId];
169         if (workerInspectorWindow)
170             workerInspectorWindow.close();
171     },
172
173     _mainFrameNavigated: function(event)
174     {
175         for (var workerId in this._workerIdToWindow)
176             this.closeWorkerInspector(workerId);
177         this.dispatchEventToListeners(WebInspector.WorkerManager.Events.WorkersCleared);
178     },
179
180     _pageInspectorClosing: function()
181     {
182         this._ignoreWorkerInspectorClosing = true;
183         for (var workerId in this._workerIdToWindow) {
184             this._workerIdToWindow[workerId].close();
185             WorkerAgent.disconnectFromWorker(parseInt(workerId, 10));
186         }
187     },
188
189     _workerInspectorClosing: function(workerId, event)
190     {
191         if (this._ignoreWorkerInspectorClosing)
192             return;
193         delete this._workerIdToWindow[workerId];
194         WorkerAgent.disconnectFromWorker(workerId);
195     },
196
197     _disconnectedFromWorker: function()
198     {
199         function onHide()
200         {
201             WebInspector.debuggerModel.removeEventListener(WebInspector.DebuggerModel.Events.GlobalObjectCleared, screen.hide, screen);
202         }
203         var screen = new WebInspector.WorkerTerminatedScreen();
204         WebInspector.debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.GlobalObjectCleared, screen.hide, screen);
205         screen.show(onHide.bind(this));
206     }
207 }
208
209 WebInspector.WorkerManager.prototype.__proto__ = WebInspector.Object.prototype;
210
211 /**
212  * @constructor
213  * @implements {WorkerAgent.Dispatcher}
214  */
215 WebInspector.WorkerDispatcher = function(workerManager)
216 {
217     this._workerManager = workerManager;
218     window.addEventListener("message", this._receiveMessage.bind(this), true);
219 }
220
221 WebInspector.WorkerDispatcher.prototype = {
222     _receiveMessage: function(event)
223     {
224         var workerId = event.data["workerId"];
225         workerId = parseInt(workerId, 10);
226         var command = event.data.command;
227         var message = event.data.message;
228
229         if (command == "sendMessageToBackend")
230             WorkerAgent.sendMessageToWorker(workerId, message);
231     },
232
233     workerCreated: function(workerId, url, inspectorConnected)
234     {
235         this._workerManager._workerCreated(workerId, url, inspectorConnected);
236     },
237
238     workerTerminated: function(workerId)
239     {
240         this._workerManager._workerTerminated(workerId);
241     },
242
243     dispatchMessageFromWorker: function(workerId, message)
244     {
245         this._workerManager._sendMessageToWorkerInspector(workerId, message);
246     },
247
248     disconnectedFromWorker: function()
249     {
250         this._workerManager._disconnectedFromWorker();
251     }
252 }
253
254 /**
255  * @constructor
256  * @extends {WebInspector.HelpScreen}
257  */
258 WebInspector.WorkerTerminatedScreen = function()
259 {
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.");
264 }
265
266 WebInspector.WorkerTerminatedScreen.prototype.__proto__ = WebInspector.HelpScreen.prototype;