Web Inspector: Make closing ContentViews more leak proof
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Views / TextResourceContentView.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.TextResourceContentView = function(resource)
27 {
28     WebInspector.ResourceContentView.call(this, resource, WebInspector.TextResourceContentView.StyleClassName);
29
30     resource.addEventListener(WebInspector.SourceCode.Event.ContentDidChange, this._sourceCodeContentDidChange, this);
31
32     this._textEditor = new WebInspector.SourceCodeTextEditor(resource);
33     this._textEditor.addEventListener(WebInspector.TextEditor.Event.ExecutionLineNumberDidChange, this._executionLineNumberDidChange, this);
34     this._textEditor.addEventListener(WebInspector.TextEditor.Event.NumberOfSearchResultsDidChange, this._numberOfSearchResultsDidChange, this);
35     this._textEditor.addEventListener(WebInspector.TextEditor.Event.ContentDidChange, this._textEditorContentDidChange, this);
36     this._textEditor.addEventListener(WebInspector.TextEditor.Event.FormattingDidChange, this._textEditorFormattingDidChange, this);
37     this._textEditor.addEventListener(WebInspector.SourceCodeTextEditor.Event.ContentWillPopulate, this._contentWillPopulate, this);
38     this._textEditor.addEventListener(WebInspector.SourceCodeTextEditor.Event.ContentDidPopulate, this._contentDidPopulate, this);
39
40     WebInspector.probeManager.addEventListener(WebInspector.ProbeManager.Event.ProbeSetAdded, this._probeSetsChanged, this);
41     WebInspector.probeManager.addEventListener(WebInspector.ProbeManager.Event.ProbeSetRemoved, this._probeSetsChanged, this);
42
43     var curleyBracesImage;
44     if (WebInspector.Platform.isLegacyMacOS)
45         curleyBracesImage = {src: "Images/Legacy/NavigationItemCurleyBraces.svg", width: 16, height: 16};
46     else
47         curleyBracesImage = {src: "Images/NavigationItemCurleyBraces.svg", width: 13, height: 13};
48
49     var toolTip = WebInspector.UIString("Pretty print");
50     var activatedToolTip = WebInspector.UIString("Original formatting");
51     this._prettyPrintButtonNavigationItem = new WebInspector.ActivateButtonNavigationItem("pretty-print", toolTip, activatedToolTip, curleyBracesImage.src, curleyBracesImage.width, curleyBracesImage.height);
52     this._prettyPrintButtonNavigationItem.addEventListener(WebInspector.ButtonNavigationItem.Event.Clicked, this._togglePrettyPrint, this);
53     this._prettyPrintButtonNavigationItem.enabled = false; // Enabled when the text editor is populated with content.
54
55     var toolTipTypes = WebInspector.UIString("Show type information");
56     var activatedToolTipTypes = WebInspector.UIString("Hide type information");
57     this._showTypesButtonNavigationItem = new WebInspector.ActivateButtonNavigationItem("show-types", toolTipTypes, activatedToolTipTypes, "Images/NavigationItemTypes.svg", 13, 14);
58     this._showTypesButtonNavigationItem.addEventListener(WebInspector.ButtonNavigationItem.Event.Clicked, this._toggleTypeAnnotations, this);
59     this._showTypesButtonNavigationItem.enabled = false;
60
61     WebInspector.showJavaScriptTypeInformationSetting.addEventListener(WebInspector.Setting.Event.Changed, this._showJavaScriptTypeInformationSettingChanged, this);
62 };
63
64 WebInspector.TextResourceContentView.StyleClassName = "text";
65
66 WebInspector.TextResourceContentView.prototype = {
67     constructor: WebInspector.TextResourceContentView,
68
69     // Public
70
71     get navigationItems()
72     {
73         return [this._prettyPrintButtonNavigationItem, this._showTypesButtonNavigationItem];
74     },
75
76     get managesOwnIssues()
77     {
78         // SourceCodeTextEditor manages the issues, we don't need ResourceContentView doing it.
79         return true;
80     },
81
82     get textEditor()
83     {
84         return this._textEditor;
85     },
86
87     get supplementalRepresentedObjects()
88     {
89         var objects = WebInspector.probeManager.probeSets.filter(function(probeSet) {
90             return this._resource.url === probeSet.breakpoint.url;
91         }, this);
92
93         // If the SourceCodeTextEditor has an executionLineNumber, we can assume
94         // it is always the active call frame.
95         if (!isNaN(this._textEditor.executionLineNumber))
96             objects.push(WebInspector.debuggerManager.activeCallFrame);
97
98         return objects;
99     },
100
101     revealPosition: function(position, textRangeToSelect, forceUnformatted)
102     {
103         this._textEditor.revealPosition(position, textRangeToSelect, forceUnformatted);
104     },
105
106     shown: function()
107     {
108         WebInspector.ResourceContentView.prototype.shown.call(this);
109
110         this._textEditor.shown();
111     },
112
113     hidden: function()
114     {
115         WebInspector.ResourceContentView.prototype.hidden.call(this);
116
117         this._textEditor.hidden();
118     },
119
120     closed: function()
121     {
122         WebInspector.ResourceContentView.prototype.closed.call(this);
123
124         this.resource.removeEventListener(null, null, this);
125
126         this._textEditor.close();
127     },
128
129     get supportsSave()
130     {
131         return true;
132     },
133
134     get saveData()
135     {
136         return {url: this.resource.url, content: this._textEditor.string};
137     },
138
139     get supportsSearch()
140     {
141         return true;
142     },
143
144     get numberOfSearchResults()
145     {
146         return this._textEditor.numberOfSearchResults;
147     },
148
149     get hasPerformedSearch()
150     {
151         return this._textEditor.currentSearchQuery !== null;
152     },
153
154     set automaticallyRevealFirstSearchResult(reveal)
155     {
156         this._textEditor.automaticallyRevealFirstSearchResult = reveal;
157     },
158
159     performSearch: function(query)
160     {
161         this._textEditor.performSearch(query);
162     },
163
164     searchCleared: function()
165     {
166         this._textEditor.searchCleared();
167     },
168
169     searchQueryWithSelection: function()
170     {
171         return this._textEditor.searchQueryWithSelection();
172     },
173
174     revealPreviousSearchResult: function(changeFocus)
175     {
176         this._textEditor.revealPreviousSearchResult(changeFocus);
177     },
178
179     revealNextSearchResult: function(changeFocus)
180     {
181         this._textEditor.revealNextSearchResult(changeFocus);
182     },
183
184     updateLayout: function()
185     {
186         this._textEditor.updateLayout();
187     },
188
189     // Private
190
191     _contentWillPopulate: function(event)
192     {
193         if (this._textEditor.element.parentNode === this.element)
194             return;
195
196         // Check the MIME-type for CSS since Resource.Type.Stylesheet also includes XSL, which we can't edit yet.
197         if (this.resource.type === WebInspector.Resource.Type.Stylesheet && this.resource.syntheticMIMEType === "text/css")
198             this._textEditor.readOnly = false;
199
200         // Allow editing any local file since edits can be saved and reloaded right from the Inspector.
201         if (this.resource.urlComponents.scheme === "file")
202             this._textEditor.readOnly = false;
203
204         this.element.removeChildren();
205         this.element.appendChild(this._textEditor.element);
206     },
207
208     _contentDidPopulate: function(event)
209     {
210         this._prettyPrintButtonNavigationItem.enabled = this._textEditor.canBeFormatted();
211         this._showTypesButtonNavigationItem.enabled = this._textEditor.canShowTypeAnnotations();
212         this._showTypesButtonNavigationItem.activated = WebInspector.showJavaScriptTypeInformationSetting.value;
213     },
214
215     _togglePrettyPrint: function(event)
216     {
217         var activated = !this._prettyPrintButtonNavigationItem.activated;
218         this._textEditor.formatted = activated;
219     },
220
221     _toggleTypeAnnotations: function(event)
222     {
223         this._textEditor.toggleTypeAnnotations();
224     },
225
226     _showJavaScriptTypeInformationSettingChanged: function(event)
227     {
228         this._showTypesButtonNavigationItem.activated = WebInspector.showJavaScriptTypeInformationSetting.value;
229     },
230
231     _textEditorFormattingDidChange: function(event)
232     {
233         this._prettyPrintButtonNavigationItem.activated = this._textEditor.formatted;
234     },
235
236     _sourceCodeContentDidChange: function(event)
237     {
238         if (this._ignoreSourceCodeContentDidChangeEvent)
239             return;
240
241         this._textEditor.string = this.resource.currentRevision.content;
242     },
243
244     _textEditorContentDidChange: function(event)
245     {
246         this._ignoreSourceCodeContentDidChangeEvent = true;
247         WebInspector.branchManager.currentBranch.revisionForRepresentedObject(this.resource).content = this._textEditor.string;
248         delete this._ignoreSourceCodeContentDidChangeEvent;
249     },
250
251     _executionLineNumberDidChange: function(event)
252     {
253         this.dispatchEventToListeners(WebInspector.ContentView.Event.SupplementalRepresentedObjectsDidChange);
254     },
255
256     _numberOfSearchResultsDidChange: function(event)
257     {
258         this.dispatchEventToListeners(WebInspector.ContentView.Event.NumberOfSearchResultsDidChange);
259     },
260
261     _probeSetsChanged: function(event)
262     {
263         var breakpoint = event.data.probeSet.breakpoint;
264         if (breakpoint.sourceCodeLocation.sourceCode === this.resource)
265             this.dispatchEventToListeners(WebInspector.ContentView.Event.SupplementalRepresentedObjectsDidChange);
266     }
267 };
268
269 WebInspector.TextResourceContentView.prototype.__proto__ = WebInspector.ResourceContentView.prototype;