Roll src/third_party/WebKit 48de1d8:ada7a3d (svn 202597:202598)
[chromium-blink-merge.git] / components / syncable_prefs / synced_pref_change_registrar.h
blob7c4a541fd2918dd4a473990a5ce82adadc966ecd
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_
8 #include <map>
9 #include <string>
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 {
22 public:
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.
41 void RemoveAll();
43 // Indicates whether or not an observer is already registered for |path|.
44 bool IsObserved(const char* path) const;
46 private:
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_