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

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;
983 const ContentSettingsPattern& primary_pattern = pattern_pair.first; 979 exceptions_dictionary = update.Get();
Bernhard Bauer 2011/08/02 11:32:32 Declare |exceptions_dictionary| in this line?
markusheintz_ 2011/08/02 12:24:53 Absolutely.
984 if (!primary_pattern.IsValid()) { 980 for (DictionaryValue::key_iterator i(
985 LOG(DFATAL) << "Invalid pattern strings: " << key; 981 all_settings_dictionary->begin_keys());
986 continue; 982 i != all_settings_dictionary->end_keys();
983 ++i) {
984 const std::string& key(*i);
985 // Validate pattern string and skip it if it is invalid.
986 std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair =
987 ParsePatternString(key);
988
989 // Broken patterns will be removed
990 if (!pattern_pair.first.IsValid()) {
991 keys_to_remove.push_back(key);
992 continue;
993 }
994
995 // Fix key with pattern pairs.
996 // TODO(markusheintz): Delete the "!pattern_pair.second.IsValid()".
Bernhard Bauer 2011/08/02 11:32:32 ?
markusheintz_ 2011/08/02 12:24:53 Removed
997 if (pattern_pair.first.IsValid() &&
998 (!pattern_pair.second.IsValid() ||
999 !(pattern_pair.second == ContentSettingsPattern::Wildcard()))) {
Bernhard Bauer 2011/08/02 11:32:32 |pattern_pair.second != ContentSettingsPattern::Wi
markusheintz_ 2011/08/02 12:24:53 ContentSettingsPattern does not have a != operator
1000 // If the dictionary has a non corrupted key that equals the primary
1001 // key of a corrupted pattern pair key, don't fix the key but remove
1002 // it.
1003 if (all_settings_dictionary->HasKey(
Bernhard Bauer 2011/08/02 11:32:32 Can you fit this on one line?
markusheintz_ 2011/08/02 12:24:53 yep. Done.
1004 pattern_pair.first.ToString())) {
1005 keys_to_remove.push_back(key);
1006 continue;
1007 }
1008
1009 // If there is more than one key with a pattern pair the has the same
1010 // valid primary key, then the value of the last key will overwrite
1011 // the valule of previous keys.
Bernhard Bauer 2011/08/02 11:32:32 Nit: "value"
markusheintz_ 2011/08/02 12:24:53 Done.
1012 keys_to_swap.push_back(std::make_pair(
1013 key, pattern_pair.first.ToString()));
1014 }
1015
1016 // Copy dictionary value.
1017 DictionaryValue* dictionary = NULL;
1018 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion(
1019 key, &dictionary);
1020 DCHECK(found);
1021 std::string new_key = CreatePatternString(
1022 pattern_pair.first, ContentSettingsPattern::Wildcard());
1023 // Existing values are overwritten.
1024 exceptions_dictionary->SetWithoutPathExpansion(
1025 new_key, dictionary->DeepCopy());
1026 }
1027 }
1028
1029 {
1030 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatterns);
1031 DictionaryValue* mutable_patterns_dictionary = update.Get();
1032 // Fix broken pattern strings.
1033 for (std::list<StringPair>::iterator i(keys_to_swap.begin());
Bernhard Bauer 2011/08/02 11:32:32 Why can't you do this directly in the loop above?
markusheintz_ 2011/08/02 12:24:53 Last time I tried that the iterator didn't like it
Bernhard Bauer 2011/08/02 12:36:40 Ah, right. I think you could still copy the new v
Bernhard Bauer 2011/08/03 08:33:33 What about this one?
markusheintz_ 2011/08/03 11:23:41 I shouldn't add new keys while I'm iterating over
1034 i != keys_to_swap.end();
1035 ++i) {
1036 const StringPair& pattern_str_pair(*i);
1037 Value* dict;
1038 bool found = mutable_patterns_dictionary->RemoveWithoutPathExpansion(
1039 pattern_str_pair.first, &dict);
Bernhard Bauer 2011/08/02 11:32:32 Nit: indent
markusheintz_ 2011/08/02 12:24:53 Done.
1040 DCHECK(found);
1041 mutable_patterns_dictionary->SetWithoutPathExpansion(
1042 pattern_str_pair.second, dict);
987 } 1043 }
988 1044
989 // Copy dictionary value. 1045 // Remove settings for invalid pattern strings (keys).
990 // Get old settings. 1046 for (std::list<std::string>::iterator i(keys_to_remove.begin());
991 DictionaryValue* dictionary = NULL; 1047 i != keys_to_remove.end();
992 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( 1048 ++i) {
993 key, &dictionary); 1049 const std::string& key(*i);
994 DCHECK(found); 1050 bool found =
995 1051 mutable_patterns_dictionary->RemoveWithoutPathExpansion(key, NULL);
996 // Create new dictionary key. 1052 DCHECK(found);
997 std::string new_pattern_str = CreatePatternString( 1053 }
998 primary_pattern, ContentSettingsPattern::Wildcard());
999
1000 // Existing values are overwritten.
1001 exceptions_dictionary->SetWithoutPathExpansion(
1002 new_pattern_str, dictionary->DeepCopy());
1003 } 1054 }
1004 } 1055 }
1005 } 1056 }
1006 1057
1007 void PrefProvider::SyncObsoletePref() { 1058 void PrefProvider::SyncObsoletePref() {
1008 AutoReset<bool> auto_reset(&updating_preferences_, true); 1059 AutoReset<bool> auto_reset(&updating_preferences_, true);
1009 if (prefs_->HasPrefPath(prefs::kContentSettingsPatternPairs) && 1060 if (prefs_->HasPrefPath(prefs::kContentSettingsPatternPairs) &&
1010 !is_incognito_) { 1061 !is_incognito_) {
1011 const DictionaryValue* pattern_pairs_dictionary = 1062 const DictionaryValue* pattern_pairs_dictionary =
1012 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs); 1063 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs);
(...skipping 21 matching lines...) Expand all
1034 DCHECK(found); 1085 DCHECK(found);
1035 std::string new_key = pattern_pair.first.ToString(); 1086 std::string new_key = pattern_pair.first.ToString();
1036 // Existing values are overwritten. 1087 // Existing values are overwritten.
1037 obsolete_settings_dictionary->SetWithoutPathExpansion( 1088 obsolete_settings_dictionary->SetWithoutPathExpansion(
1038 new_key, dictionary->DeepCopy()); 1089 new_key, dictionary->DeepCopy());
1039 } 1090 }
1040 } 1091 }
1041 } 1092 }
1042 1093
1043 } // namespace content_settings 1094 } // 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