Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(287)

Side by Side Diff: base/prefs/pref_member.cc

Issue 753603002: Change preference APIs to take std::string instead of const char*. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Removed all calls to c_str() in prefs. Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/prefs/pref_member.h ('k') | base/prefs/pref_member_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "base/prefs/pref_member.h" 5 #include "base/prefs/pref_member.h"
6 6
7 #include "base/callback.h" 7 #include "base/callback.h"
8 #include "base/callback_helpers.h" 8 #include "base/callback_helpers.h"
9 #include "base/location.h" 9 #include "base/location.h"
10 #include "base/message_loop/message_loop_proxy.h" 10 #include "base/message_loop/message_loop_proxy.h"
11 #include "base/prefs/pref_service.h" 11 #include "base/prefs/pref_service.h"
12 #include "base/value_conversions.h" 12 #include "base/value_conversions.h"
13 13
14 using base::MessageLoopProxy; 14 using base::MessageLoopProxy;
15 using base::SingleThreadTaskRunner; 15 using base::SingleThreadTaskRunner;
16 16
17 namespace subtle { 17 namespace subtle {
18 18
19 PrefMemberBase::PrefMemberBase() 19 PrefMemberBase::PrefMemberBase()
20 : prefs_(NULL), 20 : prefs_(NULL),
21 setting_value_(false) { 21 setting_value_(false) {
22 } 22 }
23 23
24 PrefMemberBase::~PrefMemberBase() { 24 PrefMemberBase::~PrefMemberBase() {
25 Destroy(); 25 Destroy();
26 } 26 }
27 27
28 void PrefMemberBase::Init(const char* pref_name, 28 void PrefMemberBase::Init(const std::string& pref_name,
29 PrefService* prefs, 29 PrefService* prefs,
30 const NamedChangeCallback& observer) { 30 const NamedChangeCallback& observer) {
31 observer_ = observer; 31 observer_ = observer;
32 Init(pref_name, prefs); 32 Init(pref_name, prefs);
33 } 33 }
34 34
35 void PrefMemberBase::Init(const char* pref_name, 35 void PrefMemberBase::Init(const std::string& pref_name, PrefService* prefs) {
36 PrefService* prefs) {
37 DCHECK(pref_name);
38 DCHECK(prefs); 36 DCHECK(prefs);
39 DCHECK(pref_name_.empty()); // Check that Init is only called once. 37 DCHECK(pref_name_.empty()); // Check that Init is only called once.
40 prefs_ = prefs; 38 prefs_ = prefs;
41 pref_name_ = pref_name; 39 pref_name_ = pref_name;
42 // Check that the preference is registered. 40 // Check that the preference is registered.
43 DCHECK(prefs_->FindPreference(pref_name_.c_str())) 41 DCHECK(prefs_->FindPreference(pref_name_)) << pref_name << " not registered.";
44 << pref_name << " not registered.";
45 42
46 // Add ourselves as a pref observer so we can keep our local value in sync. 43 // Add ourselves as a pref observer so we can keep our local value in sync.
47 prefs_->AddPrefObserver(pref_name, this); 44 prefs_->AddPrefObserver(pref_name, this);
48 } 45 }
49 46
50 void PrefMemberBase::Destroy() { 47 void PrefMemberBase::Destroy() {
51 if (prefs_ && !pref_name_.empty()) { 48 if (prefs_ && !pref_name_.empty()) {
52 prefs_->RemovePrefObserver(pref_name_.c_str(), this); 49 prefs_->RemovePrefObserver(pref_name_, this);
53 prefs_ = NULL; 50 prefs_ = NULL;
54 } 51 }
55 } 52 }
56 53
57 void PrefMemberBase::MoveToThread( 54 void PrefMemberBase::MoveToThread(
58 const scoped_refptr<SingleThreadTaskRunner>& task_runner) { 55 const scoped_refptr<SingleThreadTaskRunner>& task_runner) {
59 VerifyValuePrefName(); 56 VerifyValuePrefName();
60 // Load the value from preferences if it hasn't been loaded so far. 57 // Load the value from preferences if it hasn't been loaded so far.
61 if (!internal()) 58 if (!internal())
62 UpdateValueFromPref(base::Closure()); 59 UpdateValueFromPref(base::Closure());
63 internal()->MoveToThread(task_runner); 60 internal()->MoveToThread(task_runner);
64 } 61 }
65 62
66 void PrefMemberBase::OnPreferenceChanged(PrefService* service, 63 void PrefMemberBase::OnPreferenceChanged(PrefService* service,
67 const std::string& pref_name) { 64 const std::string& pref_name) {
68 VerifyValuePrefName(); 65 VerifyValuePrefName();
69 UpdateValueFromPref((!setting_value_ && !observer_.is_null()) ? 66 UpdateValueFromPref((!setting_value_ && !observer_.is_null()) ?
70 base::Bind(observer_, pref_name) : base::Closure()); 67 base::Bind(observer_, pref_name) : base::Closure());
71 } 68 }
72 69
73 void PrefMemberBase::UpdateValueFromPref(const base::Closure& callback) const { 70 void PrefMemberBase::UpdateValueFromPref(const base::Closure& callback) const {
74 VerifyValuePrefName(); 71 VerifyValuePrefName();
75 const PrefService::Preference* pref = 72 const PrefService::Preference* pref = prefs_->FindPreference(pref_name_);
76 prefs_->FindPreference(pref_name_.c_str());
77 DCHECK(pref); 73 DCHECK(pref);
78 if (!internal()) 74 if (!internal())
79 CreateInternal(); 75 CreateInternal();
80 internal()->UpdateValue(pref->GetValue()->DeepCopy(), 76 internal()->UpdateValue(pref->GetValue()->DeepCopy(),
81 pref->IsManaged(), 77 pref->IsManaged(),
82 pref->IsUserModifiable(), 78 pref->IsUserModifiable(),
83 callback); 79 callback);
84 } 80 }
85 81
86 void PrefMemberBase::VerifyPref() const { 82 void PrefMemberBase::VerifyPref() const {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 } 147 }
152 148
153 string_vector->swap(local_vector); 149 string_vector->swap(local_vector);
154 return true; 150 return true;
155 } 151 }
156 152
157 } // namespace subtle 153 } // namespace subtle
158 154
159 template <> 155 template <>
160 void PrefMember<bool>::UpdatePref(const bool& value) { 156 void PrefMember<bool>::UpdatePref(const bool& value) {
161 prefs()->SetBoolean(pref_name().c_str(), value); 157 prefs()->SetBoolean(pref_name(), value);
162 } 158 }
163 159
164 template <> 160 template <>
165 bool PrefMember<bool>::Internal::UpdateValueInternal( 161 bool PrefMember<bool>::Internal::UpdateValueInternal(
166 const base::Value& value) const { 162 const base::Value& value) const {
167 return value.GetAsBoolean(&value_); 163 return value.GetAsBoolean(&value_);
168 } 164 }
169 165
170 template <> 166 template <>
171 void PrefMember<int>::UpdatePref(const int& value) { 167 void PrefMember<int>::UpdatePref(const int& value) {
172 prefs()->SetInteger(pref_name().c_str(), value); 168 prefs()->SetInteger(pref_name(), value);
173 } 169 }
174 170
175 template <> 171 template <>
176 bool PrefMember<int>::Internal::UpdateValueInternal( 172 bool PrefMember<int>::Internal::UpdateValueInternal(
177 const base::Value& value) const { 173 const base::Value& value) const {
178 return value.GetAsInteger(&value_); 174 return value.GetAsInteger(&value_);
179 } 175 }
180 176
181 template <> 177 template <>
182 void PrefMember<double>::UpdatePref(const double& value) { 178 void PrefMember<double>::UpdatePref(const double& value) {
183 prefs()->SetDouble(pref_name().c_str(), value); 179 prefs()->SetDouble(pref_name(), value);
184 } 180 }
185 181
186 template <> 182 template <>
187 bool PrefMember<double>::Internal::UpdateValueInternal(const base::Value& value) 183 bool PrefMember<double>::Internal::UpdateValueInternal(const base::Value& value)
188 const { 184 const {
189 return value.GetAsDouble(&value_); 185 return value.GetAsDouble(&value_);
190 } 186 }
191 187
192 template <> 188 template <>
193 void PrefMember<std::string>::UpdatePref(const std::string& value) { 189 void PrefMember<std::string>::UpdatePref(const std::string& value) {
194 prefs()->SetString(pref_name().c_str(), value); 190 prefs()->SetString(pref_name(), value);
195 } 191 }
196 192
197 template <> 193 template <>
198 bool PrefMember<std::string>::Internal::UpdateValueInternal( 194 bool PrefMember<std::string>::Internal::UpdateValueInternal(
199 const base::Value& value) 195 const base::Value& value)
200 const { 196 const {
201 return value.GetAsString(&value_); 197 return value.GetAsString(&value_);
202 } 198 }
203 199
204 template <> 200 template <>
205 void PrefMember<base::FilePath>::UpdatePref(const base::FilePath& value) { 201 void PrefMember<base::FilePath>::UpdatePref(const base::FilePath& value) {
206 prefs()->SetFilePath(pref_name().c_str(), value); 202 prefs()->SetFilePath(pref_name(), value);
207 } 203 }
208 204
209 template <> 205 template <>
210 bool PrefMember<base::FilePath>::Internal::UpdateValueInternal( 206 bool PrefMember<base::FilePath>::Internal::UpdateValueInternal(
211 const base::Value& value) 207 const base::Value& value)
212 const { 208 const {
213 return base::GetValueAsFilePath(value, &value_); 209 return base::GetValueAsFilePath(value, &value_);
214 } 210 }
215 211
216 template <> 212 template <>
217 void PrefMember<std::vector<std::string> >::UpdatePref( 213 void PrefMember<std::vector<std::string> >::UpdatePref(
218 const std::vector<std::string>& value) { 214 const std::vector<std::string>& value) {
219 base::ListValue list_value; 215 base::ListValue list_value;
220 list_value.AppendStrings(value); 216 list_value.AppendStrings(value);
221 prefs()->Set(pref_name().c_str(), list_value); 217 prefs()->Set(pref_name(), list_value);
222 } 218 }
223 219
224 template <> 220 template <>
225 bool PrefMember<std::vector<std::string> >::Internal::UpdateValueInternal( 221 bool PrefMember<std::vector<std::string> >::Internal::UpdateValueInternal(
226 const base::Value& value) const { 222 const base::Value& value) const {
227 return subtle::PrefMemberVectorStringUpdate(value, &value_); 223 return subtle::PrefMemberVectorStringUpdate(value, &value_);
228 } 224 }
OLDNEW
« no previous file with comments | « base/prefs/pref_member.h ('k') | base/prefs/pref_member_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698