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.set(sourceMap, script);
151 var sourceURLs = sourceMap.sources();
152 var missingSources = [];
153 for (var i = 0; i < sourceURLs.length; ++i) {
154 var sourceURL = sourceURLs[i];
155 if (this._sourceMapForURL.get(sourceURL))
157 this._sourceMapForURL.set(sourceURL, sourceMap);
158 if (!this._workspace.hasMappingForURL(sourceURL) && !this._workspace.uiSourceCodeForURL(sourceURL)) {
159 var contentProvider = sourceMap.sourceContentProvider(sourceURL, WebInspector.resourceTypes.Script);
160 this._networkWorkspaceBinding.addFileForURL(sourceURL, contentProvider, script.isContentScript());
162 var uiSourceCode = this._workspace.uiSourceCodeForURL(sourceURL);
164 this._bindUISourceCode(uiSourceCode);
166 if (missingSources.length < 3)
167 missingSources.push(sourceURL);
168 else if (missingSources.peekLast() !== "\u2026")
169 missingSources.push("\u2026");
172 if (missingSources.length) {
173 WebInspector.console.warn(
174 WebInspector.UIString("Source map %s points to the files missing from the workspace: [%s]",
175 sourceMap.url(), missingSources.join(", ")));
178 this._debuggerWorkspaceBinding.updateLocations(script);
185 isIdentity: function()
191 * @param {!WebInspector.UISourceCode} uiSourceCode
192 * @param {number} lineNumber
195 uiLineHasMapping: function(uiSourceCode, lineNumber)
197 if (!uiSourceCode.url)
199 var sourceMap = this._sourceMapForURL.get(uiSourceCode.url);
202 return !!sourceMap.findEntryReversed(uiSourceCode.url, lineNumber, 0);
206 * @param {!WebInspector.UISourceCode} uiSourceCode
208 _bindUISourceCode: function(uiSourceCode)
210 this._debuggerWorkspaceBinding.setSourceMapping(this._target, uiSourceCode, this);
214 * @param {!WebInspector.UISourceCode} uiSourceCode
216 _unbindUISourceCode: function(uiSourceCode)
218 this._debuggerWorkspaceBinding.setSourceMapping(this._target, uiSourceCode, null);
222 * @param {!WebInspector.Event} event
224 _uiSourceCodeAddedToWorkspace: function(event)
226 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.data);
227 if (!uiSourceCode.url || !this._sourceMapForURL.get(uiSourceCode.url))
229 this._bindUISourceCode(uiSourceCode);
233 * @param {!WebInspector.Script} script
234 * @param {function(?WebInspector.SourceMap)} callback
236 loadSourceMapForScript: function(script, callback)
238 // script.sourceURL can be a random string, but is generally an absolute path -> complete it to inspected page url for
240 if (!script.sourceMapURL) {
244 var scriptURL = WebInspector.ParsedURL.completeURL(script.target().resourceTreeModel.inspectedPageURL(), script.sourceURL);
249 var sourceMapURL = WebInspector.ParsedURL.completeURL(scriptURL, script.sourceMapURL);
255 var sourceMap = this._sourceMapForSourceMapURL[sourceMapURL];
261 var pendingCallbacks = this._pendingSourceMapLoadingCallbacks[sourceMapURL];
262 if (pendingCallbacks) {
263 pendingCallbacks.push(callback);
267 pendingCallbacks = [callback];
268 this._pendingSourceMapLoadingCallbacks[sourceMapURL] = pendingCallbacks;
270 WebInspector.SourceMap.load(sourceMapURL, scriptURL, sourceMapLoaded.bind(this));
273 * @param {?WebInspector.SourceMap} sourceMap
274 * @this {WebInspector.CompilerScriptMapping}
276 function sourceMapLoaded(sourceMap)
278 var url = /** @type {string} */ (sourceMapURL);
279 var callbacks = this._pendingSourceMapLoadingCallbacks[url];
280 delete this._pendingSourceMapLoadingCallbacks[url];
284 this._sourceMapForSourceMapURL[url] = sourceMap;
285 for (var i = 0; i < callbacks.length; ++i)
286 callbacks[i](sourceMap);
290 _debuggerReset: function()
293 * @param {string} sourceURL
294 * @this {WebInspector.CompilerScriptMapping}
296 function unbindUISourceCodeForURL(sourceURL)
298 var uiSourceCode = this._workspace.uiSourceCodeForURL(sourceURL);
301 this._unbindUISourceCode(uiSourceCode);
304 this._sourceMapForURL.keys().forEach(unbindUISourceCodeForURL.bind(this));
306 this._sourceMapForSourceMapURL = {};
307 this._pendingSourceMapLoadingCallbacks = {};
308 this._sourceMapForScriptId = {};
309 this._scriptForSourceMap.clear();
310 this._sourceMapForURL.clear();
315 this._workspace.removeEventListener(WebInspector.Workspace.Events.UISourceCodeAdded, this._uiSourceCodeAddedToWorkspace, this);