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 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 bool rv = base::GetValueAsFilePath(*value, &result); | 170 bool rv = base::GetValueAsFilePath(*value, &result); |
171 DCHECK(rv); | 171 DCHECK(rv); |
172 return result; | 172 return result; |
173 } | 173 } |
174 | 174 |
175 bool PrefService::HasPrefPath(const char* path) const { | 175 bool PrefService::HasPrefPath(const char* path) const { |
176 const Preference* pref = FindPreference(path); | 176 const Preference* pref = FindPreference(path); |
177 return pref && !pref->IsDefaultValue(); | 177 return pref && !pref->IsDefaultValue(); |
178 } | 178 } |
179 | 179 |
180 DictionaryValue* PrefService::GetPreferenceValues() const { | 180 base::DictionaryValue* PrefService::GetPreferenceValues() const { |
181 DCHECK(CalledOnValidThread()); | 181 DCHECK(CalledOnValidThread()); |
182 DictionaryValue* out = new DictionaryValue; | 182 base::DictionaryValue* out = new base::DictionaryValue; |
183 PrefRegistry::const_iterator i = pref_registry_->begin(); | 183 PrefRegistry::const_iterator i = pref_registry_->begin(); |
184 for (; i != pref_registry_->end(); ++i) { | 184 for (; i != pref_registry_->end(); ++i) { |
185 const Value* value = GetPreferenceValue(i->first); | 185 const base::Value* value = GetPreferenceValue(i->first); |
186 DCHECK(value); | 186 DCHECK(value); |
187 out->Set(i->first, value->DeepCopy()); | 187 out->Set(i->first, value->DeepCopy()); |
188 } | 188 } |
189 return out; | 189 return out; |
190 } | 190 } |
191 | 191 |
192 const PrefService::Preference* PrefService::FindPreference( | 192 const PrefService::Preference* PrefService::FindPreference( |
193 const char* pref_name) const { | 193 const char* pref_name) const { |
194 DCHECK(CalledOnValidThread()); | 194 DCHECK(CalledOnValidThread()); |
195 PreferenceMap::iterator it = prefs_map_.find(pref_name); | 195 PreferenceMap::iterator it = prefs_map_.find(pref_name); |
(...skipping 30 matching lines...) Expand all Loading... |
226 bool PrefService::IsManagedPreference(const char* pref_name) const { | 226 bool PrefService::IsManagedPreference(const char* pref_name) const { |
227 const Preference* pref = FindPreference(pref_name); | 227 const Preference* pref = FindPreference(pref_name); |
228 return pref && pref->IsManaged(); | 228 return pref && pref->IsManaged(); |
229 } | 229 } |
230 | 230 |
231 bool PrefService::IsUserModifiablePreference(const char* pref_name) const { | 231 bool PrefService::IsUserModifiablePreference(const char* pref_name) const { |
232 const Preference* pref = FindPreference(pref_name); | 232 const Preference* pref = FindPreference(pref_name); |
233 return pref && pref->IsUserModifiable(); | 233 return pref && pref->IsUserModifiable(); |
234 } | 234 } |
235 | 235 |
236 const DictionaryValue* PrefService::GetDictionary(const char* path) const { | 236 const base::DictionaryValue* PrefService::GetDictionary( |
| 237 const char* path) const { |
237 DCHECK(CalledOnValidThread()); | 238 DCHECK(CalledOnValidThread()); |
238 | 239 |
239 const Value* value = GetPreferenceValue(path); | 240 const base::Value* value = GetPreferenceValue(path); |
240 if (!value) { | 241 if (!value) { |
241 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 242 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
242 return NULL; | 243 return NULL; |
243 } | 244 } |
244 if (value->GetType() != Value::TYPE_DICTIONARY) { | 245 if (value->GetType() != base::Value::TYPE_DICTIONARY) { |
245 NOTREACHED(); | 246 NOTREACHED(); |
246 return NULL; | 247 return NULL; |
247 } | 248 } |
248 return static_cast<const DictionaryValue*>(value); | 249 return static_cast<const base::DictionaryValue*>(value); |
249 } | 250 } |
250 | 251 |
251 const base::Value* PrefService::GetUserPrefValue(const char* path) const { | 252 const base::Value* PrefService::GetUserPrefValue(const char* path) const { |
252 DCHECK(CalledOnValidThread()); | 253 DCHECK(CalledOnValidThread()); |
253 | 254 |
254 const Preference* pref = FindPreference(path); | 255 const Preference* pref = FindPreference(path); |
255 if (!pref) { | 256 if (!pref) { |
256 NOTREACHED() << "Trying to get an unregistered pref: " << path; | 257 NOTREACHED() << "Trying to get an unregistered pref: " << path; |
257 return NULL; | 258 return NULL; |
258 } | 259 } |
(...skipping 22 matching lines...) Expand all Loading... |
281 DCHECK(CalledOnValidThread()); | 282 DCHECK(CalledOnValidThread()); |
282 // Lookup the preference in the default store. | 283 // Lookup the preference in the default store. |
283 const base::Value* value = NULL; | 284 const base::Value* value = NULL; |
284 if (!pref_registry_->defaults()->GetValue(path, &value)) { | 285 if (!pref_registry_->defaults()->GetValue(path, &value)) { |
285 NOTREACHED() << "Default value missing for pref: " << path; | 286 NOTREACHED() << "Default value missing for pref: " << path; |
286 return NULL; | 287 return NULL; |
287 } | 288 } |
288 return value; | 289 return value; |
289 } | 290 } |
290 | 291 |
291 const ListValue* PrefService::GetList(const char* path) const { | 292 const base::ListValue* PrefService::GetList(const char* path) const { |
292 DCHECK(CalledOnValidThread()); | 293 DCHECK(CalledOnValidThread()); |
293 | 294 |
294 const Value* value = GetPreferenceValue(path); | 295 const base::Value* value = GetPreferenceValue(path); |
295 if (!value) { | 296 if (!value) { |
296 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 297 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
297 return NULL; | 298 return NULL; |
298 } | 299 } |
299 if (value->GetType() != Value::TYPE_LIST) { | 300 if (value->GetType() != base::Value::TYPE_LIST) { |
300 NOTREACHED(); | 301 NOTREACHED(); |
301 return NULL; | 302 return NULL; |
302 } | 303 } |
303 return static_cast<const ListValue*>(value); | 304 return static_cast<const base::ListValue*>(value); |
304 } | 305 } |
305 | 306 |
306 void PrefService::AddPrefObserver(const char* path, PrefObserver* obs) { | 307 void PrefService::AddPrefObserver(const char* path, PrefObserver* obs) { |
307 pref_notifier_->AddPrefObserver(path, obs); | 308 pref_notifier_->AddPrefObserver(path, obs); |
308 } | 309 } |
309 | 310 |
310 void PrefService::RemovePrefObserver(const char* path, PrefObserver* obs) { | 311 void PrefService::RemovePrefObserver(const char* path, PrefObserver* obs) { |
311 pref_notifier_->RemovePrefObserver(path, obs); | 312 pref_notifier_->RemovePrefObserver(path, obs); |
312 } | 313 } |
313 | 314 |
(...skipping 12 matching lines...) Expand all Loading... |
326 AddRegisteredPreference(it->first.c_str(), it->second); | 327 AddRegisteredPreference(it->first.c_str(), it->second); |
327 } | 328 } |
328 } | 329 } |
329 | 330 |
330 // TODO(joi): Once MarkNeedsEmptyValue is gone, we can probably | 331 // TODO(joi): Once MarkNeedsEmptyValue is gone, we can probably |
331 // completely get rid of this method. There will be one difference in | 332 // completely get rid of this method. There will be one difference in |
332 // semantics; currently all registered preferences are stored right | 333 // semantics; currently all registered preferences are stored right |
333 // away in the prefs_map_, if we remove this they would be stored only | 334 // away in the prefs_map_, if we remove this they would be stored only |
334 // opportunistically. | 335 // opportunistically. |
335 void PrefService::AddRegisteredPreference(const char* path, | 336 void PrefService::AddRegisteredPreference(const char* path, |
336 Value* default_value) { | 337 base::Value* default_value) { |
337 DCHECK(CalledOnValidThread()); | 338 DCHECK(CalledOnValidThread()); |
338 | 339 |
339 // For ListValue and DictionaryValue with non empty default, empty value | 340 // For ListValue and DictionaryValue with non empty default, empty value |
340 // for |path| needs to be persisted in |user_pref_store_|. So that | 341 // for |path| needs to be persisted in |user_pref_store_|. So that |
341 // non empty default is not used when user sets an empty ListValue or | 342 // non empty default is not used when user sets an empty ListValue or |
342 // DictionaryValue. | 343 // DictionaryValue. |
343 bool needs_empty_value = false; | 344 bool needs_empty_value = false; |
344 base::Value::Type orig_type = default_value->GetType(); | 345 base::Value::Type orig_type = default_value->GetType(); |
345 if (orig_type == base::Value::TYPE_LIST) { | 346 if (orig_type == base::Value::TYPE_LIST) { |
346 const base::ListValue* list = NULL; | 347 const base::ListValue* list = NULL; |
(...skipping 12 matching lines...) Expand all Loading... |
359 DCHECK(CalledOnValidThread()); | 360 DCHECK(CalledOnValidThread()); |
360 | 361 |
361 const Preference* pref = FindPreference(path); | 362 const Preference* pref = FindPreference(path); |
362 if (!pref) { | 363 if (!pref) { |
363 NOTREACHED() << "Trying to clear an unregistered pref: " << path; | 364 NOTREACHED() << "Trying to clear an unregistered pref: " << path; |
364 return; | 365 return; |
365 } | 366 } |
366 user_pref_store_->RemoveValue(path); | 367 user_pref_store_->RemoveValue(path); |
367 } | 368 } |
368 | 369 |
369 void PrefService::Set(const char* path, const Value& value) { | 370 void PrefService::Set(const char* path, const base::Value& value) { |
370 SetUserPrefValue(path, value.DeepCopy()); | 371 SetUserPrefValue(path, value.DeepCopy()); |
371 } | 372 } |
372 | 373 |
373 void PrefService::SetBoolean(const char* path, bool value) { | 374 void PrefService::SetBoolean(const char* path, bool value) { |
374 SetUserPrefValue(path, Value::CreateBooleanValue(value)); | 375 SetUserPrefValue(path, base::Value::CreateBooleanValue(value)); |
375 } | 376 } |
376 | 377 |
377 void PrefService::SetInteger(const char* path, int value) { | 378 void PrefService::SetInteger(const char* path, int value) { |
378 SetUserPrefValue(path, Value::CreateIntegerValue(value)); | 379 SetUserPrefValue(path, base::Value::CreateIntegerValue(value)); |
379 } | 380 } |
380 | 381 |
381 void PrefService::SetDouble(const char* path, double value) { | 382 void PrefService::SetDouble(const char* path, double value) { |
382 SetUserPrefValue(path, Value::CreateDoubleValue(value)); | 383 SetUserPrefValue(path, base::Value::CreateDoubleValue(value)); |
383 } | 384 } |
384 | 385 |
385 void PrefService::SetString(const char* path, const std::string& value) { | 386 void PrefService::SetString(const char* path, const std::string& value) { |
386 SetUserPrefValue(path, Value::CreateStringValue(value)); | 387 SetUserPrefValue(path, base::Value::CreateStringValue(value)); |
387 } | 388 } |
388 | 389 |
389 void PrefService::SetFilePath(const char* path, const base::FilePath& value) { | 390 void PrefService::SetFilePath(const char* path, const base::FilePath& value) { |
390 SetUserPrefValue(path, base::CreateFilePathValue(value)); | 391 SetUserPrefValue(path, base::CreateFilePathValue(value)); |
391 } | 392 } |
392 | 393 |
393 void PrefService::SetInt64(const char* path, int64 value) { | 394 void PrefService::SetInt64(const char* path, int64 value) { |
394 SetUserPrefValue(path, Value::CreateStringValue(base::Int64ToString(value))); | 395 SetUserPrefValue(path, |
| 396 base::Value::CreateStringValue(base::Int64ToString(value))); |
395 } | 397 } |
396 | 398 |
397 int64 PrefService::GetInt64(const char* path) const { | 399 int64 PrefService::GetInt64(const char* path) const { |
398 DCHECK(CalledOnValidThread()); | 400 DCHECK(CalledOnValidThread()); |
399 | 401 |
400 const Value* value = GetPreferenceValue(path); | 402 const base::Value* value = GetPreferenceValue(path); |
401 if (!value) { | 403 if (!value) { |
402 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 404 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
403 return 0; | 405 return 0; |
404 } | 406 } |
405 std::string result("0"); | 407 std::string result("0"); |
406 bool rv = value->GetAsString(&result); | 408 bool rv = value->GetAsString(&result); |
407 DCHECK(rv); | 409 DCHECK(rv); |
408 | 410 |
409 int64 val; | 411 int64 val; |
410 base::StringToInt64(result, &val); | 412 base::StringToInt64(result, &val); |
411 return val; | 413 return val; |
412 } | 414 } |
413 | 415 |
414 void PrefService::SetUint64(const char* path, uint64 value) { | 416 void PrefService::SetUint64(const char* path, uint64 value) { |
415 SetUserPrefValue(path, Value::CreateStringValue(base::Uint64ToString(value))); | 417 SetUserPrefValue(path, |
| 418 base::Value::CreateStringValue(base::Uint64ToString(value))); |
416 } | 419 } |
417 | 420 |
418 uint64 PrefService::GetUint64(const char* path) const { | 421 uint64 PrefService::GetUint64(const char* path) const { |
419 DCHECK(CalledOnValidThread()); | 422 DCHECK(CalledOnValidThread()); |
420 | 423 |
421 const Value* value = GetPreferenceValue(path); | 424 const base::Value* value = GetPreferenceValue(path); |
422 if (!value) { | 425 if (!value) { |
423 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 426 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
424 return 0; | 427 return 0; |
425 } | 428 } |
426 std::string result("0"); | 429 std::string result("0"); |
427 bool rv = value->GetAsString(&result); | 430 bool rv = value->GetAsString(&result); |
428 DCHECK(rv); | 431 DCHECK(rv); |
429 | 432 |
430 uint64 val; | 433 uint64 val; |
431 base::StringToUint64(result, &val); | 434 base::StringToUint64(result, &val); |
432 return val; | 435 return val; |
433 } | 436 } |
434 | 437 |
435 Value* PrefService::GetMutableUserPref(const char* path, | 438 base::Value* PrefService::GetMutableUserPref(const char* path, |
436 base::Value::Type type) { | 439 base::Value::Type type) { |
437 CHECK(type == Value::TYPE_DICTIONARY || type == Value::TYPE_LIST); | 440 CHECK(type == base::Value::TYPE_DICTIONARY || type == base::Value::TYPE_LIST); |
438 DCHECK(CalledOnValidThread()); | 441 DCHECK(CalledOnValidThread()); |
439 | 442 |
440 const Preference* pref = FindPreference(path); | 443 const Preference* pref = FindPreference(path); |
441 if (!pref) { | 444 if (!pref) { |
442 NOTREACHED() << "Trying to get an unregistered pref: " << path; | 445 NOTREACHED() << "Trying to get an unregistered pref: " << path; |
443 return NULL; | 446 return NULL; |
444 } | 447 } |
445 if (pref->GetType() != type) { | 448 if (pref->GetType() != type) { |
446 NOTREACHED() << "Wrong type for GetMutableValue: " << path; | 449 NOTREACHED() << "Wrong type for GetMutableValue: " << path; |
447 return NULL; | 450 return NULL; |
448 } | 451 } |
449 | 452 |
450 // Look for an existing preference in the user store. If it doesn't | 453 // Look for an existing preference in the user store. If it doesn't |
451 // exist or isn't the correct type, create a new user preference. | 454 // exist or isn't the correct type, create a new user preference. |
452 Value* value = NULL; | 455 base::Value* value = NULL; |
453 if (!user_pref_store_->GetMutableValue(path, &value) || | 456 if (!user_pref_store_->GetMutableValue(path, &value) || |
454 !value->IsType(type)) { | 457 !value->IsType(type)) { |
455 if (type == Value::TYPE_DICTIONARY) { | 458 if (type == base::Value::TYPE_DICTIONARY) { |
456 value = new DictionaryValue; | 459 value = new base::DictionaryValue; |
457 } else if (type == Value::TYPE_LIST) { | 460 } else if (type == base::Value::TYPE_LIST) { |
458 value = new ListValue; | 461 value = new base::ListValue; |
459 } else { | 462 } else { |
460 NOTREACHED(); | 463 NOTREACHED(); |
461 } | 464 } |
462 user_pref_store_->SetValueSilently(path, value); | 465 user_pref_store_->SetValueSilently(path, value); |
463 } | 466 } |
464 return value; | 467 return value; |
465 } | 468 } |
466 | 469 |
467 void PrefService::ReportUserPrefChanged(const std::string& key) { | 470 void PrefService::ReportUserPrefChanged(const std::string& key) { |
468 user_pref_store_->ReportValueChanged(key); | 471 user_pref_store_->ReportValueChanged(key); |
469 } | 472 } |
470 | 473 |
471 void PrefService::SetUserPrefValue(const char* path, Value* new_value) { | 474 void PrefService::SetUserPrefValue(const char* path, base::Value* new_value) { |
472 scoped_ptr<Value> owned_value(new_value); | 475 scoped_ptr<base::Value> owned_value(new_value); |
473 DCHECK(CalledOnValidThread()); | 476 DCHECK(CalledOnValidThread()); |
474 | 477 |
475 const Preference* pref = FindPreference(path); | 478 const Preference* pref = FindPreference(path); |
476 if (!pref) { | 479 if (!pref) { |
477 NOTREACHED() << "Trying to write an unregistered pref: " << path; | 480 NOTREACHED() << "Trying to write an unregistered pref: " << path; |
478 return; | 481 return; |
479 } | 482 } |
480 if (pref->GetType() != new_value->GetType()) { | 483 if (pref->GetType() != new_value->GetType()) { |
481 NOTREACHED() << "Trying to set pref " << path | 484 NOTREACHED() << "Trying to set pref " << path |
482 << " of type " << pref->GetType() | 485 << " of type " << pref->GetType() |
(...skipping 22 matching lines...) Expand all Loading... |
505 } | 508 } |
506 | 509 |
507 const std::string PrefService::Preference::name() const { | 510 const std::string PrefService::Preference::name() const { |
508 return name_; | 511 return name_; |
509 } | 512 } |
510 | 513 |
511 base::Value::Type PrefService::Preference::GetType() const { | 514 base::Value::Type PrefService::Preference::GetType() const { |
512 return type_; | 515 return type_; |
513 } | 516 } |
514 | 517 |
515 const Value* PrefService::Preference::GetValue() const { | 518 const base::Value* PrefService::Preference::GetValue() const { |
516 const Value* result= pref_service_->GetPreferenceValue(name_); | 519 const base::Value* result= pref_service_->GetPreferenceValue(name_); |
517 DCHECK(result) << "Must register pref before getting its value"; | 520 DCHECK(result) << "Must register pref before getting its value"; |
518 return result; | 521 return result; |
519 } | 522 } |
520 | 523 |
521 const Value* PrefService::Preference::GetRecommendedValue() const { | 524 const base::Value* PrefService::Preference::GetRecommendedValue() const { |
522 DCHECK(pref_service_->FindPreference(name_.c_str())) << | 525 DCHECK(pref_service_->FindPreference(name_.c_str())) << |
523 "Must register pref before getting its value"; | 526 "Must register pref before getting its value"; |
524 | 527 |
525 const Value* found_value = NULL; | 528 const base::Value* found_value = NULL; |
526 if (pref_value_store()->GetRecommendedValue(name_, type_, &found_value)) { | 529 if (pref_value_store()->GetRecommendedValue(name_, type_, &found_value)) { |
527 DCHECK(found_value->IsType(type_)); | 530 DCHECK(found_value->IsType(type_)); |
528 return found_value; | 531 return found_value; |
529 } | 532 } |
530 | 533 |
531 // The pref has no recommended value. | 534 // The pref has no recommended value. |
532 return NULL; | 535 return NULL; |
533 } | 536 } |
534 | 537 |
535 bool PrefService::Preference::IsManaged() const { | 538 bool PrefService::Preference::IsManaged() const { |
(...skipping 28 matching lines...) Expand all Loading... |
564 return pref_value_store()->PrefValueUserModifiable(name_.c_str()); | 567 return pref_value_store()->PrefValueUserModifiable(name_.c_str()); |
565 } | 568 } |
566 | 569 |
567 bool PrefService::Preference::IsExtensionModifiable() const { | 570 bool PrefService::Preference::IsExtensionModifiable() const { |
568 return pref_value_store()->PrefValueExtensionModifiable(name_.c_str()); | 571 return pref_value_store()->PrefValueExtensionModifiable(name_.c_str()); |
569 } | 572 } |
570 | 573 |
571 const base::Value* PrefService::GetPreferenceValue( | 574 const base::Value* PrefService::GetPreferenceValue( |
572 const std::string& path) const { | 575 const std::string& path) const { |
573 DCHECK(CalledOnValidThread()); | 576 DCHECK(CalledOnValidThread()); |
574 const Value* default_value = NULL; | 577 const base::Value* default_value = NULL; |
575 if (pref_registry_->defaults()->GetValue(path, &default_value)) { | 578 if (pref_registry_->defaults()->GetValue(path, &default_value)) { |
576 const Value* found_value = NULL; | 579 const base::Value* found_value = NULL; |
577 base::Value::Type default_type = default_value->GetType(); | 580 base::Value::Type default_type = default_value->GetType(); |
578 if (pref_value_store_->GetValue(path, default_type, &found_value)) { | 581 if (pref_value_store_->GetValue(path, default_type, &found_value)) { |
579 DCHECK(found_value->IsType(default_type)); | 582 DCHECK(found_value->IsType(default_type)); |
580 return found_value; | 583 return found_value; |
581 } else { | 584 } else { |
582 // Every registered preference has at least a default value. | 585 // Every registered preference has at least a default value. |
583 NOTREACHED() << "no valid value found for registered pref " << path; | 586 NOTREACHED() << "no valid value found for registered pref " << path; |
584 } | 587 } |
585 } | 588 } |
586 | 589 |
587 return NULL; | 590 return NULL; |
588 } | 591 } |
OLD | NEW |