Web Inspector: ResourceContentView.js incorrectly contains call to WebInspector.UIStr...
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Models / SourceCode.js
1 /*
2  * Copyright (C) 2013 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25
26 WebInspector.SourceCode = function()
27 {
28     WebInspector.Object.call(this);
29
30     this._originalRevision = new WebInspector.SourceCodeRevision(this, null, false);
31     this._currentRevision = this._originalRevision;
32
33     this._sourceMaps = null;
34     this._formatterSourceMap = null;
35     this._requestContentPromise = null;
36 };
37
38 WebInspector.Object.addConstructorFunctions(WebInspector.SourceCode);
39
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"
46 };
47
48 WebInspector.SourceCode.prototype = {
49     constructor: WebInspector.SourceCode,
50     __proto__: WebInspector.Object.prototype,
51
52     // Public
53
54     get displayName()
55     {
56         // Implemented by subclasses.
57         console.error("Needs to be implemented by a subclass.");
58         return "";
59     },
60
61     get originalRevision()
62     {
63         return this._originalRevision;
64     },
65
66     get currentRevision()
67     {
68         return this._currentRevision;
69     },
70
71     set currentRevision(revision)
72     {
73         console.assert(revision instanceof WebInspector.SourceCodeRevision);
74         if (!(revision instanceof WebInspector.SourceCodeRevision))
75             return;
76
77         console.assert(revision.sourceCode === this);
78         if (revision.sourceCode !== this)
79             return;
80
81         this._currentRevision = revision;
82
83         this.dispatchEventToListeners(WebInspector.SourceCode.Event.ContentDidChange);
84     },
85
86     get content()
87     {
88         return this._currentRevision.content;
89     },
90
91     get contentIsBase64Encoded()
92     {
93         return this._currentRevision.contentIsBase64Encoded;
94     },
95
96     get sourceMaps()
97     {
98         return this._sourceMaps || [];
99     },
100
101     addSourceMap: function(sourceMap)
102     {
103         console.assert(sourceMap instanceof WebInspector.SourceMap);
104
105         if (!this._sourceMaps)
106             this._sourceMaps = [];
107
108         this._sourceMaps.push(sourceMap);
109
110         this.dispatchEventToListeners(WebInspector.SourceCode.Event.SourceMapAdded);
111     },
112
113     get formatterSourceMap()
114     {
115         return this._formatterSourceMap;
116     },
117
118     set formatterSourceMap(formatterSourceMap)
119     {
120         console.assert(this._formatterSourceMap === null || formatterSourceMap === null);
121         console.assert(formatterSourceMap === null || formatterSourceMap instanceof WebInspector.FormatterSourceMap);
122
123         this._formatterSourceMap = formatterSourceMap;
124
125         this.dispatchEventToListeners(WebInspector.SourceCode.Event.FormatterDidChange);
126     },
127
128     requestContent: function()
129     {
130         this._requestContentPromise = this._requestContentPromise || this.requestContentFromBackend().then(this._processContent.bind(this));
131
132         return this._requestContentPromise;
133     },
134
135     createSourceCodeLocation: function(lineNumber, columnNumber)
136     {
137         return new WebInspector.SourceCodeLocation(this, lineNumber, columnNumber);
138     },
139
140     createLazySourceCodeLocation: function(lineNumber, columnNumber)
141     {
142         return new WebInspector.LazySourceCodeLocation(this, lineNumber, columnNumber);
143     },
144
145     createSourceCodeTextRange: function(textRange)
146     {
147         return new WebInspector.SourceCodeTextRange(this, textRange);
148     },
149
150     // Protected
151
152     revisionContentDidChange: function(revision)
153     {
154         if (this._ignoreRevisionContentDidChangeEvent)
155             return;
156
157         if (revision !== this._currentRevision)
158             return;
159
160         this.handleCurrentRevisionContentChange();
161
162         this.dispatchEventToListeners(WebInspector.SourceCode.Event.ContentDidChange);
163     },
164
165     handleCurrentRevisionContentChange: function()
166     {
167         // Implemented by subclasses if needed.
168     },
169
170     get revisionForRequestedContent()
171     {
172         // Implemented by subclasses if needed.
173         return this._originalRevision;
174     },
175
176     markContentAsStale: function()
177     {
178         this._contentReceived = false;
179     },
180
181     requestContentFromBackend: function()
182     {
183         // Implemented by subclasses.
184         console.error("Needs to be implemented by a subclass.");
185         return Promise.reject(new Error("Needs to be implemented by a subclass."));
186     },
187
188     // Private
189
190     _processContent: function(parameters)
191     {
192         // Different backend APIs return one of `content, `body`, or `scriptSource`.
193         var content = parameters.content || parameters.body || parameters.scriptSource;
194         var base64Encoded = parameters.base64Encoded;
195
196         var revision = this.revisionForRequestedContent;
197
198         this._ignoreRevisionContentDidChangeEvent = true;
199         revision.content = content || null;
200         revision.contentIsBase64Encoded = base64Encoded || false;
201         delete this._ignoreRevisionContentDidChangeEvent;
202
203         return Promise.resolve({
204             sourceCode: this,
205             content: content,
206             base64Encoded: base64Encoded
207         });
208     }
209 };