Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(507)

Side by Side Diff: chrome/browser/resources/settings/prefs/prefs.js

Issue 1019403002: Fetch and actually set prefs (using chrome.send for now). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: One more round of michael comments. Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 /** 21
22 * Accessibility preferences state. 22 /**
23 * 23 * A list of all pref paths used on all platforms in the UI.
24 * @attribute settings 24 * TODO(jlklein): This is a temporary workaround that needs to be removed
25 * @type CrSettingsPrefs.Settings 25 * once settingsPrivate is implemented with the fetchAll function. We will
26 * @default null 26 * not need to tell the settingsPrivate API which prefs to fetch.
27 */ 27 * @const {!Array<string>}
28 settings: null, 28 */
29 }, 29 var PREFS_TO_FECTH = [
Oren Blasberg 2015/03/20 01:06:53 spelling: FETCH
Jeremy Klein 2015/03/20 01:11:05 Done.
30 30 'download.default_directory',
31 /** @override */ 31 'download.prompt_for_download',
32 created: function() { 32 ];
33 'use strict'; 33
34 34 /**
35 this.settings = {}; 35 * A list of all CrOS-only pref paths used in the UI.
36 this.initializeA11y_(); 36 * TODO(jlklein): This is a temporary workaround that needs to be removed
37 this.initializeDownloads_(); 37 * once settingsPrivate is implemented with the fetchAll function. We will
38 var observer = new ObjectObserver(this.settings); 38 * not need to tell the settingsPrivate API which prefs to fetch.
39 observer.open(this.propertyChangeCallback_.bind(this, 'settings')); 39 * @const {!Array<string>}
40 40 */
41 // For all Object properties of settings, create an ObjectObserver. 41 var CROS_ONLY_PREFS = [
42 for (let key in this.settings) { 42 'settings.accessibility',
43 if (typeof this.settings[key] == 'object') { 43 'settings.a11y.autoclick',
44 let keyObserver = new ObjectObserver(this.settings[key]); 44 'settings.a11y.autoclick_delay_ms',
45 'settings.a11y.enable_menu',
46 'settings.a11y.high_contrast_enabled',
47 'settings.a11y.large_cursor_enabled',
48 'settings.a11y.screen_magnifier',
49 'settings.a11y.sticky_keys_enabled',
50 'settings.a11y.virtual_keyboard',
51 'settings.touchpad.enable_tap_dragging',
52 ];
53
54 Polymer('cr-settings-prefs', {
55 publish: {
56 /**
57 * Object containing all preferences.
58 *
59 * @attribute settings
60 * @type CrSettingsPrefs.Settings
61 * @default null
62 */
63 settings: null,
64 },
65
66 /** @override */
67 created: function() {
68 this.settings = {};
69 this.fetchSettings_();
70 },
71
72 /**
73 * Fetches all settings from settingsPrivate.
74 * TODO(jlklein): Implement using settingsPrivate when it's available.
75 * @private
76 */
77 fetchSettings_: function() {
78 // *Sigh* We need to export the function name to global scope.
Oren Blasberg 2015/03/20 01:06:53 In a comment, explain why (it's temporary b/c opti
Jeremy Klein 2015/03/20 01:11:05 Done.
79 var callbackName = 'CrSettingsPrefs_onPrefsFetched';
80 window[callbackName] = this.onPrefsFetched_.bind(this);
81 var prefsToFetch = PREFS_TO_FECTH;
82 if (cr.isChromeOS)
83 prefsToFetch.concat(CROS_ONLY_PREFS);
84
85 chrome.send('fetchPrefs', [callbackName].concat(prefsToFetch));
86 },
87
88 /**
89 * Fetches all settings from settingsPrivate.
90 * @param {!Object} dict Map of fetched property values.
91 * @private
92 */
93 onPrefsFetched_: function(dict) {
94 this.parsePrefDict_('', dict);
95 },
96
97 /**
98 * Helper function for parsing the prefs dict and constructing Preference
99 * objects.
100 * @param {string} prefix The namespace prefix of the pref.
101 * @param {!Object} dict Map with preference values.
102 * @private
103 */
104 parsePrefDict_: function(prefix, dict) {
105 for (let prefName in dict) {
106 let prefObj = dict[prefName];
107 if (!this.isPrefObject_(prefObj)) {
108 this.parsePrefDict_(prefix + prefName + '.', prefObj);
109 continue;
110 }
111
112 // prefObj is actually a pref object. Construct the path to pref using
113 // prefix, add the pref to this.settings, and observe changes.
114 let root = this.settings;
115 let pathPieces = prefix.slice(0, -1).split('.');
116 for (let i in pathPieces) {
117 root[pathPieces[i]] = root[pathPieces[i]] || {};
118 root = root[pathPieces[i]];
119 }
120
121 root[prefName] = prefObj;
122 let keyObserver = new ObjectObserver(prefObj);
45 keyObserver.open( 123 keyObserver.open(
46 this.propertyChangeCallback_.bind(this, 'settings.' + key)); 124 this.propertyChangeCallback_.bind(this, prefix + prefName));
47 } 125 }
48 } 126 },
49 }, 127
50 128 /**
51 /** 129 * Determines whether the passed object is a pref object from Chrome.
52 * Initializes some defaults for the a11y settings. 130 * @param {*} rawPref The object to check.
53 * @private 131 * @return {boolean} True if the passes object is a pref.
54 */ 132 * @private
55 initializeA11y_: function() { 133 */
56 this.settings.a11y = { 134 isPrefObject_: function(rawPref) {
57 enableMenu: true, 135 return rawPref && typeof rawPref == 'object' &&
58 largeCursorEnabled: false, 136 rawPref.hasOwnProperty('value') &&
59 highContrastEnabled: false, 137 rawPref.hasOwnProperty('disabled');
60 stickyKeysEnabled: false, 138 },
61 screenMagnifier: false, 139
62 enableTapDragging: false, 140 /**
63 autoclick: false, 141 * Called when a property of a pref changes.
64 autoclickDelayMs: 200, 142 * @param {string} propertyPath The path before the property names.
65 virtualKeyboard: false, 143 * @param {!Array<string>} added An array of keys which were added.
66 }; 144 * @param {!Array<string>} removed An array of keys which were removed.
67 145 * @param {!Array<string>} changed An array of keys of properties whose
68 this.settings.touchpad = { 146 * values changed.
69 enableTapDragging: false, 147 * @param {function(string) : *} getOldValueFn A function which takes a
70 }; 148 * property name and returns the old value for that property.
71 149 * @private
72 // ChromeVox is enbaled/disabled via the 'settings.accessibility' boolean 150 */
73 // pref. 151 propertyChangeCallback_: function(
74 this.settings.accessibility = false; 152 propertyPath, added, removed, changed, getOldValueFn) {
75 153 for (let property in changed) {
76 // TODO(jlklein): Actually pull the data out of prefs and initialize. 154 // UI should only be able to change the value of a setting for now, not
77 }, 155 // disabled, etc.
78 156 assert(property == 'value');
79 /** 157
80 * Initializes some defaults for the downloads settings. 158 let newValue = changed[property];
81 * @private 159 assert(newValue !== undefined);
82 */ 160
83 initializeDownloads_: function() { 161 switch (typeof newValue) {
84 this.settings.download = { 162 case 'boolean':
85 downloadLocation: '', 163 this.setBooleanPref_(
86 promptForDownload: false, 164 propertyPath, /** @type {boolean} */ (newValue));
87 }; 165 break;
88 }, 166 case 'number':
89 167 this.setNumberPref_(
90 /** 168 propertyPath, /** @type {number} */ (newValue));
91 * @param {string} propertyPath The path before the property names. 169 break;
92 * @param {!Array<string>} added An array of keys which were added. 170 case 'string':
93 * @param {!Array<string>} removed An array of keys which were removed. 171 this.setStringPref_(
94 * @param {!Array<string>} changed An array of keys of properties whose 172 propertyPath, /** @type {string} */ (newValue));
95 * values changed. 173 break;
96 * @param {function(string) : *} getOldValueFn A function which takes a 174 case 'object':
97 * property name and returns the old value for that property. 175 assertInstanceof(newValue, Array);
98 * @private 176 this.setArrayPref_(
99 */ 177 propertyPath, /** @type {!Array} */ (newValue));
100 propertyChangeCallback_: function( 178 }
101 propertyPath, added, removed, changed, getOldValueFn) { 179 }
102 Object.keys(changed).forEach(function(property) { 180 },
103 console.log( 181
104 `${propertyPath}.${property}`, 182 /**
105 `old : ${getOldValueFn(property)}`, 183 * @param {string} propertyPath The full path of the pref.
106 `newValue : ${changed[property]}`); 184 * @param {boolean} value The new value of the pref.
107 185 * @private
108 // TODO(jlklein): Actually set the changed property back to prefs. 186 */
109 }); 187 setBooleanPref_: function(propertyPath, value) {
110 }, 188 chrome.send('setBooleanPref', [propertyPath, value]);
111 }); 189 },
190
191 /**
192 * @param {string} propertyPath The full path of the pref.
193 * @param {string} value The new value of the pref.
194 * @private
195 */
196 setStringPref_: function(propertyPath, value) {
197 chrome.send('setStringPref', [propertyPath, value]);
198 },
199
200 /**
201 * @param {string} propertyPath The full path of the pref.
202 * @param {number} value The new value of the pref.
203 * @private
204 */
205 setNumberPref_: function(propertyPath, value) {
206 var setFn = value % 1 == 0 ? 'setIntegerPref' : 'setDoublePref';
207 chrome.send(setFn, [propertyPath, value]);
208 },
209
210 /**
211 * @param {string} propertyPath The full path of the pref.
212 * @param {!Array} value The new value of the pref.
213 * @private
214 */
215 setArrayPref_: function(propertyPath, value) {
216 chrome.send('setListPref', [propertyPath, value]);
217 },
218 });
219 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698