Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 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_value_store.h" | 5 #include "chrome/browser/prefs/pref_value_store.h" |
| 6 | 6 |
| 7 #include "chrome/browser/prefs/pref_notifier.h" | 7 #include "chrome/browser/prefs/pref_notifier.h" |
| 8 | 8 |
| 9 PrefValueStore::PrefStoreKeeper::PrefStoreKeeper() | 9 PrefValueStore::PrefStoreKeeper::PrefStoreKeeper() |
| 10 : pref_value_store_(NULL), | 10 : pref_value_store_(NULL), |
| 11 type_(PrefValueStore::INVALID_STORE) { | 11 type_(PrefValueStore::INVALID_STORE) { |
| 12 } | 12 } |
| 13 | 13 |
| 14 PrefValueStore::PrefStoreKeeper::~PrefStoreKeeper() { | 14 PrefValueStore::PrefStoreKeeper::~PrefStoreKeeper() { |
| 15 if (pref_store_.get()) | 15 if (pref_store_.get()) |
| 16 pref_store_->RemoveObserver(this); | 16 pref_store_->RemoveObserver(this); |
| 17 } | 17 } |
| 18 | 18 |
| 19 void PrefValueStore::PrefStoreKeeper::Initialize( | 19 void PrefValueStore::PrefStoreKeeper::Initialize( |
| 20 PrefValueStore* store, | 20 PrefValueStore* store, |
| 21 PrefStore* pref_store, | 21 PrefStore* pref_store, |
| 22 PrefValueStore::PrefStoreType type) { | 22 PrefValueStore::PrefStoreType type) { |
| 23 if (pref_store_.get()) | 23 if (pref_store_.get()) |
| 24 pref_store_->RemoveObserver(this); | 24 pref_store_->RemoveObserver(this); |
| 25 type_ = type; | 25 type_ = type; |
| 26 pref_value_store_ = store; | 26 pref_value_store_ = store; |
| 27 pref_store_.reset(pref_store); | 27 pref_store_ = pref_store; |
| 28 if (pref_store_.get()) | 28 if (pref_store_.get()) |
| 29 pref_store_->AddObserver(this); | 29 pref_store_->AddObserver(this); |
| 30 } | 30 } |
| 31 | 31 |
| 32 void PrefValueStore::PrefStoreKeeper::OnPrefValueChanged( | 32 void PrefValueStore::PrefStoreKeeper::OnPrefValueChanged( |
| 33 const std::string& key) { | 33 const std::string& key) { |
| 34 pref_value_store_->OnPrefValueChanged(type_, key); | 34 pref_value_store_->OnPrefValueChanged(type_, key); |
| 35 } | 35 } |
| 36 | 36 |
| 37 void PrefValueStore::PrefStoreKeeper::OnInitializationCompleted() { | 37 void PrefValueStore::PrefStoreKeeper::OnInitializationCompleted() { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 53 InitPrefStore(COMMAND_LINE_STORE, command_line_prefs); | 53 InitPrefStore(COMMAND_LINE_STORE, command_line_prefs); |
| 54 InitPrefStore(USER_STORE, user_prefs); | 54 InitPrefStore(USER_STORE, user_prefs); |
| 55 InitPrefStore(RECOMMENDED_STORE, recommended_prefs); | 55 InitPrefStore(RECOMMENDED_STORE, recommended_prefs); |
| 56 InitPrefStore(DEFAULT_STORE, default_prefs); | 56 InitPrefStore(DEFAULT_STORE, default_prefs); |
| 57 | 57 |
| 58 CheckInitializationCompleted(); | 58 CheckInitializationCompleted(); |
| 59 } | 59 } |
| 60 | 60 |
| 61 PrefValueStore::~PrefValueStore() {} | 61 PrefValueStore::~PrefValueStore() {} |
| 62 | 62 |
| 63 PrefValueStore* PrefValueStore::CloneAndSpecialize( | |
| 64 PrefStore* managed_platform_prefs, | |
| 65 PrefStore* device_management_prefs, | |
| 66 PrefStore* extension_prefs, | |
| 67 PrefStore* command_line_prefs, | |
| 68 PrefStore* user_prefs, | |
| 69 PrefStore* recommended_prefs, | |
| 70 PrefStore* default_prefs, | |
| 71 PrefNotifier* pref_notifier) { | |
| 72 DCHECK(pref_notifier); | |
| 73 if (!managed_platform_prefs) | |
| 74 managed_platform_prefs = GetPrefStore(MANAGED_PLATFORM_STORE); | |
| 75 if (!device_management_prefs) | |
| 76 device_management_prefs = GetPrefStore(DEVICE_MANAGEMENT_STORE); | |
| 77 if (!extension_prefs) extension_prefs = GetPrefStore(EXTENSION_STORE); | |
| 78 if (!command_line_prefs) | |
| 79 command_line_prefs = GetPrefStore(COMMAND_LINE_STORE); | |
| 80 if (!user_prefs) user_prefs = GetPrefStore(USER_STORE); | |
| 81 if (!recommended_prefs) recommended_prefs = GetPrefStore(RECOMMENDED_STORE); | |
|
Mattias Nissler (ping if slow)
2011/01/21 08:53:08
The style guide says "Short conditional statements
battre
2011/01/24 17:47:18
Done.
| |
| 82 if (!default_prefs) default_prefs = GetPrefStore(DEFAULT_STORE); | |
| 83 | |
| 84 return new PrefValueStore( | |
| 85 managed_platform_prefs, device_management_prefs, extension_prefs, | |
| 86 command_line_prefs, user_prefs, recommended_prefs, default_prefs, | |
| 87 pref_notifier); | |
| 88 } | |
| 89 | |
| 63 bool PrefValueStore::GetValue(const std::string& name, | 90 bool PrefValueStore::GetValue(const std::string& name, |
| 91 Value::ValueType type, | |
| 64 Value** out_value) const { | 92 Value** out_value) const { |
| 93 *out_value = NULL; | |
| 65 // Check the |PrefStore|s in order of their priority from highest to lowest | 94 // Check the |PrefStore|s in order of their priority from highest to lowest |
| 66 // to find the value of the preference described by the given preference name. | 95 // to find the value of the preference described by the given preference name. |
| 67 for (size_t i = 0; i <= PREF_STORE_TYPE_MAX; ++i) { | 96 for (size_t i = 0; i <= PREF_STORE_TYPE_MAX; ++i) { |
| 68 if (GetValueFromStore(name.c_str(), static_cast<PrefStoreType>(i), | 97 if (GetValueFromStore(name.c_str(), static_cast<PrefStoreType>(i), |
| 69 out_value)) | 98 out_value)) { |
| 99 if (!(*out_value)->IsType(type)) { | |
| 100 LOG(WARNING) << "Expected type for " << name << " is " << type | |
| 101 << " but got " << (*out_value)->GetType() | |
| 102 << " in store " << i; | |
| 103 continue; | |
| 104 } | |
| 70 return true; | 105 return true; |
| 106 } | |
| 71 } | 107 } |
| 72 return false; | 108 return false; |
| 73 } | 109 } |
| 74 | 110 |
| 75 void PrefValueStore::RegisterPreferenceType(const std::string& name, | |
| 76 Value::ValueType type) { | |
| 77 pref_types_[name] = type; | |
| 78 } | |
| 79 | |
| 80 Value::ValueType PrefValueStore::GetRegisteredType( | |
| 81 const std::string& name) const { | |
| 82 PrefTypeMap::const_iterator found = pref_types_.find(name); | |
| 83 if (found == pref_types_.end()) | |
| 84 return Value::TYPE_NULL; | |
| 85 return found->second; | |
| 86 } | |
| 87 | |
| 88 bool PrefValueStore::HasPrefPath(const char* path) const { | |
| 89 Value* tmp_value = NULL; | |
| 90 const std::string name(path); | |
| 91 bool rv = GetValue(name, &tmp_value); | |
| 92 // Merely registering a pref doesn't count as "having" it: we require a | |
| 93 // non-default value set. | |
| 94 return rv && !PrefValueFromDefaultStore(path); | |
| 95 } | |
| 96 | |
| 97 void PrefValueStore::NotifyPrefChanged( | 111 void PrefValueStore::NotifyPrefChanged( |
| 98 const char* path, | 112 const char* path, |
| 99 PrefValueStore::PrefStoreType new_store) { | 113 PrefValueStore::PrefStoreType new_store) { |
| 100 DCHECK(new_store != INVALID_STORE); | 114 DCHECK(new_store != INVALID_STORE); |
| 101 | 115 |
| 102 // If this pref is not registered, just discard the notification. | |
| 103 if (!pref_types_.count(path)) | |
| 104 return; | |
| 105 | |
| 106 bool changed = true; | 116 bool changed = true; |
| 107 // Replying that the pref has changed in case the new store is invalid may | 117 // Replying that the pref has changed in case the new store is invalid may |
| 108 // cause problems, but it's the safer choice. | 118 // cause problems, but it's the safer choice. |
| 109 if (new_store != INVALID_STORE) { | 119 if (new_store != INVALID_STORE) { |
| 110 PrefStoreType controller = ControllingPrefStoreForPref(path); | 120 PrefStoreType controller = ControllingPrefStoreForPref(path); |
| 111 DCHECK(controller != INVALID_STORE); | 121 DCHECK(controller != INVALID_STORE); |
| 112 // If the pref is controlled by a higher-priority store, its effective value | 122 // If the pref is controlled by a higher-priority store, its effective value |
| 113 // cannot have changed. | 123 // cannot have changed. |
| 114 if (controller != INVALID_STORE && | 124 if (controller != INVALID_STORE && |
| 115 controller < new_store) { | 125 controller < new_store) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 148 bool PrefValueStore::PrefValueFromDefaultStore(const char* name) const { | 158 bool PrefValueStore::PrefValueFromDefaultStore(const char* name) const { |
| 149 return ControllingPrefStoreForPref(name) == DEFAULT_STORE; | 159 return ControllingPrefStoreForPref(name) == DEFAULT_STORE; |
| 150 } | 160 } |
| 151 | 161 |
| 152 bool PrefValueStore::PrefValueUserModifiable(const char* name) const { | 162 bool PrefValueStore::PrefValueUserModifiable(const char* name) const { |
| 153 PrefStoreType effective_store = ControllingPrefStoreForPref(name); | 163 PrefStoreType effective_store = ControllingPrefStoreForPref(name); |
| 154 return effective_store >= USER_STORE || | 164 return effective_store >= USER_STORE || |
| 155 effective_store == INVALID_STORE; | 165 effective_store == INVALID_STORE; |
| 156 } | 166 } |
| 157 | 167 |
| 158 // Returns true if the actual value is a valid type for the expected type when | |
| 159 // found in the given store. | |
| 160 bool PrefValueStore::IsValidType(Value::ValueType expected, | |
| 161 Value::ValueType actual, | |
| 162 PrefValueStore::PrefStoreType store) { | |
| 163 if (expected == actual) | |
| 164 return true; | |
| 165 | |
| 166 // Dictionaries and lists are allowed to hold TYPE_NULL values too, but only | |
| 167 // in the default pref store. | |
| 168 if (store == DEFAULT_STORE && | |
| 169 actual == Value::TYPE_NULL && | |
| 170 (expected == Value::TYPE_DICTIONARY || expected == Value::TYPE_LIST)) { | |
| 171 return true; | |
| 172 } | |
| 173 return false; | |
| 174 } | |
| 175 | |
| 176 bool PrefValueStore::PrefValueInStore( | 168 bool PrefValueStore::PrefValueInStore( |
| 177 const char* name, | 169 const char* name, |
| 178 PrefValueStore::PrefStoreType store) const { | 170 PrefValueStore::PrefStoreType store) const { |
| 179 // Declare a temp Value* and call GetValueFromStore, | 171 // Declare a temp Value* and call GetValueFromStore, |
| 180 // ignoring the output value. | 172 // ignoring the output value. |
| 181 Value* tmp_value = NULL; | 173 Value* tmp_value = NULL; |
| 182 return GetValueFromStore(name, store, &tmp_value); | 174 return GetValueFromStore(name, store, &tmp_value); |
| 183 } | 175 } |
| 184 | 176 |
| 185 bool PrefValueStore::PrefValueInStoreRange( | 177 bool PrefValueStore::PrefValueInStoreRange( |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 217 if (store) { | 209 if (store) { |
| 218 switch (store->GetValue(name, out_value)) { | 210 switch (store->GetValue(name, out_value)) { |
| 219 case PrefStore::READ_USE_DEFAULT: | 211 case PrefStore::READ_USE_DEFAULT: |
| 220 store = GetPrefStore(DEFAULT_STORE); | 212 store = GetPrefStore(DEFAULT_STORE); |
| 221 if (!store || store->GetValue(name, out_value) != PrefStore::READ_OK) { | 213 if (!store || store->GetValue(name, out_value) != PrefStore::READ_OK) { |
| 222 *out_value = NULL; | 214 *out_value = NULL; |
| 223 return false; | 215 return false; |
| 224 } | 216 } |
| 225 // Fall through... | 217 // Fall through... |
| 226 case PrefStore::READ_OK: | 218 case PrefStore::READ_OK: |
| 227 if (IsValidType(GetRegisteredType(name), | 219 return true; |
| 228 (*out_value)->GetType(), | |
| 229 store_type)) { | |
| 230 return true; | |
| 231 } | |
| 232 break; | |
| 233 case PrefStore::READ_NO_VALUE: | 220 case PrefStore::READ_NO_VALUE: |
| 234 break; | 221 break; |
| 235 } | 222 } |
| 236 } | 223 } |
| 237 | 224 |
| 238 // No valid value found for the given preference name: set the return false. | 225 // No valid value found for the given preference name: set the return false. |
| 239 *out_value = NULL; | 226 *out_value = NULL; |
| 240 return false; | 227 return false; |
| 241 } | 228 } |
| 242 | 229 |
| 243 void PrefValueStore::OnPrefValueChanged(PrefValueStore::PrefStoreType type, | 230 void PrefValueStore::OnPrefValueChanged(PrefValueStore::PrefStoreType type, |
| 244 const std::string& key) { | 231 const std::string& key) { |
| 245 NotifyPrefChanged(key.c_str(), type); | 232 NotifyPrefChanged(key.c_str(), type); |
| 246 } | 233 } |
| 247 | 234 |
| 248 void PrefValueStore::OnInitializationCompleted( | 235 void PrefValueStore::OnInitializationCompleted( |
| 249 PrefValueStore::PrefStoreType type) { | 236 PrefValueStore::PrefStoreType type) { |
| 250 CheckInitializationCompleted(); | 237 CheckInitializationCompleted(); |
| 251 } | 238 } |
| 252 | 239 |
| 253 void PrefValueStore::InitPrefStore(PrefValueStore::PrefStoreType type, | 240 void PrefValueStore::InitPrefStore(PrefValueStore::PrefStoreType type, |
| 254 PrefStore* pref_store) { | 241 PrefStore* pref_store) { |
| 255 pref_stores_[type].Initialize(this, pref_store, type); | 242 pref_stores_[type].Initialize(this, pref_store, type); |
| 256 } | 243 } |
| 257 | 244 |
| 258 void PrefValueStore::CheckInitializationCompleted() { | 245 void PrefValueStore::CheckInitializationCompleted() { |
| 259 for (size_t i = 0; i <= PREF_STORE_TYPE_MAX; ++i) { | 246 for (size_t i = 0; i <= PREF_STORE_TYPE_MAX; ++i) { |
| 260 PrefStore* store = GetPrefStore(static_cast<PrefStoreType>(i)); | 247 scoped_refptr<PrefStore> store = |
| 248 GetPrefStore(static_cast<PrefStoreType>(i)); | |
| 261 if (store && !store->IsInitializationComplete()) | 249 if (store && !store->IsInitializationComplete()) |
| 262 return; | 250 return; |
| 263 } | 251 } |
| 264 pref_notifier_->OnInitializationCompleted(); | 252 pref_notifier_->OnInitializationCompleted(); |
| 265 } | 253 } |
| OLD | NEW |