Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(105)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/sdk/SubTargetsManager.js

Issue 2431223003: [DevTools]: Require explicit connection (Closed)
Patch Set: Code review comments were addressed Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /** 5 /**
6 * @constructor 6 * @constructor
7 * @extends {WebInspector.SDKModel} 7 * @extends {WebInspector.SDKModel}
8 * @param {!WebInspector.Target} target 8 * @param {!WebInspector.Target} target
9 */ 9 */
10 WebInspector.SubTargetsManager = function(target) 10 WebInspector.SubTargetsManager = function(target)
11 { 11 {
12 WebInspector.SDKModel.call(this, WebInspector.SubTargetsManager, target); 12 WebInspector.SDKModel.call(this, WebInspector.SubTargetsManager, target);
13 target.registerTargetDispatcher(new WebInspector.SubTargetsDispatcher(this)) ; 13 target.registerTargetDispatcher(new WebInspector.SubTargetsDispatcher(this)) ;
14 this._lastAnonymousTargetId = 0; 14 this._lastAnonymousTargetId = 0;
15 this._agent = target.targetAgent(); 15 this._agent = target.targetAgent();
16 16
17 /** @type {!Map<string, !WebInspector.Target>} */ 17 /** @type {!Map<string, !WebInspector.Target>} */
18 this._attachedTargets = new Map(); 18 this._attachedTargets = new Map();
19 /** @type {!Map<string, !WebInspector.SubTargetConnection>} */ 19 /** @type {!Map<string, !WebInspector.SubTargetConnection>} */
20 this._connections = new Map(); 20 this._connections = new Map();
21 /** @type {!Map<string, !WebInspector.PendingSubTarget>} */
22 this._pendingSubTargets = new Map();
21 23
22 this._agent.setAutoAttach(true /* autoAttach */, true /* waitForDebuggerOnSt art */); 24 this._agent.setAutoAttach(true /* autoAttach */, true /* waitForDebuggerOnSt art */);
23 if (Runtime.experiments.isEnabled("autoAttachToCrossProcessSubframes")) 25 if (Runtime.experiments.isEnabled("autoAttachToCrossProcessSubframes"))
24 this._agent.setAttachToFrames(true); 26 this._agent.setAttachToFrames(true);
25 27
26 if (Runtime.experiments.isEnabled("nodeDebugging") && !target.parentTarget() ) { 28 if (Runtime.experiments.isEnabled("nodeDebugging") && !target.parentTarget() ) {
27 var defaultLocations = [{host: "localhost", port: 9229}]; 29 var defaultLocations = [{host: "localhost", port: 9229}];
28 this._agent.setRemoteLocations(defaultLocations); 30 this._agent.setRemoteLocations(defaultLocations);
29 this._agent.setDiscoverTargets(true); 31 this._agent.setDiscoverTargets(true);
30 } 32 }
31 WebInspector.targetManager.addEventListener(WebInspector.TargetManager.Event s.MainFrameNavigated, this._mainFrameNavigated, this); 33 WebInspector.targetManager.addEventListener(WebInspector.TargetManager.Event s.MainFrameNavigated, this._mainFrameNavigated, this);
32 }; 34 };
33 35
34 /** @enum {symbol} */ 36 /** @enum {symbol} */
35 WebInspector.SubTargetsManager.Events = { 37 WebInspector.SubTargetsManager.Events = {
36 SubTargetAdded: Symbol("SubTargetAdded"), 38 SubTargetAdded: Symbol("SubTargetAdded"),
37 SubTargetRemoved: Symbol("SubTargetRemoved"), 39 SubTargetRemoved: Symbol("SubTargetRemoved"),
40 SubTargetAttached: Symbol("SubTargetAttached"),
41 SubTargetDetached: Symbol("SubTargetDetached"),
38 }; 42 };
39 43
40 WebInspector.SubTargetsManager._InfoSymbol = Symbol("SubTargetInfo"); 44 WebInspector.SubTargetsManager._InfoSymbol = Symbol("SubTargetInfo");
41 45
42 WebInspector.SubTargetsManager.prototype = { 46 WebInspector.SubTargetsManager.prototype = {
43 /** 47 /**
44 * @override 48 * @override
45 * @return {!Promise} 49 * @return {!Promise}
46 */ 50 */
47 suspendModel: function() 51 suspendModel: function()
(...skipping 14 matching lines...) Expand all
62 var promise = new Promise(f => fulfill = f); 66 var promise = new Promise(f => fulfill = f);
63 this._agent.setAutoAttach(true /* autoAttach */, true /* waitForDebugger OnStart */, fulfill); 67 this._agent.setAutoAttach(true /* autoAttach */, true /* waitForDebugger OnStart */, fulfill);
64 return promise; 68 return promise;
65 }, 69 },
66 70
67 /** 71 /**
68 * @override 72 * @override
69 */ 73 */
70 dispose: function() 74 dispose: function()
71 { 75 {
72 for (var connection of this._connections.values()) { 76 for (var attachedTargetId of this._attachedTargets.keys())
73 this._agent.detachFromTarget(connection._targetId); 77 this._detachedFromTarget(attachedTargetId);
dgozman 2016/11/01 22:07:39 I think it was important to send this to the agent
eostroukhov 2016/11/02 21:55:53 It does so in the _detachedFromTarget
74 connection._onDisconnect.call(null, "disposed"); 78 for (var pendingConnectionId of this._pendingSubTargets.keys())
75 } 79 this._targetDestroyed(pendingConnectionId);
76 this._connections.clear();
77 this._attachedTargets.clear();
78 }, 80 },
79 81
80 /** 82 /**
81 * @param {!TargetAgent.TargetID} targetId 83 * @param {!TargetAgent.TargetID} targetId
82 */ 84 */
83 activateTarget: function(targetId) 85 activateTarget: function(targetId)
84 { 86 {
85 this._agent.activateTarget(targetId); 87 this._agent.activateTarget(targetId);
86 }, 88 },
87 89
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 } 164 }
163 var target = WebInspector.targetManager.createTarget(targetName, this._c apabilitiesForType(targetInfo.type), this._createConnection.bind(this, targetInf o.id), this.target()); 165 var target = WebInspector.targetManager.createTarget(targetName, this._c apabilitiesForType(targetInfo.type), this._createConnection.bind(this, targetInf o.id), this.target());
164 target[WebInspector.SubTargetsManager._InfoSymbol] = targetInfo; 166 target[WebInspector.SubTargetsManager._InfoSymbol] = targetInfo;
165 this._attachedTargets.set(targetInfo.id, target); 167 this._attachedTargets.set(targetInfo.id, target);
166 168
167 // Only pause new worker if debugging SW - we are going through the paus e on start checkbox. 169 // Only pause new worker if debugging SW - we are going through the paus e on start checkbox.
168 var mainIsServiceWorker = !this.target().parentTarget() && this.target() .hasTargetCapability() && !this.target().hasBrowserCapability(); 170 var mainIsServiceWorker = !this.target().parentTarget() && this.target() .hasTargetCapability() && !this.target().hasBrowserCapability();
169 if (mainIsServiceWorker && waitingForDebugger) 171 if (mainIsServiceWorker && waitingForDebugger)
170 target.debuggerAgent().pause(); 172 target.debuggerAgent().pause();
171 target.runtimeAgent().runIfWaitingForDebugger(); 173 target.runtimeAgent().runIfWaitingForDebugger();
172 174 var connection = this._pendingSubTargets.get(targetInfo.id);
173 this.dispatchEventToListeners(WebInspector.SubTargetsManager.Events.SubT argetAdded, target); 175 if (!connection) {
176 this._targetCreated(targetInfo);
177 connection = this._pendingSubTargets.get(targetInfo.id);
178 }
179 connection.notifyAttached(target);
180 this.dispatchEventToListeners(WebInspector.SubTargetsManager.Events.SubT argetAttached, connection);
174 }, 181 },
175 182
176 /** 183 /**
177 * @param {string} targetId 184 * @param {string} targetId
178 * @param {!InspectorBackendClass.Connection.Params} params 185 * @param {!InspectorBackendClass.Connection.Params} params
179 * @return {!InspectorBackendClass.Connection} 186 * @return {!InspectorBackendClass.Connection}
180 */ 187 */
181 _createConnection: function(targetId, params) 188 _createConnection: function(targetId, params)
182 { 189 {
183 var connection = new WebInspector.SubTargetConnection(this._agent, targe tId, params); 190 var connection = new WebInspector.SubTargetConnection(this._agent, targe tId, params);
184 this._connections.set(targetId, connection); 191 this._connections.set(targetId, connection);
185 return connection; 192 return connection;
186 }, 193 },
187 194
188 /** 195 /**
189 * @param {string} targetId 196 * @param {string} targetId
190 */ 197 */
191 _detachedFromTarget: function(targetId) 198 _detachedFromTarget: function(targetId)
192 { 199 {
193 var target = this._attachedTargets.get(targetId); 200 var target = this._attachedTargets.get(targetId);
194 this._attachedTargets.delete(targetId); 201 this._attachedTargets.delete(targetId);
195 this.dispatchEventToListeners(WebInspector.SubTargetsManager.Events.SubT argetRemoved, target);
196 var connection = this._connections.get(targetId); 202 var connection = this._connections.get(targetId);
197 if (connection) 203 connection._onDisconnect.call(null, "target terminated");
198 connection._onDisconnect.call(null, "target terminated");
199 this._connections.delete(targetId); 204 this._connections.delete(targetId);
205 this.dispatchEventToListeners(WebInspector.SubTargetsManager.Events.SubT argetDetached, this._pendingSubTargets.get(targetId));
200 }, 206 },
201 207
202 /** 208 /**
203 * @param {string} targetId 209 * @param {string} targetId
204 * @param {string} message 210 * @param {string} message
205 */ 211 */
206 _receivedMessageFromTarget: function(targetId, message) 212 _receivedMessageFromTarget: function(targetId, message)
207 { 213 {
208 var connection = this._connections.get(targetId); 214 var connection = this._connections.get(targetId);
209 if (connection) 215 if (connection)
210 connection._onMessage.call(null, message); 216 connection._onMessage.call(null, message);
211 }, 217 },
212 218
213 /** 219 /**
214 * @param {!WebInspector.TargetInfo} targetInfo 220 * @param {!WebInspector.TargetInfo} targetInfo
215 */ 221 */
216 _targetCreated: function(targetInfo) 222 _targetCreated: function(targetInfo)
217 { 223 {
218 if (targetInfo.type !== "node") 224 var connection = this._pendingSubTargets.get(targetInfo.id);
219 return; 225 console.assert(!connection);
220 this._agent.attachToTarget(targetInfo.id); 226 connection = new WebInspector.PendingSubTarget(targetInfo.id, targetInfo .title, targetInfo.type === "node", this);
227 this._pendingSubTargets.set(targetInfo.id, connection);
228 this.dispatchEventToListeners(WebInspector.SubTargetsManager.Events.SubT argetAdded, connection);
221 }, 229 },
222 230
223 /** 231 /**
224 * @param {string} targetId 232 * @param {string} targetId
225 */ 233 */
226 _targetDestroyed: function(targetId) 234 _targetDestroyed: function(targetId)
227 { 235 {
228 // All the work is done in _detachedFromTarget. 236 var connection = this._pendingSubTargets.get(targetId);
237 if (!connection)
238 return;
239 this._pendingSubTargets.delete(targetId);
240 this.dispatchEventToListeners(WebInspector.SubTargetsManager.Events.SubT argetRemoved, connection);
229 }, 241 },
230 242
231 /** 243 /**
244 * @return {!Array<!WebInspector.PendingSubTarget>}
245 */
246 subTargets: function()
247 {
248 return this._pendingSubTargets.valuesArray();
249 },
250
251 /**
232 * @param {!WebInspector.Event} event 252 * @param {!WebInspector.Event} event
233 */ 253 */
234 _mainFrameNavigated: function(event) 254 _mainFrameNavigated: function(event)
235 { 255 {
236 if (event.data.target() !== this.target()) 256 if (event.data.target() !== this.target())
237 return; 257 return;
238 258
239 var idsToDetach = []; 259 var idsToDetach = [];
240 for (var targetId of this._attachedTargets.keys()) { 260 for (var targetId of this._attachedTargets.keys()) {
241 var target = this._attachedTargets.get(targetId); 261 var target = this._attachedTargets.get(targetId);
242 var targetInfo = this.targetInfo(target); 262 var targetInfo = this.targetInfo(target);
243 if (targetInfo.type === "worker") 263 if (targetInfo.type === "worker")
244 idsToDetach.push(targetId); 264 idsToDetach.push(targetId);
245 } 265 }
246 idsToDetach.forEach(id => this._detachedFromTarget(id)); 266 idsToDetach.forEach(id => this._detachedFromTarget(id));
247 }, 267 },
248 268
249 __proto__: WebInspector.SDKModel.prototype 269 __proto__: WebInspector.SDKModel.prototype
250 }; 270 };
251 271
252 /** 272 /**
273 * @param {!WebInspector.Target} target
274 * @return {?WebInspector.SubTargetsManager}
275 */
276 WebInspector.SubTargetsManager.fromTarget = function(target)
277 {
278 return /** @type {?WebInspector.SubTargetsManager} */ (target.model(WebInspe ctor.SubTargetsManager));
279 };
280
281
282 /**
253 * @constructor 283 * @constructor
254 * @implements {TargetAgent.Dispatcher} 284 * @implements {TargetAgent.Dispatcher}
255 * @param {!WebInspector.SubTargetsManager} manager 285 * @param {!WebInspector.SubTargetsManager} manager
256 */ 286 */
257 WebInspector.SubTargetsDispatcher = function(manager) 287 WebInspector.SubTargetsDispatcher = function(manager)
258 { 288 {
259 this._manager = manager; 289 this._manager = manager;
260 }; 290 };
261 291
262 WebInspector.SubTargetsDispatcher.prototype = { 292 WebInspector.SubTargetsDispatcher.prototype = {
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
353 this.url = payload.url; 383 this.url = payload.url;
354 this.type = payload.type; 384 this.type = payload.type;
355 this.canActivate = this.type === "page" || this.type === "iframe"; 385 this.canActivate = this.type === "page" || this.type === "iframe";
356 if (this.type === "node") 386 if (this.type === "node")
357 this.title = WebInspector.UIString("Node: %s", this.url); 387 this.title = WebInspector.UIString("Node: %s", this.url);
358 else if (this.type === "page" || this.type === "iframe") 388 else if (this.type === "page" || this.type === "iframe")
359 this.title = payload.title; 389 this.title = payload.title;
360 else 390 else
361 this.title = WebInspector.UIString("Worker: %s", this.url); 391 this.title = WebInspector.UIString("Worker: %s", this.url);
362 }; 392 };
393
394 /**
395 * @constructor
396 * @param {string} id
397 * @param {string} title
398 * @param {boolean} isRemote
399 * @param {?WebInspector.SubTargetsManager} manager
400 */
401 WebInspector.PendingSubTarget = function(id, title, isRemote, manager)
402 {
403 this._id = id;
404 this._title = title;
405 this._isRemote = isRemote;
406 this._manager = manager;
407 /** @type {?Promise} */
408 this._connectPromise = null;
409 /** @type {?Function} */
410 this._attachedCallback = null;
411 };
412
413 WebInspector.PendingSubTarget.prototype = {
414 /**
415 * @return {string}
416 */
417 id: function()
418 {
419 return this._id;
420 },
421
422 /**
423 * @return {?WebInspector.Target}
424 */
425 target: function()
426 {
427 if (!this._manager)
428 return null;
429 return this._manager.targetForId(this.id());
430 },
431
432 /**
433 * @return {string}
434 */
435 name: function()
436 {
437 return this._title;
438 },
439
440 /**
441 * @return {!Promise}
442 */
443 connect: function()
444 {
445 if (!this._manager)
446 return Promise.reject();
447 if (this._connectPromise)
448 return this._connectPromise;
449 if (this.target())
450 return Promise.resolve(this.target());
451 this._connectPromise = new Promise(resolve => {
452 this._attachedCallback = resolve;
453 this._manager._agent.attachToTarget(this.id());
454 });
455 return this._connectPromise;
456 },
457
458 disconnect: function()
459 {
460 if (!this._manager)
461 return;
462 this._manager._agent.detachFromTarget(this.id());
463 },
464
465 /**
466 * @param {!WebInspector.Target} target
467 */
468 notifyAttached: function(target)
469 {
470 if (this._attachedCallback)
471 this._attachedCallback(target);
472 this._connectPromise = null;
473 this._attachedCallback = null;
474 },
475
476 /**
477 * @return {boolean}
478 */
479 isRemote: function()
dgozman 2016/11/01 22:07:39 canConnect?
eostroukhov 2016/11/02 21:55:53 Done.
480 {
481 return this._isRemote;
482 },
483 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698