Chromium Code Reviews| Index: Source/devtools/front_end/common/Settings.js |
| diff --git a/Source/devtools/front_end/common/Settings.js b/Source/devtools/front_end/common/Settings.js |
| index c0ebb65c39fe3eee70b1cadfa77cb4b3d57bc1a2..ef18246f5f501bf2faded5e27eaf4dc119d5b5d0 100644 |
| --- a/Source/devtools/front_end/common/Settings.js |
| +++ b/Source/devtools/front_end/common/Settings.js |
| @@ -30,9 +30,11 @@ |
| /** |
| * @constructor |
| + * @param {!Object<string, string>} prefs |
| */ |
| -WebInspector.Settings = function() |
| +WebInspector.Settings = function(prefs) |
| { |
| + this._settingsStorage = prefs; |
| this._eventSupport = new WebInspector.Object(); |
| /** @type {!Map<string, !WebInspector.Setting>} */ |
| this._registry = new Map(); |
| @@ -51,7 +53,8 @@ WebInspector.Settings.prototype = { |
| var settingName = descriptor["settingName"]; |
| var settingType = descriptor["settingType"]; |
| var defaultValue = descriptor["defaultValue"]; |
| - var setting = settingType === "regex" ? this.createRegExpSetting(settingName, defaultValue) : this.createSetting(settingName, defaultValue); |
| + var isLocal = !!descriptor["local"]; |
| + var setting = settingType === "regex" ? this.createRegExpSetting(settingName, defaultValue, undefined, isLocal) : this.createSetting(settingName, defaultValue, isLocal); |
| this._moduleSettings.set(settingName, setting); |
| }, |
| @@ -82,39 +85,63 @@ WebInspector.Settings.prototype = { |
| /** |
| * @param {string} key |
| * @param {*} defaultValue |
| + * @param {boolean=} isLocal |
| * @return {!WebInspector.Setting} |
| */ |
| - createSetting: function(key, defaultValue) |
| + createSetting: function(key, defaultValue, isLocal) |
| { |
| if (!this._registry.get(key)) |
| - this._registry.set(key, new WebInspector.Setting(key, defaultValue, this._eventSupport, window.localStorage)); |
| + this._registry.set(key, new WebInspector.Setting(this, key, defaultValue, this._eventSupport, isLocal ? (window.localStorage || {}) : this._settingsStorage)); |
| return /** @type {!WebInspector.Setting} */ (this._registry.get(key)); |
| }, |
| /** |
| * @param {string} key |
| + * @param {*} defaultValue |
| + * @return {!WebInspector.Setting} |
| + */ |
| + createLocalSetting: function(key, defaultValue) |
| + { |
| + return this.createSetting(key, defaultValue, true); |
| + }, |
| + |
| + /** |
| + * @param {string} key |
| * @param {string} defaultValue |
| * @param {string=} regexFlags |
| + * @param {boolean=} isLocal |
| * @return {!WebInspector.RegExpSetting} |
| */ |
| - createRegExpSetting: function(key, defaultValue, regexFlags) |
| + createRegExpSetting: function(key, defaultValue, regexFlags, isLocal) |
| { |
| if (!this._registry.get(key)) |
| - this._registry.set(key, new WebInspector.RegExpSetting(key, defaultValue, this._eventSupport, window.localStorage, regexFlags)); |
| + this._registry.set(key, new WebInspector.RegExpSetting(this, key, defaultValue, this._eventSupport, isLocal ? (window.localStorage || {}) : this._settingsStorage, regexFlags)); |
| return /** @type {!WebInspector.RegExpSetting} */ (this._registry.get(key)); |
| + }, |
| + |
| + clearAll: function() |
| + { |
| + if (window.localStorage) |
| + window.localStorage.clear(); |
| + for (var key in this._settingsStorage) |
| + delete this._settingsStorage[key]; |
| + var versionSetting = WebInspector.settings.createSetting(WebInspector.VersionController._currentVersionName, 0); |
| + versionSetting.set(WebInspector.VersionController.currentVersion); |
| } |
| } |
| /** |
| * @constructor |
| + * @param {!WebInspector.Settings} settings |
| * @param {string} name |
| * @param {V} defaultValue |
| * @param {!WebInspector.Object} eventSupport |
| - * @param {?Storage} storage |
| + * @param {!Object} storage |
| * @template V |
| */ |
| -WebInspector.Setting = function(name, defaultValue, eventSupport, storage) |
| +WebInspector.Setting = function(settings, name, defaultValue, eventSupport, storage) |
| { |
| + this._settings = settings; |
| this._name = name; |
| this._defaultValue = defaultValue; |
| this._eventSupport = eventSupport; |
| @@ -154,11 +181,11 @@ WebInspector.Setting.prototype = { |
| return this._value; |
| this._value = this._defaultValue; |
| - if (this._storage && this._name in this._storage) { |
| + if (this._name in this._storage) { |
| try { |
| this._value = JSON.parse(this._storage[this._name]); |
| } catch(e) { |
| - delete this._storage[this._name]; |
| + this.remove(); |
| } |
| } |
| return this._value; |
| @@ -170,21 +197,26 @@ WebInspector.Setting.prototype = { |
| set: function(value) |
| { |
| this._value = value; |
| - if (this._storage) { |
| + try { |
| + var settingString = JSON.stringify(value); |
| try { |
| - var settingString = JSON.stringify(value); |
| - try { |
| - this._storage[this._name] = settingString; |
| - } catch(e) { |
| - this._printSettingsSavingError(e.message, this._name, settingString); |
| - } |
| + this._storage[this._name] = settingString; |
| } catch(e) { |
| - WebInspector.console.error("Cannot stringify setting with name: " + this._name + ", error: " + e.message); |
| + this._printSettingsSavingError(e.message, this._name, settingString); |
| } |
| + } catch(e) { |
| + WebInspector.console.error("Cannot stringify setting with name: " + this._name + ", error: " + e.message); |
| } |
| this._eventSupport.dispatchEventToListeners(this._name, value); |
| }, |
| + remove: function() |
| + { |
| + this._settings._registry.delete(this._name); |
| + this._settings._moduleSettings.delete(this._name); |
| + delete this._storage[this._name]; |
| + }, |
| + |
| /** |
| * @param {string} message |
| * @param {string} name |
| @@ -199,7 +231,7 @@ WebInspector.Setting.prototype = { |
| var sizes = { __proto__: null }; |
| for (var key in this._storage) |
| - sizes[key] = this._storage.getItem(key).length; |
| + sizes[key] = this._storage[key].length; |
| var keys = Object.keys(sizes); |
| function comparator(key1, key2) |
| @@ -211,21 +243,22 @@ WebInspector.Setting.prototype = { |
| for (var i = 0; i < 10 && i < keys.length; ++i) |
| WebInspector.console.log("Setting: '" + keys[i] + "', size: " + sizes[keys[i]]); |
| - }, |
| + } |
| } |
| /** |
| * @constructor |
| * @extends {WebInspector.Setting} |
| + * @param {!WebInspector.Settings} settings |
| * @param {string} name |
| * @param {string} defaultValue |
| * @param {!WebInspector.Object} eventSupport |
| - * @param {?Storage} storage |
| + * @param {!Object<string, string>} storage |
| * @param {string=} regexFlags |
| */ |
| -WebInspector.RegExpSetting = function(name, defaultValue, eventSupport, storage, regexFlags) |
| +WebInspector.RegExpSetting = function(settings, name, defaultValue, eventSupport, storage, regexFlags) |
| { |
| - WebInspector.Setting.call(this, name, defaultValue ? [{ pattern: defaultValue }] : [], eventSupport, storage); |
| + WebInspector.Setting.call(this, settings, name, defaultValue ? [{ pattern: defaultValue }] : [], eventSupport, storage); |
| this._regexFlags = regexFlags; |
| } |
| @@ -299,12 +332,13 @@ WebInspector.VersionController = function() |
| { |
| } |
| -WebInspector.VersionController.currentVersion = 11; |
| +WebInspector.VersionController._currentVersionName = "inspectorVersion"; |
| +WebInspector.VersionController.currentVersion = 12; |
| WebInspector.VersionController.prototype = { |
| updateVersion: function() |
| { |
| - var versionSetting = WebInspector.settings.createSetting("inspectorVersion", 0); |
| + var versionSetting = WebInspector.settings.createSetting(WebInspector.VersionController._currentVersionName, 0); |
| var currentVersion = WebInspector.VersionController.currentVersion; |
| var oldVersion = versionSetting.get(); |
| var methodsToRun = this._methodsToRunToUpdateVersion(oldVersion, currentVersion); |
| @@ -327,33 +361,29 @@ WebInspector.VersionController.prototype = { |
| _updateVersionFrom0To1: function() |
| { |
| - this._clearBreakpointsWhenTooMany(WebInspector.settings.createSetting("breakpoints", []), 500000); |
| + this._clearBreakpointsWhenTooMany(WebInspector.settings.createLocalSetting("breakpoints", []), 500000); |
| }, |
| _updateVersionFrom1To2: function() |
| { |
| - var versionSetting = WebInspector.settings.createSetting("previouslyViewedFiles", []); |
| - versionSetting.set([]); |
| + WebInspector.settings.createSetting("previouslyViewedFiles", []).set([]); |
| }, |
| _updateVersionFrom2To3: function() |
| { |
| - var fileSystemMappingSetting = WebInspector.settings.createSetting("fileSystemMapping", {}); |
| - fileSystemMappingSetting.set({}); |
| - if (window.localStorage) |
| - delete window.localStorage["fileMappingEntries"]; |
| + WebInspector.settings.createSetting("fileSystemMapping", {}).set({}); |
| + WebInspector.settings.createSetting("fileMappingEntries", []).remove(); |
| }, |
| _updateVersionFrom3To4: function() |
| { |
| - var advancedMode = WebInspector.settings.createSetting("showHeaSnapshotObjectsHiddenProperties", false).get(); |
| - WebInspector.moduleSetting("showAdvancedHeapSnapshotProperties").set(advancedMode); |
| + var advancedMode = WebInspector.settings.createSetting("showHeaSnapshotObjectsHiddenProperties", false); |
| + WebInspector.moduleSetting("showAdvancedHeapSnapshotProperties").set(advancedMode.get()); |
| + advancedMode.remove(); |
| }, |
| _updateVersionFrom4To5: function() |
| { |
| - if (!window.localStorage) |
| - return; |
| var settingNames = { |
| "FileSystemViewSidebarWidth": "fileSystemViewSplitViewState", |
| "elementsSidebarWidth": "elementsPanelSplitViewState", |
| @@ -375,52 +405,50 @@ WebInspector.VersionController.prototype = { |
| "profilesSidebarWidth": "profilesPanelSplitViewState", |
| "resourcesSidebarWidth": "resourcesPanelSplitViewState" |
| }; |
| + var empty = {}; |
| for (var oldName in settingNames) { |
| var newName = settingNames[oldName]; |
| var oldNameH = oldName + "H"; |
| var newValue = null; |
| - var oldSetting = WebInspector.settings.createSetting(oldName, undefined).get(); |
| - if (oldSetting) { |
| + var oldSetting = WebInspector.settings.createSetting(oldName, empty); |
| + if (oldSetting.get() !== empty) { |
| newValue = newValue || {}; |
| newValue.vertical = {}; |
| - newValue.vertical.size = oldSetting; |
| - delete window.localStorage[oldName]; |
| + newValue.vertical.size = oldSetting.get(); |
| + oldSetting.remove(); |
| } |
| - var oldSettingH = WebInspector.settings.createSetting(oldNameH, undefined).get(); |
| - if (oldSettingH) { |
| + var oldSettingH = WebInspector.settings.createSetting(oldNameH, empty); |
| + if (oldSettingH.get() !== empty) { |
| newValue = newValue || {}; |
| newValue.horizontal = {}; |
| - newValue.horizontal.size = oldSettingH; |
| - delete window.localStorage[oldNameH]; |
| + newValue.horizontal.size = oldSettingH.get(); |
| + oldSettingH.remove(); |
| } |
| - var newSetting = WebInspector.settings.createSetting(newName, {}); |
| if (newValue) |
| - newSetting.set(newValue); |
| + WebInspector.settings.createSetting(newName, {}).set(newValue); |
| } |
| }, |
| _updateVersionFrom5To6: function() |
| { |
| - if (!window.localStorage) |
| - return; |
| - |
| var settingNames = { |
| "debuggerSidebarHidden": "sourcesPanelSplitViewState", |
| "navigatorHidden": "sourcesPanelNavigatorSplitViewState", |
| "WebInspector.Drawer.showOnLoad": "Inspector.drawerSplitViewState" |
| }; |
| + var empty = {}; |
| for (var oldName in settingNames) { |
| var newName = settingNames[oldName]; |
| - var oldSetting = WebInspector.settings.createSetting(oldName, undefined).get(); |
| + var oldSetting = WebInspector.settings.createSetting(oldName, empty); |
| var invert = "WebInspector.Drawer.showOnLoad" === oldName; |
| - var hidden = !!oldSetting !== invert; |
| - delete window.localStorage[oldName]; |
| + var hidden = (oldSetting.get() !== empty) !== invert; |
| + oldSetting.remove(); |
| var showMode = hidden ? "OnlyMain" : "Both"; |
| - var newSetting = WebInspector.settings.createSetting(newName, null); |
| + var newSetting = WebInspector.settings.createSetting(newName, empty); |
| var newValue = newSetting.get() || {}; |
| newValue.vertical = newValue.vertical || {}; |
| newValue.vertical.showMode = showMode; |
| @@ -432,9 +460,6 @@ WebInspector.VersionController.prototype = { |
| _updateVersionFrom6To7: function() |
| { |
| - if (!window.localStorage) |
| - return; |
| - |
| var settingNames = { |
| "sourcesPanelNavigatorSplitViewState": "sourcesPanelNavigatorSplitViewState", |
| "elementsPanelSplitViewState": "elementsPanelSplitViewState", |
| @@ -442,12 +467,11 @@ WebInspector.VersionController.prototype = { |
| "sourcesPanelDebuggerSidebarSplitViewState": "sourcesPanelDebuggerSidebarSplitViewState" |
| }; |
| + var empty = {}; |
| for (var name in settingNames) { |
| - if (!(name in window.localStorage)) |
| - continue; |
| - var setting = WebInspector.settings.createSetting(name, undefined); |
| + var setting = WebInspector.settings.createSetting(name, empty); |
| var value = setting.get(); |
| - if (!value) |
| + if (value === empty) |
| continue; |
| // Zero out saved percentage sizes, and they will be restored to defaults. |
| if (value.vertical && value.vertical.size && value.vertical.size < 1) |
| @@ -460,63 +484,36 @@ WebInspector.VersionController.prototype = { |
| _updateVersionFrom7To8: function() |
| { |
| - var settingName = "deviceMetrics"; |
| - if (!window.localStorage || !(settingName in window.localStorage)) |
| - return; |
| - var setting = WebInspector.settings.createSetting(settingName, undefined); |
| - var value = setting.get(); |
| - if (!value) |
| - return; |
| - |
| - var components = value.split("x"); |
| - if (components.length >= 3) { |
| - var width = parseInt(components[0], 10); |
| - var height = parseInt(components[1], 10); |
| - var deviceScaleFactor = parseFloat(components[2]); |
| - if (deviceScaleFactor) { |
| - components[0] = "" + Math.round(width / deviceScaleFactor); |
| - components[1] = "" + Math.round(height / deviceScaleFactor); |
| - } |
| - } |
| - value = components.join("x"); |
| - setting.set(value); |
| }, |
| _updateVersionFrom8To9: function() |
| { |
| - if (!window.localStorage) |
| - return; |
| - |
| var settingNames = [ |
| "skipStackFramesPattern", |
| "workspaceFolderExcludePattern" |
| ]; |
| for (var i = 0; i < settingNames.length; ++i) { |
| - var settingName = settingNames[i]; |
| - if (!(settingName in window.localStorage)) |
| - continue; |
| - try { |
| - var value = JSON.parse(window.localStorage[settingName]); |
| - if (!value) |
| - continue; |
| - if (typeof value === "string") |
| - value = [value]; |
| - for (var j = 0; j < value.length; ++j) { |
| - if (typeof value[j] === "string") |
| - value[j] = { pattern: value[j] }; |
| - } |
| - window.localStorage[settingName] = JSON.stringify(value); |
| - } catch(e) { |
| + var setting = WebInspector.settings.createSetting(settingNames[i], ""); |
| + var value = setting.get(); |
| + if (!value) |
| + return; |
| + |
| + if (typeof value === "string") |
| + value = [value]; |
| + for (var j = 0; j < value.length; ++j) { |
| + if (typeof value[j] === "string") |
| + value[j] = { pattern: value[j] }; |
| } |
| + setting.set(value); |
| } |
| }, |
| _updateVersionFrom9To10: function() |
| { |
| + // This one is localStorage secific, which is fine. |
|
dgozman
2015/04/13 14:25:38
typo: specific
pfeldman
2015/04/13 16:03:38
Done.
|
| if (!window.localStorage) |
| return; |
| - |
| for (var key in window.localStorage) { |
| if (key.startsWith("revision-history")) |
| window.localStorage.removeItem(key); |
| @@ -525,40 +522,52 @@ WebInspector.VersionController.prototype = { |
| _updateVersionFrom10To11: function() |
| { |
| - var setting = "customDevicePresets"; |
| - var newSetting = "customEmulatedDeviceList"; |
| - if (!window.localStorage) |
| + var oldSettingName = "customDevicePresets"; |
| + var newSettingName = "customEmulatedDeviceList"; |
| + var oldSetting = WebInspector.settings.createSetting(oldSettingName, undefined); |
| + var list = oldSetting.get(); |
| + if (!Array.isArray(list)) |
| return; |
| - if (!(setting in window.localStorage)) |
| + var newList = []; |
| + for (var i = 0; i < list.length; ++i) { |
| + var value = list[i]; |
| + var device = {}; |
| + device["title"] = value["title"]; |
| + device["type"] = "unknown"; |
| + device["user-agent"] = value["userAgent"]; |
| + device["capabilities"] = []; |
| + if (value["touch"]) |
| + device["capabilities"].push("touch"); |
| + if (value["mobile"]) |
| + device["capabilities"].push("mobile"); |
| + device["screen"] = {}; |
| + device["screen"]["vertical"] = {width: value["width"], height: value["height"]}; |
| + device["screen"]["horizontal"] = {width: value["height"], height: value["width"]}; |
| + device["screen"]["device-pixel-ratio"] = value["deviceScaleFactor"]; |
| + device["modes"] = []; |
| + device["show-by-default"] = true; |
| + device["show"] = "Default"; |
| + newList.push(device); |
| + } |
| + if (newList.length) |
| + WebInspector.settings.createSetting(newSettingName, []).set(newList); |
| + oldSetting.remove(); |
| + }, |
| + |
| + _updateVersionFrom11To12: function() |
| + { |
| + // This step migrates all the settings except for the ones below into the browser profile. |
| + var localSettings = [ "breakpoints", "consoleHistory", "domBreakpoints", "eventListenerBreakpoints", |
|
dgozman
2015/04/13 14:25:38
I'd add "lastSelectedSourcesSidebarPaneTab", "prev
pfeldman
2015/04/13 16:03:38
Done.
|
| + "fileSystemMapping", "savedURLs", "watchExpressions", "workspaceExcludedFolders", "xhrBreakpoints" ].keySet(); |
| + if (!window.localStorage) |
| return; |
| - try { |
| - var list = JSON.parse(window.localStorage[setting]); |
| - if (!Array.isArray(list)) |
| - return; |
| - var newList = []; |
| - for (var i = 0; i < list.length; ++i) { |
| - var value = list[i]; |
| - var device = {}; |
| - device["title"] = value["title"]; |
| - device["type"] = "unknown"; |
| - device["user-agent"] = value["userAgent"]; |
| - device["capabilities"] = []; |
| - if (value["touch"]) |
| - device["capabilities"].push("touch"); |
| - if (value["mobile"]) |
| - device["capabilities"].push("mobile"); |
| - device["screen"] = {}; |
| - device["screen"]["vertical"] = {width: value["width"], height: value["height"]}; |
| - device["screen"]["horizontal"] = {width: value["height"], height: value["width"]}; |
| - device["screen"]["device-pixel-ratio"] = value["deviceScaleFactor"]; |
| - device["modes"] = []; |
| - device["show-by-default"] = true; |
| - device["show"] = "Default"; |
| - newList.push(device); |
| - } |
| - window.localStorage[newSetting] = JSON.stringify(newList); |
| - delete window.localStorage[setting]; |
| - } catch(e) { |
| + |
| + for (var key in window.localStorage) { |
| + if (key in localSettings) |
| + continue; |
| + var value = window.localStorage[key]; |
| + WebInspector.settings._settingsStorage[key] = value; |
| + window.localStorage.removeItem(key); |
|
dgozman
2015/04/13 14:25:38
If settings storage is |localStorage|, this code b
pfeldman
2015/04/13 16:03:38
Done.
|
| } |
| }, |