OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // A helper class that stays in sync with a preference (bool, int, real, | 5 // A helper class that stays in sync with a preference (bool, int, real, |
6 // string or filepath). For example: | 6 // string or filepath). For example: |
7 // | 7 // |
8 // class MyClass { | 8 // class MyClass { |
9 // public: | 9 // public: |
10 // MyClass(PrefService* prefs) { | 10 // MyClass(PrefService* prefs) { |
11 // my_string_.Init(prefs::kHomePage, prefs); | 11 // my_string_.Init(prefs::kHomePage, prefs); |
12 // } | 12 // } |
13 // private: | 13 // private: |
14 // StringPrefMember my_string_; | 14 // StringPrefMember my_string_; |
15 // }; | 15 // }; |
16 // | 16 // |
17 // my_string_ should stay in sync with the prefs::kHomePage pref and will | 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. | 18 // update if either the pref changes or if my_string_.SetValue is called. |
19 // | 19 // |
20 // An optional observer can be passed into the Init method which can be used to | 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 | 21 // notify MyClass of changes. Note that if you use SetValue(), the observer |
22 // will not be notified. | 22 // will not be notified. |
23 | 23 |
24 #ifndef COMPONENTS_PREFS_PREF_MEMBER_H_ | 24 #ifndef BASE_PREFS_PREF_MEMBER_H_ |
25 #define COMPONENTS_PREFS_PREF_MEMBER_H_ | 25 #define BASE_PREFS_PREF_MEMBER_H_ |
26 | 26 |
27 #include <string> | 27 #include <string> |
28 #include <vector> | 28 #include <vector> |
29 | 29 |
30 #include "base/bind.h" | 30 #include "base/bind.h" |
31 #include "base/callback_forward.h" | 31 #include "base/callback_forward.h" |
32 #include "base/files/file_path.h" | 32 #include "base/files/file_path.h" |
33 #include "base/logging.h" | 33 #include "base/logging.h" |
34 #include "base/macros.h" | 34 #include "base/macros.h" |
35 #include "base/memory/ref_counted.h" | 35 #include "base/memory/ref_counted.h" |
36 #include "base/single_thread_task_runner.h" | 36 #include "base/single_thread_task_runner.h" |
37 #include "base/values.h" | 37 #include "base/values.h" |
38 #include "components/prefs/base_prefs_export.h" | 38 #include "components/prefs/base_prefs_export.h" |
39 #include "components/prefs/pref_observer.h" | 39 #include "components/prefs/pref_observer.h" |
40 | 40 |
41 class PrefService; | 41 class PrefService; |
42 | 42 |
43 namespace subtle { | 43 namespace subtle { |
44 | 44 |
45 class COMPONENTS_PREFS_EXPORT PrefMemberBase : public PrefObserver { | 45 class BASE_PREFS_EXPORT PrefMemberBase : public PrefObserver { |
46 public: | 46 public: |
47 // Type of callback you can register if you need to know the name of | 47 // Type of callback you can register if you need to know the name of |
48 // the pref that is changing. | 48 // the pref that is changing. |
49 typedef base::Callback<void(const std::string&)> NamedChangeCallback; | 49 typedef base::Callback<void(const std::string&)> NamedChangeCallback; |
50 | 50 |
51 PrefService* prefs() { return prefs_; } | 51 PrefService* prefs() { return prefs_; } |
52 const PrefService* prefs() const { return prefs_; } | 52 const PrefService* prefs() const { return prefs_; } |
53 | 53 |
54 protected: | 54 protected: |
55 class COMPONENTS_PREFS_EXPORT Internal | 55 class BASE_PREFS_EXPORT Internal |
56 : public base::RefCountedThreadSafe<Internal> { | 56 : public base::RefCountedThreadSafe<Internal> { |
57 public: | 57 public: |
58 Internal(); | 58 Internal(); |
59 | 59 |
60 // Update the value, either by calling |UpdateValueInternal| directly | 60 // Update the value, either by calling |UpdateValueInternal| directly |
61 // or by dispatching to the right thread. | 61 // or by dispatching to the right thread. |
62 // Takes ownership of |value|. | 62 // Takes ownership of |value|. |
63 void UpdateValue(base::Value* value, | 63 void UpdateValue(base::Value* value, |
64 bool is_managed, | 64 bool is_managed, |
65 bool is_user_modifiable, | 65 bool is_user_modifiable, |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 std::string pref_name_; | 144 std::string pref_name_; |
145 NamedChangeCallback observer_; | 145 NamedChangeCallback observer_; |
146 PrefService* prefs_; | 146 PrefService* prefs_; |
147 | 147 |
148 protected: | 148 protected: |
149 bool setting_value_; | 149 bool setting_value_; |
150 }; | 150 }; |
151 | 151 |
152 // This function implements StringListPrefMember::UpdateValue(). | 152 // This function implements StringListPrefMember::UpdateValue(). |
153 // It is exposed here for testing purposes. | 153 // It is exposed here for testing purposes. |
154 bool COMPONENTS_PREFS_EXPORT PrefMemberVectorStringUpdate( | 154 bool BASE_PREFS_EXPORT PrefMemberVectorStringUpdate( |
155 const base::Value& value, | 155 const base::Value& value, |
156 std::vector<std::string>* string_vector); | 156 std::vector<std::string>* string_vector); |
157 | 157 |
158 } // namespace subtle | 158 } // namespace subtle |
159 | 159 |
160 template <typename ValueType> | 160 template <typename ValueType> |
161 class PrefMember : public subtle::PrefMemberBase { | 161 class PrefMember : public subtle::PrefMemberBase { |
162 public: | 162 public: |
163 // Defer initialization to an Init method so it's easy to make this class be | 163 // Defer initialization to an Init method so it's easy to make this class be |
164 // a member variable. | 164 // a member variable. |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 Internal() : value_(ValueType()) {} | 256 Internal() : value_(ValueType()) {} |
257 | 257 |
258 ValueType value() { | 258 ValueType value() { |
259 CheckOnCorrectThread(); | 259 CheckOnCorrectThread(); |
260 return value_; | 260 return value_; |
261 } | 261 } |
262 | 262 |
263 protected: | 263 protected: |
264 ~Internal() override {} | 264 ~Internal() override {} |
265 | 265 |
266 COMPONENTS_PREFS_EXPORT bool UpdateValueInternal( | 266 BASE_PREFS_EXPORT bool UpdateValueInternal( |
267 const base::Value& value) const override; | 267 const base::Value& value) const override; |
268 | 268 |
269 // We cache the value of the pref so we don't have to keep walking the pref | 269 // We cache the value of the pref so we don't have to keep walking the pref |
270 // tree. | 270 // tree. |
271 mutable ValueType value_; | 271 mutable ValueType value_; |
272 | 272 |
273 private: | 273 private: |
274 DISALLOW_COPY_AND_ASSIGN(Internal); | 274 DISALLOW_COPY_AND_ASSIGN(Internal); |
275 }; | 275 }; |
276 | 276 |
277 Internal* internal() const override { return internal_.get(); } | 277 Internal* internal() const override { return internal_.get(); } |
278 void CreateInternal() const override { internal_ = new Internal(); } | 278 void CreateInternal() const override { internal_ = new Internal(); } |
279 | 279 |
280 // This method is used to do the actual sync with pref of the specified type. | 280 // This method is used to do the actual sync with pref of the specified type. |
281 void COMPONENTS_PREFS_EXPORT UpdatePref(const ValueType& value); | 281 void BASE_PREFS_EXPORT UpdatePref(const ValueType& value); |
282 | 282 |
283 mutable scoped_refptr<Internal> internal_; | 283 mutable scoped_refptr<Internal> internal_; |
284 | 284 |
285 DISALLOW_COPY_AND_ASSIGN(PrefMember); | 285 DISALLOW_COPY_AND_ASSIGN(PrefMember); |
286 }; | 286 }; |
287 | 287 |
288 // Declaration of template specialization need to be repeated here | 288 // Declaration of template specialization need to be repeated here |
289 // specifically for each specialization (rather than just once above) | 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. | 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 | 291 // Specifically, it was failing on ChromeOS with a complaint about |
292 // PrefMember<FilePath>::UpdateValueInternal not being defined when | 292 // PrefMember<FilePath>::UpdateValueInternal not being defined when |
293 // built in a chroot with the following parameters: | 293 // built in a chroot with the following parameters: |
294 // | 294 // |
295 // FEATURES="noclean nostrip" USE="-chrome_debug -chrome_remoting | 295 // FEATURES="noclean nostrip" USE="-chrome_debug -chrome_remoting |
296 // -chrome_internal -chrome_pdf component_build" | 296 // -chrome_internal -chrome_pdf component_build" |
297 // ~/trunk/goma/goma-wrapper cros_chrome_make --board=${BOARD} | 297 // ~/trunk/goma/goma-wrapper cros_chrome_make --board=${BOARD} |
298 // --install --runhooks | 298 // --install --runhooks |
299 | 299 |
300 template <> | 300 template <> |
301 COMPONENTS_PREFS_EXPORT void PrefMember<bool>::UpdatePref(const bool& value); | 301 BASE_PREFS_EXPORT void PrefMember<bool>::UpdatePref(const bool& value); |
302 | 302 |
303 template <> | 303 template <> |
304 COMPONENTS_PREFS_EXPORT bool PrefMember<bool>::Internal::UpdateValueInternal( | 304 BASE_PREFS_EXPORT bool PrefMember<bool>::Internal::UpdateValueInternal( |
305 const base::Value& value) const; | 305 const base::Value& value) const; |
306 | 306 |
307 template <> | 307 template <> |
308 COMPONENTS_PREFS_EXPORT void PrefMember<int>::UpdatePref(const int& value); | 308 BASE_PREFS_EXPORT void PrefMember<int>::UpdatePref(const int& value); |
309 | 309 |
310 template <> | 310 template <> |
311 COMPONENTS_PREFS_EXPORT bool PrefMember<int>::Internal::UpdateValueInternal( | 311 BASE_PREFS_EXPORT bool PrefMember<int>::Internal::UpdateValueInternal( |
312 const base::Value& value) const; | 312 const base::Value& value) const; |
313 | 313 |
314 template <> | 314 template <> |
315 COMPONENTS_PREFS_EXPORT void | 315 BASE_PREFS_EXPORT void PrefMember<double>::UpdatePref(const double& value); |
316 PrefMember<double>::UpdatePref(const double& value); | |
317 | 316 |
318 template <> | 317 template <> |
319 COMPONENTS_PREFS_EXPORT bool PrefMember<double>::Internal::UpdateValueInternal( | 318 BASE_PREFS_EXPORT bool PrefMember<double>::Internal::UpdateValueInternal( |
320 const base::Value& value) const; | 319 const base::Value& value) const; |
321 | 320 |
322 template <> | 321 template <> |
323 COMPONENTS_PREFS_EXPORT void PrefMember<std::string>::UpdatePref( | 322 BASE_PREFS_EXPORT void PrefMember<std::string>::UpdatePref( |
324 const std::string& value); | 323 const std::string& value); |
325 | 324 |
326 template <> | 325 template <> |
327 COMPONENTS_PREFS_EXPORT bool | 326 BASE_PREFS_EXPORT bool PrefMember<std::string>::Internal::UpdateValueInternal( |
328 PrefMember<std::string>::Internal::UpdateValueInternal( | |
329 const base::Value& value) const; | 327 const base::Value& value) const; |
330 | 328 |
331 template <> | 329 template <> |
332 COMPONENTS_PREFS_EXPORT void PrefMember<base::FilePath>::UpdatePref( | 330 BASE_PREFS_EXPORT void PrefMember<base::FilePath>::UpdatePref( |
333 const base::FilePath& value); | 331 const base::FilePath& value); |
334 | 332 |
335 template <> | 333 template <> |
336 COMPONENTS_PREFS_EXPORT bool | 334 BASE_PREFS_EXPORT bool |
337 PrefMember<base::FilePath>::Internal::UpdateValueInternal( | 335 PrefMember<base::FilePath>::Internal::UpdateValueInternal( |
338 const base::Value& value) const; | 336 const base::Value& value) const; |
339 | 337 |
340 template <> | 338 template <> |
341 COMPONENTS_PREFS_EXPORT void PrefMember<std::vector<std::string>>::UpdatePref( | 339 BASE_PREFS_EXPORT void PrefMember<std::vector<std::string> >::UpdatePref( |
342 const std::vector<std::string>& value); | 340 const std::vector<std::string>& value); |
343 | 341 |
344 template <> | 342 template <> |
345 COMPONENTS_PREFS_EXPORT bool | 343 BASE_PREFS_EXPORT bool |
346 PrefMember<std::vector<std::string>>::Internal::UpdateValueInternal( | 344 PrefMember<std::vector<std::string> >::Internal::UpdateValueInternal( |
347 const base::Value& value) const; | 345 const base::Value& value) const; |
348 | 346 |
349 typedef PrefMember<bool> BooleanPrefMember; | 347 typedef PrefMember<bool> BooleanPrefMember; |
350 typedef PrefMember<int> IntegerPrefMember; | 348 typedef PrefMember<int> IntegerPrefMember; |
351 typedef PrefMember<double> DoublePrefMember; | 349 typedef PrefMember<double> DoublePrefMember; |
352 typedef PrefMember<std::string> StringPrefMember; | 350 typedef PrefMember<std::string> StringPrefMember; |
353 typedef PrefMember<base::FilePath> FilePathPrefMember; | 351 typedef PrefMember<base::FilePath> FilePathPrefMember; |
354 // This preference member is expensive for large string arrays. | 352 // This preference member is expensive for large string arrays. |
355 typedef PrefMember<std::vector<std::string>> StringListPrefMember; | 353 typedef PrefMember<std::vector<std::string> > StringListPrefMember; |
356 | 354 |
357 #endif // COMPONENTS_PREFS_PREF_MEMBER_H_ | 355 #endif // BASE_PREFS_PREF_MEMBER_H_ |
OLD | NEW |