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

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

Issue 1287913005: Refactor prefs.js for MD-Settings (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: closure updates Created 5 years, 3 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' models Chrome settings and preferences, listening for
8 * interaction with settings which are stored in Chrome's 8 * changes to Chrome prefs whitelisted in chrome.settingsPrivate.
9 * Preferences. 9 * When changing prefs in this element's 'prefs' property via the UI, this
10 * element tries to set those preferences in Chrome. Whether or not the calls to
11 * settingsPrivate.setPref succeed, 'prefs' is eventually consistent with the
12 * Chrome pref store.
10 * 13 *
11 * Example: 14 * Example:
12 * 15 *
13 * <cr-settings-prefs id="prefs"></cr-settings-prefs> 16 * <cr-settings-prefs prefs="{{prefs}}"></cr-settings-prefs>
14 * <cr-settings-a11y-page prefs="{{this.$.prefs}}"></cr-settings-a11y-page> 17 * <cr-settings-a11y-page prefs="{{prefs}}"></cr-settings-a11y-page>
15 * 18 *
16 * @group Chrome Settings Elements 19 * @group Chrome Settings Elements
17 * @element cr-settings-a11y-page 20 * @element cr-settings-prefs
18 */ 21 */
22
23 /**
24 * Pref state object. Copies values of PrefObjects received from
25 * settingsPrivate to determine when pref values have changed.
26 * This prototype works for primitive types, but more complex types should
27 * override these functions.
28 * @constructor
29 * @param {!chrome.settingsPrivate.PrefObject} prefObj
30 */
31 function PrefWrapper(prefObj) {
32 this.key_ = prefObj.key;
33 this.value_ = prefObj.value;
34 }
35
36 /**
Dan Beam 2015/08/29 01:49:30 off
michaelpg 2015/08/29 01:53:23 ew, sorry
37 * Checks if other's value equals this.value_. Both prefs wrapped by the
38 * PrefWrappers should have the same keys.
39 * @param {PrefWrapper} other
40 * @return {boolean}
41 */
42 PrefWrapper.prototype.equals = function(other) {
43 assert(this.key_ == other.key_);
44 return this.value_ == other.value_;
45 };
46
47 /**
Dan Beam 2015/08/29 01:49:30 also off
michaelpg 2015/08/29 01:53:23 Done.
48 * @constructor
49 * @extends {PrefWrapper}
50 * @param {!chrome.settingsPrivate.PrefObject} prefObj
51 */
52 function ListPrefWrapper(prefObj) {
53 // Copy the array so changes to prefObj aren't reflected in this.value_.
54 // TODO(michaelpg): Do a deep copy to support nested lists and objects.
55 this.value_ = prefObj.value.slice();
56 }
57
58 ListPrefWrapper.prototype = {
59 __proto__: PrefWrapper.prototype,
60
61 /**
62 * Tests whether two ListPrefWrapper values contain the same list items.
63 * @override
64 */
65 equals: function(other) {
66 'use strict';
67 assert(this.key_ == other.key_);
68 if (this.value_.length != other.value_.length)
69 return false;
70 for (let i = 0; i < this.value_.length; i++) {
71 if (this.value_[i] != other.value_[i])
72 return false;
73 }
74 return true;
75 },
76 };
77
19 (function() { 78 (function() {
20 'use strict'; 79 'use strict';
21 80
22 Polymer({ 81 Polymer({
23 is: 'cr-settings-prefs', 82 is: 'cr-settings-prefs',
24 83
25 properties: { 84 properties: {
26 /** 85 /**
27 * Object containing all preferences. 86 * Object containing all preferences, for use by Polymer controls.
28 */ 87 */
29 prefStore: { 88 prefs: {
30 type: Object, 89 type: Object,
31 value: function() { return {}; }, 90 value: function() { return {}; },
32 notify: true, 91 notify: true,
33 }, 92 },
93
94 /**
95 * Map of pref keys to PrefWrapper objects representing the state of the
96 * Chrome pref store.
97 * @type {Object<PrefWrapper>}
98 * @private
99 */
100 prefWrappers_: {
101 type: Object,
102 value: function() { return {}; },
103 },
34 }, 104 },
35 105
106 observers: [
107 'prefsChanged_(prefs.*)',
108 ],
109
36 /** @override */ 110 /** @override */
37 created: function() { 111 created: function() {
38 CrSettingsPrefs.isInitialized = false; 112 CrSettingsPrefs.isInitialized = false;
39 113
40 chrome.settingsPrivate.onPrefsChanged.addListener( 114 chrome.settingsPrivate.onPrefsChanged.addListener(
41 this.onPrefsChanged_.bind(this)); 115 this.onSettingsPrivatePrefsChanged_.bind(this));
42 chrome.settingsPrivate.getAllPrefs(this.onPrefsFetched_.bind(this)); 116 chrome.settingsPrivate.getAllPrefs(
117 this.onSettingsPrivatePrefsFetched_.bind(this));
118 },
119
120 /**
121 * Polymer callback for changes to this.prefs.
122 * @param {!{path: string, value: *}} change
123 * @private
124 */
125 prefsChanged_: function(change) {
126 if (!CrSettingsPrefs.isInitialized)
127 return;
128
129 var key = this.getPrefKeyFromPath_(change.path);
130 var prefWrapper = this.prefWrappers_[key];
131 if (!prefWrapper)
132 return;
133
134 var prefObj = /** @type {chrome.settingsPrivate.PrefObject} */(
135 this.get(key, this.prefs));
136
137 // If settingsPrivate already has this value, do nothing. (Otherwise,
138 // a change event from settingsPrivate could make us call
139 // settingsPrivate.setPref and potentially trigger an IPC loop.)
140 if (prefWrapper.equals(this.createPrefWrapper_(prefObj)))
141 return;
142
143 chrome.settingsPrivate.setPref(
144 key,
145 prefObj.value,
146 /* pageId */ '',
147 /* callback */ this.setPrefCallback_.bind(this, key));
43 }, 148 },
44 149
45 /** 150 /**
46 * Called when prefs in the underlying Chrome pref store are changed. 151 * Called when prefs in the underlying Chrome pref store are changed.
47 * @param {!Array<!PrefObject>} prefs The prefs that changed. 152 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs
153 * The prefs that changed.
48 * @private 154 * @private
49 */ 155 */
50 onPrefsChanged_: function(prefs) { 156 onSettingsPrivatePrefsChanged_: function(prefs) {
51 this.updatePrefs_(prefs, false); 157 if (CrSettingsPrefs.isInitialized)
158 this.updatePrefs_(prefs);
52 }, 159 },
53 160
54 /** 161 /**
55 * Called when prefs are fetched from settingsPrivate. 162 * Called when prefs are fetched from settingsPrivate.
56 * @param {!Array<!PrefObject>} prefs 163 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs
57 * @private 164 * @private
58 */ 165 */
59 onPrefsFetched_: function(prefs) { 166 onSettingsPrivatePrefsFetched_: function(prefs) {
60 this.updatePrefs_(prefs, true); 167 this.updatePrefs_(prefs);
61 168
62 CrSettingsPrefs.isInitialized = true; 169 CrSettingsPrefs.isInitialized = true;
63 document.dispatchEvent(new Event(CrSettingsPrefs.INITIALIZED)); 170 document.dispatchEvent(new Event(CrSettingsPrefs.INITIALIZED));
64 }, 171 },
65 172
173 /**
174 * Checks the result of calling settingsPrivate.setPref.
175 * @param {string} key The key used in the call to setPref.
176 * @param {boolean} success True if setting the pref succeeded.
177 * @private
178 */
179 setPrefCallback_: function(key, success) {
180 if (success)
181 return;
182
183 // Get the current pref value from chrome.settingsPrivate to ensure the
184 // UI stays up to date.
185 chrome.settingsPrivate.getPref(key, function(pref) {
186 this.updatePrefs_([pref]);
187 }.bind(this));
188 },
66 189
67 /** 190 /**
68 * Updates the settings model with the given prefs. 191 * Updates the prefs model with the given prefs.
69 * @param {!Array<!PrefObject>} prefs 192 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs
70 * @param {boolean} shouldObserve Whether each of the prefs should be
71 * observed.
72 * @private 193 * @private
73 */ 194 */
74 updatePrefs_: function(prefs, shouldObserve) { 195 updatePrefs_: function(prefs) {
75 prefs.forEach(function(prefObj) { 196 prefs.forEach(function(newPrefObj) {
76 let root = this.prefStore; 197 // Use the PrefObject from settingsPrivate to create a PrefWrapper in
77 let tokens = prefObj.key.split('.'); 198 // prefWrappers_ at the pref's key.
199 this.prefWrappers_[newPrefObj.key] =
200 this.createPrefWrapper_(newPrefObj);
78 201
79 assert(tokens.length > 0); 202 // Set or update the pref in |prefs|. This triggers observers in the UI,
80 203 // which update controls associated with the pref.
81 for (let i = 0; i < tokens.length; i++) { 204 this.setPref_(newPrefObj);
82 let token = tokens[i];
83
84 if (!root.hasOwnProperty(token)) {
85 let path = 'prefStore.' + tokens.slice(0, i + 1).join('.');
86 this.set(path, {});
87 }
88 root = root[token];
89 }
90
91 // NOTE: Do this copy rather than just a re-assignment, so that the
92 // observer fires.
93 for (let objKey in prefObj) {
94 let path = 'prefStore.' + prefObj.key + '.' + objKey;
95
96 // Handle lists specially. We don't want to call this.set()
97 // unconditionally upon updating a list value, since even its contents
98 // are the same as the old list, doing this set() may cause an
99 // infinite update cycle (http://crbug.com/498586).
100 if (objKey == 'value' &&
101 prefObj.type == chrome.settingsPrivate.PrefType.LIST &&
102 !this.shouldUpdateListPrefValue_(root, prefObj['value'])) {
103 continue;
104 }
105
106 this.set(path, prefObj[objKey]);
107 }
108
109 if (shouldObserve) {
110 Object.observe(root, this.propertyChangeCallback_, ['update']);
111 }
112 }, this); 205 }, this);
113 }, 206 },
114 207
208 /**
209 * Given a 'property-changed' path, returns the key of the preference the
210 * path refers to. E.g., if the path of the changed property is
211 * 'prefs.search.suggest_enabled.value', the key of the pref that changed is
212 * 'search.suggest_enabled'.
213 * @param {string} path
214 * @return {string}
215 * @private
216 */
217 getPrefKeyFromPath_: function(path) {
218 // Skip the first token, which refers to the member variable (this.prefs).
219 var parts = path.split('.');
220 assert(parts.shift() == 'prefs');
115 221
116 /** 222 for (let i = 1; i <= parts.length; i++) {
117 * @param {Object} root The root object for a pref that contains a list 223 let key = parts.slice(0, i).join('.');
118 * value. 224 // The prefWrappers_ keys match the pref keys.
119 * @param {!Array} newValue The new list value. 225 if (this.prefWrappers_[key] != undefined)
120 * @return {boolean} Whether the new value is different from the one in 226 return key;
121 * root, thus necessitating a pref update.
122 */
123 shouldUpdateListPrefValue_: function(root, newValue) {
124 if (root.value == null ||
125 root.value.length != newValue.length) {
126 return true;
127 } 227 }
128 228 return '';
129 for (let i = 0; i < newValue.length; i++) {
130 if (root.value != null && root.value[i] != newValue[i]) {
131 return true;
132 }
133 }
134
135 return false;
136 }, 229 },
137 230
138 /** 231 /**
139 * Called when a property of a pref changes. 232 * Sets or updates the pref denoted by newPrefObj.key in the publicy exposed
140 * @param {!Array<!Object>} changes An array of objects describing changes. 233 * |prefs| property.
141 * @see http://www.html5rocks.com/en/tutorials/es7/observe/ 234 * @param {chrome.settingsPrivate.PrefObject} newPrefObj The pref object to
235 * update the pref with.
142 * @private 236 * @private
143 */ 237 */
144 propertyChangeCallback_: function(changes) { 238 setPref_: function(newPrefObj) {
145 changes.forEach(function(change) { 239 // Check if the pref exists already in the Polymer |prefs| object.
146 // UI should only be able to change the value of a setting for now, not 240 if (this.get(newPrefObj.key, this.prefs)) {
147 // disabled, etc. 241 // Update just the value, notifying listeners of the change.
148 assert(change.name == 'value'); 242 this.set('prefs.' + newPrefObj.key + '.value', newPrefObj.value);
243 } else {
244 // Add the pref to |prefs|. cr.exportPath doesn't use Polymer.Base.set,
245 // which is OK because the nested property update events aren't useful.
246 cr.exportPath(newPrefObj.key, newPrefObj, this.prefs);
247 // Notify listeners of the change at the preference key.
248 this.notifyPath('prefs.' + newPrefObj.key, newPrefObj);
249 }
250 },
149 251
150 let newValue = change.object[change.name]; 252 /**
151 assert(newValue !== undefined); 253 * Creates a PrefWrapper object from a chrome.settingsPrivate pref.
152 254 * @param {!chrome.settingsPrivate.PrefObject} prefObj
153 chrome.settingsPrivate.setPref( 255 * PrefObject received from chrome.settingsPrivate.
154 change.object['key'], 256 * @return {PrefWrapper} An object containing a copy of the PrefObject's
155 newValue, 257 * value.
156 /* pageId */ '', 258 * @private
157 /* callback */ function() {}); 259 */
158 }); 260 createPrefWrapper_: function(prefObj) {
261 return prefObj.type == chrome.settingsPrivate.PrefType.LIST ?
262 new ListPrefWrapper(prefObj) : new PrefWrapper(prefObj);
159 }, 263 },
160 }); 264 });
161 })(); 265 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698