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) { |
(...skipping 29 matching lines...) Expand all Loading... |
40 class PrefServiceBase; | 40 class PrefServiceBase; |
41 | 41 |
42 namespace subtle { | 42 namespace subtle { |
43 | 43 |
44 class PrefMemberBase : public PrefObserver { | 44 class PrefMemberBase : public PrefObserver { |
45 public: | 45 public: |
46 // Type of callback you can register if you need to know the name of | 46 // Type of callback you can register if you need to know the name of |
47 // the pref that is changing. | 47 // the pref that is changing. |
48 typedef base::Callback<void(const std::string&)> NamedChangeCallback; | 48 typedef base::Callback<void(const std::string&)> NamedChangeCallback; |
49 | 49 |
| 50 PrefServiceBase* prefs() { return prefs_; } |
| 51 const PrefServiceBase* prefs() const { return prefs_; } |
| 52 |
50 protected: | 53 protected: |
51 class Internal : public base::RefCountedThreadSafe<Internal> { | 54 class Internal : public base::RefCountedThreadSafe<Internal> { |
52 public: | 55 public: |
53 Internal(); | 56 Internal(); |
54 | 57 |
55 // Update the value, either by calling |UpdateValueInternal| directly | 58 // Update the value, either by calling |UpdateValueInternal| directly |
56 // or by dispatching to the right thread. | 59 // or by dispatching to the right thread. |
57 // Takes ownership of |value|. | 60 // Takes ownership of |value|. |
58 void UpdateValue(base::Value* value, | 61 void UpdateValue(base::Value* value, |
59 bool is_managed, | 62 bool is_managed, |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 // This method is used to do the actual sync with the preference. | 121 // This method is used to do the actual sync with the preference. |
119 // Note: it is logically const, because it doesn't modify the state | 122 // Note: it is logically const, because it doesn't modify the state |
120 // seen by the outside world. It is just doing a lazy load behind the scenes. | 123 // seen by the outside world. It is just doing a lazy load behind the scenes. |
121 void UpdateValueFromPref() const; | 124 void UpdateValueFromPref() const; |
122 | 125 |
123 // Verifies the preference name, and lazily loads the preference value if | 126 // Verifies the preference name, and lazily loads the preference value if |
124 // it hasn't been loaded yet. | 127 // it hasn't been loaded yet. |
125 void VerifyPref() const; | 128 void VerifyPref() const; |
126 | 129 |
127 const std::string& pref_name() const { return pref_name_; } | 130 const std::string& pref_name() const { return pref_name_; } |
128 PrefServiceBase* prefs() { return prefs_; } | |
129 const PrefServiceBase* prefs() const { return prefs_; } | |
130 | 131 |
131 virtual Internal* internal() const = 0; | 132 virtual Internal* internal() const = 0; |
132 | 133 |
133 // Used to allow registering plain base::Closure callbacks. | 134 // Used to allow registering plain base::Closure callbacks. |
134 static void InvokeUnnamedCallback(const base::Closure& callback, | 135 static void InvokeUnnamedCallback(const base::Closure& callback, |
135 const std::string& pref_name); | 136 const std::string& pref_name); |
136 | 137 |
137 private: | 138 private: |
138 // Ordered the members to compact the class instance. | 139 // Ordered the members to compact the class instance. |
139 std::string pref_name_; | 140 std::string pref_name_; |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 | 291 |
291 typedef PrefMember<bool> BooleanPrefMember; | 292 typedef PrefMember<bool> BooleanPrefMember; |
292 typedef PrefMember<int> IntegerPrefMember; | 293 typedef PrefMember<int> IntegerPrefMember; |
293 typedef PrefMember<double> DoublePrefMember; | 294 typedef PrefMember<double> DoublePrefMember; |
294 typedef PrefMember<std::string> StringPrefMember; | 295 typedef PrefMember<std::string> StringPrefMember; |
295 typedef PrefMember<FilePath> FilePathPrefMember; | 296 typedef PrefMember<FilePath> FilePathPrefMember; |
296 // This preference member is expensive for large string arrays. | 297 // This preference member is expensive for large string arrays. |
297 typedef PrefMember<std::vector<std::string> > StringListPrefMember; | 298 typedef PrefMember<std::vector<std::string> > StringListPrefMember; |
298 | 299 |
299 #endif // CHROME_BROWSER_API_PREFS_PREF_MEMBER_H_ | 300 #endif // CHROME_BROWSER_API_PREFS_PREF_MEMBER_H_ |
OLD | NEW |