| 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 "chrome/browser/prefs/pref_service.h" | 5 #include "chrome/browser/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/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 713 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 724 | 724 |
| 725 const Preference* pref = FindPreference(path); | 725 const Preference* pref = FindPreference(path); |
| 726 if (!pref) { | 726 if (!pref) { |
| 727 NOTREACHED() << "Trying to get an unregistered pref: " << path; | 727 NOTREACHED() << "Trying to get an unregistered pref: " << path; |
| 728 return NULL; | 728 return NULL; |
| 729 } | 729 } |
| 730 | 730 |
| 731 // Look for an existing preference in the user store. If it doesn't | 731 // Look for an existing preference in the user store. If it doesn't |
| 732 // exist, return NULL. | 732 // exist, return NULL. |
| 733 base::Value* value = NULL; | 733 base::Value* value = NULL; |
| 734 if (user_pref_store_->GetMutableValue(path, &value) != PrefStore::READ_OK) | 734 if (!user_pref_store_->GetMutableValue(path, &value)) |
| 735 return NULL; | 735 return NULL; |
| 736 | 736 |
| 737 if (!value->IsType(pref->GetType())) { | 737 if (!value->IsType(pref->GetType())) { |
| 738 NOTREACHED() << "Pref value type doesn't match registered type."; | 738 NOTREACHED() << "Pref value type doesn't match registered type."; |
| 739 return NULL; | 739 return NULL; |
| 740 } | 740 } |
| 741 | 741 |
| 742 return value; | 742 return value; |
| 743 } | 743 } |
| 744 | 744 |
| 745 const base::Value* PrefService::GetDefaultPrefValue(const char* path) const { | 745 const base::Value* PrefService::GetDefaultPrefValue(const char* path) const { |
| 746 DCHECK(CalledOnValidThread()); | 746 DCHECK(CalledOnValidThread()); |
| 747 // Lookup the preference in the default store. | 747 // Lookup the preference in the default store. |
| 748 const base::Value* value = NULL; | 748 const base::Value* value = NULL; |
| 749 if (default_store_->GetValue(path, &value) != PrefStore::READ_OK) { | 749 if (!default_store_->GetValue(path, &value)) { |
| 750 NOTREACHED() << "Default value missing for pref: " << path; | 750 NOTREACHED() << "Default value missing for pref: " << path; |
| 751 return NULL; | 751 return NULL; |
| 752 } | 752 } |
| 753 return value; | 753 return value; |
| 754 } | 754 } |
| 755 | 755 |
| 756 const ListValue* PrefService::GetList(const char* path) const { | 756 const ListValue* PrefService::GetList(const char* path) const { |
| 757 DCHECK(CalledOnValidThread()); | 757 DCHECK(CalledOnValidThread()); |
| 758 | 758 |
| 759 const Preference* pref = FindPreference(path); | 759 const Preference* pref = FindPreference(path); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 782 } | 782 } |
| 783 | 783 |
| 784 void PrefService::RegisterPreference(const char* path, | 784 void PrefService::RegisterPreference(const char* path, |
| 785 Value* default_value, | 785 Value* default_value, |
| 786 PrefSyncStatus sync_status) { | 786 PrefSyncStatus sync_status) { |
| 787 DCHECK(CalledOnValidThread()); | 787 DCHECK(CalledOnValidThread()); |
| 788 | 788 |
| 789 // The main code path takes ownership, but most don't. We'll be safe. | 789 // The main code path takes ownership, but most don't. We'll be safe. |
| 790 scoped_ptr<Value> scoped_value(default_value); | 790 scoped_ptr<Value> scoped_value(default_value); |
| 791 | 791 |
| 792 DCHECK(!FindPreference(path)) << "Tried to register duplicate pref " << path; | 792 CHECK(!FindPreference(path)) << "Tried to register duplicate pref " << path; |
| 793 | 793 |
| 794 base::Value::Type orig_type = default_value->GetType(); | 794 base::Value::Type orig_type = default_value->GetType(); |
| 795 DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) << | 795 DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) << |
| 796 "invalid preference type: " << orig_type; | 796 "invalid preference type: " << orig_type; |
| 797 | 797 |
| 798 // For ListValue and DictionaryValue with non empty default, empty value | 798 // For ListValue and DictionaryValue with non empty default, empty value |
| 799 // for |path| needs to be persisted in |user_pref_store_|. So that | 799 // for |path| needs to be persisted in |user_pref_store_|. So that |
| 800 // non empty default is not used when user sets an empty ListValue or | 800 // non empty default is not used when user sets an empty ListValue or |
| 801 // DictionaryValue. | 801 // DictionaryValue. |
| 802 bool needs_empty_value = false; | 802 bool needs_empty_value = false; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 817 | 817 |
| 818 // Register with sync if necessary. | 818 // Register with sync if necessary. |
| 819 if (sync_status == SYNCABLE_PREF && pref_sync_associator_.get()) | 819 if (sync_status == SYNCABLE_PREF && pref_sync_associator_.get()) |
| 820 pref_sync_associator_->RegisterPref(path); | 820 pref_sync_associator_->RegisterPref(path); |
| 821 } | 821 } |
| 822 | 822 |
| 823 void PrefService::UnregisterPreference(const char* path) { | 823 void PrefService::UnregisterPreference(const char* path) { |
| 824 DCHECK(CalledOnValidThread()); | 824 DCHECK(CalledOnValidThread()); |
| 825 | 825 |
| 826 PreferenceMap::iterator it = prefs_map_.find(path); | 826 PreferenceMap::iterator it = prefs_map_.find(path); |
| 827 if (it == prefs_map_.end()) { | 827 CHECK(it != prefs_map_.end()) << "Trying to unregister an unregistered pref: " |
| 828 NOTREACHED() << "Trying to unregister an unregistered pref: " << path; | 828 << path; |
| 829 return; | |
| 830 } | |
| 831 | 829 |
| 832 prefs_map_.erase(it); | 830 prefs_map_.erase(it); |
| 833 default_store_->RemoveDefaultValue(path); | 831 default_store_->RemoveDefaultValue(path); |
| 834 if (pref_sync_associator_.get() && | 832 if (pref_sync_associator_.get() && |
| 835 pref_sync_associator_->IsPrefRegistered(path)) { | 833 pref_sync_associator_->IsPrefRegistered(path)) { |
| 836 pref_sync_associator_->UnregisterPref(path); | 834 pref_sync_associator_->UnregisterPref(path); |
| 837 } | 835 } |
| 838 } | 836 } |
| 839 | 837 |
| 840 void PrefService::ClearPref(const char* path) { | 838 void PrefService::ClearPref(const char* path) { |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 925 return NULL; | 923 return NULL; |
| 926 } | 924 } |
| 927 if (pref->GetType() != type) { | 925 if (pref->GetType() != type) { |
| 928 NOTREACHED() << "Wrong type for GetMutableValue: " << path; | 926 NOTREACHED() << "Wrong type for GetMutableValue: " << path; |
| 929 return NULL; | 927 return NULL; |
| 930 } | 928 } |
| 931 | 929 |
| 932 // Look for an existing preference in the user store. If it doesn't | 930 // Look for an existing preference in the user store. If it doesn't |
| 933 // exist or isn't the correct type, create a new user preference. | 931 // exist or isn't the correct type, create a new user preference. |
| 934 Value* value = NULL; | 932 Value* value = NULL; |
| 935 if (user_pref_store_->GetMutableValue(path, &value) != PrefStore::READ_OK || | 933 if (!user_pref_store_->GetMutableValue(path, &value) || |
| 936 !value->IsType(type)) { | 934 !value->IsType(type)) { |
| 937 if (type == Value::TYPE_DICTIONARY) { | 935 if (type == Value::TYPE_DICTIONARY) { |
| 938 value = new DictionaryValue; | 936 value = new DictionaryValue; |
| 939 } else if (type == Value::TYPE_LIST) { | 937 } else if (type == Value::TYPE_LIST) { |
| 940 value = new ListValue; | 938 value = new ListValue; |
| 941 } else { | 939 } else { |
| 942 NOTREACHED(); | 940 NOTREACHED(); |
| 943 } | 941 } |
| 944 user_pref_store_->SetValueSilently(path, value); | 942 user_pref_store_->SetValueSilently(path, value); |
| 945 } | 943 } |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1059 return pref_value_store()->PrefValueFromDefaultStore(name_.c_str()); | 1057 return pref_value_store()->PrefValueFromDefaultStore(name_.c_str()); |
| 1060 } | 1058 } |
| 1061 | 1059 |
| 1062 bool PrefService::Preference::IsUserModifiable() const { | 1060 bool PrefService::Preference::IsUserModifiable() const { |
| 1063 return pref_value_store()->PrefValueUserModifiable(name_.c_str()); | 1061 return pref_value_store()->PrefValueUserModifiable(name_.c_str()); |
| 1064 } | 1062 } |
| 1065 | 1063 |
| 1066 bool PrefService::Preference::IsExtensionModifiable() const { | 1064 bool PrefService::Preference::IsExtensionModifiable() const { |
| 1067 return pref_value_store()->PrefValueExtensionModifiable(name_.c_str()); | 1065 return pref_value_store()->PrefValueExtensionModifiable(name_.c_str()); |
| 1068 } | 1066 } |
| OLD | NEW |