ad6985da326eb170512e813d7468e075ca4be64e
[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.cssRegions = this._createExperiment("cssRegions", "CSS Regions Support");
214     this.showOverridesInDrawer = this._createExperiment("showOverridesInDrawer", "Show Overrides in drawer");
215     this.fileSystemProject = this._createExperiment("fileSystemProject", "File system folders in Sources Panel");
216     this.showWhitespaceInEditor = this._createExperiment("showWhitespaceInEditor", "Show whitespace characters in editor");
217     this.textEditorSmartBraces = this._createExperiment("textEditorSmartBraces", "Enable smart braces in text editor");
218     this.separateProfilers = this._createExperiment("separateProfilers", "Separate profiler tools");
219
220     this._cleanUpSetting();
221 }
222
223 WebInspector.ExperimentsSettings.prototype = {
224     /**
225      * @return {Array.<WebInspector.Experiment>}
226      */
227     get experiments()
228     {
229         return this._experiments.slice();
230     },
231     
232     /**
233      * @return {boolean}
234      */
235     get experimentsEnabled()
236     {
237         return Preferences.experimentsEnabled || ("experiments" in WebInspector.queryParamsObject);
238     },
239     
240     /**
241      * @param {string} experimentName
242      * @param {string} experimentTitle
243      * @return {WebInspector.Experiment}
244      */
245     _createExperiment: function(experimentName, experimentTitle)
246     {
247         var experiment = new WebInspector.Experiment(this, experimentName, experimentTitle);
248         this._experiments.push(experiment);
249         return experiment;
250     },
251     
252     /**
253      * @param {string} experimentName
254      * @return {boolean}
255      */
256     isEnabled: function(experimentName)
257     {
258         if (this._enabledForTest[experimentName])
259             return true;
260
261         if (!this.experimentsEnabled)
262             return false;
263         
264         var experimentsSetting = this._setting.get();
265         return experimentsSetting[experimentName];
266     },
267     
268     /**
269      * @param {string} experimentName
270      * @param {boolean} enabled
271      */
272     setEnabled: function(experimentName, enabled)
273     {
274         var experimentsSetting = this._setting.get();
275         experimentsSetting[experimentName] = enabled;
276         this._setting.set(experimentsSetting);
277     },
278
279     /**
280      * @param {string} experimentName
281      */
282     _enableForTest: function(experimentName)
283     {
284         this._enabledForTest[experimentName] = true;
285     },
286
287     _cleanUpSetting: function()
288     {
289         var experimentsSetting = this._setting.get();
290         var cleanedUpExperimentSetting = {};
291         for (var i = 0; i < this._experiments.length; ++i) {
292             var experimentName = this._experiments[i].name;
293             if (experimentsSetting[experimentName])
294                 cleanedUpExperimentSetting[experimentName] = true;
295         }
296         this._setting.set(cleanedUpExperimentSetting);
297     }
298 }
299
300 /**
301  * @constructor
302  * @param {WebInspector.ExperimentsSettings} experimentsSettings
303  * @param {string} name
304  * @param {string} title
305  */
306 WebInspector.Experiment = function(experimentsSettings, name, title)
307 {
308     this._name = name;
309     this._title = title;
310     this._experimentsSettings = experimentsSettings;
311 }
312
313 WebInspector.Experiment.prototype = {
314     /**
315      * @return {string}
316      */
317     get name()
318     {
319         return this._name;
320     },
321     
322     /**
323      * @return {string}
324      */
325     get title()
326     {
327         return this._title;
328     },
329     
330     /**
331      * @return {boolean}
332      */
333     isEnabled: function()
334     {
335         return this._experimentsSettings.isEnabled(this._name);
336     },
337     
338     /**
339      * @param {boolean} enabled
340      */
341     setEnabled: function(enabled)
342     {
343         return this._experimentsSettings.setEnabled(this._name, enabled);
344     },
345
346     enableForTest: function()
347     {
348         this._experimentsSettings._enableForTest(this._name);
349     }
350 }
351
352 /**
353  * @constructor
354  */
355 WebInspector.VersionController = function()
356 {
357 }
358
359 WebInspector.VersionController.currentVersion = 1;
360
361 WebInspector.VersionController.prototype = {
362     updateVersion: function()
363     {
364         var versionSetting = WebInspector.settings.createSetting("inspectorVersion", 0);
365         var currentVersion = WebInspector.VersionController.currentVersion;
366         var oldVersion = versionSetting.get();
367         var methodsToRun = this._methodsToRunToUpdateVersion(oldVersion, currentVersion);
368         for (var i = 0; i < methodsToRun.length; ++i)
369             this[methodsToRun[i]].call(this);
370         versionSetting.set(currentVersion);
371     },
372
373     /**
374      * @param {number} oldVersion
375      * @param {number} currentVersion
376      */
377     _methodsToRunToUpdateVersion: function(oldVersion, currentVersion)
378     {
379         var result = [];
380         for (var i = oldVersion; i < currentVersion; ++i)
381             result.push("_updateVersionFrom" + i + "To" + (i + 1));
382         return result;
383     },
384
385     _updateVersionFrom0To1: function()
386     {
387         this._clearBreakpointsWhenTooMany(WebInspector.settings.breakpoints, 500000);
388     },
389
390     /**
391      * @param {WebInspector.Setting} breakpointsSetting
392      * @param {number} maxBreakpointsCount
393      */
394     _clearBreakpointsWhenTooMany: function(breakpointsSetting, maxBreakpointsCount)
395     {
396         // If there are too many breakpoints in a storage, it is likely due to a recent bug that caused
397         // periodical breakpoints duplication leading to inspector slowness.
398         if (breakpointsSetting.get().length > maxBreakpointsCount)
399             breakpointsSetting.set([]);
400     }
401 }
402
403 WebInspector.settings = new WebInspector.Settings();
404 WebInspector.experimentsSettings = new WebInspector.ExperimentsSettings();