| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 /** | 31 /** |
| 32 * @constructor | 32 * @constructor |
| 33 * @param {!Object<string, string>} prefs | 33 * @param {!WebInspector.SettingsStorage} storage |
| 34 */ | 34 */ |
| 35 WebInspector.Settings = function(prefs) | 35 WebInspector.Settings = function(storage) |
| 36 { | 36 { |
| 37 this._settingsStorage = prefs; | 37 this._settingsStorage = storage; |
| 38 this._localStorage = new WebInspector.SettingsStorage(window.localStorage ||
{}); |
| 39 |
| 38 this._eventSupport = new WebInspector.Object(); | 40 this._eventSupport = new WebInspector.Object(); |
| 39 /** @type {!Map<string, !WebInspector.Setting>} */ | 41 /** @type {!Map<string, !WebInspector.Setting>} */ |
| 40 this._registry = new Map(); | 42 this._registry = new Map(); |
| 41 /** @type {!Map<string, !WebInspector.Setting>} */ | 43 /** @type {!Map<string, !WebInspector.Setting>} */ |
| 42 this._moduleSettings = new Map(); | 44 this._moduleSettings = new Map(); |
| 43 self.runtime.extensions("setting").forEach(this._registerModuleSetting.bind(
this)); | 45 self.runtime.extensions("setting").forEach(this._registerModuleSetting.bind(
this)); |
| 44 } | 46 } |
| 45 | 47 |
| 46 WebInspector.Settings.prototype = { | 48 WebInspector.Settings.prototype = { |
| 47 /** | 49 /** |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 | 86 |
| 85 /** | 87 /** |
| 86 * @param {string} key | 88 * @param {string} key |
| 87 * @param {*} defaultValue | 89 * @param {*} defaultValue |
| 88 * @param {boolean=} isLocal | 90 * @param {boolean=} isLocal |
| 89 * @return {!WebInspector.Setting} | 91 * @return {!WebInspector.Setting} |
| 90 */ | 92 */ |
| 91 createSetting: function(key, defaultValue, isLocal) | 93 createSetting: function(key, defaultValue, isLocal) |
| 92 { | 94 { |
| 93 if (!this._registry.get(key)) | 95 if (!this._registry.get(key)) |
| 94 this._registry.set(key, new WebInspector.Setting(this, key, defaultV
alue, this._eventSupport, isLocal ? (window.localStorage || {}) : this._settings
Storage)); | 96 this._registry.set(key, new WebInspector.Setting(this, key, defaultV
alue, this._eventSupport, isLocal ? this._localStorage : this._settingsStorage))
; |
| 95 return /** @type {!WebInspector.Setting} */ (this._registry.get(key)); | 97 return /** @type {!WebInspector.Setting} */ (this._registry.get(key)); |
| 96 }, | 98 }, |
| 97 | 99 |
| 98 /** | 100 /** |
| 99 * @param {string} key | 101 * @param {string} key |
| 100 * @param {*} defaultValue | 102 * @param {*} defaultValue |
| 101 * @return {!WebInspector.Setting} | 103 * @return {!WebInspector.Setting} |
| 102 */ | 104 */ |
| 103 createLocalSetting: function(key, defaultValue) | 105 createLocalSetting: function(key, defaultValue) |
| 104 { | 106 { |
| 105 return this.createSetting(key, defaultValue, true); | 107 return this.createSetting(key, defaultValue, true); |
| 106 }, | 108 }, |
| 107 | 109 |
| 108 /** | 110 /** |
| 109 * @param {string} key | 111 * @param {string} key |
| 110 * @param {string} defaultValue | 112 * @param {string} defaultValue |
| 111 * @param {string=} regexFlags | 113 * @param {string=} regexFlags |
| 112 * @param {boolean=} isLocal | 114 * @param {boolean=} isLocal |
| 113 * @return {!WebInspector.RegExpSetting} | 115 * @return {!WebInspector.RegExpSetting} |
| 114 */ | 116 */ |
| 115 createRegExpSetting: function(key, defaultValue, regexFlags, isLocal) | 117 createRegExpSetting: function(key, defaultValue, regexFlags, isLocal) |
| 116 { | 118 { |
| 117 if (!this._registry.get(key)) | 119 if (!this._registry.get(key)) |
| 118 this._registry.set(key, new WebInspector.RegExpSetting(this, key, de
faultValue, this._eventSupport, isLocal ? (window.localStorage || {}) : this._se
ttingsStorage, regexFlags)); | 120 this._registry.set(key, new WebInspector.RegExpSetting(this, key, de
faultValue, this._eventSupport, isLocal ? this._localStorage : this._settingsSto
rage, regexFlags)); |
| 119 return /** @type {!WebInspector.RegExpSetting} */ (this._registry.get(ke
y)); | 121 return /** @type {!WebInspector.RegExpSetting} */ (this._registry.get(ke
y)); |
| 120 }, | 122 }, |
| 121 | 123 |
| 122 clearAll: function() | 124 clearAll: function() |
| 123 { | 125 { |
| 124 if (window.localStorage) | 126 this._settingsStorage.removeAll(); |
| 125 window.localStorage.clear(); | 127 this._localStorage.removeAll(); |
| 126 for (var key in this._settingsStorage) | |
| 127 delete this._settingsStorage[key]; | |
| 128 var versionSetting = WebInspector.settings.createSetting(WebInspector.Ve
rsionController._currentVersionName, 0); | 128 var versionSetting = WebInspector.settings.createSetting(WebInspector.Ve
rsionController._currentVersionName, 0); |
| 129 versionSetting.set(WebInspector.VersionController.currentVersion); | 129 versionSetting.set(WebInspector.VersionController.currentVersion); |
| 130 } | 130 } |
| 131 } | 131 } |
| 132 | 132 |
| 133 /** | 133 /** |
| 134 * @constructor | 134 * @constructor |
| 135 * @param {!Object} object |
| 136 * @param {function(string, string)=} setCallback |
| 137 * @param {function(string)=} removeCallback |
| 138 * @param {function(string)=} removeAllCallback |
| 139 */ |
| 140 WebInspector.SettingsStorage = function(object, setCallback, removeCallback, rem
oveAllCallback) |
| 141 { |
| 142 this._object = object; |
| 143 this._setCallback = setCallback || function() {}; |
| 144 this._removeCallback = removeCallback || function() {}; |
| 145 this._removeAllCallback = removeAllCallback || function() {}; |
| 146 } |
| 147 |
| 148 WebInspector.SettingsStorage.prototype = { |
| 149 /** |
| 150 * @param {string} name |
| 151 * @param {string} value |
| 152 */ |
| 153 set: function(name, value) |
| 154 { |
| 155 this._object[name] = value; |
| 156 this._setCallback(name, value); |
| 157 }, |
| 158 |
| 159 /** |
| 160 * @param {string} name |
| 161 * @return {boolean} |
| 162 */ |
| 163 has: function(name) |
| 164 { |
| 165 return name in this._object; |
| 166 }, |
| 167 |
| 168 /** |
| 169 * @param {string} name |
| 170 * @return {string} |
| 171 */ |
| 172 get: function(name) |
| 173 { |
| 174 return this._object[name]; |
| 175 }, |
| 176 |
| 177 /** |
| 178 * @param {string} name |
| 179 */ |
| 180 remove: function(name) |
| 181 { |
| 182 delete this._object[name]; |
| 183 this._removeCallback(name); |
| 184 }, |
| 185 |
| 186 removeAll: function() |
| 187 { |
| 188 this._object = {}; |
| 189 this._removeAllCallback(); |
| 190 }, |
| 191 |
| 192 _dumpSizes: function() |
| 193 { |
| 194 WebInspector.console.log("Ten largest settings: "); |
| 195 |
| 196 var sizes = { __proto__: null }; |
| 197 for (var key in this._object) |
| 198 sizes[key] = this._object[key].length; |
| 199 var keys = Object.keys(sizes); |
| 200 |
| 201 function comparator(key1, key2) |
| 202 { |
| 203 return sizes[key2] - sizes[key1]; |
| 204 } |
| 205 |
| 206 keys.sort(comparator); |
| 207 |
| 208 for (var i = 0; i < 10 && i < keys.length; ++i) |
| 209 WebInspector.console.log("Setting: '" + keys[i] + "', size: " + size
s[keys[i]]); |
| 210 } |
| 211 } |
| 212 |
| 213 /** |
| 214 * @constructor |
| 135 * @param {!WebInspector.Settings} settings | 215 * @param {!WebInspector.Settings} settings |
| 136 * @param {string} name | 216 * @param {string} name |
| 137 * @param {V} defaultValue | 217 * @param {V} defaultValue |
| 138 * @param {!WebInspector.Object} eventSupport | 218 * @param {!WebInspector.Object} eventSupport |
| 139 * @param {!Object} storage | 219 * @param {!WebInspector.SettingsStorage} storage |
| 140 * @template V | 220 * @template V |
| 141 */ | 221 */ |
| 142 WebInspector.Setting = function(settings, name, defaultValue, eventSupport, stor
age) | 222 WebInspector.Setting = function(settings, name, defaultValue, eventSupport, stor
age) |
| 143 { | 223 { |
| 144 this._settings = settings; | 224 this._settings = settings; |
| 145 this._name = name; | 225 this._name = name; |
| 146 this._defaultValue = defaultValue; | 226 this._defaultValue = defaultValue; |
| 147 this._eventSupport = eventSupport; | 227 this._eventSupport = eventSupport; |
| 148 this._storage = storage; | 228 this._storage = storage; |
| 149 } | 229 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 174 | 254 |
| 175 /** | 255 /** |
| 176 * @return {V} | 256 * @return {V} |
| 177 */ | 257 */ |
| 178 get: function() | 258 get: function() |
| 179 { | 259 { |
| 180 if (typeof this._value !== "undefined") | 260 if (typeof this._value !== "undefined") |
| 181 return this._value; | 261 return this._value; |
| 182 | 262 |
| 183 this._value = this._defaultValue; | 263 this._value = this._defaultValue; |
| 184 if (this._name in this._storage) { | 264 if (this._storage.has(this._name)) { |
| 185 try { | 265 try { |
| 186 this._value = JSON.parse(this._storage[this._name]); | 266 this._value = JSON.parse(this._storage.get(this._name)); |
| 187 } catch(e) { | 267 } catch(e) { |
| 188 this.remove(); | 268 this.remove(); |
| 189 } | 269 } |
| 190 } | 270 } |
| 191 return this._value; | 271 return this._value; |
| 192 }, | 272 }, |
| 193 | 273 |
| 194 /** | 274 /** |
| 195 * @param {V} value | 275 * @param {V} value |
| 196 */ | 276 */ |
| 197 set: function(value) | 277 set: function(value) |
| 198 { | 278 { |
| 199 this._value = value; | 279 this._value = value; |
| 200 try { | 280 try { |
| 201 var settingString = JSON.stringify(value); | 281 var settingString = JSON.stringify(value); |
| 202 try { | 282 try { |
| 203 this._storage[this._name] = settingString; | 283 this._storage.set(this._name, settingString); |
| 204 } catch(e) { | 284 } catch(e) { |
| 205 this._printSettingsSavingError(e.message, this._name, settingStr
ing); | 285 this._printSettingsSavingError(e.message, this._name, settingStr
ing); |
| 206 } | 286 } |
| 207 } catch(e) { | 287 } catch(e) { |
| 208 WebInspector.console.error("Cannot stringify setting with name: " +
this._name + ", error: " + e.message); | 288 WebInspector.console.error("Cannot stringify setting with name: " +
this._name + ", error: " + e.message); |
| 209 } | 289 } |
| 210 this._eventSupport.dispatchEventToListeners(this._name, value); | 290 this._eventSupport.dispatchEventToListeners(this._name, value); |
| 211 }, | 291 }, |
| 212 | 292 |
| 213 remove: function() | 293 remove: function() |
| 214 { | 294 { |
| 215 this._settings._registry.delete(this._name); | 295 this._settings._registry.delete(this._name); |
| 216 this._settings._moduleSettings.delete(this._name); | 296 this._settings._moduleSettings.delete(this._name); |
| 217 delete this._storage[this._name]; | 297 this._storage.remove(this._name); |
| 218 }, | 298 }, |
| 219 | 299 |
| 220 /** | 300 /** |
| 221 * @param {string} message | 301 * @param {string} message |
| 222 * @param {string} name | 302 * @param {string} name |
| 223 * @param {string} value | 303 * @param {string} value |
| 224 */ | 304 */ |
| 225 _printSettingsSavingError: function(message, name, value) | 305 _printSettingsSavingError: function(message, name, value) |
| 226 { | 306 { |
| 227 var errorMessage = "Error saving setting with name: " + this._name + ",
value length: " + value.length + ". Error: " + message; | 307 var errorMessage = "Error saving setting with name: " + this._name + ",
value length: " + value.length + ". Error: " + message; |
| 228 console.error(errorMessage); | 308 console.error(errorMessage); |
| 229 WebInspector.console.error(errorMessage); | 309 WebInspector.console.error(errorMessage); |
| 230 WebInspector.console.log("Ten largest settings: "); | 310 this._storage._dumpSizes(); |
| 231 | |
| 232 var sizes = { __proto__: null }; | |
| 233 for (var key in this._storage) | |
| 234 sizes[key] = this._storage[key].length; | |
| 235 var keys = Object.keys(sizes); | |
| 236 | |
| 237 function comparator(key1, key2) | |
| 238 { | |
| 239 return sizes[key2] - sizes[key1]; | |
| 240 } | |
| 241 | |
| 242 keys.sort(comparator); | |
| 243 | |
| 244 for (var i = 0; i < 10 && i < keys.length; ++i) | |
| 245 WebInspector.console.log("Setting: '" + keys[i] + "', size: " + size
s[keys[i]]); | |
| 246 } | 311 } |
| 247 } | 312 } |
| 248 | 313 |
| 249 /** | 314 /** |
| 250 * @constructor | 315 * @constructor |
| 251 * @extends {WebInspector.Setting} | 316 * @extends {WebInspector.Setting} |
| 252 * @param {!WebInspector.Settings} settings | 317 * @param {!WebInspector.Settings} settings |
| 253 * @param {string} name | 318 * @param {string} name |
| 254 * @param {string} defaultValue | 319 * @param {string} defaultValue |
| 255 * @param {!WebInspector.Object} eventSupport | 320 * @param {!WebInspector.Object} eventSupport |
| 256 * @param {!Object<string, string>} storage | 321 * @param {!WebInspector.SettingsStorage} storage |
| 257 * @param {string=} regexFlags | 322 * @param {string=} regexFlags |
| 258 */ | 323 */ |
| 259 WebInspector.RegExpSetting = function(settings, name, defaultValue, eventSupport
, storage, regexFlags) | 324 WebInspector.RegExpSetting = function(settings, name, defaultValue, eventSupport
, storage, regexFlags) |
| 260 { | 325 { |
| 261 WebInspector.Setting.call(this, settings, name, defaultValue ? [{ pattern: d
efaultValue }] : [], eventSupport, storage); | 326 WebInspector.Setting.call(this, settings, name, defaultValue ? [{ pattern: d
efaultValue }] : [], eventSupport, storage); |
| 262 this._regexFlags = regexFlags; | 327 this._regexFlags = regexFlags; |
| 263 } | 328 } |
| 264 | 329 |
| 265 WebInspector.RegExpSetting.prototype = { | 330 WebInspector.RegExpSetting.prototype = { |
| 266 /** | 331 /** |
| (...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 648 } | 713 } |
| 649 | 714 |
| 650 /** | 715 /** |
| 651 * @param {string} settingName | 716 * @param {string} settingName |
| 652 * @return {!WebInspector.Setting} | 717 * @return {!WebInspector.Setting} |
| 653 */ | 718 */ |
| 654 WebInspector.settingForTest = function(settingName) | 719 WebInspector.settingForTest = function(settingName) |
| 655 { | 720 { |
| 656 return WebInspector.settings.settingForTest(settingName); | 721 return WebInspector.settings.settingForTest(settingName); |
| 657 } | 722 } |
| OLD | NEW |