Chromium Code Reviews| Index: chrome/browser/extensions/extension_prefs.cc | 
| diff --git a/chrome/browser/extensions/extension_prefs.cc b/chrome/browser/extensions/extension_prefs.cc | 
| index 1f128ab2f7ce218be0f3f783fd8bbde5b772d424..e1919d3c5ea3d5ec313218c21e7d1fcfd5103178 100644 | 
| --- a/chrome/browser/extensions/extension_prefs.cc | 
| +++ b/chrome/browser/extensions/extension_prefs.cc | 
| @@ -141,12 +141,17 @@ static void ExtentToStringSet(const ExtensionExtent& host_extent, | 
| } // namespace | 
| -ExtensionPrefs::ExtensionPrefs(PrefService* prefs, | 
| - const FilePath& root_dir, | 
| - ExtensionPrefStore* pref_store) | 
| +ExtensionPrefs::ExtensionPrefs( | 
| + PrefService* prefs, | 
| + PrefService* incognito_prefs, | 
| + const FilePath& root_dir, | 
| + ExtensionPrefStore* pref_store, | 
| + ExtensionPrefStore* incognito_pref_store) | 
| : prefs_(prefs), | 
| + incognito_prefs_(incognito_prefs), | 
| install_directory_(root_dir), | 
| - pref_store_(pref_store) { | 
| + pref_store_(pref_store), | 
| + incognito_pref_store_(incognito_pref_store) { | 
| // TODO(asargent) - Remove this in a couple of months. (See comment above | 
| // CleanupBadExtensionKeys). | 
| CleanupBadExtensionKeys(prefs_); | 
| @@ -823,10 +828,15 @@ void ExtensionPrefs::UpdateExtensionPref(const std::string& extension_id, | 
| } | 
| void ExtensionPrefs::DeleteExtensionPrefs(const std::string& extension_id) { | 
| - DictionaryValue* dict = prefs_->GetMutableDictionary(kExtensionsPref); | 
| - if (dict->HasKey(extension_id)) { | 
| - dict->Remove(extension_id, NULL); | 
| - SavePrefsAndNotify(); | 
| + for (int incognito = 0; incognito <= 1; ++incognito) { | 
| 
 
danno
2010/12/22 10:48:21
Ouch. This fast-and-loose use of boolean/int conve
 
battre
2010/12/22 18:34:53
Done. - Though with "boolean incognito" instead of
 
 | 
| + PrefService* prefs = incognito ? incognito_prefs_ : prefs_; | 
| + if (!prefs) // May be null for unit tests. | 
| + continue; | 
| + DictionaryValue* dict = prefs->GetMutableDictionary(kExtensionsPref); | 
| + if (dict->HasKey(extension_id)) { | 
| + dict->Remove(extension_id, NULL); | 
| + SavePrefsAndNotify(); | 
| + } | 
| } | 
| } | 
| @@ -854,14 +864,32 @@ DictionaryValue* ExtensionPrefs::GetExtensionPref( | 
| } | 
| DictionaryValue* ExtensionPrefs::GetExtensionControlledPrefs( | 
| - const std::string& extension_id) const { | 
| - DictionaryValue* extension = GetExtensionPref(extension_id); | 
| - if (!extension) { | 
| - NOTREACHED(); | 
| + const std::string& extension_id, | 
| + bool incognito) const { | 
| + PrefService* source = incognito ? incognito_prefs_ : prefs_; | 
| + if (source == NULL) // May be null for unit tests. | 
| return NULL; | 
| + DictionaryValue* source_dict = source->GetMutableDictionary(kExtensionsPref); | 
| + | 
| + // The user pref store of incognito_prefs_ should have a kExtensionsPref | 
| + // dictionary. The request must not fall through to the underlying | 
| + // PrefService. | 
| + if (incognito && | 
| + source_dict == prefs_->GetMutableDictionary(kExtensionsPref)) { | 
| + DictionaryValue empty; | 
| + incognito_prefs_->Set(kExtensionsPref, empty); | 
| + source_dict = source->GetMutableDictionary(kExtensionsPref); | 
| } | 
| + DCHECK(!incognito || | 
| + source_dict != prefs_->GetMutableDictionary(kExtensionsPref)); | 
| + | 
| DictionaryValue* preferences = NULL; | 
| - extension->GetDictionary(kPrefPreferences, &preferences); | 
| + 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; | 
| } | 
| @@ -1194,11 +1222,12 @@ void ExtensionPrefs::InitPrefStore() { | 
| // Store winning preference for each extension controlled preference. | 
| UpdatePrefStore(ext_controlled_prefs); | 
| - pref_store_->OnInitializationCompleted(); | 
| + if (pref_store_) | 
| + pref_store_->OnInitializationCompleted(); | 
| } | 
| const Value* ExtensionPrefs::GetWinningExtensionControlledPrefValue( | 
| - const std::string& key) const { | 
| + const std::string& key, bool incognito) const { | 
| Value *winner = NULL; | 
| base::Time winners_install_time = base::Time(); | 
| @@ -1213,12 +1242,14 @@ const Value* ExtensionPrefs::GetWinningExtensionControlledPrefValue( | 
| 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; | 
| + for (int i = 0; i <= (incognito ? 1 : 0); ++i) { | 
| + DictionaryValue* preferences = GetExtensionControlledPrefs(*ext_id, !!i); | 
| 
 
danno
2010/12/22 10:48:21
whoa. !!!(likes(danno, this)). Is this really more
 
battre
2010/12/22 18:34:53
Philistine! This is art. ;-)
Done.
 
 | 
| + 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; | 
| + } | 
| } | 
| } | 
| @@ -1229,42 +1260,44 @@ void ExtensionPrefs::UpdatePrefStore( | 
| const ExtensionPrefs::PrefKeySet& pref_keys) { | 
| for (PrefKeySet::const_iterator i = pref_keys.begin(); | 
| i != pref_keys.end(); ++i) { | 
| - UpdatePrefStore(*i); | 
| + UpdatePrefStore(*i, false); // Regular PrefService. | 
| + UpdatePrefStore(*i, true); // Incognito PrefService. | 
| } | 
| } | 
| -void ExtensionPrefs::UpdatePrefStore(const std::string& pref_key) { | 
| - if (pref_store_ == NULL) | 
| +void ExtensionPrefs::UpdatePrefStore(const std::string& pref_key, | 
| + bool incognito) { | 
| + scoped_refptr<ExtensionPrefStore> destination = | 
| + incognito ? incognito_pref_store_ : pref_store_; | 
| + if (destination == NULL) // May be null for unit tests. | 
| return; | 
| const Value* winning_pref_value = | 
| - GetWinningExtensionControlledPrefValue(pref_key); | 
| + GetWinningExtensionControlledPrefValue(pref_key, incognito); | 
| if (winning_pref_value) | 
| - pref_store_->SetExtensionPref(pref_key, winning_pref_value->DeepCopy()); | 
| + destination->SetExtensionPref(pref_key, winning_pref_value->DeepCopy()); | 
| else | 
| - pref_store_->RemoveExtensionPref(pref_key); | 
| + destination->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); | 
| + if (incognito && !incognito_pref_store_) { | 
| + LOG(WARNING) << "Ignoring SetExtensionControlledPref for incognito " | 
| + << "preferences, missing an incognito_pref_store."; | 
| + return; | 
| } | 
| + DictionaryValue* extension_preferences = | 
| + GetExtensionControlledPrefs(extension_id, incognito); | 
| + | 
| Value* oldValue = NULL; | 
| extension_preferences->GetWithoutPathExpansion(pref_key, &oldValue); | 
| bool modified = !Value::Equals(oldValue, scoped_value.get()); | 
| @@ -1276,19 +1309,25 @@ void ExtensionPrefs::SetExtensionControlledPref(const std::string& extension_id, | 
| else | 
| extension_preferences->SetWithoutPathExpansion(pref_key, | 
| scoped_value.release()); | 
| - pref_service()->ScheduleSavePersistentPrefs(); | 
| + if (!incognito) | 
| + pref_service()->ScheduleSavePersistentPrefs(); | 
| - UpdatePrefStore(pref_key); | 
| + if (!incognito) | 
| + UpdatePrefStore(pref_key, false); // Update regular prefs. | 
| + UpdatePrefStore(pref_key, true); // Update incognito prefs. | 
| } | 
| 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); | 
| + for (int incognito = 0; incognito <= 1; ++incognito) { | 
| + DictionaryValue* ext_prefs = GetExtensionControlledPrefs(extension_id, | 
| + !!incognito); | 
| 
 
danno
2010/12/22 10:48:21
!!!(still_likes(danno, this))
 
battre
2010/12/22 18:34:53
Done.
 
 | 
| + if (ext_prefs) { | 
| + for (DictionaryValue::key_iterator i = ext_prefs->begin_keys(); | 
| + i != ext_prefs->end_keys(); ++i) { | 
| + out->insert(*i); | 
| + } | 
| } | 
| } | 
| } |