| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * Class to manage user preferences. |
| 7 * |
| 8 * @constructor |
| 9 */ |
| 10 let SwitchAccessPrefs = function() { |
| 11 /** |
| 12 * User preferences, initially set to the default preference values. |
| 13 * |
| 14 * @private |
| 15 */ |
| 16 this.prefs_ = Object.assign({}, this.DEFAULT_PREFS); |
| 17 this.loadPrefs_(); |
| 18 chrome.storage.onChanged.addListener(this.handleStorageChange_.bind(this)); |
| 19 }; |
| 20 |
| 21 SwitchAccessPrefs.prototype = { |
| 22 /** |
| 23 * Asynchronously load the current preferences from chrome.storage.sync and |
| 24 * store them in this.prefs_. |
| 25 * |
| 26 * @private |
| 27 */ |
| 28 loadPrefs_: function() { |
| 29 let defaultKeys = Object.keys(this.DEFAULT_PREFS); |
| 30 chrome.storage.sync.get(defaultKeys, function(loadedPrefs) { |
| 31 let loadedKeys = Object.keys(loadedPrefs); |
| 32 if (loadedKeys.length > 0) { |
| 33 for (let key of loadedKeys) { |
| 34 this.prefs_[key] = loadedPrefs[key]; |
| 35 } |
| 36 let event = new CustomEvent('prefsUpdate', {'detail': loadedPrefs}); |
| 37 document.dispatchEvent(event); |
| 38 } |
| 39 }.bind(this)); |
| 40 }, |
| 41 |
| 42 /** |
| 43 * Store any changes to chrome.storage.sync in this.prefs_. |
| 44 * |
| 45 * @param {!Object} changes |
| 46 * @param {string} areaName |
| 47 * @private |
| 48 */ |
| 49 handleStorageChange_: function(changes, areaName) { |
| 50 for (let key of Object.keys(changes)) { |
| 51 // If pref change happened on same device, prefs will already be updated, |
| 52 // so this will have no effect. |
| 53 this.prefs_[key] = changes[key].newValue; |
| 54 changes[key] = changes[key].newValue; |
| 55 } |
| 56 |
| 57 let event = new CustomEvent('prefsUpdate', {'detail': changes}); |
| 58 document.dispatchEvent(event); |
| 59 }, |
| 60 |
| 61 /** |
| 62 * Set the value of the preference |key| to |value|. |
| 63 * |
| 64 * @param {string} key |
| 65 * @param {boolean|string|number} value |
| 66 */ |
| 67 setPref: function(key, value) { |
| 68 let pref = {}; |
| 69 pref[key] = value; |
| 70 chrome.storage.sync.set(pref); |
| 71 this.prefs_[key] = value; |
| 72 }, |
| 73 |
| 74 /** |
| 75 * Get all user preferences. |
| 76 */ |
| 77 getPrefs: function() { |
| 78 return Object.assign({}, this.prefs_); |
| 79 }, |
| 80 |
| 81 /** |
| 82 * The default value of all preferences. All preferences should be primitives |
| 83 * to prevent changes to default values. |
| 84 * |
| 85 * @const |
| 86 */ |
| 87 DEFAULT_PREFS: { |
| 88 'enableAutoScan': false, |
| 89 'autoScanTime': .75 |
| 90 } |
| 91 }; |
| OLD | NEW |