Organize WebInspectorUI/UserInterface into sub-directories.
[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._pendingContentRequestCallbacks = [];
31
32     this._originalRevision = new WebInspector.SourceCodeRevision(this, null, false);
33     this._currentRevision = this._originalRevision;
34
35     this._sourceMaps = null;
36     this._formatterSourceMap = null;
37 };
38
39 WebInspector.SourceCode.Event = {
40     ContentDidChange: "source-code-content-did-change",
41     SourceMapAdded: "source-code-source-map-added",
42     FormatterDidChange: "source-code-formatter-did-change"
43 };
44
45 WebInspector.SourceCode.prototype = {
46     constructor: WebInspector.SourceCode,
47
48     // Public
49
50     get displayName()
51     {
52         // Implemented by subclasses.
53         console.error("Needs to be implemented by a subclass.");
54         return "";
55     },
56
57     get originalRevision()
58     {
59         return this._originalRevision;
60     },
61
62     get currentRevision()
63     {
64         return this._currentRevision;
65     },
66
67     set currentRevision(revision)
68     {
69         console.assert(revision instanceof WebInspector.SourceCodeRevision);
70         if (!(revision instanceof WebInspector.SourceCodeRevision))
71             return;
72
73         console.assert(revision.sourceCode === this);
74         if (revision.sourceCode !== this)
75             return;
76
77         this._currentRevision = revision;
78
79         this.dispatchEventToListeners(WebInspector.SourceCode.Event.ContentDidChange);
80     },
81
82     get content()
83     {
84         return this._currentRevision.content;
85     },
86
87     get contentIsBase64Encoded()
88     {
89         return this._currentRevision.contentIsBase64Encoded;
90     },
91
92     get sourceMaps()
93     {
94         return this._sourceMaps || [];
95     },
96
97     addSourceMap: function(sourceMap)
98     {
99         console.assert(sourceMap instanceof WebInspector.SourceMap);
100
101         if (!this._sourceMaps)
102             this._sourceMaps = [];
103
104         this._sourceMaps.push(sourceMap);
105
106         this.dispatchEventToListeners(WebInspector.SourceCode.Event.SourceMapAdded);
107     },
108
109     get formatterSourceMap()
110     {
111         return this._formatterSourceMap;
112     },
113
114     set formatterSourceMap(formatterSourceMap)
115     {
116         console.assert(this._formatterSourceMap === null || formatterSourceMap === null);
117         console.assert(formatterSourceMap === null || formatterSourceMap instanceof WebInspector.FormatterSourceMap);
118
119         this._formatterSourceMap = formatterSourceMap;
120
121         this.dispatchEventToListeners(WebInspector.SourceCode.Event.FormatterDidChange);
122     },
123
124     requestContent: function(callback)
125     {
126         console.assert(typeof callback === "function");
127         if (typeof callback !== "function")
128             return;
129
130         this._pendingContentRequestCallbacks.push(callback);
131
132         if (this._contentReceived) {
133             // Call _servicePendingContentRequests on a timeout to force callbacks to be asynchronous.
134             if (!this._servicePendingContentRequestsTimeoutIdentifier)
135                 this._servicePendingContentRequestsTimeoutIdentifier = setTimeout(this.servicePendingContentRequests.bind(this), 0);
136         } else if (this.canRequestContentFromBackend())
137             this.requestContentFromBackendIfNeeded();
138     },
139
140     createSourceCodeLocation: function(lineNumber, columnNumber)
141     {
142         return new WebInspector.SourceCodeLocation(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     canRequestContentFromBackend: function()
182     {
183         // Implemented by subclasses.
184         console.error("Needs to be implemented by a subclass.");
185         return false;
186     },
187
188     requestContentFromBackend: function(callback)
189     {
190         // Implemented by subclasses.
191         console.error("Needs to be implemented by a subclass.");
192     },
193
194     requestContentFromBackendIfNeeded: function()
195     {
196         console.assert(this.canRequestContentFromBackend());
197         if (!this.canRequestContentFromBackend())
198             return;
199
200         if (!this._pendingContentRequestCallbacks.length)
201             return;
202
203         if (this._contentRequestResponsePending)
204             return;
205
206         this._contentRequestResponsePending = true;
207
208         if (this.requestContentFromBackend(this._processContent.bind(this)))
209             return;
210
211         // Since requestContentFromBackend returned false, just call _processContent,
212         // which will cause the pending callbacks to get null content.
213         this._processContent();
214     },
215
216     servicePendingContentRequests: function(force)
217     {
218         if (this._servicePendingContentRequestsTimeoutIdentifier) {
219             clearTimeout(this._servicePendingContentRequestsTimeoutIdentifier);
220             delete this._servicePendingContentRequestsTimeoutIdentifier;
221         }
222
223         // Force the content requests to be sent. To do this correctly we also force
224         // _contentReceived to be true so future calls to requestContent go through.
225         if (force)
226             this._contentReceived = true;
227
228         console.assert(this._contentReceived);
229         if (!this._contentReceived)
230             return;
231
232         // Move the callbacks into a local and clear _pendingContentRequestCallbacks so
233         // callbacks that might call requestContent again will not modify the array.
234         var callbacks = this._pendingContentRequestCallbacks;
235         this._pendingContentRequestCallbacks = [];
236
237         for (var i = 0; i < callbacks.length; ++i)
238             callbacks[i](this, this.content, this.contentIsBase64Encoded);
239     },
240
241     // Private
242
243     _processContent: function(error, content, base64Encoded)
244     {
245         if (error)
246             console.error(error);
247
248         this._contentRequestResponsePending = false;
249         this._contentReceived = true;
250
251         var revision = this.revisionForRequestedContent;
252
253         this._ignoreRevisionContentDidChangeEvent = true;
254         revision.content = content || null;
255         revision.contentIsBase64Encoded = base64Encoded || false;
256         delete this._ignoreRevisionContentDidChangeEvent;
257
258         this.servicePendingContentRequests();
259     }
260 };
261
262 WebInspector.SourceCode.prototype.__proto__ = WebInspector.Object.prototype;