| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 /** | 30 /** |
| 31 * @implements {SDK.TargetManager.Observer} | |
| 32 * @unrestricted | 31 * @unrestricted |
| 33 */ | 32 */ |
| 34 Bindings.NetworkProjectManager = class extends Common.Object { | 33 Bindings.NetworkProjectManager = class extends Common.Object { |
| 35 /** | |
| 36 * @param {!SDK.TargetManager} targetManager | |
| 37 * @param {!Workspace.Workspace} workspace | |
| 38 */ | |
| 39 constructor(targetManager, workspace) { | |
| 40 super(); | |
| 41 this._workspace = workspace; | |
| 42 targetManager.observeTargets(this); | |
| 43 } | |
| 44 | |
| 45 /** | |
| 46 * @override | |
| 47 * @param {!SDK.Target} target | |
| 48 */ | |
| 49 targetAdded(target) { | |
| 50 new Bindings.NetworkProject(target, this._workspace); | |
| 51 } | |
| 52 | |
| 53 /** | |
| 54 * @override | |
| 55 * @param {!SDK.Target} target | |
| 56 */ | |
| 57 targetRemoved(target) { | |
| 58 Bindings.NetworkProject.forTarget(target)._dispose(); | |
| 59 } | |
| 60 }; | 34 }; |
| 61 | 35 |
| 62 Bindings.NetworkProjectManager.Events = { | 36 Bindings.NetworkProjectManager.Events = { |
| 63 FrameAttributionAdded: Symbol('FrameAttributionAdded'), | 37 FrameAttributionAdded: Symbol('FrameAttributionAdded'), |
| 64 FrameAttributionRemoved: Symbol('FrameAttributionRemoved') | 38 FrameAttributionRemoved: Symbol('FrameAttributionRemoved') |
| 65 }; | 39 }; |
| 66 | 40 |
| 67 /** | 41 /** |
| 68 * @unrestricted | 42 * @unrestricted |
| 69 */ | 43 */ |
| 70 Bindings.NetworkProject = class { | 44 Bindings.NetworkProject = class { |
| 71 /** | 45 /** |
| 72 * @param {!SDK.Target} target | |
| 73 * @param {!Workspace.Workspace} workspace | |
| 74 */ | |
| 75 constructor(target, workspace) { | |
| 76 this._target = target; | |
| 77 this._workspace = workspace; | |
| 78 /** @type {!Map<string, !Bindings.ContentProviderBasedProject>} */ | |
| 79 this._workspaceProjects = new Map(); | |
| 80 target[Bindings.NetworkProject._networkProjectSymbol] = this; | |
| 81 | |
| 82 this._eventListeners = []; | |
| 83 | |
| 84 this._debuggerModel = target.model(SDK.DebuggerModel); | |
| 85 /** @type {!Set<!SDK.Script>} */ | |
| 86 this._acceptedScripts = new Set(); | |
| 87 if (this._debuggerModel) { | |
| 88 var runtimeModel = this._debuggerModel.runtimeModel(); | |
| 89 this._eventListeners.push( | |
| 90 runtimeModel.addEventListener( | |
| 91 SDK.RuntimeModel.Events.ExecutionContextDestroyed, this._execution
ContextDestroyed, this), | |
| 92 this._debuggerModel.addEventListener( | |
| 93 SDK.DebuggerModel.Events.GlobalObjectCleared, this._globalObjectCl
eared, this), | |
| 94 this._debuggerModel.addEventListener( | |
| 95 SDK.DebuggerModel.Events.ParsedScriptSource, this._parsedScriptSou
rce, this), | |
| 96 this._debuggerModel.addEventListener( | |
| 97 SDK.DebuggerModel.Events.FailedToParseScriptSource, this._parsedSc
riptSource, this)); | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 /** | |
| 102 * @param {!SDK.Target} target | |
| 103 * @param {string} frameId | |
| 104 * @param {boolean} isContentScripts | |
| 105 * @return {string} | |
| 106 */ | |
| 107 static projectId(target, frameId, isContentScripts) { | |
| 108 return target.id() + ':' + frameId + ':' + (isContentScripts ? 'contentscrip
ts' : ''); | |
| 109 } | |
| 110 | |
| 111 /** | |
| 112 * @param {!SDK.Target} target | |
| 113 * @return {!Bindings.NetworkProject} | |
| 114 */ | |
| 115 static forTarget(target) { | |
| 116 return target[Bindings.NetworkProject._networkProjectSymbol]; | |
| 117 } | |
| 118 | |
| 119 /** | |
| 120 * @param {!Workspace.UISourceCode} uiSourceCode | 46 * @param {!Workspace.UISourceCode} uiSourceCode |
| 121 * @param {string} frameId | 47 * @param {string} frameId |
| 122 */ | 48 */ |
| 123 static _resolveFrame(uiSourceCode, frameId) { | 49 static _resolveFrame(uiSourceCode, frameId) { |
| 124 var target = Bindings.NetworkProject.targetForUISourceCode(uiSourceCode); | 50 var target = Bindings.NetworkProject.targetForUISourceCode(uiSourceCode); |
| 125 var resourceTreeModel = target && target.model(SDK.ResourceTreeModel); | 51 var resourceTreeModel = target && target.model(SDK.ResourceTreeModel); |
| 126 return resourceTreeModel ? resourceTreeModel.frameForId(frameId) : null; | 52 return resourceTreeModel ? resourceTreeModel.frameForId(frameId) : null; |
| 127 } | 53 } |
| 128 | 54 |
| 129 /** | 55 /** |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 */ | 124 */ |
| 199 static framesForUISourceCode(uiSourceCode) { | 125 static framesForUISourceCode(uiSourceCode) { |
| 200 var target = Bindings.NetworkProject.targetForUISourceCode(uiSourceCode); | 126 var target = Bindings.NetworkProject.targetForUISourceCode(uiSourceCode); |
| 201 var resourceTreeModel = target && target.model(SDK.ResourceTreeModel); | 127 var resourceTreeModel = target && target.model(SDK.ResourceTreeModel); |
| 202 var attribution = uiSourceCode[Bindings.NetworkProject._frameAttributionSymb
ol]; | 128 var attribution = uiSourceCode[Bindings.NetworkProject._frameAttributionSymb
ol]; |
| 203 if (!resourceTreeModel || !attribution) | 129 if (!resourceTreeModel || !attribution) |
| 204 return []; | 130 return []; |
| 205 var frames = Array.from(attribution.keys()).map(frameId => resourceTreeModel
.frameForId(frameId)); | 131 var frames = Array.from(attribution.keys()).map(frameId => resourceTreeModel
.frameForId(frameId)); |
| 206 return frames.filter(frame => !!frame); | 132 return frames.filter(frame => !!frame); |
| 207 } | 133 } |
| 208 | |
| 209 /** | |
| 210 * @param {string} frameId | |
| 211 * @param {boolean} isContentScripts | |
| 212 * @return {!Bindings.ContentProviderBasedProject} | |
| 213 */ | |
| 214 _workspaceProject(frameId, isContentScripts) { | |
| 215 var projectId = Bindings.NetworkProject.projectId(this._target, frameId, isC
ontentScripts); | |
| 216 var projectType = isContentScripts ? Workspace.projectTypes.ContentScripts :
Workspace.projectTypes.Network; | |
| 217 | |
| 218 var project = this._workspaceProjects.get(projectId); | |
| 219 if (project) | |
| 220 return project; | |
| 221 | |
| 222 project = new Bindings.ContentProviderBasedProject( | |
| 223 this._workspace, projectId, projectType, '', false /* isServiceProject *
/); | |
| 224 project[Bindings.NetworkProject._targetSymbol] = this._target; | |
| 225 this._workspaceProjects.set(projectId, project); | |
| 226 return project; | |
| 227 } | |
| 228 | |
| 229 /** | |
| 230 * @param {string} frameId | |
| 231 * @param {string} url | |
| 232 * @param {boolean} isContentScript | |
| 233 */ | |
| 234 _removeFileForURL(url, frameId, isContentScript) { | |
| 235 var project = | |
| 236 this._workspaceProjects.get(Bindings.NetworkProject.projectId(this._targ
et, frameId, isContentScript)); | |
| 237 if (!project) | |
| 238 return; | |
| 239 project.removeFile(url); | |
| 240 } | |
| 241 | |
| 242 /** | |
| 243 * @param {!Workspace.UISourceCode} uiSourceCode | |
| 244 * @param {!Common.ContentProvider} contentProvider | |
| 245 * @param {?Workspace.UISourceCodeMetadata} metadata | |
| 246 * @param {string} mimeType | |
| 247 */ | |
| 248 _addUISourceCodeWithProvider(uiSourceCode, contentProvider, metadata, mimeType
) { | |
| 249 /** @type {!Bindings.ContentProviderBasedProject} */ (uiSourceCode.project()
) | |
| 250 .addUISourceCodeWithProvider(uiSourceCode, contentProvider, metadata, mi
meType); | |
| 251 } | |
| 252 | |
| 253 /** | |
| 254 * @param {!SDK.Script} script | |
| 255 * @return {boolean} | |
| 256 */ | |
| 257 _acceptsScript(script) { | |
| 258 if (!script.sourceURL || script.isLiveEdit() || (script.isInlineScript() &&
!script.hasSourceURL)) | |
| 259 return false; | |
| 260 // Filter out embedder injected content scripts. | |
| 261 if (script.isContentScript() && !script.hasSourceURL) { | |
| 262 var parsedURL = new Common.ParsedURL(script.sourceURL); | |
| 263 if (!parsedURL.isValid) | |
| 264 return false; | |
| 265 } | |
| 266 return true; | |
| 267 } | |
| 268 | |
| 269 /** | |
| 270 * @param {!Common.Event} event | |
| 271 */ | |
| 272 _parsedScriptSource(event) { | |
| 273 var script = /** @type {!SDK.Script} */ (event.data); | |
| 274 if (!this._acceptsScript(script)) | |
| 275 return; | |
| 276 this._acceptedScripts.add(script); | |
| 277 var originalContentProvider = script.originalContentProvider(); | |
| 278 var frameId = Bindings.frameIdForScript(script); | |
| 279 script[Bindings.NetworkProject._frameIdSymbol] = frameId; | |
| 280 var uiSourceCode = this._createFile(originalContentProvider, frameId, script
.isContentScript()); | |
| 281 var metadata = Bindings.metadataForURL(this._target, frameId, uiSourceCode.u
rl()); | |
| 282 this._addUISourceCodeWithProvider(uiSourceCode, originalContentProvider, met
adata, 'text/javascript'); | |
| 283 } | |
| 284 | |
| 285 /** | |
| 286 * @param {!Common.Event} event | |
| 287 */ | |
| 288 _executionContextDestroyed(event) { | |
| 289 var executionContext = /** @type {!SDK.ExecutionContext} */ (event.data); | |
| 290 var scripts = this._debuggerModel.scriptsForExecutionContext(executionContex
t); | |
| 291 this._removeScripts(scripts); | |
| 292 } | |
| 293 | |
| 294 /** | |
| 295 * @param {!Array<!SDK.Script>} scripts | |
| 296 */ | |
| 297 _removeScripts(scripts) { | |
| 298 for (var script of scripts) { | |
| 299 if (!this._acceptedScripts.has(script)) | |
| 300 continue; | |
| 301 this._acceptedScripts.delete(script); | |
| 302 var frameId = script[Bindings.NetworkProject._frameIdSymbol]; | |
| 303 this._removeFileForURL(script.contentURL(), frameId, script.isContentScrip
t()); | |
| 304 } | |
| 305 } | |
| 306 | |
| 307 /** | |
| 308 * @param {!Common.Event} event | |
| 309 */ | |
| 310 _globalObjectCleared(event) { | |
| 311 this._removeScripts(Array.from(this._acceptedScripts)); | |
| 312 } | |
| 313 | |
| 314 /** | |
| 315 * @param {!Common.ContentProvider} contentProvider | |
| 316 * @param {string} frameId | |
| 317 * @param {boolean} isContentScript | |
| 318 * @return {!Workspace.UISourceCode} | |
| 319 */ | |
| 320 _createFile(contentProvider, frameId, isContentScript) { | |
| 321 var url = contentProvider.contentURL(); | |
| 322 var project = this._workspaceProject(frameId, isContentScript); | |
| 323 var uiSourceCode = project.createUISourceCode(url, contentProvider.contentTy
pe()); | |
| 324 if (frameId) | |
| 325 Bindings.NetworkProject.setInitialFrameAttribution(uiSourceCode, frameId); | |
| 326 return uiSourceCode; | |
| 327 } | |
| 328 | |
| 329 _dispose() { | |
| 330 for (var project of this._workspaceProjects.values()) | |
| 331 project.removeProject(); | |
| 332 Common.EventTarget.removeEventListeners(this._eventListeners); | |
| 333 delete this._target[Bindings.NetworkProject._networkProjectSymbol]; | |
| 334 this._workspaceProjects.clear(); | |
| 335 } | |
| 336 | |
| 337 _resetForTest() { | |
| 338 for (var project of this._workspaceProjects.values()) | |
| 339 project.removeProject(); | |
| 340 this._workspaceProjects.clear(); | |
| 341 } | |
| 342 | |
| 343 /** | |
| 344 * @param {!Workspace.Workspace} workspace | |
| 345 * @param {string} url | |
| 346 * @param {!SDK.Script} script | |
| 347 * @return {?Workspace.UISourceCode} | |
| 348 */ | |
| 349 static uiSourceCodeForScriptURL(workspace, url, script) { | |
| 350 var target = script.debuggerModel.target(); | |
| 351 var executionContext = script.executionContext(); | |
| 352 var frameId = executionContext ? executionContext.frameId || '' : ''; | |
| 353 return workspace.uiSourceCode(Bindings.NetworkProject.projectId(target, fram
eId, false), url) || | |
| 354 workspace.uiSourceCode(Bindings.NetworkProject.projectId(target, frameId
, true), url); | |
| 355 } | |
| 356 }; | 134 }; |
| 357 | 135 |
| 358 Bindings.NetworkProject._networkProjectSymbol = Symbol('networkProject'); | |
| 359 Bindings.NetworkProject._targetSymbol = Symbol('target'); | 136 Bindings.NetworkProject._targetSymbol = Symbol('target'); |
| 360 Bindings.NetworkProject._frameIdSymbol = Symbol('frameid'); | |
| 361 | |
| 362 Bindings.NetworkProject._frameAttributionSymbol = Symbol('Bindings.NetworkProjec
t._frameAttributionSymbol'); | 137 Bindings.NetworkProject._frameAttributionSymbol = Symbol('Bindings.NetworkProjec
t._frameAttributionSymbol'); |
| OLD | NEW |