Chromium Code Reviews| 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 PrefWrapper.prototype.equals = function(other) { | 42 PrefWrapper.prototype.equals = function(other) { |
| 43 assert(this.key_ == other.key_); | 43 assert(this.key_ == other.key_); |
| 44 return this.value_ == other.value_; | 44 return this.value_ == other.value_; |
| 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) { |
|
Dan Beam
2015/09/17 06:36:56
Why wouldn't we just make a DictPrefWrapper?
michaelpg
2015/09/17 17:53:47
These wrappers don't make much sense to me so I'm
Dan Beam
2015/09/17 18:13:31
that's not a problem
michaelpg
2015/09/17 22:54:45
I'm happy to separate out the equality methods, bu
| |
| 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 this.value_ = PrefWrapper.deepCopy(prefObj.value); |
| 55 this.value_ = prefObj.value.slice(); | |
| 56 } | 55 } |
| 57 | 56 |
| 58 ListPrefWrapper.prototype = { | 57 ListPrefWrapper.prototype = { |
| 59 __proto__: PrefWrapper.prototype, | 58 __proto__: PrefWrapper.prototype, |
| 60 | 59 |
| 61 /** | 60 /** |
| 62 * Tests whether two ListPrefWrapper values contain the same list items. | 61 * Tests whether two ListPrefWrapper values contain the same list items. |
| 63 * @override | 62 * @override |
| 64 */ | 63 */ |
| 65 equals: function(other) { | 64 equals: function(other) { |
| 66 'use strict'; | |
| 67 assert(this.key_ == other.key_); | 65 assert(this.key_ == other.key_); |
| 68 if (this.value_.length != other.value_.length) | 66 return PrefWrapper.valuesEquivalent(this.value_, other.value_); |
| 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 }, | 67 }, |
| 76 }; | 68 }; |
| 77 | 69 |
| 78 (function() { | 70 (function() { |
| 79 'use strict'; | 71 'use strict'; |
| 80 | 72 |
| 73 /** | |
| 74 * @param {*} val1 Value to compare. | |
| 75 * @param {*} val2 Value to compare with val1. | |
| 76 * @return {boolean} True if the values are equivalent (equal | |
| 77 * fundamental values, or objects whose elements are equivalent). | |
| 78 */ | |
| 79 PrefWrapper.valuesEquivalent = function(val1, val2) { | |
|
Dan Beam
2015/09/17 06:36:56
var toString = Object.prototype.toString;
if (toSt
michaelpg
2015/09/17 17:53:47
I assume 80% of the time we'll be comparing boolea
| |
| 80 if (val1 == val2) | |
|
Dan Beam
2015/09/17 06:36:56
Eh probably don't do this without type checking fi
michaelpg
2015/09/17 17:53:47
Acknowledged.
michaelpg
2015/09/17 22:54:45
Actually, in my new version I think checking "==="
Dan Beam
2015/09/17 23:07:51
=== is probably OK, yeah
| |
| 81 return true; | |
| 82 | |
| 83 if (Array.isArray(val1)) { | |
| 84 if (!Array.isArray(val2)) | |
| 85 return false; | |
| 86 if (val1.length != val2.length) | |
| 87 return false; | |
| 88 | |
| 89 for (var i = 0; i < val1.length; i++) { | |
| 90 if (!PrefWrapper.valuesEquivalent(val1[i], val2[i])) | |
| 91 return false; | |
| 92 } | |
| 93 return true; | |
|
Dan Beam
2015/09/17 18:13:31
this is the ListPrefWrapper.equals implementation
| |
| 94 } | |
| 95 | |
| 96 if (typeof val1 == 'object') { | |
|
Dan Beam
2015/09/17 06:36:56
What about {}, null?
michaelpg
2015/09/17 17:53:47
{}: "just works", valuesEquivalent({}, {}) == true
Dan Beam
2015/09/17 18:13:31
no, I meant val1 == {}, val2 == null
michaelpg
2015/09/17 22:54:45
New version handles this case.
| |
| 97 var props1 = Object.keys(val1); | |
| 98 var props2 = Object.keys(val2); | |
| 99 if (props1.length != props2.length) | |
| 100 return false; | |
| 101 for (var prop of props1) { | |
| 102 if (!PrefWrapper.valuesEquivalent(val1[prop], val2[prop])) | |
| 103 return false; | |
| 104 } | |
| 105 return true; | |
|
Dan Beam
2015/09/17 18:13:31
this is the DictPrefWrapper.equals implementation
michaelpg
2015/09/17 22:54:45
the problem is I don't want a ListPrefWrapper to b
| |
| 106 } | |
| 107 | |
| 108 return false; | |
| 109 }; | |
| 110 | |
| 111 /** | |
| 112 * @param {*} val Value to copy. | |
| 113 * @return {*} A deep copy of the value. | |
| 114 */ | |
| 115 PrefWrapper.deepCopy = function(val) { | |
| 116 if (Array.isArray(val)) { | |
| 117 var copy = []; | |
| 118 for (let el of val) | |
| 119 copy.push(PrefWrapper.deepCopy(el)); | |
| 120 return copy; | |
| 121 } | |
| 122 if (typeof val == 'object') { | |
|
Dan Beam
2015/09/17 06:36:56
null?
michaelpg
2015/09/17 17:53:47
Done.
| |
| 123 var copy = {}; | |
| 124 for (let prop in val) | |
| 125 copy[prop] = PrefWrapper.deepCopy(val[prop]); | |
| 126 } | |
| 127 return val; | |
| 128 }; | |
| 129 | |
| 81 Polymer({ | 130 Polymer({ |
| 82 is: 'cr-settings-prefs', | 131 is: 'cr-settings-prefs', |
| 83 | 132 |
| 84 properties: { | 133 properties: { |
| 85 /** | 134 /** |
| 86 * Object containing all preferences, for use by Polymer controls. | 135 * Object containing all preferences, for use by Polymer controls. |
| 87 */ | 136 */ |
| 88 prefs: { | 137 prefs: { |
| 89 type: Object, | 138 type: Object, |
| 90 notify: true, | 139 notify: true, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 var prefWrapper = this.prefWrappers_[key]; | 184 var prefWrapper = this.prefWrappers_[key]; |
| 136 if (!prefWrapper) | 185 if (!prefWrapper) |
| 137 return; | 186 return; |
| 138 | 187 |
| 139 var prefObj = /** @type {chrome.settingsPrivate.PrefObject} */( | 188 var prefObj = /** @type {chrome.settingsPrivate.PrefObject} */( |
| 140 this.get(key, this.prefs)); | 189 this.get(key, this.prefs)); |
| 141 | 190 |
| 142 // If settingsPrivate already has this value, do nothing. (Otherwise, | 191 // If settingsPrivate already has this value, do nothing. (Otherwise, |
| 143 // a change event from settingsPrivate could make us call | 192 // a change event from settingsPrivate could make us call |
| 144 // settingsPrivate.setPref and potentially trigger an IPC loop.) | 193 // settingsPrivate.setPref and potentially trigger an IPC loop.) |
| 145 if (prefWrapper.equals(this.createPrefWrapper_(prefObj))) | 194 if (PrefWrapper.valuesEquivalent(prefWrapper.value_, prefObj.value)) |
| 146 return; | 195 return; |
| 147 | 196 |
| 148 this.settingsApi_.setPref( | 197 this.settingsApi_.setPref( |
| 149 key, | 198 key, |
| 150 prefObj.value, | 199 prefObj.value, |
| 151 /* pageId */ '', | 200 /* pageId */ '', |
| 152 /* callback */ this.setPrefCallback_.bind(this, key)); | 201 /* callback */ this.setPrefCallback_.bind(this, key)); |
| 153 }, | 202 }, |
| 154 | 203 |
| 155 /** | 204 /** |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 246 * @return {PrefWrapper} An object containing a copy of the PrefObject's | 295 * @return {PrefWrapper} An object containing a copy of the PrefObject's |
| 247 * value. | 296 * value. |
| 248 * @private | 297 * @private |
| 249 */ | 298 */ |
| 250 createPrefWrapper_: function(prefObj) { | 299 createPrefWrapper_: function(prefObj) { |
| 251 return prefObj.type == chrome.settingsPrivate.PrefType.LIST ? | 300 return prefObj.type == chrome.settingsPrivate.PrefType.LIST ? |
| 252 new ListPrefWrapper(prefObj) : new PrefWrapper(prefObj); | 301 new ListPrefWrapper(prefObj) : new PrefWrapper(prefObj); |
| 253 }, | 302 }, |
| 254 }); | 303 }); |
| 255 })(); | 304 })(); |
| OLD | NEW |