OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/browser/prefs/pref_service.h" | 5 #include "chrome/browser/prefs/pref_service.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 #include "content/common/notification_service.h" | 33 #include "content/common/notification_service.h" |
34 #include "grit/chromium_strings.h" | 34 #include "grit/chromium_strings.h" |
35 #include "grit/generated_resources.h" | 35 #include "grit/generated_resources.h" |
36 #include "ui/base/l10n/l10n_util.h" | 36 #include "ui/base/l10n/l10n_util.h" |
37 | 37 |
38 namespace { | 38 namespace { |
39 | 39 |
40 // A helper function for RegisterLocalized*Pref that creates a Value* based on | 40 // A helper function for RegisterLocalized*Pref that creates a Value* based on |
41 // the string value in the locale dll. Because we control the values in a | 41 // the string value in the locale dll. Because we control the values in a |
42 // locale dll, this should always return a Value of the appropriate type. | 42 // locale dll, this should always return a Value of the appropriate type. |
43 Value* CreateLocaleDefaultValue(Value::ValueType type, int message_id) { | 43 Value* CreateLocaleDefaultValue(base::Value::Type type, int message_id) { |
44 std::string resource_string = l10n_util::GetStringUTF8(message_id); | 44 std::string resource_string = l10n_util::GetStringUTF8(message_id); |
45 DCHECK(!resource_string.empty()); | 45 DCHECK(!resource_string.empty()); |
46 switch (type) { | 46 switch (type) { |
47 case Value::TYPE_BOOLEAN: { | 47 case Value::TYPE_BOOLEAN: { |
48 if ("true" == resource_string) | 48 if ("true" == resource_string) |
49 return Value::CreateBooleanValue(true); | 49 return Value::CreateBooleanValue(true); |
50 if ("false" == resource_string) | 50 if ("false" == resource_string) |
51 return Value::CreateBooleanValue(false); | 51 return Value::CreateBooleanValue(false); |
52 break; | 52 break; |
53 } | 53 } |
(...skipping 577 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
631 return out; | 631 return out; |
632 } | 632 } |
633 | 633 |
634 const PrefService::Preference* PrefService::FindPreference( | 634 const PrefService::Preference* PrefService::FindPreference( |
635 const char* pref_name) const { | 635 const char* pref_name) const { |
636 DCHECK(CalledOnValidThread()); | 636 DCHECK(CalledOnValidThread()); |
637 Preference p(this, pref_name, Value::TYPE_NULL); | 637 Preference p(this, pref_name, Value::TYPE_NULL); |
638 PreferenceSet::const_iterator it = prefs_.find(&p); | 638 PreferenceSet::const_iterator it = prefs_.find(&p); |
639 if (it != prefs_.end()) | 639 if (it != prefs_.end()) |
640 return *it; | 640 return *it; |
641 const Value::ValueType type = default_store_->GetType(pref_name); | 641 const base::Value::Type type = default_store_->GetType(pref_name); |
642 if (type == Value::TYPE_NULL) | 642 if (type == Value::TYPE_NULL) |
643 return NULL; | 643 return NULL; |
644 Preference* new_pref = new Preference(this, pref_name, type); | 644 Preference* new_pref = new Preference(this, pref_name, type); |
645 prefs_.insert(new_pref); | 645 prefs_.insert(new_pref); |
646 return new_pref; | 646 return new_pref; |
647 } | 647 } |
648 | 648 |
649 bool PrefService::ReadOnly() const { | 649 bool PrefService::ReadOnly() const { |
650 return user_pref_store_->ReadOnly(); | 650 return user_pref_store_->ReadOnly(); |
651 } | 651 } |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
703 DCHECK(CalledOnValidThread()); | 703 DCHECK(CalledOnValidThread()); |
704 | 704 |
705 // The main code path takes ownership, but most don't. We'll be safe. | 705 // The main code path takes ownership, but most don't. We'll be safe. |
706 scoped_ptr<Value> scoped_value(default_value); | 706 scoped_ptr<Value> scoped_value(default_value); |
707 | 707 |
708 if (FindPreference(path)) { | 708 if (FindPreference(path)) { |
709 NOTREACHED() << "Tried to register duplicate pref " << path; | 709 NOTREACHED() << "Tried to register duplicate pref " << path; |
710 return; | 710 return; |
711 } | 711 } |
712 | 712 |
713 Value::ValueType orig_type = default_value->GetType(); | 713 base::Value::Type orig_type = default_value->GetType(); |
714 DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) << | 714 DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) << |
715 "invalid preference type: " << orig_type; | 715 "invalid preference type: " << orig_type; |
716 | 716 |
717 // Hand off ownership. | 717 // Hand off ownership. |
718 default_store_->SetDefaultValue(path, scoped_value.release()); | 718 default_store_->SetDefaultValue(path, scoped_value.release()); |
719 | 719 |
720 // Register with sync if necessary. | 720 // Register with sync if necessary. |
721 if (sync_status == SYNCABLE_PREF && pref_sync_associator_.get()) | 721 if (sync_status == SYNCABLE_PREF && pref_sync_associator_.get()) |
722 pref_sync_associator_->RegisterPref(path); | 722 pref_sync_associator_->RegisterPref(path); |
723 } | 723 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
772 std::string result("0"); | 772 std::string result("0"); |
773 bool rv = pref->GetValue()->GetAsString(&result); | 773 bool rv = pref->GetValue()->GetAsString(&result); |
774 DCHECK(rv); | 774 DCHECK(rv); |
775 | 775 |
776 int64 val; | 776 int64 val; |
777 base::StringToInt64(result, &val); | 777 base::StringToInt64(result, &val); |
778 return val; | 778 return val; |
779 } | 779 } |
780 | 780 |
781 Value* PrefService::GetMutableUserPref(const char* path, | 781 Value* PrefService::GetMutableUserPref(const char* path, |
782 Value::ValueType type) { | 782 base::Value::Type type) { |
783 CHECK(type == Value::TYPE_DICTIONARY || type == Value::TYPE_LIST); | 783 CHECK(type == Value::TYPE_DICTIONARY || type == Value::TYPE_LIST); |
784 DCHECK(CalledOnValidThread()); | 784 DCHECK(CalledOnValidThread()); |
785 | 785 |
786 const Preference* pref = FindPreference(path); | 786 const Preference* pref = FindPreference(path); |
787 if (!pref) { | 787 if (!pref) { |
788 NOTREACHED() << "Trying to get an unregistered pref: " << path; | 788 NOTREACHED() << "Trying to get an unregistered pref: " << path; |
789 return NULL; | 789 return NULL; |
790 } | 790 } |
791 if (pref->GetType() != type) { | 791 if (pref->GetType() != type) { |
792 NOTREACHED() << "Wrong type for GetMutableValue: " << path; | 792 NOTREACHED() << "Wrong type for GetMutableValue: " << path; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
836 | 836 |
837 SyncableService* PrefService::GetSyncableService() { | 837 SyncableService* PrefService::GetSyncableService() { |
838 return pref_sync_associator_.get(); | 838 return pref_sync_associator_.get(); |
839 } | 839 } |
840 | 840 |
841 /////////////////////////////////////////////////////////////////////////////// | 841 /////////////////////////////////////////////////////////////////////////////// |
842 // PrefService::Preference | 842 // PrefService::Preference |
843 | 843 |
844 PrefService::Preference::Preference(const PrefService* service, | 844 PrefService::Preference::Preference(const PrefService* service, |
845 const char* name, | 845 const char* name, |
846 Value::ValueType type) | 846 base::Value::Type type) |
847 : name_(name), | 847 : name_(name), |
848 type_(type), | 848 type_(type), |
849 pref_service_(service) { | 849 pref_service_(service) { |
850 DCHECK(name); | 850 DCHECK(name); |
851 DCHECK(service); | 851 DCHECK(service); |
852 } | 852 } |
853 | 853 |
854 Value::ValueType PrefService::Preference::GetType() const { | 854 base::Value::Type PrefService::Preference::GetType() const { |
855 return type_; | 855 return type_; |
856 } | 856 } |
857 | 857 |
858 const Value* PrefService::Preference::GetValue() const { | 858 const Value* PrefService::Preference::GetValue() const { |
859 DCHECK(pref_service_->FindPreference(name_.c_str())) << | 859 DCHECK(pref_service_->FindPreference(name_.c_str())) << |
860 "Must register pref before getting its value"; | 860 "Must register pref before getting its value"; |
861 | 861 |
862 const Value* found_value = NULL; | 862 const Value* found_value = NULL; |
863 if (pref_value_store()->GetValue(name_, type_, &found_value)) { | 863 if (pref_value_store()->GetValue(name_, type_, &found_value)) { |
864 DCHECK(found_value->IsType(type_)); | 864 DCHECK(found_value->IsType(type_)); |
(...skipping 29 matching lines...) Expand all Loading... |
894 return pref_value_store()->PrefValueFromDefaultStore(name_.c_str()); | 894 return pref_value_store()->PrefValueFromDefaultStore(name_.c_str()); |
895 } | 895 } |
896 | 896 |
897 bool PrefService::Preference::IsUserModifiable() const { | 897 bool PrefService::Preference::IsUserModifiable() const { |
898 return pref_value_store()->PrefValueUserModifiable(name_.c_str()); | 898 return pref_value_store()->PrefValueUserModifiable(name_.c_str()); |
899 } | 899 } |
900 | 900 |
901 bool PrefService::Preference::IsExtensionModifiable() const { | 901 bool PrefService::Preference::IsExtensionModifiable() const { |
902 return pref_value_store()->PrefValueExtensionModifiable(name_.c_str()); | 902 return pref_value_store()->PrefValueExtensionModifiable(name_.c_str()); |
903 } | 903 } |
OLD | NEW |