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 <algorithm> |
| 8 #include <iterator> |
7 #include <utility> | 9 #include <utility> |
8 | 10 |
9 #include "base/auto_reset.h" | 11 #include "base/auto_reset.h" |
10 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
11 #include "base/json/json_string_value_serializer.h" | 13 #include "base/json/json_string_value_serializer.h" |
12 #include "base/location.h" | 14 #include "base/location.h" |
13 #include "base/logging.h" | 15 #include "base/logging.h" |
14 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
15 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
16 #include "base/values.h" | 18 #include "base/values.h" |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 | 233 |
232 std::unique_ptr<base::Value> PrefModelAssociator::MergePreference( | 234 std::unique_ptr<base::Value> PrefModelAssociator::MergePreference( |
233 const std::string& name, | 235 const std::string& name, |
234 const base::Value& local_value, | 236 const base::Value& local_value, |
235 const base::Value& server_value) { | 237 const base::Value& server_value) { |
236 // This function special cases preferences individually, so don't attempt | 238 // This function special cases preferences individually, so don't attempt |
237 // to merge for all migrated values. | 239 // to merge for all migrated values. |
238 if (client_) { | 240 if (client_) { |
239 std::string new_pref_name; | 241 std::string new_pref_name; |
240 if (client_->IsMergeableListPreference(name)) | 242 if (client_->IsMergeableListPreference(name)) |
241 return base::WrapUnique(MergeListValues(local_value, server_value)); | 243 return MergeListValues(local_value, server_value); |
242 if (client_->IsMergeableDictionaryPreference(name)) | 244 if (client_->IsMergeableDictionaryPreference(name)) |
243 return base::WrapUnique(MergeDictionaryValues(local_value, server_value)); | 245 return MergeDictionaryValues(local_value, server_value); |
244 } | 246 } |
245 | 247 |
246 // If this is not a specially handled preference, server wins. | 248 // If this is not a specially handled preference, server wins. |
247 return base::WrapUnique(server_value.DeepCopy()); | 249 return base::WrapUnique(server_value.DeepCopy()); |
248 } | 250 } |
249 | 251 |
250 bool PrefModelAssociator::CreatePrefSyncData( | 252 bool PrefModelAssociator::CreatePrefSyncData( |
251 const std::string& name, | 253 const std::string& name, |
252 const base::Value& value, | 254 const base::Value& value, |
253 syncer::SyncData* sync_data) const { | 255 syncer::SyncData* sync_data) const { |
(...skipping 14 matching lines...) Expand all Loading... |
268 sync_pb::EntitySpecifics specifics; | 270 sync_pb::EntitySpecifics specifics; |
269 sync_pb::PreferenceSpecifics* pref_specifics = | 271 sync_pb::PreferenceSpecifics* pref_specifics = |
270 GetMutableSpecifics(type_, &specifics); | 272 GetMutableSpecifics(type_, &specifics); |
271 | 273 |
272 pref_specifics->set_name(name); | 274 pref_specifics->set_name(name); |
273 pref_specifics->set_value(serialized); | 275 pref_specifics->set_value(serialized); |
274 *sync_data = syncer::SyncData::CreateLocalData(name, name, specifics); | 276 *sync_data = syncer::SyncData::CreateLocalData(name, name, specifics); |
275 return true; | 277 return true; |
276 } | 278 } |
277 | 279 |
278 base::Value* PrefModelAssociator::MergeListValues(const base::Value& from_value, | 280 std::unique_ptr<base::Value> PrefModelAssociator::MergeListValues( |
279 const base::Value& to_value) { | 281 const base::Value& from_value, |
| 282 const base::Value& to_value) { |
280 if (from_value.GetType() == base::Value::Type::NONE) | 283 if (from_value.GetType() == base::Value::Type::NONE) |
281 return to_value.DeepCopy(); | 284 return base::MakeUnique<base::Value>(to_value); |
282 if (to_value.GetType() == base::Value::Type::NONE) | 285 if (to_value.GetType() == base::Value::Type::NONE) |
283 return from_value.DeepCopy(); | 286 return base::MakeUnique<base::Value>(from_value); |
284 | 287 |
285 DCHECK(from_value.GetType() == base::Value::Type::LIST); | 288 DCHECK(from_value.GetType() == base::Value::Type::LIST); |
286 DCHECK(to_value.GetType() == base::Value::Type::LIST); | 289 DCHECK(to_value.GetType() == base::Value::Type::LIST); |
287 const base::ListValue& from_list_value = | 290 const base::ListValue& from_list_value = |
288 static_cast<const base::ListValue&>(from_value); | 291 static_cast<const base::ListValue&>(from_value); |
289 const base::ListValue& to_list_value = | 292 const base::ListValue& to_list_value = |
290 static_cast<const base::ListValue&>(to_value); | 293 static_cast<const base::ListValue&>(to_value); |
291 base::ListValue* result = to_list_value.DeepCopy(); | |
292 | 294 |
293 for (const auto& value : from_list_value) { | 295 auto result = base::MakeUnique<base::ListValue>(to_list_value); |
294 result->AppendIfNotPresent(value.CreateDeepCopy()); | 296 base::Value::ListStorage& list = result->GetList(); |
295 } | 297 std::copy_if( |
296 return result; | 298 from_list_value.GetList().begin(), from_list_value.GetList().end(), |
| 299 std::back_inserter(list), [&list](const base::Value& value) { |
| 300 return std::find(list.begin(), list.end(), value) == list.end(); |
| 301 }); |
| 302 return std::move(result); |
297 } | 303 } |
298 | 304 |
299 base::Value* PrefModelAssociator::MergeDictionaryValues( | 305 std::unique_ptr<base::Value> PrefModelAssociator::MergeDictionaryValues( |
300 const base::Value& from_value, | 306 const base::Value& from_value, |
301 const base::Value& to_value) { | 307 const base::Value& to_value) { |
302 if (from_value.GetType() == base::Value::Type::NONE) | 308 if (from_value.GetType() == base::Value::Type::NONE) |
303 return to_value.DeepCopy(); | 309 return base::MakeUnique<base::Value>(to_value); |
304 if (to_value.GetType() == base::Value::Type::NONE) | 310 if (to_value.GetType() == base::Value::Type::NONE) |
305 return from_value.DeepCopy(); | 311 return base::MakeUnique<base::Value>(from_value); |
306 | 312 |
307 DCHECK_EQ(from_value.GetType(), base::Value::Type::DICTIONARY); | 313 DCHECK_EQ(from_value.GetType(), base::Value::Type::DICTIONARY); |
308 DCHECK_EQ(to_value.GetType(), base::Value::Type::DICTIONARY); | 314 DCHECK_EQ(to_value.GetType(), base::Value::Type::DICTIONARY); |
309 const base::DictionaryValue& from_dict_value = | 315 const base::DictionaryValue& from_dict_value = |
310 static_cast<const base::DictionaryValue&>(from_value); | 316 static_cast<const base::DictionaryValue&>(from_value); |
311 const base::DictionaryValue& to_dict_value = | 317 const base::DictionaryValue& to_dict_value = |
312 static_cast<const base::DictionaryValue&>(to_value); | 318 static_cast<const base::DictionaryValue&>(to_value); |
313 base::DictionaryValue* result = to_dict_value.DeepCopy(); | 319 auto result = base::MakeUnique<base::DictionaryValue>(to_dict_value); |
314 | 320 |
315 for (base::DictionaryValue::Iterator it(from_dict_value); !it.IsAtEnd(); | 321 for (base::DictionaryValue::Iterator it(from_dict_value); !it.IsAtEnd(); |
316 it.Advance()) { | 322 it.Advance()) { |
317 const base::Value* from_key_value = &it.value(); | 323 const base::Value* from_key_value = &it.value(); |
318 base::Value* to_key_value; | 324 base::Value* to_key_value; |
319 if (result->GetWithoutPathExpansion(it.key(), &to_key_value)) { | 325 if (result->GetWithoutPathExpansion(it.key(), &to_key_value)) { |
320 if (from_key_value->GetType() == base::Value::Type::DICTIONARY && | 326 if (from_key_value->GetType() == base::Value::Type::DICTIONARY && |
321 to_key_value->GetType() == base::Value::Type::DICTIONARY) { | 327 to_key_value->GetType() == base::Value::Type::DICTIONARY) { |
322 base::Value* merged_value = | 328 std::unique_ptr<base::Value> merged_value = |
323 MergeDictionaryValues(*from_key_value, *to_key_value); | 329 MergeDictionaryValues(*from_key_value, *to_key_value); |
324 result->SetWithoutPathExpansion(it.key(), merged_value); | 330 result->SetWithoutPathExpansion(it.key(), std::move(merged_value)); |
325 } | 331 } |
326 // Note that for all other types we want to preserve the "to" | 332 // Note that for all other types we want to preserve the "to" |
327 // values so we do nothing here. | 333 // values so we do nothing here. |
328 } else { | 334 } else { |
329 result->SetWithoutPathExpansion(it.key(), from_key_value->DeepCopy()); | 335 result->SetWithoutPathExpansion( |
| 336 it.key(), base::MakeUnique<base::Value>(*from_key_value)); |
330 } | 337 } |
331 } | 338 } |
332 return result; | 339 return std::move(result); |
333 } | 340 } |
334 | 341 |
335 // Note: This will build a model of all preferences registered as syncable | 342 // Note: This will build a model of all preferences registered as syncable |
336 // with user controlled data. We do not track any information for preferences | 343 // with user controlled data. We do not track any information for preferences |
337 // not registered locally as syncable and do not inform the syncer of | 344 // not registered locally as syncable and do not inform the syncer of |
338 // non-user controlled preferences. | 345 // non-user controlled preferences. |
339 syncer::SyncDataList PrefModelAssociator::GetAllSyncData( | 346 syncer::SyncDataList PrefModelAssociator::GetAllSyncData( |
340 syncer::ModelType type) const { | 347 syncer::ModelType type) const { |
341 DCHECK_EQ(type_, type); | 348 DCHECK_EQ(type_, type); |
342 syncer::SyncDataList current_data; | 349 syncer::SyncDataList current_data; |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
524 bool from_sync) const { | 531 bool from_sync) const { |
525 auto observer_iter = synced_pref_observers_.find(path); | 532 auto observer_iter = synced_pref_observers_.find(path); |
526 if (observer_iter == synced_pref_observers_.end()) | 533 if (observer_iter == synced_pref_observers_.end()) |
527 return; | 534 return; |
528 SyncedPrefObserverList* observers = observer_iter->second.get(); | 535 SyncedPrefObserverList* observers = observer_iter->second.get(); |
529 for (auto& observer : *observers) | 536 for (auto& observer : *observers) |
530 observer.OnSyncedPrefChanged(path, from_sync); | 537 observer.OnSyncedPrefChanged(path, from_sync); |
531 } | 538 } |
532 | 539 |
533 } // namespace sync_preferences | 540 } // namespace sync_preferences |
OLD | NEW |