| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/common/pref_service.h" | 5 #include "chrome/common/pref_service.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/stl_util-inl.h" | 10 #include "base/stl_util-inl.h" |
| 11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| (...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 if (!pref) { | 428 if (!pref) { |
| 429 NOTREACHED() << "Trying to write an unregistered pref: " << path; | 429 NOTREACHED() << "Trying to write an unregistered pref: " << path; |
| 430 return; | 430 return; |
| 431 } | 431 } |
| 432 if (pref->type() != Value::TYPE_BOOLEAN) { | 432 if (pref->type() != Value::TYPE_BOOLEAN) { |
| 433 NOTREACHED() << "Wrong type for SetBoolean: " << path; | 433 NOTREACHED() << "Wrong type for SetBoolean: " << path; |
| 434 return; | 434 return; |
| 435 } | 435 } |
| 436 | 436 |
| 437 scoped_ptr<Value> old_value(GetPrefCopy(path)); | 437 scoped_ptr<Value> old_value(GetPrefCopy(path)); |
| 438 bool rv = persistent_->SetBoolean(path, value); | 438 persistent_->SetBoolean(path, value); |
| 439 DCHECK(rv); | |
| 440 | 439 |
| 441 FireObserversIfChanged(path, old_value.get()); | 440 FireObserversIfChanged(path, old_value.get()); |
| 442 } | 441 } |
| 443 | 442 |
| 444 void PrefService::SetInteger(const wchar_t* path, int value) { | 443 void PrefService::SetInteger(const wchar_t* path, int value) { |
| 445 DCHECK(CalledOnValidThread()); | 444 DCHECK(CalledOnValidThread()); |
| 446 | 445 |
| 447 const Preference* pref = FindPreference(path); | 446 const Preference* pref = FindPreference(path); |
| 448 if (!pref) { | 447 if (!pref) { |
| 449 NOTREACHED() << "Trying to write an unregistered pref: " << path; | 448 NOTREACHED() << "Trying to write an unregistered pref: " << path; |
| 450 return; | 449 return; |
| 451 } | 450 } |
| 452 if (pref->type() != Value::TYPE_INTEGER) { | 451 if (pref->type() != Value::TYPE_INTEGER) { |
| 453 NOTREACHED() << "Wrong type for SetInteger: " << path; | 452 NOTREACHED() << "Wrong type for SetInteger: " << path; |
| 454 return; | 453 return; |
| 455 } | 454 } |
| 456 | 455 |
| 457 scoped_ptr<Value> old_value(GetPrefCopy(path)); | 456 scoped_ptr<Value> old_value(GetPrefCopy(path)); |
| 458 bool rv = persistent_->SetInteger(path, value); | 457 persistent_->SetInteger(path, value); |
| 459 DCHECK(rv); | |
| 460 | 458 |
| 461 FireObserversIfChanged(path, old_value.get()); | 459 FireObserversIfChanged(path, old_value.get()); |
| 462 } | 460 } |
| 463 | 461 |
| 464 void PrefService::SetReal(const wchar_t* path, double value) { | 462 void PrefService::SetReal(const wchar_t* path, double value) { |
| 465 DCHECK(CalledOnValidThread()); | 463 DCHECK(CalledOnValidThread()); |
| 466 | 464 |
| 467 const Preference* pref = FindPreference(path); | 465 const Preference* pref = FindPreference(path); |
| 468 if (!pref) { | 466 if (!pref) { |
| 469 NOTREACHED() << "Trying to write an unregistered pref: " << path; | 467 NOTREACHED() << "Trying to write an unregistered pref: " << path; |
| 470 return; | 468 return; |
| 471 } | 469 } |
| 472 if (pref->type() != Value::TYPE_REAL) { | 470 if (pref->type() != Value::TYPE_REAL) { |
| 473 NOTREACHED() << "Wrong type for SetReal: " << path; | 471 NOTREACHED() << "Wrong type for SetReal: " << path; |
| 474 return; | 472 return; |
| 475 } | 473 } |
| 476 | 474 |
| 477 scoped_ptr<Value> old_value(GetPrefCopy(path)); | 475 scoped_ptr<Value> old_value(GetPrefCopy(path)); |
| 478 bool rv = persistent_->SetReal(path, value); | 476 persistent_->SetReal(path, value); |
| 479 DCHECK(rv); | |
| 480 | 477 |
| 481 FireObserversIfChanged(path, old_value.get()); | 478 FireObserversIfChanged(path, old_value.get()); |
| 482 } | 479 } |
| 483 | 480 |
| 484 void PrefService::SetString(const wchar_t* path, const std::wstring& value) { | 481 void PrefService::SetString(const wchar_t* path, const std::wstring& value) { |
| 485 DCHECK(CalledOnValidThread()); | 482 DCHECK(CalledOnValidThread()); |
| 486 | 483 |
| 487 const Preference* pref = FindPreference(path); | 484 const Preference* pref = FindPreference(path); |
| 488 if (!pref) { | 485 if (!pref) { |
| 489 NOTREACHED() << "Trying to write an unregistered pref: " << path; | 486 NOTREACHED() << "Trying to write an unregistered pref: " << path; |
| 490 return; | 487 return; |
| 491 } | 488 } |
| 492 if (pref->type() != Value::TYPE_STRING) { | 489 if (pref->type() != Value::TYPE_STRING) { |
| 493 NOTREACHED() << "Wrong type for SetString: " << path; | 490 NOTREACHED() << "Wrong type for SetString: " << path; |
| 494 return; | 491 return; |
| 495 } | 492 } |
| 496 | 493 |
| 497 scoped_ptr<Value> old_value(GetPrefCopy(path)); | 494 scoped_ptr<Value> old_value(GetPrefCopy(path)); |
| 498 bool rv = persistent_->SetString(path, value); | 495 persistent_->SetString(path, value); |
| 499 DCHECK(rv); | |
| 500 | 496 |
| 501 FireObserversIfChanged(path, old_value.get()); | 497 FireObserversIfChanged(path, old_value.get()); |
| 502 } | 498 } |
| 503 | 499 |
| 504 void PrefService::SetFilePath(const wchar_t* path, const FilePath& value) { | 500 void PrefService::SetFilePath(const wchar_t* path, const FilePath& value) { |
| 505 DCHECK(CalledOnValidThread()); | 501 DCHECK(CalledOnValidThread()); |
| 506 | 502 |
| 507 const Preference* pref = FindPreference(path); | 503 const Preference* pref = FindPreference(path); |
| 508 if (!pref) { | 504 if (!pref) { |
| 509 NOTREACHED() << "Trying to write an unregistered pref: " << path; | 505 NOTREACHED() << "Trying to write an unregistered pref: " << path; |
| 510 return; | 506 return; |
| 511 } | 507 } |
| 512 if (pref->type() != Value::TYPE_STRING) { | 508 if (pref->type() != Value::TYPE_STRING) { |
| 513 NOTREACHED() << "Wrong type for SetFilePath: " << path; | 509 NOTREACHED() << "Wrong type for SetFilePath: " << path; |
| 514 return; | 510 return; |
| 515 } | 511 } |
| 516 | 512 |
| 517 scoped_ptr<Value> old_value(GetPrefCopy(path)); | 513 scoped_ptr<Value> old_value(GetPrefCopy(path)); |
| 518 #if defined(OS_POSIX) | 514 #if defined(OS_POSIX) |
| 519 // Value::SetString only knows about UTF8 strings, so convert the path from | 515 // Value::SetString only knows about UTF8 strings, so convert the path from |
| 520 // the system native value to UTF8. | 516 // the system native value to UTF8. |
| 521 std::string path_utf8 = WideToUTF8(base::SysNativeMBToWide(value.value())); | 517 std::string path_utf8 = WideToUTF8(base::SysNativeMBToWide(value.value())); |
| 522 bool rv = persistent_->SetString(path, path_utf8); | 518 persistent_->SetString(path, path_utf8); |
| 523 #else | 519 #else |
| 524 bool rv = persistent_->SetString(path, value.value()); | 520 persistent_->SetString(path, value.value()); |
| 525 #endif | 521 #endif |
| 526 DCHECK(rv); | |
| 527 | 522 |
| 528 FireObserversIfChanged(path, old_value.get()); | 523 FireObserversIfChanged(path, old_value.get()); |
| 529 } | 524 } |
| 530 | 525 |
| 531 void PrefService::SetInt64(const wchar_t* path, int64 value) { | 526 void PrefService::SetInt64(const wchar_t* path, int64 value) { |
| 532 DCHECK(CalledOnValidThread()); | 527 DCHECK(CalledOnValidThread()); |
| 533 | 528 |
| 534 const Preference* pref = FindPreference(path); | 529 const Preference* pref = FindPreference(path); |
| 535 if (!pref) { | 530 if (!pref) { |
| 536 NOTREACHED() << "Trying to write an unregistered pref: " << path; | 531 NOTREACHED() << "Trying to write an unregistered pref: " << path; |
| 537 return; | 532 return; |
| 538 } | 533 } |
| 539 if (pref->type() != Value::TYPE_STRING) { | 534 if (pref->type() != Value::TYPE_STRING) { |
| 540 NOTREACHED() << "Wrong type for SetInt64: " << path; | 535 NOTREACHED() << "Wrong type for SetInt64: " << path; |
| 541 return; | 536 return; |
| 542 } | 537 } |
| 543 | 538 |
| 544 scoped_ptr<Value> old_value(GetPrefCopy(path)); | 539 scoped_ptr<Value> old_value(GetPrefCopy(path)); |
| 545 bool rv = persistent_->SetString(path, Int64ToWString(value)); | 540 persistent_->SetString(path, Int64ToWString(value)); |
| 546 DCHECK(rv); | |
| 547 | 541 |
| 548 FireObserversIfChanged(path, old_value.get()); | 542 FireObserversIfChanged(path, old_value.get()); |
| 549 } | 543 } |
| 550 | 544 |
| 551 int64 PrefService::GetInt64(const wchar_t* path) const { | 545 int64 PrefService::GetInt64(const wchar_t* path) const { |
| 552 DCHECK(CalledOnValidThread()); | 546 DCHECK(CalledOnValidThread()); |
| 553 | 547 |
| 554 std::wstring result; | 548 std::wstring result; |
| 555 if (transient_->GetString(path, &result)) | 549 if (transient_->GetString(path, &result)) |
| 556 return StringToInt64(WideToUTF16Hack(result)); | 550 return StringToInt64(WideToUTF16Hack(result)); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 578 if (!pref) { | 572 if (!pref) { |
| 579 NOTREACHED() << "Trying to get an unregistered pref: " << path; | 573 NOTREACHED() << "Trying to get an unregistered pref: " << path; |
| 580 return NULL; | 574 return NULL; |
| 581 } | 575 } |
| 582 if (pref->type() != Value::TYPE_DICTIONARY) { | 576 if (pref->type() != Value::TYPE_DICTIONARY) { |
| 583 NOTREACHED() << "Wrong type for GetMutableDictionary: " << path; | 577 NOTREACHED() << "Wrong type for GetMutableDictionary: " << path; |
| 584 return NULL; | 578 return NULL; |
| 585 } | 579 } |
| 586 | 580 |
| 587 DictionaryValue* dict = NULL; | 581 DictionaryValue* dict = NULL; |
| 588 bool rv = persistent_->GetDictionary(path, &dict); | 582 if (!persistent_->GetDictionary(path, &dict)) { |
| 589 if (!rv) { | |
| 590 dict = new DictionaryValue; | 583 dict = new DictionaryValue; |
| 591 rv = persistent_->Set(path, dict); | 584 persistent_->Set(path, dict); |
| 592 DCHECK(rv); | |
| 593 } | 585 } |
| 594 return dict; | 586 return dict; |
| 595 } | 587 } |
| 596 | 588 |
| 597 ListValue* PrefService::GetMutableList(const wchar_t* path) { | 589 ListValue* PrefService::GetMutableList(const wchar_t* path) { |
| 598 DCHECK(CalledOnValidThread()); | 590 DCHECK(CalledOnValidThread()); |
| 599 | 591 |
| 600 const Preference* pref = FindPreference(path); | 592 const Preference* pref = FindPreference(path); |
| 601 if (!pref) { | 593 if (!pref) { |
| 602 NOTREACHED() << "Trying to get an unregistered pref: " << path; | 594 NOTREACHED() << "Trying to get an unregistered pref: " << path; |
| 603 return NULL; | 595 return NULL; |
| 604 } | 596 } |
| 605 if (pref->type() != Value::TYPE_LIST) { | 597 if (pref->type() != Value::TYPE_LIST) { |
| 606 NOTREACHED() << "Wrong type for GetMutableList: " << path; | 598 NOTREACHED() << "Wrong type for GetMutableList: " << path; |
| 607 return NULL; | 599 return NULL; |
| 608 } | 600 } |
| 609 | 601 |
| 610 ListValue* list = NULL; | 602 ListValue* list = NULL; |
| 611 bool rv = persistent_->GetList(path, &list); | 603 if (!persistent_->GetList(path, &list)) { |
| 612 if (!rv) { | |
| 613 list = new ListValue; | 604 list = new ListValue; |
| 614 rv = persistent_->Set(path, list); | 605 persistent_->Set(path, list); |
| 615 DCHECK(rv); | |
| 616 } | 606 } |
| 617 return list; | 607 return list; |
| 618 } | 608 } |
| 619 | 609 |
| 620 Value* PrefService::GetPrefCopy(const wchar_t* path) { | 610 Value* PrefService::GetPrefCopy(const wchar_t* path) { |
| 621 DCHECK(CalledOnValidThread()); | 611 DCHECK(CalledOnValidThread()); |
| 622 | 612 |
| 623 const Preference* pref = FindPreference(path); | 613 const Preference* pref = FindPreference(path); |
| 624 DCHECK(pref); | 614 DCHECK(pref); |
| 625 return pref->GetValue()->DeepCopy(); | 615 return pref->GetValue()->DeepCopy(); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 695 } | 685 } |
| 696 | 686 |
| 697 // Pref not found, just return the app default. | 687 // Pref not found, just return the app default. |
| 698 return default_value_.get(); | 688 return default_value_.get(); |
| 699 } | 689 } |
| 700 | 690 |
| 701 bool PrefService::Preference::IsDefaultValue() const { | 691 bool PrefService::Preference::IsDefaultValue() const { |
| 702 DCHECK(default_value_.get()); | 692 DCHECK(default_value_.get()); |
| 703 return default_value_->Equals(GetValue()); | 693 return default_value_->Equals(GetValue()); |
| 704 } | 694 } |
| OLD | NEW |