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

Side by Side Diff: Source/devtools/front_end/sdk/Target.js

Issue 400633003: DevTools: introduce multitarget model listeners (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 5 months 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 | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright 2014 The Chromium Authors. All rights reserved. 2 * Copyright 2014 The Chromium Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be 3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file. 4 * found in the LICENSE file.
5 */ 5 */
6 6
7 /** 7 /**
8 * @constructor 8 * @constructor
9 * @extends {Protocol.Agents} 9 * @extends {Protocol.Agents}
10 * @param {string} name 10 * @param {string} name
11 * @param {!InspectorBackendClass.Connection} connection 11 * @param {!InspectorBackendClass.Connection} connection
12 * @param {function(!WebInspector.Target)=} callback 12 * @param {function(!WebInspector.Target)=} callback
13 */ 13 */
14 WebInspector.Target = function(name, connection, callback) 14 WebInspector.Target = function(name, connection, callback)
15 { 15 {
16 Protocol.Agents.call(this, connection.agentsMap()); 16 Protocol.Agents.call(this, connection.agentsMap());
17 /** @type {!WeakReference.<!WebInspector.Target>} */ 17 /** @type {!WeakReference.<!WebInspector.Target>} */
18 this._weakReference = new WeakReference(this); 18 this._weakReference = new WeakReference(this);
19 this._name = name; 19 this._name = name;
20 this._connection = connection; 20 this._connection = connection;
21 connection.addEventListener(InspectorBackendClass.Connection.Events.Disconne cted, this._onDisconnect, this); 21 connection.addEventListener(InspectorBackendClass.Connection.Events.Disconne cted, this._onDisconnect, this);
22 this._id = WebInspector.Target._nextId++; 22 this._id = WebInspector.Target._nextId++;
23 23
24 /** @type {!Map.<!Function, !WebInspector.SDKModel>} */
25 this._modelByConstructor = new Map();
26
24 /** @type {!Object.<string, boolean>} */ 27 /** @type {!Object.<string, boolean>} */
25 this._capabilities = {}; 28 this._capabilities = {};
26 this.pageAgent().canScreencast(this._initializeCapability.bind(this, WebInsp ector.Target.Capabilities.canScreencast, null)); 29 this.pageAgent().canScreencast(this._initializeCapability.bind(this, WebInsp ector.Target.Capabilities.canScreencast, null));
27 this.pageAgent().hasTouchInputs(this._initializeCapability.bind(this, WebIns pector.Target.Capabilities.hasTouchInputs, null)); 30 this.pageAgent().hasTouchInputs(this._initializeCapability.bind(this, WebIns pector.Target.Capabilities.hasTouchInputs, null));
28 if (WebInspector.experimentsSettings.timelinePowerProfiler.isEnabled()) 31 if (WebInspector.experimentsSettings.timelinePowerProfiler.isEnabled())
29 this.powerAgent().canProfilePower(this._initializeCapability.bind(this, WebInspector.Target.Capabilities.canProfilePower, null)); 32 this.powerAgent().canProfilePower(this._initializeCapability.bind(this, WebInspector.Target.Capabilities.canProfilePower, null));
30 this.workerAgent().canInspectWorkers(this._initializeCapability.bind(this, W ebInspector.Target.Capabilities.canInspectWorkers, this._loadedWithCapabilities. bind(this, callback))); 33 this.workerAgent().canInspectWorkers(this._initializeCapability.bind(this, W ebInspector.Target.Capabilities.canInspectWorkers, this._loadedWithCapabilities. bind(this, callback)));
31 34
32 /** @type {!WebInspector.Lock} */ 35 /** @type {!WebInspector.Lock} */
33 this.profilingLock = new WebInspector.Lock(); 36 this.profilingLock = new WebInspector.Lock();
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 /** 100 /**
98 * @param {function(!WebInspector.Target)=} callback 101 * @param {function(!WebInspector.Target)=} callback
99 */ 102 */
100 _loadedWithCapabilities: function(callback) 103 _loadedWithCapabilities: function(callback)
101 { 104 {
102 /** @type {!WebInspector.ConsoleModel} */ 105 /** @type {!WebInspector.ConsoleModel} */
103 this.consoleModel = new WebInspector.ConsoleModel(this); 106 this.consoleModel = new WebInspector.ConsoleModel(this);
104 107
105 /** @type {!WebInspector.NetworkManager} */ 108 /** @type {!WebInspector.NetworkManager} */
106 this.networkManager = new WebInspector.NetworkManager(this); 109 this.networkManager = new WebInspector.NetworkManager(this);
107 if (!WebInspector.networkManager)
108 WebInspector.networkManager = this.networkManager;
109 110
110 /** @type {!WebInspector.ResourceTreeModel} */ 111 /** @type {!WebInspector.ResourceTreeModel} */
111 this.resourceTreeModel = new WebInspector.ResourceTreeModel(this); 112 this.resourceTreeModel = new WebInspector.ResourceTreeModel(this);
112 if (!WebInspector.resourceTreeModel) 113 if (!WebInspector.resourceTreeModel)
113 WebInspector.resourceTreeModel = this.resourceTreeModel; 114 WebInspector.resourceTreeModel = this.resourceTreeModel;
114 115
115 /** @type {!WebInspector.NetworkLog} */ 116 /** @type {!WebInspector.NetworkLog} */
116 this.networkLog = new WebInspector.NetworkLog(this); 117 this.networkLog = new WebInspector.NetworkLog(this);
117 if (!WebInspector.networkLog) 118 if (!WebInspector.networkLog)
118 WebInspector.networkLog = this.networkLog; 119 WebInspector.networkLog = this.networkLog;
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 target: function() 233 target: function()
233 { 234 {
234 return this._target; 235 return this._target;
235 }, 236 },
236 237
237 __proto__: WebInspector.Object.prototype 238 __proto__: WebInspector.Object.prototype
238 } 239 }
239 240
240 /** 241 /**
241 * @constructor 242 * @constructor
243 * @extends {WebInspector.SDKObject}
244 * @param {!Function} modelClass
245 * @param {!WebInspector.Target} target
246 */
247 WebInspector.SDKModel = function(modelClass, target)
248 {
249 WebInspector.SDKObject.call(this, target);
250 target._modelByConstructor.put(modelClass, this);
251 }
252
253 WebInspector.SDKModel.prototype = {
254
apavlov 2014/07/17 22:09:59 extra line
pfeldman 2014/07/18 11:59:47 Done.
255 __proto__: WebInspector.SDKObject.prototype
256 }
257
258 /**
259 * @constructor
242 */ 260 */
243 WebInspector.TargetManager = function() 261 WebInspector.TargetManager = function()
244 { 262 {
245 /** @type {!Array.<!WebInspector.Target>} */ 263 /** @type {!Array.<!WebInspector.Target>} */
246 this._targets = []; 264 this._targets = [];
247 /** @type {!Array.<!WebInspector.TargetManager.Observer>} */ 265 /** @type {!Array.<!WebInspector.TargetManager.Observer>} */
248 this._observers = []; 266 this._observers = [];
267 /** @type {!Object.<string, !Array.<{modelClass: !Function, thisObject: (!Ob ject|undefined), listener: function(!WebInspector.Event)}>>} */
268 this._listeners = {};
249 } 269 }
250 270
251 WebInspector.TargetManager.prototype = { 271 WebInspector.TargetManager.prototype = {
272 /**
273 * @param {!Function} modelClass
274 * @param {string} eventType
275 * @param {function(!WebInspector.Event)} listener
276 * @param {!Object=} thisObject
277 */
278 addModelListener: function(modelClass, eventType, listener, thisObject)
279 {
280 for (var i = 0; i < this._targets.length; ++i) {
281 var target = this._targets[i];
apavlov 2014/07/17 22:09:58 can be inlined
pfeldman 2014/07/18 11:59:47 Done.
282 var model = target._modelByConstructor.get(modelClass);
283 model.addEventListener(eventType, listener, thisObject);
284 }
285 if (!this._listeners[eventType])
286 this._listeners[eventType] = [];
287 this._listeners[eventType].push({ modelClass: modelClass, thisObject: th isObject, listener: listener });
288 },
289
290 /**
291 * @param {!Function} modelClass
292 * @param {string} eventType
293 * @param {function(!WebInspector.Event)} listener
294 * @param {!Object=} thisObject
295 */
296 removeModelListener: function(modelClass, eventType, listener, thisObject)
297 {
298 if (!this._listeners[eventType])
299 return;
300
301 for (var i = 0; i < this._targets.length; ++i) {
302 var target = this._targets[i];
apavlov 2014/07/17 22:09:59 ditto
pfeldman 2014/07/18 11:59:47 Done.
303 var model = target._modelByConstructor.get(modelClass);
304 model.removeEventListener(eventType, listener, thisObject);
305 }
306
307 var listeners = this._listeners[eventType];
308 for (var i = 0; i < listeners.length; ++i) {
309 if (listeners[i].modelClass === modelClass && listeners[i].listener === listener && listeners[i].thisObject === thisObject)
310 listeners.splice(i--, 1);
311 }
312 if (!listeners.length)
313 delete this._listeners[eventType];
314 },
252 315
253 /** 316 /**
254 * @param {!WebInspector.TargetManager.Observer} targetObserver 317 * @param {!WebInspector.TargetManager.Observer} targetObserver
255 */ 318 */
256 observeTargets: function(targetObserver) 319 observeTargets: function(targetObserver)
257 { 320 {
258 this.targets().forEach(targetObserver.targetAdded.bind(targetObserver)); 321 this.targets().forEach(targetObserver.targetAdded.bind(targetObserver));
259 this._observers.push(targetObserver); 322 this._observers.push(targetObserver);
260 }, 323 },
261 324
(...skipping 20 matching lines...) Expand all
282 */ 345 */
283 function callbackWrapper(newTarget) 346 function callbackWrapper(newTarget)
284 { 347 {
285 this.addTarget(newTarget); 348 this.addTarget(newTarget);
286 if (callback) 349 if (callback)
287 callback(newTarget); 350 callback(newTarget);
288 } 351 }
289 }, 352 },
290 353
291 /** 354 /**
292 * @param {!WebInspector.Target} newTarget 355 * @param {!WebInspector.Target} target
293 */ 356 */
294 addTarget: function(newTarget) 357 addTarget: function(target)
295 { 358 {
296 this._targets.push(newTarget); 359 this._targets.push(target);
297 var copy = this._observers; 360 var copy = this._observers;
apavlov 2014/07/17 22:09:59 while we are here, "copy" sounds a bit wrong - it'
pfeldman 2014/07/18 11:59:47 Done.
298 for (var i = 0; i < copy.length; ++i) 361 for (var i = 0; i < copy.length; ++i) {
299 copy[i].targetAdded(newTarget); 362 copy[i].targetAdded(target);
363 for (var eventType in this._listeners) {
apavlov 2014/07/17 22:09:58 Should this loop reside outside the outer one? It
pfeldman 2014/07/18 11:59:47 Done.
364 var listeners = this._listeners[eventType];
365 for (var i = 0; i < listeners.length; ++i) {
apavlov 2014/07/17 22:09:58 nested iteration by "i". Argh.
pfeldman 2014/07/18 11:59:47 Done.
366 var model = target._modelByConstructor.get(listeners[i].mode lClass);
367 model.addEventListener(eventType, listeners[i].listener, lis teners[i].thisObject);
368 }
369 }
370 }
300 }, 371 },
301 372
302 /** 373 /**
303 * @param {!WebInspector.Target} target 374 * @param {!WebInspector.Target} target
304 */ 375 */
305 removeTarget: function(target) 376 removeTarget: function(target)
306 { 377 {
307 this._targets.remove(target); 378 this._targets.remove(target);
308 var copy = this._observers.slice(); 379 var copy = this._observers.slice();
309 for (var i = 0; i < copy.length; ++i) 380 for (var i = 0; i < copy.length; ++i) {
310 copy[i].targetRemoved(target); 381 copy[i].targetRemoved(target);
382 for (var eventType in this._listeners) {
apavlov 2014/07/17 22:09:59 ditto
pfeldman 2014/07/18 11:59:47 Done.
383 var listeners = this._listeners[eventType];
384 for (var i = 0; i < listeners.length; ++i) {
apavlov 2014/07/17 22:09:59 ditto
pfeldman 2014/07/18 11:59:47 Done.
385 var model = target._modelByConstructor.get(listeners[i].mode lClass);
386 model.removeEventListener(eventType, listeners[i].listener, listeners[i].thisObject);
387 }
388 }
389 }
311 }, 390 },
312 391
313 /** 392 /**
314 * @return {!Array.<!WebInspector.Target>} 393 * @return {!Array.<!WebInspector.Target>}
315 */ 394 */
316 targets: function() 395 targets: function()
317 { 396 {
318 return this._targets.slice(); 397 return this._targets.slice();
319 }, 398 },
320 399
(...skipping 22 matching lines...) Expand all
343 /** 422 /**
344 * @param {!WebInspector.Target} target 423 * @param {!WebInspector.Target} target
345 */ 424 */
346 targetRemoved: function(target) { }, 425 targetRemoved: function(target) { },
347 } 426 }
348 427
349 /** 428 /**
350 * @type {!WebInspector.TargetManager} 429 * @type {!WebInspector.TargetManager}
351 */ 430 */
352 WebInspector.targetManager = new WebInspector.TargetManager(); 431 WebInspector.targetManager = new WebInspector.TargetManager();
OLDNEW
« no previous file with comments | « Source/devtools/front_end/sdk/RuntimeModel.js ('k') | Source/devtools/front_end/sdk/TimelineManager.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698