Chromium Code Reviews| 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 453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 464 | 464 |
| 465 void PrefService::RegisterStringPref(const char* path, | 465 void PrefService::RegisterStringPref(const char* path, |
| 466 const std::string& default_value, | 466 const std::string& default_value, |
| 467 PrefSyncStatus sync_status) { | 467 PrefSyncStatus sync_status) { |
| 468 DCHECK(IsProfilePrefService(this)); | 468 DCHECK(IsProfilePrefService(this)); |
| 469 RegisterPreference(path, | 469 RegisterPreference(path, |
| 470 Value::CreateStringValue(default_value), | 470 Value::CreateStringValue(default_value), |
| 471 sync_status); | 471 sync_status); |
| 472 } | 472 } |
| 473 | 473 |
| 474 void PrefService::RegisterStringPrefIfNew(const char* path, | |
| 475 const std::string& default_value, | |
| 476 PrefSyncStatus sync_status) { | |
| 477 DCHECK(IsProfilePrefService(this)); | |
| 478 RegisterPreferenceIfNew(path, | |
| 479 Value::CreateStringValue(default_value), | |
| 480 sync_status); | |
| 481 } | |
| 482 | |
| 474 void PrefService::RegisterFilePathPref(const char* path, | 483 void PrefService::RegisterFilePathPref(const char* path, |
| 475 const FilePath& default_value, | 484 const FilePath& default_value, |
| 476 PrefSyncStatus sync_status) { | 485 PrefSyncStatus sync_status) { |
| 477 DCHECK(IsProfilePrefService(this)); | 486 DCHECK(IsProfilePrefService(this)); |
| 478 RegisterPreference(path, | 487 RegisterPreference(path, |
| 479 Value::CreateStringValue(default_value.value()), | 488 Value::CreateStringValue(default_value.value()), |
| 480 sync_status); | 489 sync_status); |
| 481 } | 490 } |
| 482 | 491 |
| 483 void PrefService::RegisterListPref(const char* path, | 492 void PrefService::RegisterListPref(const char* path, |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 772 void PrefService::AddPrefObserver(const char* path, | 781 void PrefService::AddPrefObserver(const char* path, |
| 773 content::NotificationObserver* obs) { | 782 content::NotificationObserver* obs) { |
| 774 pref_notifier_->AddPrefObserver(path, obs); | 783 pref_notifier_->AddPrefObserver(path, obs); |
| 775 } | 784 } |
| 776 | 785 |
| 777 void PrefService::RemovePrefObserver(const char* path, | 786 void PrefService::RemovePrefObserver(const char* path, |
| 778 content::NotificationObserver* obs) { | 787 content::NotificationObserver* obs) { |
| 779 pref_notifier_->RemovePrefObserver(path, obs); | 788 pref_notifier_->RemovePrefObserver(path, obs); |
| 780 } | 789 } |
| 781 | 790 |
| 782 void PrefService::RegisterPreference(const char* path, | 791 void PrefService::RegisterNewPreference(const char* path, |
| 783 Value* default_value, | 792 Value* default_value, |
| 784 PrefSyncStatus sync_status) { | 793 PrefSyncStatus sync_status) { |
| 794 // Note: The perfomance of this method is critical to startup performance, | |
| 795 // at least on Android, so do not introduce extra checks unless they are | |
| 796 // compiled out of official builds. | |
| 785 DCHECK(CalledOnValidThread()); | 797 DCHECK(CalledOnValidThread()); |
| 786 | 798 |
| 787 // The main code path takes ownership, but most don't. We'll be safe. | 799 // The main code path takes ownership, but most don't. We'll be safe. |
| 788 scoped_ptr<Value> scoped_value(default_value); | 800 scoped_ptr<Value> scoped_value(default_value); |
| 789 | 801 |
| 790 if (FindPreference(path)) { | |
| 791 NOTREACHED() << "Tried to register duplicate pref " << path; | |
|
Bernhard Bauer
2012/10/30 14:34:16
Seeing as this is just a NOTREACHED (which would i
| |
| 792 return; | |
| 793 } | |
| 794 | |
| 795 base::Value::Type orig_type = default_value->GetType(); | 802 base::Value::Type orig_type = default_value->GetType(); |
| 796 DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) << | 803 DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) << |
| 797 "invalid preference type: " << orig_type; | 804 "invalid preference type: " << orig_type; |
| 798 | 805 |
| 799 // For ListValue and DictionaryValue with non empty default, empty value | 806 // For ListValue and DictionaryValue with non empty default, empty value |
| 800 // for |path| needs to be persisted in |user_pref_store_|. So that | 807 // for |path| needs to be persisted in |user_pref_store_|. So that |
| 801 // non empty default is not used when user sets an empty ListValue or | 808 // non empty default is not used when user sets an empty ListValue or |
| 802 // DictionaryValue. | 809 // DictionaryValue. |
| 803 bool needs_empty_value = false; | 810 bool needs_empty_value = false; |
| 804 if (orig_type == base::Value::TYPE_LIST) { | 811 if (orig_type == base::Value::TYPE_LIST) { |
| 805 const base::ListValue* list = NULL; | 812 const base::ListValue* list = NULL; |
| 806 if (default_value->GetAsList(&list) && !list->empty()) | 813 if (default_value->GetAsList(&list) && !list->empty()) |
| 807 needs_empty_value = true; | 814 needs_empty_value = true; |
| 808 } else if (orig_type == base::Value::TYPE_DICTIONARY) { | 815 } else if (orig_type == base::Value::TYPE_DICTIONARY) { |
| 809 const base::DictionaryValue* dict = NULL; | 816 const base::DictionaryValue* dict = NULL; |
| 810 if (default_value->GetAsDictionary(&dict) && !dict->empty()) | 817 if (default_value->GetAsDictionary(&dict) && !dict->empty()) |
| 811 needs_empty_value = true; | 818 needs_empty_value = true; |
| 812 } | 819 } |
| 813 if (needs_empty_value) | 820 if (needs_empty_value) |
| 814 user_pref_store_->MarkNeedsEmptyValue(path); | 821 user_pref_store_->MarkNeedsEmptyValue(path); |
| 815 | 822 |
| 816 // Hand off ownership. | 823 // Hand off ownership. |
| 817 default_store_->SetDefaultValue(path, scoped_value.release()); | 824 default_store_->SetDefaultValue(path, scoped_value.release()); |
| 818 | 825 |
| 819 // Register with sync if necessary. | 826 // Register with sync if necessary. |
| 820 if (sync_status == SYNCABLE_PREF && pref_sync_associator_.get()) | 827 if (sync_status == SYNCABLE_PREF && pref_sync_associator_.get()) |
| 821 pref_sync_associator_->RegisterPref(path); | 828 pref_sync_associator_->RegisterPref(path); |
| 822 } | 829 } |
| 823 | 830 |
| 831 void PrefService::RegisterPreference(const char* path, | |
| 832 Value* default_value, | |
| 833 PrefSyncStatus sync_status) { | |
| 834 if (default_store_->GetType(path) != Value::TYPE_NULL) { | |
| 835 // Preference already exists | |
| 836 NOTREACHED() << "Tried to register duplicate pref " << path; | |
| 837 return; | |
| 838 } | |
| 839 RegisterNewPreference(path, default_value, sync_status); | |
| 840 } | |
| 841 | |
| 842 void PrefService::RegisterPreferenceIfNew(const char* path, | |
| 843 Value* default_value, | |
| 844 PrefSyncStatus sync_status) { | |
| 845 if (default_store_->GetType(path) != Value::TYPE_NULL) { | |
| 846 // Preference already exists | |
| 847 return; | |
| 848 } | |
| 849 RegisterNewPreference(path, default_value, sync_status); | |
| 850 } | |
| 851 | |
| 824 void PrefService::UnregisterPreference(const char* path) { | 852 void PrefService::UnregisterPreference(const char* path) { |
| 825 DCHECK(CalledOnValidThread()); | 853 DCHECK(CalledOnValidThread()); |
| 826 | 854 |
| 827 PreferenceMap::iterator it = prefs_map_.find(path); | 855 PreferenceMap::iterator it = prefs_map_.find(path); |
| 828 if (it == prefs_map_.end()) { | 856 if (it == prefs_map_.end()) { |
| 829 NOTREACHED() << "Trying to unregister an unregistered pref: " << path; | 857 NOTREACHED() << "Trying to unregister an unregistered pref: " << path; |
| 830 return; | 858 return; |
| 831 } | 859 } |
| 832 | 860 |
| 833 prefs_map_.erase(it); | 861 prefs_map_.erase(it); |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1060 return pref_value_store()->PrefValueFromDefaultStore(name_.c_str()); | 1088 return pref_value_store()->PrefValueFromDefaultStore(name_.c_str()); |
| 1061 } | 1089 } |
| 1062 | 1090 |
| 1063 bool PrefService::Preference::IsUserModifiable() const { | 1091 bool PrefService::Preference::IsUserModifiable() const { |
| 1064 return pref_value_store()->PrefValueUserModifiable(name_.c_str()); | 1092 return pref_value_store()->PrefValueUserModifiable(name_.c_str()); |
| 1065 } | 1093 } |
| 1066 | 1094 |
| 1067 bool PrefService::Preference::IsExtensionModifiable() const { | 1095 bool PrefService::Preference::IsExtensionModifiable() const { |
| 1068 return pref_value_store()->PrefValueExtensionModifiable(name_.c_str()); | 1096 return pref_value_store()->PrefValueExtensionModifiable(name_.c_str()); |
| 1069 } | 1097 } |
| OLD | NEW |