| 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 */ |
| 139 WebInspector.SettingsStorage = function(object, setCallback, removeCallback) |
| 140 { |
| 141 this._object = object; |
| 142 this._setCallback = setCallback || function() {}; |
| 143 this._removeCallback = removeCallback || function() {}; |
| 144 } |
| 145 |
| 146 WebInspector.SettingsStorage.prototype = { |
| 147 /** |
| 148 * @param {string} name |
| 149 * @param {string} value |
| 150 */ |
| 151 set: function(name, value) |
| 152 { |
| 153 this._object[name] = value; |
| 154 this._setCallback(name, value); |
| 155 }, |
| 156 |
| 157 /** |
| 158 * @param {string} name |
| 159 * @return {boolean} |
| 160 */ |
| 161 has: function(name) |
| 162 { |
| 163 return name in this._object; |
| 164 }, |
| 165 |
| 166 /** |
| 167 * @param {string} name |
| 168 * @return {string} |
| 169 */ |
| 170 get: function(name) |
| 171 { |
| 172 return this._object[name]; |
| 173 }, |
| 174 |
| 175 /** |
| 176 * @param {string} name |
| 177 */ |
| 178 remove: function(name) |
| 179 { |
| 180 delete this._object[name]; |
| 181 this._removeCallback(name); |
| 182 }, |
| 183 |
| 184 removeAll: function() |
| 185 { |
| 186 for (var name in this._object) |
| 187 this.remove(name); |
| 188 }, |
| 189 |
| 190 _dumpSizes: function() |
| 191 { |
| 192 WebInspector.console.log("Ten largest settings: "); |
| 193 |
| 194 var sizes = { __proto__: null }; |
| 195 for (var key in this._object) |
| 196 sizes[key] = this._object[key].length; |
| 197 var keys = Object.keys(sizes); |
| 198 |
| 199 function comparator(key1, key2) |
| 200 { |
| 201 return sizes[key2] - sizes[key1]; |
| 202 } |
| 203 |
| 204 keys.sort(comparator); |
| 205 |
| 206 for (var i = 0; i < 10 && i < keys.length; ++i) |
| 207 WebInspector.console.log("Setting: '" + keys[i] + "', size: " + size
s[keys[i]]); |
| 208 } |
| 209 } |
| 210 |
| 211 /** |
| 212 * @constructor |
| 135 * @param {!WebInspector.Settings} settings | 213 * @param {!WebInspector.Settings} settings |
| 136 * @param {string} name | 214 * @param {string} name |
| 137 * @param {V} defaultValue | 215 * @param {V} defaultValue |
| 138 * @param {!WebInspector.Object} eventSupport | 216 * @param {!WebInspector.Object} eventSupport |
| 139 * @param {!Object} storage | 217 * @param {!WebInspector.SettingsStorage} storage |
| 140 * @template V | 218 * @template V |
| 141 */ | 219 */ |
| 142 WebInspector.Setting = function(settings, name, defaultValue, eventSupport, stor
age) | 220 WebInspector.Setting = function(settings, name, defaultValue, eventSupport, stor
age) |
| 143 { | 221 { |
| 144 this._settings = settings; | 222 this._settings = settings; |
| 145 this._name = name; | 223 this._name = name; |
| 146 this._defaultValue = defaultValue; | 224 this._defaultValue = defaultValue; |
| 147 this._eventSupport = eventSupport; | 225 this._eventSupport = eventSupport; |
| 148 this._storage = storage; | 226 this._storage = storage; |
| 149 } | 227 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 174 | 252 |
| 175 /** | 253 /** |
| 176 * @return {V} | 254 * @return {V} |
| 177 */ | 255 */ |
| 178 get: function() | 256 get: function() |
| 179 { | 257 { |
| 180 if (typeof this._value !== "undefined") | 258 if (typeof this._value !== "undefined") |
| 181 return this._value; | 259 return this._value; |
| 182 | 260 |
| 183 this._value = this._defaultValue; | 261 this._value = this._defaultValue; |
| 184 if (this._name in this._storage) { | 262 if (this._storage.has(this._name)) { |
| 185 try { | 263 try { |
| 186 this._value = JSON.parse(this._storage[this._name]); | 264 this._value = JSON.parse(this._storage.get(this._name)); |
| 187 } catch(e) { | 265 } catch(e) { |
| 188 this.remove(); | 266 this.remove(); |
| 189 } | 267 } |
| 190 } | 268 } |
| 191 return this._value; | 269 return this._value; |
| 192 }, | 270 }, |
| 193 | 271 |
| 194 /** | 272 /** |
| 195 * @param {V} value | 273 * @param {V} value |
| 196 */ | 274 */ |
| 197 set: function(value) | 275 set: function(value) |
| 198 { | 276 { |
| 199 this._value = value; | 277 this._value = value; |
| 200 try { | 278 try { |
| 201 var settingString = JSON.stringify(value); | 279 var settingString = JSON.stringify(value); |
| 202 try { | 280 try { |
| 203 this._storage[this._name] = settingString; | 281 this._storage.set(this._name, settingString); |
| 204 } catch(e) { | 282 } catch(e) { |
| 205 this._printSettingsSavingError(e.message, this._name, settingStr
ing); | 283 this._printSettingsSavingError(e.message, this._name, settingStr
ing); |
| 206 } | 284 } |
| 207 } catch(e) { | 285 } catch(e) { |
| 208 WebInspector.console.error("Cannot stringify setting with name: " +
this._name + ", error: " + e.message); | 286 WebInspector.console.error("Cannot stringify setting with name: " +
this._name + ", error: " + e.message); |
| 209 } | 287 } |
| 210 this._eventSupport.dispatchEventToListeners(this._name, value); | 288 this._eventSupport.dispatchEventToListeners(this._name, value); |
| 211 }, | 289 }, |
| 212 | 290 |
| 213 remove: function() | 291 remove: function() |
| 214 { | 292 { |
| 215 this._settings._registry.delete(this._name); | 293 this._settings._registry.delete(this._name); |
| 216 this._settings._moduleSettings.delete(this._name); | 294 this._settings._moduleSettings.delete(this._name); |
| 217 delete this._storage[this._name]; | 295 this._storage.remove(this._name); |
| 218 }, | 296 }, |
| 219 | 297 |
| 220 /** | 298 /** |
| 221 * @param {string} message | 299 * @param {string} message |
| 222 * @param {string} name | 300 * @param {string} name |
| 223 * @param {string} value | 301 * @param {string} value |
| 224 */ | 302 */ |
| 225 _printSettingsSavingError: function(message, name, value) | 303 _printSettingsSavingError: function(message, name, value) |
| 226 { | 304 { |
| 227 var errorMessage = "Error saving setting with name: " + this._name + ",
value length: " + value.length + ". Error: " + message; | 305 var errorMessage = "Error saving setting with name: " + this._name + ",
value length: " + value.length + ". Error: " + message; |
| 228 console.error(errorMessage); | 306 console.error(errorMessage); |
| 229 WebInspector.console.error(errorMessage); | 307 WebInspector.console.error(errorMessage); |
| 230 WebInspector.console.log("Ten largest settings: "); | 308 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 } | 309 } |
| 247 } | 310 } |
| 248 | 311 |
| 249 /** | 312 /** |
| 250 * @constructor | 313 * @constructor |
| 251 * @extends {WebInspector.Setting} | 314 * @extends {WebInspector.Setting} |
| 252 * @param {!WebInspector.Settings} settings | 315 * @param {!WebInspector.Settings} settings |
| 253 * @param {string} name | 316 * @param {string} name |
| 254 * @param {string} defaultValue | 317 * @param {string} defaultValue |
| 255 * @param {!WebInspector.Object} eventSupport | 318 * @param {!WebInspector.Object} eventSupport |
| 256 * @param {!Object<string, string>} storage | 319 * @param {!WebInspector.SettingsStorage} storage |
| 257 * @param {string=} regexFlags | 320 * @param {string=} regexFlags |
| 258 */ | 321 */ |
| 259 WebInspector.RegExpSetting = function(settings, name, defaultValue, eventSupport
, storage, regexFlags) | 322 WebInspector.RegExpSetting = function(settings, name, defaultValue, eventSupport
, storage, regexFlags) |
| 260 { | 323 { |
| 261 WebInspector.Setting.call(this, settings, name, defaultValue ? [{ pattern: d
efaultValue }] : [], eventSupport, storage); | 324 WebInspector.Setting.call(this, settings, name, defaultValue ? [{ pattern: d
efaultValue }] : [], eventSupport, storage); |
| 262 this._regexFlags = regexFlags; | 325 this._regexFlags = regexFlags; |
| 263 } | 326 } |
| 264 | 327 |
| 265 WebInspector.RegExpSetting.prototype = { | 328 WebInspector.RegExpSetting.prototype = { |
| 266 /** | 329 /** |
| (...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 648 } | 711 } |
| 649 | 712 |
| 650 /** | 713 /** |
| 651 * @param {string} settingName | 714 * @param {string} settingName |
| 652 * @return {!WebInspector.Setting} | 715 * @return {!WebInspector.Setting} |
| 653 */ | 716 */ |
| 654 WebInspector.settingForTest = function(settingName) | 717 WebInspector.settingForTest = function(settingName) |
| 655 { | 718 { |
| 656 return WebInspector.settings.settingForTest(settingName); | 719 return WebInspector.settings.settingForTest(settingName); |
| 657 } | 720 } |
| OLD | NEW |