1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/syncable_prefs/synced_pref_change_registrar.h"
9 namespace syncable_prefs
{
13 void InvokeUnnamedCallback(
14 const SyncedPrefChangeRegistrar::ChangeCallback
& callback
,
15 const std::string
& path
,
17 callback
.Run(from_sync
);
22 SyncedPrefChangeRegistrar::SyncedPrefChangeRegistrar(
23 PrefServiceSyncable
* pref_service
) {
24 pref_service_
= pref_service
;
27 SyncedPrefChangeRegistrar::~SyncedPrefChangeRegistrar() {
31 void SyncedPrefChangeRegistrar::Add(const char *path
,
32 const ChangeCallback
& callback
) {
33 Add(path
, base::Bind(InvokeUnnamedCallback
, callback
));
36 void SyncedPrefChangeRegistrar::Add(const char *path
,
37 const NamedChangeCallback
& callback
) {
38 DCHECK(!IsObserved(path
));
39 observers_
[path
] = callback
;
40 pref_service_
->AddSyncedPrefObserver(path
, this);
43 void SyncedPrefChangeRegistrar::Remove(const char *path
) {
44 observers_
.erase(path
);
45 pref_service_
->RemoveSyncedPrefObserver(path
, this);
48 void SyncedPrefChangeRegistrar::RemoveAll() {
49 for (ObserverMap::iterator iter
= observers_
.begin();
50 iter
!= observers_
.end(); ++iter
) {
51 pref_service_
->RemoveSyncedPrefObserver(iter
->first
, this);
56 bool SyncedPrefChangeRegistrar::IsObserved(const char* path
) const {
57 return observers_
.find(path
) != observers_
.end();
60 void SyncedPrefChangeRegistrar::OnSyncedPrefChanged(const std::string
& path
,
62 if (pref_service_
->IsManagedPreference(path
.c_str()))
64 ObserverMap::const_iterator iter
= observers_
.find(path
);
65 if (iter
== observers_
.end())
67 iter
->second
.Run(path
, from_sync
);
70 } // namespace syncable_prefs