Web Inspector: implement Flame Chart for CPU profiler.
[WebKit-https.git] / Source / WebCore / inspector / front-end / Settings.js
1 /*
2  * Copyright (C) 2009 Google 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 are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31
32 var Preferences = {
33     maxInlineTextChildLength: 80,
34     minConsoleHeight: 75,
35     minSidebarWidth: 100,
36     minSidebarHeight: 75,
37     minElementsSidebarWidth: 200,
38     minElementsSidebarHeight: 200,
39     minScriptsSidebarWidth: 200,
40     styleRulesExpandedState: {},
41     showMissingLocalizedStrings: false,
42     useLowerCaseMenuTitlesOnWindows: false,
43     sharedWorkersDebugNote: undefined,
44     localizeUI: true,
45     exposeDisableCache: false,
46     applicationTitle: "Web Inspector - %s",
47     showDockToRight: false,
48     exposeFileSystemInspection: false,
49     experimentsEnabled: true
50 }
51
52 var Capabilities = {
53     samplingCPUProfiler: false,
54     debuggerCausesRecompilation: true,
55     separateScriptCompilationAndExecutionEnabled: false,
56     profilerCausesRecompilation: true,
57     heapProfilerPresent: false,
58     canOverrideDeviceMetrics: false,
59     timelineSupportsFrameInstrumentation: false,
60     timelineCanMonitorMainThread: false,
61     canOverrideGeolocation: false,
62     canOverrideDeviceOrientation: false,
63     canShowDebugBorders: false,
64     canShowFPSCounter: false,
65     canContinuouslyPaint: false
66 }
67
68 /**
69  * @constructor
70  */
71 WebInspector.Settings = function()
72 {
73     this._eventSupport = new WebInspector.Object();
74
75     this.colorFormat = this.createSetting("colorFormat", "original");
76     this.consoleHistory = this.createSetting("consoleHistory", []);
77     this.debuggerEnabled = this.createSetting("debuggerEnabled", false);
78     this.domWordWrap = this.createSetting("domWordWrap", true);
79     this.profilerEnabled = this.createSetting("profilerEnabled", false);
80     this.eventListenersFilter = this.createSetting("eventListenersFilter", "all");
81     this.lastActivePanel = this.createSetting("lastActivePanel", "elements");
82     this.lastViewedScriptFile = this.createSetting("lastViewedScriptFile", "application");
83     this.monitoringXHREnabled = this.createSetting("monitoringXHREnabled", false);
84     this.preserveConsoleLog = this.createSetting("preserveConsoleLog", false);
85     this.resourcesLargeRows = this.createSetting("resourcesLargeRows", true);
86     this.resourcesSortOptions = this.createSetting("resourcesSortOptions", {timeOption: "responseTime", sizeOption: "transferSize"});
87     this.resourceViewTab = this.createSetting("resourceViewTab", "preview");
88     this.showInheritedComputedStyleProperties = this.createSetting("showInheritedComputedStyleProperties", false);
89     this.showUserAgentStyles = this.createSetting("showUserAgentStyles", true);
90     this.watchExpressions = this.createSetting("watchExpressions", []);
91     this.breakpoints = this.createSetting("breakpoints", []);
92     this.eventListenerBreakpoints = this.createSetting("eventListenerBreakpoints", []);
93     this.domBreakpoints = this.createSetting("domBreakpoints", []);
94     this.xhrBreakpoints = this.createSetting("xhrBreakpoints", []);
95     this.sourceMapsEnabled = this.createSetting("sourceMapsEnabled", false);
96     this.cacheDisabled = this.createSetting("cacheDisabled", false);
97     this.overrideUserAgent = this.createSetting("overrideUserAgent", "");
98     this.userAgent = this.createSetting("userAgent", "");
99     this.deviceMetrics = this.createSetting("deviceMetrics", "");
100     this.deviceFitWindow = this.createSetting("deviceFitWindow", false);
101     this.emulateTouchEvents = this.createSetting("emulateTouchEvents", false);
102     this.showPaintRects = this.createSetting("showPaintRects", false);
103     this.continuousPainting = this.createSetting("continuousPainting", false);
104     this.showDebugBorders = this.createSetting("showDebugBorders", false);
105     this.showFPSCounter = this.createSetting("showFPSCounter", false);
106     this.showShadowDOM = this.createSetting("showShadowDOM", false);
107     this.zoomLevel = this.createSetting("zoomLevel", 0);
108     this.savedURLs = this.createSetting("savedURLs", {});
109     this.javaScriptDisabled = this.createSetting("javaScriptDisabled", false);
110     this.geolocationOverride = this.createSetting("geolocationOverride", "");
111     this.deviceOrientationOverride = this.createSetting("deviceOrientationOverride", "");
112     this.showHeapSnapshotObjectsHiddenProperties = this.createSetting("showHeaSnapshotObjectsHiddenProperties", false);
113     this.showNativeSnapshotUninstrumentedSize = this.createSetting("showNativeSnapshotUninstrumentedSize", false);
114     this.searchInContentScripts = this.createSetting("searchInContentScripts", false);
115     this.textEditorIndent = this.createSetting("textEditorIndent", "    ");
116     this.lastDockState = this.createSetting("lastDockState", "");
117     this.cssReloadEnabled = this.createSetting("cssReloadEnabled", false);
118     this.cssReloadTimeout = this.createSetting("cssReloadTimeout", 1000);
119     this.showCpuOnTimelineRuler = this.createSetting("showCpuOnTimelineRuler", false);
120     this.showMetricsRulers = this.createSetting("showMetricsRulers", false);
121     this.emulatedCSSMedia = this.createSetting("emulatedCSSMedia", "print");
122     this.showToolbarIcons = this.createSetting("showToolbarIcons", false);
123     this.workerInspectorWidth = this.createSetting("workerInspectorWidth", 600);
124     this.workerInspectorHeight = this.createSetting("workerInspectorHeight", 600);
125     this.messageURLFilters = this.createSetting("messageURLFilters", {});
126     this.splitVerticallyWhenDockedToRight = this.createSetting("splitVerticallyWhenDockedToRight", true);
127 }
128
129 WebInspector.Settings.prototype = {
130     /**
131      * @return {WebInspector.Setting}
132      */
133     createSetting: function(key, defaultValue)
134     {
135         return new WebInspector.Setting(key, defaultValue, this._eventSupport);
136     }
137 }
138
139 /**
140  * @constructor
141  */
142 WebInspector.Setting = function(name, defaultValue, eventSupport)
143 {
144     this._name = name;
145     this._defaultValue = defaultValue;
146     this._eventSupport = eventSupport;
147 }
148
149 WebInspector.Setting.prototype = {
150     addChangeListener: function(listener, thisObject)
151     {
152         this._eventSupport.addEventListener(this._name, listener, thisObject);
153     },
154
155     removeChangeListener: function(listener, thisObject)
156     {
157         this._eventSupport.removeEventListener(this._name, listener, thisObject);
158     },
159
160     get name()
161     {
162         return this._name;
163     },
164
165     get: function()
166     {
167         if (typeof this._value !== "undefined")
168             return this._value;
169
170         this._value = this._defaultValue;
171         if (window.localStorage != null && this._name in window.localStorage) {
172             try {
173                 this._value = JSON.parse(window.localStorage[this._name]);
174             } catch(e) {
175                 window.localStorage.removeItem(this._name);
176             }
177         }
178         return this._value;
179     },
180
181     set: function(value)
182     {
183         this._value = value;
184         if (window.localStorage != null) {
185             try {
186                 window.localStorage[this._name] = JSON.stringify(value);
187             } catch(e) {
188                 console.error("Error saving setting with name:" + this._name);
189             }
190         }
191         this._eventSupport.dispatchEventToListeners(this._name, value);
192     }
193 }
194
195 /**
196  * @constructor
197  */
198 WebInspector.ExperimentsSettings = function()
199 {
200     this._setting = WebInspector.settings.createSetting("experiments", {});
201     this._experiments = [];
202     this._enabledForTest = {};
203     
204     // Add currently running experiments here.
205     this.snippetsSupport = this._createExperiment("snippetsSupport", "Snippets support");
206     this.nativeMemorySnapshots = this._createExperiment("nativeMemorySnapshots", "Native memory profiling");
207     this.liveNativeMemoryChart = this._createExperiment("liveNativeMemoryChart", "Live native memory chart");
208     this.nativeMemoryTimeline = this._createExperiment("nativeMemoryTimeline", "Native memory timeline");
209     this.fileSystemInspection = this._createExperiment("fileSystemInspection", "FileSystem inspection");
210     this.canvasInspection = this._createExperiment("canvasInspection ", "Canvas inspection");
211     this.sass = this._createExperiment("sass", "Support for Sass");
212     this.codemirror = this._createExperiment("codemirror", "Use CodeMirror editor");
213     this.aceTextEditor = this._createExperiment("aceTextEditor", "Use Ace editor");
214     this.cssRegions = this._createExperiment("cssRegions", "CSS Regions Support");
215     this.showOverridesInDrawer = this._createExperiment("showOverridesInDrawer", "Show Overrides in drawer");
216     this.fileSystemProject = this._createExperiment("fileSystemProject", "File system folders in Sources Panel");
217     this.showWhitespaceInEditor = this._createExperiment("showWhitespaceInEditor", "Show whitespace characters in editor");
218     this.textEditorSmartBraces = this._createExperiment("textEditorSmartBraces", "Enable smart braces in text editor");
219     this.separateProfilers = this._createExperiment("separateProfilers", "Separate profiler tools");
220     this.cpuFlameChart = this._createExperiment("cpuFlameChart", "Show Flame Chart in CPU Profiler");
221
222     this._cleanUpSetting();
223 }
224
225 WebInspector.ExperimentsSettings.prototype = {
226     /**
227      * @return {Array.<WebInspector.Experiment>}
228      */
229     get experiments()
230     {
231         return this._experiments.slice();
232     },
233     
234     /**
235      * @return {boolean}
236      */
237     get experimentsEnabled()
238     {
239         return Preferences.experimentsEnabled || ("experiments" in WebInspector.queryParamsObject);
240     },
241     
242     /**
243      * @param {string} experimentName
244      * @param {string} experimentTitle
245      * @return {WebInspector.Experiment}
246      */
247     _createExperiment: function(experimentName, experimentTitle)
248     {
249         var experiment = new WebInspector.Experiment(this, experimentName, experimentTitle);
250         this._experiments.push(experiment);
251         return experiment;
252     },
253     
254     /**
255      * @param {string} experimentName
256      * @return {boolean}
257      */
258     isEnabled: function(experimentName)
259     {
260         if (this._enabledForTest[experimentName])
261             return true;
262
263         if (!this.experimentsEnabled)
264             return false;
265         
266         var experimentsSetting = this._setting.get();
267         return experimentsSetting[experimentName];
268     },
269     
270     /**
271      * @param {string} experimentName
272      * @param {boolean} enabled
273      */
274     setEnabled: function(experimentName, enabled)
275     {
276         var experimentsSetting = this._setting.get();
277         experimentsSetting[experimentName] = enabled;
278         this._setting.set(experimentsSetting);
279     },
280
281     /**
282      * @param {string} experimentName
283      */
284     _enableForTest: function(experimentName)
285     {
286         this._enabledForTest[experimentName] = true;
287     },
288
289     _cleanUpSetting: function()
290     {
291         var experimentsSetting = this._setting.get();
292         var cleanedUpExperimentSetting = {};
293         for (var i = 0; i < this._experiments.length; ++i) {
294             var experimentName = this._experiments[i].name;
295             if (experimentsSetting[experimentName])
296                 cleanedUpExperimentSetting[experimentName] = true;
297         }
298         this._setting.set(cleanedUpExperimentSetting);
299     }
300 }
301
302 /**
303  * @constructor
304  * @param {WebInspector.ExperimentsSettings} experimentsSettings
305  * @param {string} name
306  * @param {string} title
307  */
308 WebInspector.Experiment = function(experimentsSettings, name, title)
309 {
310     this._name = name;
311     this._title = title;
312     this._experimentsSettings = experimentsSettings;
313 }
314
315 WebInspector.Experiment.prototype = {
316     /**
317      * @return {string}
318      */
319     get name()
320     {
321         return this._name;
322     },
323     
324     /**
325      * @return {string}
326      */
327     get title()
328     {
329         return this._title;
330     },
331     
332     /**
333      * @return {boolean}
334      */
335     isEnabled: function()
336     {
337         return this._experimentsSettings.isEnabled(this._name);
338     },
339     
340     /**
341      * @param {boolean} enabled
342      */
343     setEnabled: function(enabled)
344     {
345         return this._experimentsSettings.setEnabled(this._name, enabled);
346     },
347
348     enableForTest: function()
349     {
350         this._experimentsSettings._enableForTest(this._name);
351     }
352 }
353
354 /**
355  * @constructor
356  */
357 WebInspector.VersionController = function()
358 {
359 }
360
361 WebInspector.VersionController.currentVersion = 1;
362
363 WebInspector.VersionController.prototype = {
364     updateVersion: function()
365     {
366         var versionSetting = WebInspector.settings.createSetting("inspectorVersion", 0);
367         var currentVersion = WebInspector.VersionController.currentVersion;
368         var oldVersion = versionSetting.get();
369         var methodsToRun = this._methodsToRunToUpdateVersion(oldVersion, currentVersion);
370         for (var i = 0; i < methodsToRun.length; ++i)
371             this[methodsToRun[i]].call(this);
372         versionSetting.set(currentVersion);
373     },
374
375     /**
376      * @param {number} oldVersion
377      * @param {number} currentVersion
378      */
379     _methodsToRunToUpdateVersion: function(oldVersion, currentVersion)
380     {
381         var result = [];
382         for (var i = oldVersion; i < currentVersion; ++i)
383             result.push("_updateVersionFrom" + i + "To" + (i + 1));
384         return result;
385     },
386
387     _updateVersionFrom0To1: function()
388     {
389         this._clearBreakpointsWhenTooMany(WebInspector.settings.breakpoints, 500000);
390     },
391
392     /**
393      * @param {WebInspector.Setting} breakpointsSetting
394      * @param {number} maxBreakpointsCount
395      */
396     _clearBreakpointsWhenTooMany: function(breakpointsSetting, maxBreakpointsCount)
397     {
398         // If there are too many breakpoints in a storage, it is likely due to a recent bug that caused
399         // periodical breakpoints duplication leading to inspector slowness.
400         if (breakpointsSetting.get().length > maxBreakpointsCount)
401             breakpointsSetting.set([]);
402     }
403 }
404
405 WebInspector.settings = new WebInspector.Settings();
406 WebInspector.experimentsSettings = new WebInspector.ExperimentsSettings();