OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "components/sync_preferences/pref_model_associator.h" | 5 #include "components/sync_preferences/pref_model_associator.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 | 94 |
95 if (user_pref_value) { | 95 if (user_pref_value) { |
96 DVLOG(1) << "Found user pref value for " << pref_name; | 96 DVLOG(1) << "Found user pref value for " << pref_name; |
97 // We have both server and local values. Merge them. | 97 // We have both server and local values. Merge them. |
98 std::unique_ptr<base::Value> new_value( | 98 std::unique_ptr<base::Value> new_value( |
99 MergePreference(pref_name, *user_pref_value, *sync_value)); | 99 MergePreference(pref_name, *user_pref_value, *sync_value)); |
100 | 100 |
101 // Update the local preference based on what we got from the | 101 // Update the local preference based on what we got from the |
102 // sync server. Note: this only updates the user value store, which is | 102 // sync server. Note: this only updates the user value store, which is |
103 // ignored if the preference is policy controlled. | 103 // ignored if the preference is policy controlled. |
104 if (new_value->IsType(base::Value::TYPE_NULL)) { | 104 if (new_value->IsType(base::Value::Type::NONE)) { |
105 LOG(WARNING) << "Sync has null value for pref " << pref_name.c_str(); | 105 LOG(WARNING) << "Sync has null value for pref " << pref_name.c_str(); |
106 pref_service_->ClearPref(pref_name.c_str()); | 106 pref_service_->ClearPref(pref_name.c_str()); |
107 } else if (!new_value->IsType(user_pref_value->GetType())) { | 107 } else if (!new_value->IsType(user_pref_value->GetType())) { |
108 LOG(WARNING) << "Synced value for " << preference.name() | 108 LOG(WARNING) << "Synced value for " << preference.name() |
109 << " is of type " << new_value->GetType() | 109 << " is of type " << new_value->GetType() |
110 << " which doesn't match pref type " | 110 << " which doesn't match pref type " |
111 << user_pref_value->GetType(); | 111 << user_pref_value->GetType(); |
112 } else if (!user_pref_value->Equals(new_value.get())) { | 112 } else if (!user_pref_value->Equals(new_value.get())) { |
113 pref_service_->Set(pref_name.c_str(), *new_value); | 113 pref_service_->Set(pref_name.c_str(), *new_value); |
114 } | 114 } |
115 | 115 |
116 // If the merge resulted in an updated value, inform the syncer. | 116 // If the merge resulted in an updated value, inform the syncer. |
117 if (!sync_value->Equals(new_value.get())) { | 117 if (!sync_value->Equals(new_value.get())) { |
118 syncer::SyncData sync_data; | 118 syncer::SyncData sync_data; |
119 if (!CreatePrefSyncData(pref_name, *new_value, &sync_data)) { | 119 if (!CreatePrefSyncData(pref_name, *new_value, &sync_data)) { |
120 LOG(ERROR) << "Failed to update preference."; | 120 LOG(ERROR) << "Failed to update preference."; |
121 return; | 121 return; |
122 } | 122 } |
123 | 123 |
124 sync_changes->push_back(syncer::SyncChange( | 124 sync_changes->push_back(syncer::SyncChange( |
125 FROM_HERE, syncer::SyncChange::ACTION_UPDATE, sync_data)); | 125 FROM_HERE, syncer::SyncChange::ACTION_UPDATE, sync_data)); |
126 } | 126 } |
127 } else if (!sync_value->IsType(base::Value::TYPE_NULL)) { | 127 } else if (!sync_value->IsType(base::Value::Type::NONE)) { |
128 // Only a server value exists. Just set the local user value. | 128 // Only a server value exists. Just set the local user value. |
129 pref_service_->Set(pref_name.c_str(), *sync_value); | 129 pref_service_->Set(pref_name.c_str(), *sync_value); |
130 } else { | 130 } else { |
131 LOG(WARNING) << "Sync has null value for pref " << pref_name.c_str(); | 131 LOG(WARNING) << "Sync has null value for pref " << pref_name.c_str(); |
132 } | 132 } |
133 synced_preferences_.insert(preference.name()); | 133 synced_preferences_.insert(preference.name()); |
134 } else if (user_pref_value) { | 134 } else if (user_pref_value) { |
135 // The server does not know about this preference and should be added | 135 // The server does not know about this preference and should be added |
136 // to the syncer's database. | 136 // to the syncer's database. |
137 syncer::SyncData sync_data; | 137 syncer::SyncData sync_data; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 } | 244 } |
245 | 245 |
246 // If this is not a specially handled preference, server wins. | 246 // If this is not a specially handled preference, server wins. |
247 return base::WrapUnique(server_value.DeepCopy()); | 247 return base::WrapUnique(server_value.DeepCopy()); |
248 } | 248 } |
249 | 249 |
250 bool PrefModelAssociator::CreatePrefSyncData( | 250 bool PrefModelAssociator::CreatePrefSyncData( |
251 const std::string& name, | 251 const std::string& name, |
252 const base::Value& value, | 252 const base::Value& value, |
253 syncer::SyncData* sync_data) const { | 253 syncer::SyncData* sync_data) const { |
254 if (value.IsType(base::Value::TYPE_NULL)) { | 254 if (value.IsType(base::Value::Type::NONE)) { |
255 LOG(ERROR) << "Attempting to sync a null pref value for " << name; | 255 LOG(ERROR) << "Attempting to sync a null pref value for " << name; |
256 return false; | 256 return false; |
257 } | 257 } |
258 | 258 |
259 std::string serialized; | 259 std::string serialized; |
260 // TODO(zea): consider JSONWriter::Write since you don't have to check | 260 // TODO(zea): consider JSONWriter::Write since you don't have to check |
261 // failures to deserialize. | 261 // failures to deserialize. |
262 JSONStringValueSerializer json(&serialized); | 262 JSONStringValueSerializer json(&serialized); |
263 if (!json.Serialize(value)) { | 263 if (!json.Serialize(value)) { |
264 LOG(ERROR) << "Failed to serialize preference value."; | 264 LOG(ERROR) << "Failed to serialize preference value."; |
265 return false; | 265 return false; |
266 } | 266 } |
267 | 267 |
268 sync_pb::EntitySpecifics specifics; | 268 sync_pb::EntitySpecifics specifics; |
269 sync_pb::PreferenceSpecifics* pref_specifics = | 269 sync_pb::PreferenceSpecifics* pref_specifics = |
270 GetMutableSpecifics(type_, &specifics); | 270 GetMutableSpecifics(type_, &specifics); |
271 | 271 |
272 pref_specifics->set_name(name); | 272 pref_specifics->set_name(name); |
273 pref_specifics->set_value(serialized); | 273 pref_specifics->set_value(serialized); |
274 *sync_data = syncer::SyncData::CreateLocalData(name, name, specifics); | 274 *sync_data = syncer::SyncData::CreateLocalData(name, name, specifics); |
275 return true; | 275 return true; |
276 } | 276 } |
277 | 277 |
278 base::Value* PrefModelAssociator::MergeListValues(const base::Value& from_value, | 278 base::Value* PrefModelAssociator::MergeListValues(const base::Value& from_value, |
279 const base::Value& to_value) { | 279 const base::Value& to_value) { |
280 if (from_value.GetType() == base::Value::TYPE_NULL) | 280 if (from_value.GetType() == base::Value::Type::NONE) |
281 return to_value.DeepCopy(); | 281 return to_value.DeepCopy(); |
282 if (to_value.GetType() == base::Value::TYPE_NULL) | 282 if (to_value.GetType() == base::Value::Type::NONE) |
283 return from_value.DeepCopy(); | 283 return from_value.DeepCopy(); |
284 | 284 |
285 DCHECK(from_value.GetType() == base::Value::TYPE_LIST); | 285 DCHECK(from_value.GetType() == base::Value::Type::LIST); |
286 DCHECK(to_value.GetType() == base::Value::TYPE_LIST); | 286 DCHECK(to_value.GetType() == base::Value::Type::LIST); |
287 const base::ListValue& from_list_value = | 287 const base::ListValue& from_list_value = |
288 static_cast<const base::ListValue&>(from_value); | 288 static_cast<const base::ListValue&>(from_value); |
289 const base::ListValue& to_list_value = | 289 const base::ListValue& to_list_value = |
290 static_cast<const base::ListValue&>(to_value); | 290 static_cast<const base::ListValue&>(to_value); |
291 base::ListValue* result = to_list_value.DeepCopy(); | 291 base::ListValue* result = to_list_value.DeepCopy(); |
292 | 292 |
293 for (const auto& value : from_list_value) { | 293 for (const auto& value : from_list_value) { |
294 result->AppendIfNotPresent(value->CreateDeepCopy()); | 294 result->AppendIfNotPresent(value->CreateDeepCopy()); |
295 } | 295 } |
296 return result; | 296 return result; |
297 } | 297 } |
298 | 298 |
299 base::Value* PrefModelAssociator::MergeDictionaryValues( | 299 base::Value* PrefModelAssociator::MergeDictionaryValues( |
300 const base::Value& from_value, | 300 const base::Value& from_value, |
301 const base::Value& to_value) { | 301 const base::Value& to_value) { |
302 if (from_value.GetType() == base::Value::TYPE_NULL) | 302 if (from_value.GetType() == base::Value::Type::NONE) |
303 return to_value.DeepCopy(); | 303 return to_value.DeepCopy(); |
304 if (to_value.GetType() == base::Value::TYPE_NULL) | 304 if (to_value.GetType() == base::Value::Type::NONE) |
305 return from_value.DeepCopy(); | 305 return from_value.DeepCopy(); |
306 | 306 |
307 DCHECK_EQ(from_value.GetType(), base::Value::TYPE_DICTIONARY); | 307 DCHECK_EQ(from_value.GetType(), base::Value::Type::DICTIONARY); |
308 DCHECK_EQ(to_value.GetType(), base::Value::TYPE_DICTIONARY); | 308 DCHECK_EQ(to_value.GetType(), base::Value::Type::DICTIONARY); |
309 const base::DictionaryValue& from_dict_value = | 309 const base::DictionaryValue& from_dict_value = |
310 static_cast<const base::DictionaryValue&>(from_value); | 310 static_cast<const base::DictionaryValue&>(from_value); |
311 const base::DictionaryValue& to_dict_value = | 311 const base::DictionaryValue& to_dict_value = |
312 static_cast<const base::DictionaryValue&>(to_value); | 312 static_cast<const base::DictionaryValue&>(to_value); |
313 base::DictionaryValue* result = to_dict_value.DeepCopy(); | 313 base::DictionaryValue* result = to_dict_value.DeepCopy(); |
314 | 314 |
315 for (base::DictionaryValue::Iterator it(from_dict_value); !it.IsAtEnd(); | 315 for (base::DictionaryValue::Iterator it(from_dict_value); !it.IsAtEnd(); |
316 it.Advance()) { | 316 it.Advance()) { |
317 const base::Value* from_key_value = &it.value(); | 317 const base::Value* from_key_value = &it.value(); |
318 base::Value* to_key_value; | 318 base::Value* to_key_value; |
319 if (result->GetWithoutPathExpansion(it.key(), &to_key_value)) { | 319 if (result->GetWithoutPathExpansion(it.key(), &to_key_value)) { |
320 if (from_key_value->GetType() == base::Value::TYPE_DICTIONARY && | 320 if (from_key_value->GetType() == base::Value::Type::DICTIONARY && |
321 to_key_value->GetType() == base::Value::TYPE_DICTIONARY) { | 321 to_key_value->GetType() == base::Value::Type::DICTIONARY) { |
322 base::Value* merged_value = | 322 base::Value* merged_value = |
323 MergeDictionaryValues(*from_key_value, *to_key_value); | 323 MergeDictionaryValues(*from_key_value, *to_key_value); |
324 result->SetWithoutPathExpansion(it.key(), merged_value); | 324 result->SetWithoutPathExpansion(it.key(), merged_value); |
325 } | 325 } |
326 // Note that for all other types we want to preserve the "to" | 326 // Note that for all other types we want to preserve the "to" |
327 // values so we do nothing here. | 327 // values so we do nothing here. |
328 } else { | 328 } else { |
329 result->SetWithoutPathExpansion(it.key(), from_key_value->DeepCopy()); | 329 result->SetWithoutPathExpansion(it.key(), from_key_value->DeepCopy()); |
330 } | 330 } |
331 } | 331 } |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
525 bool from_sync) const { | 525 bool from_sync) const { |
526 auto observer_iter = synced_pref_observers_.find(path); | 526 auto observer_iter = synced_pref_observers_.find(path); |
527 if (observer_iter == synced_pref_observers_.end()) | 527 if (observer_iter == synced_pref_observers_.end()) |
528 return; | 528 return; |
529 SyncedPrefObserverList* observers = observer_iter->second.get(); | 529 SyncedPrefObserverList* observers = observer_iter->second.get(); |
530 for (auto& observer : *observers) | 530 for (auto& observer : *observers) |
531 observer.OnSyncedPrefChanged(path, from_sync); | 531 observer.OnSyncedPrefChanged(path, from_sync); |
532 } | 532 } |
533 | 533 |
534 } // namespace sync_preferences | 534 } // namespace sync_preferences |
OLD | NEW |