From ed6ef78e5ffb06b695a457f47ce5c72be5492314 Mon Sep 17 00:00:00 2001 From: dgozman Date: Fri, 27 Jan 2017 19:03:39 -0800 Subject: [PATCH] [DevTools] Make all SDKModels take a single argument in constructor. - NetworkLog is not an SDKModel anymore. - Moved ApplicationCacheModel to Resources. BUG=none Review-Url: https://codereview.chromium.org/2654423003 Cr-Commit-Position: refs/heads/master@{#446893} --- BUILD.gn | 2 +- front_end/accessibility/AccessibilityModel.js | 11 ++--- front_end/animation/AnimationModel.js | 13 ++---- front_end/layers/LayerTreeModel.js | 12 ++---- .../ApplicationCacheModel.js | 30 +++++++------- front_end/resources/ClearStorageView.js | 2 +- front_end/resources/DOMStorageModel.js | 15 +++---- front_end/resources/DatabaseModel.js | 10 ++--- front_end/resources/IndexedDBModel.js | 14 +++---- front_end/resources/ResourcesPanel.js | 14 +++---- front_end/resources/module.json | 1 + front_end/sdk/CPUProfilerModel.js | 5 ++- front_end/sdk/CSSModel.js | 9 +++-- front_end/sdk/ConsoleModel.js | 10 +++-- front_end/sdk/DOMModel.js | 4 +- front_end/sdk/DebuggerModel.js | 8 ++-- front_end/sdk/HeapProfilerModel.js | 5 ++- front_end/sdk/NetworkLog.js | 16 ++++++-- front_end/sdk/NetworkManager.js | 4 +- front_end/sdk/ResourceTreeModel.js | 12 +++--- front_end/sdk/RuntimeModel.js | 5 ++- front_end/sdk/SecurityOriginManager.js | 9 ++--- front_end/sdk/ServiceWorkerCacheModel.js | 16 +++----- front_end/sdk/ServiceWorkerManager.js | 10 +++-- front_end/sdk/SubTargetsManager.js | 4 +- front_end/sdk/Target.js | 31 +++++++++++--- front_end/sdk/TargetManager.js | 40 ++++++------------- front_end/sdk/module.json | 1 - front_end/security/SecurityModel.js | 9 ++--- 29 files changed, 169 insertions(+), 153 deletions(-) rename front_end/{sdk => resources}/ApplicationCacheModel.js (85%) diff --git a/BUILD.gn b/BUILD.gn index 2de885e257..762402fafa 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -380,6 +380,7 @@ all_devtools_files = [ "front_end/quick_open/filteredListWidget.css", "front_end/quick_open/FilteredListWidget.js", "front_end/quick_open/module.json", + "front_end/resources/ApplicationCacheModel.js", "front_end/resources/ApplicationCacheItemsView.js", "front_end/resources/appManifestView.css", "front_end/resources/AppManifestView.js", @@ -413,7 +414,6 @@ all_devtools_files = [ "front_end/screencast/ScreencastApp.js", "front_end/screencast/screencastView.css", "front_end/screencast/ScreencastView.js", - "front_end/sdk/ApplicationCacheModel.js", "front_end/sdk/Connections.js", "front_end/sdk/ConsoleModel.js", "front_end/sdk/ContentProviders.js", diff --git a/front_end/accessibility/AccessibilityModel.js b/front_end/accessibility/AccessibilityModel.js index a3bb2bfd6b..e262c257c4 100644 --- a/front_end/accessibility/AccessibilityModel.js +++ b/front_end/accessibility/AccessibilityModel.js @@ -215,7 +215,7 @@ Accessibility.AccessibilityModel = class extends SDK.SDKModel { * @param {!SDK.Target} target */ constructor(target) { - super(Accessibility.AccessibilityModel, target); + super(target); this._agent = target.accessibilityAgent(); /** @type {!Map} */ @@ -225,13 +225,10 @@ Accessibility.AccessibilityModel = class extends SDK.SDKModel { /** * @param {!SDK.Target} target - * @return {!Accessibility.AccessibilityModel} + * @return {?Accessibility.AccessibilityModel} */ static fromTarget(target) { - if (!target[Accessibility.AccessibilityModel._symbol]) - target[Accessibility.AccessibilityModel._symbol] = new Accessibility.AccessibilityModel(target); - - return target[Accessibility.AccessibilityModel._symbol]; + return target.model(Accessibility.AccessibilityModel); } clear() { @@ -314,4 +311,4 @@ Accessibility.AccessibilityModel = class extends SDK.SDKModel { } }; -Accessibility.AccessibilityModel._symbol = Symbol('AccessibilityModel'); +SDK.SDKModel.register(Accessibility.AccessibilityModel, SDK.Target.Capability.DOM); diff --git a/front_end/animation/AnimationModel.js b/front_end/animation/AnimationModel.js index 7b62f5b7a1..28b24e4c4d 100644 --- a/front_end/animation/AnimationModel.js +++ b/front_end/animation/AnimationModel.js @@ -10,7 +10,7 @@ Animation.AnimationModel = class extends SDK.SDKModel { * @param {!SDK.Target} target */ constructor(target) { - super(Animation.AnimationModel, target); + super(target); this._agent = target.animationAgent(); target.registerAnimationDispatcher(new Animation.AnimationDispatcher(this)); /** @type {!Map.} */ @@ -31,12 +31,7 @@ Animation.AnimationModel = class extends SDK.SDKModel { * @return {?Animation.AnimationModel} */ static fromTarget(target) { - if (!target.hasDOMCapability()) - return null; - if (!target[Animation.AnimationModel._symbol]) - target[Animation.AnimationModel._symbol] = new Animation.AnimationModel(target); - - return target[Animation.AnimationModel._symbol]; + return target.model(Animation.AnimationModel); } _reset() { @@ -191,14 +186,14 @@ Animation.AnimationModel = class extends SDK.SDKModel { } }; +SDK.SDKModel.register(Animation.AnimationModel, SDK.Target.Capability.DOM); + /** @enum {symbol} */ Animation.AnimationModel.Events = { AnimationGroupStarted: Symbol('AnimationGroupStarted'), ModelReset: Symbol('ModelReset') }; -Animation.AnimationModel._symbol = Symbol('AnimationModel'); - /** * @unrestricted diff --git a/front_end/layers/LayerTreeModel.js b/front_end/layers/LayerTreeModel.js index cb4cc7bd7b..ba1f9decea 100644 --- a/front_end/layers/LayerTreeModel.js +++ b/front_end/layers/LayerTreeModel.js @@ -33,7 +33,7 @@ */ Layers.LayerTreeModel = class extends SDK.SDKModel { constructor(target) { - super(Layers.LayerTreeModel, target); + super(target); target.registerLayerTreeDispatcher(new Layers.LayerTreeDispatcher(this)); SDK.targetManager.addEventListener(SDK.TargetManager.Events.MainFrameNavigated, this._onMainFrameNavigated, this); /** @type {?SDK.LayerTreeBase} */ @@ -45,13 +45,7 @@ Layers.LayerTreeModel = class extends SDK.SDKModel { * @return {?Layers.LayerTreeModel} */ static fromTarget(target) { - if (!target.hasDOMCapability()) - return null; - - var model = target.model(Layers.LayerTreeModel); - if (!model) - model = new Layers.LayerTreeModel(target); - return model; + return target.model(Layers.LayerTreeModel); } disable() { @@ -131,6 +125,8 @@ Layers.LayerTreeModel = class extends SDK.SDKModel { } }; +SDK.SDKModel.register(Layers.LayerTreeModel, SDK.Target.Capability.DOM); + /** @enum {symbol} */ Layers.LayerTreeModel.Events = { LayerTreeChanged: Symbol('LayerTreeChanged'), diff --git a/front_end/sdk/ApplicationCacheModel.js b/front_end/resources/ApplicationCacheModel.js similarity index 85% rename from front_end/sdk/ApplicationCacheModel.js rename to front_end/resources/ApplicationCacheModel.js index 17352c306d..613b0d3e6c 100644 --- a/front_end/sdk/ApplicationCacheModel.js +++ b/front_end/resources/ApplicationCacheModel.js @@ -29,18 +29,18 @@ /** * @unrestricted */ -SDK.ApplicationCacheModel = class extends SDK.SDKModel { +Resources.ApplicationCacheModel = class extends SDK.SDKModel { /** * @param {!SDK.Target} target - * @param {!SDK.ResourceTreeModel} resourceTreeModel */ - constructor(target, resourceTreeModel) { - super(SDK.ApplicationCacheModel, target); + constructor(target) { + super(target); - target.registerApplicationCacheDispatcher(new SDK.ApplicationCacheDispatcher(this)); + target.registerApplicationCacheDispatcher(new Resources.ApplicationCacheDispatcher(this)); this._agent = target.applicationCacheAgent(); this._agent.enable(); + var resourceTreeModel = SDK.ResourceTreeModel.fromTarget(target); resourceTreeModel.addEventListener(SDK.ResourceTreeModel.Events.FrameNavigated, this._frameNavigated, this); resourceTreeModel.addEventListener(SDK.ResourceTreeModel.Events.FrameDetached, this._frameDetached, this); @@ -53,10 +53,10 @@ SDK.ApplicationCacheModel = class extends SDK.SDKModel { /** * @param {!SDK.Target} target - * @return {?SDK.ApplicationCacheModel} + * @return {?Resources.ApplicationCacheModel} */ static fromTarget(target) { - return target.model(SDK.ApplicationCacheModel); + return target.model(Resources.ApplicationCacheModel); } _frameNavigated(event) { @@ -80,7 +80,7 @@ SDK.ApplicationCacheModel = class extends SDK.SDKModel { reset() { this._statuses = {}; this._manifestURLsByFrame = {}; - this.dispatchEventToListeners(SDK.ApplicationCacheModel.Events.FrameManifestsReset); + this.dispatchEventToListeners(Resources.ApplicationCacheModel.Events.FrameManifestsReset); } _mainFrameNavigated() { @@ -140,11 +140,11 @@ SDK.ApplicationCacheModel = class extends SDK.SDKModel { if (!this._manifestURLsByFrame[frameId]) { this._manifestURLsByFrame[frameId] = manifestURL; - this.dispatchEventToListeners(SDK.ApplicationCacheModel.Events.FrameManifestAdded, frameId); + this.dispatchEventToListeners(Resources.ApplicationCacheModel.Events.FrameManifestAdded, frameId); } if (statusChanged) - this.dispatchEventToListeners(SDK.ApplicationCacheModel.Events.FrameManifestStatusUpdated, frameId); + this.dispatchEventToListeners(Resources.ApplicationCacheModel.Events.FrameManifestStatusUpdated, frameId); } /** @@ -157,7 +157,7 @@ SDK.ApplicationCacheModel = class extends SDK.SDKModel { delete this._manifestURLsByFrame[frameId]; delete this._statuses[frameId]; - this.dispatchEventToListeners(SDK.ApplicationCacheModel.Events.FrameManifestRemoved, frameId); + this.dispatchEventToListeners(Resources.ApplicationCacheModel.Events.FrameManifestRemoved, frameId); } /** @@ -219,12 +219,14 @@ SDK.ApplicationCacheModel = class extends SDK.SDKModel { */ _networkStateUpdated(isNowOnline) { this._onLine = isNowOnline; - this.dispatchEventToListeners(SDK.ApplicationCacheModel.Events.NetworkStateChanged, isNowOnline); + this.dispatchEventToListeners(Resources.ApplicationCacheModel.Events.NetworkStateChanged, isNowOnline); } }; +SDK.SDKModel.register(Resources.ApplicationCacheModel, SDK.Target.Capability.DOM); + /** @enum {symbol} */ -SDK.ApplicationCacheModel.Events = { +Resources.ApplicationCacheModel.Events = { FrameManifestStatusUpdated: Symbol('FrameManifestStatusUpdated'), FrameManifestAdded: Symbol('FrameManifestAdded'), FrameManifestRemoved: Symbol('FrameManifestRemoved'), @@ -236,7 +238,7 @@ SDK.ApplicationCacheModel.Events = { * @implements {Protocol.ApplicationCacheDispatcher} * @unrestricted */ -SDK.ApplicationCacheDispatcher = class { +Resources.ApplicationCacheDispatcher = class { constructor(applicationCacheModel) { this._applicationCacheModel = applicationCacheModel; } diff --git a/front_end/resources/ClearStorageView.js b/front_end/resources/ClearStorageView.js index c674334d27..3e25d9ebc9 100644 --- a/front_end/resources/ClearStorageView.js +++ b/front_end/resources/ClearStorageView.js @@ -143,7 +143,7 @@ Resources.ClearStorageView = class extends UI.VBox { } if (set.has(Protocol.Storage.StorageType.Appcache) || hasAll) { - var appcacheModel = SDK.ApplicationCacheModel.fromTarget(this._target); + var appcacheModel = Resources.ApplicationCacheModel.fromTarget(this._target); if (appcacheModel) appcacheModel.reset(); } diff --git a/front_end/resources/DOMStorageModel.js b/front_end/resources/DOMStorageModel.js index 04588c4ecf..d166423218 100644 --- a/front_end/resources/DOMStorageModel.js +++ b/front_end/resources/DOMStorageModel.js @@ -109,12 +109,11 @@ Resources.DOMStorage.Events = { Resources.DOMStorageModel = class extends SDK.SDKModel { /** * @param {!SDK.Target} target - * @param {!SDK.SecurityOriginManager} securityOriginManager */ - constructor(target, securityOriginManager) { - super(Resources.DOMStorageModel, target); + constructor(target) { + super(target); - this._securityOriginManager = securityOriginManager; + this._securityOriginManager = SDK.SecurityOriginManager.fromTarget(target); /** @type {!Object.} */ this._storages = {}; this._agent = target.domstorageAgent(); @@ -125,11 +124,7 @@ Resources.DOMStorageModel = class extends SDK.SDKModel { * @return {!Resources.DOMStorageModel} */ static fromTarget(target) { - var model = target.model(Resources.DOMStorageModel); - if (!model) - model = new Resources.DOMStorageModel(target, SDK.SecurityOriginManager.fromTarget(target)); - - return model; + return /** @type {!Resources.DOMStorageModel} */ (target.model(Resources.DOMStorageModel)); } enable() { @@ -289,6 +284,8 @@ Resources.DOMStorageModel = class extends SDK.SDKModel { } }; +SDK.SDKModel.register(Resources.DOMStorageModel, SDK.Target.Capability.None); + /** @enum {symbol} */ Resources.DOMStorageModel.Events = { DOMStorageAdded: Symbol('DOMStorageAdded'), diff --git a/front_end/resources/DatabaseModel.js b/front_end/resources/DatabaseModel.js index 64902939e7..255546cb32 100644 --- a/front_end/resources/DatabaseModel.js +++ b/front_end/resources/DatabaseModel.js @@ -133,7 +133,7 @@ Resources.DatabaseModel = class extends SDK.SDKModel { * @param {!SDK.Target} target */ constructor(target) { - super(Resources.DatabaseModel, target); + super(target); this._databases = []; this._agent = target.databaseAgent(); @@ -145,10 +145,7 @@ Resources.DatabaseModel = class extends SDK.SDKModel { * @return {!Resources.DatabaseModel} */ static fromTarget(target) { - if (!target[Resources.DatabaseModel._symbol]) - target[Resources.DatabaseModel._symbol] = new Resources.DatabaseModel(target); - - return target[Resources.DatabaseModel._symbol]; + return /** @type {!Resources.DatabaseModel} */ (target.model(Resources.DatabaseModel)); } enable() { @@ -186,6 +183,9 @@ Resources.DatabaseModel = class extends SDK.SDKModel { } }; +SDK.SDKModel.register(Resources.DatabaseModel, SDK.Target.Capability.None); + + /** @implements {Common.Emittable} */ Resources.DatabaseModel.DatabaseAddedEvent = class { /** diff --git a/front_end/resources/IndexedDBModel.js b/front_end/resources/IndexedDBModel.js index c8bbc66528..75850e21bb 100644 --- a/front_end/resources/IndexedDBModel.js +++ b/front_end/resources/IndexedDBModel.js @@ -34,11 +34,10 @@ Resources.IndexedDBModel = class extends SDK.SDKModel { /** * @param {!SDK.Target} target - * @param {!SDK.SecurityOriginManager} securityOriginManager */ - constructor(target, securityOriginManager) { - super(Resources.IndexedDBModel, target); - this._securityOriginManager = securityOriginManager; + constructor(target) { + super(target); + this._securityOriginManager = SDK.SecurityOriginManager.fromTarget(target); this._agent = target.indexedDBAgent(); /** @type {!Map.} */ @@ -144,10 +143,7 @@ Resources.IndexedDBModel = class extends SDK.SDKModel { * @return {!Resources.IndexedDBModel} */ static fromTarget(target) { - var model = target.model(Resources.IndexedDBModel); - if (!model) - model = new Resources.IndexedDBModel(target, SDK.SecurityOriginManager.fromTarget(target)); - return model; + return /** @type {!Resources.IndexedDBModel} */ (target.model(Resources.IndexedDBModel)); } enable() { @@ -430,6 +426,8 @@ Resources.IndexedDBModel = class extends SDK.SDKModel { } }; +SDK.SDKModel.register(Resources.IndexedDBModel, SDK.Target.Capability.None); + Resources.IndexedDBModel.KeyTypes = { NumberType: 'number', StringType: 'string', diff --git a/front_end/resources/ResourcesPanel.js b/front_end/resources/ResourcesPanel.js index 357ba474da..647480927a 100644 --- a/front_end/resources/ResourcesPanel.js +++ b/front_end/resources/ResourcesPanel.js @@ -700,24 +700,24 @@ Resources.ResourcesPanel = class extends UI.PanelWithSidebar { * @param {!SDK.ResourceTreeModel} resourceTreeModel */ _populateApplicationCacheTree(resourceTreeModel) { - this._applicationCacheModel = new SDK.ApplicationCacheModel(this._target, resourceTreeModel); + this._applicationCacheModel = Resources.ApplicationCacheModel.fromTarget(this._target); this._applicationCacheViews = {}; this._applicationCacheFrameElements = {}; this._applicationCacheManifestElements = {}; this._applicationCacheModel.addEventListener( - SDK.ApplicationCacheModel.Events.FrameManifestAdded, this._applicationCacheFrameManifestAdded, this); + Resources.ApplicationCacheModel.Events.FrameManifestAdded, this._applicationCacheFrameManifestAdded, this); this._applicationCacheModel.addEventListener( - SDK.ApplicationCacheModel.Events.FrameManifestRemoved, this._applicationCacheFrameManifestRemoved, this); + Resources.ApplicationCacheModel.Events.FrameManifestRemoved, this._applicationCacheFrameManifestRemoved, this); this._applicationCacheModel.addEventListener( - SDK.ApplicationCacheModel.Events.FrameManifestsReset, this._resetAppCache, this); + Resources.ApplicationCacheModel.Events.FrameManifestsReset, this._resetAppCache, this); this._applicationCacheModel.addEventListener( - SDK.ApplicationCacheModel.Events.FrameManifestStatusUpdated, this._applicationCacheFrameManifestStatusChanged, - this); + Resources.ApplicationCacheModel.Events.FrameManifestStatusUpdated, + this._applicationCacheFrameManifestStatusChanged, this); this._applicationCacheModel.addEventListener( - SDK.ApplicationCacheModel.Events.NetworkStateChanged, this._applicationCacheNetworkStateChanged, this); + Resources.ApplicationCacheModel.Events.NetworkStateChanged, this._applicationCacheNetworkStateChanged, this); } _applicationCacheFrameManifestAdded(event) { diff --git a/front_end/resources/module.json b/front_end/resources/module.json index 4c645d7321..8d45eeecb2 100644 --- a/front_end/resources/module.json +++ b/front_end/resources/module.json @@ -24,6 +24,7 @@ "components" ], "scripts": [ + "ApplicationCacheModel.js", "AppManifestView.js", "ApplicationCacheItemsView.js", "ClearStorageView.js", diff --git a/front_end/sdk/CPUProfilerModel.js b/front_end/sdk/CPUProfilerModel.js index 6393c514d7..500369d22a 100644 --- a/front_end/sdk/CPUProfilerModel.js +++ b/front_end/sdk/CPUProfilerModel.js @@ -34,7 +34,7 @@ SDK.CPUProfilerModel = class extends SDK.SDKModel { * @param {!SDK.Target} target */ constructor(target) { - super(SDK.CPUProfilerModel, target); + super(target); this._isRecording = false; target.registerProfilerDispatcher(this); target.profilerAgent().enable(); @@ -129,6 +129,9 @@ SDK.CPUProfilerModel = class extends SDK.SDKModel { } }; +// TODO(dgozman): should be JS. +SDK.SDKModel.register(SDK.CPUProfilerModel, SDK.Target.Capability.None); + /** @enum {symbol} */ SDK.CPUProfilerModel.Events = { ConsoleProfileStarted: Symbol('ConsoleProfileStarted'), diff --git a/front_end/sdk/CSSModel.js b/front_end/sdk/CSSModel.js index da779c02cc..cab9e63bc1 100644 --- a/front_end/sdk/CSSModel.js +++ b/front_end/sdk/CSSModel.js @@ -34,11 +34,10 @@ SDK.CSSModel = class extends SDK.SDKModel { /** * @param {!SDK.Target} target - * @param {!SDK.DOMModel} domModel */ - constructor(target, domModel) { - super(SDK.CSSModel, target); - this._domModel = domModel; + constructor(target) { + super(target); + this._domModel = /** @type {!SDK.DOMModel} */ (SDK.DOMModel.fromTarget(target)); this._agent = target.cssAgent(); this._styleLoader = new SDK.CSSModel.ComputedStyleLoader(this); SDK.targetManager.addEventListener(SDK.TargetManager.Events.MainFrameNavigated, this._mainFrameNavigated, this); @@ -1001,6 +1000,8 @@ SDK.CSSModel = class extends SDK.SDKModel { } }; +SDK.SDKModel.register(SDK.CSSModel, SDK.Target.Capability.DOM); + /** @typedef {!{range: !Protocol.CSS.SourceRange, styleSheetId: !Protocol.CSS.StyleSheetId, wasUsed: boolean}} */ SDK.CSSModel.RuleUsage; diff --git a/front_end/sdk/ConsoleModel.js b/front_end/sdk/ConsoleModel.js index 9793a4bfa6..e839a2f2be 100644 --- a/front_end/sdk/ConsoleModel.js +++ b/front_end/sdk/ConsoleModel.js @@ -34,10 +34,9 @@ SDK.ConsoleModel = class extends SDK.SDKModel { /** * @param {!SDK.Target} target - * @param {?Protocol.LogAgent} logAgent */ - constructor(target, logAgent) { - super(SDK.ConsoleModel, target); + constructor(target) { + super(target); /** @type {!Array.} */ this._messages = []; @@ -45,7 +44,8 @@ SDK.ConsoleModel = class extends SDK.SDKModel { this._messageByExceptionId = new Map(); this._warnings = 0; this._errors = 0; - this._logAgent = logAgent; + /** @type {?Protocol.LogAgent} */ + this._logAgent = target.hasLogCapability() ? target.logAgent() : null; if (this._logAgent) { target.registerLogDispatcher(new SDK.LogDispatcher(this)); this._logAgent.enable(); @@ -225,6 +225,8 @@ SDK.ConsoleModel = class extends SDK.SDKModel { } }; +SDK.SDKModel.register(SDK.ConsoleModel, SDK.Target.Capability.None); + /** @enum {symbol} */ SDK.ConsoleModel.Events = { ConsoleCleared: Symbol('ConsoleCleared'), diff --git a/front_end/sdk/DOMModel.js b/front_end/sdk/DOMModel.js index 9d304fc49f..b0394b6501 100644 --- a/front_end/sdk/DOMModel.js +++ b/front_end/sdk/DOMModel.js @@ -1048,7 +1048,7 @@ SDK.DOMModel = class extends SDK.SDKModel { * @param {!SDK.Target} target */ constructor(target) { - super(SDK.DOMModel, target); + super(target); this._agent = target.domAgent(); @@ -1908,6 +1908,8 @@ SDK.DOMModel = class extends SDK.SDKModel { } }; +SDK.SDKModel.register(SDK.DOMModel, SDK.Target.Capability.DOM); + /** @enum {symbol} */ SDK.DOMModel.Events = { AttrModified: Symbol('AttrModified'), diff --git a/front_end/sdk/DebuggerModel.js b/front_end/sdk/DebuggerModel.js index 5e6d0f0753..634bc730ac 100644 --- a/front_end/sdk/DebuggerModel.js +++ b/front_end/sdk/DebuggerModel.js @@ -36,7 +36,7 @@ SDK.DebuggerModel = class extends SDK.SDKModel { * @param {!SDK.Target} target */ constructor(target) { - super(SDK.DebuggerModel, target); + super(target); target.registerDebuggerDispatcher(new SDK.DebuggerDispatcher(this)); this._agent = target.debuggerAgent(); @@ -84,9 +84,7 @@ SDK.DebuggerModel = class extends SDK.SDKModel { * @return {?SDK.DebuggerModel} */ static fromTarget(target) { - if (!target || !target.hasJSCapability()) - return null; - return target.model(SDK.DebuggerModel); + return target ? target.model(SDK.DebuggerModel) : null; } /** @@ -837,6 +835,8 @@ SDK.DebuggerModel = class extends SDK.SDKModel { } }; +SDK.SDKModel.register(SDK.DebuggerModel, SDK.Target.Capability.JS); + /** @typedef {{location: ?SDK.DebuggerModel.Location, functionName: string}} */ SDK.DebuggerModel.FunctionDetails; diff --git a/front_end/sdk/HeapProfilerModel.js b/front_end/sdk/HeapProfilerModel.js index 7179be727d..c53f92d62e 100644 --- a/front_end/sdk/HeapProfilerModel.js +++ b/front_end/sdk/HeapProfilerModel.js @@ -6,7 +6,7 @@ SDK.HeapProfilerModel = class extends SDK.SDKModel { * @param {!SDK.Target} target */ constructor(target) { - super(SDK.HeapProfilerModel, target); + super(target); target.registerHeapProfilerDispatcher(new SDK.HeapProfilerDispatcher(this)); this._enabled = false; this._heapProfilerAgent = target.heapProfilerAgent(); @@ -70,6 +70,9 @@ SDK.HeapProfilerModel = class extends SDK.SDKModel { } }; +// TODO(dgozman): should be JS. +SDK.SDKModel.register(SDK.HeapProfilerModel, SDK.Target.Capability.None); + /** @enum {symbol} */ SDK.HeapProfilerModel.Events = { HeapStatsUpdate: Symbol('HeapStatsUpdate'), diff --git a/front_end/sdk/NetworkLog.js b/front_end/sdk/NetworkLog.js index d2632d77e2..b33346e59e 100644 --- a/front_end/sdk/NetworkLog.js +++ b/front_end/sdk/NetworkLog.js @@ -31,15 +31,15 @@ /** * @unrestricted */ -SDK.NetworkLog = class extends SDK.SDKModel { +SDK.NetworkLog = class { /** * @param {!SDK.Target} target * @param {!SDK.ResourceTreeModel} resourceTreeModel * @param {!SDK.NetworkManager} networkManager */ constructor(target, resourceTreeModel, networkManager) { - super(SDK.NetworkLog, target); - + this._target = target; + target[SDK.NetworkLog._logSymbol] = this; /** @type {!Array} */ this._requests = []; /** @type {!Object} */ @@ -52,12 +52,19 @@ SDK.NetworkLog = class extends SDK.SDKModel { networkManager.on(SDK.NetworkManager.RequestRedirectEvent, this._onRequestRedirect, this); } + /** + * @return {!SDK.Target} + */ + target() { + return this._target; + } + /** * @param {!SDK.Target} target * @return {?SDK.NetworkLog} */ static fromTarget(target) { - return target.model(SDK.NetworkLog); + return target[SDK.NetworkLog._logSymbol] || null; } /** @@ -326,3 +333,4 @@ SDK.NetworkLog._InitiatorInfo; SDK.NetworkLog._initiatorDataSymbol = Symbol('InitiatorData'); SDK.NetworkLog._pageLoadForRequestSymbol = Symbol('PageLoadForRequest'); +SDK.NetworkLog._logSymbol = Symbol('NetworkLog'); diff --git a/front_end/sdk/NetworkManager.js b/front_end/sdk/NetworkManager.js index f8f244b8b7..e890b94916 100644 --- a/front_end/sdk/NetworkManager.js +++ b/front_end/sdk/NetworkManager.js @@ -36,7 +36,7 @@ SDK.NetworkManager = class extends SDK.SDKModel { * @param {!SDK.Target} target */ constructor(target) { - super(SDK.NetworkManager, target); + super(target); this._dispatcher = new SDK.NetworkDispatcher(this); this._target = target; this._networkAgent = target.networkAgent(); @@ -122,6 +122,8 @@ SDK.NetworkManager = class extends SDK.SDKModel { } }; +SDK.SDKModel.register(SDK.NetworkManager, SDK.Target.Capability.Network); + /** @enum {symbol} */ SDK.NetworkManager.Events = { RequestStarted: Symbol('RequestStarted'), diff --git a/front_end/sdk/ResourceTreeModel.js b/front_end/sdk/ResourceTreeModel.js index 035b0987e0..327dac9e50 100644 --- a/front_end/sdk/ResourceTreeModel.js +++ b/front_end/sdk/ResourceTreeModel.js @@ -34,11 +34,11 @@ SDK.ResourceTreeModel = class extends SDK.SDKModel { /** * @param {!SDK.Target} target - * @param {?SDK.NetworkManager} networkManager - * @param {!SDK.SecurityOriginManager} securityOriginManager */ - constructor(target, networkManager, securityOriginManager) { - super(SDK.ResourceTreeModel, target); + constructor(target) { + super(target); + + var networkManager = SDK.NetworkManager.fromTarget(target); if (networkManager) { networkManager.addEventListener(SDK.NetworkManager.Events.RequestFinished, this._onRequestFinished, this); networkManager.addEventListener( @@ -47,7 +47,7 @@ SDK.ResourceTreeModel = class extends SDK.SDKModel { this._agent = target.pageAgent(); this._agent.enable(); - this._securityOriginManager = securityOriginManager; + this._securityOriginManager = SDK.SecurityOriginManager.fromTarget(target); this._fetchResourceTree(); @@ -446,6 +446,8 @@ SDK.ResourceTreeModel = class extends SDK.SDKModel { } }; +SDK.SDKModel.register(SDK.ResourceTreeModel, SDK.Target.Capability.DOM); + /** @enum {symbol} */ SDK.ResourceTreeModel.Events = { FrameAdded: Symbol('FrameAdded'), diff --git a/front_end/sdk/RuntimeModel.js b/front_end/sdk/RuntimeModel.js index 7a09eeb08c..8d9464a48d 100644 --- a/front_end/sdk/RuntimeModel.js +++ b/front_end/sdk/RuntimeModel.js @@ -36,7 +36,7 @@ SDK.RuntimeModel = class extends SDK.SDKModel { * @param {!SDK.Target} target */ constructor(target) { - super(SDK.RuntimeModel, target); + super(target); this._agent = target.runtimeAgent(); this.target().registerRuntimeDispatcher(new SDK.RuntimeDispatcher(this)); @@ -323,6 +323,9 @@ SDK.RuntimeModel = class extends SDK.SDKModel { } }; +// TODO(dgozman): should be JS. +SDK.SDKModel.register(SDK.RuntimeModel, SDK.Target.Capability.None); + /** @enum {symbol} */ SDK.RuntimeModel.Events = { ExecutionContextCreated: Symbol('ExecutionContextCreated'), diff --git a/front_end/sdk/SecurityOriginManager.js b/front_end/sdk/SecurityOriginManager.js index 0c579c057f..4b780c2f0c 100644 --- a/front_end/sdk/SecurityOriginManager.js +++ b/front_end/sdk/SecurityOriginManager.js @@ -9,7 +9,7 @@ SDK.SecurityOriginManager = class extends SDK.SDKModel { * @param {!SDK.Target} target */ constructor(target) { - super(SDK.SecurityOriginManager, target); + super(target); /** @type {!Set} */ this._securityOrigins = new Set(); @@ -21,10 +21,7 @@ SDK.SecurityOriginManager = class extends SDK.SDKModel { * @return {!SDK.SecurityOriginManager} */ static fromTarget(target) { - var securityOriginManager = target.model(SDK.SecurityOriginManager); - if (!securityOriginManager) - securityOriginManager = new SDK.SecurityOriginManager(target); - return securityOriginManager; + return /** @type {!SDK.SecurityOriginManager} */ (target.model(SDK.SecurityOriginManager)); } /** @@ -68,6 +65,8 @@ SDK.SecurityOriginManager = class extends SDK.SDKModel { } }; +SDK.SDKModel.register(SDK.SecurityOriginManager, SDK.Target.Capability.None); + /** @enum {symbol} */ SDK.SecurityOriginManager.Events = { SecurityOriginAdded: Symbol('SecurityOriginAdded'), diff --git a/front_end/sdk/ServiceWorkerCacheModel.js b/front_end/sdk/ServiceWorkerCacheModel.js index f6fe747a37..85217de777 100644 --- a/front_end/sdk/ServiceWorkerCacheModel.js +++ b/front_end/sdk/ServiceWorkerCacheModel.js @@ -8,17 +8,16 @@ SDK.ServiceWorkerCacheModel = class extends SDK.SDKModel { /** * Invariant: This model can only be constructed on a ServiceWorker target. * @param {!SDK.Target} target - * @param {!SDK.SecurityOriginManager} securityOriginManager */ - constructor(target, securityOriginManager) { - super(SDK.ServiceWorkerCacheModel, target); + constructor(target) { + super(target); /** @type {!Map} */ this._caches = new Map(); this._agent = target.cacheStorageAgent(); - this._securityOriginManager = securityOriginManager; + this._securityOriginManager = SDK.SecurityOriginManager.fromTarget(target); /** @type {boolean} */ this._enabled = false; @@ -29,12 +28,7 @@ SDK.ServiceWorkerCacheModel = class extends SDK.SDKModel { * @return {?SDK.ServiceWorkerCacheModel} */ static fromTarget(target) { - if (!target.hasBrowserCapability()) - return null; - var instance = target.model(SDK.ServiceWorkerCacheModel); - if (!instance) - instance = new SDK.ServiceWorkerCacheModel(target, SDK.SecurityOriginManager.fromTarget(target)); - return instance; + return target.model(SDK.ServiceWorkerCacheModel); } enable() { @@ -271,6 +265,8 @@ SDK.ServiceWorkerCacheModel = class extends SDK.SDKModel { } }; +SDK.SDKModel.register(SDK.ServiceWorkerCacheModel, SDK.Target.Capability.Browser); + /** @enum {symbol} */ SDK.ServiceWorkerCacheModel.Events = { CacheAdded: Symbol('CacheAdded'), diff --git a/front_end/sdk/ServiceWorkerManager.js b/front_end/sdk/ServiceWorkerManager.js index 6fe0efe494..01a7eef229 100644 --- a/front_end/sdk/ServiceWorkerManager.js +++ b/front_end/sdk/ServiceWorkerManager.js @@ -31,12 +31,11 @@ /** * @unrestricted */ -SDK.ServiceWorkerManager = class extends SDK.SDKObject { +SDK.ServiceWorkerManager = class extends SDK.SDKModel { /** * @param {!SDK.Target} target - * @param {!SDK.SubTargetsManager} subTargetsManager */ - constructor(target, subTargetsManager) { + constructor(target) { super(target); target.registerServiceWorkerDispatcher(new SDK.ServiceWorkerDispatcher(this)); this._lastAnonymousTargetId = 0; @@ -48,7 +47,8 @@ SDK.ServiceWorkerManager = class extends SDK.SDKObject { if (this._forceUpdateSetting.get()) this._forceUpdateSettingChanged(); this._forceUpdateSetting.addChangeListener(this._forceUpdateSettingChanged, this); - new SDK.ServiceWorkerContextNamer(target, this, subTargetsManager); + new SDK.ServiceWorkerContextNamer( + target, this, /** @type {!SDK.SubTargetsManager} */ (SDK.SubTargetsManager.fromTarget(target))); } enable() { @@ -244,6 +244,8 @@ SDK.ServiceWorkerManager = class extends SDK.SDKObject { } }; +SDK.SDKModel.register(SDK.ServiceWorkerManager, SDK.Target.Capability.Target | SDK.Target.Capability.Browser); + /** @enum {symbol} */ SDK.ServiceWorkerManager.Events = { RegistrationUpdated: Symbol('RegistrationUpdated'), diff --git a/front_end/sdk/SubTargetsManager.js b/front_end/sdk/SubTargetsManager.js index d5845ad8eb..b8501c5b55 100644 --- a/front_end/sdk/SubTargetsManager.js +++ b/front_end/sdk/SubTargetsManager.js @@ -9,7 +9,7 @@ SDK.SubTargetsManager = class extends SDK.SDKModel { * @param {!SDK.Target} target */ constructor(target) { - super(SDK.SubTargetsManager, target); + super(target); target.registerTargetDispatcher(new SDK.SubTargetsDispatcher(this)); this._lastAnonymousTargetId = 0; this._agent = target.targetAgent(); @@ -242,6 +242,8 @@ SDK.SubTargetsManager = class extends SDK.SDKModel { } }; +SDK.SDKModel.register(SDK.SubTargetsManager, SDK.Target.Capability.Target); + /** @enum {symbol} */ SDK.SubTargetsManager.Events = { AvailableNodeTargetsChanged: Symbol('AvailableNodeTargetsChanged'), diff --git a/front_end/sdk/Target.js b/front_end/sdk/Target.js index 3d0c17307a..5b49b92c6d 100644 --- a/front_end/sdk/Target.js +++ b/front_end/sdk/Target.js @@ -24,7 +24,7 @@ SDK.Target = class extends Protocol.TargetBase { this._parentTarget = parentTarget; this._id = SDK.Target._nextId++; - /** @type {!Map.} */ + /** @type {!Map.} */ this._modelByConstructor = new Map(); } @@ -136,11 +136,18 @@ SDK.Target = class extends Protocol.TargetBase { } /** - * @param {function(new: (!T), ...)} modelClass + * @param {function(new:T, !SDK.Target)} modelClass * @return {?T} * @template T */ model(modelClass) { + if (!this._modelByConstructor.get(modelClass)) { + var capabilities = SDK.SDKModel._capabilitiesByModelClass.get(modelClass); + if (capabilities === undefined) + throw 'Model class is not registered'; + if ((this._capabilitiesMask & capabilities) === capabilities) + this._modelByConstructor.set(modelClass, new modelClass(this)); + } return this._modelByConstructor.get(modelClass) || null; } @@ -184,6 +191,8 @@ SDK.Target.Capability = { Network: 16, Target: 32, + None: 0, + AllForTests: 63 }; @@ -214,12 +223,10 @@ SDK.SDKObject = class extends Common.Object { */ SDK.SDKModel = class extends SDK.SDKObject { /** - * @param {!Function} modelClass * @param {!SDK.Target} target */ - constructor(modelClass, target) { + constructor(target) { super(target); - target._modelByConstructor.set(modelClass, this); } /** @@ -249,3 +256,17 @@ SDK.SDKModel = class extends SDK.SDKObject { this.dispose(); } }; + + +/** + * @param {function(new:SDK.SDKModel, !SDK.Target)} modelClass + * @param {number} capabilities + */ +SDK.SDKModel.register = function(modelClass, capabilities) { + if (!SDK.SDKModel._capabilitiesByModelClass) + SDK.SDKModel._capabilitiesByModelClass = new Map(); + SDK.SDKModel._capabilitiesByModelClass.set(modelClass, capabilities); +}; + +/** @type {!Map} */ +SDK.SDKModel._capabilitiesByModelClass; diff --git a/front_end/sdk/TargetManager.js b/front_end/sdk/TargetManager.js index 31d26722eb..992546edfc 100644 --- a/front_end/sdk/TargetManager.js +++ b/front_end/sdk/TargetManager.js @@ -164,44 +164,30 @@ SDK.TargetManager = class extends Common.Object { createTarget(name, capabilitiesMask, connectionFactory, parentTarget) { var target = new SDK.Target(this, name, capabilitiesMask, connectionFactory, parentTarget); - var logAgent = target.hasLogCapability() ? target.logAgent() : null; - /** @type {!SDK.ConsoleModel} */ - target.consoleModel = new SDK.ConsoleModel(target, logAgent); - - var networkManager = null; - var resourceTreeModel = null; - if (target.hasNetworkCapability()) - networkManager = new SDK.NetworkManager(target); - if (networkManager && target.hasDOMCapability()) { - resourceTreeModel = - new SDK.ResourceTreeModel(target, networkManager, SDK.SecurityOriginManager.fromTarget(target)); + target.consoleModel = /** @type {!SDK.ConsoleModel} */ (target.model(SDK.ConsoleModel)); + + var networkManager = target.model(SDK.NetworkManager); + var resourceTreeModel = target.model(SDK.ResourceTreeModel); + if (networkManager && resourceTreeModel) new SDK.NetworkLog(target, resourceTreeModel, networkManager); - } /** @type {!SDK.RuntimeModel} */ - target.runtimeModel = new SDK.RuntimeModel(target); - - if (target.hasJSCapability()) - new SDK.DebuggerModel(target); - - if (resourceTreeModel) { - var domModel = new SDK.DOMModel(target); - // TODO(eostroukhov) CSSModel should not depend on RTM - new SDK.CSSModel(target, domModel); - } + target.runtimeModel = /** @type {!SDK.RuntimeModel} */ (target.model(SDK.RuntimeModel)); + target.model(SDK.DebuggerModel); + target.model(SDK.DOMModel); + target.model(SDK.CSSModel); /** @type {?SDK.SubTargetsManager} */ - target.subTargetsManager = target.hasTargetCapability() ? new SDK.SubTargetsManager(target) : null; + target.subTargetsManager = target.model(SDK.SubTargetsManager); /** @type {!SDK.CPUProfilerModel} */ - target.cpuProfilerModel = new SDK.CPUProfilerModel(target); + target.cpuProfilerModel = /** @type {!SDK.CPUProfilerModel} */ (target.model(SDK.CPUProfilerModel)); /** @type {!SDK.HeapProfilerModel} */ - target.heapProfilerModel = new SDK.HeapProfilerModel(target); + target.heapProfilerModel = /** @type {!SDK.HeapProfilerModel} */ (target.model(SDK.HeapProfilerModel)); target.tracingManager = new SDK.TracingManager(target); - if (target.subTargetsManager && target.hasBrowserCapability()) - target.serviceWorkerManager = new SDK.ServiceWorkerManager(target, target.subTargetsManager); + target.serviceWorkerManager = target.model(SDK.ServiceWorkerManager); this.addTarget(target); return target; diff --git a/front_end/sdk/module.json b/front_end/sdk/module.json index a7799096f5..0e4c4b4438 100644 --- a/front_end/sdk/module.json +++ b/front_end/sdk/module.json @@ -92,7 +92,6 @@ "scripts": [ "Target.js", "TargetManager.js", - "ApplicationCacheModel.js", "Connections.js", "ConsoleModel.js", "ContentProviders.js", diff --git a/front_end/security/SecurityModel.js b/front_end/security/SecurityModel.js index 26bf8821ef..157f4182ef 100644 --- a/front_end/security/SecurityModel.js +++ b/front_end/security/SecurityModel.js @@ -9,7 +9,7 @@ Security.SecurityModel = class extends SDK.SDKModel { * @param {!SDK.Target} target */ constructor(target) { - super(Security.SecurityModel, target); + super(target); this._dispatcher = new Security.SecurityDispatcher(this); this._securityAgent = target.securityAgent(); target.registerSecurityDispatcher(this._dispatcher); @@ -21,10 +21,7 @@ Security.SecurityModel = class extends SDK.SDKModel { * @return {?Security.SecurityModel} */ static fromTarget(target) { - var model = target.model(Security.SecurityModel); - if (!model) - model = new Security.SecurityModel(target); - return model; + return target.model(Security.SecurityModel); } /** @@ -61,6 +58,8 @@ Security.SecurityModel = class extends SDK.SDKModel { } }; +SDK.SDKModel.register(Security.SecurityModel, SDK.Target.Capability.None); + /** @enum {symbol} */ Security.SecurityModel.Events = { SecurityStateChanged: Symbol('SecurityStateChanged')