2 * Copyright (C) 2013 Apple 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
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
26 WebInspector.SourceCode = function()
28 WebInspector.Object.call(this);
30 this._originalRevision = new WebInspector.SourceCodeRevision(this, null, false);
31 this._currentRevision = this._originalRevision;
33 this._sourceMaps = null;
34 this._formatterSourceMap = null;
35 this._requestContentPromise = null;
38 WebInspector.Object.addConstructorFunctions(WebInspector.SourceCode);
40 WebInspector.SourceCode.Event = {
41 ContentDidChange: "source-code-content-did-change",
42 SourceMapAdded: "source-code-source-map-added",
43 FormatterDidChange: "source-code-formatter-did-change",
44 LoadingDidFinish: "source-code-loading-did-finish",
45 LoadingDidFail: "source-code-loading-did-fail"
48 WebInspector.SourceCode.prototype = {
49 constructor: WebInspector.SourceCode,
55 // Implemented by subclasses.
56 console.error("Needs to be implemented by a subclass.");
60 get originalRevision()
62 return this._originalRevision;
67 return this._currentRevision;
70 set currentRevision(revision)
72 console.assert(revision instanceof WebInspector.SourceCodeRevision);
73 if (!(revision instanceof WebInspector.SourceCodeRevision))
76 console.assert(revision.sourceCode === this);
77 if (revision.sourceCode !== this)
80 this._currentRevision = revision;
82 this.dispatchEventToListeners(WebInspector.SourceCode.Event.ContentDidChange);
87 return this._currentRevision.content;
90 get contentIsBase64Encoded()
92 return this._currentRevision.contentIsBase64Encoded;
97 return this._sourceMaps || [];
100 addSourceMap: function(sourceMap)
102 console.assert(sourceMap instanceof WebInspector.SourceMap);
104 if (!this._sourceMaps)
105 this._sourceMaps = [];
107 this._sourceMaps.push(sourceMap);
109 this.dispatchEventToListeners(WebInspector.SourceCode.Event.SourceMapAdded);
112 get formatterSourceMap()
114 return this._formatterSourceMap;
117 set formatterSourceMap(formatterSourceMap)
119 console.assert(this._formatterSourceMap === null || formatterSourceMap === null);
120 console.assert(formatterSourceMap === null || formatterSourceMap instanceof WebInspector.FormatterSourceMap);
122 this._formatterSourceMap = formatterSourceMap;
124 this.dispatchEventToListeners(WebInspector.SourceCode.Event.FormatterDidChange);
127 requestContent: function()
129 this._requestContentPromise = this._requestContentPromise || this.requestContentFromBackend().then(this._processContent.bind(this));
131 return this._requestContentPromise;
134 createSourceCodeLocation: function(lineNumber, columnNumber)
136 return new WebInspector.SourceCodeLocation(this, lineNumber, columnNumber);
139 createLazySourceCodeLocation: function(lineNumber, columnNumber)
141 return new WebInspector.LazySourceCodeLocation(this, lineNumber, columnNumber);
144 createSourceCodeTextRange: function(textRange)
146 return new WebInspector.SourceCodeTextRange(this, textRange);
151 revisionContentDidChange: function(revision)
153 if (this._ignoreRevisionContentDidChangeEvent)
156 if (revision !== this._currentRevision)
159 this.handleCurrentRevisionContentChange();
161 this.dispatchEventToListeners(WebInspector.SourceCode.Event.ContentDidChange);
164 handleCurrentRevisionContentChange: function()
166 // Implemented by subclasses if needed.
169 get revisionForRequestedContent()
171 // Implemented by subclasses if needed.
172 return this._originalRevision;
175 markContentAsStale: function()
177 this._contentReceived = false;
180 requestContentFromBackend: function()
182 // Implemented by subclasses.
183 console.error("Needs to be implemented by a subclass.");
184 return Promise.reject(new Error("Needs to be implemented by a subclass."));
189 _processContent: function(parameters)
191 // Different backend APIs return one of `content, `body`, or `scriptSource`.
192 var content = parameters.content || parameters.body || parameters.scriptSource;
193 var base64Encoded = parameters.base64Encoded;
195 var revision = this.revisionForRequestedContent;
197 this._ignoreRevisionContentDidChangeEvent = true;
198 revision.content = content || null;
199 revision.contentIsBase64Encoded = base64Encoded || false;
200 delete this._ignoreRevisionContentDidChangeEvent;
202 return Promise.resolve({
205 base64Encoded: base64Encoded
210 WebInspector.SourceCode.prototype.__proto__ = WebInspector.Object.prototype;