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..375695fe9e159908e276806f512886982902e8b6 100644 |
--- a/chrome/browser/content_settings/content_settings_pref_provider.cc |
+++ b/chrome/browser/content_settings/content_settings_pref_provider.cc |
@@ -30,6 +30,8 @@ |
namespace { |
+typedef std::pair<std::string, std::string> StringPair; |
+ |
// The preference keys where resource identifiers are stored for |
// ContentSettingsType values that support resource identifiers. |
const char* kResourceTypeNames[] = { |
@@ -967,39 +969,85 @@ void PrefProvider::MigrateObsoleteContentSettingsPatternPref() { |
const DictionaryValue* all_settings_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(); |
- ++i) { |
- const std::string& key(*i); |
- if (key.empty()) |
- continue; |
+ // A list with invalid keys that will be removed. |
+ std::list<std::string> keys_to_remove; |
+ std::list<StringPair> keys_to_swap; |
- // Validate pattern string and skip it if it is invalid. |
- std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair = |
- ParsePatternString(key); |
- const ContentSettingsPattern& primary_pattern = pattern_pair.first; |
- if (!primary_pattern.IsValid()) { |
- LOG(DFATAL) << "Invalid pattern strings: " << key; |
- continue; |
- } |
+ { |
+ DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs); |
+ DictionaryValue* exceptions_dictionary = update.Get(); |
+ for (DictionaryValue::key_iterator i( |
+ all_settings_dictionary->begin_keys()); |
+ i != all_settings_dictionary->end_keys(); |
+ ++i) { |
+ const std::string& key(*i); |
+ // Validate pattern string and skip it if it is invalid. |
+ std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair = |
+ ParsePatternString(key); |
+ |
+ // Broken patterns will be removed |
+ if (!pattern_pair.first.IsValid()) { |
+ keys_to_remove.push_back(key); |
+ continue; |
+ } |
- // Copy dictionary value. |
- // Get old settings. |
- DictionaryValue* dictionary = NULL; |
- bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
- key, &dictionary); |
- DCHECK(found); |
+ // Fix key with pattern pairs. |
+ if (pattern_pair.first.IsValid() && |
+ (!pattern_pair.second.IsValid() || |
+ !(pattern_pair.second == ContentSettingsPattern::Wildcard()))) { |
Bernhard Bauer
2011/08/02 12:36:40
Also, an invalid pattern shouldn't compare equal t
markusheintz_
2011/08/02 13:02:56
Yep!
|
+ // If the dictionary has a non corrupted key that equals the primary |
+ // key of a corrupted pattern pair key, don't fix the key but remove |
+ // it. |
+ if (all_settings_dictionary->HasKey(pattern_pair.first.ToString())) { |
+ keys_to_remove.push_back(key); |
+ continue; |
+ } |
- // Create new dictionary key. |
- std::string new_pattern_str = CreatePatternString( |
- primary_pattern, ContentSettingsPattern::Wildcard()); |
+ // If there is more than one key with a pattern pair the has the same |
+ // valid primary key, then the value of the last key will overwrite |
+ // the value of previous keys. |
+ keys_to_swap.push_back(std::make_pair( |
+ key, pattern_pair.first.ToString())); |
+ } |
- // Existing values are overwritten. |
- exceptions_dictionary->SetWithoutPathExpansion( |
- new_pattern_str, dictionary->DeepCopy()); |
+ // Copy dictionary value. |
+ DictionaryValue* dictionary = NULL; |
+ bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
+ key, &dictionary); |
+ DCHECK(found); |
+ std::string new_key = CreatePatternString( |
+ pattern_pair.first, ContentSettingsPattern::Wildcard()); |
+ // Existing values are overwritten. |
+ exceptions_dictionary->SetWithoutPathExpansion( |
+ new_key, dictionary->DeepCopy()); |
+ } |
+ } |
+ |
+ { |
+ DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatterns); |
+ DictionaryValue* mutable_patterns_dictionary = update.Get(); |
+ // Fix broken pattern strings. |
+ for (std::list<StringPair>::iterator i(keys_to_swap.begin()); |
+ i != keys_to_swap.end(); |
+ ++i) { |
+ const StringPair& pattern_str_pair(*i); |
+ Value* dict; |
+ bool found = mutable_patterns_dictionary->RemoveWithoutPathExpansion( |
+ pattern_str_pair.first, &dict); |
+ DCHECK(found); |
+ mutable_patterns_dictionary->SetWithoutPathExpansion( |
+ pattern_str_pair.second, dict); |
+ } |
+ |
+ // Remove settings for invalid pattern strings (keys). |
+ for (std::list<std::string>::iterator i(keys_to_remove.begin()); |
+ i != keys_to_remove.end(); |
+ ++i) { |
+ const std::string& key(*i); |
+ bool found = |
+ mutable_patterns_dictionary->RemoveWithoutPathExpansion(key, NULL); |
+ DCHECK(found); |
+ } |
} |
} |
} |