70c22fe3f43a7770da949382fa6d65a4f8afd54d
[platform/framework/web/crosswalk.git] / src / base / prefs / pref_change_registrar.h
1 // Copyright (c) 2010 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 BASE_PREFS_PREF_CHANGE_REGISTRAR_H_
6 #define BASE_PREFS_PREF_CHANGE_REGISTRAR_H_
7
8 #include <map>
9 #include <string>
10
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/prefs/base_prefs_export.h"
14 #include "base/prefs/pref_observer.h"
15
16 class PrefService;
17
18 // Automatically manages the registration of one or more pref change observers
19 // with a PrefStore. Functions much like NotificationRegistrar, but specifically
20 // manages observers of preference changes. When the Registrar is destroyed,
21 // all registered observers are automatically unregistered with the PrefStore.
22 class BASE_PREFS_EXPORT PrefChangeRegistrar : public PrefObserver {
23  public:
24   // You can register this type of callback if you need to know the
25   // path of the preference that is changing.
26   typedef base::Callback<void(const std::string&)> NamedChangeCallback;
27
28   PrefChangeRegistrar();
29   virtual ~PrefChangeRegistrar();
30
31   // Must be called before adding or removing observers. Can be called more
32   // than once as long as the value of |service| doesn't change.
33   void Init(PrefService* service);
34
35   // Adds a pref observer for the specified pref |path| and |obs| observer
36   // object. All registered observers will be automatically unregistered
37   // when the registrar's destructor is called.
38   //
39   // The second version binds a callback that will receive the path of
40   // the preference that is changing as its parameter.
41   //
42   // Only one observer may be registered per path.
43   void Add(const char* path, const base::Closure& obs);
44   void Add(const char* path, const NamedChangeCallback& obs);
45
46   // Removes the pref observer registered for |path|.
47   void Remove(const char* path);
48
49   // Removes all observers that have been previously added with a call to Add.
50   void RemoveAll();
51
52   // Returns true if no pref observers are registered.
53   bool IsEmpty() const;
54
55   // Check whether |pref| is in the set of preferences being observed.
56   bool IsObserved(const std::string& pref);
57
58   // Check whether any of the observed preferences has the managed bit set.
59   bool IsManaged();
60
61   // Return the PrefService for this registrar.
62   PrefService* prefs();
63   const PrefService* prefs() const;
64
65  private:
66   // PrefObserver:
67   void OnPreferenceChanged(PrefService* service,
68                            const std::string& pref_name) override;
69
70   static void InvokeUnnamedCallback(const base::Closure& callback,
71                                     const std::string& pref_name);
72
73   typedef std::map<std::string, NamedChangeCallback> ObserverMap;
74
75   ObserverMap observers_;
76   PrefService* service_;
77
78   DISALLOW_COPY_AND_ASSIGN(PrefChangeRegistrar);
79 };
80
81 #endif  // BASE_PREFS_PREF_CHANGE_REGISTRAR_H_