Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / bindings / NetworkProject.js
bloba49a7e0d1651c3ab353631913b7fdd3247379eb6
1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
31 /**
32 * @constructor
33 * @extends {WebInspector.ContentProviderBasedProjectDelegate}
34 * @param {!WebInspector.Target} target
35 * @param {!WebInspector.Workspace} workspace
36 * @param {string} projectId
37 * @param {string} projectURL
38 * @param {!WebInspector.projectTypes} projectType
40 WebInspector.NetworkProjectDelegate = function(target, workspace, projectId, projectURL, projectType)
42 this._url = projectURL;
43 this._target = target;
44 this._id = projectId;
45 WebInspector.ContentProviderBasedProjectDelegate.call(this, workspace, projectId, projectType);
48 WebInspector.NetworkProjectDelegate.prototype = {
49 /**
50 * @return {!WebInspector.Target}
52 target: function()
54 return this._target;
57 /**
58 * @return {string}
60 id: function()
62 return this._id;
65 /**
66 * @override
67 * @return {string}
69 displayName: function()
71 if (typeof this._displayName !== "undefined")
72 return this._displayName;
74 var targetSuffix = this._target.isPage() ? "" : " \u2014 " + this._target.name();
75 if (!this._url) {
76 this._displayName = WebInspector.UIString("(no domain)") + targetSuffix;
77 return this._displayName;
79 var parsedURL = new WebInspector.ParsedURL(this._url);
80 var prettyURL = parsedURL.isValid ? parsedURL.host + (parsedURL.port ? (":" + parsedURL.port) : "") : "";
81 this._displayName = (prettyURL || this._url) + targetSuffix;
82 return this._displayName;
85 /**
86 * @override
87 * @return {string}
89 url: function()
91 return this._url;
94 /**
95 * @param {string} parentPath
96 * @param {string} name
97 * @param {string} url
98 * @param {!WebInspector.ContentProvider} contentProvider
99 * @return {string}
101 addFile: function(parentPath, name, url, contentProvider)
103 return this.addContentProvider(parentPath, name, url, url, contentProvider);
106 __proto__: WebInspector.ContentProviderBasedProjectDelegate.prototype
110 * @constructor
111 * @param {!WebInspector.TargetManager} targetManager
112 * @param {!WebInspector.Workspace} workspace
113 * @param {!WebInspector.NetworkMapping} networkMapping
114 * @implements {WebInspector.TargetManager.Observer}
116 WebInspector.NetworkProjectManager = function(targetManager, workspace, networkMapping)
118 this._workspace = workspace;
119 this._networkMapping = networkMapping;
120 targetManager.observeTargets(this);
123 WebInspector.NetworkProjectManager.prototype = {
125 * @override
126 * @param {!WebInspector.Target} target
128 targetAdded: function(target)
130 new WebInspector.NetworkProject(target, this._workspace, this._networkMapping);
134 * @override
135 * @param {!WebInspector.Target} target
137 targetRemoved: function(target)
139 WebInspector.NetworkProject.forTarget(target)._dispose();
144 * @constructor
145 * @extends {WebInspector.SDKObject}
146 * @param {!WebInspector.Target} target
147 * @param {!WebInspector.Workspace} workspace
148 * @param {!WebInspector.NetworkMapping} networkMapping
150 WebInspector.NetworkProject = function(target, workspace, networkMapping)
152 WebInspector.SDKObject.call(this, target);
153 this._workspace = workspace;
154 this._networkMapping = networkMapping;
155 this._projectDelegates = {};
156 this._processedURLs = {};
157 target[WebInspector.NetworkProject._networkProjectSymbol] = this;
159 target.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.ResourceAdded, this._resourceAdded, this);
160 target.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.MainFrameNavigated, this._mainFrameNavigated, this);
162 var debuggerModel = WebInspector.DebuggerModel.fromTarget(target);
163 if (debuggerModel) {
164 debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.ParsedScriptSource, this._parsedScriptSource, this);
165 debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.FailedToParseScriptSource, this._parsedScriptSource, this);
167 var cssModel = WebInspector.CSSStyleModel.fromTarget(target);
168 if (cssModel) {
169 cssModel.addEventListener(WebInspector.CSSStyleModel.Events.StyleSheetAdded, this._styleSheetAdded, this);
170 cssModel.addEventListener(WebInspector.CSSStyleModel.Events.StyleSheetRemoved, this._styleSheetRemoved, this);
174 WebInspector.NetworkProject._networkProjectSymbol = Symbol("networkProject");
175 WebInspector.NetworkProject._contentTypeSymbol = Symbol("networkContentType");
178 * @param {!WebInspector.Target} target
179 * @param {string} projectURL
180 * @param {boolean} isContentScripts
181 * @return {string}
183 WebInspector.NetworkProject.projectId = function(target, projectURL, isContentScripts)
185 return target.id() + ":" + (isContentScripts ? "contentscripts:" : "") + projectURL;
189 * @param {!WebInspector.Project} project
190 * @return {?WebInspector.Target}
192 WebInspector.NetworkProject._targetForProject = function(project)
194 var targetId = parseInt(project.id(), 10);
195 return WebInspector.targetManager.targetById(targetId);
199 * @param {!WebInspector.Target} target
200 * @return {!WebInspector.NetworkProject}
202 WebInspector.NetworkProject.forTarget = function(target)
204 return target[WebInspector.NetworkProject._networkProjectSymbol];
208 * @param {!WebInspector.UISourceCode} uiSourceCode
209 * @return {?WebInspector.Target} target
211 WebInspector.NetworkProject.targetForUISourceCode = function(uiSourceCode)
213 if (uiSourceCode.project().type() !== WebInspector.projectTypes.ContentScripts && uiSourceCode.project().type() !== WebInspector.projectTypes.Network)
214 return null;
216 return WebInspector.NetworkProject._targetForProject(uiSourceCode.project());
220 * @param {!WebInspector.UISourceCode} uiSourceCode
221 * @return {(!WebInspector.ResourceType|undefined)}
223 WebInspector.NetworkProject.uiSourceCodeContentType = function(uiSourceCode)
225 return uiSourceCode[WebInspector.NetworkProject._contentTypeSymbol];
228 WebInspector.NetworkProject.prototype = {
230 * @param {string} projectURL
231 * @param {boolean} isContentScripts
232 * @return {!WebInspector.NetworkProjectDelegate}
234 _projectDelegate: function(projectURL, isContentScripts)
236 var projectId = WebInspector.NetworkProject.projectId(this.target(), projectURL, isContentScripts);
237 var projectType = isContentScripts ? WebInspector.projectTypes.ContentScripts : WebInspector.projectTypes.Network;
239 if (this._projectDelegates[projectId])
240 return this._projectDelegates[projectId];
241 var projectDelegate = new WebInspector.NetworkProjectDelegate(this.target(), this._workspace, projectId, projectURL, projectType);
242 this._projectDelegates[projectId] = projectDelegate;
243 return projectDelegate;
247 * @param {string} url
248 * @param {!WebInspector.ContentProvider} contentProvider
249 * @param {boolean=} isContentScript
250 * @return {!WebInspector.UISourceCode}
252 addFileForURL: function(url, contentProvider, isContentScript)
254 var splitURL = WebInspector.ParsedURL.splitURLIntoPathComponents(url);
255 var projectURL = splitURL[0];
256 var parentPath = splitURL.slice(1, -1).join("/");
257 var name = splitURL.peekLast() || "";
258 var projectDelegate = this._projectDelegate(projectURL, isContentScript || false);
259 var path = projectDelegate.addFile(parentPath, name, url, contentProvider);
260 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (this._workspace.uiSourceCode(projectDelegate.id(), path));
261 console.assert(uiSourceCode);
262 return uiSourceCode;
266 * @param {string} url
268 _removeFileForURL: function(url)
270 var splitURL = WebInspector.ParsedURL.splitURLIntoPathComponents(url);
271 var projectURL = splitURL[0];
272 var path = splitURL.slice(1).join("/");
273 var projectDelegate = this._projectDelegates[WebInspector.NetworkProject.projectId(this.target(), projectURL, false)];
274 projectDelegate.removeFile(path);
277 _populate: function()
280 * @param {!WebInspector.ResourceTreeFrame} frame
281 * @this {WebInspector.NetworkProject}
283 function populateFrame(frame)
285 for (var i = 0; i < frame.childFrames.length; ++i)
286 populateFrame.call(this, frame.childFrames[i]);
288 var resources = frame.resources();
289 for (var i = 0; i < resources.length; ++i)
290 this._addResource(resources[i]);
293 var mainFrame = this.target().resourceTreeModel.mainFrame;
294 if (mainFrame)
295 populateFrame.call(this, mainFrame);
299 * @param {!WebInspector.Event} event
301 _parsedScriptSource: function(event)
303 var script = /** @type {!WebInspector.Script} */ (event.data);
304 if (!script.sourceURL || (script.isInlineScript() && !script.hasSourceURL))
305 return;
306 // Filter out embedder injected content scripts.
307 if (script.isContentScript() && !script.hasSourceURL) {
308 var parsedURL = new WebInspector.ParsedURL(script.sourceURL);
309 if (!parsedURL.isValid)
310 return;
312 this._addFile(script.sourceURL, script, script.isContentScript());
316 * @param {!WebInspector.Event} event
318 _styleSheetAdded: function(event)
320 var header = /** @type {!WebInspector.CSSStyleSheetHeader} */ (event.data);
321 if (header.isInline && !header.hasSourceURL && header.origin !== "inspector")
322 return;
324 this._addFile(header.resourceURL(), header, false);
328 * @param {!WebInspector.Event} event
330 _styleSheetRemoved: function(event)
332 var header = /** @type {!WebInspector.CSSStyleSheetHeader} */ (event.data);
333 if (header.isInline && !header.hasSourceURL && header.origin !== "inspector")
334 return;
336 this._removeFile(header.resourceURL());
340 * @param {!WebInspector.Event} event
342 _resourceAdded: function(event)
344 var resource = /** @type {!WebInspector.Resource} */ (event.data);
345 this._addResource(resource);
349 * @param {!WebInspector.Resource} resource
351 _addResource: function(resource)
353 if (resource.resourceType() === WebInspector.resourceTypes.Document)
354 this._addFile(resource.url, resource);
358 * @param {!WebInspector.Event} event
360 _mainFrameNavigated: function(event)
362 this._reset();
363 this._populate();
367 * @param {string} url
368 * @param {!WebInspector.ContentProvider} contentProvider
369 * @param {boolean=} isContentScript
371 _addFile: function(url, contentProvider, isContentScript)
373 if (this._networkMapping.hasMappingForURL(url))
374 return;
376 var type = contentProvider.contentType();
377 if (type !== WebInspector.resourceTypes.Stylesheet && type !== WebInspector.resourceTypes.Document && type !== WebInspector.resourceTypes.Script)
378 return;
379 if (this._processedURLs[url])
380 return;
381 this._processedURLs[url] = true;
382 var uiSourceCode = this.addFileForURL(url, contentProvider, isContentScript);
383 uiSourceCode[WebInspector.NetworkProject._contentTypeSymbol] = type;
387 * @param {string} url
389 _removeFile: function(url)
391 if (!this._processedURLs[url])
392 return;
393 delete this._processedURLs[url];
394 this._removeFileForURL(url);
397 _dispose: function()
399 this._reset();
400 var target = this.target();
401 target.resourceTreeModel.removeEventListener(WebInspector.ResourceTreeModel.EventTypes.ResourceAdded, this._resourceAdded, this);
402 target.resourceTreeModel.removeEventListener(WebInspector.ResourceTreeModel.EventTypes.MainFrameNavigated, this._mainFrameNavigated, this);
403 var debuggerModel = WebInspector.DebuggerModel.fromTarget(target);
404 if (debuggerModel) {
405 debuggerModel.removeEventListener(WebInspector.DebuggerModel.Events.ParsedScriptSource, this._parsedScriptSource, this);
406 debuggerModel.removeEventListener(WebInspector.DebuggerModel.Events.FailedToParseScriptSource, this._parsedScriptSource, this);
408 var cssModel = WebInspector.CSSStyleModel.fromTarget(target);
409 if (cssModel) {
410 cssModel.removeEventListener(WebInspector.CSSStyleModel.Events.StyleSheetAdded, this._styleSheetAdded, this);
411 cssModel.removeEventListener(WebInspector.CSSStyleModel.Events.StyleSheetRemoved, this._styleSheetRemoved, this);
413 delete target[WebInspector.NetworkProject._networkProjectSymbol];
416 _reset: function()
418 this._processedURLs = {};
419 for (var projectId in this._projectDelegates)
420 this._projectDelegates[projectId].reset();
421 this._projectDelegates = {};
424 __proto__: WebInspector.SDKObject.prototype