2 * Copyright (C) 2011 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
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
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.
33 * @extends {WebInspector.SDKModel}
34 * @param {!WebInspector.Target} target
36 WebInspector.ResourceTreeModel = function(target)
38 WebInspector.SDKModel.call(this, WebInspector.ResourceTreeModel, target);
40 target.networkManager.addEventListener(WebInspector.NetworkManager.EventTypes.RequestFinished, this._onRequestFinished, this);
41 target.networkManager.addEventListener(WebInspector.NetworkManager.EventTypes.RequestUpdateDropped, this._onRequestUpdateDropped, this);
43 target.consoleModel.addEventListener(WebInspector.ConsoleModel.Events.MessageAdded, this._consoleMessageAdded, this);
44 target.consoleModel.addEventListener(WebInspector.ConsoleModel.Events.ConsoleCleared, this._consoleCleared, this);
46 this._agent = target.pageAgent();
49 this._fetchResourceTree();
51 target.registerPageDispatcher(new WebInspector.PageDispatcher(this));
53 this._pendingConsoleMessages = {};
54 this._securityOriginFrameCount = {};
55 this._inspectedPageURL = "";
58 WebInspector.ResourceTreeModel.EventTypes = {
59 FrameAdded: "FrameAdded",
60 FrameNavigated: "FrameNavigated",
61 FrameDetached: "FrameDetached",
62 FrameResized: "FrameResized",
63 MainFrameNavigated: "MainFrameNavigated",
64 ResourceAdded: "ResourceAdded",
65 WillLoadCachedResources: "WillLoadCachedResources",
66 CachedResourcesLoaded: "CachedResourcesLoaded",
67 DOMContentLoaded: "DOMContentLoaded",
69 WillReloadPage: "WillReloadPage",
70 InspectedURLChanged: "InspectedURLChanged",
71 SecurityOriginAdded: "SecurityOriginAdded",
72 SecurityOriginRemoved: "SecurityOriginRemoved",
73 ScreencastFrame: "ScreencastFrame",
74 ScreencastVisibilityChanged: "ScreencastVisibilityChanged",
75 ViewportChanged: "ViewportChanged"
78 WebInspector.ResourceTreeModel.prototype = {
79 _fetchResourceTree: function()
81 /** @type {!Object.<string, !WebInspector.ResourceTreeFrame>} */
83 delete this._cachedResourcesProcessed;
84 this._agent.getResourceTree(this._processCachedResources.bind(this));
87 _processCachedResources: function(error, mainFramePayload)
90 //FIXME: remove resourceTreeModel from worker
91 if (!this.target().isWorkerTarget())
92 console.error(JSON.stringify(error));
96 this.dispatchEventToListeners(WebInspector.ResourceTreeModel.EventTypes.WillLoadCachedResources);
97 this._inspectedPageURL = mainFramePayload.frame.url;
98 this._addFramesRecursively(null, mainFramePayload);
99 this._dispatchInspectedURLChanged();
100 this.dispatchEventToListeners(WebInspector.ResourceTreeModel.EventTypes.CachedResourcesLoaded);
101 this._cachedResourcesProcessed = true;
107 inspectedPageURL: function()
109 return this._inspectedPageURL;
115 inspectedPageDomain: function()
117 var parsedURL = this._inspectedPageURL ? this._inspectedPageURL.asParsedURL() : null;
118 return parsedURL ? parsedURL.host : "";
124 cachedResourcesLoaded: function()
126 return this._cachedResourcesProcessed;
129 _dispatchInspectedURLChanged: function()
131 InspectorFrontendHost.inspectedURLChanged(this._inspectedPageURL);
132 this.dispatchEventToListeners(WebInspector.ResourceTreeModel.EventTypes.InspectedURLChanged, this._inspectedPageURL);
136 * @param {!WebInspector.ResourceTreeFrame} frame
137 * @param {boolean=} aboutToNavigate
139 _addFrame: function(frame, aboutToNavigate)
141 this._frames[frame.id] = frame;
142 if (frame.isMainFrame())
143 this.mainFrame = frame;
144 this.dispatchEventToListeners(WebInspector.ResourceTreeModel.EventTypes.FrameAdded, frame);
145 if (!aboutToNavigate)
146 this._addSecurityOrigin(frame.securityOrigin);
150 * @param {string} securityOrigin
152 _addSecurityOrigin: function(securityOrigin)
154 if (!this._securityOriginFrameCount[securityOrigin]) {
155 this._securityOriginFrameCount[securityOrigin] = 1;
156 this.dispatchEventToListeners(WebInspector.ResourceTreeModel.EventTypes.SecurityOriginAdded, securityOrigin);
159 this._securityOriginFrameCount[securityOrigin] += 1;
163 * @param {string|undefined} securityOrigin
165 _removeSecurityOrigin: function(securityOrigin)
167 if (typeof securityOrigin === "undefined")
169 if (this._securityOriginFrameCount[securityOrigin] === 1) {
170 delete this._securityOriginFrameCount[securityOrigin];
171 this.dispatchEventToListeners(WebInspector.ResourceTreeModel.EventTypes.SecurityOriginRemoved, securityOrigin);
174 this._securityOriginFrameCount[securityOrigin] -= 1;
178 * @return {!Array.<string>}
180 securityOrigins: function()
182 return Object.keys(this._securityOriginFrameCount);
186 * @param {!WebInspector.ResourceTreeFrame} mainFrame
188 _handleMainFrameDetached: function(mainFrame)
191 * @param {!WebInspector.ResourceTreeFrame} frame
192 * @this {WebInspector.ResourceTreeModel}
194 function removeOriginForFrame(frame)
196 for (var i = 0; i < frame.childFrames.length; ++i)
197 removeOriginForFrame.call(this, frame.childFrames[i]);
198 if (!frame.isMainFrame())
199 this._removeSecurityOrigin(frame.securityOrigin);
201 removeOriginForFrame.call(this, WebInspector.resourceTreeModel.mainFrame);
205 * @param {!PageAgent.FrameId} frameId
206 * @param {?PageAgent.FrameId} parentFrameId
207 * @return {?WebInspector.ResourceTreeFrame}
209 _frameAttached: function(frameId, parentFrameId)
211 // Do nothing unless cached resource tree is processed - it will overwrite everything.
212 if (!this._cachedResourcesProcessed)
214 if (this._frames[frameId])
217 var parentFrame = parentFrameId ? this._frames[parentFrameId] : null;
218 var frame = new WebInspector.ResourceTreeFrame(this, parentFrame, frameId);
219 if (frame.isMainFrame() && this.mainFrame) {
220 this._handleMainFrameDetached(this.mainFrame);
221 // Navigation to the new backend process.
222 this._frameDetached(this.mainFrame.id);
224 this._addFrame(frame, true);
229 * @param {!PageAgent.Frame} framePayload
231 _frameNavigated: function(framePayload)
233 // Do nothing unless cached resource tree is processed - it will overwrite everything.
234 if (!this._cachedResourcesProcessed)
236 var frame = this._frames[framePayload.id];
238 // Simulate missed "frameAttached" for a main frame navigation to the new backend process.
239 console.assert(!framePayload.parentId, "Main frame shouldn't have parent frame id.");
240 frame = this._frameAttached(framePayload.id, framePayload.parentId || "");
241 console.assert(frame);
243 this._removeSecurityOrigin(frame.securityOrigin);
244 frame._navigate(framePayload);
245 var addedOrigin = frame.securityOrigin;
247 if (frame.isMainFrame())
248 this._inspectedPageURL = frame.url;
250 this.dispatchEventToListeners(WebInspector.ResourceTreeModel.EventTypes.FrameNavigated, frame);
251 if (frame.isMainFrame())
252 this.dispatchEventToListeners(WebInspector.ResourceTreeModel.EventTypes.MainFrameNavigated, frame);
254 this._addSecurityOrigin(addedOrigin);
256 // Fill frame with retained resources (the ones loaded using new loader).
257 var resources = frame.resources();
258 for (var i = 0; i < resources.length; ++i)
259 this.dispatchEventToListeners(WebInspector.ResourceTreeModel.EventTypes.ResourceAdded, resources[i]);
261 if (frame.isMainFrame())
262 this._dispatchInspectedURLChanged();
266 * @param {!PageAgent.FrameId} frameId
268 _frameDetached: function(frameId)
270 // Do nothing unless cached resource tree is processed - it will overwrite everything.
271 if (!this._cachedResourcesProcessed)
274 var frame = this._frames[frameId];
278 this._removeSecurityOrigin(frame.securityOrigin);
279 if (frame.parentFrame)
280 frame.parentFrame._removeChildFrame(frame);
286 * @param {!WebInspector.Event} event
288 _onRequestFinished: function(event)
290 if (!this._cachedResourcesProcessed)
293 var request = /** @type {!WebInspector.NetworkRequest} */ (event.data);
294 if (request.failed || request.type === WebInspector.resourceTypes.XHR)
297 var frame = this._frames[request.frameId];
299 var resource = frame._addRequest(request);
300 this._addPendingConsoleMessagesToResource(resource);
305 * @param {!WebInspector.Event} event
307 _onRequestUpdateDropped: function(event)
309 if (!this._cachedResourcesProcessed)
312 var frameId = event.data.frameId;
313 var frame = this._frames[frameId];
317 var url = event.data.url;
318 if (frame._resourcesMap[url])
321 var resource = new WebInspector.Resource(this.target(), null, url, frame.url, frameId, event.data.loaderId, WebInspector.resourceTypes[event.data.resourceType], event.data.mimeType);
322 frame.addResource(resource);
326 * @param {!PageAgent.FrameId} frameId
327 * @return {!WebInspector.ResourceTreeFrame}
329 frameForId: function(frameId)
331 return this._frames[frameId];
335 * @param {function(!WebInspector.Resource)} callback
338 forAllResources: function(callback)
341 return this.mainFrame._callForFrameResources(callback);
346 * @return {!Array.<!WebInspector.ResourceTreeFrame>}
350 return Object.values(this._frames);
354 * @param {!WebInspector.Event} event
356 _consoleMessageAdded: function(event)
358 var msg = /** @type {!WebInspector.ConsoleMessage} */ (event.data);
359 var resource = msg.url ? this.resourceForURL(msg.url) : null;
361 this._addConsoleMessageToResource(msg, resource);
363 this._addPendingConsoleMessage(msg);
367 * @param {!WebInspector.ConsoleMessage} msg
369 _addPendingConsoleMessage: function(msg)
373 if (!this._pendingConsoleMessages[msg.url])
374 this._pendingConsoleMessages[msg.url] = [];
375 this._pendingConsoleMessages[msg.url].push(msg);
379 * @param {!WebInspector.Resource} resource
381 _addPendingConsoleMessagesToResource: function(resource)
383 var messages = this._pendingConsoleMessages[resource.url];
385 for (var i = 0; i < messages.length; i++)
386 this._addConsoleMessageToResource(messages[i], resource);
387 delete this._pendingConsoleMessages[resource.url];
392 * @param {!WebInspector.ConsoleMessage} msg
393 * @param {!WebInspector.Resource} resource
395 _addConsoleMessageToResource: function(msg, resource)
398 case WebInspector.ConsoleMessage.MessageLevel.Warning:
401 case WebInspector.ConsoleMessage.MessageLevel.Error:
405 resource.addMessage(msg);
408 _consoleCleared: function()
410 function callback(resource)
412 resource.clearErrorsAndWarnings();
415 this._pendingConsoleMessages = {};
416 this.forAllResources(callback);
420 * @param {string} url
421 * @return {?WebInspector.Resource}
423 resourceForURL: function(url)
425 // Workers call into this with no frames available.
426 return this.mainFrame ? this.mainFrame.resourceForURL(url) : null;
430 * @param {?WebInspector.ResourceTreeFrame} parentFrame
431 * @param {!PageAgent.FrameResourceTree} frameTreePayload
433 _addFramesRecursively: function(parentFrame, frameTreePayload)
435 var framePayload = frameTreePayload.frame;
436 var frame = new WebInspector.ResourceTreeFrame(this, parentFrame, framePayload.id, framePayload);
437 this._addFrame(frame);
439 var frameResource = this._createResourceFromFramePayload(framePayload, framePayload.url, WebInspector.resourceTypes.Document, framePayload.mimeType);
440 if (frame.isMainFrame())
441 this._inspectedPageURL = frameResource.url;
442 frame.addResource(frameResource);
444 for (var i = 0; frameTreePayload.childFrames && i < frameTreePayload.childFrames.length; ++i)
445 this._addFramesRecursively(frame, frameTreePayload.childFrames[i]);
447 for (var i = 0; i < frameTreePayload.resources.length; ++i) {
448 var subresource = frameTreePayload.resources[i];
449 var resource = this._createResourceFromFramePayload(framePayload, subresource.url, WebInspector.resourceTypes[subresource.type], subresource.mimeType);
450 frame.addResource(resource);
455 * @param {!PageAgent.Frame} frame
456 * @param {string} url
457 * @param {!WebInspector.ResourceType} type
458 * @param {string} mimeType
459 * @return {!WebInspector.Resource}
461 _createResourceFromFramePayload: function(frame, url, type, mimeType)
463 return new WebInspector.Resource(this.target(), null, url, frame.url, frame.id, frame.loaderId, type, mimeType);
467 * @param {boolean=} ignoreCache
468 * @param {string=} scriptToEvaluateOnLoad
469 * @param {string=} scriptPreprocessor
471 reloadPage: function(ignoreCache, scriptToEvaluateOnLoad, scriptPreprocessor)
473 this.dispatchEventToListeners(WebInspector.ResourceTreeModel.EventTypes.WillReloadPage);
474 this._agent.reload(ignoreCache, scriptToEvaluateOnLoad, scriptPreprocessor);
477 __proto__: WebInspector.SDKModel.prototype
482 * @param {!WebInspector.ResourceTreeModel} model
483 * @param {?WebInspector.ResourceTreeFrame} parentFrame
484 * @param {!PageAgent.FrameId} frameId
485 * @param {!PageAgent.Frame=} payload
487 WebInspector.ResourceTreeFrame = function(model, parentFrame, frameId, payload)
490 this._parentFrame = parentFrame;
495 this._loaderId = payload.loaderId;
496 this._name = payload.name;
497 this._url = payload.url;
498 this._securityOrigin = payload.securityOrigin;
499 this._mimeType = payload.mimeType;
503 * @type {!Array.<!WebInspector.ResourceTreeFrame>}
505 this._childFrames = [];
508 * @type {!Object.<string, !WebInspector.Resource>}
510 this._resourcesMap = {};
512 if (this._parentFrame)
513 this._parentFrame._childFrames.push(this);
516 WebInspector.ResourceTreeFrame.prototype = {
518 * @return {!WebInspector.Target}
522 return this._model.target();
538 return this._name || "";
554 return this._securityOrigin;
562 return this._loaderId;
566 * @return {?WebInspector.ResourceTreeFrame}
570 return this._parentFrame;
574 * @return {!Array.<!WebInspector.ResourceTreeFrame>}
578 return this._childFrames;
584 isMainFrame: function()
586 return !this._parentFrame;
590 * @param {!PageAgent.Frame} framePayload
592 _navigate: function(framePayload)
594 this._loaderId = framePayload.loaderId;
595 this._name = framePayload.name;
596 this._url = framePayload.url;
597 this._securityOrigin = framePayload.securityOrigin;
598 this._mimeType = framePayload.mimeType;
600 var mainResource = this._resourcesMap[this._url];
601 this._resourcesMap = {};
602 this._removeChildFrames();
603 if (mainResource && mainResource.loaderId === this._loaderId)
604 this.addResource(mainResource);
608 * @return {!WebInspector.Resource}
612 return this._resourcesMap[this._url];
616 * @param {!WebInspector.ResourceTreeFrame} frame
618 _removeChildFrame: function(frame)
620 this._childFrames.remove(frame);
624 _removeChildFrames: function()
626 var frames = this._childFrames;
627 this._childFrames = [];
628 for (var i = 0; i < frames.length; ++i)
634 this._removeChildFrames();
635 delete this._model._frames[this.id];
636 this._model.dispatchEventToListeners(WebInspector.ResourceTreeModel.EventTypes.FrameDetached, this);
640 * @param {!WebInspector.Resource} resource
642 addResource: function(resource)
644 if (this._resourcesMap[resource.url] === resource) {
645 // Already in the tree, we just got an extra update.
648 this._resourcesMap[resource.url] = resource;
649 this._model.dispatchEventToListeners(WebInspector.ResourceTreeModel.EventTypes.ResourceAdded, resource);
653 * @param {!WebInspector.NetworkRequest} request
654 * @return {!WebInspector.Resource}
656 _addRequest: function(request)
658 var resource = this._resourcesMap[request.url];
659 if (resource && resource.request === request) {
660 // Already in the tree, we just got an extra update.
663 resource = new WebInspector.Resource(this.target(), request, request.url, request.documentURL, request.frameId, request.loaderId, request.type, request.mimeType);
664 this._resourcesMap[resource.url] = resource;
665 this._model.dispatchEventToListeners(WebInspector.ResourceTreeModel.EventTypes.ResourceAdded, resource);
670 * @return {!Array.<!WebInspector.Resource>}
672 resources: function()
675 for (var url in this._resourcesMap)
676 result.push(this._resourcesMap[url]);
681 * @param {string} url
682 * @return {?WebInspector.Resource}
684 resourceForURL: function(url)
687 function filter(resource)
689 if (resource.url === url) {
694 this._callForFrameResources(filter);
695 return result || null;
699 * @param {function(!WebInspector.Resource)} callback
702 _callForFrameResources: function(callback)
704 for (var url in this._resourcesMap) {
705 if (callback(this._resourcesMap[url]))
709 for (var i = 0; i < this._childFrames.length; ++i) {
710 if (this._childFrames[i]._callForFrameResources(callback))
719 displayName: function()
721 if (!this._parentFrame)
722 return WebInspector.UIString("<top frame>");
723 var subtitle = new WebInspector.ParsedURL(this._url).displayName;
727 return this._name + "( " + subtitle + " )";
729 return WebInspector.UIString("<iframe>");
735 * @implements {PageAgent.Dispatcher}
737 WebInspector.PageDispatcher = function(resourceTreeModel)
739 this._resourceTreeModel = resourceTreeModel;
742 WebInspector.PageDispatcher.prototype = {
743 domContentEventFired: function(time)
745 this._resourceTreeModel.dispatchEventToListeners(WebInspector.ResourceTreeModel.EventTypes.DOMContentLoaded, time);
748 loadEventFired: function(time)
750 this._resourceTreeModel.dispatchEventToListeners(WebInspector.ResourceTreeModel.EventTypes.Load, time);
753 frameAttached: function(frameId, parentFrameId)
755 this._resourceTreeModel._frameAttached(frameId, parentFrameId);
758 frameNavigated: function(frame)
760 this._resourceTreeModel._frameNavigated(frame);
763 frameDetached: function(frameId)
765 this._resourceTreeModel._frameDetached(frameId);
768 frameStartedLoading: function(frameId)
772 frameStoppedLoading: function(frameId)
776 frameScheduledNavigation: function(frameId, delay)
780 frameClearedScheduledNavigation: function(frameId)
784 frameResized: function()
786 this._resourceTreeModel.dispatchEventToListeners(WebInspector.ResourceTreeModel.EventTypes.FrameResized, null);
789 javascriptDialogOpening: function(message)
793 javascriptDialogClosed: function()
797 scriptsEnabled: function(isEnabled)
799 WebInspector.settings.javaScriptDisabled.set(!isEnabled);
803 * @param {string} data
804 * @param {!PageAgent.ScreencastFrameMetadata=} metadata
806 screencastFrame: function(data, metadata)
808 this._resourceTreeModel.dispatchEventToListeners(WebInspector.ResourceTreeModel.EventTypes.ScreencastFrame, {data:data, metadata:metadata});
812 * @param {boolean} visible
814 screencastVisibilityChanged: function(visible)
816 this._resourceTreeModel.dispatchEventToListeners(WebInspector.ResourceTreeModel.EventTypes.ScreencastVisibilityChanged, {visible:visible});
820 * @param {!PageAgent.Viewport=} viewport
822 viewportChanged: function(viewport)
824 this._resourceTreeModel.dispatchEventToListeners(WebInspector.ResourceTreeModel.EventTypes.ViewportChanged, viewport);
829 * @type {!WebInspector.ResourceTreeModel}
831 WebInspector.resourceTreeModel;