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.DebuggerSourceMapping}
34 * @param {!WebInspector.DebuggerModel} debuggerModel
35 * @param {!WebInspector.Workspace} workspace
36 * @param {!WebInspector.NetworkWorkspaceBinding} networkWorkspaceBinding
37 * @param {!WebInspector.DebuggerWorkspaceBinding} debuggerWorkspaceBinding
39 WebInspector.CompilerScriptMapping = function(debuggerModel, workspace, networkWorkspaceBinding, debuggerWorkspaceBinding)
41 this._target = debuggerModel.target();
42 this._debuggerModel = debuggerModel;
43 this._workspace = workspace;
44 this._workspace.addEventListener(WebInspector.Workspace.Events.UISourceCodeAdded, this._uiSourceCodeAddedToWorkspace, this);
45 this._networkWorkspaceBinding = networkWorkspaceBinding;
46 this._debuggerWorkspaceBinding = debuggerWorkspaceBinding;
48 /** @type {!Object.<string, !WebInspector.SourceMap>} */
49 this._sourceMapForSourceMapURL = {};
50 /** @type {!Object.<string, !Array.<function(?WebInspector.SourceMap)>>} */
51 this._pendingSourceMapLoadingCallbacks = {};
52 /** @type {!Object.<string, !WebInspector.SourceMap>} */
53 this._sourceMapForScriptId = {};
54 /** @type {!Map.<!WebInspector.SourceMap, !WebInspector.Script>} */
55 this._scriptForSourceMap = new Map();
56 /** @type {!StringMap.<!WebInspector.SourceMap>} */
57 this._sourceMapForURL = new StringMap();
58 debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.GlobalObjectCleared, this._debuggerReset, this);
61 WebInspector.CompilerScriptMapping.prototype = {
63 * @param {!WebInspector.DebuggerModel.Location} rawLocation
64 * @return {?WebInspector.UILocation}
66 rawLocationToUILocation: function(rawLocation)
68 var debuggerModelLocation = /** @type {!WebInspector.DebuggerModel.Location} */ (rawLocation);
69 var sourceMap = this._sourceMapForScriptId[debuggerModelLocation.scriptId];
72 var lineNumber = debuggerModelLocation.lineNumber;
73 var columnNumber = debuggerModelLocation.columnNumber || 0;
74 var entry = sourceMap.findEntry(lineNumber, columnNumber);
75 if (!entry || entry.length === 2)
77 var url = /** @type {string} */ (entry[2]);
78 var uiSourceCode = this._workspace.uiSourceCodeForURL(url);
81 return uiSourceCode.uiLocation(/** @type {number} */ (entry[3]), /** @type {number} */ (entry[4]));
85 * @param {!WebInspector.UISourceCode} uiSourceCode
86 * @param {number} lineNumber
87 * @param {number} columnNumber
88 * @return {?WebInspector.DebuggerModel.Location}
90 uiLocationToRawLocation: function(uiSourceCode, lineNumber, columnNumber)
92 if (!uiSourceCode.url)
94 var sourceMap = this._sourceMapForURL.get(uiSourceCode.url);
97 var script = /** @type {!WebInspector.Script} */ (this._scriptForSourceMap.get(sourceMap));
98 console.assert(script);
99 var mappingSearchLinesCount = 5;
100 // We do not require precise (breakpoint) location but limit the number of lines to search or mapping.
101 var entry = sourceMap.findEntryReversed(uiSourceCode.url, lineNumber, mappingSearchLinesCount);
104 return this._debuggerModel.createRawLocation(script, /** @type {number} */ (entry[0]), /** @type {number} */ (entry[1]));
108 * @param {!WebInspector.Script} script
110 addScript: function(script)
112 this._debuggerWorkspaceBinding.pushSourceMapping(script, this);
113 script.addEventListener(WebInspector.Script.Events.SourceMapURLAdded, this._sourceMapURLAdded.bind(this));
114 this._processScript(script);
118 * @param {!WebInspector.Event} event
120 _sourceMapURLAdded: function(event)
122 var script = /** @type {!WebInspector.Script} */ (event.target);
123 this._processScript(script);
127 * @param {!WebInspector.Script} script
129 _processScript: function(script)
131 this.loadSourceMapForScript(script, sourceMapLoaded.bind(this));
134 * @param {?WebInspector.SourceMap} sourceMap
135 * @this {WebInspector.CompilerScriptMapping}
137 function sourceMapLoaded(sourceMap)
142 if (this._scriptForSourceMap.get(sourceMap)) {
143 this._sourceMapForScriptId[script.scriptId] = sourceMap;
144 this._debuggerWorkspaceBinding.updateLocations(script);
148 this._sourceMapForScriptId[script.scriptId] = sourceMap;
149 this._scriptForSourceMap.put(sourceMap, script);
151 var sourceURLs = sourceMap.sources();
152 for (var i = 0; i < sourceURLs.length; ++i) {
153 var sourceURL = sourceURLs[i];
154 if (this._sourceMapForURL.get(sourceURL))
156 this._sourceMapForURL.put(sourceURL, sourceMap);
157 if (!this._workspace.hasMappingForURL(sourceURL) && !this._workspace.uiSourceCodeForURL(sourceURL)) {
158 var contentProvider = sourceMap.sourceContentProvider(sourceURL, WebInspector.resourceTypes.Script);
159 this._networkWorkspaceBinding.addFileForURL(sourceURL, contentProvider, script.isContentScript());
161 var uiSourceCode = this._workspace.uiSourceCodeForURL(sourceURL);
163 this._bindUISourceCode(uiSourceCode);
165 WebInspector.console.error(WebInspector.UIString("Failed to locate workspace file mapped to URL %s from source map %s", sourceURL, sourceMap.url()));
167 this._debuggerWorkspaceBinding.updateLocations(script);
174 isIdentity: function()
180 * @param {!WebInspector.UISourceCode} uiSourceCode
181 * @param {number} lineNumber
184 uiLineHasMapping: function(uiSourceCode, lineNumber)
186 if (!uiSourceCode.url)
188 var sourceMap = this._sourceMapForURL.get(uiSourceCode.url);
191 return !!sourceMap.findEntryReversed(uiSourceCode.url, lineNumber, 0);
195 * @param {!WebInspector.UISourceCode} uiSourceCode
197 _bindUISourceCode: function(uiSourceCode)
199 this._debuggerWorkspaceBinding.setSourceMapping(this._target, uiSourceCode, this);
203 * @param {!WebInspector.UISourceCode} uiSourceCode
205 _unbindUISourceCode: function(uiSourceCode)
207 this._debuggerWorkspaceBinding.setSourceMapping(this._target, uiSourceCode, null);
211 * @param {!WebInspector.Event} event
213 _uiSourceCodeAddedToWorkspace: function(event)
215 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.data);
216 if (!uiSourceCode.url || !this._sourceMapForURL.get(uiSourceCode.url))
218 this._bindUISourceCode(uiSourceCode);
222 * @param {!WebInspector.Script} script
223 * @param {function(?WebInspector.SourceMap)} callback
225 loadSourceMapForScript: function(script, callback)
227 // script.sourceURL can be a random string, but is generally an absolute path -> complete it to inspected page url for
229 if (!script.sourceMapURL) {
233 var scriptURL = WebInspector.ParsedURL.completeURL(script.target().resourceTreeModel.inspectedPageURL(), script.sourceURL);
238 var sourceMapURL = WebInspector.ParsedURL.completeURL(scriptURL, script.sourceMapURL);
244 var sourceMap = this._sourceMapForSourceMapURL[sourceMapURL];
250 var pendingCallbacks = this._pendingSourceMapLoadingCallbacks[sourceMapURL];
251 if (pendingCallbacks) {
252 pendingCallbacks.push(callback);
256 pendingCallbacks = [callback];
257 this._pendingSourceMapLoadingCallbacks[sourceMapURL] = pendingCallbacks;
259 WebInspector.SourceMap.load(sourceMapURL, scriptURL, sourceMapLoaded.bind(this));
262 * @param {?WebInspector.SourceMap} sourceMap
263 * @this {WebInspector.CompilerScriptMapping}
265 function sourceMapLoaded(sourceMap)
267 var url = /** @type {string} */ (sourceMapURL);
268 var callbacks = this._pendingSourceMapLoadingCallbacks[url];
269 delete this._pendingSourceMapLoadingCallbacks[url];
273 this._sourceMapForSourceMapURL[url] = sourceMap;
274 for (var i = 0; i < callbacks.length; ++i)
275 callbacks[i](sourceMap);
279 _debuggerReset: function()
282 * @param {string} sourceURL
283 * @this {WebInspector.CompilerScriptMapping}
285 function unbindUISourceCodeForURL(sourceURL)
287 var uiSourceCode = this._workspace.uiSourceCodeForURL(sourceURL);
290 this._unbindUISourceCode(uiSourceCode);
293 this._sourceMapForURL.keys().forEach(unbindUISourceCodeForURL.bind(this));
295 this._sourceMapForSourceMapURL = {};
296 this._pendingSourceMapLoadingCallbacks = {};
297 this._sourceMapForScriptId = {};
298 this._scriptForSourceMap.clear();
299 this._sourceMapForURL.clear();
304 this._workspace.removeEventListener(WebInspector.Workspace.Events.UISourceCodeAdded, this._uiSourceCodeAddedToWorkspace, this);