| OLD | NEW |
| (Empty) |
| 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.SDKObject} | |
| 34 * @param {!WebInspector.Target} target | |
| 35 */ | |
| 36 WebInspector.WorkerManager = function(target) | |
| 37 { | |
| 38 WebInspector.SDKObject.call(this, target); | |
| 39 target.registerWorkerDispatcher(new WebInspector.WorkerDispatcher(this)); | |
| 40 this._lastAnonymousTargetId = 0; | |
| 41 /** @type {!Map.<string, !WebInspector.WorkerConnection>} */ | |
| 42 this._connections = new Map(); | |
| 43 | |
| 44 /** @type {!Map.<string, !WebInspector.Target>} */ | |
| 45 this._targetsByWorkerId = new Map(); | |
| 46 | |
| 47 WebInspector.targetManager.addEventListener(WebInspector.TargetManager.Event
s.SuspendStateChanged, this._onSuspendStateChanged, this); | |
| 48 this._onSuspendStateChanged(); | |
| 49 this.enable(); | |
| 50 }; | |
| 51 | |
| 52 WebInspector.WorkerManager.prototype = { | |
| 53 enable: function() | |
| 54 { | |
| 55 if (this._enabled) | |
| 56 return; | |
| 57 this._enabled = true; | |
| 58 | |
| 59 this.target().workerAgent().enable(); | |
| 60 WebInspector.targetManager.addEventListener(WebInspector.TargetManager.E
vents.MainFrameNavigated, this._mainFrameNavigated, this); | |
| 61 }, | |
| 62 | |
| 63 disable: function() | |
| 64 { | |
| 65 if (!this._enabled) | |
| 66 return; | |
| 67 this._enabled = false; | |
| 68 this._reset(); | |
| 69 this.target().workerAgent().disable(); | |
| 70 WebInspector.targetManager.removeEventListener(WebInspector.TargetManage
r.Events.MainFrameNavigated, this._mainFrameNavigated, this); | |
| 71 }, | |
| 72 | |
| 73 dispose: function() | |
| 74 { | |
| 75 this._reset(); | |
| 76 }, | |
| 77 | |
| 78 _reset: function() | |
| 79 { | |
| 80 for (var connection of this._connections.values()) | |
| 81 connection._onDisconnect.call(null, "reset"); | |
| 82 this._connections.clear(); | |
| 83 this._targetsByWorkerId.clear(); | |
| 84 }, | |
| 85 | |
| 86 _onSuspendStateChanged: function() | |
| 87 { | |
| 88 var suspended = WebInspector.targetManager.allTargetsSuspended(); | |
| 89 this.target().workerAgent().setWaitForDebuggerOnStart(!suspended); | |
| 90 }, | |
| 91 | |
| 92 /** | |
| 93 * @param {string} workerId | |
| 94 * @param {string} url | |
| 95 * @param {boolean} waitingForDebugger | |
| 96 */ | |
| 97 _workerCreated: function(workerId, url, waitingForDebugger) | |
| 98 { | |
| 99 var capabilities = WebInspector.Target.Capability.JS | WebInspector.Targ
et.Capability.Log; | |
| 100 var parsedURL = url.asParsedURL(); | |
| 101 var workerName = parsedURL ? parsedURL.lastPathComponentWithFragment() :
"#" + (++this._lastAnonymousTargetId); | |
| 102 var target = WebInspector.targetManager.createTarget(workerName, capabil
ities, this._createConnection.bind(this, workerId), this.target()); | |
| 103 this._targetsByWorkerId.set(workerId, target); | |
| 104 | |
| 105 // Only pause new worker if debugging SW - we are going through the | |
| 106 // pause on start checkbox. | |
| 107 var mainIsServiceWorker = WebInspector.targetManager.mainTarget().hasWor
kerCapability() && !WebInspector.targetManager.mainTarget().hasBrowserCapability
(); | |
| 108 if (mainIsServiceWorker && waitingForDebugger) | |
| 109 target.debuggerAgent().pause(); | |
| 110 target.runtimeAgent().runIfWaitingForDebugger(); | |
| 111 }, | |
| 112 | |
| 113 /** | |
| 114 * @param {string} workerId | |
| 115 * @param {!InspectorBackendClass.Connection.Params} params | |
| 116 * @return {!InspectorBackendClass.Connection} | |
| 117 */ | |
| 118 _createConnection: function(workerId, params) | |
| 119 { | |
| 120 var connection = new WebInspector.WorkerConnection(this, workerId, param
s); | |
| 121 this._connections.set(workerId, connection); | |
| 122 return connection; | |
| 123 }, | |
| 124 | |
| 125 /** | |
| 126 * @param {string} workerId | |
| 127 */ | |
| 128 _workerTerminated: function(workerId) | |
| 129 { | |
| 130 var connection = this._connections.get(workerId); | |
| 131 if (connection) | |
| 132 connection._onDisconnect.call(null, "worker terminated"); | |
| 133 this._connections.delete(workerId); | |
| 134 this._targetsByWorkerId.delete(workerId); | |
| 135 }, | |
| 136 | |
| 137 /** | |
| 138 * @param {string} workerId | |
| 139 * @param {string} message | |
| 140 */ | |
| 141 _dispatchMessageFromWorker: function(workerId, message) | |
| 142 { | |
| 143 var connection = this._connections.get(workerId); | |
| 144 if (connection) | |
| 145 connection._onMessage.call(null, message); | |
| 146 }, | |
| 147 | |
| 148 /** | |
| 149 * @param {!WebInspector.Event} event | |
| 150 */ | |
| 151 _mainFrameNavigated: function(event) | |
| 152 { | |
| 153 if (event.data.target() !== this.target()) | |
| 154 return; | |
| 155 this._reset(); // TODO (dgozman): Probably, unnecessary. Consider remova
l. | |
| 156 }, | |
| 157 | |
| 158 /** | |
| 159 * @param {string} workerId | |
| 160 * @return {?WebInspector.Target} | |
| 161 */ | |
| 162 targetByWorkerId: function(workerId) | |
| 163 { | |
| 164 return this._targetsByWorkerId.get(workerId) || null; | |
| 165 }, | |
| 166 | |
| 167 __proto__: WebInspector.SDKObject.prototype | |
| 168 }; | |
| 169 | |
| 170 /** | |
| 171 * @constructor | |
| 172 * @implements {WorkerAgent.Dispatcher} | |
| 173 */ | |
| 174 WebInspector.WorkerDispatcher = function(workerManager) | |
| 175 { | |
| 176 this._workerManager = workerManager; | |
| 177 }; | |
| 178 | |
| 179 WebInspector.WorkerDispatcher.prototype = { | |
| 180 /** | |
| 181 * @override | |
| 182 * @param {string} workerId | |
| 183 * @param {string} url | |
| 184 * @param {boolean} waitingForDebugger | |
| 185 */ | |
| 186 workerCreated: function(workerId, url, waitingForDebugger) | |
| 187 { | |
| 188 this._workerManager._workerCreated(workerId, url, waitingForDebugger); | |
| 189 }, | |
| 190 | |
| 191 /** | |
| 192 * @override | |
| 193 * @param {string} workerId | |
| 194 */ | |
| 195 workerTerminated: function(workerId) | |
| 196 { | |
| 197 this._workerManager._workerTerminated(workerId); | |
| 198 }, | |
| 199 | |
| 200 /** | |
| 201 * @override | |
| 202 * @param {string} workerId | |
| 203 * @param {string} message | |
| 204 */ | |
| 205 dispatchMessageFromWorker: function(workerId, message) | |
| 206 { | |
| 207 this._workerManager._dispatchMessageFromWorker(workerId, message); | |
| 208 } | |
| 209 }; | |
| 210 | |
| 211 /** | |
| 212 * @constructor | |
| 213 * @implements {InspectorBackendClass.Connection} | |
| 214 * @param {!WebInspector.WorkerManager} workerManager | |
| 215 * @param {string} workerId | |
| 216 * @param {!InspectorBackendClass.Connection.Params} params | |
| 217 */ | |
| 218 WebInspector.WorkerConnection = function(workerManager, workerId, params) | |
| 219 { | |
| 220 this._agent = workerManager.target().workerAgent(); | |
| 221 this._workerId = workerId; | |
| 222 this._onMessage = params.onMessage; | |
| 223 this._onDisconnect = params.onDisconnect; | |
| 224 }; | |
| 225 | |
| 226 WebInspector.WorkerConnection.prototype = { | |
| 227 /** | |
| 228 * @override | |
| 229 * @param {string} message | |
| 230 */ | |
| 231 sendMessage: function(message) | |
| 232 { | |
| 233 this._agent.sendMessageToWorker(this._workerId, message); | |
| 234 }, | |
| 235 | |
| 236 /** | |
| 237 * @override | |
| 238 * @return {!Promise} | |
| 239 */ | |
| 240 disconnect: function() | |
| 241 { | |
| 242 throw new Error("Not implemented"); | |
| 243 }, | |
| 244 }; | |
| OLD | NEW |