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 <map> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/auto_reset.h" | 12 #include "base/auto_reset.h" |
13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
14 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
| 15 #include "base/scoped_ptr.h" |
15 #include "chrome/browser/content_settings/content_settings_pattern.h" | 16 #include "chrome/browser/content_settings/content_settings_pattern.h" |
16 #include "chrome/browser/content_settings/content_settings_utils.h" | 17 #include "chrome/browser/content_settings/content_settings_utils.h" |
17 #include "chrome/browser/prefs/pref_service.h" | 18 #include "chrome/browser/prefs/pref_service.h" |
18 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 19 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
19 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
20 #include "chrome/common/chrome_notification_types.h" | 21 #include "chrome/common/chrome_notification_types.h" |
21 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
22 #include "chrome/common/content_settings.h" | 23 #include "chrome/common/content_settings.h" |
23 #include "chrome/common/pref_names.h" | 24 #include "chrome/common/pref_names.h" |
24 #include "content/browser/browser_thread.h" | 25 #include "content/browser/browser_thread.h" |
25 #include "content/browser/user_metrics.h" | 26 #include "content/browser/user_metrics.h" |
26 #include "content/common/notification_service.h" | 27 #include "content/common/notification_service.h" |
27 #include "content/common/notification_source.h" | 28 #include "content/common/notification_source.h" |
28 #include "googleurl/src/gurl.h" | 29 #include "googleurl/src/gurl.h" |
29 #include "net/base/net_util.h" | 30 #include "net/base/net_util.h" |
30 | 31 |
31 namespace { | 32 namespace { |
32 | 33 |
| 34 typedef std::pair<std::string, std::string> StringPair; |
| 35 typedef std::map<std::string, std::string> StringMap; |
| 36 |
33 // The preference keys where resource identifiers are stored for | 37 // The preference keys where resource identifiers are stored for |
34 // ContentSettingsType values that support resource identifiers. | 38 // ContentSettingsType values that support resource identifiers. |
35 const char* kResourceTypeNames[] = { | 39 const char* kResourceTypeNames[] = { |
36 NULL, | 40 NULL, |
37 NULL, | 41 NULL, |
38 NULL, | 42 NULL, |
39 "per_plugin", | 43 "per_plugin", |
40 NULL, | 44 NULL, |
41 NULL, // Not used for Geolocation | 45 NULL, // Not used for Geolocation |
42 NULL, // Not used for Notifications | 46 NULL, // Not used for Notifications |
(...skipping 914 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
957 CONTENT_SETTINGS_TYPE_POPUPS, | 961 CONTENT_SETTINGS_TYPE_POPUPS, |
958 "", | 962 "", |
959 CONTENT_SETTING_ALLOW); | 963 CONTENT_SETTING_ALLOW); |
960 } | 964 } |
961 prefs_->ClearPref(prefs::kPopupWhitelistedHosts); | 965 prefs_->ClearPref(prefs::kPopupWhitelistedHosts); |
962 } | 966 } |
963 } | 967 } |
964 | 968 |
965 void PrefProvider::MigrateObsoleteContentSettingsPatternPref() { | 969 void PrefProvider::MigrateObsoleteContentSettingsPatternPref() { |
966 if (prefs_->HasPrefPath(prefs::kContentSettingsPatterns) && !is_incognito_) { | 970 if (prefs_->HasPrefPath(prefs::kContentSettingsPatterns) && !is_incognito_) { |
967 const DictionaryValue* all_settings_dictionary = | 971 const DictionaryValue* patterns_dictionary = |
968 prefs_->GetDictionary(prefs::kContentSettingsPatterns); | 972 prefs_->GetDictionary(prefs::kContentSettingsPatterns); |
969 | 973 |
970 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs); | 974 // A map with an old key, new key mapping. If the new key is empty then the |
971 DictionaryValue* exceptions_dictionary; | 975 // value for the old key will be removed. |
972 exceptions_dictionary = update.Get(); | 976 StringMap keys_to_change; |
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 | 977 |
980 // Validate pattern string and skip it if it is invalid. | 978 { |
981 std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair = | 979 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs); |
982 ParsePatternString(key); | 980 DictionaryValue* pattern_pairs_dictionary = update.Get(); |
983 const ContentSettingsPattern& primary_pattern = pattern_pair.first; | 981 for (DictionaryValue::key_iterator i( |
984 if (!primary_pattern.IsValid()) { | 982 patterns_dictionary->begin_keys()); |
985 LOG(DFATAL) << "Invalid pattern strings: " << key; | 983 i != patterns_dictionary->end_keys(); |
986 continue; | 984 ++i) { |
| 985 const std::string& key(*i); |
| 986 // Remove broken pattern keys and fix keys with pattern pairs. |
| 987 size_t sep_pos = key.find(","); |
| 988 ContentSettingsPattern pattern = |
| 989 ContentSettingsPattern::FromString(key.substr(0, sep_pos)); |
| 990 |
| 991 // Save the key if it contains a invalid patterns to remove it later. |
| 992 // Continue and don't try to migrate the broken pattern key. |
| 993 if (!pattern.IsValid()) { |
| 994 keys_to_change[key] = ""; |
| 995 continue; |
| 996 } |
| 997 |
| 998 // If the key contains a pattern pair, then remove the secondary |
| 999 // pattern from the key. |
| 1000 if (sep_pos != std::string::npos) { |
| 1001 // If the dictionary already has a key that equals the primary pattern |
| 1002 // of the corrupted pattern pair key, don't fix the key but remove it. |
| 1003 if (patterns_dictionary->HasKey(pattern.ToString())) { |
| 1004 keys_to_change[key] = ""; |
| 1005 continue; |
| 1006 } |
| 1007 |
| 1008 // If there is more than one key with a pattern pair that has the same |
| 1009 // valid primary pattern, then the value of the last key processed |
| 1010 // will win and overwrite the value any previous key. |
| 1011 keys_to_change[key] = pattern.ToString(); |
| 1012 } |
| 1013 |
| 1014 // Copy dictionary value. |
| 1015 DictionaryValue* dictionary = NULL; |
| 1016 bool found = patterns_dictionary->GetDictionaryWithoutPathExpansion( |
| 1017 key, &dictionary); |
| 1018 DCHECK(found); |
| 1019 std::string new_key = CreatePatternString( |
| 1020 pattern, ContentSettingsPattern::Wildcard()); |
| 1021 // Existing values are overwritten. |
| 1022 pattern_pairs_dictionary->SetWithoutPathExpansion( |
| 1023 new_key, dictionary->DeepCopy()); |
987 } | 1024 } |
| 1025 } |
988 | 1026 |
989 // Copy dictionary value. | 1027 { |
990 // Get old settings. | 1028 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatterns); |
991 DictionaryValue* dictionary = NULL; | 1029 DictionaryValue* mutable_patterns_dictionary = update.Get(); |
992 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( | 1030 // Fix broken pattern strings. |
993 key, &dictionary); | 1031 for (StringMap::iterator i(keys_to_change.begin()); |
994 DCHECK(found); | 1032 i != keys_to_change.end(); |
995 | 1033 ++i) { |
996 // Create new dictionary key. | 1034 const StringPair& pattern_str_pair(*i); |
997 std::string new_pattern_str = CreatePatternString( | 1035 Value* dict_ptr = NULL; |
998 primary_pattern, ContentSettingsPattern::Wildcard()); | 1036 bool found = mutable_patterns_dictionary->RemoveWithoutPathExpansion( |
999 | 1037 pattern_str_pair.first, &dict_ptr); |
1000 // Existing values are overwritten. | 1038 scoped_ptr<Value> dict(dict_ptr); |
1001 exceptions_dictionary->SetWithoutPathExpansion( | 1039 DCHECK(found); |
1002 new_pattern_str, dictionary->DeepCopy()); | 1040 if (!pattern_str_pair.second.empty()) { |
| 1041 mutable_patterns_dictionary->SetWithoutPathExpansion( |
| 1042 pattern_str_pair.second, dict.release()); |
| 1043 } |
| 1044 } |
1003 } | 1045 } |
1004 } | 1046 } |
1005 } | 1047 } |
1006 | 1048 |
1007 void PrefProvider::SyncObsoletePref() { | 1049 void PrefProvider::SyncObsoletePref() { |
1008 AutoReset<bool> auto_reset(&updating_preferences_, true); | 1050 AutoReset<bool> auto_reset(&updating_preferences_, true); |
1009 if (prefs_->HasPrefPath(prefs::kContentSettingsPatternPairs) && | 1051 if (prefs_->HasPrefPath(prefs::kContentSettingsPatternPairs) && |
1010 !is_incognito_) { | 1052 !is_incognito_) { |
1011 const DictionaryValue* pattern_pairs_dictionary = | 1053 const DictionaryValue* pattern_pairs_dictionary = |
1012 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs); | 1054 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs); |
(...skipping 21 matching lines...) Expand all Loading... |
1034 DCHECK(found); | 1076 DCHECK(found); |
1035 std::string new_key = pattern_pair.first.ToString(); | 1077 std::string new_key = pattern_pair.first.ToString(); |
1036 // Existing values are overwritten. | 1078 // Existing values are overwritten. |
1037 obsolete_settings_dictionary->SetWithoutPathExpansion( | 1079 obsolete_settings_dictionary->SetWithoutPathExpansion( |
1038 new_key, dictionary->DeepCopy()); | 1080 new_key, dictionary->DeepCopy()); |
1039 } | 1081 } |
1040 } | 1082 } |
1041 } | 1083 } |
1042 | 1084 |
1043 } // namespace content_settings | 1085 } // namespace content_settings |
OLD | NEW |