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    WebInspector.cssModel.addEventListener(WebInspector.CSSStyleModel.Events.StyleSheetAdded, this._styleSheetAdded, this);
44
45    this._processedURLs = {};
46}
47
48WebInspector.NetworkUISourceCodeProvider.prototype = {
49    _populate: function()
50    {
51        /**
52         * @param {!WebInspector.ResourceTreeFrame} frame
53         * @this {WebInspector.NetworkUISourceCodeProvider}
54         */
55        function populateFrame(frame)
56        {
57            for (var i = 0; i < frame.childFrames.length; ++i)
58                populateFrame.call(this, frame.childFrames[i]);
59
60            var resources = frame.resources();
61            for (var i = 0; i < resources.length; ++i)
62                this._resourceAdded({data:resources[i]});
63        }
64
65        populateFrame.call(this, WebInspector.resourceTreeModel.mainFrame);
66    },
67
68    /**
69     * @param {!WebInspector.Event} event
70     */
71    _parsedScriptSource: function(event)
72    {
73        var script = /** @type {!WebInspector.Script} */ (event.data);
74        if (!script.sourceURL || script.isInlineScript() || script.isSnippet())
75            return;
76        // Filter out embedder injected content scripts.
77        if (script.isContentScript && !script.hasSourceURL) {
78            var parsedURL = new WebInspector.ParsedURL(script.sourceURL);
79            if (!parsedURL.isValid)
80                return;
81        }
82        this._addFile(script.sourceURL, script, script.isContentScript);
83    },
84
85    /**
86     * @param {!WebInspector.Event} event
87     */
88    _styleSheetAdded: function(event)
89    {
90        var header = /** @type {!WebInspector.CSSStyleSheetHeader} */ (event.data);
91        if ((!header.hasSourceURL || header.isInline) && header.origin !== "inspector")
92            return;
93
94        this._addFile(header.resourceURL(), header, false);
95    },
96
97    /**
98     * @param {!WebInspector.Event|{data: !WebInspector.Resource}} event
99     */
100    _resourceAdded: function(event)
101    {
102        var resource = /** @type {!WebInspector.Resource} */ (event.data);
103        this._addFile(resource.url, resource);
104    },
105
106    /**
107     * @param {!WebInspector.Event} event
108     */
109    _mainFrameNavigated: function(event)
110    {
111        this._reset();
112    },
113
114    /**
115     * @param {string} url
116     * @param {!WebInspector.ContentProvider} contentProvider
117     * @param {boolean=} isContentScript
118     */
119    _addFile: function(url, contentProvider, isContentScript)
120    {
121        if (this._workspace.hasMappingForURL(url))
122            return;
123
124        var type = contentProvider.contentType();
125        if (type !== WebInspector.resourceTypes.Stylesheet && type !== WebInspector.resourceTypes.Document && type !== WebInspector.resourceTypes.Script)
126            return;
127        if (this._processedURLs[url])
128            return;
129        this._processedURLs[url] = true;
130        var isEditable = type !== WebInspector.resourceTypes.Document;
131        this._networkWorkspaceProvider.addFileForURL(url, contentProvider, isEditable, isContentScript);
132    },
133
134    _reset: function()
135    {
136        this._processedURLs = {};
137        this._networkWorkspaceProvider.reset();
138        this._populate();
139    }
140}
141
142/**
143 * @type {!WebInspector.SimpleWorkspaceProvider}
144 */
145WebInspector.networkWorkspaceProvider;
146