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 * |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
61 this.updatePrefs_(prefs, true); | 61 this.updatePrefs_(prefs, true); |
62 | 62 |
63 CrSettingsPrefs.isInitialized = true; | 63 CrSettingsPrefs.isInitialized = true; |
64 document.dispatchEvent(new Event(CrSettingsPrefs.INITIALIZED)); | 64 document.dispatchEvent(new Event(CrSettingsPrefs.INITIALIZED)); |
65 }, | 65 }, |
66 | 66 |
67 | 67 |
68 /** | 68 /** |
69 * Updates the settings model with the given prefs. | 69 * Updates the settings model with the given prefs. |
70 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs | 70 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs |
71 * @param {boolean} shouldObserve Whether to add an ObjectObserver for each | 71 * @param {boolean} shouldObserve Whether to add an ObjectObserver for each |
michaelpg
2015/05/20 03:25:40
Reword this, e.g. whether each of the prefs should
Jeremy Klein
2015/05/20 18:06:30
Done.
| |
72 * of the prefs. | 72 * of the prefs. |
73 * @private | 73 * @private |
74 */ | 74 */ |
75 updatePrefs_: function(prefs, shouldObserve) { | 75 updatePrefs_: function(prefs, shouldObserve) { |
76 prefs.forEach(function(prefObj) { | 76 prefs.forEach(function(prefObj) { |
77 let root = this.prefStore; | 77 let root = this.prefStore; |
78 let tokens = prefObj.key.split('.'); | 78 let tokens = prefObj.key.split('.'); |
79 | 79 |
80 assert(tokens.length > 0); | 80 assert(tokens.length > 0); |
81 | 81 |
82 for (let i = 0; i < tokens.length; i++) { | 82 for (let i = 0; i < tokens.length; i++) { |
83 let token = tokens[i]; | 83 let token = tokens[i]; |
84 | 84 |
85 if (!root.hasOwnProperty(token)) { | 85 if (!root.hasOwnProperty(token)) { |
86 let path = 'prefStore.' + tokens.slice(0, i + 1).join('.'); | 86 let path = 'prefStore.' + tokens.slice(0, i + 1).join('.'); |
87 this.setPathValue(path, {}); | 87 this.setPathValue(path, {}); |
88 } | 88 } |
89 root = root[token]; | 89 root = root[token]; |
90 } | 90 } |
91 | 91 |
92 // NOTE: Do this copy rather than just a re-assignment, so that the | 92 // NOTE: Do this copy rather than just a re-assignment, so that the |
93 // ObjectObserver fires. | 93 // ObjectObserver fires. |
michaelpg
2015/05/20 03:25:40
Reword this
Jeremy Klein
2015/05/20 18:06:30
Done.
| |
94 for (let objKey in prefObj) { | 94 for (let objKey in prefObj) { |
95 let path = 'prefStore.' + prefObj.key + '.' + objKey; | 95 let path = 'prefStore.' + prefObj.key + '.' + objKey; |
96 this.setPathValue(path, prefObj[objKey]); | 96 this.setPathValue(path, prefObj[objKey]); |
97 } | 97 } |
98 | 98 |
99 if (shouldObserve) { | 99 if (shouldObserve) { |
100 let keyObserver = new ObjectObserver(root); | 100 Object.observe(root, this.propertyChangeCallback_, ['update']); |
101 keyObserver.open( | |
102 this.propertyChangeCallback_.bind(this, prefObj.key)); | |
103 } | 101 } |
104 }, this); | 102 }, this); |
105 }, | 103 }, |
106 | 104 |
107 /** | 105 /** |
108 * Called when a property of a pref changes. | 106 * Called when a property of a pref changes. |
109 * @param {string} propertyPath The path before the property names. | 107 * @param {!Array<!Object>} changes An array of objects describing changes. |
110 * @param {!Array<string>} added An array of keys which were added. | 108 * @see http://www.html5rocks.com/en/tutorials/es7/observe/ |
111 * @param {!Array<string>} removed An array of keys which were removed. | |
112 * @param {!Array<string>} changed An array of keys of properties whose | |
113 * values changed. | |
114 * @param {function(string) : *} getOldValueFn A function which takes a | |
115 * property name and returns the old value for that property. | |
116 * @private | 109 * @private |
117 */ | 110 */ |
118 propertyChangeCallback_: function( | 111 propertyChangeCallback_: function(changes) { |
119 propertyPath, added, removed, changed, getOldValueFn) { | 112 changes.forEach(function(change) { |
120 for (let property in changed) { | |
121 // UI should only be able to change the value of a setting for now, not | 113 // UI should only be able to change the value of a setting for now, not |
122 // disabled, etc. | 114 // disabled, etc. |
123 assert(property == 'value'); | 115 assert(change.name == 'value'); |
124 | 116 |
125 let newValue = changed[property]; | 117 let newValue = change.object[change.name]; |
126 assert(newValue !== undefined); | 118 assert(newValue !== undefined); |
127 | 119 |
128 chrome.settingsPrivate.setPref( | 120 chrome.settingsPrivate.setPref( |
129 propertyPath, | 121 change.object['key'], |
130 newValue, | 122 newValue, |
131 /* pageId */ '', | 123 /* pageId */ '', |
132 /* callback */ function() {}); | 124 /* callback */ function() {}); |
133 } | 125 }); |
134 }, | 126 }, |
135 }); | 127 }); |
136 })(); | 128 })(); |
OLD | NEW |