Web Inspector: [regression] dynamically created script tab is bound to the document...
[WebKit-https.git] / Source / WebCore / inspector / front-end / ResourceScriptMapping.js
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  * @extends {WebInspector.Object}
34  * @implements {WebInspector.SourceMapping}
35  * @implements {WebInspector.UISourceCodeProvider}
36  */
37 WebInspector.ResourceScriptMapping = function()
38 {
39     this._rawSourceCodes = [];
40     this._rawSourceCodeForScriptId = {};
41     this._rawSourceCodeForURL = {};
42     this._rawSourceCodeForDocumentURL = {};
43     this._rawSourceCodeForUISourceCode = new Map();
44 }
45
46 WebInspector.ResourceScriptMapping.prototype = {
47     /**
48      * @param {DebuggerAgent.Location} rawLocation
49      * @return {WebInspector.UILocation}
50      */
51     rawLocationToUILocation: function(rawLocation)
52     {
53         var rawSourceCode = this._rawSourceCodeForScriptId[rawLocation.scriptId];
54         return rawSourceCode.rawLocationToUILocation(rawLocation);
55     },
56
57     /**
58      * @param {WebInspector.UISourceCode} uiSourceCode
59      * @param {number} lineNumber
60      * @param {number} columnNumber
61      * @return {DebuggerAgent.Location}
62      */
63     uiLocationToRawLocation: function(uiSourceCode, lineNumber, columnNumber)
64     {
65         var rawSourceCode = this._rawSourceCodeForUISourceCode.get(uiSourceCode);
66         return rawSourceCode.uiLocationToRawLocation(uiSourceCode, lineNumber, columnNumber);
67     },
68
69     /**
70      * @return {Array.<WebInspector.UISourceCode>}
71      */
72     uiSourceCodes: function()
73     {
74         var result = [];
75         for (var i = 0; i < this._rawSourceCodes.length; ++i) {
76             var uiSourceCode = this._rawSourceCodes[i].uiSourceCode();
77             if (uiSourceCode)
78                 result.push(uiSourceCode);
79         }
80         return result;
81     },
82
83     /**
84      * @param {WebInspector.Script} script
85      */
86     addScript: function(script)
87     {
88         var resource = null;
89         var request = null;
90         if (!script.isAnonymousScript()) {
91             // First lookup the resource that has already been loaded.
92             resource = WebInspector.resourceForURL(script.sourceURL);
93             // Ignore resource in case it has not yet finished loading.
94             if (resource && resource.request && !resource.request.finished)
95                 resource = null;
96             // Only bind inline and standalone scripts.
97             if (resource && !this._shouldBindScriptToContentProvider(script, resource))
98                 resource = null;
99             if (!resource) {
100                 // When there is no resource, lookup in-flight requests.
101                 request = WebInspector.networkManager.inflightRequestForURL(script.sourceURL);
102                 // Only bind inline and standalone scripts.
103                 if (request && !this._shouldBindScriptToContentProvider(script, request))
104                   request = null;
105             }
106         }
107         console.assert(!resource || !request);
108
109         var isInlineScript = script.isInlineScript() && (request || resource);
110         // If either of these exists, we bind script to the resource.
111         if (isInlineScript) {
112             var rawSourceCode = this._rawSourceCodeForDocumentURL[script.sourceURL];
113             if (rawSourceCode) {
114                 rawSourceCode.addScript(script);
115                 this._bindScriptToRawSourceCode(script, rawSourceCode);
116                 return;
117             }
118         }
119
120         var rawSourceCode = new WebInspector.RawSourceCode(script.scriptId, script, resource, request, this);
121         this._rawSourceCodes.push(rawSourceCode);
122         this._bindScriptToRawSourceCode(script, rawSourceCode);
123         if (isInlineScript)
124             this._rawSourceCodeForDocumentURL[script.sourceURL] = rawSourceCode;
125
126         if (rawSourceCode.uiSourceCode())
127             this._uiSourceCodeAdded(rawSourceCode, rawSourceCode.uiSourceCode());
128         rawSourceCode.addEventListener(WebInspector.RawSourceCode.Events.UISourceCodeChanged, this._handleUISourceCodeChanged, this);
129     },
130
131     /**
132      * @param {WebInspector.Script} script
133      * @param {WebInspector.ContentProvider} contentProvider
134      * @return {boolean}
135      */
136     _shouldBindScriptToContentProvider: function(script, contentProvider)
137     {
138         if (script.isInlineScript())
139             return contentProvider.contentType() === WebInspector.resourceTypes.Document;
140         return contentProvider.contentType() === WebInspector.resourceTypes.Script;
141     },
142
143     /**
144      * @param {WebInspector.Event} event
145      */
146     _handleUISourceCodeChanged: function(event)
147     {
148         var rawSourceCode = /** @type {WebInspector.RawSourceCode} */ event.target;
149         var uiSourceCode = /** @type {WebInspector.UISourceCode} */ event.data.uiSourceCode;
150         var oldUISourceCode = /** @type {WebInspector.UISourceCode} */ event.data.oldUISourceCode;
151         if (!oldUISourceCode)
152             this._uiSourceCodeAdded(rawSourceCode, uiSourceCode);
153         else
154             this._uiSourceCodeReplaced(rawSourceCode, oldUISourceCode, uiSourceCode);
155     },
156
157     /**
158      * @param {WebInspector.RawSourceCode} rawSourceCode
159      * @paran {WebInspector.UISourceCode} uiSourceCode
160      */
161     _uiSourceCodeAdded: function(rawSourceCode, uiSourceCode)
162     {
163         this._rawSourceCodeForUISourceCode.put(uiSourceCode, rawSourceCode);
164         this.dispatchEventToListeners(WebInspector.UISourceCodeProvider.Events.UISourceCodeAdded, uiSourceCode);
165     },
166
167     /**
168      * @param {WebInspector.RawSourceCode} rawSourceCode
169      * @param {WebInspector.UISourceCode} oldUISourceCode
170      * @param {WebInspector.UISourceCode} uiSourceCode
171      */
172     _uiSourceCodeReplaced: function(rawSourceCode, oldUISourceCode, uiSourceCode)
173     {
174         this._rawSourceCodeForUISourceCode.remove(oldUISourceCode);
175         this._rawSourceCodeForUISourceCode.put(uiSourceCode, rawSourceCode);
176
177         for (var i = 0; i < rawSourceCode._scripts.length; ++i)
178             rawSourceCode._scripts[i].setSourceMapping(this);
179
180         var data = { oldUISourceCode: oldUISourceCode, uiSourceCode: uiSourceCode };
181         this.dispatchEventToListeners(WebInspector.UISourceCodeProvider.Events.UISourceCodeReplaced, data);
182     },
183
184     /**
185      * @param {WebInspector.RawSourceCode} rawSourceCode
186      * @paran {WebInspector.UISourceCode} uiSourceCode
187      */
188     _uiSourceCodeRemoved: function(rawSourceCode, uiSourceCode)
189     {
190         this._rawSourceCodeForUISourceCode.remove(uiSourceCode);
191         this.dispatchEventToListeners(WebInspector.UISourceCodeProvider.Events.UISourceCodeRemoved, uiSourceCode);
192     },
193
194     /**
195      * @param {WebInspector.Script} script
196      * @param {WebInspector.RawSourceCode} rawSourceCode
197      */
198     _bindScriptToRawSourceCode: function(script, rawSourceCode)
199     {
200         this._rawSourceCodeForScriptId[script.scriptId] = rawSourceCode;
201         this._rawSourceCodeForURL[script.sourceURL] = rawSourceCode;
202         script.setSourceMapping(this);
203     },
204
205     reset: function()
206     {
207         for (var i = 0; i < this._rawSourceCodes.length; ++i) {
208             var rawSourceCode = this._rawSourceCodes[i];
209             this._uiSourceCodeRemoved(rawSourceCode, rawSourceCode.uiSourceCode());
210             rawSourceCode.removeAllListeners();
211         }
212         this._rawSourceCodes = [];
213         this._rawSourceCodeForScriptId = {};
214         this._rawSourceCodeForURL = {};
215         this._rawSourceCodeForDocumentURL = {};
216         this._rawSourceCodeForUISourceCode.clear();
217     }
218 }
219
220 WebInspector.ResourceScriptMapping.prototype.__proto__ = WebInspector.Object.prototype;