Home | History | Annotate | Download | only in prefs
      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 CHROME_BROWSER_PREFS_SYNCED_PREF_CHANGE_REGISTRAR_H_
      6 #define CHROME_BROWSER_PREFS_SYNCED_PREF_CHANGE_REGISTRAR_H_
      7 
      8 #include <map>
      9 #include <string>
     10 
     11 #include "base/callback.h"
     12 #include "chrome/browser/prefs/pref_service_syncable.h"
     13 #include "chrome/browser/prefs/synced_pref_observer.h"
     14 
     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 {
     20  public:
     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;
     26 
     27   explicit SyncedPrefChangeRegistrar(PrefServiceSyncable* pref_service);
     28   virtual ~SyncedPrefChangeRegistrar();
     29 
     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);
     34 
     35   // Remove the registered observer for |path|.
     36   void Remove(const char* path);
     37 
     38   // Remove all registered observers.
     39   void RemoveAll();
     40 
     41   // Indicates whether or not an observer is already registered for |path|.
     42   bool IsObserved(const char* path) const;
     43 
     44  private:
     45   // SyncedPrefObserver implementation
     46   virtual void OnSyncedPrefChanged(const std::string& path, bool from_sync)
     47       OVERRIDE;
     48 
     49   typedef std::map<std::string, NamedChangeCallback> ObserverMap;
     50 
     51   PrefServiceSyncable* pref_service_;
     52   ObserverMap observers_;
     53 
     54   DISALLOW_COPY_AND_ASSIGN(SyncedPrefChangeRegistrar);
     55 };
     56 
     57 #endif  // CHROME_BROWSER_PREFS_SYNCED_PREF_CHANGE_REGISTRAR_H_
     58