1/*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
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.
17 *
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.
29 */
30
31/**
32 * @constructor
33 * @param {WebInspector.SimpleWorkspaceProvider} networkWorkspaceProvider
34 * @param {WebInspector.Workspace} workspace
35 */
36WebInspector.NetworkUISourceCodeProvider = function(networkWorkspaceProvider, workspace)
37{
38    this._networkWorkspaceProvider = networkWorkspaceProvider;
39    this._workspace = workspace;
40    WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.ResourceAdded, this._resourceAdded, this);
41    WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.MainFrameNavigated, this._mainFrameNavigated, this);
42    WebInspector.debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.ParsedScriptSource, this._parsedScriptSource, this);
43
44    this._processedURLs = {};
45    this._lastDynamicAnonymousScriptIndexForURL = {};
46}
47
48WebInspector.NetworkUISourceCodeProvider.prototype = {
49    _populate: function()
50    {
51        function populateFrame(frame)
52        {
53            for (var i = 0; i < frame.childFrames.length; ++i)
54                populateFrame.call(this, frame.childFrames[i]);
55
56            var resources = frame.resources();
57            for (var i = 0; i < resources.length; ++i)
58                this._resourceAdded({data:resources[i]});
59        }
60
61        populateFrame.call(this, WebInspector.resourceTreeModel.mainFrame);
62    },
63
64    /**
65     * @param {WebInspector.Event} event
66     */
67    _parsedScriptSource: function(event)
68    {
69        var script = /** @type {WebInspector.Script} */ (event.data);
70        if (!script.sourceURL || script.isInlineScript())
71            return;
72        if (WebInspector.experimentsSettings.snippetsSupport.isEnabled() && script.isSnippet())
73            return;
74        var isDynamicAnonymousScript;
75        // Only add uiSourceCodes for
76        // - content scripts;
77        // - scripts with explicit sourceURL comment;
78        // - dynamic scripts (script elements with src attribute) when inspector is opened after the script was loaded.
79        if (!script.hasSourceURL && !script.isContentScript) {
80            var requestURL = script.sourceURL.replace(/#.*/, "");
81            if (WebInspector.resourceForURL(requestURL) || WebInspector.networkLog.requestForURL(requestURL))
82                return;
83        }
84        // Filter out embedder injected content scripts.
85        if (script.isContentScript && !script.hasSourceURL) {
86            var parsedURL = new WebInspector.ParsedURL(script.sourceURL);
87            if (!parsedURL.host)
88                return;
89        }
90        this._addFile(script.sourceURL, script, script.isContentScript);
91    },
92
93    /**
94     * @param {WebInspector.Event} event
95     */
96    _resourceAdded: function(event)
97    {
98        var resource = /** @type {WebInspector.Resource} */ (event.data);
99        this._addFile(resource.url, resource);
100    },
101
102    /**
103     * @param {WebInspector.Event} event
104     */
105    _mainFrameNavigated: function(event)
106    {
107        this._reset();
108    },
109
110    /**
111     * @param {string} url
112     * @param {WebInspector.ContentProvider} contentProvider
113     * @param {boolean=} isContentScript
114     */
115    _addFile: function(url, contentProvider, isContentScript)
116    {
117        if (this._workspace.hasMappingForURL(url))
118            return;
119
120        var type = contentProvider.contentType();
121        if (type !== WebInspector.resourceTypes.Stylesheet && type !== WebInspector.resourceTypes.Document && type !== WebInspector.resourceTypes.Script)
122            return;
123        if (this._processedURLs[url])
124            return;
125        this._processedURLs[url] = true;
126        var isEditable = type !== WebInspector.resourceTypes.Document;
127        this._networkWorkspaceProvider.addFileForURL(url, contentProvider, isEditable, isContentScript);
128    },
129
130    _reset: function()
131    {
132        this._processedURLs = {};
133        this._lastDynamicAnonymousScriptIndexForURL = {};
134        this._networkWorkspaceProvider.reset();
135        this._populate();
136    }
137}
138
139/**
140 * @type {?WebInspector.SimpleWorkspaceProvider}
141 */
142WebInspector.networkWorkspaceProvider = null;
143