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