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' models Chrome settings and preferences, listening for | 7 * 'cr-settings-prefs' models Chrome settings and preferences, listening for |
8 * changes to Chrome prefs whitelisted in chrome.settingsPrivate. | 8 * changes to Chrome prefs whitelisted in chrome.settingsPrivate. |
9 * When changing prefs in this element's 'prefs' property via the UI, this | 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 | 10 * element tries to set those preferences in Chrome. Whether or not the calls to |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 }; | 45 }; |
46 | 46 |
47 /** | 47 /** |
48 * @constructor | 48 * @constructor |
49 * @extends {PrefWrapper} | 49 * @extends {PrefWrapper} |
50 * @param {!chrome.settingsPrivate.PrefObject} prefObj | 50 * @param {!chrome.settingsPrivate.PrefObject} prefObj |
51 */ | 51 */ |
52 function ListPrefWrapper(prefObj) { | 52 function ListPrefWrapper(prefObj) { |
53 // Copy the array so changes to prefObj aren't reflected in this.value_. | 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. | 54 // TODO(michaelpg): Do a deep copy to support nested lists and objects. |
55 this.value_ = prefObj.value.slice(); | 55 this.value_ = PrefWrapper.deepCopy(prefObj.value); |
| 56 // this.value_ = prefObj.value.slice(); |
56 } | 57 } |
57 | 58 |
58 ListPrefWrapper.prototype = { | 59 ListPrefWrapper.prototype = { |
59 __proto__: PrefWrapper.prototype, | 60 __proto__: PrefWrapper.prototype, |
60 | 61 |
61 /** | 62 /** |
62 * Tests whether two ListPrefWrapper values contain the same list items. | 63 * Tests whether two ListPrefWrapper values contain the same list items. |
63 * @override | 64 * @override |
64 */ | 65 */ |
65 equals: function(other) { | 66 equals: function(other) { |
66 'use strict'; | |
67 assert(this.key_ == other.key_); | 67 assert(this.key_ == other.key_); |
68 if (this.value_.length != other.value_.length) | 68 return PrefWrapper.objectsEqual(this.value_, other.value_); |
69 return false; | 69 }, |
70 for (let i = 0; i < this.value_.length; i++) { | 70 }; |
71 if (this.value_[i] != other.value_[i]) | 71 |
72 return false; | 72 /** |
73 } | 73 * @constructor |
74 return true; | 74 * @extends {PrefWrapper} |
| 75 * @param {!chrome.settingsPrivate.PrefObject} prefObj |
| 76 */ |
| 77 function DictPrefWrapper(prefObj) { |
| 78 // Copy the object so changes to prefObj aren't reflected in this.value_. |
| 79 // TODO(michaelpg): Do a deep copy to support nested lists and objects. |
| 80 this.value_ = prefObj.value.slice(); |
| 81 } |
| 82 |
| 83 DictPrefWrapper.prototype = { |
| 84 __proto__: PrefWrapper.prototype, |
| 85 |
| 86 /** |
| 87 * Tests whether two DictPrefWrapper values contain the same list items. |
| 88 * @override |
| 89 */ |
| 90 equals: function(other) { |
| 91 assert(this.key_ == other.key_); |
| 92 return PrefWrapper.objectsEqual(this.value_, other.value_); |
75 }, | 93 }, |
76 }; | 94 }; |
77 | 95 |
78 (function() { | 96 (function() { |
79 'use strict'; | 97 'use strict'; |
| 98 |
| 99 PrefWrapper.objectsEqual = function(obj1, obj2) { |
| 100 if (Array.isArray(obj1)) { |
| 101 if (!Array.isArray(obj2)) |
| 102 return false; |
| 103 if (obj1.length != obj2.length) |
| 104 return false; |
| 105 if (obj1 == obj2) { |
| 106 return true; |
| 107 } |
| 108 for (var i = 0; i < obj1.length; i++) { |
| 109 if (!PrefWrapper.objectsEqual(obj1[i], obj2[i])) { |
| 110 return false; |
| 111 } |
| 112 else if (typeof obj1[i] == 'object') { |
| 113 } |
| 114 } |
| 115 return true; |
| 116 } |
| 117 if (typeof obj1 == 'object') { |
| 118 var props1 = Object.keys(obj1); |
| 119 var props2 = Object.keys(obj2); |
| 120 if (props1.length != props2.length) |
| 121 return false; |
| 122 for (var prop of props1) { |
| 123 if (!PrefWrapper.objectsEqual(obj1[prop], obj2[prop])) |
| 124 return false; |
| 125 } |
| 126 return true; |
| 127 } |
| 128 return obj1 == obj2; |
| 129 }; |
| 130 |
| 131 PrefWrapper.deepCopy = function(obj) { |
| 132 if (Array.isArray(obj)) { |
| 133 var copy = []; |
| 134 for (let el of obj) |
| 135 copy.push(PrefWrapper.deepCopy(el)); |
| 136 return copy; |
| 137 } |
| 138 if (typeof obj == 'object') { |
| 139 var copy = {}; |
| 140 for (let prop in obj) |
| 141 copy[prop] = PrefWrapper.deepCopy(obj[prop]); |
| 142 } |
| 143 return obj; |
| 144 }; |
| 145 |
80 | 146 |
81 Polymer({ | 147 Polymer({ |
82 is: 'cr-settings-prefs', | 148 is: 'cr-settings-prefs', |
83 | 149 |
84 properties: { | 150 properties: { |
85 /** | 151 /** |
86 * Object containing all preferences, for use by Polymer controls. | 152 * Object containing all preferences, for use by Polymer controls. |
87 */ | 153 */ |
88 prefs: { | 154 prefs: { |
89 type: Object, | 155 type: Object, |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 var prefWrapper = this.prefWrappers_[key]; | 205 var prefWrapper = this.prefWrappers_[key]; |
140 if (!prefWrapper) | 206 if (!prefWrapper) |
141 return; | 207 return; |
142 | 208 |
143 var prefObj = /** @type {chrome.settingsPrivate.PrefObject} */( | 209 var prefObj = /** @type {chrome.settingsPrivate.PrefObject} */( |
144 this.get(key, this.prefs)); | 210 this.get(key, this.prefs)); |
145 | 211 |
146 // If settingsPrivate already has this value, do nothing. (Otherwise, | 212 // If settingsPrivate already has this value, do nothing. (Otherwise, |
147 // a change event from settingsPrivate could make us call | 213 // a change event from settingsPrivate could make us call |
148 // settingsPrivate.setPref and potentially trigger an IPC loop.) | 214 // settingsPrivate.setPref and potentially trigger an IPC loop.) |
149 if (prefWrapper.equals(this.createPrefWrapper_(prefObj))) | 215 // |
| 216 // this copy is a waste |
| 217 if (PrefWrapper.objectsEqual(prefWrapper.value_, this.createPrefWrapper_(p
refObj).value_)) { |
150 return; | 218 return; |
| 219 } |
151 | 220 |
152 this.settingsApi_.setPref( | 221 this.settingsApi_.setPref( |
153 key, | 222 key, |
154 prefObj.value, | 223 prefObj.value, |
155 /* pageId */ '', | 224 /* pageId */ '', |
156 /* callback */ this.setPrefCallback_.bind(this, key)); | 225 /* callback */ this.setPrefCallback_.bind(this, key)); |
157 }, | 226 }, |
158 | 227 |
159 /** | 228 /** |
160 * Called when prefs in the underlying Chrome pref store are changed. | 229 * Called when prefs in the underlying Chrome pref store are changed. |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 * @return {PrefWrapper} An object containing a copy of the PrefObject's | 334 * @return {PrefWrapper} An object containing a copy of the PrefObject's |
266 * value. | 335 * value. |
267 * @private | 336 * @private |
268 */ | 337 */ |
269 createPrefWrapper_: function(prefObj) { | 338 createPrefWrapper_: function(prefObj) { |
270 return prefObj.type == chrome.settingsPrivate.PrefType.LIST ? | 339 return prefObj.type == chrome.settingsPrivate.PrefType.LIST ? |
271 new ListPrefWrapper(prefObj) : new PrefWrapper(prefObj); | 340 new ListPrefWrapper(prefObj) : new PrefWrapper(prefObj); |
272 }, | 341 }, |
273 }); | 342 }); |
274 })(); | 343 })(); |
OLD | NEW |