2 * Copyright (C) 2012 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 * @implements {WebInspector.ScriptSourceMapping}
34 * @param {!WebInspector.DebuggerModel} debuggerModel
35 * @param {!WebInspector.Workspace} workspace
36 * @param {!WebInspector.SimpleWorkspaceProvider} networkWorkspaceProvider
38 WebInspector.CompilerScriptMapping = function(debuggerModel, workspace, networkWorkspaceProvider)
40 this._debuggerModel = debuggerModel;
41 this._workspace = workspace;
42 this._workspace.addEventListener(WebInspector.Workspace.Events.UISourceCodeAdded, this._uiSourceCodeAddedToWorkspace, this);
43 this._networkWorkspaceProvider = networkWorkspaceProvider;
44 /** @type {!Object.<string, !WebInspector.SourceMap>} */
45 this._sourceMapForSourceMapURL = {};
46 /** @type {!Object.<string, !Array.<function(?WebInspector.SourceMap)>>} */
47 this._pendingSourceMapLoadingCallbacks = {};
48 /** @type {!Object.<string, !WebInspector.SourceMap>} */
49 this._sourceMapForScriptId = {};
50 /** @type {!Map.<!WebInspector.SourceMap, !WebInspector.Script>} */
51 this._scriptForSourceMap = new Map();
52 /** @type {!StringMap.<!WebInspector.SourceMap>} */
53 this._sourceMapForURL = new StringMap();
54 debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.GlobalObjectCleared, this._debuggerReset, this);
57 WebInspector.CompilerScriptMapping.prototype = {
59 * @param {!WebInspector.RawLocation} rawLocation
60 * @return {?WebInspector.UILocation}
62 rawLocationToUILocation: function(rawLocation)
64 var debuggerModelLocation = /** @type {!WebInspector.DebuggerModel.Location} */ (rawLocation);
65 var sourceMap = this._sourceMapForScriptId[debuggerModelLocation.scriptId];
68 var lineNumber = debuggerModelLocation.lineNumber;
69 var columnNumber = debuggerModelLocation.columnNumber || 0;
70 var entry = sourceMap.findEntry(lineNumber, columnNumber);
71 if (!entry || entry.length === 2)
73 var url = /** @type {string} */ (entry[2]);
74 var uiSourceCode = this._workspace.uiSourceCodeForURL(url);
77 return new WebInspector.UILocation(uiSourceCode, /** @type {number} */ (entry[3]), /** @type {number} */ (entry[4]));
81 * @param {!WebInspector.UISourceCode} uiSourceCode
82 * @param {number} lineNumber
83 * @param {number} columnNumber
84 * @return {?WebInspector.DebuggerModel.Location}
86 uiLocationToRawLocation: function(uiSourceCode, lineNumber, columnNumber)
88 if (!uiSourceCode.url)
90 var sourceMap = this._sourceMapForURL.get(uiSourceCode.url);
93 var script = /** @type {!WebInspector.Script} */ (this._scriptForSourceMap.get(sourceMap));
94 console.assert(script);
95 var entry = sourceMap.findEntryReversed(uiSourceCode.url, lineNumber);
96 return this._debuggerModel.createRawLocation(script, /** @type {number} */ (entry[0]), /** @type {number} */ (entry[1]));
100 * @param {!WebInspector.Script} script
102 addScript: function(script)
104 script.pushSourceMapping(this);
105 this.loadSourceMapForScript(script, sourceMapLoaded.bind(this));
108 * @param {?WebInspector.SourceMap} sourceMap
109 * @this {WebInspector.CompilerScriptMapping}
111 function sourceMapLoaded(sourceMap)
116 if (this._scriptForSourceMap.get(sourceMap)) {
117 this._sourceMapForScriptId[script.scriptId] = sourceMap;
118 script.updateLocations();
122 this._sourceMapForScriptId[script.scriptId] = sourceMap;
123 this._scriptForSourceMap.put(sourceMap, script);
125 var sourceURLs = sourceMap.sources();
126 for (var i = 0; i < sourceURLs.length; ++i) {
127 var sourceURL = sourceURLs[i];
128 if (this._sourceMapForURL.get(sourceURL))
130 this._sourceMapForURL.put(sourceURL, sourceMap);
131 if (!this._workspace.hasMappingForURL(sourceURL) && !this._workspace.uiSourceCodeForURL(sourceURL)) {
132 var contentProvider = sourceMap.sourceContentProvider(sourceURL, WebInspector.resourceTypes.Script);
133 this._networkWorkspaceProvider.addFileForURL(sourceURL, contentProvider, true);
135 var uiSourceCode = this._workspace.uiSourceCodeForURL(sourceURL);
137 this._bindUISourceCode(uiSourceCode);
138 uiSourceCode.isContentScript = script.isContentScript;
140 WebInspector.showErrorMessage(WebInspector.UIString("Failed to locate workspace file mapped to URL %s from source map %s", sourceURL, sourceMap.url()));
143 script.updateLocations();
148 * @param {!WebInspector.UISourceCode} uiSourceCode
150 _bindUISourceCode: function(uiSourceCode)
152 uiSourceCode.setSourceMapping(this);
156 * @param {!WebInspector.UISourceCode} uiSourceCode
158 _unbindUISourceCode: function(uiSourceCode)
160 uiSourceCode.setSourceMapping(null);
164 * @param {!WebInspector.Event} event
166 _uiSourceCodeAddedToWorkspace: function(event)
168 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.data);
169 if (!uiSourceCode.url || !this._sourceMapForURL.get(uiSourceCode.url))
171 this._bindUISourceCode(uiSourceCode);
175 * @param {!WebInspector.Script} script
176 * @param {function(?WebInspector.SourceMap)} callback
178 loadSourceMapForScript: function(script, callback)
180 // script.sourceURL can be a random string, but is generally an absolute path -> complete it to inspected page url for
182 if (!script.sourceMapURL) {
186 var scriptURL = WebInspector.ParsedURL.completeURL(WebInspector.inspectedPageURL, script.sourceURL);
191 var sourceMapURL = WebInspector.ParsedURL.completeURL(scriptURL, script.sourceMapURL);
197 var sourceMap = this._sourceMapForSourceMapURL[sourceMapURL];
203 var pendingCallbacks = this._pendingSourceMapLoadingCallbacks[sourceMapURL];
204 if (pendingCallbacks) {
205 pendingCallbacks.push(callback);
209 pendingCallbacks = [callback];
210 this._pendingSourceMapLoadingCallbacks[sourceMapURL] = pendingCallbacks;
212 WebInspector.SourceMap.load(sourceMapURL, scriptURL, sourceMapLoaded.bind(this));
215 * @param {?WebInspector.SourceMap} sourceMap
216 * @this {WebInspector.CompilerScriptMapping}
218 function sourceMapLoaded(sourceMap)
220 var url = /** @type {string} */ (sourceMapURL);
221 var callbacks = this._pendingSourceMapLoadingCallbacks[url];
222 delete this._pendingSourceMapLoadingCallbacks[url];
226 this._sourceMapForSourceMapURL[url] = sourceMap;
227 for (var i = 0; i < callbacks.length; ++i)
228 callbacks[i](sourceMap);
232 _debuggerReset: function()
235 * @param {!WebInspector.SourceMap} sourceMap
236 * @this {WebInspector.CompilerScriptMapping}
238 function unbindUISourceCodesForSourceMap(sourceMap)
240 var sourceURLs = sourceMap.sources();
241 for (var i = 0; i < sourceURLs.length; ++i) {
242 var sourceURL = sourceURLs[i];
243 var uiSourceCode = this._workspace.uiSourceCodeForURL(sourceURL);
246 this._unbindUISourceCode(uiSourceCode);
250 this._sourceMapForURL.values().forEach(unbindUISourceCodesForSourceMap.bind(this));
252 this._sourceMapForSourceMapURL = {};
253 this._pendingSourceMapLoadingCallbacks = {};
254 this._sourceMapForScriptId = {};
255 this._scriptForSourceMap.clear();
256 this._sourceMapForURL.clear();