Chromium Code Reviews| 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/prefs/pref_value_store.h" | 5 #include "chrome/browser/prefs/pref_value_store.h" |
| 6 | 6 |
| 7 #include "base/values.h" | 7 #include "base/values.h" |
| 8 #include "chrome/browser/chrome_thread.h" | 8 #include "chrome/browser/chrome_thread.h" |
| 9 #include "chrome/browser/extensions/extension_pref_store.h" | 9 #include "chrome/browser/extensions/extension_pref_store.h" |
| 10 #include "chrome/browser/policy/configuration_policy_pref_store.h" | 10 #include "chrome/browser/policy/configuration_policy_pref_store.h" |
| 11 #include "chrome/browser/prefs/command_line_pref_store.h" | 11 #include "chrome/browser/prefs/command_line_pref_store.h" |
| 12 #include "chrome/browser/prefs/default_pref_store.h" | |
| 12 #include "chrome/common/json_pref_store.h" | 13 #include "chrome/common/json_pref_store.h" |
| 13 #include "chrome/common/notification_service.h" | 14 #include "chrome/common/notification_service.h" |
| 14 | 15 |
| 15 // static | 16 // static |
| 16 PrefValueStore* PrefValueStore::CreatePrefValueStore( | 17 PrefValueStore* PrefValueStore::CreatePrefValueStore( |
| 17 const FilePath& pref_filename, | 18 const FilePath& pref_filename, |
| 18 Profile* profile, | 19 Profile* profile, |
| 19 bool user_only) { | 20 bool user_only) { |
| 20 using policy::ConfigurationPolicyPrefStore; | 21 using policy::ConfigurationPolicyPrefStore; |
| 21 ConfigurationPolicyPrefStore* managed = NULL; | 22 ConfigurationPolicyPrefStore* managed = NULL; |
| 22 ExtensionPrefStore* extension = NULL; | 23 ExtensionPrefStore* extension = NULL; |
| 23 CommandLinePrefStore* command_line = NULL; | 24 CommandLinePrefStore* command_line = NULL; |
| 24 JsonPrefStore* user = NULL; | |
| 25 ConfigurationPolicyPrefStore* recommended = NULL; | 25 ConfigurationPolicyPrefStore* recommended = NULL; |
| 26 | 26 |
| 27 user = new JsonPrefStore( | 27 JsonPrefStore* user = new JsonPrefStore( |
| 28 pref_filename, | 28 pref_filename, |
| 29 ChromeThread::GetMessageLoopProxyForThread(ChromeThread::FILE)); | 29 ChromeThread::GetMessageLoopProxyForThread(ChromeThread::FILE)); |
| 30 DefaultPrefStore* default_store = new DefaultPrefStore(); | |
| 30 | 31 |
| 31 if (!user_only) { | 32 if (!user_only) { |
| 32 managed = ConfigurationPolicyPrefStore::CreateManagedPolicyPrefStore(); | 33 managed = ConfigurationPolicyPrefStore::CreateManagedPolicyPrefStore(); |
| 33 extension = new ExtensionPrefStore(profile, PrefNotifier::EXTENSION_STORE); | 34 extension = new ExtensionPrefStore(profile, PrefNotifier::EXTENSION_STORE); |
| 34 command_line = new CommandLinePrefStore(CommandLine::ForCurrentProcess()); | 35 command_line = new CommandLinePrefStore(CommandLine::ForCurrentProcess()); |
| 35 recommended = | 36 recommended = |
| 36 ConfigurationPolicyPrefStore::CreateRecommendedPolicyPrefStore(); | 37 ConfigurationPolicyPrefStore::CreateRecommendedPolicyPrefStore(); |
| 37 } | 38 } |
| 38 return new PrefValueStore(managed, extension, command_line, user, | 39 return new PrefValueStore(managed, extension, command_line, user, |
| 39 recommended); | 40 recommended, default_store); |
| 40 } | 41 } |
| 41 | 42 |
| 42 PrefValueStore::~PrefValueStore() {} | 43 PrefValueStore::~PrefValueStore() {} |
| 43 | 44 |
| 44 bool PrefValueStore::GetValue(const std::string& name, | 45 bool PrefValueStore::GetValue(const std::string& name, |
| 45 Value** out_value) const { | 46 Value** out_value) const { |
| 46 // Check the |PrefStore|s in order of their priority from highest to lowest | 47 // Check the |PrefStore|s in order of their priority from highest to lowest |
| 47 // to find the value of the preference described by the given preference name. | 48 // to find the value of the preference described by the given preference name. |
| 48 for (size_t i = 0; i <= PrefNotifier::PREF_STORE_TYPE_MAX; ++i) { | 49 for (size_t i = 0; i <= PrefNotifier::PREF_STORE_TYPE_MAX; ++i) { |
| 49 if (pref_stores_[i].get() && | 50 if (pref_stores_[i].get() && |
| 50 pref_stores_[i]->prefs()->Get(name.c_str(), out_value)) { | 51 pref_stores_[i]->prefs()->Get(name.c_str(), out_value)) { |
| 51 return true; | 52 return true; |
| 52 } | 53 } |
| 53 } | 54 } |
| 54 // No value found for the given preference name, set the return false. | 55 // No value found for the given preference name: set the return false. |
| 55 *out_value = NULL; | 56 *out_value = NULL; |
| 56 return false; | 57 return false; |
| 57 } | 58 } |
| 58 | 59 |
| 59 bool PrefValueStore::WritePrefs() { | 60 bool PrefValueStore::WritePrefs() { |
| 60 bool success = true; | 61 bool success = true; |
| 61 for (size_t i = 0; i <= PrefNotifier::PREF_STORE_TYPE_MAX; ++i) { | 62 for (size_t i = 0; i <= PrefNotifier::PREF_STORE_TYPE_MAX; ++i) { |
| 62 if (pref_stores_[i].get()) | 63 if (pref_stores_[i].get()) |
| 63 success = pref_stores_[i]->WritePrefs() && success; | 64 success = pref_stores_[i]->WritePrefs() && success; |
| 64 } | 65 } |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 83 } | 84 } |
| 84 // TODO(markusheintz): Return a better error status: maybe a struct with | 85 // TODO(markusheintz): Return a better error status: maybe a struct with |
| 85 // the error status of all PrefStores. | 86 // the error status of all PrefStores. |
| 86 return result; | 87 return result; |
| 87 } | 88 } |
| 88 | 89 |
| 89 bool PrefValueStore::HasPrefPath(const char* path) const { | 90 bool PrefValueStore::HasPrefPath(const char* path) const { |
| 90 Value* tmp_value = NULL; | 91 Value* tmp_value = NULL; |
| 91 const std::string name(path); | 92 const std::string name(path); |
| 92 bool rv = GetValue(name, &tmp_value); | 93 bool rv = GetValue(name, &tmp_value); |
| 93 return rv; | 94 // Merely registering a pref doesn't count as "having" it: we require a |
| 95 // non-default value set. | |
| 96 return rv && !PrefValueFromDefaultStore(path); | |
|
jeanluc1
2010/09/21 04:16:39
This change modifies the behavior of TemplateURLMo
| |
| 94 } | 97 } |
| 95 | 98 |
| 96 bool PrefValueStore::PrefHasChanged(const char* path, | 99 bool PrefValueStore::PrefHasChanged(const char* path, |
| 97 PrefNotifier::PrefStoreType new_store, | 100 PrefNotifier::PrefStoreType new_store) { |
| 98 const Value* old_value) { | |
| 99 DCHECK(new_store != PrefNotifier::INVALID_STORE); | 101 DCHECK(new_store != PrefNotifier::INVALID_STORE); |
| 100 // Replying that the pref has changed may cause extra work, but it should | 102 // Replying that the pref has changed may cause problems, but it's the safer |
| 101 // not be actively harmful. | 103 // choice. |
| 102 if (new_store == PrefNotifier::INVALID_STORE) | 104 if (new_store == PrefNotifier::INVALID_STORE) |
| 103 return true; | 105 return true; |
| 104 | 106 |
| 105 Value* new_value = NULL; | 107 PrefNotifier::PrefStoreType controller = ControllingPrefStoreForPref(path); |
| 106 GetValue(path, &new_value); | 108 DCHECK(controller != PrefNotifier::INVALID_STORE); |
| 107 // Some unit tests have no values for certain prefs. | 109 if (controller == PrefNotifier::INVALID_STORE) |
| 108 if (!new_value || !old_value->Equals(new_value)) | |
| 109 return true; | 110 return true; |
| 110 | 111 |
| 111 // If there's a value in a store with lower priority than the |new_store|, | 112 // If the pref is controlled by a higher-priority store, its effective value |
| 112 // and no value in a store with higher priority, assume the |new_store| just | 113 // cannot have changed. |
| 113 // took control of the pref. (This assumption is wrong if the new value | 114 if (controller < new_store) |
| 114 // and store are both the same as the old one, but that situation should be | 115 return false; |
| 115 // rare, and reporting a change when none happened should not be harmful.) | |
| 116 if (PrefValueInStoreRange(path, new_store, false) && | |
| 117 !PrefValueInStoreRange(path, new_store, true)) | |
| 118 return true; | |
| 119 | 116 |
| 120 return false; | 117 // Otherwise, we take the pref store's word that something changed. |
| 118 return true; | |
| 121 } | 119 } |
| 122 | 120 |
| 123 // Note the |DictionaryValue| referenced by the |PrefStore| user_prefs_ | 121 // Note the |DictionaryValue| referenced by the |PrefStore| USER_STORE |
| 124 // (returned by the method prefs()) takes the ownership of the Value referenced | 122 // (returned by the method prefs()) takes the ownership of the Value referenced |
| 125 // by in_value. | 123 // by in_value. |
| 126 void PrefValueStore::SetUserPrefValue(const char* name, Value* in_value) { | 124 bool PrefValueStore::SetUserPrefValue(const char* name, Value* in_value) { |
| 125 Value* old_value = NULL; | |
| 126 pref_stores_[PrefNotifier::USER_STORE]->prefs()->Get(name, &old_value); | |
| 127 bool value_changed = !(old_value && old_value->Equals(in_value)); | |
| 128 | |
| 127 pref_stores_[PrefNotifier::USER_STORE]->prefs()->Set(name, in_value); | 129 pref_stores_[PrefNotifier::USER_STORE]->prefs()->Set(name, in_value); |
| 130 return value_changed; | |
| 131 } | |
| 132 | |
| 133 // Note the |DictionaryValue| referenced by the |PrefStore| DEFAULT_STORE | |
| 134 // (returned by the method prefs()) takes the ownership of the Value referenced | |
| 135 // by in_value. | |
| 136 void PrefValueStore::SetDefaultPrefValue(const char* name, Value* in_value) { | |
| 137 pref_stores_[PrefNotifier::DEFAULT_STORE]->prefs()->Set(name, in_value); | |
| 128 } | 138 } |
| 129 | 139 |
| 130 bool PrefValueStore::ReadOnly() { | 140 bool PrefValueStore::ReadOnly() { |
| 131 return pref_stores_[PrefNotifier::USER_STORE]->ReadOnly(); | 141 return pref_stores_[PrefNotifier::USER_STORE]->ReadOnly(); |
| 132 } | 142 } |
| 133 | 143 |
| 134 void PrefValueStore::RemoveUserPrefValue(const char* name) { | 144 bool PrefValueStore::RemoveUserPrefValue(const char* name) { |
| 135 if (pref_stores_[PrefNotifier::USER_STORE].get()) { | 145 if (pref_stores_[PrefNotifier::USER_STORE].get()) { |
| 136 pref_stores_[PrefNotifier::USER_STORE]->prefs()->Remove(name, NULL); | 146 return pref_stores_[PrefNotifier::USER_STORE]->prefs()->Remove(name, NULL); |
| 137 } | 147 } |
| 148 return false; | |
| 138 } | 149 } |
| 139 | 150 |
| 140 bool PrefValueStore::PrefValueInManagedStore(const char* name) { | 151 bool PrefValueStore::PrefValueInManagedStore(const char* name) const { |
| 141 return PrefValueInStore(name, PrefNotifier::MANAGED_STORE); | 152 return PrefValueInStore(name, PrefNotifier::MANAGED_STORE); |
| 142 } | 153 } |
| 143 | 154 |
| 144 bool PrefValueStore::PrefValueInExtensionStore(const char* name) { | 155 bool PrefValueStore::PrefValueInExtensionStore(const char* name) const { |
| 145 return PrefValueInStore(name, PrefNotifier::EXTENSION_STORE); | 156 return PrefValueInStore(name, PrefNotifier::EXTENSION_STORE); |
| 146 } | 157 } |
| 147 | 158 |
| 148 bool PrefValueStore::PrefValueInUserStore(const char* name) { | 159 bool PrefValueStore::PrefValueInUserStore(const char* name) const { |
| 149 return PrefValueInStore(name, PrefNotifier::USER_STORE); | 160 return PrefValueInStore(name, PrefNotifier::USER_STORE); |
| 150 } | 161 } |
| 151 | 162 |
| 152 bool PrefValueStore::PrefValueFromExtensionStore(const char* name) { | 163 bool PrefValueStore::PrefValueFromExtensionStore(const char* name) const { |
| 153 return ControllingPrefStoreForPref(name) == PrefNotifier::EXTENSION_STORE; | 164 return ControllingPrefStoreForPref(name) == PrefNotifier::EXTENSION_STORE; |
| 154 } | 165 } |
| 155 | 166 |
| 156 bool PrefValueStore::PrefValueFromUserStore(const char* name) { | 167 bool PrefValueStore::PrefValueFromUserStore(const char* name) const { |
| 157 return ControllingPrefStoreForPref(name) == PrefNotifier::USER_STORE; | 168 return ControllingPrefStoreForPref(name) == PrefNotifier::USER_STORE; |
| 158 } | 169 } |
| 159 | 170 |
| 160 bool PrefValueStore::PrefValueUserModifiable(const char* name) { | 171 bool PrefValueStore::PrefValueFromDefaultStore(const char* name) const { |
| 172 return ControllingPrefStoreForPref(name) == PrefNotifier::DEFAULT_STORE; | |
| 173 } | |
| 174 | |
| 175 bool PrefValueStore::PrefValueUserModifiable(const char* name) const { | |
| 161 PrefNotifier::PrefStoreType effective_store = | 176 PrefNotifier::PrefStoreType effective_store = |
| 162 ControllingPrefStoreForPref(name); | 177 ControllingPrefStoreForPref(name); |
| 163 return effective_store >= PrefNotifier::USER_STORE || | 178 return effective_store >= PrefNotifier::USER_STORE || |
| 164 effective_store == PrefNotifier::INVALID_STORE; | 179 effective_store == PrefNotifier::INVALID_STORE; |
| 165 } | 180 } |
| 166 | 181 |
| 182 PrefNotifier::PrefStoreType PrefValueStore::ControllingPrefStoreForPref( | |
| 183 const char* name) const { | |
| 184 for (int i = 0; i <= PrefNotifier::PREF_STORE_TYPE_MAX; ++i) { | |
| 185 if (PrefValueInStore(name, static_cast<PrefNotifier::PrefStoreType>(i))) | |
| 186 return static_cast<PrefNotifier::PrefStoreType>(i); | |
| 187 } | |
| 188 return PrefNotifier::INVALID_STORE; | |
| 189 } | |
| 190 | |
| 167 bool PrefValueStore::PrefValueInStore(const char* name, | 191 bool PrefValueStore::PrefValueInStore(const char* name, |
| 168 PrefNotifier::PrefStoreType type) { | 192 PrefNotifier::PrefStoreType type) const { |
| 169 if (!pref_stores_[type].get()) { | 193 if (!pref_stores_[type].get()) { |
| 170 // No store of that type set, so this pref can't be in it. | 194 // No store of that type set, so this pref can't be in it. |
| 171 return false; | 195 return false; |
| 172 } | 196 } |
| 173 Value* tmp_value; | 197 Value* tmp_value; |
| 174 return pref_stores_[type]->prefs()->Get(name, &tmp_value); | 198 return pref_stores_[type]->prefs()->Get(name, &tmp_value); |
| 175 } | 199 } |
| 176 | 200 |
| 177 bool PrefValueStore::PrefValueInStoreRange(const char* name, | |
| 178 PrefNotifier::PrefStoreType boundary, | |
| 179 bool higher_priority) { | |
| 180 // Higher priorities are lower PrefStoreType values. The range is | |
| 181 // non-inclusive of the boundary. | |
| 182 int start = higher_priority ? 0 : boundary + 1; | |
| 183 int end = higher_priority ? boundary - 1 : | |
| 184 PrefNotifier::PREF_STORE_TYPE_MAX; | |
| 185 | |
| 186 for (int i = start; i <= end ; ++i) { | |
| 187 if (PrefValueInStore(name, static_cast<PrefNotifier::PrefStoreType>(i))) | |
| 188 return true; | |
| 189 } | |
| 190 return false; | |
| 191 } | |
| 192 | |
| 193 | |
| 194 PrefNotifier::PrefStoreType PrefValueStore::ControllingPrefStoreForPref( | |
| 195 const char* name) { | |
| 196 for (int i = 0; i <= PrefNotifier::PREF_STORE_TYPE_MAX; ++i) { | |
| 197 if (PrefValueInStore(name, static_cast<PrefNotifier::PrefStoreType>(i))) | |
| 198 return static_cast<PrefNotifier::PrefStoreType>(i); | |
| 199 } | |
| 200 return PrefNotifier::INVALID_STORE; | |
| 201 } | |
| 202 | |
| 203 void PrefValueStore::RefreshPolicyPrefsCompletion( | 201 void PrefValueStore::RefreshPolicyPrefsCompletion( |
| 204 PrefStore* new_managed_pref_store, | 202 PrefStore* new_managed_pref_store, |
| 205 PrefStore* new_recommended_pref_store, | 203 PrefStore* new_recommended_pref_store, |
| 206 AfterRefreshCallback* callback_pointer) { | 204 AfterRefreshCallback* callback_pointer) { |
| 207 scoped_ptr<AfterRefreshCallback> callback(callback_pointer); | 205 scoped_ptr<AfterRefreshCallback> callback(callback_pointer); |
| 208 DictionaryValue* managed_prefs_before( | 206 DictionaryValue* managed_prefs_before( |
| 209 pref_stores_[PrefNotifier::MANAGED_STORE]->prefs()); | 207 pref_stores_[PrefNotifier::MANAGED_STORE]->prefs()); |
| 210 DictionaryValue* managed_prefs_after(new_managed_pref_store->prefs()); | 208 DictionaryValue* managed_prefs_after(new_managed_pref_store->prefs()); |
| 211 DictionaryValue* recommended_prefs_before( | 209 DictionaryValue* recommended_prefs_before( |
| 212 pref_stores_[PrefNotifier::RECOMMENDED_STORE]->prefs()); | 210 pref_stores_[PrefNotifier::RECOMMENDED_STORE]->prefs()); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 282 current_thread_id, | 280 current_thread_id, |
| 283 new_managed_pref_store, | 281 new_managed_pref_store, |
| 284 new_recommended_pref_store, | 282 new_recommended_pref_store, |
| 285 callback)); | 283 callback)); |
| 286 } | 284 } |
| 287 | 285 |
| 288 PrefValueStore::PrefValueStore(PrefStore* managed_prefs, | 286 PrefValueStore::PrefValueStore(PrefStore* managed_prefs, |
| 289 PrefStore* extension_prefs, | 287 PrefStore* extension_prefs, |
| 290 PrefStore* command_line_prefs, | 288 PrefStore* command_line_prefs, |
| 291 PrefStore* user_prefs, | 289 PrefStore* user_prefs, |
| 292 PrefStore* recommended_prefs) { | 290 PrefStore* recommended_prefs, |
| 291 PrefStore* default_prefs) { | |
| 292 // NULL default pref store is usually bad, but may be OK for some unit tests. | |
| 293 if (!default_prefs) | |
| 294 LOG(WARNING) << "default pref store is null"; | |
| 293 pref_stores_[PrefNotifier::MANAGED_STORE].reset(managed_prefs); | 295 pref_stores_[PrefNotifier::MANAGED_STORE].reset(managed_prefs); |
| 294 pref_stores_[PrefNotifier::EXTENSION_STORE].reset(extension_prefs); | 296 pref_stores_[PrefNotifier::EXTENSION_STORE].reset(extension_prefs); |
| 295 pref_stores_[PrefNotifier::COMMAND_LINE_STORE].reset(command_line_prefs); | 297 pref_stores_[PrefNotifier::COMMAND_LINE_STORE].reset(command_line_prefs); |
| 296 pref_stores_[PrefNotifier::USER_STORE].reset(user_prefs); | 298 pref_stores_[PrefNotifier::USER_STORE].reset(user_prefs); |
| 297 pref_stores_[PrefNotifier::RECOMMENDED_STORE].reset(recommended_prefs); | 299 pref_stores_[PrefNotifier::RECOMMENDED_STORE].reset(recommended_prefs); |
| 300 pref_stores_[PrefNotifier::DEFAULT_STORE].reset(default_prefs); | |
| 298 } | 301 } |
| OLD | NEW |