pref_member.h revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
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// A helper class that stays in sync with a preference (bool, int, real,
6// string or filepath).  For example:
7//
8// class MyClass {
9//  public:
10//   MyClass(PrefService* prefs) {
11//     my_string_.Init(prefs::kHomePage, prefs, NULL /* no observer */);
12//   }
13//  private:
14//   StringPrefMember my_string_;
15// };
16//
17// my_string_ should stay in sync with the prefs::kHomePage pref and will
18// update if either the pref changes or if my_string_.SetValue is called.
19//
20// An optional observer can be passed into the Init method which can be used to
21// notify MyClass of changes. Note that if you use SetValue(), the observer
22// will not be notified.
23
24#ifndef CHROME_BROWSER_PREFS_PREF_MEMBER_H_
25#define CHROME_BROWSER_PREFS_PREF_MEMBER_H_
26#pragma once
27
28#include <string>
29
30#include "base/basictypes.h"
31#include "base/file_path.h"
32#include "chrome/common/notification_observer.h"
33
34class PrefService;
35
36namespace subtle {
37
38class PrefMemberBase : public NotificationObserver {
39 protected:
40  PrefMemberBase();
41  virtual ~PrefMemberBase();
42
43  // See PrefMember<> for description.
44  void Init(const char* pref_name, PrefService* prefs,
45            NotificationObserver* observer);
46
47  // See PrefMember<> for description.
48  bool IsManaged() const;
49
50  // NotificationObserver
51  virtual void Observe(NotificationType type,
52                       const NotificationSource& source,
53                       const NotificationDetails& details);
54
55  void VerifyValuePrefName() const;
56
57  // This methods is used to do the actual sync with pref of the specified type.
58  // Note: this method is logically const, because it doesn't modify the state
59  // seen by the outside world. It is just doing a lazy load behind the scenes.
60  virtual void UpdateValueFromPref() const = 0;
61
62  const std::string& pref_name() const { return pref_name_; }
63  PrefService* prefs() { return prefs_; }
64  const PrefService* prefs() const { return prefs_; }
65
66 // Ordered the members to compact the class instance.
67 private:
68  std::string pref_name_;
69  NotificationObserver* observer_;
70  PrefService* prefs_;
71
72 protected:
73  mutable bool is_synced_;
74  bool setting_value_;
75};
76
77}  // namespace subtle
78
79
80template <typename ValueType>
81class PrefMember : public subtle::PrefMemberBase {
82 public:
83  // Defer initialization to an Init method so it's easy to make this class be
84  // a member variable.
85  PrefMember() { }
86  virtual ~PrefMember() { }
87
88  // Do the actual initialization of the class.  |observer| may be null if you
89  // don't want any notifications of changes.
90  void Init(const char* pref_name, PrefService* prefs,
91            NotificationObserver* observer) {
92    subtle::PrefMemberBase::Init(pref_name, prefs, observer);
93  }
94
95  // Check whether the pref is managed, i.e. controlled externally through
96  // enterprise configuration management (e.g. windows group policy). Returns
97  // false for unknown prefs.
98  bool IsManaged() {
99    return subtle::PrefMemberBase::IsManaged();
100  }
101
102  // Retrieve the value of the member variable.
103  ValueType GetValue() const {
104    VerifyValuePrefName();
105    // We lazily fetch the value from the pref service the first time GetValue
106    // is called.
107    if (!is_synced_) {
108      UpdateValueFromPref();
109      is_synced_ = true;
110    }
111    return value_;
112  }
113
114  // Provided as a convenience.
115  ValueType operator*() const {
116    return GetValue();
117  }
118
119  // Set the value of the member variable.
120  void SetValue(const ValueType& value) {
121    VerifyValuePrefName();
122    setting_value_ = true;
123    UpdatePref(value);
124    setting_value_ = false;
125  }
126
127  // Set the value of the member variable if it is not managed.
128  void SetValueIfNotManaged(const ValueType& value) {
129    if (!IsManaged()) {
130      SetValue(value);
131    }
132  }
133
134 protected:
135  // This methods is used to do the actual sync with pref of the specified type.
136  virtual void UpdatePref(const ValueType& value) = 0;
137
138  // We cache the value of the pref so we don't have to keep walking the pref
139  // tree.
140  mutable ValueType value_;
141};
142
143///////////////////////////////////////////////////////////////////////////////
144// Implementations of Boolean, Integer, Real, and String PrefMember below.
145
146class BooleanPrefMember : public PrefMember<bool> {
147 public:
148  BooleanPrefMember();
149  virtual ~BooleanPrefMember();
150
151 protected:
152  virtual void UpdateValueFromPref() const;
153  virtual void UpdatePref(const bool& value);
154
155 private:
156  DISALLOW_COPY_AND_ASSIGN(BooleanPrefMember);
157};
158
159class IntegerPrefMember : public PrefMember<int> {
160 public:
161  IntegerPrefMember();
162  virtual ~IntegerPrefMember();
163
164 protected:
165  virtual void UpdateValueFromPref() const;
166  virtual void UpdatePref(const int& value);
167
168 private:
169  DISALLOW_COPY_AND_ASSIGN(IntegerPrefMember);
170};
171
172class RealPrefMember : public PrefMember<double> {
173 public:
174  RealPrefMember();
175  virtual ~RealPrefMember();
176
177 protected:
178  virtual void UpdateValueFromPref() const;
179  virtual void UpdatePref(const double& value);
180
181 private:
182  DISALLOW_COPY_AND_ASSIGN(RealPrefMember);
183};
184
185class StringPrefMember : public PrefMember<std::string> {
186 public:
187  StringPrefMember();
188  virtual ~StringPrefMember();
189
190 protected:
191  virtual void UpdateValueFromPref() const;
192  virtual void UpdatePref(const std::string& value);
193
194 private:
195  DISALLOW_COPY_AND_ASSIGN(StringPrefMember);
196};
197
198class FilePathPrefMember : public PrefMember<FilePath> {
199 public:
200  FilePathPrefMember();
201  virtual ~FilePathPrefMember();
202
203 protected:
204  virtual void UpdateValueFromPref() const;
205  virtual void UpdatePref(const FilePath& value);
206
207 private:
208  DISALLOW_COPY_AND_ASSIGN(FilePathPrefMember);
209};
210
211#endif  // CHROME_BROWSER_PREFS_PREF_MEMBER_H_
212