summaryrefslogtreecommitdiffstats
path: root/chrome/browser/host_content_settings_map.cc
diff options
context:
space:
mode:
authorcira@google.com <cira@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2010-10-15 20:27:04 +0000
committercira@google.com <cira@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2010-10-15 20:27:04 +0000
commit3cc4d00cae48dad31ac6bb2ed634c8ad5e098b37 (patch)
treed70c2b6368cc5478c5c835af8dd185d04e49dd96 /chrome/browser/host_content_settings_map.cc
parent048496265f826fc2e489a6319303e1122c3188a2 (diff)
downloadchromium_src-3cc4d00cae48dad31ac6bb2ed634c8ad5e098b37.zip
chromium_src-3cc4d00cae48dad31ac6bb2ed634c8ad5e098b37.tar.gz
chromium_src-3cc4d00cae48dad31ac6bb2ed634c8ad5e098b37.tar.bz2
Convert all exception patterns into punicode. Current matcher uses punicode entered in the omnibox and utf8 entered in the exception dialog, and they match only for ASCII urls.
This change will show punicode patterns to the user in the exception dialog. We already have that behavior if user uses Omnibox UI to specify the pattern. This CL doesn't solve the UI issue crbug.com/52629 as it still shows punycode representation of the pattern to the user. BUG=38317,52629 TEST=Open exception/images dialog, and add non-ascii pattern as an exception. Try going to that url. You should see the images on the page, and pattern you entered will be displayed as punicode in the exception dialog. Review URL: http://codereview.chromium.org/3087015 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@62784 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/host_content_settings_map.cc')
-rw-r--r--chrome/browser/host_content_settings_map.cc187
1 files changed, 135 insertions, 52 deletions
diff --git a/chrome/browser/host_content_settings_map.cc b/chrome/browser/host_content_settings_map.cc
index e20f725..5e2a78c 100644
--- a/chrome/browser/host_content_settings_map.cc
+++ b/chrome/browser/host_content_settings_map.cc
@@ -145,6 +145,26 @@ bool HostContentSettingsMap::Pattern::Matches(const GURL& url) const {
(match + pattern_.length() - kDomainWildcardLength == host.length());
}
+std::string HostContentSettingsMap::Pattern::CanonicalizePattern() const {
+ if (!IsValid()) {
+ return "";
+ }
+
+ bool starts_with_wildcard = pattern_.length() > kDomainWildcardLength &&
+ StartsWithASCII(pattern_, kDomainWildcard, false);
+
+ const std::string host(starts_with_wildcard ?
+ pattern_.substr(kDomainWildcardLength) : pattern_);
+
+ std::string canonicalized_pattern =
+ starts_with_wildcard ? kDomainWildcard : "";
+
+ url_canon::CanonHostInfo host_info;
+ canonicalized_pattern += net::CanonicalizeHost(host, &host_info);
+
+ return canonicalized_pattern;
+}
+
HostContentSettingsMap::HostContentSettingsMap(Profile* profile)
: profile_(profile),
block_third_party_cookies_(false),
@@ -152,58 +172,11 @@ HostContentSettingsMap::HostContentSettingsMap(Profile* profile)
updating_preferences_(false) {
PrefService* prefs = profile_->GetPrefs();
- // Migrate obsolete cookie pref.
- if (prefs->HasPrefPath(prefs::kCookieBehavior)) {
- int cookie_behavior = prefs->GetInteger(prefs::kCookieBehavior);
- prefs->ClearPref(prefs::kCookieBehavior);
- if (!prefs->HasPrefPath(prefs::kDefaultContentSettings)) {
- SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_COOKIES,
- (cookie_behavior == net::StaticCookiePolicy::BLOCK_ALL_COOKIES) ?
- CONTENT_SETTING_BLOCK : CONTENT_SETTING_ALLOW);
- }
- if (!prefs->HasPrefPath(prefs::kBlockThirdPartyCookies)) {
- SetBlockThirdPartyCookies(cookie_behavior ==
- net::StaticCookiePolicy::BLOCK_THIRD_PARTY_COOKIES);
- }
- }
+ MigrateObsoleteCookiePref(prefs);
- // Migrate obsolete popups pref.
- if (prefs->HasPrefPath(prefs::kPopupWhitelistedHosts)) {
- const ListValue* whitelist_pref =
- prefs->GetList(prefs::kPopupWhitelistedHosts);
- for (ListValue::const_iterator i(whitelist_pref->begin());
- i != whitelist_pref->end(); ++i) {
- std::string host;
- (*i)->GetAsString(&host);
- SetContentSetting(Pattern(host), CONTENT_SETTINGS_TYPE_POPUPS, "",
- CONTENT_SETTING_ALLOW);
- }
- prefs->ClearPref(prefs::kPopupWhitelistedHosts);
- }
+ MigrateObsoletePopupsPref(prefs);
- // Migrate obsolete per-host pref.
- if (prefs->HasPrefPath(prefs::kPerHostContentSettings)) {
- const DictionaryValue* all_settings_dictionary =
- prefs->GetDictionary(prefs::kPerHostContentSettings);
- for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys());
- i != all_settings_dictionary->end_keys(); ++i) {
- const std::string& host(*i);
- Pattern pattern(std::string(kDomainWildcard) + host);
- DictionaryValue* host_settings_dictionary = NULL;
- bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion(
- host, &host_settings_dictionary);
- DCHECK(found);
- ContentSettings settings;
- GetSettingsFromDictionary(host_settings_dictionary, &settings);
- for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) {
- if (settings.settings[j] != CONTENT_SETTING_DEFAULT &&
- !RequiresResourceIdentifier(ContentSettingsType(j)))
- SetContentSetting(
- pattern, ContentSettingsType(j), "", settings.settings[j]);
- }
- }
- prefs->ClearPref(prefs::kPerHostContentSettings);
- }
+ MigrateObsoletePerhostPref(prefs);
// Read global defaults.
DCHECK_EQ(arraysize(kTypeNames),
@@ -484,7 +457,7 @@ void HostContentSettingsMap::SetDefaultContentSetting(
}
void HostContentSettingsMap::SetContentSetting(
- const Pattern& pattern,
+ const Pattern& original_pattern,
ContentSettingsType content_type,
const std::string& resource_identifier,
ContentSetting setting) {
@@ -493,6 +466,8 @@ void HostContentSettingsMap::SetContentSetting(
DCHECK_NE(RequiresResourceIdentifier(content_type),
resource_identifier.empty());
+ const Pattern pattern(original_pattern.CanonicalizePattern());
+
bool early_exit = false;
std::string pattern_str(pattern.AsString());
PrefService* prefs = NULL;
@@ -861,8 +836,10 @@ void HostContentSettingsMap::ReadDefaultSettings(bool overwrite) {
PrefService* prefs = profile_->GetPrefs();
const DictionaryValue* default_settings_dictionary =
prefs->GetDictionary(prefs::kDefaultContentSettings);
+
if (overwrite)
default_content_settings_ = ContentSettings();
+
// Careful: The returned value could be NULL if the pref has never been set.
if (default_settings_dictionary != NULL) {
GetSettingsFromDictionary(default_settings_dictionary,
@@ -873,12 +850,17 @@ void HostContentSettingsMap::ReadDefaultSettings(bool overwrite) {
void HostContentSettingsMap::ReadExceptions(bool overwrite) {
PrefService* prefs = profile_->GetPrefs();
- const DictionaryValue* all_settings_dictionary =
+ DictionaryValue* all_settings_dictionary =
prefs->GetMutableDictionary(prefs::kContentSettingsPatterns);
+
if (overwrite)
host_content_settings_.clear();
+
// Careful: The returned value could be NULL if the pref has never been set.
if (all_settings_dictionary != NULL) {
+ // Convert all Unicode patterns into punycode form, then read.
+ CanonicalizeContentSettingsExceptions(all_settings_dictionary);
+
for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys());
i != all_settings_dictionary->end_keys(); ++i) {
const std::string& pattern(*i);
@@ -915,3 +897,104 @@ void HostContentSettingsMap::UnregisterObservers() {
Source<Profile>(profile_));
profile_ = NULL;
}
+
+void HostContentSettingsMap::MigrateObsoleteCookiePref(PrefService* prefs) {
+ if (prefs->HasPrefPath(prefs::kCookieBehavior)) {
+ int cookie_behavior = prefs->GetInteger(prefs::kCookieBehavior);
+ prefs->ClearPref(prefs::kCookieBehavior);
+ if (!prefs->HasPrefPath(prefs::kDefaultContentSettings)) {
+ SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_COOKIES,
+ (cookie_behavior == net::StaticCookiePolicy::BLOCK_ALL_COOKIES) ?
+ CONTENT_SETTING_BLOCK : CONTENT_SETTING_ALLOW);
+ }
+ if (!prefs->HasPrefPath(prefs::kBlockThirdPartyCookies)) {
+ SetBlockThirdPartyCookies(cookie_behavior ==
+ net::StaticCookiePolicy::BLOCK_THIRD_PARTY_COOKIES);
+ }
+ }
+}
+
+void HostContentSettingsMap::MigrateObsoletePopupsPref(PrefService* prefs) {
+ if (prefs->HasPrefPath(prefs::kPopupWhitelistedHosts)) {
+ const ListValue* whitelist_pref =
+ prefs->GetList(prefs::kPopupWhitelistedHosts);
+ for (ListValue::const_iterator i(whitelist_pref->begin());
+ i != whitelist_pref->end(); ++i) {
+ std::string host;
+ (*i)->GetAsString(&host);
+ SetContentSetting(Pattern(host), CONTENT_SETTINGS_TYPE_POPUPS, "",
+ CONTENT_SETTING_ALLOW);
+ }
+ prefs->ClearPref(prefs::kPopupWhitelistedHosts);
+ }
+}
+
+void HostContentSettingsMap::MigrateObsoletePerhostPref(PrefService* prefs) {
+ if (prefs->HasPrefPath(prefs::kPerHostContentSettings)) {
+ const DictionaryValue* all_settings_dictionary =
+ prefs->GetDictionary(prefs::kPerHostContentSettings);
+ for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys());
+ i != all_settings_dictionary->end_keys(); ++i) {
+ const std::string& host(*i);
+ Pattern pattern(std::string(kDomainWildcard) + host);
+ DictionaryValue* host_settings_dictionary = NULL;
+ bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion(
+ host, &host_settings_dictionary);
+ DCHECK(found);
+ ContentSettings settings;
+ GetSettingsFromDictionary(host_settings_dictionary, &settings);
+ for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) {
+ if (settings.settings[j] != CONTENT_SETTING_DEFAULT &&
+ !RequiresResourceIdentifier(ContentSettingsType(j)))
+ SetContentSetting(
+ pattern, ContentSettingsType(j), "", settings.settings[j]);
+ }
+ }
+ prefs->ClearPref(prefs::kPerHostContentSettings);
+ }
+}
+
+void HostContentSettingsMap::CanonicalizeContentSettingsExceptions(
+ DictionaryValue* all_settings_dictionary) {
+ DCHECK(all_settings_dictionary);
+
+ std::vector<std::string> remove_items;
+ std::vector<std::pair<std::string, std::string> > move_items;
+ for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys());
+ i != all_settings_dictionary->end_keys(); ++i) {
+ const std::string& pattern(*i);
+ const std::string canonicalized_pattern =
+ Pattern(pattern).CanonicalizePattern();
+
+ if (canonicalized_pattern.empty() || canonicalized_pattern == pattern)
+ continue;
+
+ // Clear old pattern if prefs already have canonicalized pattern.
+ DictionaryValue* new_pattern_settings_dictionary = NULL;
+ if (all_settings_dictionary->GetDictionaryWithoutPathExpansion(
+ canonicalized_pattern, &new_pattern_settings_dictionary)) {
+ remove_items.push_back(pattern);
+ continue;
+ }
+
+ // Move old pattern to canonicalized pattern.
+ DictionaryValue* old_pattern_settings_dictionary = NULL;
+ if (all_settings_dictionary->GetDictionaryWithoutPathExpansion(
+ pattern, &old_pattern_settings_dictionary)) {
+ move_items.push_back(std::make_pair(pattern, canonicalized_pattern));
+ }
+ }
+
+ for (size_t i = 0; i < remove_items.size(); ++i) {
+ all_settings_dictionary->RemoveWithoutPathExpansion(remove_items[i], NULL);
+ }
+
+ for (size_t i = 0; i < move_items.size(); ++i) {
+ Value* pattern_settings_dictionary = NULL;
+ all_settings_dictionary->RemoveWithoutPathExpansion(
+ move_items[i].first, &pattern_settings_dictionary);
+ all_settings_dictionary->SetWithoutPathExpansion(
+ move_items[i].second, pattern_settings_dictionary);
+ }
+}
+