Settings.js 15 KB

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