| OLD | NEW |
| 1 /* Copyright 2015 The Chromium Authors. All rights reserved. | 1 /* Copyright 2015 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 * @fileoverview | 6 * @fileoverview |
| 7 * 'cr-settings-prefs' is an element which serves as a model for | 7 * 'cr-settings-prefs' is an element which serves as a model for |
| 8 * interaction with settings which are stored in Chrome's | 8 * interaction with settings which are stored in Chrome's |
| 9 * Preferences. | 9 * Preferences. |
| 10 * | 10 * |
| 11 * Example: | 11 * Example: |
| 12 * | 12 * |
| 13 * <cr-settings-prefs id="prefs"></cr-settings-prefs> | 13 * <cr-settings-prefs id="prefs"></cr-settings-prefs> |
| 14 * <cr-settings-a11y-page prefs="{{this.$.prefs}}"></cr-settings-a11y-page> | 14 * <cr-settings-a11y-page prefs="{{this.$.prefs}}"></cr-settings-a11y-page> |
| 15 * | 15 * |
| 16 * @group Chrome Settings Elements | 16 * @group Chrome Settings Elements |
| 17 * @element cr-settings-a11y-page | 17 * @element cr-settings-a11y-page |
| 18 */ | 18 */ |
| 19 Polymer('cr-settings-prefs', { | 19 (function() { |
| 20 publish: { | 20 'use strict'; |
| 21 /** | |
| 22 * Accessibility preferences state. | |
| 23 * | |
| 24 * @attribute settings | |
| 25 * @type CrSettingsPrefs.Settings | |
| 26 * @default null | |
| 27 */ | |
| 28 settings: null, | |
| 29 }, | |
| 30 | |
| 31 /** @override */ | |
| 32 created: function() { | |
| 33 'use strict'; | |
| 34 | |
| 35 this.settings = {}; | |
| 36 this.initializeA11y_(); | |
| 37 this.initializeDownloads_(); | |
| 38 var observer = new ObjectObserver(this.settings); | |
| 39 observer.open(this.propertyChangeCallback_.bind(this, 'settings')); | |
| 40 | |
| 41 // For all Object properties of settings, create an ObjectObserver. | |
| 42 for (let key in this.settings) { | |
| 43 if (typeof this.settings[key] == 'object') { | |
| 44 let keyObserver = new ObjectObserver(this.settings[key]); | |
| 45 keyObserver.open( | |
| 46 this.propertyChangeCallback_.bind(this, 'settings.' + key)); | |
| 47 } | |
| 48 } | |
| 49 }, | |
| 50 | 21 |
| 51 /** | 22 /** |
| 52 * Initializes some defaults for the a11y settings. | 23 * A list of all pref paths used in the UI. |
| 53 * @private | 24 * TODO(jlklein): This is a temporary workaround that needs to be removed |
| 25 * once settingsPrivate is implemented with the fetchAll function. We will |
| 26 * not need to tell the settingsPrivate API which prefs to fetch. |
| 27 * @const {!Array<string>} |
| 54 */ | 28 */ |
| 55 initializeA11y_: function() { | 29 var PREFS_TO_FECTH = [ |
| 56 this.settings.a11y = { | 30 'settings.a11y.enableMenu', |
| 57 enableMenu: true, | 31 'settings.a11y.largeCursorEnabled', |
| 58 largeCursorEnabled: false, | 32 'settings.a11y.highContrastEnabled', |
| 59 highContrastEnabled: false, | 33 'settings.a11y.stickyKeysEnabled', |
| 60 stickyKeysEnabled: false, | 34 'settings.accessibility', |
| 61 screenMagnifier: false, | 35 'settings.a11y.screenMagnifier', |
| 62 enableTapDragging: false, | 36 'settings.touchpad.enableTapDragging', |
| 63 autoclick: false, | 37 'settings.a11y.autoclick', |
| 64 autoclickDelayMs: 200, | 38 'settings.a11y.autoclickDelayMs', |
| 65 virtualKeyboard: false, | 39 'download.default_directory', |
| 66 }; | 40 'download.prompt_for_download', |
| 41 ]; |
| 67 | 42 |
| 68 this.settings.touchpad = { | 43 Polymer('cr-settings-prefs', { |
| 69 enableTapDragging: false, | 44 publish: { |
| 70 }; | 45 /** |
| 46 * Accessibility preferences state. |
| 47 * |
| 48 * @attribute settings |
| 49 * @type CrSettingsPrefs.Settings |
| 50 * @default null |
| 51 */ |
| 52 settings: null, |
| 53 }, |
| 71 | 54 |
| 72 // ChromeVox is enbaled/disabled via the 'settings.accessibility' boolean | 55 /** @override */ |
| 73 // pref. | 56 created: function() { |
| 74 this.settings.accessibility = false; | 57 this.settings = {}; |
| 58 this.fetchSettings_(); |
| 59 var observer = new ObjectObserver(this.settings); |
| 60 observer.open(this.propertyChangeCallback_.bind(this, 'settings')); |
| 61 }, |
| 75 | 62 |
| 76 // TODO(jlklein): Actually pull the data out of prefs and initialize. | 63 /** |
| 77 }, | 64 * Fetches all settings from settingsPrivate. |
| 65 * TODO(jlklein): Implement using settingsPrivate when it's available. |
| 66 * @private |
| 67 */ |
| 68 fetchSettings_: function() { |
| 69 // *Sigh* We need to export the function name to global scope. |
| 70 var callbackName = 'CrSettingsPrefs_onPrefsFetched'; |
| 71 window[callbackName] = this.onPrefsFetched_.bind(this); |
| 72 chrome.send('fetchPrefs', [callbackName].concat(PREFS_TO_FECTH)); |
| 73 }, |
| 78 | 74 |
| 79 /** | 75 /** |
| 80 * Initializes some defaults for the downloads settings. | 76 * Fetches all settings from settingsPrivate. |
| 81 * @private | 77 * @param {!Object} dict Map of fetched property values. |
| 82 */ | 78 * @private |
| 83 initializeDownloads_: function() { | 79 */ |
| 84 this.settings.download = { | 80 onPrefsFetched_: function(dict) { |
| 85 downloadLocation: '', | 81 this.parsePrefDict_('', dict); |
| 86 promptForDownload: false, | 82 }, |
| 87 }; | |
| 88 }, | |
| 89 | 83 |
| 90 /** | 84 /** |
| 91 * @param {string} propertyPath The path before the property names. | 85 * Helper function for parsing the prefs dict and constructing Preference |
| 92 * @param {!Array<string>} added An array of keys which were added. | 86 * objects. |
| 93 * @param {!Array<string>} removed An array of keys which were removed. | 87 * @param {string} prefix The namespace prefix of the pref. |
| 94 * @param {!Array<string>} changed An array of keys of properties whose | 88 * @param {!Object} dict Map with preference values. |
| 95 * values changed. | 89 * @private |
| 96 * @param {function(string) : *} getOldValueFn A function which takes a | 90 */ |
| 97 * property name and returns the old value for that property. | 91 parsePrefDict_: function(prefix, dict) { |
| 98 * @private | 92 for (let prefName in dict) { |
| 99 */ | 93 let value = dict[prefName]; |
| 100 propertyChangeCallback_: function( | 94 if (!this.isPrefObject_(value)) { |
| 101 propertyPath, added, removed, changed, getOldValueFn) { | 95 this.parsePrefDict_(prefix + prefName + '.', value); |
| 102 Object.keys(changed).forEach(function(property) { | 96 continue; |
| 103 console.log( | 97 } |
| 104 `${propertyPath}.${property}`, | |
| 105 `old : ${getOldValueFn(property)}`, | |
| 106 `newValue : ${changed[property]}`); | |
| 107 | 98 |
| 108 // TODO(jlklein): Actually set the changed property back to prefs. | 99 // value is a pref object. Construct the path to pref using prefix, |
| 109 }); | 100 // add the pref to this.settings, and observe changes. |
| 110 }, | 101 let root = this.settings; |
| 111 }); | 102 let pathPieces = prefix.slice(0, -1).split('.'); |
| 103 for (let i in pathPieces) { |
| 104 root[pathPieces[i]] = root[pathPieces[i]] || {}; |
| 105 root = root[pathPieces[i]]; |
| 106 } |
| 107 |
| 108 root[prefName] = value; |
| 109 let keyObserver = new ObjectObserver(value); |
| 110 keyObserver.open( |
| 111 this.propertyChangeCallback_.bind(this, prefix + prefName)); |
| 112 } |
| 113 }, |
| 114 |
| 115 /** |
| 116 * Determines whether the passed object is a pref object from Chrome. |
| 117 * @param {*} rawPref The object to check. |
| 118 * @return {boolean} True if the passes object is a pref. |
| 119 * @private |
| 120 */ |
| 121 isPrefObject_: function(rawPref) { |
| 122 return rawPref && typeof rawPref == 'object' && |
| 123 rawPref.hasOwnProperty('value') && |
| 124 rawPref.hasOwnProperty('disabled'); |
| 125 }, |
| 126 |
| 127 /** |
| 128 * Called when a property of a pref changes. |
| 129 * @param {string} propertyPath The path before the property names. |
| 130 * @param {!Array<string>} added An array of keys which were added. |
| 131 * @param {!Array<string>} removed An array of keys which were removed. |
| 132 * @param {!Array<string>} changed An array of keys of properties whose |
| 133 * values changed. |
| 134 * @param {function(string) : *} getOldValueFn A function which takes a |
| 135 * property name and returns the old value for that property. |
| 136 * @private |
| 137 */ |
| 138 propertyChangeCallback_: function( |
| 139 propertyPath, added, removed, changed, getOldValueFn) { |
| 140 for (let property in changed) { |
| 141 // UI should only be able to change the value of a setting for now, not |
| 142 // disabled, etc. |
| 143 assert(property == 'value'); |
| 144 |
| 145 let newValue = changed[property]; |
| 146 switch (typeof newValue) { |
| 147 case 'boolean': |
| 148 this.setBooleanPref_( |
| 149 propertyPath, /** @type {boolean} */ (newValue)); |
| 150 break; |
| 151 case 'number': |
| 152 this.setNumberPref_( |
| 153 propertyPath, /** @type {number} */ (newValue)); |
| 154 break; |
| 155 case 'string': |
| 156 this.setStringPref_( |
| 157 propertyPath, /** @type {string} */ (newValue)); |
| 158 break; |
| 159 case 'object': |
| 160 assertInstanceof(newValue, Array); |
| 161 this.setArrayPref_( |
| 162 propertyPath, /** @type {!Array} */ (newValue)); |
| 163 } |
| 164 } |
| 165 }, |
| 166 |
| 167 /** |
| 168 * @param {string} propertyPath The path before the property names. |
| 169 * @param {boolean} value The new value of the pref. |
| 170 * @private |
| 171 */ |
| 172 setBooleanPref_: function(propertyPath, value) { |
| 173 chrome.send('setBooleanPref', [propertyPath, value]); |
| 174 }, |
| 175 |
| 176 /** |
| 177 * @param {string} propertyPath The path before the property names. |
| 178 * @param {string} value The new value of the pref. |
| 179 * @private |
| 180 */ |
| 181 setStringPref_: function(propertyPath, value) { |
| 182 chrome.send('setStringPref', [propertyPath, value]); |
| 183 }, |
| 184 |
| 185 /** |
| 186 * @param {string} propertyPath The path before the property names. |
| 187 * @param {number} value The new value of the pref. |
| 188 * @private |
| 189 */ |
| 190 setNumberPref_: function(propertyPath, value) { |
| 191 var setFn = (value % 1 == 0) ? 'setIntegerPref' : 'setDoublePref'; |
| 192 chrome.send(setFn, [propertyPath, value]); |
| 193 }, |
| 194 |
| 195 /** |
| 196 * @param {string} propertyPath The path before the property names. |
| 197 * @param {number} value The new value of the pref. |
| 198 * @private |
| 199 */ |
| 200 setArrayPref_: function(propertyPath, value) { |
| 201 chrome.send('setListPref', [propertyPath, JSON.stringify(value)]); |
| 202 }, |
| 203 }); |
| 204 })(); |
| OLD | NEW |