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.NetworkWorkspaceBinding} networkWorkspaceBinding
38 WebInspector.CompilerScriptMapping = function(debuggerModel, workspace, networkWorkspaceBinding)
40 this._debuggerModel = debuggerModel;
41 this._workspace = workspace;
42 this._workspace.addEventListener(WebInspector.Workspace.Events.UISourceCodeAdded, this._uiSourceCodeAddedToWorkspace, this);
43 this._networkWorkspaceBinding = networkWorkspaceBinding;
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 uiSourceCode.uiLocation(/** @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._networkWorkspaceBinding.addFileForURL(sourceURL, contentProvider, script.isContentScript());
135 var uiSourceCode = this._workspace.uiSourceCodeForURL(sourceURL);
137 this._bindUISourceCode(uiSourceCode);
139 WebInspector.console.showErrorMessage(WebInspector.UIString("Failed to locate workspace file mapped to URL %s from source map %s", sourceURL, sourceMap.url()));
141 script.updateLocations();
148 isIdentity: function()
154 * @param {!WebInspector.UISourceCode} uiSourceCode
156 _bindUISourceCode: function(uiSourceCode)
158 uiSourceCode.setSourceMapping(this);
162 * @param {!WebInspector.UISourceCode} uiSourceCode
164 _unbindUISourceCode: function(uiSourceCode)
166 uiSourceCode.setSourceMapping(null);
170 * @param {!WebInspector.Event} event
172 _uiSourceCodeAddedToWorkspace: function(event)
174 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.data);
175 if (!uiSourceCode.url || !this._sourceMapForURL.get(uiSourceCode.url))
177 this._bindUISourceCode(uiSourceCode);
181 * @param {!WebInspector.Script} script
182 * @param {function(?WebInspector.SourceMap)} callback
184 loadSourceMapForScript: function(script, callback)
186 // script.sourceURL can be a random string, but is generally an absolute path -> complete it to inspected page url for
188 if (!script.sourceMapURL) {
192 var scriptURL = WebInspector.ParsedURL.completeURL(script.target().resourceTreeModel.inspectedPageURL(), script.sourceURL);
197 var sourceMapURL = WebInspector.ParsedURL.completeURL(scriptURL, script.sourceMapURL);
203 var sourceMap = this._sourceMapForSourceMapURL[sourceMapURL];
209 var pendingCallbacks = this._pendingSourceMapLoadingCallbacks[sourceMapURL];
210 if (pendingCallbacks) {
211 pendingCallbacks.push(callback);
215 pendingCallbacks = [callback];
216 this._pendingSourceMapLoadingCallbacks[sourceMapURL] = pendingCallbacks;
218 WebInspector.SourceMap.load(sourceMapURL, scriptURL, sourceMapLoaded.bind(this));
221 * @param {?WebInspector.SourceMap} sourceMap
222 * @this {WebInspector.CompilerScriptMapping}
224 function sourceMapLoaded(sourceMap)
226 var url = /** @type {string} */ (sourceMapURL);
227 var callbacks = this._pendingSourceMapLoadingCallbacks[url];
228 delete this._pendingSourceMapLoadingCallbacks[url];
232 this._sourceMapForSourceMapURL[url] = sourceMap;
233 for (var i = 0; i < callbacks.length; ++i)
234 callbacks[i](sourceMap);
238 _debuggerReset: function()
241 * @param {string} sourceURL
242 * @this {WebInspector.CompilerScriptMapping}
244 function unbindUISourceCodeForURL(sourceURL)
246 var uiSourceCode = this._workspace.uiSourceCodeForURL(sourceURL);
249 this._unbindUISourceCode(uiSourceCode);
252 this._sourceMapForURL.keys().forEach(unbindUISourceCodeForURL.bind(this));
254 this._sourceMapForSourceMapURL = {};
255 this._pendingSourceMapLoadingCallbacks = {};
256 this._sourceMapForScriptId = {};
257 this._scriptForSourceMap.clear();
258 this._sourceMapForURL.clear();