| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * @constructor |
| 7 * @extends {WebInspector.SDKModel} |
| 8 * @param {!WebInspector.Target} target |
| 9 */ |
| 10 WebInspector.SubTargetsManager = function(target) |
| 11 { |
| 12 WebInspector.SDKModel.call(this, WebInspector.SubTargetsManager, target); |
| 13 target.registerTargetDispatcher(new WebInspector.SubTargetsDispatcher(this))
; |
| 14 this._lastAnonymousTargetId = 0; |
| 15 this._agent = target.targetAgent(); |
| 16 |
| 17 /** @type {!Map<string, !WebInspector.Target>} */ |
| 18 this._targets = new Map(); |
| 19 /** @type {!Map<string, !WebInspector.SubTargetConnection>} */ |
| 20 this._connections = new Map(); |
| 21 |
| 22 this._agent.setWaitForDebuggerOnStart(true); |
| 23 this._agent.enable(); |
| 24 } |
| 25 |
| 26 /** @enum {symbol} */ |
| 27 WebInspector.SubTargetsManager.Events = { |
| 28 SubTargetAdded: Symbol("SubTargetAdded"), |
| 29 SubTargetRemoved: Symbol("SubTargetRemoved"), |
| 30 } |
| 31 |
| 32 WebInspector.SubTargetsManager._TypeSymbol = Symbol("SubTargetType"); |
| 33 WebInspector.SubTargetsManager._IdSymbol = Symbol("SubTargetId"); |
| 34 |
| 35 WebInspector.SubTargetsManager.prototype = { |
| 36 /** |
| 37 * @override |
| 38 * @return {!Promise} |
| 39 */ |
| 40 suspendModel: function() |
| 41 { |
| 42 var fulfill; |
| 43 var promise = new Promise(f => fulfill = f); |
| 44 this._agent.setWaitForDebuggerOnStart(false, fulfill); |
| 45 return promise; |
| 46 }, |
| 47 |
| 48 /** |
| 49 * @override |
| 50 * @return {!Promise} |
| 51 */ |
| 52 resumeModel: function() |
| 53 { |
| 54 var fulfill; |
| 55 var promise = new Promise(f => fulfill = f); |
| 56 this._agent.setWaitForDebuggerOnStart(true, fulfill); |
| 57 return promise; |
| 58 }, |
| 59 |
| 60 /** |
| 61 * @override |
| 62 */ |
| 63 dispose: function() |
| 64 { |
| 65 for (var connection of this._connections.values()) |
| 66 connection._close(); |
| 67 this._connections.clear(); |
| 68 this._targets.clear(); |
| 69 }, |
| 70 |
| 71 /** |
| 72 * @param {!TargetAgent.TargetID} targetId |
| 73 */ |
| 74 activateTarget: function(targetId) |
| 75 { |
| 76 this._agent.activateTarget(targetId); |
| 77 }, |
| 78 |
| 79 /** |
| 80 * @param {!TargetAgent.TargetID} targetId |
| 81 * @param {function(?WebInspector.TargetInfo)=} callback |
| 82 */ |
| 83 getTargetInfo: function(targetId, callback) |
| 84 { |
| 85 /** |
| 86 * @param {?Protocol.Error} error |
| 87 * @param {?TargetAgent.TargetInfo} targetInfo |
| 88 */ |
| 89 function innerCallback(error, targetInfo) |
| 90 { |
| 91 if (error) { |
| 92 console.error(error); |
| 93 callback(null); |
| 94 return; |
| 95 } |
| 96 if (targetInfo) |
| 97 callback(new WebInspector.TargetInfo(targetInfo)); |
| 98 else |
| 99 callback(null) |
| 100 } |
| 101 this._agent.getTargetInfo(targetId, innerCallback); |
| 102 }, |
| 103 |
| 104 /** |
| 105 * @param {string} targetId |
| 106 * @return {?WebInspector.Target} |
| 107 */ |
| 108 targetForId: function(targetId) |
| 109 { |
| 110 return this._targets.get(targetId) || null; |
| 111 }, |
| 112 |
| 113 /** |
| 114 * @param {!WebInspector.Target} target |
| 115 * @return {?string} |
| 116 */ |
| 117 targetId: function(target) |
| 118 { |
| 119 return target[WebInspector.SubTargetsManager._IdSymbol] || null; |
| 120 }, |
| 121 |
| 122 /** |
| 123 * @param {!WebInspector.Target} target |
| 124 * @return {?string} |
| 125 */ |
| 126 targetType: function(target) |
| 127 { |
| 128 return target[WebInspector.SubTargetsManager._TypeSymbol] || null; |
| 129 }, |
| 130 |
| 131 /** |
| 132 * @param {string} type |
| 133 * @return {number} |
| 134 */ |
| 135 _capabilitiesForType: function(type) |
| 136 { |
| 137 if (type === "worker") |
| 138 return WebInspector.Target.Capability.JS | WebInspector.Target.Capab
ility.Log; |
| 139 if (type === "service_worker") |
| 140 return WebInspector.Target.Capability.Log | WebInspector.Target.Capa
bility.Network | WebInspector.Target.Capability.Worker; |
| 141 return 0; |
| 142 }, |
| 143 |
| 144 /** |
| 145 * @param {string} targetId |
| 146 * @param {string} type |
| 147 * @param {string} url |
| 148 * @param {boolean} waitingForDebugger |
| 149 */ |
| 150 _targetCreated: function(targetId, type, url, waitingForDebugger) |
| 151 { |
| 152 var connection = new WebInspector.SubTargetConnection(this._agent, targe
tId); |
| 153 this._connections.set(targetId, connection); |
| 154 |
| 155 var parsedURL = url.asParsedURL(); |
| 156 var targetName = parsedURL ? parsedURL.lastPathComponentWithFragment() :
"#" + (++this._lastAnonymousTargetId); |
| 157 var target = WebInspector.targetManager.createTarget(targetName, this._c
apabilitiesForType(type), connection, this.target()); |
| 158 target[WebInspector.SubTargetsManager._TypeSymbol] = type; |
| 159 target[WebInspector.SubTargetsManager._IdSymbol] = targetId; |
| 160 this._targets.set(targetId, target); |
| 161 |
| 162 // Only pause new worker if debugging SW - we are going through the paus
e on start checkbox. |
| 163 var mainIsServiceWorker = !this.target().parentTarget() && this.target()
.hasWorkerCapability() && !this.target().hasBrowserCapability(); |
| 164 if (mainIsServiceWorker && waitingForDebugger) |
| 165 target.debuggerAgent().pause(); |
| 166 target.runtimeAgent().runIfWaitingForDebugger(); |
| 167 |
| 168 this.dispatchEventToListeners(WebInspector.SubTargetsManager.Events.SubT
argetAdded, target); |
| 169 }, |
| 170 |
| 171 /** |
| 172 * @param {string} targetId |
| 173 */ |
| 174 _targetRemoved: function(targetId) |
| 175 { |
| 176 var connection = this._connections.get(targetId); |
| 177 if (connection) |
| 178 connection._close(); |
| 179 this._connections.delete(targetId); |
| 180 var target = this._targets.get(targetId); |
| 181 this._targets.delete(targetId); |
| 182 this.dispatchEventToListeners(WebInspector.SubTargetsManager.Events.SubT
argetRemoved, target); |
| 183 }, |
| 184 |
| 185 /** |
| 186 * @param {string} targetId |
| 187 * @param {string} message |
| 188 */ |
| 189 _receivedMessageFromTarget: function(targetId, message) |
| 190 { |
| 191 var connection = this._connections.get(targetId); |
| 192 if (connection) |
| 193 connection.dispatch(message); |
| 194 }, |
| 195 |
| 196 __proto__: WebInspector.SDKModel.prototype |
| 197 } |
| 198 |
| 199 /** |
| 200 * @constructor |
| 201 * @implements {TargetAgent.Dispatcher} |
| 202 * @param {!WebInspector.SubTargetsManager} manager |
| 203 */ |
| 204 WebInspector.SubTargetsDispatcher = function(manager) |
| 205 { |
| 206 this._manager = manager; |
| 207 } |
| 208 |
| 209 WebInspector.SubTargetsDispatcher.prototype = { |
| 210 /** |
| 211 * @override |
| 212 * @param {string} targetId |
| 213 * @param {string} type |
| 214 * @param {string} url |
| 215 * @param {boolean} waitingForDebugger |
| 216 */ |
| 217 targetCreated: function(targetId, type, url, waitingForDebugger) |
| 218 { |
| 219 this._manager._targetCreated(targetId, type, url, waitingForDebugger); |
| 220 }, |
| 221 |
| 222 /** |
| 223 * @override |
| 224 * @param {string} targetId |
| 225 */ |
| 226 targetRemoved: function(targetId) |
| 227 { |
| 228 this._manager._targetRemoved(targetId); |
| 229 }, |
| 230 |
| 231 /** |
| 232 * @override |
| 233 * @param {string} targetId |
| 234 * @param {string} message |
| 235 */ |
| 236 receivedMessageFromTarget: function(targetId, message) |
| 237 { |
| 238 this._manager._receivedMessageFromTarget(targetId, message); |
| 239 } |
| 240 } |
| 241 |
| 242 /** |
| 243 * @constructor |
| 244 * @extends {InspectorBackendClass.Connection} |
| 245 * @param {!Protocol.TargetAgent} agent |
| 246 * @param {string} targetId |
| 247 */ |
| 248 WebInspector.SubTargetConnection = function(agent, targetId) |
| 249 { |
| 250 InspectorBackendClass.Connection.call(this); |
| 251 this._agent = agent; |
| 252 this._targetId = targetId; |
| 253 } |
| 254 |
| 255 WebInspector.SubTargetConnection.prototype = { |
| 256 /** |
| 257 * @override |
| 258 * @param {!Object} messageObject |
| 259 */ |
| 260 sendMessage: function(messageObject) |
| 261 { |
| 262 this._agent.sendMessageToTarget(this._targetId, JSON.stringify(messageOb
ject)); |
| 263 }, |
| 264 |
| 265 _close: function() |
| 266 { |
| 267 this.connectionClosed("target_terminated"); |
| 268 }, |
| 269 |
| 270 __proto__: InspectorBackendClass.Connection.prototype |
| 271 } |
| 272 |
| 273 /** |
| 274 * @constructor |
| 275 * @param {!TargetAgent.TargetInfo} payload |
| 276 */ |
| 277 WebInspector.TargetInfo = function(payload) |
| 278 { |
| 279 this.id = payload.targetId; |
| 280 this.url = payload.url; |
| 281 if (payload.type !== "page" && payload.type !== "frame") { |
| 282 this.title = WebInspector.UIString("Worker: %s", this.url); |
| 283 this.canActivate = false; |
| 284 } else { |
| 285 this.title = payload.title; |
| 286 this.canActivate = true; |
| 287 } |
| 288 } |
| OLD | NEW |