Index: chrome/browser/extensions/extension_prefs.cc |
diff --git a/chrome/browser/extensions/extension_prefs.cc b/chrome/browser/extensions/extension_prefs.cc |
index 33ced74134ae65cc4204007d1b594814a173eb74..dfe6acb0c863fc32f8a4c819de3a7456ee46fbeb 100644 |
--- a/chrome/browser/extensions/extension_prefs.cc |
+++ b/chrome/browser/extensions/extension_prefs.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. |
@@ -141,12 +141,13 @@ static void ExtentToStringSet(const ExtensionExtent& host_extent, |
} // namespace |
-ExtensionPrefs::ExtensionPrefs(PrefService* prefs, |
- const FilePath& root_dir, |
- ExtensionPrefStore* pref_store) |
+ExtensionPrefs::ExtensionPrefs( |
+ PrefService* prefs, |
+ const FilePath& root_dir, |
+ ExtensionPrefValueMap* extension_pref_value_map) |
: prefs_(prefs), |
install_directory_(root_dir), |
- pref_store_(pref_store) { |
+ extension_pref_value_map_(extension_pref_value_map) { |
// TODO(asargent) - Remove this in a couple of months. (See comment above |
// CleanupBadExtensionKeys). |
CleanupBadExtensionKeys(prefs_); |
@@ -696,15 +697,14 @@ void ExtensionPrefs::OnExtensionInstalled( |
} |
UpdateExtensionPref(id, kPrefAppLaunchIndex, |
Value::CreateIntegerValue(GetNextAppLaunchIndex())); |
+ extension_pref_value_map_->RegisterExtension( |
+ id, install_time, initial_state == Extension::ENABLED); |
SavePrefsAndNotify(); |
} |
void ExtensionPrefs::OnExtensionUninstalled(const std::string& extension_id, |
const Extension::Location& location, |
bool external_uninstall) { |
- PrefKeySet pref_keys; |
- GetExtensionControlledPrefKeys(extension_id, &pref_keys); |
- |
// For external extensions, we save a preference reminding ourself not to try |
// and install the extension anymore (except when |external_uninstall| is |
// true, which signifies that the registry key was deleted or the pref file |
@@ -713,11 +713,10 @@ void ExtensionPrefs::OnExtensionUninstalled(const std::string& extension_id, |
UpdateExtensionPref(extension_id, kPrefState, |
Value::CreateIntegerValue(Extension::KILLBIT)); |
SavePrefsAndNotify(); |
+ extension_pref_value_map_->SetExtensionState(extension_id, false); |
} else { |
DeleteExtensionPrefs(extension_id); |
} |
- |
- UpdatePrefStore(pref_keys); |
} |
Extension::State ExtensionPrefs::GetExtensionState( |
@@ -742,12 +741,10 @@ void ExtensionPrefs::SetExtensionState(const Extension* extension, |
Extension::State state) { |
UpdateExtensionPref(extension->id(), kPrefState, |
Value::CreateIntegerValue(state)); |
- |
- PrefKeySet pref_keys; |
- GetExtensionControlledPrefKeys(extension->id(), &pref_keys); |
- UpdatePrefStore(pref_keys); |
- |
SavePrefsAndNotify(); |
+ |
+ bool enabled = (state == Extension::ENABLED); |
+ extension_pref_value_map_->SetExtensionState(extension->id(), enabled); |
} |
bool ExtensionPrefs::GetBrowserActionVisibility(const Extension* extension) { |
@@ -827,6 +824,7 @@ void ExtensionPrefs::DeleteExtensionPrefs(const std::string& extension_id) { |
dict->Remove(extension_id, NULL); |
SavePrefsAndNotify(); |
} |
+ extension_pref_value_map_->UnregisterExtension(extension_id); |
} |
DictionaryValue* ExtensionPrefs::GetOrCreateExtensionPref( |
@@ -852,18 +850,6 @@ DictionaryValue* ExtensionPrefs::GetExtensionPref( |
return extension; |
} |
-DictionaryValue* ExtensionPrefs::GetExtensionControlledPrefs( |
- const std::string& extension_id) const { |
- DictionaryValue* extension = GetExtensionPref(extension_id); |
- if (!extension) { |
- NOTREACHED(); |
- return NULL; |
- } |
- DictionaryValue* preferences = NULL; |
- extension->GetDictionary(kPrefPreferences, &preferences); |
- return preferences; |
-} |
- |
// Helper function for GetInstalledExtensionsInfo. |
static ExtensionInfo* GetInstalledExtensionInfoImpl( |
DictionaryValue* extension_data, |
@@ -1154,15 +1140,13 @@ base::Time ExtensionPrefs::GetInstallTime( |
return base::Time::FromInternalValue(install_time_i64); |
} |
-void ExtensionPrefs::GetEnabledExtensions(ExtensionIdSet* out) const { |
+void ExtensionPrefs::GetExtensions(ExtensionIdSet* out) const { |
CHECK(out); |
const DictionaryValue* extensions = |
pref_service()->GetDictionary(kExtensionsPref); |
for (DictionaryValue::key_iterator ext_id = extensions->begin_keys(); |
ext_id != extensions->end_keys(); ++ext_id) { |
- if (GetExtensionState(*ext_id) != Extension::ENABLED) |
- continue; |
out->push_back(*ext_id); |
} |
} |
@@ -1192,119 +1176,69 @@ void ExtensionPrefs::FixMissingPrefs(const ExtensionIdSet& extension_ids) { |
SavePrefsAndNotify(); |
} |
+DictionaryValue* ExtensionPrefs::GetExtensionControlledPrefs( |
+ const std::string& extension_id) const { |
+ DictionaryValue* source_dict = prefs_->GetMutableDictionary(kExtensionsPref); |
+ DictionaryValue* preferences = NULL; |
+ std::string key = extension_id + std::string(".") + kPrefPreferences; |
+ if (!source_dict->GetDictionary(key, &preferences)) { |
+ source_dict->Set(key, new DictionaryValue); |
+ bool success = source_dict->GetDictionary(key, &preferences); |
+ DCHECK(success); |
+ } |
+ return preferences; |
+} |
+ |
void ExtensionPrefs::InitPrefStore() { |
// When this is called, the PrefService is initialized and provides access |
// to the user preferences stored in a JSON file. |
ExtensionIdSet extension_ids; |
- GetEnabledExtensions(&extension_ids); |
+ GetExtensions(&extension_ids); |
FixMissingPrefs(extension_ids); |
- // Collect the unique extension controlled preference keys of all extensions. |
- PrefKeySet ext_controlled_prefs; |
+ // Store extension controlled preference values in the |
+ // |extension_pref_value_map_|, which then informs the subscribers |
+ // (ExtensionPrefStores) about the winning values. |
for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); |
ext_id != extension_ids.end(); ++ext_id) { |
- GetExtensionControlledPrefKeys(*ext_id, &ext_controlled_prefs); |
- } |
- |
- // Store winning preference for each extension controlled preference. |
- UpdatePrefStore(ext_controlled_prefs); |
- pref_store_->OnInitializationCompleted(); |
-} |
- |
-const Value* ExtensionPrefs::GetWinningExtensionControlledPrefValue( |
- const std::string& key) const { |
- Value *winner = NULL; |
- base::Time winners_install_time = base::Time(); |
- |
- ExtensionIdSet extension_ids; |
- GetEnabledExtensions(&extension_ids); |
- for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); |
- ext_id != extension_ids.end(); ++ext_id) { |
- base::Time extension_install_time = GetInstallTime(*ext_id); |
- |
- // We do not need to consider extensions that were installed before the |
- // most recent extension found that provides the requested preference. |
- if (extension_install_time < winners_install_time) |
- continue; |
- |
- DictionaryValue* preferences = GetExtensionControlledPrefs(*ext_id); |
- Value *value = NULL; |
- if (preferences && preferences->GetWithoutPathExpansion(key, &value)) { |
- // This extension is more recent than the last one providing this pref. |
- winner = value; |
- winners_install_time = extension_install_time; |
+ extension_pref_value_map_->RegisterExtension( |
+ *ext_id, |
+ GetInstallTime(*ext_id), |
+ GetExtensionState(*ext_id) == Extension::ENABLED); |
+ |
+ DictionaryValue* prefs = GetExtensionControlledPrefs(*ext_id); |
+ for (DictionaryValue::key_iterator i = prefs->begin_keys(); |
+ i != prefs->end_keys(); ++i) { |
+ Value* value; |
+ if (!prefs->GetWithoutPathExpansion(*i, &value)) |
+ continue; |
+ extension_pref_value_map_->SetExtensionPref( |
+ *ext_id, *i, false, value->DeepCopy()); |
} |
} |
- return winner; |
-} |
- |
-void ExtensionPrefs::UpdatePrefStore( |
- const ExtensionPrefs::PrefKeySet& pref_keys) { |
- for (PrefKeySet::const_iterator i = pref_keys.begin(); |
- i != pref_keys.end(); ++i) { |
- UpdatePrefStore(*i); |
- } |
+ extension_pref_value_map_->NotifyInitializationCompleted(); |
} |
-void ExtensionPrefs::UpdatePrefStore(const std::string& pref_key) { |
- if (pref_store_ == NULL) |
- return; |
- const Value* winning_pref_value = |
- GetWinningExtensionControlledPrefValue(pref_key); |
- |
- if (winning_pref_value) |
- pref_store_->SetExtensionPref(pref_key, winning_pref_value->DeepCopy()); |
- else |
- pref_store_->RemoveExtensionPref(pref_key); |
-} |
void ExtensionPrefs::SetExtensionControlledPref(const std::string& extension_id, |
const std::string& pref_key, |
+ bool incognito, |
Value* value) { |
- scoped_ptr<Value> scoped_value(value); |
DCHECK(pref_service()->FindPreference(pref_key.c_str())) |
<< "Extension controlled preference key " << pref_key |
<< " not registered."; |
- DictionaryValue* extension_preferences = |
- GetExtensionControlledPrefs(extension_id); |
- |
- if (extension_preferences == NULL) { // May be pruned when writing to disk. |
- DictionaryValue* extension = GetExtensionPref(extension_id); |
- if (extension == NULL) { |
- LOG(ERROR) << "Extension preference for " << extension_id << " undefined"; |
- return; |
- } |
- extension_preferences = new DictionaryValue; |
- extension->Set(kPrefPreferences, extension_preferences); |
- } |
- |
- Value* oldValue = NULL; |
- extension_preferences->GetWithoutPathExpansion(pref_key, &oldValue); |
- bool modified = !Value::Equals(oldValue, scoped_value.get()); |
- if (!modified) |
- return; |
- if (scoped_value.get() == NULL) |
- extension_preferences->RemoveWithoutPathExpansion(pref_key, NULL); |
- else |
- extension_preferences->SetWithoutPathExpansion(pref_key, |
- scoped_value.release()); |
- pref_service()->ScheduleSavePersistentPrefs(); |
- |
- UpdatePrefStore(pref_key); |
-} |
- |
-void ExtensionPrefs::GetExtensionControlledPrefKeys( |
- const std::string& extension_id, PrefKeySet *out) const { |
- DCHECK(out != NULL); |
- DictionaryValue* ext_prefs = GetExtensionControlledPrefs(extension_id); |
- if (ext_prefs) { |
- for (DictionaryValue::key_iterator i = ext_prefs->begin_keys(); |
- i != ext_prefs->end_keys(); ++i) { |
- out->insert(*i); |
- } |
+ if (!incognito) { |
+ // Also store in persisted Preferences file to recover after a |
+ // browser restart. |
+ DictionaryValue* dict = GetExtensionControlledPrefs(extension_id); |
+ dict->SetWithoutPathExpansion(pref_key, value->DeepCopy()); |
+ pref_service()->ScheduleSavePersistentPrefs(); |
} |
+ |
+ extension_pref_value_map_->SetExtensionPref( |
+ extension_id, pref_key, incognito, value); |
} |
// static |