1e4dcd6aa5ec7ac0e7484c76818c8bad8fd45937
[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             if (!resource) {
97                 // When there is no resource, lookup in-flight requests.
98                 request = WebInspector.networkManager.inflightRequestForURL(script.sourceURL);
99             }
100         }
101         console.assert(!resource || !request);
102
103         var isInlineScript = script.isInlineScript() && (request || resource);
104         // If either of these exists, we bind script to the resource.
105         if (isInlineScript) {
106             var rawSourceCode = this._rawSourceCodeForDocumentURL[script.sourceURL];
107             if (rawSourceCode) {
108                 rawSourceCode.addScript(script);
109                 this._bindScriptToRawSourceCode(script, rawSourceCode);
110                 return;
111             }
112         }
113
114         var rawSourceCode = new WebInspector.RawSourceCode(script.scriptId, script, resource, request, this);
115         this._rawSourceCodes.push(rawSourceCode);
116         this._bindScriptToRawSourceCode(script, rawSourceCode);
117         if (isInlineScript)
118             this._rawSourceCodeForDocumentURL[script.sourceURL] = rawSourceCode;
119
120         if (rawSourceCode.uiSourceCode())
121             this._uiSourceCodeAdded(rawSourceCode, rawSourceCode.uiSourceCode());
122         rawSourceCode.addEventListener(WebInspector.RawSourceCode.Events.UISourceCodeChanged, this._handleUISourceCodeChanged, this);
123     },
124
125     /**
126      * @param {WebInspector.Event} event
127      */
128     _handleUISourceCodeChanged: function(event)
129     {
130         var rawSourceCode = /** @type {WebInspector.RawSourceCode} */ event.target;
131         var uiSourceCode = /** @type {WebInspector.UISourceCode} */ event.data.uiSourceCode;
132         var oldUISourceCode = /** @type {WebInspector.UISourceCode} */ event.data.oldUISourceCode;
133         if (!oldUISourceCode)
134             this._uiSourceCodeAdded(rawSourceCode, uiSourceCode);
135         else
136             this._uiSourceCodeReplaced(rawSourceCode, oldUISourceCode, uiSourceCode);
137     },
138
139     /**
140      * @param {WebInspector.RawSourceCode} rawSourceCode
141      * @paran {WebInspector.UISourceCode} uiSourceCode
142      */
143     _uiSourceCodeAdded: function(rawSourceCode, uiSourceCode)
144     {
145         this._rawSourceCodeForUISourceCode.put(uiSourceCode, rawSourceCode);
146         this.dispatchEventToListeners(WebInspector.UISourceCodeProvider.Events.UISourceCodeAdded, uiSourceCode);
147     },
148
149     /**
150      * @param {WebInspector.RawSourceCode} rawSourceCode
151      * @param {WebInspector.UISourceCode} oldUISourceCode
152      * @param {WebInspector.UISourceCode} uiSourceCode
153      */
154     _uiSourceCodeReplaced: function(rawSourceCode, oldUISourceCode, uiSourceCode)
155     {
156         this._rawSourceCodeForUISourceCode.remove(oldUISourceCode);
157         this._rawSourceCodeForUISourceCode.put(uiSourceCode, rawSourceCode);
158
159         for (var i = 0; i < rawSourceCode._scripts.length; ++i)
160             rawSourceCode._scripts[i].setSourceMapping(this);
161
162         var data = { oldUISourceCode: oldUISourceCode, uiSourceCode: uiSourceCode };
163         this.dispatchEventToListeners(WebInspector.UISourceCodeProvider.Events.UISourceCodeReplaced, data);
164     },
165
166     /**
167      * @param {WebInspector.RawSourceCode} rawSourceCode
168      * @paran {WebInspector.UISourceCode} uiSourceCode
169      */
170     _uiSourceCodeRemoved: function(rawSourceCode, uiSourceCode)
171     {
172         this._rawSourceCodeForUISourceCode.remove(uiSourceCode);
173         this.dispatchEventToListeners(WebInspector.UISourceCodeProvider.Events.UISourceCodeRemoved, uiSourceCode);
174     },
175
176     /**
177      * @param {WebInspector.Script} script
178      * @param {WebInspector.RawSourceCode} rawSourceCode
179      */
180     _bindScriptToRawSourceCode: function(script, rawSourceCode)
181     {
182         this._rawSourceCodeForScriptId[script.scriptId] = rawSourceCode;
183         this._rawSourceCodeForURL[script.sourceURL] = rawSourceCode;
184         script.setSourceMapping(this);
185     },
186
187     reset: function()
188     {
189         for (var i = 0; i < this._rawSourceCodes.length; ++i) {
190             var rawSourceCode = this._rawSourceCodes[i];
191             this._uiSourceCodeRemoved(rawSourceCode, rawSourceCode.uiSourceCode());
192             rawSourceCode.removeAllListeners();
193         }
194         this._rawSourceCodes = [];
195         this._rawSourceCodeForScriptId = {};
196         this._rawSourceCodeForURL = {};
197         this._rawSourceCodeForDocumentURL = {};
198         this._rawSourceCodeForUISourceCode.clear();
199     }
200 }
201
202 WebInspector.ResourceScriptMapping.prototype.__proto__ = WebInspector.Object.prototype;