Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/content_settings/content_settings_pref_provider.h" | 5 #include "chrome/browser/content_settings/content_settings_pref_provider.h" |
| 6 | 6 |
| 7 #include <list> | 7 #include <list> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
| 24 #include "content/browser/browser_thread.h" | 24 #include "content/browser/browser_thread.h" |
| 25 #include "content/browser/user_metrics.h" | 25 #include "content/browser/user_metrics.h" |
| 26 #include "content/common/notification_service.h" | 26 #include "content/common/notification_service.h" |
| 27 #include "content/common/notification_source.h" | 27 #include "content/common/notification_source.h" |
| 28 #include "googleurl/src/gurl.h" | 28 #include "googleurl/src/gurl.h" |
| 29 #include "net/base/net_util.h" | 29 #include "net/base/net_util.h" |
| 30 | 30 |
| 31 namespace { | 31 namespace { |
| 32 | 32 |
| 33 typedef std::pair<std::string, std::string> StringPair; | |
| 34 | |
| 33 // The preference keys where resource identifiers are stored for | 35 // The preference keys where resource identifiers are stored for |
| 34 // ContentSettingsType values that support resource identifiers. | 36 // ContentSettingsType values that support resource identifiers. |
| 35 const char* kResourceTypeNames[] = { | 37 const char* kResourceTypeNames[] = { |
| 36 NULL, | 38 NULL, |
| 37 NULL, | 39 NULL, |
| 38 NULL, | 40 NULL, |
| 39 "per_plugin", | 41 "per_plugin", |
| 40 NULL, | 42 NULL, |
| 41 NULL, // Not used for Geolocation | 43 NULL, // Not used for Geolocation |
| 42 NULL, // Not used for Notifications | 44 NULL, // Not used for Notifications |
| (...skipping 914 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 957 CONTENT_SETTINGS_TYPE_POPUPS, | 959 CONTENT_SETTINGS_TYPE_POPUPS, |
| 958 "", | 960 "", |
| 959 CONTENT_SETTING_ALLOW); | 961 CONTENT_SETTING_ALLOW); |
| 960 } | 962 } |
| 961 prefs_->ClearPref(prefs::kPopupWhitelistedHosts); | 963 prefs_->ClearPref(prefs::kPopupWhitelistedHosts); |
| 962 } | 964 } |
| 963 } | 965 } |
| 964 | 966 |
| 965 void PrefProvider::MigrateObsoleteContentSettingsPatternPref() { | 967 void PrefProvider::MigrateObsoleteContentSettingsPatternPref() { |
| 966 if (prefs_->HasPrefPath(prefs::kContentSettingsPatterns) && !is_incognito_) { | 968 if (prefs_->HasPrefPath(prefs::kContentSettingsPatterns) && !is_incognito_) { |
| 967 const DictionaryValue* all_settings_dictionary = | 969 const DictionaryValue* patterns_dictionary = |
| 968 prefs_->GetDictionary(prefs::kContentSettingsPatterns); | 970 prefs_->GetDictionary(prefs::kContentSettingsPatterns); |
| 969 | 971 |
| 970 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs); | 972 // A list with invalid keys that will be removed. |
| 971 DictionaryValue* exceptions_dictionary; | 973 std::list<std::string> keys_to_remove; |
| 972 exceptions_dictionary = update.Get(); | 974 std::list<StringPair> keys_to_swap; |
| 973 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys()); | |
| 974 i != all_settings_dictionary->end_keys(); | |
| 975 ++i) { | |
| 976 const std::string& key(*i); | |
| 977 if (key.empty()) | |
| 978 continue; | |
| 979 | 975 |
| 980 // Validate pattern string and skip it if it is invalid. | 976 { |
| 981 std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair = | 977 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs); |
| 982 ParsePatternString(key); | 978 DictionaryValue* pattern_pairs_dictionary = update.Get(); |
| 983 const ContentSettingsPattern& primary_pattern = pattern_pair.first; | 979 for (DictionaryValue::key_iterator i( |
| 984 if (!primary_pattern.IsValid()) { | 980 patterns_dictionary->begin_keys()); |
| 985 LOG(DFATAL) << "Invalid pattern strings: " << key; | 981 i != patterns_dictionary->end_keys(); |
| 986 continue; | 982 ++i) { |
| 983 const std::string& key(*i); | |
| 984 // Remove broken pattern keys and fix keys with pattern pairs. | |
| 985 size_t sep_pos = key.find(","); | |
| 986 ContentSettingsPattern primary_pattern; | |
| 987 if (sep_pos != std::string::npos) { | |
| 988 primary_pattern = | |
| 989 ContentSettingsPattern::FromString(key.substr(0, sep_pos)); | |
| 990 } else { | |
| 991 primary_pattern = ContentSettingsPattern::FromString(key); | |
|
Bernhard Bauer
2011/08/03 14:53:41
You could actually simplify this to always use |ke
markusheintz_
2011/08/03 21:42:28
Done.
| |
| 992 } | |
| 993 | |
| 994 // Save the key if it contains a invalid patterns to remove it later. | |
| 995 // Continue and don't try to migrate the broken pattern key. | |
| 996 if (!primary_pattern.IsValid()) { | |
| 997 keys_to_remove.push_back(key); | |
| 998 continue; | |
| 999 } | |
| 1000 | |
| 1001 // If the key contains a pattern pair, then remove the secondary | |
| 1002 // pattern from the key. | |
| 1003 if (sep_pos != std::string::npos) { | |
| 1004 // If the dictionary already has a key that equals the primary pattern | |
| 1005 // of the corrupted pattern pair key, don't fix the key but remove it. | |
| 1006 if (patterns_dictionary->HasKey(primary_pattern.ToString())) { | |
| 1007 keys_to_remove.push_back(key); | |
| 1008 continue; | |
| 1009 } | |
| 1010 | |
| 1011 // If there is more than one key with a pattern pair that has the same | |
| 1012 // valid primary pattern, then the value of the last key processed | |
| 1013 // will win and overwrite the value any previous key. | |
| 1014 keys_to_swap.push_back(std::make_pair(key, | |
| 1015 primary_pattern.ToString())); | |
| 1016 } | |
| 1017 | |
| 1018 // Copy dictionary value. | |
| 1019 DictionaryValue* dictionary = NULL; | |
| 1020 bool found = patterns_dictionary->GetDictionaryWithoutPathExpansion( | |
| 1021 key, &dictionary); | |
| 1022 DCHECK(found); | |
| 1023 std::string new_key = CreatePatternString( | |
| 1024 primary_pattern, ContentSettingsPattern::Wildcard()); | |
| 1025 // Existing values are overwritten. | |
| 1026 pattern_pairs_dictionary->SetWithoutPathExpansion( | |
| 1027 new_key, dictionary->DeepCopy()); | |
| 1028 } | |
| 1029 } | |
| 1030 | |
| 1031 { | |
| 1032 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatterns); | |
| 1033 DictionaryValue* mutable_patterns_dictionary = update.Get(); | |
| 1034 // Fix broken pattern strings. | |
| 1035 for (std::list<StringPair>::iterator i(keys_to_swap.begin()); | |
| 1036 i != keys_to_swap.end(); | |
| 1037 ++i) { | |
| 1038 const StringPair& pattern_str_pair(*i); | |
| 1039 Value* dict; | |
| 1040 bool found = mutable_patterns_dictionary->RemoveWithoutPathExpansion( | |
| 1041 pattern_str_pair.first, &dict); | |
| 1042 DCHECK(found); | |
| 1043 mutable_patterns_dictionary->SetWithoutPathExpansion( | |
| 1044 pattern_str_pair.second, dict); | |
| 987 } | 1045 } |
| 988 | 1046 |
| 989 // Copy dictionary value. | 1047 // Remove settings for invalid pattern strings (keys). |
| 990 // Get old settings. | 1048 for (std::list<std::string>::iterator i(keys_to_remove.begin()); |
| 991 DictionaryValue* dictionary = NULL; | 1049 i != keys_to_remove.end(); |
| 992 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( | 1050 ++i) { |
| 993 key, &dictionary); | 1051 const std::string& key(*i); |
| 994 DCHECK(found); | 1052 bool found = |
| 995 | 1053 mutable_patterns_dictionary->RemoveWithoutPathExpansion(key, NULL); |
| 996 // Create new dictionary key. | 1054 DCHECK(found); |
| 997 std::string new_pattern_str = CreatePatternString( | 1055 } |
| 998 primary_pattern, ContentSettingsPattern::Wildcard()); | |
| 999 | |
| 1000 // Existing values are overwritten. | |
| 1001 exceptions_dictionary->SetWithoutPathExpansion( | |
| 1002 new_pattern_str, dictionary->DeepCopy()); | |
| 1003 } | 1056 } |
| 1004 } | 1057 } |
| 1005 } | 1058 } |
| 1006 | 1059 |
| 1007 void PrefProvider::SyncObsoletePref() { | 1060 void PrefProvider::SyncObsoletePref() { |
| 1008 AutoReset<bool> auto_reset(&updating_preferences_, true); | 1061 AutoReset<bool> auto_reset(&updating_preferences_, true); |
| 1009 if (prefs_->HasPrefPath(prefs::kContentSettingsPatternPairs) && | 1062 if (prefs_->HasPrefPath(prefs::kContentSettingsPatternPairs) && |
| 1010 !is_incognito_) { | 1063 !is_incognito_) { |
| 1011 const DictionaryValue* pattern_pairs_dictionary = | 1064 const DictionaryValue* pattern_pairs_dictionary = |
| 1012 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs); | 1065 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 1034 DCHECK(found); | 1087 DCHECK(found); |
| 1035 std::string new_key = pattern_pair.first.ToString(); | 1088 std::string new_key = pattern_pair.first.ToString(); |
| 1036 // Existing values are overwritten. | 1089 // Existing values are overwritten. |
| 1037 obsolete_settings_dictionary->SetWithoutPathExpansion( | 1090 obsolete_settings_dictionary->SetWithoutPathExpansion( |
| 1038 new_key, dictionary->DeepCopy()); | 1091 new_key, dictionary->DeepCopy()); |
| 1039 } | 1092 } |
| 1040 } | 1093 } |
| 1041 } | 1094 } |
| 1042 | 1095 |
| 1043 } // namespace content_settings | 1096 } // namespace content_settings |
| OLD | NEW |