| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/pref_value_store.h" | 5 #include "chrome/browser/pref_value_store.h" |
| 6 | 6 |
| 7 PrefValueStore::PrefValueStore(PrefStore* managed_prefs, | 7 PrefValueStore::PrefValueStore(PrefStore* managed_prefs, |
| 8 PrefStore* extension_prefs, |
| 8 PrefStore* user_prefs, | 9 PrefStore* user_prefs, |
| 9 PrefStore* recommended_prefs) | 10 PrefStore* recommended_prefs) { |
| 10 : managed_prefs_(managed_prefs), | 11 pref_stores_[MANAGED].reset(managed_prefs); |
| 11 user_prefs_(user_prefs), | 12 pref_stores_[EXTENSION].reset(extension_prefs); |
| 12 recommended_prefs_(recommended_prefs) { | 13 pref_stores_[USER].reset(user_prefs); |
| 14 pref_stores_[RECOMMENDED].reset(recommended_prefs); |
| 13 } | 15 } |
| 14 | 16 |
| 15 PrefValueStore::~PrefValueStore() { } | 17 PrefValueStore::~PrefValueStore() { } |
| 16 | 18 |
| 17 bool PrefValueStore::GetValue( | 19 bool PrefValueStore::GetValue(const std::wstring& name, |
| 18 const std::wstring& name, Value** out_value) const { | 20 Value** out_value) const { |
| 19 // Check the |PrefStore|s in order of their priority from highest to lowest | 21 // Check the |PrefStore|s in order of their priority from highest to lowest |
| 20 // to find the value of the preference described by the given preference name. | 22 // to find the value of the preference described by the given preference name. |
| 21 if (managed_prefs_.get() && | 23 for (size_t i = 0; i <= PREF_STORE_TYPE_MAX; ++i) { |
| 22 managed_prefs_->prefs()->Get(name.c_str(), out_value) ) { | 24 if (pref_stores_[i].get() && |
| 23 return true; | 25 pref_stores_[i]->prefs()->Get(name.c_str(), out_value)) { |
| 24 } else if (user_prefs_.get() && | 26 return true; |
| 25 user_prefs_->prefs()->Get(name.c_str(), out_value) ) { | 27 } |
| 26 return true; | |
| 27 } else if (recommended_prefs_.get() && | |
| 28 recommended_prefs_->prefs()->Get(name.c_str(), out_value)) { | |
| 29 return true; | |
| 30 } | 28 } |
| 31 // No value found for the given preference name, set the return false. | 29 // No value found for the given preference name, set the return false. |
| 32 *out_value = NULL; | 30 *out_value = NULL; |
| 33 return false; | 31 return false; |
| 34 } | 32 } |
| 35 | 33 |
| 36 bool PrefValueStore::WritePrefs() { | 34 bool PrefValueStore::WritePrefs() { |
| 37 // Managed and recommended preferences are not set by the user. | 35 bool success = true; |
| 38 // Hence they will not be written out. | 36 for (size_t i = 0; i <= PREF_STORE_TYPE_MAX; ++i) { |
| 39 return user_prefs_->WritePrefs(); | 37 if (pref_stores_[i].get()) |
| 38 success = pref_stores_[i]->WritePrefs() && success; |
| 39 } |
| 40 return success; |
| 40 } | 41 } |
| 41 | 42 |
| 42 void PrefValueStore::ScheduleWritePrefs() { | 43 void PrefValueStore::ScheduleWritePrefs() { |
| 43 // Managed and recommended preferences are not set by the user. | 44 for (size_t i = 0; i <= PREF_STORE_TYPE_MAX; ++i) { |
| 44 // Hence they will not be written out. | 45 if (pref_stores_[i].get()) |
| 45 user_prefs_->ScheduleWritePrefs(); | 46 pref_stores_[i]->ScheduleWritePrefs(); |
| 47 } |
| 46 } | 48 } |
| 47 | 49 |
| 48 PrefStore::PrefReadError PrefValueStore::ReadPrefs() { | 50 PrefStore::PrefReadError PrefValueStore::ReadPrefs() { |
| 49 PrefStore::PrefReadError managed_pref_error = PrefStore::PREF_READ_ERROR_NONE; | 51 PrefStore::PrefReadError result = PrefStore::PREF_READ_ERROR_NONE; |
| 50 PrefStore::PrefReadError user_pref_error = PrefStore::PREF_READ_ERROR_NONE; | 52 for (size_t i = 0; i <= PREF_STORE_TYPE_MAX; ++i) { |
| 51 PrefStore::PrefReadError recommended_pref_error = | 53 if (pref_stores_[i].get()) { |
| 52 PrefStore::PREF_READ_ERROR_NONE; | 54 PrefStore::PrefReadError this_error = pref_stores_[i]->ReadPrefs(); |
| 53 | 55 if (result == PrefStore::PREF_READ_ERROR_NONE) |
| 54 // Read managed preferences. | 56 result = this_error; |
| 55 if (managed_prefs_.get()) { | 57 } |
| 56 managed_pref_error = managed_prefs_->ReadPrefs(); | |
| 57 } | 58 } |
| 58 | 59 // TODO(markusheintz): Return a better error status: maybe a struct with |
| 59 // Read preferences set by the user. | 60 // the error status of all PrefStores. |
| 60 if (user_prefs_.get()) { | 61 return result; |
| 61 user_pref_error = user_prefs_->ReadPrefs(); | |
| 62 } | |
| 63 | |
| 64 // Read recommended preferences. | |
| 65 if (recommended_prefs_.get()) { | |
| 66 recommended_pref_error = recommended_prefs_->ReadPrefs(); | |
| 67 } | |
| 68 | |
| 69 // TODO(markusheintz): Return a better error status maybe a struct with | |
| 70 // the error status of all PrefStores. | |
| 71 | |
| 72 // Return the first pref store error that occured. | |
| 73 if (managed_pref_error != PrefStore::PREF_READ_ERROR_NONE) { | |
| 74 return managed_pref_error; | |
| 75 } | |
| 76 if (user_pref_error != PrefStore::PREF_READ_ERROR_NONE) { | |
| 77 return user_pref_error; | |
| 78 } | |
| 79 return recommended_pref_error; | |
| 80 } | 62 } |
| 81 | 63 |
| 82 bool PrefValueStore::HasPrefPath(const wchar_t* path) const { | 64 bool PrefValueStore::HasPrefPath(const wchar_t* path) const { |
| 83 Value* tmp_value = NULL; | 65 Value* tmp_value = NULL; |
| 84 const std::wstring name(path); | 66 const std::wstring name(path); |
| 85 bool rv = GetValue(name, &tmp_value); | 67 bool rv = GetValue(name, &tmp_value); |
| 86 return rv; | 68 return rv; |
| 87 } | 69 } |
| 88 | 70 |
| 89 // The value of a Preference is managed if the PrefStore for managed | 71 // The value of a Preference is managed if the PrefStore for managed |
| 90 // preferences contains a value for the given preference |name|. | 72 // preferences contains a value for the given preference |name|. |
| 91 bool PrefValueStore::PrefValueIsManaged(const wchar_t* name) { | 73 bool PrefValueStore::PrefValueIsManaged(const wchar_t* name) { |
| 92 if (managed_prefs_.get() == NULL) { | 74 if (pref_stores_[MANAGED].get() == NULL) { |
| 93 // No managed PreferenceStore set, hence there are no managed | 75 // No managed PreferenceStore set, hence there are no managed |
| 94 // preferences. | 76 // preferences. |
| 95 return false; | 77 return false; |
| 96 } | 78 } |
| 97 Value* tmp_value; | 79 Value* tmp_value; |
| 98 return managed_prefs_->prefs()->Get(name, &tmp_value); | 80 return pref_stores_[MANAGED]->prefs()->Get(name, &tmp_value); |
| 99 } | 81 } |
| 100 | 82 |
| 101 // Note the |DictionaryValue| referenced by the |PrefStore| user_prefs_ | 83 // Note the |DictionaryValue| referenced by the |PrefStore| user_prefs_ |
| 102 // (returned by the method Prefs()) takes the ownership of the Value referenced | 84 // (returned by the method prefs()) takes the ownership of the Value referenced |
| 103 // by in_value. | 85 // by in_value. |
| 104 void PrefValueStore::SetUserPrefValue(const wchar_t* name, Value* in_value) { | 86 void PrefValueStore::SetUserPrefValue(const wchar_t* name, Value* in_value) { |
| 105 user_prefs_->prefs()->Set(name, in_value); | 87 pref_stores_[USER]->prefs()->Set(name, in_value); |
| 106 } | 88 } |
| 107 | 89 |
| 108 bool PrefValueStore::ReadOnly() { | 90 bool PrefValueStore::ReadOnly() { |
| 109 return user_prefs_->ReadOnly(); | 91 return pref_stores_[USER]->ReadOnly(); |
| 110 } | 92 } |
| 111 | 93 |
| 112 void PrefValueStore::RemoveUserPrefValue(const wchar_t* name) { | 94 void PrefValueStore::RemoveUserPrefValue(const wchar_t* name) { |
| 113 if (user_prefs_.get()) { | 95 if (pref_stores_[USER].get()) { |
| 114 user_prefs_->prefs()->Remove(name, NULL); | 96 pref_stores_[USER]->prefs()->Remove(name, NULL); |
| 115 } | 97 } |
| 116 } | 98 } |
| OLD | NEW |