summaryrefslogtreecommitdiffstats
path: root/base/prefs/pref_change_registrar.h
blob: a914bea7d48cec7c4cf2149d7525c54dc10a76b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_PREFS_PREF_CHANGE_REGISTRAR_H_
#define BASE_PREFS_PREF_CHANGE_REGISTRAR_H_

#include <map>
#include <string>

#include "base/basictypes.h"
#include "base/callback.h"
#include "base/prefs/base_prefs_export.h"
#include "base/prefs/pref_observer.h"

class PrefService;

// Automatically manages the registration of one or more pref change observers
// with a PrefStore. Functions much like NotificationRegistrar, but specifically
// manages observers of preference changes. When the Registrar is destroyed,
// all registered observers are automatically unregistered with the PrefStore.
class BASE_PREFS_EXPORT PrefChangeRegistrar : public PrefObserver {
 public:
  // You can register this type of callback if you need to know the
  // path of the preference that is changing.
  typedef base::Callback<void(const std::string&)> NamedChangeCallback;

  PrefChangeRegistrar();
  virtual ~PrefChangeRegistrar();

  // Must be called before adding or removing observers. Can be called more
  // than once as long as the value of |service| doesn't change.
  void Init(PrefService* service);

  // Adds a pref observer for the specified pref |path| and |obs| observer
  // object. All registered observers will be automatically unregistered
  // when the registrar's destructor is called.
  //
  // The second version binds a callback that will receive the path of
  // the preference that is changing as its parameter.
  //
  // Only one observer may be registered per path.
  void Add(const char* path, const base::Closure& obs);
  void Add(const char* path, const NamedChangeCallback& obs);

  // Removes the pref observer registered for |path|.
  void Remove(const char* path);

  // Removes all observers that have been previously added with a call to Add.
  void RemoveAll();

  // Returns true if no pref observers are registered.
  bool IsEmpty() const;

  // Check whether |pref| is in the set of preferences being observed.
  bool IsObserved(const std::string& pref);

  // Check whether any of the observed preferences has the managed bit set.
  bool IsManaged();

  // Return the PrefService for this registrar.
  PrefService* prefs();
  const PrefService* prefs() const;

 private:
  // PrefObserver:
  virtual void OnPreferenceChanged(PrefService* service,
                                   const std::string& pref_name) OVERRIDE;

  static void InvokeUnnamedCallback(const base::Closure& callback,
                                    const std::string& pref_name);

  typedef std::map<std::string, NamedChangeCallback> ObserverMap;

  ObserverMap observers_;
  PrefService* service_;

  DISALLOW_COPY_AND_ASSIGN(PrefChangeRegistrar);
};

#endif  // BASE_PREFS_PREF_CHANGE_REGISTRAR_H_