| 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/syncable_prefs/pref_model_associator.h" | 5 #include "components/syncable_prefs/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" |
| 11 #include "base/json/json_string_value_serializer.h" | 11 #include "base/json/json_string_value_serializer.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/stl_util.h" | |
| 16 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/values.h" | 16 #include "base/values.h" |
| 18 #include "components/prefs/pref_service.h" | 17 #include "components/prefs/pref_service.h" |
| 19 #include "components/sync/api/sync_change.h" | 18 #include "components/sync/api/sync_change.h" |
| 20 #include "components/sync/api/sync_error_factory.h" | 19 #include "components/sync/api/sync_error_factory.h" |
| 21 #include "components/sync/protocol/preference_specifics.pb.h" | 20 #include "components/sync/protocol/preference_specifics.pb.h" |
| 22 #include "components/sync/protocol/sync.pb.h" | 21 #include "components/sync/protocol/sync.pb.h" |
| 23 #include "components/syncable_prefs/pref_model_associator_client.h" | 22 #include "components/syncable_prefs/pref_model_associator_client.h" |
| 24 #include "components/syncable_prefs/pref_service_syncable.h" | 23 #include "components/syncable_prefs/pref_service_syncable.h" |
| 25 | 24 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 type_(type), | 62 type_(type), |
| 64 client_(client) { | 63 client_(client) { |
| 65 DCHECK(CalledOnValidThread()); | 64 DCHECK(CalledOnValidThread()); |
| 66 DCHECK(type_ == PREFERENCES || type_ == PRIORITY_PREFERENCES); | 65 DCHECK(type_ == PREFERENCES || type_ == PRIORITY_PREFERENCES); |
| 67 } | 66 } |
| 68 | 67 |
| 69 PrefModelAssociator::~PrefModelAssociator() { | 68 PrefModelAssociator::~PrefModelAssociator() { |
| 70 DCHECK(CalledOnValidThread()); | 69 DCHECK(CalledOnValidThread()); |
| 71 pref_service_ = NULL; | 70 pref_service_ = NULL; |
| 72 | 71 |
| 73 base::STLDeleteContainerPairSecondPointers(synced_pref_observers_.begin(), | |
| 74 synced_pref_observers_.end()); | |
| 75 synced_pref_observers_.clear(); | 72 synced_pref_observers_.clear(); |
| 76 } | 73 } |
| 77 | 74 |
| 78 void PrefModelAssociator::InitPrefAndAssociate( | 75 void PrefModelAssociator::InitPrefAndAssociate( |
| 79 const syncer::SyncData& sync_pref, | 76 const syncer::SyncData& sync_pref, |
| 80 const std::string& pref_name, | 77 const std::string& pref_name, |
| 81 syncer::SyncChangeList* sync_changes) { | 78 syncer::SyncChangeList* sync_changes) { |
| 82 const base::Value* user_pref_value = pref_service_->GetUserPrefValue( | 79 const base::Value* user_pref_value = pref_service_->GetUserPrefValue( |
| 83 pref_name.c_str()); | 80 pref_name.c_str()); |
| 84 VLOG(1) << "Associating preference " << pref_name; | 81 VLOG(1) << "Associating preference " << pref_name; |
| (...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 } | 432 } |
| 436 return value.release(); | 433 return value.release(); |
| 437 } | 434 } |
| 438 | 435 |
| 439 bool PrefModelAssociator::IsPrefSynced(const std::string& name) const { | 436 bool PrefModelAssociator::IsPrefSynced(const std::string& name) const { |
| 440 return synced_preferences_.find(name) != synced_preferences_.end(); | 437 return synced_preferences_.find(name) != synced_preferences_.end(); |
| 441 } | 438 } |
| 442 | 439 |
| 443 void PrefModelAssociator::AddSyncedPrefObserver(const std::string& name, | 440 void PrefModelAssociator::AddSyncedPrefObserver(const std::string& name, |
| 444 SyncedPrefObserver* observer) { | 441 SyncedPrefObserver* observer) { |
| 445 SyncedPrefObserverList* observers = synced_pref_observers_[name]; | 442 std::unique_ptr<SyncedPrefObserverList>& observers = |
| 446 if (observers == NULL) { | 443 synced_pref_observers_[name]; |
| 447 observers = new SyncedPrefObserverList; | 444 if (!observers) |
| 448 synced_pref_observers_[name] = observers; | 445 observers = base::MakeUnique<SyncedPrefObserverList>(); |
| 449 } | 446 |
| 450 observers->AddObserver(observer); | 447 observers->AddObserver(observer); |
| 451 } | 448 } |
| 452 | 449 |
| 453 void PrefModelAssociator::RemoveSyncedPrefObserver(const std::string& name, | 450 void PrefModelAssociator::RemoveSyncedPrefObserver(const std::string& name, |
| 454 SyncedPrefObserver* observer) { | 451 SyncedPrefObserver* observer) { |
| 455 SyncedPrefObserverMap::iterator observer_iter = | 452 auto observer_iter = synced_pref_observers_.find(name); |
| 456 synced_pref_observers_.find(name); | |
| 457 if (observer_iter == synced_pref_observers_.end()) | 453 if (observer_iter == synced_pref_observers_.end()) |
| 458 return; | 454 return; |
| 459 SyncedPrefObserverList* observers = observer_iter->second; | 455 SyncedPrefObserverList* observers = observer_iter->second.get(); |
| 460 observers->RemoveObserver(observer); | 456 observers->RemoveObserver(observer); |
| 461 } | 457 } |
| 462 | 458 |
| 463 void PrefModelAssociator::SetPrefModelAssociatorClientForTesting( | 459 void PrefModelAssociator::SetPrefModelAssociatorClientForTesting( |
| 464 const PrefModelAssociatorClient* client) { | 460 const PrefModelAssociatorClient* client) { |
| 465 DCHECK(!client_); | 461 DCHECK(!client_); |
| 466 client_ = client; | 462 client_ = client; |
| 467 } | 463 } |
| 468 | 464 |
| 469 std::set<std::string> PrefModelAssociator::registered_preferences() const { | 465 std::set<std::string> PrefModelAssociator::registered_preferences() const { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 531 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); | 527 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); |
| 532 } | 528 } |
| 533 | 529 |
| 534 void PrefModelAssociator::SetPrefService(PrefServiceSyncable* pref_service) { | 530 void PrefModelAssociator::SetPrefService(PrefServiceSyncable* pref_service) { |
| 535 DCHECK(pref_service_ == NULL); | 531 DCHECK(pref_service_ == NULL); |
| 536 pref_service_ = pref_service; | 532 pref_service_ = pref_service; |
| 537 } | 533 } |
| 538 | 534 |
| 539 void PrefModelAssociator::NotifySyncedPrefObservers(const std::string& path, | 535 void PrefModelAssociator::NotifySyncedPrefObservers(const std::string& path, |
| 540 bool from_sync) const { | 536 bool from_sync) const { |
| 541 SyncedPrefObserverMap::const_iterator observer_iter = | 537 auto observer_iter = synced_pref_observers_.find(path); |
| 542 synced_pref_observers_.find(path); | |
| 543 if (observer_iter == synced_pref_observers_.end()) | 538 if (observer_iter == synced_pref_observers_.end()) |
| 544 return; | 539 return; |
| 545 SyncedPrefObserverList* observers = observer_iter->second; | 540 SyncedPrefObserverList* observers = observer_iter->second.get(); |
| 546 FOR_EACH_OBSERVER(SyncedPrefObserver, *observers, | 541 FOR_EACH_OBSERVER(SyncedPrefObserver, *observers, |
| 547 OnSyncedPrefChanged(path, from_sync)); | 542 OnSyncedPrefChanged(path, from_sync)); |
| 548 } | 543 } |
| 549 | 544 |
| 550 } // namespace syncable_prefs | 545 } // namespace syncable_prefs |
| OLD | NEW |