1 // Copyright (c) 2012 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.
5 // A helper class that stays in sync with a preference (bool, int, real,
6 // string or filepath). For example:
10 // MyClass(PrefService* prefs) {
11 // my_string_.Init(prefs::kHomePage, prefs);
14 // StringPrefMember my_string_;
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.
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.
24 #ifndef BASE_PREFS_PREF_MEMBER_H_
25 #define BASE_PREFS_PREF_MEMBER_H_
30 #include "base/basictypes.h"
31 #include "base/bind.h"
32 #include "base/callback_forward.h"
33 #include "base/files/file_path.h"
34 #include "base/logging.h"
35 #include "base/memory/ref_counted.h"
36 #include "base/prefs/base_prefs_export.h"
37 #include "base/prefs/pref_observer.h"
38 #include "base/single_thread_task_runner.h"
39 #include "base/values.h"
45 class BASE_PREFS_EXPORT PrefMemberBase
: public PrefObserver
{
47 // Type of callback you can register if you need to know the name of
48 // the pref that is changing.
49 typedef base::Callback
<void(const std::string
&)> NamedChangeCallback
;
51 PrefService
* prefs() { return prefs_
; }
52 const PrefService
* prefs() const { return prefs_
; }
55 class BASE_PREFS_EXPORT Internal
56 : public base::RefCountedThreadSafe
<Internal
> {
60 // Update the value, either by calling |UpdateValueInternal| directly
61 // or by dispatching to the right thread.
62 // Takes ownership of |value|.
63 void UpdateValue(base::Value
* value
,
65 bool is_user_modifiable
,
66 const base::Closure
& callback
) const;
68 void MoveToThread(scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
);
70 // See PrefMember<> for description.
71 bool IsManaged() const {
75 bool IsUserModifiable() const {
76 return is_user_modifiable_
;
80 friend class base::RefCountedThreadSafe
<Internal
>;
83 void CheckOnCorrectThread() const {
84 DCHECK(IsOnCorrectThread());
88 // This method actually updates the value. It should only be called from
89 // the thread the PrefMember is on.
90 virtual bool UpdateValueInternal(const base::Value
& value
) const = 0;
92 bool IsOnCorrectThread() const;
94 scoped_refptr
<base::SingleThreadTaskRunner
> thread_task_runner_
;
95 mutable bool is_managed_
;
96 mutable bool is_user_modifiable_
;
98 DISALLOW_COPY_AND_ASSIGN(Internal
);
102 virtual ~PrefMemberBase();
104 // See PrefMember<> for description.
105 void Init(const std::string
& pref_name
,
107 const NamedChangeCallback
& observer
);
108 void Init(const std::string
& pref_name
, PrefService
* prefs
);
110 virtual void CreateInternal() const = 0;
112 // See PrefMember<> for description.
115 void MoveToThread(scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
);
118 void OnPreferenceChanged(PrefService
* service
,
119 const std::string
& pref_name
) override
;
121 void VerifyValuePrefName() const {
122 DCHECK(!pref_name_
.empty());
125 // This method is used to do the actual sync with the preference.
126 // Note: it is logically const, because it doesn't modify the state
127 // seen by the outside world. It is just doing a lazy load behind the scenes.
128 void UpdateValueFromPref(const base::Closure
& callback
) const;
130 // Verifies the preference name, and lazily loads the preference value if
131 // it hasn't been loaded yet.
132 void VerifyPref() const;
134 const std::string
& pref_name() const { return pref_name_
; }
136 virtual Internal
* internal() const = 0;
138 // Used to allow registering plain base::Closure callbacks.
139 static void InvokeUnnamedCallback(const base::Closure
& callback
,
140 const std::string
& pref_name
);
143 // Ordered the members to compact the class instance.
144 std::string pref_name_
;
145 NamedChangeCallback observer_
;
152 // This function implements StringListPrefMember::UpdateValue().
153 // It is exposed here for testing purposes.
154 bool BASE_PREFS_EXPORT
PrefMemberVectorStringUpdate(
155 const base::Value
& value
,
156 std::vector
<std::string
>* string_vector
);
158 } // namespace subtle
160 template <typename ValueType
>
161 class PrefMember
: public subtle::PrefMemberBase
{
163 // Defer initialization to an Init method so it's easy to make this class be
164 // a member variable.
166 virtual ~PrefMember() {}
168 // Do the actual initialization of the class. Use the two-parameter
169 // version if you don't want any notifications of changes. This
170 // method should only be called on the UI thread.
171 void Init(const std::string
& pref_name
,
173 const NamedChangeCallback
& observer
) {
174 subtle::PrefMemberBase::Init(pref_name
, prefs
, observer
);
176 void Init(const std::string
& pref_name
,
178 const base::Closure
& observer
) {
179 subtle::PrefMemberBase::Init(
181 base::Bind(&PrefMemberBase::InvokeUnnamedCallback
, observer
));
183 void Init(const std::string
& pref_name
, PrefService
* prefs
) {
184 subtle::PrefMemberBase::Init(pref_name
, prefs
);
187 // Unsubscribes the PrefMember from the PrefService. After calling this
188 // function, the PrefMember may not be used any more on the UI thread.
189 // Assuming |MoveToThread| was previously called, |GetValue|, |IsManaged|,
190 // and |IsUserModifiable| can still be called from the other thread but
191 // the results will no longer update from the PrefService.
192 // This method should only be called on the UI thread.
194 subtle::PrefMemberBase::Destroy();
197 // Moves the PrefMember to another thread, allowing read accesses from there.
198 // Changes from the PrefService will be propagated asynchronously
200 // This method should only be used from the thread the PrefMember is currently
201 // on, which is the UI thread by default.
202 void MoveToThread(scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
) {
203 subtle::PrefMemberBase::MoveToThread(task_runner
);
206 // Check whether the pref is managed, i.e. controlled externally through
207 // enterprise configuration management (e.g. windows group policy). Returns
208 // false for unknown prefs.
209 // This method should only be used from the thread the PrefMember is currently
210 // on, which is the UI thread unless changed by |MoveToThread|.
211 bool IsManaged() const {
213 return internal_
->IsManaged();
216 // Checks whether the pref can be modified by the user. This returns false
217 // when the pref is managed by a policy or an extension, and when a command
218 // line flag overrides the pref.
219 // This method should only be used from the thread the PrefMember is currently
220 // on, which is the UI thread unless changed by |MoveToThread|.
221 bool IsUserModifiable() const {
223 return internal_
->IsUserModifiable();
226 // Retrieve the value of the member variable.
227 // This method should only be used from the thread the PrefMember is currently
228 // on, which is the UI thread unless changed by |MoveToThread|.
229 ValueType
GetValue() const {
231 return internal_
->value();
234 // Provided as a convenience.
235 ValueType
operator*() const {
239 // Set the value of the member variable.
240 // This method should only be called on the UI thread.
241 void SetValue(const ValueType
& value
) {
242 VerifyValuePrefName();
243 setting_value_
= true;
245 setting_value_
= false;
248 // Returns the pref name.
249 const std::string
& GetPrefName() const {
254 class Internal
: public subtle::PrefMemberBase::Internal
{
256 Internal() : value_(ValueType()) {}
259 CheckOnCorrectThread();
264 ~Internal() override
{}
266 BASE_PREFS_EXPORT
bool UpdateValueInternal(
267 const base::Value
& value
) const override
;
269 // We cache the value of the pref so we don't have to keep walking the pref
271 mutable ValueType value_
;
274 DISALLOW_COPY_AND_ASSIGN(Internal
);
277 Internal
* internal() const override
{ return internal_
.get(); }
278 void CreateInternal() const override
{ internal_
= new Internal(); }
280 // This method is used to do the actual sync with pref of the specified type.
281 void BASE_PREFS_EXPORT
UpdatePref(const ValueType
& value
);
283 mutable scoped_refptr
<Internal
> internal_
;
285 DISALLOW_COPY_AND_ASSIGN(PrefMember
);
288 // Declaration of template specialization need to be repeated here
289 // specifically for each specialization (rather than just once above)
290 // or at least one of our compilers won't be happy in all cases.
291 // Specifically, it was failing on ChromeOS with a complaint about
292 // PrefMember<FilePath>::UpdateValueInternal not being defined when
293 // built in a chroot with the following parameters:
295 // FEATURES="noclean nostrip" USE="-chrome_debug -chrome_remoting
296 // -chrome_internal -chrome_pdf component_build"
297 // ~/trunk/goma/goma-wrapper cros_chrome_make --board=${BOARD}
298 // --install --runhooks
301 BASE_PREFS_EXPORT
void PrefMember
<bool>::UpdatePref(const bool& value
);
304 BASE_PREFS_EXPORT
bool PrefMember
<bool>::Internal::UpdateValueInternal(
305 const base::Value
& value
) const;
308 BASE_PREFS_EXPORT
void PrefMember
<int>::UpdatePref(const int& value
);
311 BASE_PREFS_EXPORT
bool PrefMember
<int>::Internal::UpdateValueInternal(
312 const base::Value
& value
) const;
315 BASE_PREFS_EXPORT
void PrefMember
<double>::UpdatePref(const double& value
);
318 BASE_PREFS_EXPORT
bool PrefMember
<double>::Internal::UpdateValueInternal(
319 const base::Value
& value
) const;
322 BASE_PREFS_EXPORT
void PrefMember
<std::string
>::UpdatePref(
323 const std::string
& value
);
326 BASE_PREFS_EXPORT
bool PrefMember
<std::string
>::Internal::UpdateValueInternal(
327 const base::Value
& value
) const;
330 BASE_PREFS_EXPORT
void PrefMember
<base::FilePath
>::UpdatePref(
331 const base::FilePath
& value
);
334 BASE_PREFS_EXPORT
bool
335 PrefMember
<base::FilePath
>::Internal::UpdateValueInternal(
336 const base::Value
& value
) const;
339 BASE_PREFS_EXPORT
void PrefMember
<std::vector
<std::string
> >::UpdatePref(
340 const std::vector
<std::string
>& value
);
343 BASE_PREFS_EXPORT
bool
344 PrefMember
<std::vector
<std::string
> >::Internal::UpdateValueInternal(
345 const base::Value
& value
) const;
347 typedef PrefMember
<bool> BooleanPrefMember
;
348 typedef PrefMember
<int> IntegerPrefMember
;
349 typedef PrefMember
<double> DoublePrefMember
;
350 typedef PrefMember
<std::string
> StringPrefMember
;
351 typedef PrefMember
<base::FilePath
> FilePathPrefMember
;
352 // This preference member is expensive for large string arrays.
353 typedef PrefMember
<std::vector
<std::string
> > StringListPrefMember
;
355 #endif // BASE_PREFS_PREF_MEMBER_H_