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 #ifndef COMPONENTS_SYNCABLE_PREFS_SYNCED_PREF_CHANGE_REGISTRAR_H_
6 #define COMPONENTS_SYNCABLE_PREFS_SYNCED_PREF_CHANGE_REGISTRAR_H_
11 #include "base/callback.h"
12 #include "components/syncable_prefs/pref_service_syncable.h"
13 #include "components/syncable_prefs/synced_pref_observer.h"
15 namespace syncable_prefs
{
17 // Manages the registration of one or more SyncedPrefObservers on a
18 // PrefServiceSyncable. This is modeled after base::PrefChangeRegistrar, and
19 // it should be used whenever it's necessary to determine whether a pref change
20 // has come from sync or from some other mechanism (managed, UI, external, etc.)
21 class SyncedPrefChangeRegistrar
: public SyncedPrefObserver
{
23 // Registered callbacks may optionally take a path argument.
24 // The boolean argument indicates whether (true) or not (false)
25 // the change was a result of syncing.
26 typedef base::Callback
<void(bool)> ChangeCallback
;
27 typedef base::Callback
<void(const std::string
&, bool)> NamedChangeCallback
;
29 explicit SyncedPrefChangeRegistrar(PrefServiceSyncable
* pref_service
);
30 virtual ~SyncedPrefChangeRegistrar();
32 // Register an observer callback for sync change events on the pref at
33 // |path|. Only one callback may be registered per pref.
34 void Add(const char* path
, const ChangeCallback
& callback
);
35 void Add(const char* path
, const NamedChangeCallback
& callback
);
37 // Remove the registered observer for |path|.
38 void Remove(const char* path
);
40 // Remove all registered observers.
43 // Indicates whether or not an observer is already registered for |path|.
44 bool IsObserved(const char* path
) const;
47 // SyncedPrefObserver implementation
48 void OnSyncedPrefChanged(const std::string
& path
, bool from_sync
) override
;
50 typedef std::map
<std::string
, NamedChangeCallback
> ObserverMap
;
52 PrefServiceSyncable
* pref_service_
;
53 ObserverMap observers_
;
55 DISALLOW_COPY_AND_ASSIGN(SyncedPrefChangeRegistrar
);
58 } // namespace syncable_prefs
60 #endif // COMPONENTS_SYNCABLE_PREFS_SYNCED_PREF_CHANGE_REGISTRAR_H_