OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Class to manage user preferences. | 6 * Class to manage user preferences. |
7 * | 7 * |
8 * @constructor | 8 * @constructor |
9 */ | 9 */ |
10 function SwitchAccessPrefs() { | 10 function SwitchAccessPrefs() { |
11 /** | 11 /** |
12 * User preferences, initially set to the default preference values. | 12 * User preferences, initially set to the default preference values. |
13 * | 13 * |
14 * @private | 14 * @private |
15 */ | 15 */ |
16 this.prefs_ = Object.assign({}, this.DEFAULT_PREFS); | 16 this.prefs_ = Object.assign( |
| 17 {}, this.DEFAULT_COMMAND_KEYCODE_MAP, this.DEFAULT_OTHER_PREFS); |
17 | 18 |
18 this.loadPrefs_(); | 19 this.loadPrefs_(); |
19 chrome.storage.onChanged.addListener(this.handleStorageChange_.bind(this)); | 20 chrome.storage.onChanged.addListener(this.handleStorageChange_.bind(this)); |
20 } | 21 } |
21 | 22 |
22 SwitchAccessPrefs.prototype = { | 23 SwitchAccessPrefs.prototype = { |
23 /** | 24 /** |
24 * Asynchronously load the current preferences from chrome.storage.sync and | 25 * Asynchronously load the current preferences from chrome.storage.sync and |
25 * store them in this.prefs_, if this.prefs_ is not already set to that value. | 26 * store them in this.prefs_, if this.prefs_ is not already set to that value. |
26 * If this.prefs_ changes, fire a prefsUpdate event. | 27 * If this.prefs_ changes, fire a prefsUpdate event. |
27 * | 28 * |
28 * @private | 29 * @private |
29 */ | 30 */ |
30 loadPrefs_: function() { | 31 loadPrefs_: function() { |
31 let defaultKeys = Object.keys(this.DEFAULT_PREFS); | 32 let defaultKeys = Object.keys(this.prefs_); |
32 chrome.storage.sync.get(defaultKeys, function(loadedPrefs) { | 33 chrome.storage.sync.get(defaultKeys, function(loadedPrefs) { |
33 let updatedPrefs = {}; | 34 let updatedPrefs = {}; |
34 for (let key of Object.keys(loadedPrefs)) { | 35 for (let key of Object.keys(loadedPrefs)) { |
35 if (this.prefs_[key] !== loadedPrefs[key]) { | 36 if (this.prefs_[key] !== loadedPrefs[key]) { |
36 this.prefs_[key] = loadedPrefs[key]; | 37 this.prefs_[key] = loadedPrefs[key]; |
37 updatedPrefs[key] = loadedPrefs[key]; | 38 updatedPrefs[key] = loadedPrefs[key]; |
38 } | 39 } |
39 } | 40 } |
40 if (Object.keys(updatedPrefs).length > 0) { | 41 if (Object.keys(updatedPrefs).length > 0) { |
41 let event = new CustomEvent('prefsUpdate', {'detail': updatedPrefs}); | 42 let event = new CustomEvent('prefsUpdate', {'detail': updatedPrefs}); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 */ | 120 */ |
120 getStringPref: function(key) { | 121 getStringPref: function(key) { |
121 let value = this.prefs_[key]; | 122 let value = this.prefs_[key]; |
122 if (typeof value === 'string') | 123 if (typeof value === 'string') |
123 return value; | 124 return value; |
124 else | 125 else |
125 throw new TypeError('No value of string type for key \'' + key + '\''); | 126 throw new TypeError('No value of string type for key \'' + key + '\''); |
126 }, | 127 }, |
127 | 128 |
128 /** | 129 /** |
129 * The default value of all preferences. All preferences should be primitives | 130 * Get a list of the commands that can be run using a keyboard key. |
130 * to prevent changes to default values. | 131 * |
| 132 * @return {!Array<string>} |
| 133 */ |
| 134 getCommands: function() { |
| 135 return Object.keys(this.DEFAULT_COMMAND_KEYCODE_MAP); |
| 136 }, |
| 137 |
| 138 /** |
| 139 * Returns true if |keyCode| is already used to run a command from the |
| 140 * keyboard. |
| 141 * |
| 142 * @param {number} keyCode |
| 143 * @return {boolean} |
| 144 */ |
| 145 keyCodeIsUsed: function(keyCode) { |
| 146 for (let command of this.getCommands()) { |
| 147 if (keyCode === this.prefs_[command]) |
| 148 return true; |
| 149 } |
| 150 return false; |
| 151 }, |
| 152 |
| 153 /** |
| 154 * The default mapping between commands and key codes. |
131 * | 155 * |
132 * @const | 156 * @const |
133 */ | 157 */ |
134 DEFAULT_PREFS: { | 158 DEFAULT_COMMAND_KEYCODE_MAP: { |
| 159 'next': 49, |
| 160 'previous': 50, |
| 161 'select': 51, |
| 162 'options': 52, |
| 163 'debugNext': 54, |
| 164 'debugPrevious': 55, |
| 165 'debugChild': 56, |
| 166 'debugParent': 57 |
| 167 }, |
| 168 |
| 169 /** |
| 170 * The default value of all other preferences. All preferences should be |
| 171 * primitives to prevent changes to default values. |
| 172 * |
| 173 * @const |
| 174 */ |
| 175 DEFAULT_OTHER_PREFS: { |
135 'enableAutoScan': false, | 176 'enableAutoScan': false, |
136 'autoScanTime': 800 | 177 'autoScanTime': 800 |
137 } | 178 } |
138 }; | 179 }; |
OLD | NEW |