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 #include "base/prefs/pref_service.h" | 5 #include "base/prefs/pref_service.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 user_pref_store_.get(), | 79 user_pref_store_.get(), |
80 new ReadErrorHandler(read_error_callback_))); | 80 new ReadErrorHandler(read_error_callback_))); |
81 } | 81 } |
82 } | 82 } |
83 | 83 |
84 void PrefService::CommitPendingWrite() { | 84 void PrefService::CommitPendingWrite() { |
85 DCHECK(CalledOnValidThread()); | 85 DCHECK(CalledOnValidThread()); |
86 user_pref_store_->CommitPendingWrite(); | 86 user_pref_store_->CommitPendingWrite(); |
87 } | 87 } |
88 | 88 |
89 bool PrefService::GetBoolean(const char* path) const { | 89 bool PrefService::GetBoolean(const std::string& path) const { |
90 DCHECK(CalledOnValidThread()); | 90 DCHECK(CalledOnValidThread()); |
91 | 91 |
92 bool result = false; | 92 bool result = false; |
93 | 93 |
94 const base::Value* value = GetPreferenceValue(path); | 94 const base::Value* value = GetPreferenceValue(path); |
95 if (!value) { | 95 if (!value) { |
96 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 96 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
97 return result; | 97 return result; |
98 } | 98 } |
99 bool rv = value->GetAsBoolean(&result); | 99 bool rv = value->GetAsBoolean(&result); |
100 DCHECK(rv); | 100 DCHECK(rv); |
101 return result; | 101 return result; |
102 } | 102 } |
103 | 103 |
104 int PrefService::GetInteger(const char* path) const { | 104 int PrefService::GetInteger(const std::string& path) const { |
105 DCHECK(CalledOnValidThread()); | 105 DCHECK(CalledOnValidThread()); |
106 | 106 |
107 int result = 0; | 107 int result = 0; |
108 | 108 |
109 const base::Value* value = GetPreferenceValue(path); | 109 const base::Value* value = GetPreferenceValue(path); |
110 if (!value) { | 110 if (!value) { |
111 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 111 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
112 return result; | 112 return result; |
113 } | 113 } |
114 bool rv = value->GetAsInteger(&result); | 114 bool rv = value->GetAsInteger(&result); |
115 DCHECK(rv); | 115 DCHECK(rv); |
116 return result; | 116 return result; |
117 } | 117 } |
118 | 118 |
119 double PrefService::GetDouble(const char* path) const { | 119 double PrefService::GetDouble(const std::string& path) const { |
120 DCHECK(CalledOnValidThread()); | 120 DCHECK(CalledOnValidThread()); |
121 | 121 |
122 double result = 0.0; | 122 double result = 0.0; |
123 | 123 |
124 const base::Value* value = GetPreferenceValue(path); | 124 const base::Value* value = GetPreferenceValue(path); |
125 if (!value) { | 125 if (!value) { |
126 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 126 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
127 return result; | 127 return result; |
128 } | 128 } |
129 bool rv = value->GetAsDouble(&result); | 129 bool rv = value->GetAsDouble(&result); |
130 DCHECK(rv); | 130 DCHECK(rv); |
131 return result; | 131 return result; |
132 } | 132 } |
133 | 133 |
134 std::string PrefService::GetString(const char* path) const { | 134 std::string PrefService::GetString(const std::string& path) const { |
135 DCHECK(CalledOnValidThread()); | 135 DCHECK(CalledOnValidThread()); |
136 | 136 |
137 std::string result; | 137 std::string result; |
138 | 138 |
139 const base::Value* value = GetPreferenceValue(path); | 139 const base::Value* value = GetPreferenceValue(path); |
140 if (!value) { | 140 if (!value) { |
141 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 141 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
142 return result; | 142 return result; |
143 } | 143 } |
144 bool rv = value->GetAsString(&result); | 144 bool rv = value->GetAsString(&result); |
145 DCHECK(rv); | 145 DCHECK(rv); |
146 return result; | 146 return result; |
147 } | 147 } |
148 | 148 |
149 base::FilePath PrefService::GetFilePath(const char* path) const { | 149 base::FilePath PrefService::GetFilePath(const std::string& path) const { |
150 DCHECK(CalledOnValidThread()); | 150 DCHECK(CalledOnValidThread()); |
151 | 151 |
152 base::FilePath result; | 152 base::FilePath result; |
153 | 153 |
154 const base::Value* value = GetPreferenceValue(path); | 154 const base::Value* value = GetPreferenceValue(path); |
155 if (!value) { | 155 if (!value) { |
156 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 156 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
157 return base::FilePath(result); | 157 return base::FilePath(result); |
158 } | 158 } |
159 bool rv = base::GetValueAsFilePath(*value, &result); | 159 bool rv = base::GetValueAsFilePath(*value, &result); |
160 DCHECK(rv); | 160 DCHECK(rv); |
161 return result; | 161 return result; |
162 } | 162 } |
163 | 163 |
164 bool PrefService::HasPrefPath(const char* path) const { | 164 bool PrefService::HasPrefPath(const std::string& path) const { |
165 const Preference* pref = FindPreference(path); | 165 const Preference* pref = FindPreference(path); |
166 return pref && !pref->IsDefaultValue(); | 166 return pref && !pref->IsDefaultValue(); |
167 } | 167 } |
168 | 168 |
169 scoped_ptr<base::DictionaryValue> PrefService::GetPreferenceValues() const { | 169 scoped_ptr<base::DictionaryValue> PrefService::GetPreferenceValues() const { |
170 DCHECK(CalledOnValidThread()); | 170 DCHECK(CalledOnValidThread()); |
171 scoped_ptr<base::DictionaryValue> out(new base::DictionaryValue); | 171 scoped_ptr<base::DictionaryValue> out(new base::DictionaryValue); |
172 PrefRegistry::const_iterator i = pref_registry_->begin(); | 172 for (const auto& it : *pref_registry_) { |
173 for (; i != pref_registry_->end(); ++i) { | 173 const base::Value* value = GetPreferenceValue(it.first); |
174 const base::Value* value = GetPreferenceValue(i->first); | 174 out->Set(it.first, value->DeepCopy()); |
175 DCHECK(value); | |
176 out->Set(i->first, value->DeepCopy()); | |
177 } | 175 } |
178 return out.Pass(); | 176 return out.Pass(); |
179 } | 177 } |
| 178 |
| 179 scoped_ptr<base::DictionaryValue> PrefService::GetPreferenceValuesOmitDefaults() |
| 180 const { |
| 181 DCHECK(CalledOnValidThread()); |
| 182 scoped_ptr<base::DictionaryValue> out(new base::DictionaryValue); |
| 183 for (const auto& it : *pref_registry_) { |
| 184 const Preference* pref = FindPreference(it.first); |
| 185 if (pref->IsDefaultValue()) |
| 186 continue; |
| 187 out->Set(it.first, pref->GetValue()->DeepCopy()); |
| 188 } |
| 189 return out.Pass(); |
| 190 } |
180 | 191 |
181 scoped_ptr<base::DictionaryValue> | 192 scoped_ptr<base::DictionaryValue> |
182 PrefService::GetPreferenceValuesWithoutPathExpansion() const { | 193 PrefService::GetPreferenceValuesWithoutPathExpansion() const { |
183 DCHECK(CalledOnValidThread()); | 194 DCHECK(CalledOnValidThread()); |
184 scoped_ptr<base::DictionaryValue> out(new base::DictionaryValue); | 195 scoped_ptr<base::DictionaryValue> out(new base::DictionaryValue); |
185 PrefRegistry::const_iterator i = pref_registry_->begin(); | 196 for (const auto& it : *pref_registry_) { |
186 for (; i != pref_registry_->end(); ++i) { | 197 const base::Value* value = GetPreferenceValue(it.first); |
187 const base::Value* value = GetPreferenceValue(i->first); | |
188 DCHECK(value); | 198 DCHECK(value); |
189 out->SetWithoutPathExpansion(i->first, value->DeepCopy()); | 199 out->SetWithoutPathExpansion(it.first, value->DeepCopy()); |
190 } | 200 } |
191 return out.Pass(); | 201 return out.Pass(); |
192 } | 202 } |
193 | 203 |
194 const PrefService::Preference* PrefService::FindPreference( | 204 const PrefService::Preference* PrefService::FindPreference( |
195 const char* pref_name) const { | 205 const std::string& pref_name) const { |
196 DCHECK(CalledOnValidThread()); | 206 DCHECK(CalledOnValidThread()); |
197 PreferenceMap::iterator it = prefs_map_.find(pref_name); | 207 PreferenceMap::iterator it = prefs_map_.find(pref_name); |
198 if (it != prefs_map_.end()) | 208 if (it != prefs_map_.end()) |
199 return &(it->second); | 209 return &(it->second); |
200 const base::Value* default_value = NULL; | 210 const base::Value* default_value = NULL; |
201 if (!pref_registry_->defaults()->GetValue(pref_name, &default_value)) | 211 if (!pref_registry_->defaults()->GetValue(pref_name, &default_value)) |
202 return NULL; | 212 return NULL; |
203 it = prefs_map_.insert( | 213 it = prefs_map_.insert( |
204 std::make_pair(pref_name, Preference( | 214 std::make_pair(pref_name, Preference( |
205 this, pref_name, default_value->GetType()))).first; | 215 this, pref_name, default_value->GetType()))).first; |
(...skipping 12 matching lines...) Expand all Loading... |
218 switch (user_pref_store_->GetReadError()) { | 228 switch (user_pref_store_->GetReadError()) { |
219 case PersistentPrefStore::PREF_READ_ERROR_NONE: | 229 case PersistentPrefStore::PREF_READ_ERROR_NONE: |
220 return INITIALIZATION_STATUS_SUCCESS; | 230 return INITIALIZATION_STATUS_SUCCESS; |
221 case PersistentPrefStore::PREF_READ_ERROR_NO_FILE: | 231 case PersistentPrefStore::PREF_READ_ERROR_NO_FILE: |
222 return INITIALIZATION_STATUS_CREATED_NEW_PREF_STORE; | 232 return INITIALIZATION_STATUS_CREATED_NEW_PREF_STORE; |
223 default: | 233 default: |
224 return INITIALIZATION_STATUS_ERROR; | 234 return INITIALIZATION_STATUS_ERROR; |
225 } | 235 } |
226 } | 236 } |
227 | 237 |
228 bool PrefService::IsManagedPreference(const char* pref_name) const { | 238 bool PrefService::IsManagedPreference(const std::string& pref_name) const { |
229 const Preference* pref = FindPreference(pref_name); | 239 const Preference* pref = FindPreference(pref_name); |
230 return pref && pref->IsManaged(); | 240 return pref && pref->IsManaged(); |
231 } | 241 } |
232 | 242 |
233 bool PrefService::IsUserModifiablePreference(const char* pref_name) const { | 243 bool PrefService::IsUserModifiablePreference( |
| 244 const std::string& pref_name) const { |
234 const Preference* pref = FindPreference(pref_name); | 245 const Preference* pref = FindPreference(pref_name); |
235 return pref && pref->IsUserModifiable(); | 246 return pref && pref->IsUserModifiable(); |
236 } | 247 } |
237 | 248 |
238 const base::DictionaryValue* PrefService::GetDictionary( | 249 const base::DictionaryValue* PrefService::GetDictionary( |
239 const char* path) const { | 250 const std::string& path) const { |
240 DCHECK(CalledOnValidThread()); | 251 DCHECK(CalledOnValidThread()); |
241 | 252 |
242 const base::Value* value = GetPreferenceValue(path); | 253 const base::Value* value = GetPreferenceValue(path); |
243 if (!value) { | 254 if (!value) { |
244 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 255 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
245 return NULL; | 256 return NULL; |
246 } | 257 } |
247 if (value->GetType() != base::Value::TYPE_DICTIONARY) { | 258 if (value->GetType() != base::Value::TYPE_DICTIONARY) { |
248 NOTREACHED(); | 259 NOTREACHED(); |
249 return NULL; | 260 return NULL; |
250 } | 261 } |
251 return static_cast<const base::DictionaryValue*>(value); | 262 return static_cast<const base::DictionaryValue*>(value); |
252 } | 263 } |
253 | 264 |
254 const base::Value* PrefService::GetUserPrefValue(const char* path) const { | 265 const base::Value* PrefService::GetUserPrefValue( |
| 266 const std::string& path) const { |
255 DCHECK(CalledOnValidThread()); | 267 DCHECK(CalledOnValidThread()); |
256 | 268 |
257 const Preference* pref = FindPreference(path); | 269 const Preference* pref = FindPreference(path); |
258 if (!pref) { | 270 if (!pref) { |
259 NOTREACHED() << "Trying to get an unregistered pref: " << path; | 271 NOTREACHED() << "Trying to get an unregistered pref: " << path; |
260 return NULL; | 272 return NULL; |
261 } | 273 } |
262 | 274 |
263 // Look for an existing preference in the user store. If it doesn't | 275 // Look for an existing preference in the user store. If it doesn't |
264 // exist, return NULL. | 276 // exist, return NULL. |
265 base::Value* value = NULL; | 277 base::Value* value = NULL; |
266 if (!user_pref_store_->GetMutableValue(path, &value)) | 278 if (!user_pref_store_->GetMutableValue(path, &value)) |
267 return NULL; | 279 return NULL; |
268 | 280 |
269 if (!value->IsType(pref->GetType())) { | 281 if (!value->IsType(pref->GetType())) { |
270 NOTREACHED() << "Pref value type doesn't match registered type."; | 282 NOTREACHED() << "Pref value type doesn't match registered type."; |
271 return NULL; | 283 return NULL; |
272 } | 284 } |
273 | 285 |
274 return value; | 286 return value; |
275 } | 287 } |
276 | 288 |
277 void PrefService::SetDefaultPrefValue(const char* path, | 289 void PrefService::SetDefaultPrefValue(const std::string& path, |
278 base::Value* value) { | 290 base::Value* value) { |
279 DCHECK(CalledOnValidThread()); | 291 DCHECK(CalledOnValidThread()); |
280 pref_registry_->SetDefaultPrefValue(path, value); | 292 pref_registry_->SetDefaultPrefValue(path, value); |
281 } | 293 } |
282 | 294 |
283 const base::Value* PrefService::GetDefaultPrefValue(const char* path) const { | 295 const base::Value* PrefService::GetDefaultPrefValue( |
| 296 const std::string& path) const { |
284 DCHECK(CalledOnValidThread()); | 297 DCHECK(CalledOnValidThread()); |
285 // Lookup the preference in the default store. | 298 // Lookup the preference in the default store. |
286 const base::Value* value = NULL; | 299 const base::Value* value = NULL; |
287 if (!pref_registry_->defaults()->GetValue(path, &value)) { | 300 if (!pref_registry_->defaults()->GetValue(path, &value)) { |
288 NOTREACHED() << "Default value missing for pref: " << path; | 301 NOTREACHED() << "Default value missing for pref: " << path; |
289 return NULL; | 302 return NULL; |
290 } | 303 } |
291 return value; | 304 return value; |
292 } | 305 } |
293 | 306 |
294 const base::ListValue* PrefService::GetList(const char* path) const { | 307 const base::ListValue* PrefService::GetList(const std::string& path) const { |
295 DCHECK(CalledOnValidThread()); | 308 DCHECK(CalledOnValidThread()); |
296 | 309 |
297 const base::Value* value = GetPreferenceValue(path); | 310 const base::Value* value = GetPreferenceValue(path); |
298 if (!value) { | 311 if (!value) { |
299 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 312 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
300 return NULL; | 313 return NULL; |
301 } | 314 } |
302 if (value->GetType() != base::Value::TYPE_LIST) { | 315 if (value->GetType() != base::Value::TYPE_LIST) { |
303 NOTREACHED(); | 316 NOTREACHED(); |
304 return NULL; | 317 return NULL; |
305 } | 318 } |
306 return static_cast<const base::ListValue*>(value); | 319 return static_cast<const base::ListValue*>(value); |
307 } | 320 } |
308 | 321 |
309 void PrefService::AddPrefObserver(const char* path, PrefObserver* obs) { | 322 void PrefService::AddPrefObserver(const std::string& path, PrefObserver* obs) { |
310 pref_notifier_->AddPrefObserver(path, obs); | 323 pref_notifier_->AddPrefObserver(path, obs); |
311 } | 324 } |
312 | 325 |
313 void PrefService::RemovePrefObserver(const char* path, PrefObserver* obs) { | 326 void PrefService::RemovePrefObserver(const std::string& path, |
| 327 PrefObserver* obs) { |
314 pref_notifier_->RemovePrefObserver(path, obs); | 328 pref_notifier_->RemovePrefObserver(path, obs); |
315 } | 329 } |
316 | 330 |
317 void PrefService::AddPrefInitObserver(base::Callback<void(bool)> obs) { | 331 void PrefService::AddPrefInitObserver(base::Callback<void(bool)> obs) { |
318 pref_notifier_->AddInitObserver(obs); | 332 pref_notifier_->AddInitObserver(obs); |
319 } | 333 } |
320 | 334 |
321 PrefRegistry* PrefService::DeprecatedGetPrefRegistry() { | 335 PrefRegistry* PrefService::DeprecatedGetPrefRegistry() { |
322 return pref_registry_.get(); | 336 return pref_registry_.get(); |
323 } | 337 } |
324 | 338 |
325 void PrefService::ClearPref(const char* path) { | 339 void PrefService::ClearPref(const std::string& path) { |
326 DCHECK(CalledOnValidThread()); | 340 DCHECK(CalledOnValidThread()); |
327 | 341 |
328 const Preference* pref = FindPreference(path); | 342 const Preference* pref = FindPreference(path); |
329 if (!pref) { | 343 if (!pref) { |
330 NOTREACHED() << "Trying to clear an unregistered pref: " << path; | 344 NOTREACHED() << "Trying to clear an unregistered pref: " << path; |
331 return; | 345 return; |
332 } | 346 } |
333 user_pref_store_->RemoveValue(path); | 347 user_pref_store_->RemoveValue(path); |
334 } | 348 } |
335 | 349 |
336 void PrefService::Set(const char* path, const base::Value& value) { | 350 void PrefService::Set(const std::string& path, const base::Value& value) { |
337 SetUserPrefValue(path, value.DeepCopy()); | 351 SetUserPrefValue(path, value.DeepCopy()); |
338 } | 352 } |
339 | 353 |
340 void PrefService::SetBoolean(const char* path, bool value) { | 354 void PrefService::SetBoolean(const std::string& path, bool value) { |
341 SetUserPrefValue(path, new base::FundamentalValue(value)); | 355 SetUserPrefValue(path, new base::FundamentalValue(value)); |
342 } | 356 } |
343 | 357 |
344 void PrefService::SetInteger(const char* path, int value) { | 358 void PrefService::SetInteger(const std::string& path, int value) { |
345 SetUserPrefValue(path, new base::FundamentalValue(value)); | 359 SetUserPrefValue(path, new base::FundamentalValue(value)); |
346 } | 360 } |
347 | 361 |
348 void PrefService::SetDouble(const char* path, double value) { | 362 void PrefService::SetDouble(const std::string& path, double value) { |
349 SetUserPrefValue(path, new base::FundamentalValue(value)); | 363 SetUserPrefValue(path, new base::FundamentalValue(value)); |
350 } | 364 } |
351 | 365 |
352 void PrefService::SetString(const char* path, const std::string& value) { | 366 void PrefService::SetString(const std::string& path, const std::string& value) { |
353 SetUserPrefValue(path, new base::StringValue(value)); | 367 SetUserPrefValue(path, new base::StringValue(value)); |
354 } | 368 } |
355 | 369 |
356 void PrefService::SetFilePath(const char* path, const base::FilePath& value) { | 370 void PrefService::SetFilePath(const std::string& path, |
| 371 const base::FilePath& value) { |
357 SetUserPrefValue(path, base::CreateFilePathValue(value)); | 372 SetUserPrefValue(path, base::CreateFilePathValue(value)); |
358 } | 373 } |
359 | 374 |
360 void PrefService::SetInt64(const char* path, int64 value) { | 375 void PrefService::SetInt64(const std::string& path, int64 value) { |
361 SetUserPrefValue(path, new base::StringValue(base::Int64ToString(value))); | 376 SetUserPrefValue(path, new base::StringValue(base::Int64ToString(value))); |
362 } | 377 } |
363 | 378 |
364 int64 PrefService::GetInt64(const char* path) const { | 379 int64 PrefService::GetInt64(const std::string& path) const { |
365 DCHECK(CalledOnValidThread()); | 380 DCHECK(CalledOnValidThread()); |
366 | 381 |
367 const base::Value* value = GetPreferenceValue(path); | 382 const base::Value* value = GetPreferenceValue(path); |
368 if (!value) { | 383 if (!value) { |
369 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 384 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
370 return 0; | 385 return 0; |
371 } | 386 } |
372 std::string result("0"); | 387 std::string result("0"); |
373 bool rv = value->GetAsString(&result); | 388 bool rv = value->GetAsString(&result); |
374 DCHECK(rv); | 389 DCHECK(rv); |
375 | 390 |
376 int64 val; | 391 int64 val; |
377 base::StringToInt64(result, &val); | 392 base::StringToInt64(result, &val); |
378 return val; | 393 return val; |
379 } | 394 } |
380 | 395 |
381 void PrefService::SetUint64(const char* path, uint64 value) { | 396 void PrefService::SetUint64(const std::string& path, uint64 value) { |
382 SetUserPrefValue(path, new base::StringValue(base::Uint64ToString(value))); | 397 SetUserPrefValue(path, new base::StringValue(base::Uint64ToString(value))); |
383 } | 398 } |
384 | 399 |
385 uint64 PrefService::GetUint64(const char* path) const { | 400 uint64 PrefService::GetUint64(const std::string& path) const { |
386 DCHECK(CalledOnValidThread()); | 401 DCHECK(CalledOnValidThread()); |
387 | 402 |
388 const base::Value* value = GetPreferenceValue(path); | 403 const base::Value* value = GetPreferenceValue(path); |
389 if (!value) { | 404 if (!value) { |
390 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 405 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
391 return 0; | 406 return 0; |
392 } | 407 } |
393 std::string result("0"); | 408 std::string result("0"); |
394 bool rv = value->GetAsString(&result); | 409 bool rv = value->GetAsString(&result); |
395 DCHECK(rv); | 410 DCHECK(rv); |
396 | 411 |
397 uint64 val; | 412 uint64 val; |
398 base::StringToUint64(result, &val); | 413 base::StringToUint64(result, &val); |
399 return val; | 414 return val; |
400 } | 415 } |
401 | 416 |
402 base::Value* PrefService::GetMutableUserPref(const char* path, | 417 base::Value* PrefService::GetMutableUserPref(const std::string& path, |
403 base::Value::Type type) { | 418 base::Value::Type type) { |
404 CHECK(type == base::Value::TYPE_DICTIONARY || type == base::Value::TYPE_LIST); | 419 CHECK(type == base::Value::TYPE_DICTIONARY || type == base::Value::TYPE_LIST); |
405 DCHECK(CalledOnValidThread()); | 420 DCHECK(CalledOnValidThread()); |
406 | 421 |
407 const Preference* pref = FindPreference(path); | 422 const Preference* pref = FindPreference(path); |
408 if (!pref) { | 423 if (!pref) { |
409 NOTREACHED() << "Trying to get an unregistered pref: " << path; | 424 NOTREACHED() << "Trying to get an unregistered pref: " << path; |
410 return NULL; | 425 return NULL; |
411 } | 426 } |
412 if (pref->GetType() != type) { | 427 if (pref->GetType() != type) { |
(...skipping 16 matching lines...) Expand all Loading... |
429 user_pref_store_->SetValueSilently(path, value); | 444 user_pref_store_->SetValueSilently(path, value); |
430 } | 445 } |
431 return value; | 446 return value; |
432 } | 447 } |
433 | 448 |
434 void PrefService::ReportUserPrefChanged(const std::string& key) { | 449 void PrefService::ReportUserPrefChanged(const std::string& key) { |
435 DCHECK(CalledOnValidThread()); | 450 DCHECK(CalledOnValidThread()); |
436 user_pref_store_->ReportValueChanged(key); | 451 user_pref_store_->ReportValueChanged(key); |
437 } | 452 } |
438 | 453 |
439 void PrefService::SetUserPrefValue(const char* path, base::Value* new_value) { | 454 void PrefService::SetUserPrefValue(const std::string& path, |
| 455 base::Value* new_value) { |
440 scoped_ptr<base::Value> owned_value(new_value); | 456 scoped_ptr<base::Value> owned_value(new_value); |
441 DCHECK(CalledOnValidThread()); | 457 DCHECK(CalledOnValidThread()); |
442 | 458 |
443 const Preference* pref = FindPreference(path); | 459 const Preference* pref = FindPreference(path); |
444 if (!pref) { | 460 if (!pref) { |
445 NOTREACHED() << "Trying to write an unregistered pref: " << path; | 461 NOTREACHED() << "Trying to write an unregistered pref: " << path; |
446 return; | 462 return; |
447 } | 463 } |
448 if (pref->GetType() != new_value->GetType()) { | 464 if (pref->GetType() != new_value->GetType()) { |
449 NOTREACHED() << "Trying to set pref " << path | 465 NOTREACHED() << "Trying to set pref " << path |
450 << " of type " << pref->GetType() | 466 << " of type " << pref->GetType() |
451 << " to value of type " << new_value->GetType(); | 467 << " to value of type " << new_value->GetType(); |
452 return; | 468 return; |
453 } | 469 } |
454 | 470 |
455 user_pref_store_->SetValue(path, owned_value.release()); | 471 user_pref_store_->SetValue(path, owned_value.release()); |
456 } | 472 } |
457 | 473 |
458 void PrefService::UpdateCommandLinePrefStore(PrefStore* command_line_store) { | 474 void PrefService::UpdateCommandLinePrefStore(PrefStore* command_line_store) { |
459 pref_value_store_->UpdateCommandLinePrefStore(command_line_store); | 475 pref_value_store_->UpdateCommandLinePrefStore(command_line_store); |
460 } | 476 } |
461 | 477 |
462 /////////////////////////////////////////////////////////////////////////////// | 478 /////////////////////////////////////////////////////////////////////////////// |
463 // PrefService::Preference | 479 // PrefService::Preference |
464 | 480 |
465 PrefService::Preference::Preference(const PrefService* service, | 481 PrefService::Preference::Preference(const PrefService* service, |
466 const char* name, | 482 const std::string& name, |
467 base::Value::Type type) | 483 base::Value::Type type) |
468 : name_(name), | 484 : name_(name), type_(type), pref_service_(service) { |
469 type_(type), | |
470 pref_service_(service) { | |
471 DCHECK(name); | |
472 DCHECK(service); | 485 DCHECK(service); |
473 } | 486 } |
474 | 487 |
475 const std::string PrefService::Preference::name() const { | 488 const std::string PrefService::Preference::name() const { |
476 return name_; | 489 return name_; |
477 } | 490 } |
478 | 491 |
479 base::Value::Type PrefService::Preference::GetType() const { | 492 base::Value::Type PrefService::Preference::GetType() const { |
480 return type_; | 493 return type_; |
481 } | 494 } |
482 | 495 |
483 const base::Value* PrefService::Preference::GetValue() const { | 496 const base::Value* PrefService::Preference::GetValue() const { |
484 const base::Value* result= pref_service_->GetPreferenceValue(name_); | 497 const base::Value* result= pref_service_->GetPreferenceValue(name_); |
485 DCHECK(result) << "Must register pref before getting its value"; | 498 DCHECK(result) << "Must register pref before getting its value"; |
486 return result; | 499 return result; |
487 } | 500 } |
488 | 501 |
489 const base::Value* PrefService::Preference::GetRecommendedValue() const { | 502 const base::Value* PrefService::Preference::GetRecommendedValue() const { |
490 DCHECK(pref_service_->FindPreference(name_.c_str())) << | 503 DCHECK(pref_service_->FindPreference(name_)) |
491 "Must register pref before getting its value"; | 504 << "Must register pref before getting its value"; |
492 | 505 |
493 const base::Value* found_value = NULL; | 506 const base::Value* found_value = NULL; |
494 if (pref_value_store()->GetRecommendedValue(name_, type_, &found_value)) { | 507 if (pref_value_store()->GetRecommendedValue(name_, type_, &found_value)) { |
495 DCHECK(found_value->IsType(type_)); | 508 DCHECK(found_value->IsType(type_)); |
496 return found_value; | 509 return found_value; |
497 } | 510 } |
498 | 511 |
499 // The pref has no recommended value. | 512 // The pref has no recommended value. |
500 return NULL; | 513 return NULL; |
501 } | 514 } |
502 | 515 |
503 bool PrefService::Preference::IsManaged() const { | 516 bool PrefService::Preference::IsManaged() const { |
504 return pref_value_store()->PrefValueInManagedStore(name_.c_str()); | 517 return pref_value_store()->PrefValueInManagedStore(name_); |
505 } | 518 } |
506 | 519 |
507 bool PrefService::Preference::IsRecommended() const { | 520 bool PrefService::Preference::IsRecommended() const { |
508 return pref_value_store()->PrefValueFromRecommendedStore(name_.c_str()); | 521 return pref_value_store()->PrefValueFromRecommendedStore(name_); |
509 } | 522 } |
510 | 523 |
511 bool PrefService::Preference::HasExtensionSetting() const { | 524 bool PrefService::Preference::HasExtensionSetting() const { |
512 return pref_value_store()->PrefValueInExtensionStore(name_.c_str()); | 525 return pref_value_store()->PrefValueInExtensionStore(name_); |
513 } | 526 } |
514 | 527 |
515 bool PrefService::Preference::HasUserSetting() const { | 528 bool PrefService::Preference::HasUserSetting() const { |
516 return pref_value_store()->PrefValueInUserStore(name_.c_str()); | 529 return pref_value_store()->PrefValueInUserStore(name_); |
517 } | 530 } |
518 | 531 |
519 bool PrefService::Preference::IsExtensionControlled() const { | 532 bool PrefService::Preference::IsExtensionControlled() const { |
520 return pref_value_store()->PrefValueFromExtensionStore(name_.c_str()); | 533 return pref_value_store()->PrefValueFromExtensionStore(name_); |
521 } | 534 } |
522 | 535 |
523 bool PrefService::Preference::IsUserControlled() const { | 536 bool PrefService::Preference::IsUserControlled() const { |
524 return pref_value_store()->PrefValueFromUserStore(name_.c_str()); | 537 return pref_value_store()->PrefValueFromUserStore(name_); |
525 } | 538 } |
526 | 539 |
527 bool PrefService::Preference::IsDefaultValue() const { | 540 bool PrefService::Preference::IsDefaultValue() const { |
528 return pref_value_store()->PrefValueFromDefaultStore(name_.c_str()); | 541 return pref_value_store()->PrefValueFromDefaultStore(name_); |
529 } | 542 } |
530 | 543 |
531 bool PrefService::Preference::IsUserModifiable() const { | 544 bool PrefService::Preference::IsUserModifiable() const { |
532 return pref_value_store()->PrefValueUserModifiable(name_.c_str()); | 545 return pref_value_store()->PrefValueUserModifiable(name_); |
533 } | 546 } |
534 | 547 |
535 bool PrefService::Preference::IsExtensionModifiable() const { | 548 bool PrefService::Preference::IsExtensionModifiable() const { |
536 return pref_value_store()->PrefValueExtensionModifiable(name_.c_str()); | 549 return pref_value_store()->PrefValueExtensionModifiable(name_); |
537 } | 550 } |
538 | 551 |
539 const base::Value* PrefService::GetPreferenceValue( | 552 const base::Value* PrefService::GetPreferenceValue( |
540 const std::string& path) const { | 553 const std::string& path) const { |
541 DCHECK(CalledOnValidThread()); | 554 DCHECK(CalledOnValidThread()); |
542 const base::Value* default_value = NULL; | 555 const base::Value* default_value = NULL; |
543 if (pref_registry_->defaults()->GetValue(path, &default_value)) { | 556 if (pref_registry_->defaults()->GetValue(path, &default_value)) { |
544 const base::Value* found_value = NULL; | 557 const base::Value* found_value = NULL; |
545 base::Value::Type default_type = default_value->GetType(); | 558 base::Value::Type default_type = default_value->GetType(); |
546 if (pref_value_store_->GetValue(path, default_type, &found_value)) { | 559 if (pref_value_store_->GetValue(path, default_type, &found_value)) { |
547 DCHECK(found_value->IsType(default_type)); | 560 DCHECK(found_value->IsType(default_type)); |
548 return found_value; | 561 return found_value; |
549 } else { | 562 } else { |
550 // Every registered preference has at least a default value. | 563 // Every registered preference has at least a default value. |
551 NOTREACHED() << "no valid value found for registered pref " << path; | 564 NOTREACHED() << "no valid value found for registered pref " << path; |
552 } | 565 } |
553 } | 566 } |
554 | 567 |
555 return NULL; | 568 return NULL; |
556 } | 569 } |
OLD | NEW |