Chromium Code Reviews| Index: chrome/browser/content_settings/content_settings_pref_provider.cc |
| diff --git a/chrome/browser/content_settings/content_settings_pref_provider.cc b/chrome/browser/content_settings/content_settings_pref_provider.cc |
| index c7fe7fdcdc13715573fbf2f236926aa08a0cee62..1b43ae2203d8feb62298cd1cc1fdcec0ba0843e0 100644 |
| --- a/chrome/browser/content_settings/content_settings_pref_provider.cc |
| +++ b/chrome/browser/content_settings/content_settings_pref_provider.cc |
| @@ -38,7 +38,7 @@ const char* kResourceTypeNames[] = { |
| NULL, |
| "per_plugin", |
| NULL, |
| - NULL, // Not used for Geolocation |
| + NULL, |
| NULL, // Not used for Notifications |
| }; |
| COMPILE_ASSERT(arraysize(kResourceTypeNames) == CONTENT_SETTINGS_NUM_TYPES, |
| @@ -64,9 +64,9 @@ const char* kTypeNames[] = { |
| "javascript", |
| "plugins", |
| "popups", |
| + "geolocation", |
| // TODO(markusheintz): Refactoring in progress. Content settings exceptions |
| - // for notifications and geolocation will be added next. |
| - "geolocation", // Only used for default Geolocation settings |
| + // for notifications added next. |
| "notifications", // Only used for default Notifications settings. |
| }; |
| COMPILE_ASSERT(arraysize(kTypeNames) == CONTENT_SETTINGS_NUM_TYPES, |
| @@ -83,6 +83,26 @@ void SetDefaultContentSettings(DictionaryValue* default_settings) { |
| } |
| } |
| +void CopyKeys(DictionaryValue* source, |
|
Bernhard Bauer
2011/08/09 14:09:45
Didn't you already delete this?
markusheintz_
2011/08/09 14:40:49
*Grrrr* to myself. Sorry that sneaked back in with
|
| + DictionaryValue* target, |
| + const char** keys_to_copy, |
| + size_t size) { |
| + for (DictionaryValue::key_iterator i(source->begin_keys()); |
| + i != source->end_keys(); ++i) { |
| + const std::string& key(*i); |
| + for (size_t i = 0; i < size; ++i) { |
| + if ((keys_to_copy[i] != NULL) && (keys_to_copy[i] == key)) { |
| + Value* value; |
| + bool found = source->GetWithoutPathExpansion( |
| + key, &value); |
| + DCHECK(found); |
| + target->SetWithoutPathExpansion(key, value->DeepCopy()); |
| + break; |
| + } |
| + } |
| + } |
| +} |
| + |
| ContentSetting ValueToContentSetting(Value* value) { |
| int int_value; |
| value->GetAsInteger(&int_value); |
| @@ -357,6 +377,8 @@ void PrefProvider::RegisterUserPrefs(PrefService* prefs) { |
| PrefService::SYNCABLE_PREF); |
| // Obsolete prefs, for migration: |
| + prefs->RegisterDictionaryPref(prefs::kGeolocationContentSettings, |
| + PrefService::SYNCABLE_PREF); |
| prefs->RegisterDictionaryPref(prefs::kContentSettingsPatterns, |
| PrefService::SYNCABLE_PREF); |
| prefs->RegisterListPref(prefs::kPopupWhitelistedHosts, |
| @@ -376,6 +398,7 @@ PrefProvider::PrefProvider(PrefService* prefs, |
| MigrateObsoletePerhostPref(); |
| MigrateObsoletePopupsPref(); |
| MigrateObsoleteContentSettingsPatternPref(); |
| + MigrateObsoleteGeolocationPref(); |
| } |
| // Verify preferences version. |
| @@ -399,6 +422,7 @@ PrefProvider::PrefProvider(PrefService* prefs, |
| pref_change_registrar_.Init(prefs_); |
| pref_change_registrar_.Add(prefs::kContentSettingsPatterns, this); |
| pref_change_registrar_.Add(prefs::kContentSettingsPatternPairs, this); |
| + pref_change_registrar_.Add(prefs::kGeolocationContentSettings, this); |
| } |
| ContentSetting PrefProvider::GetContentSetting( |
| @@ -465,7 +489,7 @@ void PrefProvider::SetContentSetting( |
| ContentSetting setting) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| DCHECK(prefs_); |
| - DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. |
| + DCHECK(kTypeNames[content_type] != NULL); |
| // Update in memory value map. |
| OriginIdentifierValueMap* map_to_modify = &incognito_value_map_; |
| @@ -551,12 +575,17 @@ void PrefProvider::Observe( |
| std::string* name = Details<std::string>(details).ptr(); |
| if (*name == prefs::kContentSettingsPatternPairs) { |
| - SyncObsoletePref(); |
| + SyncObsoletePatternPref(); |
| + SyncObsoleteGeolocationPref(); |
| ReadContentSettingsFromPref(true); |
| } else if (*name == prefs::kContentSettingsPatterns) { |
| AutoReset<bool> auto_reset(&updating_preferences_, true); |
| MigrateObsoleteContentSettingsPatternPref(); |
| ReadContentSettingsFromPref(true); |
| + } else if (*name == prefs::kGeolocationContentSettings) { |
| + AutoReset<bool> auto_reset(&updating_preferences_, true); |
| + MigrateObsoleteGeolocationPref(); |
| + ReadContentSettingsFromPref(true); |
| } else { |
| NOTREACHED() << "Unexpected preference observed"; |
| return; |
| @@ -590,11 +619,17 @@ void PrefProvider::UpdatePref( |
| content_type, |
| resource_identifier, |
| setting); |
| - UpdatePatternsPref(primary_pattern, |
| - secondary_pattern, |
| - content_type, |
| - resource_identifier, |
| - setting); |
| + if (content_type != CONTENT_SETTINGS_TYPE_GEOLOCATION && |
| + content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS) { |
| + UpdateObsoletePatternsPref(primary_pattern, |
| + secondary_pattern, |
| + content_type, |
| + resource_identifier, |
| + setting); |
| + } |
| + if (content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION) { |
| + UpdateObsoleteGeolocationPref(primary_pattern, secondary_pattern, setting); |
| + } |
| } |
| void PrefProvider::ReadContentSettingsFromPref(bool overwrite) { |
| @@ -689,7 +724,7 @@ void PrefProvider::ReadContentSettingsFromPref(bool overwrite) { |
| } |
| } |
| -void PrefProvider::UpdatePatternsPref( |
| +void PrefProvider::UpdateObsoletePatternsPref( |
| const ContentSettingsPattern& primary_pattern, |
| const ContentSettingsPattern& secondary_pattern, |
| ContentSettingsType content_type, |
| @@ -822,6 +857,42 @@ void PrefProvider::UpdatePatternPairsPref( |
| } |
| } |
| +void PrefProvider::UpdateObsoleteGeolocationPref( |
| + const ContentSettingsPattern& primary_pattern, |
| + const ContentSettingsPattern& secondary_pattern, |
| + ContentSetting setting) { |
| + if (!prefs_) |
| + return; |
| + |
| + const GURL requesting_origin(primary_pattern.ToString()); |
| + const GURL embedding_origin(secondary_pattern.ToString()); |
| + DCHECK(requesting_origin.is_valid() && embedding_origin.is_valid()); |
| + |
| + DictionaryPrefUpdate update(prefs_, prefs::kGeolocationContentSettings); |
| + DictionaryValue* all_settings_dictionary = update.Get(); |
| + DictionaryValue* requesting_origin_settings_dictionary = NULL; |
| + all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
| + requesting_origin.spec(), &requesting_origin_settings_dictionary); |
| + if (setting == CONTENT_SETTING_DEFAULT) { |
| + if (requesting_origin_settings_dictionary) { |
| + requesting_origin_settings_dictionary->RemoveWithoutPathExpansion( |
| + embedding_origin.spec(), NULL); |
| + if (requesting_origin_settings_dictionary->empty()) |
|
Bernhard Bauer
2011/08/09 14:09:45
Nit: curlies please.
markusheintz_
2011/08/09 14:40:49
Done.
|
| + all_settings_dictionary->RemoveWithoutPathExpansion( |
| + requesting_origin.spec(), NULL); |
| + } |
| + } else { |
| + if (!requesting_origin_settings_dictionary) { |
| + requesting_origin_settings_dictionary = new DictionaryValue; |
| + all_settings_dictionary->SetWithoutPathExpansion( |
| + requesting_origin.spec(), requesting_origin_settings_dictionary); |
| + } |
| + DCHECK(requesting_origin_settings_dictionary); |
| + requesting_origin_settings_dictionary->SetWithoutPathExpansion( |
| + embedding_origin.spec(), Value::CreateIntegerValue(setting)); |
| + } |
| +} |
| + |
| // static |
| void PrefProvider::CanonicalizeContentSettingsExceptions( |
| DictionaryValue* all_settings_dictionary) { |
| @@ -964,14 +1035,14 @@ void PrefProvider::MigrateObsoletePopupsPref() { |
| void PrefProvider::MigrateObsoleteContentSettingsPatternPref() { |
| if (prefs_->HasPrefPath(prefs::kContentSettingsPatterns) && !is_incognito_) { |
| - const DictionaryValue* all_settings_dictionary = |
| - prefs_->GetDictionary(prefs::kContentSettingsPatterns); |
| + const DictionaryValue* patterns_dictionary = |
| + prefs_->GetDictionary(prefs::kContentSettingsPatterns); |
| DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs); |
| DictionaryValue* exceptions_dictionary; |
| exceptions_dictionary = update.Get(); |
| - for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys()); |
| - i != all_settings_dictionary->end_keys(); |
| + for (DictionaryValue::key_iterator i(patterns_dictionary->begin_keys()); |
| + i != patterns_dictionary->end_keys(); |
| ++i) { |
| const std::string& key(*i); |
| if (key.empty()) |
| @@ -989,7 +1060,7 @@ void PrefProvider::MigrateObsoleteContentSettingsPatternPref() { |
| // Copy dictionary value. |
| // Get old settings. |
| DictionaryValue* dictionary = NULL; |
| - bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
| + bool found = patterns_dictionary->GetDictionaryWithoutPathExpansion( |
| key, &dictionary); |
| DCHECK(found); |
| @@ -1004,7 +1075,7 @@ void PrefProvider::MigrateObsoleteContentSettingsPatternPref() { |
| } |
| } |
| -void PrefProvider::SyncObsoletePref() { |
| +void PrefProvider::SyncObsoletePatternPref() { |
| AutoReset<bool> auto_reset(&updating_preferences_, true); |
| if (prefs_->HasPrefPath(prefs::kContentSettingsPatternPairs) && |
| !is_incognito_) { |
| @@ -1027,15 +1098,134 @@ void PrefProvider::SyncObsoletePref() { |
| continue; |
| } |
| - // Copy dictionary |
| - DictionaryValue* dictionary = NULL; |
| + DictionaryValue* settings_dictionary = NULL; |
| bool found = pattern_pairs_dictionary->GetDictionaryWithoutPathExpansion( |
| - key, &dictionary); |
| + key, &settings_dictionary); |
| DCHECK(found); |
| - std::string new_key = pattern_pair.first.ToString(); |
| - // Existing values are overwritten. |
| - obsolete_settings_dictionary->SetWithoutPathExpansion( |
| - new_key, dictionary->DeepCopy()); |
| + scoped_ptr<DictionaryValue> settings_dictionary_copy( |
| + new DictionaryValue()); |
| + for (size_t i = CONTENT_SETTINGS_TYPE_COOKIES; |
| + i <= CONTENT_SETTINGS_TYPE_POPUPS; |
| + ++i) { |
| + DCHECK(kTypeNames[i]); |
| + std::string type_name(kTypeNames[i]); |
| + if (settings_dictionary->HasKey(type_name)) { |
| + Value* value = NULL; |
| + bool found = settings_dictionary->GetWithoutPathExpansion( |
| + type_name, &value); |
| + DCHECK(found); |
| + settings_dictionary_copy->SetWithoutPathExpansion( |
| + type_name, value->DeepCopy()); |
| + } |
| + } |
| + |
| + // Ignore empty dictionaryies. |
| + if (!settings_dictionary_copy->empty()) { |
| + std::string new_key = pattern_pair.first.ToString(); |
| + // Existing values are overwritten. |
| + obsolete_settings_dictionary->SetWithoutPathExpansion( |
| + new_key, settings_dictionary_copy.release()); |
| + } |
| + } |
| + } |
| +} |
| + |
| +void PrefProvider::MigrateObsoleteGeolocationPref() { |
| + if (!prefs_->HasPrefPath(prefs::kGeolocationContentSettings)) |
| + return; |
| + |
| + const DictionaryValue* geolocation_settings = |
| + prefs_->GetDictionary(prefs::kGeolocationContentSettings); |
| + for (DictionaryValue::key_iterator i = |
| + geolocation_settings->begin_keys(); |
| + i != geolocation_settings->end_keys(); |
| + ++i) { |
| + const std::string& primary_key(*i); |
| + GURL primary_url(primary_key); |
| + DCHECK(primary_url.is_valid()); |
| + |
| + DictionaryValue* requesting_origin_settings = NULL; |
| + bool found = geolocation_settings->GetDictionaryWithoutPathExpansion( |
| + primary_key, &requesting_origin_settings); |
| + DCHECK(found); |
| + |
| + for (DictionaryValue::key_iterator j = |
| + requesting_origin_settings->begin_keys(); |
| + j != requesting_origin_settings->end_keys(); |
| + ++j) { |
| + const std::string& secondary_key(*j); |
| + GURL secondary_url(secondary_key); |
| + DCHECK(secondary_url.is_valid()); |
| + |
| + int setting_value; |
| + found = requesting_origin_settings->GetIntegerWithoutPathExpansion( |
| + secondary_key, &setting_value); |
| + DCHECK(found); |
| + |
| + ContentSettingsPattern primary_pattern = |
| + ContentSettingsPattern::FromURLNoWildcard(primary_url); |
| + ContentSettingsPattern secondary_pattern = |
| + ContentSettingsPattern::FromURLNoWildcard(secondary_url); |
| + DCHECK(primary_pattern.IsValid() && secondary_pattern.IsValid()); |
| + |
| + SetContentSetting(primary_pattern, |
| + secondary_pattern, |
| + CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| + std::string(), |
| + IntToContentSetting(setting_value)); |
| + } |
| + } |
| +} |
| + |
| +void PrefProvider::SyncObsoleteGeolocationPref() { |
| + DCHECK(prefs_); |
| + DCHECK(prefs_->HasPrefPath(prefs::kContentSettingsPatternPairs)); |
| + |
| + { |
|
Bernhard Bauer
2011/08/09 14:09:45
Nit: No need for the additional scope.
markusheintz_
2011/08/09 14:40:49
Done.
Bernhard Bauer
2011/08/09 14:57:33
Um, no. Scope, not empty line :-)
markusheintz_
2011/08/10 09:33:05
Uups ... I guess I just read "nit" and stopped rea
Bernhard Bauer
2011/08/10 10:40:03
:-D
|
| + DictionaryPrefUpdate update(prefs_, prefs::kGeolocationContentSettings); |
| + DictionaryValue* obsolete_geolocation_settings = update.Get(); |
| + obsolete_geolocation_settings->Clear(); |
| + |
| + const DictionaryValue* pattern_pairs_dictionary = |
| + prefs_->GetDictionary(prefs::kContentSettingsPatternPairs); |
| + for (DictionaryValue::key_iterator i = |
| + pattern_pairs_dictionary->begin_keys(); |
| + i != pattern_pairs_dictionary->end_keys(); |
| + ++i) { |
| + const std::string& key(*i); |
| + std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair = |
| + ParsePatternString(key); |
| + DCHECK(pattern_pair.first.IsValid() && pattern_pair.second.IsValid()); |
| + |
| + DictionaryValue* settings_dictionary = NULL; |
| + bool found = pattern_pairs_dictionary->GetDictionaryWithoutPathExpansion( |
| + key, &settings_dictionary); |
| + DCHECK(found); |
| + |
| + if (settings_dictionary->HasKey( |
| + kTypeNames[CONTENT_SETTINGS_TYPE_GEOLOCATION])) { |
| + const GURL primary_url(pattern_pair.first.ToString()); |
| + const GURL secondary_url(pattern_pair.second.ToString()); |
| + DCHECK(primary_url.is_valid() && secondary_url.is_valid()); |
| + |
| + int setting_value; |
| + settings_dictionary->GetInteger( |
| + kTypeNames[CONTENT_SETTINGS_TYPE_GEOLOCATION], &setting_value); |
| + |
| + // Add to obsolete pref. |
| + DictionaryValue* one_origin_settings = NULL; |
| + bool found = |
| + obsolete_geolocation_settings->GetDictionaryWithoutPathExpansion( |
| + primary_url.spec(), &one_origin_settings); |
| + if (!found) { |
| + one_origin_settings = new DictionaryValue(); |
| + obsolete_geolocation_settings->SetWithoutPathExpansion( |
| + primary_url.spec(), one_origin_settings); |
| + } |
| + |
| + one_origin_settings->SetWithoutPathExpansion( |
| + secondary_url.spec(), Value::CreateIntegerValue(setting_value)); |
| + } |
| } |
| } |
| } |