Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(262)

Side by Side Diff: chrome/browser/content_settings/content_settings_pref_provider.cc

Issue 7529025: fix partially broken patterns, delete totally brocken patterns (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: " Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/content_settings/content_settings_pref_provider_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 917 matching lines...) Expand 10 before | Expand all | Expand 10 after
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* all_settings_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* exceptions_dictionary = update.Get();
983 const ContentSettingsPattern& primary_pattern = pattern_pair.first; 979 for (DictionaryValue::key_iterator i(
984 if (!primary_pattern.IsValid()) { 980 all_settings_dictionary->begin_keys());
985 LOG(DFATAL) << "Invalid pattern strings: " << key; 981 i != all_settings_dictionary->end_keys();
986 continue; 982 ++i) {
983 const std::string& key(*i);
984 // Validate pattern string and skip it if it is invalid.
985 std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair =
986 ParsePatternString(key);
987
988 // Broken patterns will be removed
989 if (!pattern_pair.first.IsValid()) {
990 keys_to_remove.push_back(key);
991 continue;
992 }
993
994 // Fix key with pattern pairs.
995 if (pattern_pair.first.IsValid() &&
996 (!pattern_pair.second.IsValid() ||
997 !(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!
998 // If the dictionary has a non corrupted key that equals the primary
999 // key of a corrupted pattern pair key, don't fix the key but remove
1000 // it.
1001 if (all_settings_dictionary->HasKey(pattern_pair.first.ToString())) {
1002 keys_to_remove.push_back(key);
1003 continue;
1004 }
1005
1006 // If there is more than one key with a pattern pair the has the same
1007 // valid primary key, then the value of the last key will overwrite
1008 // the value of previous keys.
1009 keys_to_swap.push_back(std::make_pair(
1010 key, pattern_pair.first.ToString()));
1011 }
1012
1013 // Copy dictionary value.
1014 DictionaryValue* dictionary = NULL;
1015 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion(
1016 key, &dictionary);
1017 DCHECK(found);
1018 std::string new_key = CreatePatternString(
1019 pattern_pair.first, ContentSettingsPattern::Wildcard());
1020 // Existing values are overwritten.
1021 exceptions_dictionary->SetWithoutPathExpansion(
1022 new_key, dictionary->DeepCopy());
1023 }
1024 }
1025
1026 {
1027 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatterns);
1028 DictionaryValue* mutable_patterns_dictionary = update.Get();
1029 // Fix broken pattern strings.
1030 for (std::list<StringPair>::iterator i(keys_to_swap.begin());
1031 i != keys_to_swap.end();
1032 ++i) {
1033 const StringPair& pattern_str_pair(*i);
1034 Value* dict;
1035 bool found = mutable_patterns_dictionary->RemoveWithoutPathExpansion(
1036 pattern_str_pair.first, &dict);
1037 DCHECK(found);
1038 mutable_patterns_dictionary->SetWithoutPathExpansion(
1039 pattern_str_pair.second, dict);
987 } 1040 }
988 1041
989 // Copy dictionary value. 1042 // Remove settings for invalid pattern strings (keys).
990 // Get old settings. 1043 for (std::list<std::string>::iterator i(keys_to_remove.begin());
991 DictionaryValue* dictionary = NULL; 1044 i != keys_to_remove.end();
992 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( 1045 ++i) {
993 key, &dictionary); 1046 const std::string& key(*i);
994 DCHECK(found); 1047 bool found =
995 1048 mutable_patterns_dictionary->RemoveWithoutPathExpansion(key, NULL);
996 // Create new dictionary key. 1049 DCHECK(found);
997 std::string new_pattern_str = CreatePatternString( 1050 }
998 primary_pattern, ContentSettingsPattern::Wildcard());
999
1000 // Existing values are overwritten.
1001 exceptions_dictionary->SetWithoutPathExpansion(
1002 new_pattern_str, dictionary->DeepCopy());
1003 } 1051 }
1004 } 1052 }
1005 } 1053 }
1006 1054
1007 void PrefProvider::SyncObsoletePref() { 1055 void PrefProvider::SyncObsoletePref() {
1008 AutoReset<bool> auto_reset(&updating_preferences_, true); 1056 AutoReset<bool> auto_reset(&updating_preferences_, true);
1009 if (prefs_->HasPrefPath(prefs::kContentSettingsPatternPairs) && 1057 if (prefs_->HasPrefPath(prefs::kContentSettingsPatternPairs) &&
1010 !is_incognito_) { 1058 !is_incognito_) {
1011 const DictionaryValue* pattern_pairs_dictionary = 1059 const DictionaryValue* pattern_pairs_dictionary =
1012 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs); 1060 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs);
(...skipping 21 matching lines...) Expand all
1034 DCHECK(found); 1082 DCHECK(found);
1035 std::string new_key = pattern_pair.first.ToString(); 1083 std::string new_key = pattern_pair.first.ToString();
1036 // Existing values are overwritten. 1084 // Existing values are overwritten.
1037 obsolete_settings_dictionary->SetWithoutPathExpansion( 1085 obsolete_settings_dictionary->SetWithoutPathExpansion(
1038 new_key, dictionary->DeepCopy()); 1086 new_key, dictionary->DeepCopy());
1039 } 1087 }
1040 } 1088 }
1041 } 1089 }
1042 1090
1043 } // namespace content_settings 1091 } // namespace content_settings
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/content_settings/content_settings_pref_provider_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698