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 CHROME_BROWSER_PREFS_SYNCED_PREF_CHANGE_REGISTRAR_H_
6 #define CHROME_BROWSER_PREFS_SYNCED_PREF_CHANGE_REGISTRAR_H_
11 #include "base/callback.h"
12 #include "chrome/browser/prefs/pref_service_syncable.h"
13 #include "chrome/browser/prefs/synced_pref_observer.h"
15 // Manages the registration of one or more SyncedPrefObservers on a
16 // PrefServiceSyncable. This is modeled after base::PrefChangeRegistrar, and
17 // it should be used whenever it's necessary to determine whether a pref change
18 // has come from sync or from some other mechanism (managed, UI, external, etc.)
19 class SyncedPrefChangeRegistrar
: public SyncedPrefObserver
{
21 // Registered callbacks may optionally take a path argument.
22 // The boolean argument indicates whether (true) or not (false)
23 // the change was a result of syncing.
24 typedef base::Callback
<void(bool)> ChangeCallback
;
25 typedef base::Callback
<void(const std::string
&, bool)> NamedChangeCallback
;
27 explicit SyncedPrefChangeRegistrar(PrefServiceSyncable
* pref_service
);
28 virtual ~SyncedPrefChangeRegistrar();
30 // Register an observer callback for sync change events on the pref at
31 // |path|. Only one callback may be registered per pref.
32 void Add(const char* path
, const ChangeCallback
& callback
);
33 void Add(const char* path
, const NamedChangeCallback
& callback
);
35 // Remove the registered observer for |path|.
36 void Remove(const char* path
);
38 // Remove all registered observers.
41 // Indicates whether or not an observer is already registered for |path|.
42 bool IsObserved(const char* path
) const;
45 // SyncedPrefObserver implementation
46 virtual void OnSyncedPrefChanged(const std::string
& path
, bool from_sync
)
49 typedef std::map
<std::string
, NamedChangeCallback
> ObserverMap
;
51 PrefServiceSyncable
* pref_service_
;
52 ObserverMap observers_
;
54 DISALLOW_COPY_AND_ASSIGN(SyncedPrefChangeRegistrar
);
57 #endif // CHROME_BROWSER_PREFS_SYNCED_PREF_CHANGE_REGISTRAR_H_