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

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 <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 "chrome/browser/content_settings/content_settings_pattern.h" 15 #include "chrome/browser/content_settings/content_settings_pattern.h"
16 #include "chrome/browser/content_settings/content_settings_utils.h" 16 #include "chrome/browser/content_settings/content_settings_utils.h"
17 #include "chrome/browser/prefs/pref_service.h" 17 #include "chrome/browser/prefs/pref_service.h"
18 #include "chrome/browser/prefs/scoped_user_pref_update.h" 18 #include "chrome/browser/prefs/scoped_user_pref_update.h"
19 #include "chrome/browser/profiles/profile.h" 19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/common/chrome_notification_types.h" 20 #include "chrome/common/chrome_notification_types.h"
21 #include "chrome/common/chrome_switches.h" 21 #include "chrome/common/chrome_switches.h"
22 #include "chrome/common/content_settings.h" 22 #include "chrome/common/content_settings.h"
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 typedef std::map<std::string, std::string> StringMap;
35
33 // The preference keys where resource identifiers are stored for 36 // The preference keys where resource identifiers are stored for
34 // ContentSettingsType values that support resource identifiers. 37 // ContentSettingsType values that support resource identifiers.
35 const char* kResourceTypeNames[] = { 38 const char* kResourceTypeNames[] = {
36 NULL, 39 NULL,
37 NULL, 40 NULL,
38 NULL, 41 NULL,
39 "per_plugin", 42 "per_plugin",
40 NULL, 43 NULL,
41 NULL, // Not used for Geolocation 44 NULL, // Not used for Geolocation
42 NULL, // Not used for Notifications 45 NULL, // Not used for Notifications
(...skipping 914 matching lines...) Expand 10 before | Expand all | Expand 10 after
957 CONTENT_SETTINGS_TYPE_POPUPS, 960 CONTENT_SETTINGS_TYPE_POPUPS,
958 "", 961 "",
959 CONTENT_SETTING_ALLOW); 962 CONTENT_SETTING_ALLOW);
960 } 963 }
961 prefs_->ClearPref(prefs::kPopupWhitelistedHosts); 964 prefs_->ClearPref(prefs::kPopupWhitelistedHosts);
962 } 965 }
963 } 966 }
964 967
965 void PrefProvider::MigrateObsoleteContentSettingsPatternPref() { 968 void PrefProvider::MigrateObsoleteContentSettingsPatternPref() {
966 if (prefs_->HasPrefPath(prefs::kContentSettingsPatterns) && !is_incognito_) { 969 if (prefs_->HasPrefPath(prefs::kContentSettingsPatterns) && !is_incognito_) {
967 const DictionaryValue* all_settings_dictionary = 970 const DictionaryValue* patterns_dictionary =
968 prefs_->GetDictionary(prefs::kContentSettingsPatterns); 971 prefs_->GetDictionary(prefs::kContentSettingsPatterns);
969 972
970 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs); 973 // A map with an old key, new key mapping. If the new key is empty then the
971 DictionaryValue* exceptions_dictionary; 974 // value for the old key will be removed.
972 exceptions_dictionary = update.Get(); 975 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 976
980 // Validate pattern string and skip it if it is invalid. 977 {
981 std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair = 978 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs);
982 ParsePatternString(key); 979 DictionaryValue* pattern_pairs_dictionary = update.Get();
983 const ContentSettingsPattern& primary_pattern = pattern_pair.first; 980 for (DictionaryValue::key_iterator i(
984 if (!primary_pattern.IsValid()) { 981 patterns_dictionary->begin_keys());
985 LOG(DFATAL) << "Invalid pattern strings: " << key; 982 i != patterns_dictionary->end_keys();
986 continue; 983 ++i) {
984 const std::string& key(*i);
985 // Remove broken pattern keys and fix keys with pattern pairs.
986 size_t sep_pos = key.find(",");
987 ContentSettingsPattern pattern =
988 ContentSettingsPattern::FromString(key.substr(0, sep_pos));
989
990 // Save the key if it contains a invalid patterns to remove it later.
991 // Continue and don't try to migrate the broken pattern key.
992 if (!pattern.IsValid()) {
993 keys_to_change[key] = "";
994 continue;
995 }
996
997 // If the key contains a pattern pair, then remove the secondary
998 // pattern from the key.
999 if (sep_pos != std::string::npos) {
1000 // If the dictionary already has a key that equals the primary pattern
1001 // of the corrupted pattern pair key, don't fix the key but remove it.
1002 if (patterns_dictionary->HasKey(pattern.ToString())) {
1003 keys_to_change[key] = "";
1004 continue;
1005 }
1006
1007 // If there is more than one key with a pattern pair that has the same
1008 // valid primary pattern, then the value of the last key processed
1009 // will win and overwrite the value any previous key.
1010 keys_to_change[key] = pattern.ToString();
1011 }
1012
1013 // Copy dictionary value.
1014 DictionaryValue* dictionary = NULL;
1015 bool found = patterns_dictionary->GetDictionaryWithoutPathExpansion(
1016 key, &dictionary);
1017 DCHECK(found);
1018 std::string new_key = CreatePatternString(
1019 pattern, ContentSettingsPattern::Wildcard());
1020 // Existing values are overwritten.
1021 pattern_pairs_dictionary->SetWithoutPathExpansion(
1022 new_key, dictionary->DeepCopy());
987 } 1023 }
1024 }
988 1025
989 // Copy dictionary value. 1026 {
990 // Get old settings. 1027 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatterns);
991 DictionaryValue* dictionary = NULL; 1028 DictionaryValue* mutable_patterns_dictionary = update.Get();
992 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( 1029 // Fix broken pattern strings.
993 key, &dictionary); 1030 for (StringMap::iterator i(keys_to_change.begin());
994 DCHECK(found); 1031 i != keys_to_change.end();
995 1032 ++i) {
996 // Create new dictionary key. 1033 const StringPair& pattern_str_pair(*i);
997 std::string new_pattern_str = CreatePatternString( 1034 Value* dict;
Bernhard Bauer 2011/08/04 08:44:03 Initialize please :-)
markusheintz_ 2011/08/04 12:13:39 Done.
998 primary_pattern, ContentSettingsPattern::Wildcard()); 1035 bool found = mutable_patterns_dictionary->RemoveWithoutPathExpansion(
999 1036 pattern_str_pair.first, &dict);
1000 // Existing values are overwritten. 1037 DCHECK(found);
1001 exceptions_dictionary->SetWithoutPathExpansion( 1038 if (pattern_str_pair.second.empty()) {
1002 new_pattern_str, dictionary->DeepCopy()); 1039 delete dict;
Bernhard Bauer 2011/08/04 08:44:03 It sucks that we have to delete |dict| manually. R
markusheintz_ 2011/08/04 12:13:39 I totally support you on that. I guess I better p
1040 } else {
1041 mutable_patterns_dictionary->SetWithoutPathExpansion(
1042 pattern_str_pair.second, dict);
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
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
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