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
3 * found in the LICENSE file. */
7 * 'cr-settings-prefs' is an element which serves as a model for
8 * interaction with settings which are stored in Chrome's
13 * <cr-settings-prefs id="prefs"></cr-settings-prefs>
14 * <cr-settings-a11y-page prefs="{{this.$.prefs}}"></cr-settings-a11y-page>
16 * @group Chrome Settings Elements
17 * @element cr-settings-a11y-page
23 * A list of all pref paths used on all platforms in the UI.
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>}
29 var PREFS_TO_FETCH = [
30 'download.default_directory',
31 'download.prompt_for_download',
35 * A list of all CrOS-only pref paths used in the UI.
36 * TODO(jlklein): This is a temporary workaround that needs to be removed
37 * once settingsPrivate is implemented with the fetchAll function. We will
38 * not need to tell the settingsPrivate API which prefs to fetch.
39 * @const {!Array<string>}
41 var CROS_ONLY_PREFS = [
42 'settings.accessibility',
43 'settings.a11y.autoclick',
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',
54 Polymer('cr-settings-prefs', {
57 * Object containing all preferences.
68 CrSettingsPrefs.isInitialized = false;
70 this.fetchSettings_();
74 * Fetches all settings from settingsPrivate.
75 * TODO(jlklein): Implement using settingsPrivate when it's available.
78 fetchSettings_: function() {
79 // *Sigh* We need to export the function name to global scope. This is
80 // needed the CoreOptionsHandler can only call a function in the global
82 var callbackName = 'CrSettingsPrefs_onPrefsFetched';
83 window[callbackName] = this.onPrefsFetched_.bind(this);
84 var prefsToFetch = PREFS_TO_FETCH;
86 prefsToFetch.concat(CROS_ONLY_PREFS);
88 chrome.send('fetchPrefs', [callbackName].concat(prefsToFetch));
92 * Fetches all settings from settingsPrivate.
93 * @param {!Object} dict Map of fetched property values.
96 onPrefsFetched_: function(dict) {
97 this.parsePrefDict_('', dict);
98 CrSettingsPrefs.isInitialized = true;
99 document.dispatchEvent(new Event(CrSettingsPrefs.INITIALIZED));
103 * Helper function for parsing the prefs dict and constructing Preference
105 * @param {string} prefix The namespace prefix of the pref.
106 * @param {!Object} dict Map with preference values.
109 parsePrefDict_: function(prefix, dict) {
110 for (let prefName in dict) {
111 let prefObj = dict[prefName];
112 if (!this.isPrefObject_(prefObj)) {
113 this.parsePrefDict_(prefix + prefName + '.', prefObj);
117 // prefObj is actually a pref object. Construct the path to pref using
118 // prefix, add the pref to this.settings, and observe changes.
119 let root = this.settings;
120 let pathPieces = prefix.slice(0, -1).split('.');
121 pathPieces.forEach(function(pathPiece) {
122 root[pathPiece] = root[pathPiece] || {};
123 root = root[pathPiece];
126 root[prefName] = prefObj;
127 let keyObserver = new ObjectObserver(prefObj);
129 this.propertyChangeCallback_.bind(this, prefix + prefName));
134 * Determines whether the passed object is a pref object from Chrome.
135 * @param {*} rawPref The object to check.
136 * @return {boolean} True if the passes object is a pref.
139 isPrefObject_: function(rawPref) {
140 return rawPref && typeof rawPref == 'object' &&
141 rawPref.hasOwnProperty('value') &&
142 rawPref.hasOwnProperty('disabled');
146 * Called when a property of a pref changes.
147 * @param {string} propertyPath The path before the property names.
148 * @param {!Array<string>} added An array of keys which were added.
149 * @param {!Array<string>} removed An array of keys which were removed.
150 * @param {!Array<string>} changed An array of keys of properties whose
152 * @param {function(string) : *} getOldValueFn A function which takes a
153 * property name and returns the old value for that property.
156 propertyChangeCallback_: function(
157 propertyPath, added, removed, changed, getOldValueFn) {
158 for (let property in changed) {
159 // UI should only be able to change the value of a setting for now, not
161 assert(property == 'value');
163 let newValue = changed[property];
164 assert(newValue !== undefined);
166 switch (typeof newValue) {
168 this.setBooleanPref_(
169 propertyPath, /** @type {boolean} */ (newValue));
173 propertyPath, /** @type {number} */ (newValue));
177 propertyPath, /** @type {string} */ (newValue));
180 assertInstanceof(newValue, Array);
182 propertyPath, /** @type {!Array} */ (newValue));
188 * @param {string} propertyPath The full path of the pref.
189 * @param {boolean} value The new value of the pref.
192 setBooleanPref_: function(propertyPath, value) {
193 chrome.send('setBooleanPref', [propertyPath, value]);
197 * @param {string} propertyPath The full path of the pref.
198 * @param {string} value The new value of the pref.
201 setStringPref_: function(propertyPath, value) {
202 chrome.send('setStringPref', [propertyPath, value]);
206 * @param {string} propertyPath The full path of the pref.
207 * @param {number} value The new value of the pref.
210 setNumberPref_: function(propertyPath, value) {
211 var setFn = value % 1 == 0 ? 'setIntegerPref' : 'setDoublePref';
212 chrome.send(setFn, [propertyPath, value]);
216 * @param {string} propertyPath The full path of the pref.
217 * @param {!Array} value The new value of the pref.
220 setArrayPref_: function(propertyPath, value) {
221 chrome.send('setListPref', [propertyPath, value]);