2 * Copyright (C) 2011 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.
31 // RawSourceCode represents JavaScript resource or HTML resource with inlined scripts
32 // as it came from network.
36 * @extends {WebInspector.Object}
38 * @param {WebInspector.Script} script
39 * @param {WebInspector.Resource} resource
40 * @param {WebInspector.ScriptFormatter} formatter
41 * @param {boolean} formatted
43 WebInspector.RawSourceCode = function(id, script, resource, formatter, formatted)
46 this.url = script.sourceURL;
47 this.isContentScript = script.isContentScript;
48 this._scripts = [script];
49 this._formatter = formatter;
50 this._formatted = formatted;
51 this._resource = resource;
54 this._useTemporaryContent = this._resource && !this._resource.finished;
55 this._hasNewScripts = true;
56 if (!this._useTemporaryContent)
57 this._updateSourceMapping();
58 else if (this._resource)
59 this._resource.addEventListener("finished", this._resourceFinished.bind(this));
62 WebInspector.RawSourceCode.Events = {
63 SourceMappingUpdated: "source-mapping-updated"
66 WebInspector.RawSourceCode.prototype = {
68 * @param {WebInspector.Script} script
70 addScript: function(script)
72 this._scripts.push(script);
73 this._hasNewScripts = true;
77 * @return {WebInspector.RawSourceCode.SourceMapping}
81 return this._sourceMapping;
85 * @param {boolean} formatted
87 setFormatted: function(formatted)
89 if (this._formatted === formatted)
91 this._formatted = formatted;
92 this._updateSourceMapping();
96 * @param {WebInspector.CompilerSourceMapping} compilerSourceMapping
98 setCompilerSourceMapping: function(compilerSourceMapping)
100 if (compilerSourceMapping)
101 this._useTemporaryContent = false;
102 this._compilerSourceMapping = compilerSourceMapping;
103 this._updateSourceMapping();
106 _resourceFinished: function()
108 if (this._compilerSourceMapping)
110 this._useTemporaryContent = false;
111 this._updateSourceMapping();
115 * @param {number} lineNumber
116 * @param {number} columnNumber
117 * @return {WebInspector.Script}
119 _scriptForRawLocation: function(lineNumber, columnNumber)
121 var closestScript = this._scripts[0];
122 for (var i = 1; i < this._scripts.length; ++i) {
123 var script = this._scripts[i];
124 if (script.lineOffset > lineNumber || (script.lineOffset === lineNumber && script.columnOffset > columnNumber))
126 if (script.lineOffset > closestScript.lineOffset ||
127 (script.lineOffset === closestScript.lineOffset && script.columnOffset > closestScript.columnOffset))
128 closestScript = script;
130 return closestScript;
134 * @param {WebInspector.Script} script
136 forceUpdateSourceMapping: function(script)
138 if (!this._useTemporaryContent || !this._hasNewScripts)
140 this._hasNewScripts = false;
141 this._updateSourceMapping();
144 _updateSourceMapping: function()
146 if (this._updatingSourceMapping) {
147 this._updateNeeded = true;
150 this._updatingSourceMapping = true;
151 this._updateNeeded = false;
153 this._createSourceMapping(didCreateSourceMapping.bind(this));
156 * @this {WebInspector.RawSourceCode}
157 * @param {WebInspector.RawSourceCode.SourceMapping} sourceMapping
159 function didCreateSourceMapping(sourceMapping)
161 this._updatingSourceMapping = false;
164 if (!this._updateNeeded)
165 this._saveSourceMapping(sourceMapping);
167 this._updateSourceMapping();
171 _createContentProvider: function()
173 if (this._resource && this._resource.finished)
174 return new WebInspector.ResourceContentProvider(this._resource);
175 if (this._scripts.length === 1 && !this._scripts[0].lineOffset && !this._scripts[0].columnOffset)
176 return new WebInspector.ScriptContentProvider(this._scripts[0]);
177 return new WebInspector.ConcatenatedScriptsContentProvider(this._scripts);
181 * @param {function(WebInspector.RawSourceCode.SourceMapping)} callback
183 _createSourceMapping: function(callback)
185 if (this._compilerSourceMapping) {
186 var success = this._compilerSourceMapping.load();
188 delete this._compilerSourceMapping;
192 var uiSourceCodeList = [];
193 var sourceURLs = this._compilerSourceMapping.sources();
194 for (var i = 0; i < sourceURLs.length; ++i) {
195 var sourceURL = sourceURLs[i];
196 var contentProvider = new WebInspector.CompilerSourceMappingContentProvider(sourceURL, this._compilerSourceMapping);
197 var uiSourceCode = new WebInspector.UISourceCode(sourceURL, sourceURL, this.isContentScript, this, contentProvider);
198 uiSourceCodeList.push(uiSourceCode);
200 var sourceMapping = new WebInspector.RawSourceCode.CompilerSourceMapping(this, uiSourceCodeList, this._compilerSourceMapping);
201 callback(sourceMapping);
205 var originalContentProvider = this._createContentProvider();
206 if (!this._formatted) {
207 var uiSourceCode = new WebInspector.UISourceCode(this.url, this.url, this.isContentScript, this, originalContentProvider);
208 var sourceMapping = new WebInspector.RawSourceCode.PlainSourceMapping(this, uiSourceCode);
209 callback(sourceMapping);
214 * @this {WebInspector.RawSourceCode}
215 * @param {string} mimeType
216 * @param {string} content
218 function didRequestContent(mimeType, content)
221 * @this {WebInspector.RawSourceCode}
222 * @param {string} formattedContent
223 * @param {WebInspector.FormattedSourceMapping} mapping
225 function didFormatContent(formattedContent, mapping)
227 var contentProvider = new WebInspector.StaticContentProvider(mimeType, formattedContent)
228 var uiSourceCode = new WebInspector.UISourceCode("deobfuscated:" + this.url, this.url, this.isContentScript, this, contentProvider);
229 var sourceMapping = new WebInspector.RawSourceCode.FormattedSourceMapping(this, uiSourceCode, mapping);
230 callback(sourceMapping);
232 this._formatter.formatContent(mimeType, content, didFormatContent.bind(this));
234 originalContentProvider.requestContent(didRequestContent.bind(this));
238 * @param {WebInspector.RawSourceCode.SourceMapping} sourceMapping
240 _saveSourceMapping: function(sourceMapping)
242 var oldSourceMapping;
243 if (this._sourceMapping)
244 oldSourceMapping = this._sourceMapping;
245 this._sourceMapping = sourceMapping;
246 this.dispatchEventToListeners(WebInspector.RawSourceCode.Events.SourceMappingUpdated, { oldSourceMapping: oldSourceMapping });
250 WebInspector.RawSourceCode.prototype.__proto__ = WebInspector.Object.prototype;
255 WebInspector.RawSourceCode.SourceMapping = function()
259 WebInspector.RawSourceCode.SourceMapping.prototype = {
261 * @param {DebuggerAgent.Location} rawLocation
262 * @return {WebInspector.UILocation}
264 rawLocationToUILocation: function(rawLocation) { },
267 * @param {WebInspector.UISourceCode} uiSourceCode
268 * @param {number} lineNumber
269 * @param {number} columnNumber
270 * @return {DebuggerAgent.Location}
272 uiLocationToRawLocation: function(uiSourceCode, lineNumber, columnNumber) { }
277 * @implements {WebInspector.RawSourceCode.SourceMapping}
278 * @param {WebInspector.RawSourceCode} rawSourceCode
279 * @param {WebInspector.UISourceCode} uiSourceCode
281 WebInspector.RawSourceCode.PlainSourceMapping = function(rawSourceCode, uiSourceCode)
283 this._rawSourceCode = rawSourceCode;
284 this._uiSourceCodeList = [uiSourceCode];
287 WebInspector.RawSourceCode.PlainSourceMapping.prototype = {
289 * @param {DebuggerAgent.Location} rawLocation
290 * @return {WebInspector.UILocation}
292 rawLocationToUILocation: function(rawLocation)
294 return new WebInspector.UILocation(this._uiSourceCodeList[0], rawLocation.lineNumber, rawLocation.columnNumber || 0);
298 * @param {WebInspector.UISourceCode} uiSourceCode
299 * @param {number} lineNumber
300 * @param {number} columnNumber
301 * @return {DebuggerAgent.Location}
303 uiLocationToRawLocation: function(uiSourceCode, lineNumber, columnNumber)
305 console.assert(uiSourceCode === this._uiSourceCodeList[0]);
306 var rawLocation = { lineNumber: lineNumber, columnNumber: columnNumber };
307 rawLocation.scriptId = this._rawSourceCode._scriptForRawLocation(rawLocation.lineNumber, rawLocation.columnNumber).scriptId;
308 return /** @type {DebuggerAgent.Location} */ rawLocation;
312 * @return {Array.<WebInspector.UISourceCode>}
314 uiSourceCodeList: function()
316 return this._uiSourceCodeList;
322 * @implements {WebInspector.RawSourceCode.SourceMapping}
323 * @param {WebInspector.RawSourceCode} rawSourceCode
324 * @param {WebInspector.UISourceCode} uiSourceCode
325 * @param {WebInspector.FormattedSourceMapping} mapping
327 WebInspector.RawSourceCode.FormattedSourceMapping = function(rawSourceCode, uiSourceCode, mapping)
329 this._rawSourceCode = rawSourceCode;
330 this._uiSourceCodeList = [uiSourceCode];
331 this._mapping = mapping;
334 WebInspector.RawSourceCode.FormattedSourceMapping.prototype = {
336 * @param {DebuggerAgent.Location} rawLocation
338 rawLocationToUILocation: function(rawLocation)
340 var location = this._mapping.originalToFormatted(rawLocation);
341 return new WebInspector.UILocation(this._uiSourceCodeList[0], location.lineNumber, location.columnNumber || 0);
345 * @param {WebInspector.UISourceCode} uiSourceCode
346 * @param {number} lineNumber
347 * @param {number} columnNumber
348 * @return {DebuggerAgent.Location}
350 uiLocationToRawLocation: function(uiSourceCode, lineNumber, columnNumber)
352 console.assert(uiSourceCode === this._uiSourceCodeList[0]);
353 var rawLocation = this._mapping.formattedToOriginal({ lineNumber: lineNumber, columnNumber: columnNumber });
354 rawLocation.scriptId = this._rawSourceCode._scriptForRawLocation(rawLocation.lineNumber, rawLocation.columnNumber).scriptId;
359 * @return {Array.<WebInspector.UISourceCode>}
361 uiSourceCodeList: function()
363 return this._uiSourceCodeList;
369 * @implements {WebInspector.RawSourceCode.SourceMapping}
370 * @param {WebInspector.RawSourceCode} rawSourceCode
371 * @param {Array.<WebInspector.UISourceCode>} uiSourceCodeList
372 * @param {WebInspector.CompilerSourceMapping} mapping
374 WebInspector.RawSourceCode.CompilerSourceMapping = function(rawSourceCode, uiSourceCodeList, mapping)
376 this._rawSourceCode = rawSourceCode;
377 this._uiSourceCodeList = uiSourceCodeList;
378 this._mapping = mapping;
379 this._uiSourceCodeByURL = {};
380 for (var i = 0; i < uiSourceCodeList.length; ++i)
381 this._uiSourceCodeByURL[uiSourceCodeList[i].url] = uiSourceCodeList[i];
384 WebInspector.RawSourceCode.CompilerSourceMapping.prototype = {
386 * @param {DebuggerAgent.Location} rawLocation
388 rawLocationToUILocation: function(rawLocation)
390 var location = this._mapping.compiledLocationToSourceLocation(rawLocation.lineNumber, rawLocation.columnNumber || 0);
391 var uiSourceCode = this._uiSourceCodeByURL[location.sourceURL];
392 return new WebInspector.UILocation(uiSourceCode, location.lineNumber, location.columnNumber);
396 * @param {WebInspector.UISourceCode} uiSourceCode
397 * @param {number} lineNumber
398 * @param {number} columnNumber
399 * @return {DebuggerAgent.Location}
401 uiLocationToRawLocation: function(uiSourceCode, lineNumber, columnNumber)
403 var rawLocation = this._mapping.sourceLocationToCompiledLocation(uiSourceCode.url, lineNumber);
404 rawLocation.scriptId = this._rawSourceCode._scriptForRawLocation(rawLocation.lineNumber, rawLocation.columnNumber).scriptId;
405 return /** @type {DebuggerAgent.Location} */ rawLocation;
409 * @return {Array.<WebInspector.UISourceCode>}
411 uiSourceCodeList: function()
413 return this._uiSourceCodeList;
419 * @param {WebInspector.UISourceCode} uiSourceCode
420 * @param {number} lineNumber
421 * @param {number} columnNumber
423 WebInspector.UILocation = function(uiSourceCode, lineNumber, columnNumber)
425 this.uiSourceCode = uiSourceCode;
426 this.lineNumber = lineNumber;
427 this.columnNumber = columnNumber;