Chromium Code Reviews| 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 46 /** @type {!Map<string, !Common.Setting>} */ | 46 /** @type {!Map<string, !Common.Setting>} */ |
| 47 this._moduleSettings = new Map(); | 47 this._moduleSettings = new Map(); |
| 48 self.runtime.extensions('setting').forEach(this._registerModuleSetting.bind( this)); | 48 self.runtime.extensions('setting').forEach(this._registerModuleSetting.bind( this)); |
| 49 } | 49 } |
| 50 | 50 |
| 51 /** | 51 /** |
| 52 * @param {!Runtime.Extension} extension | 52 * @param {!Runtime.Extension} extension |
| 53 */ | 53 */ |
| 54 _registerModuleSetting(extension) { | 54 _registerModuleSetting(extension) { |
| 55 var descriptor = extension.descriptor(); | 55 var descriptor = extension.descriptor(); |
| 56 var title = descriptor['title']; | |
| 56 var settingName = descriptor['settingName']; | 57 var settingName = descriptor['settingName']; |
| 57 var settingType = descriptor['settingType']; | 58 var settingType = descriptor['settingType']; |
| 58 var defaultValue = descriptor['defaultValue']; | 59 var defaultValue = descriptor['defaultValue']; |
| 59 var isLocal = !!descriptor['local']; | 60 var isLocal = !!descriptor['local']; |
| 60 var setting = settingType === 'regex' ? this.createRegExpSetting(settingName , defaultValue, undefined, isLocal) : | 61 var isRegex = settingType === 'regex'; |
| 61 this.createSetting(settingName, defa ultValue, isLocal); | 62 var setting = isRegex ? this.createRegExpSetting(settingName, defaultValue, undefined, isLocal, title) : |
| 63 this.createSetting(settingName, defaultValue, isLoca l, title); | |
| 62 this._moduleSettings.set(settingName, setting); | 64 this._moduleSettings.set(settingName, setting); |
| 63 } | 65 } |
| 64 | 66 |
| 65 /** | 67 /** |
| 66 * @param {string} settingName | 68 * @param {string} settingName |
| 67 * @return {!Common.Setting} | 69 * @return {!Common.Setting} |
| 68 */ | 70 */ |
| 69 moduleSetting(settingName) { | 71 moduleSetting(settingName) { |
| 70 var setting = this._moduleSettings.get(settingName); | 72 var setting = this._moduleSettings.get(settingName); |
| 71 if (!setting) | 73 if (!setting) |
| 72 throw new Error('No setting registered: ' + settingName); | 74 throw new Error('No setting registered: ' + settingName); |
| 73 return setting; | 75 return setting; |
| 74 } | 76 } |
| 75 | 77 |
| 76 /** | 78 /** |
| 77 * @param {string} settingName | 79 * @param {string} settingName |
| 78 * @return {!Common.Setting} | 80 * @return {!Common.Setting} |
| 79 */ | 81 */ |
| 80 settingForTest(settingName) { | 82 settingForTest(settingName) { |
| 81 var setting = this._registry.get(settingName); | 83 var setting = this._registry.get(settingName); |
| 82 if (!setting) | 84 if (!setting) |
| 83 throw new Error('No setting registered: ' + settingName); | 85 throw new Error('No setting registered: ' + settingName); |
| 84 return setting; | 86 return setting; |
| 85 } | 87 } |
| 86 | 88 |
| 87 /** | 89 /** |
| 88 * @param {string} key | 90 * @param {string} key |
| 89 * @param {*} defaultValue | 91 * @param {*} defaultValue |
| 90 * @param {boolean=} isLocal | 92 * @param {boolean=} isLocal |
| 93 * @param {string=} title | |
| 91 * @return {!Common.Setting} | 94 * @return {!Common.Setting} |
| 92 */ | 95 */ |
| 93 createSetting(key, defaultValue, isLocal) { | 96 createSetting(key, defaultValue, isLocal, title) { |
|
pfeldman
2017/02/07 20:10:28
Unfortunately, this makes all the users that want
luoe
2017/02/13 06:27:19
Done.
| |
| 94 if (!this._registry.get(key)) { | 97 if (!this._registry.get(key)) { |
| 95 this._registry.set( | 98 var setting = new Common.Setting( |
| 96 key, new Common.Setting( | 99 this, key, defaultValue, this._eventSupport, isLocal ? this._localStor age : this._settingsStorage, title); |
| 97 this, key, defaultValue, this._eventSupport, isLocal ? this._ localStorage : this._settingsStorage)); | 100 this._registry.set(key, setting); |
| 98 } | 101 } |
| 99 return /** @type {!Common.Setting} */ (this._registry.get(key)); | 102 return /** @type {!Common.Setting} */ (this._registry.get(key)); |
| 100 } | 103 } |
| 101 | 104 |
| 102 /** | 105 /** |
| 103 * @param {string} key | 106 * @param {string} key |
| 104 * @param {*} defaultValue | 107 * @param {*} defaultValue |
| 108 * @param {string=} title | |
| 105 * @return {!Common.Setting} | 109 * @return {!Common.Setting} |
| 106 */ | 110 */ |
| 107 createLocalSetting(key, defaultValue) { | 111 createLocalSetting(key, defaultValue, title) { |
| 108 return this.createSetting(key, defaultValue, true); | 112 return this.createSetting(key, defaultValue, true, title); |
| 109 } | 113 } |
| 110 | 114 |
| 111 /** | 115 /** |
| 112 * @param {string} key | 116 * @param {string} key |
| 113 * @param {string} defaultValue | 117 * @param {string} defaultValue |
| 114 * @param {string=} regexFlags | 118 * @param {string=} regexFlags |
| 115 * @param {boolean=} isLocal | 119 * @param {boolean=} isLocal |
| 120 * @param {string=} title | |
| 116 * @return {!Common.RegExpSetting} | 121 * @return {!Common.RegExpSetting} |
| 117 */ | 122 */ |
| 118 createRegExpSetting(key, defaultValue, regexFlags, isLocal) { | 123 createRegExpSetting(key, defaultValue, regexFlags, isLocal, title) { |
| 119 if (!this._registry.get(key)) { | 124 if (!this._registry.get(key)) { |
| 120 this._registry.set( | 125 var setting = new Common.RegExpSetting( |
| 121 key, new Common.RegExpSetting( | 126 this, key, defaultValue, this._eventSupport, isLocal ? this._localStor age : this._settingsStorage, regexFlags, |
| 122 this, key, defaultValue, this._eventSupport, isLocal ? this._ localStorage : this._settingsStorage, | 127 title); |
| 123 regexFlags)); | 128 this._registry.set(key, setting); |
| 124 } | 129 } |
| 125 return /** @type {!Common.RegExpSetting} */ (this._registry.get(key)); | 130 return /** @type {!Common.RegExpSetting} */ (this._registry.get(key)); |
| 126 } | 131 } |
| 127 | 132 |
| 128 clearAll() { | 133 clearAll() { |
| 129 this._settingsStorage.removeAll(); | 134 this._settingsStorage.removeAll(); |
| 130 this._localStorage.removeAll(); | 135 this._localStorage.removeAll(); |
| 131 var versionSetting = Common.settings.createSetting(Common.VersionController. _currentVersionName, 0); | 136 var versionSetting = Common.settings.createSetting(Common.VersionController. _currentVersionName, 0); |
| 132 versionSetting.set(Common.VersionController.currentVersion); | 137 versionSetting.set(Common.VersionController.currentVersion); |
| 133 } | 138 } |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 217 * @template V | 222 * @template V |
| 218 * @unrestricted | 223 * @unrestricted |
| 219 */ | 224 */ |
| 220 Common.Setting = class { | 225 Common.Setting = class { |
| 221 /** | 226 /** |
| 222 * @param {!Common.Settings} settings | 227 * @param {!Common.Settings} settings |
| 223 * @param {string} name | 228 * @param {string} name |
| 224 * @param {V} defaultValue | 229 * @param {V} defaultValue |
| 225 * @param {!Common.Object} eventSupport | 230 * @param {!Common.Object} eventSupport |
| 226 * @param {!Common.SettingsStorage} storage | 231 * @param {!Common.SettingsStorage} storage |
| 232 * @param {string=} title | |
| 227 */ | 233 */ |
| 228 constructor(settings, name, defaultValue, eventSupport, storage) { | 234 constructor(settings, name, defaultValue, eventSupport, storage, title) { |
| 229 this._settings = settings; | 235 this._settings = settings; |
| 230 this._name = name; | 236 this._name = name; |
| 231 this._defaultValue = defaultValue; | 237 this._defaultValue = defaultValue; |
| 232 this._eventSupport = eventSupport; | 238 this._eventSupport = eventSupport; |
| 233 this._storage = storage; | 239 this._storage = storage; |
| 240 this._title = title; | |
| 234 } | 241 } |
| 235 | 242 |
| 236 /** | 243 /** |
| 237 * @param {function(!Common.Event)} listener | 244 * @param {function(!Common.Event)} listener |
| 238 * @param {!Object=} thisObject | 245 * @param {!Object=} thisObject |
| 239 */ | 246 */ |
| 240 addChangeListener(listener, thisObject) { | 247 addChangeListener(listener, thisObject) { |
| 241 this._eventSupport.addEventListener(this._name, listener, thisObject); | 248 this._eventSupport.addEventListener(this._name, listener, thisObject); |
| 242 } | 249 } |
| 243 | 250 |
| 244 /** | 251 /** |
| 245 * @param {function(!Common.Event)} listener | 252 * @param {function(!Common.Event)} listener |
| 246 * @param {!Object=} thisObject | 253 * @param {!Object=} thisObject |
| 247 */ | 254 */ |
| 248 removeChangeListener(listener, thisObject) { | 255 removeChangeListener(listener, thisObject) { |
| 249 this._eventSupport.removeEventListener(this._name, listener, thisObject); | 256 this._eventSupport.removeEventListener(this._name, listener, thisObject); |
| 250 } | 257 } |
| 251 | 258 |
| 252 get name() { | 259 get name() { |
| 253 return this._name; | 260 return this._name; |
| 254 } | 261 } |
| 255 | 262 |
| 256 /** | 263 /** |
| 264 * @return {string|undefined} | |
| 265 */ | |
| 266 title() { | |
| 267 return this._title; | |
| 268 } | |
| 269 | |
| 270 /** | |
| 257 * @return {V} | 271 * @return {V} |
| 258 */ | 272 */ |
| 259 get() { | 273 get() { |
| 260 if (typeof this._value !== 'undefined') | 274 if (typeof this._value !== 'undefined') |
| 261 return this._value; | 275 return this._value; |
| 262 | 276 |
| 263 this._value = this._defaultValue; | 277 this._value = this._defaultValue; |
| 264 if (this._storage.has(this._name)) { | 278 if (this._storage.has(this._name)) { |
| 265 try { | 279 try { |
| 266 this._value = JSON.parse(this._storage.get(this._name)); | 280 this._value = JSON.parse(this._storage.get(this._name)); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 313 * @unrestricted | 327 * @unrestricted |
| 314 */ | 328 */ |
| 315 Common.RegExpSetting = class extends Common.Setting { | 329 Common.RegExpSetting = class extends Common.Setting { |
| 316 /** | 330 /** |
| 317 * @param {!Common.Settings} settings | 331 * @param {!Common.Settings} settings |
| 318 * @param {string} name | 332 * @param {string} name |
| 319 * @param {string} defaultValue | 333 * @param {string} defaultValue |
| 320 * @param {!Common.Object} eventSupport | 334 * @param {!Common.Object} eventSupport |
| 321 * @param {!Common.SettingsStorage} storage | 335 * @param {!Common.SettingsStorage} storage |
| 322 * @param {string=} regexFlags | 336 * @param {string=} regexFlags |
| 337 * @param {string=} title | |
| 323 */ | 338 */ |
| 324 constructor(settings, name, defaultValue, eventSupport, storage, regexFlags) { | 339 constructor(settings, name, defaultValue, eventSupport, storage, regexFlags, t itle) { |
| 325 super(settings, name, defaultValue ? [{pattern: defaultValue}] : [], eventSu pport, storage); | 340 super(settings, name, defaultValue ? [{pattern: defaultValue}] : [], eventSu pport, storage, title); |
| 326 this._regexFlags = regexFlags; | 341 this._regexFlags = regexFlags; |
| 327 } | 342 } |
| 328 | 343 |
| 329 /** | 344 /** |
| 330 * @override | 345 * @override |
| 331 * @return {string} | 346 * @return {string} |
| 332 */ | 347 */ |
| 333 get() { | 348 get() { |
| 334 var result = []; | 349 var result = []; |
| 335 var items = this.getAsArray(); | 350 var items = this.getAsArray(); |
| (...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 765 return Common.settings.moduleSetting(settingName); | 780 return Common.settings.moduleSetting(settingName); |
| 766 }; | 781 }; |
| 767 | 782 |
| 768 /** | 783 /** |
| 769 * @param {string} settingName | 784 * @param {string} settingName |
| 770 * @return {!Common.Setting} | 785 * @return {!Common.Setting} |
| 771 */ | 786 */ |
| 772 Common.settingForTest = function(settingName) { | 787 Common.settingForTest = function(settingName) { |
| 773 return Common.settings.settingForTest(settingName); | 788 return Common.settings.settingForTest(settingName); |
| 774 }; | 789 }; |
| OLD | NEW |