Chromium Code Reviews| Index: chrome/browser/prefs/pref_service.cc |
| diff --git a/chrome/browser/prefs/pref_service.cc b/chrome/browser/prefs/pref_service.cc |
| index 809a8ac1c359f2e73ab239f1729f7c229ece3210..910a993a127988d713c24721dd46673587abf81d 100644 |
| --- a/chrome/browser/prefs/pref_service.cc |
| +++ b/chrome/browser/prefs/pref_service.cc |
| @@ -1,4 +1,4 @@ |
| -// Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| @@ -21,9 +21,11 @@ |
| #include "base/utf_string_conversions.h" |
| #include "build/build_config.h" |
| #include "chrome/browser/browser_thread.h" |
| +#include "chrome/browser/extensions/extension_pref_store.h" |
| #include "chrome/browser/policy/configuration_policy_pref_store.h" |
| #include "chrome/browser/prefs/command_line_pref_store.h" |
| #include "chrome/browser/prefs/default_pref_store.h" |
| +#include "chrome/browser/prefs/overlay_persistent_pref_store.h" |
| #include "chrome/browser/prefs/pref_notifier_impl.h" |
| #include "chrome/browser/prefs/pref_value_store.h" |
| #include "chrome/common/json_pref_store.h" |
| @@ -114,9 +116,25 @@ PrefService* PrefService::CreatePrefService(const FilePath& pref_filename, |
| BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); |
| ConfigurationPolicyPrefStore* recommended = |
| ConfigurationPolicyPrefStore::CreateRecommendedPolicyPrefStore(); |
| + DefaultPrefStore* default_pref_store = new DefaultPrefStore(); |
| return new PrefService(managed, device_management, extension_prefs, |
| - command_line, user, recommended); |
| + command_line, user, recommended, default_pref_store, |
| + true); |
| +} |
| + |
| +PrefService* PrefService::CreateIncognitoPrefService( |
| + PrefStore* incognito_extension_prefs) { |
| + return new PrefService( |
| + managed_platform_prefs_, |
| + device_management_prefs_, |
| + incognito_extension_prefs, |
| + command_line_prefs_, |
| + new OverlayPersistentPrefStore(user_pref_store_.get()), |
| + recommended_prefs_, |
| + default_store_, |
| + false |
| + ); |
| } |
| PrefService::PrefService(PrefStore* managed_platform_prefs, |
| @@ -124,20 +142,28 @@ PrefService::PrefService(PrefStore* managed_platform_prefs, |
| PrefStore* extension_prefs, |
| PrefStore* command_line_prefs, |
| PersistentPrefStore* user_prefs, |
| - PrefStore* recommended_prefs) |
| - : user_pref_store_(user_prefs) { |
| + PrefStore* recommended_prefs, |
| + DefaultPrefStore* default_store, |
| + bool init_from_storage) |
| + : managed_platform_prefs_(managed_platform_prefs), |
| + device_management_prefs_(device_management_prefs), |
| + extension_prefs_(extension_prefs), |
| + command_line_prefs_(command_line_prefs), |
| + recommended_prefs_(recommended_prefs), |
| + user_pref_store_(user_prefs), |
| + default_store_(default_store) { |
| pref_notifier_.reset(new PrefNotifierImpl(this)); |
| - default_store_ = new DefaultPrefStore(); |
| pref_value_store_ = |
| - new PrefValueStore(managed_platform_prefs, |
| - device_management_prefs, |
| - extension_prefs, |
| - command_line_prefs, |
| + new PrefValueStore(managed_platform_prefs_, |
| + device_management_prefs_, |
| + extension_prefs_, |
| + command_line_prefs_, |
| user_pref_store_, |
| - recommended_prefs, |
| + recommended_prefs_, |
| default_store_, |
| pref_notifier_.get()); |
| - InitFromStorage(); |
| + if (init_from_storage) |
|
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
I believe I have commented on this before but don'
battre
2011/01/05 20:23:08
Done.
|
| + InitFromStorage(); |
| } |
| PrefService::~PrefService() { |
| @@ -325,15 +351,23 @@ FilePath PrefService::GetFilePath(const char* path) const { |
| } |
| bool PrefService::HasPrefPath(const char* path) const { |
| - return pref_value_store_->HasPrefPath(path); |
| + const Preference* pref = FindPreference(path); |
| + return pref && !pref->IsDefaultValue(); |
| } |
| const PrefService::Preference* PrefService::FindPreference( |
| const char* pref_name) const { |
| DCHECK(CalledOnValidThread()); |
| - Preference p(this, pref_name); |
| + Preference p(this, pref_name, Value::TYPE_NULL); |
| PreferenceSet::const_iterator it = prefs_.find(&p); |
| - return it == prefs_.end() ? NULL : *it; |
| + if (it != prefs_.end()) |
| + return *it; |
| + const Value::ValueType type = default_store_->GetType(pref_name); |
| + if (type == Value::TYPE_NULL) |
| + return NULL; |
| + Preference* new_pref = new Preference(this, pref_name, type); |
| + prefs_.insert(new_pref); |
| + return new_pref; |
| } |
| bool PrefService::ReadOnly() const { |
| @@ -346,10 +380,7 @@ PrefNotifier* PrefService::pref_notifier() const { |
| bool PrefService::IsManagedPreference(const char* pref_name) const { |
| const Preference* pref = FindPreference(pref_name); |
| - if (pref && pref->IsManaged()) { |
| - return true; |
| - } |
| - return false; |
| + return pref && pref->IsManaged(); |
| } |
| const DictionaryValue* PrefService::GetDictionary(const char* path) const { |
| @@ -405,18 +436,10 @@ void PrefService::RegisterPreference(const char* path, Value* default_value) { |
| DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) << |
| "invalid preference type: " << orig_type; |
| - // We set the default value of dictionaries and lists to be null so it's |
| - // easier for callers to check for empty dict/list prefs. The PrefValueStore |
| - // accepts ownership of the value (null or default_value). |
| - if (Value::TYPE_LIST == orig_type || Value::TYPE_DICTIONARY == orig_type) { |
| - default_store_->SetDefaultValue(path, Value::CreateNullValue()); |
| - } else { |
| - // Hand off ownership. |
| - default_store_->SetDefaultValue(path, scoped_value.release()); |
| - } |
| + // Hand off ownership. |
| + default_store_->SetDefaultValue(path, scoped_value.release()); |
| - pref_value_store_->RegisterPreferenceType(path, orig_type); |
| - prefs_.insert(new Preference(this, path)); |
| + prefs_.insert(new Preference(this, path, orig_type)); |
|
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
If new do this, we don't need the lazy creation co
battre
2011/01/05 20:23:08
Done.
|
| } |
| void PrefService::ClearPref(const char* path) { |
| @@ -439,13 +462,7 @@ void PrefService::Set(const char* path, const Value& value) { |
| return; |
| } |
| - // Allow dictionary and list types to be set to null, which removes their |
| - // user values. |
| - if (value.GetType() == Value::TYPE_NULL && |
| - (pref->GetType() == Value::TYPE_DICTIONARY || |
| - pref->GetType() == Value::TYPE_LIST)) { |
| - user_pref_store_->RemoveValue(path); |
| - } else if (pref->GetType() != value.GetType()) { |
| + if (pref->GetType() != value.GetType()) { |
| NOTREACHED() << "Trying to set pref " << path |
| << " of type " << pref->GetType() |
| << " to value of type " << value.GetType(); |
| @@ -587,15 +604,17 @@ void PrefService::SetUserPrefValue(const char* path, Value* new_value) { |
| // PrefService::Preference |
| PrefService::Preference::Preference(const PrefService* service, |
| - const char* name) |
| + const char* name, |
| + Value::ValueType type) |
| : name_(name), |
| + type_(type), |
| pref_service_(service) { |
| DCHECK(name); |
| DCHECK(service); |
| } |
| Value::ValueType PrefService::Preference::GetType() const { |
| - return pref_service_->pref_value_store_->GetRegisteredType(name_); |
| + return type_; |
| } |
| const Value* PrefService::Preference::GetValue() const { |
| @@ -603,8 +622,10 @@ const Value* PrefService::Preference::GetValue() const { |
| "Must register pref before getting its value"; |
| Value* found_value = NULL; |
| - if (pref_service_->pref_value_store_->GetValue(name_, &found_value)) |
| + if (pref_service_->pref_value_store_->GetValue(name_, type_, &found_value)) { |
| + DCHECK(found_value->IsType(type_)); |
| return found_value; |
| + } |
| // Every registered preference has at least a default value. |
| NOTREACHED() << "no valid value found for registered pref " << name_; |