| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/host_content_settings_map.h" | 5 #include "chrome/browser/host_content_settings_map.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/chrome_thread.h" | 9 #include "chrome/browser/chrome_thread.h" |
| 10 #include "chrome/browser/pref_service.h" | 10 #include "chrome/browser/pref_service.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "net/base/net_util.h" | 21 #include "net/base/net_util.h" |
| 22 #include "net/base/static_cookie_policy.h" | 22 #include "net/base/static_cookie_policy.h" |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 // The version of the pattern format implemented. Version 1 includes the | 25 // The version of the pattern format implemented. Version 1 includes the |
| 26 // following patterns: | 26 // following patterns: |
| 27 // - [*.]domain.tld (matches domain.tld and all sub-domains) | 27 // - [*.]domain.tld (matches domain.tld and all sub-domains) |
| 28 // - host (matches an exact hostname) | 28 // - host (matches an exact hostname) |
| 29 // - a.b.c.d (matches an exact IPv4 ip) | 29 // - a.b.c.d (matches an exact IPv4 ip) |
| 30 // - [a:b:c:d:e:f:g:h] (matches an exact IPv6 ip) | 30 // - [a:b:c:d:e:f:g:h] (matches an exact IPv6 ip) |
| 31 // - file:///tmp/test.html (a complete URL without a host) |
| 31 const int kContentSettingsPatternVersion = 1; | 32 const int kContentSettingsPatternVersion = 1; |
| 32 | 33 |
| 33 // The format of a domain wildcard. | 34 // The format of a domain wildcard. |
| 34 const char kDomainWildcard[] = "[*.]"; | 35 const char kDomainWildcard[] = "[*.]"; |
| 35 | 36 |
| 36 // The length of kDomainWildcard (without the trailing '\0') | 37 // The length of kDomainWildcard (without the trailing '\0') |
| 37 const size_t kDomainWildcardLength = arraysize(kDomainWildcard) - 1; | 38 const size_t kDomainWildcardLength = arraysize(kDomainWildcard) - 1; |
| 38 | 39 |
| 39 // Returns the host part of an URL, or the spec, if no host is present. | |
| 40 std::string HostFromURL(const GURL& url) { | |
| 41 return url.has_host() ? net::TrimEndingDot(url.host()) : url.spec(); | |
| 42 } | |
| 43 } // namespace | 40 } // namespace |
| 44 | 41 |
| 45 // static | 42 // static |
| 46 HostContentSettingsMap::Pattern HostContentSettingsMap::Pattern::FromURL( | 43 HostContentSettingsMap::Pattern HostContentSettingsMap::Pattern::FromURL( |
| 47 const GURL& url) { | 44 const GURL& url) { |
| 48 return Pattern(!url.has_host() || url.HostIsIPAddress() ? | 45 return Pattern(!url.has_host() || url.HostIsIPAddress() ? |
| 49 HostFromURL(url) : std::string(kDomainWildcard) + url.host()); | 46 net::GetHostOrSpecFromURL(url) : |
| 47 std::string(kDomainWildcard) + url.host()); |
| 50 } | 48 } |
| 51 | 49 |
| 52 bool HostContentSettingsMap::Pattern::IsValid() const { | 50 bool HostContentSettingsMap::Pattern::IsValid() const { |
| 53 if (pattern_.empty()) | 51 if (pattern_.empty()) |
| 54 return false; | 52 return false; |
| 55 | 53 |
| 56 const std::string host(pattern_.length() > kDomainWildcardLength && | 54 const std::string host(pattern_.length() > kDomainWildcardLength && |
| 57 StartsWithASCII(pattern_, kDomainWildcard, false) ? | 55 StartsWithASCII(pattern_, kDomainWildcard, false) ? |
| 58 pattern_.substr(kDomainWildcardLength) : | 56 pattern_.substr(kDomainWildcardLength) : |
| 59 pattern_); | 57 pattern_); |
| 60 url_canon::CanonHostInfo host_info; | 58 url_canon::CanonHostInfo host_info; |
| 61 return host.find('*') == std::string::npos && | 59 return host.find('*') == std::string::npos && |
| 62 !net::CanonicalizeHost(host, &host_info).empty(); | 60 !net::CanonicalizeHost(host, &host_info).empty(); |
| 63 } | 61 } |
| 64 | 62 |
| 65 bool HostContentSettingsMap::Pattern::Matches(const GURL& url) const { | 63 bool HostContentSettingsMap::Pattern::Matches(const GURL& url) const { |
| 66 if (!IsValid()) | 64 if (!IsValid()) |
| 67 return false; | 65 return false; |
| 68 | 66 |
| 69 const std::string host(HostFromURL(url)); | 67 const std::string host(net::GetHostOrSpecFromURL(url)); |
| 70 if (pattern_.length() < kDomainWildcardLength || | 68 if (pattern_.length() < kDomainWildcardLength || |
| 71 !StartsWithASCII(pattern_, kDomainWildcard, false)) | 69 !StartsWithASCII(pattern_, kDomainWildcard, false)) |
| 72 return pattern_ == host; | 70 return pattern_ == host; |
| 73 | 71 |
| 74 const size_t match = | 72 const size_t match = |
| 75 host.rfind(pattern_.substr(kDomainWildcardLength)); | 73 host.rfind(pattern_.substr(kDomainWildcardLength)); |
| 76 | 74 |
| 77 return (match != std::string::npos) && | 75 return (match != std::string::npos) && |
| 78 (match == 0 || host[match - 1] == '.') && | 76 (match == 0 || host[match - 1] == '.') && |
| 79 (match + pattern_.length() - kDomainWildcardLength == host.length()); | 77 (match + pattern_.length() - kDomainWildcardLength == host.length()); |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 return GetContentSettings(url).settings[content_type]; | 218 return GetContentSettings(url).settings[content_type]; |
| 221 } | 219 } |
| 222 | 220 |
| 223 ContentSettings HostContentSettingsMap::GetContentSettings( | 221 ContentSettings HostContentSettingsMap::GetContentSettings( |
| 224 const GURL& url) const { | 222 const GURL& url) const { |
| 225 if (ShouldAllowAllContent(url)) | 223 if (ShouldAllowAllContent(url)) |
| 226 return ContentSettings(CONTENT_SETTING_ALLOW); | 224 return ContentSettings(CONTENT_SETTING_ALLOW); |
| 227 | 225 |
| 228 AutoLock auto_lock(lock_); | 226 AutoLock auto_lock(lock_); |
| 229 | 227 |
| 230 const std::string host(HostFromURL(url)); | 228 const std::string host(net::GetHostOrSpecFromURL(url)); |
| 231 | 229 |
| 232 // Check for exact matches first. | 230 // Check for exact matches first. |
| 233 HostContentSettings::const_iterator i(host_content_settings_.find(host)); | 231 HostContentSettings::const_iterator i(host_content_settings_.find(host)); |
| 234 if (i != host_content_settings_.end()) { | 232 if (i != host_content_settings_.end()) { |
| 235 ContentSettings output = i->second; | 233 ContentSettings output = i->second; |
| 236 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { | 234 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { |
| 237 if (output.settings[j] == CONTENT_SETTING_DEFAULT) | 235 if (output.settings[j] == CONTENT_SETTING_DEFAULT) |
| 238 output.settings[j] = default_content_settings_.settings[j]; | 236 output.settings[j] = default_content_settings_.settings[j]; |
| 239 } | 237 } |
| 240 return output; | 238 return output; |
| (...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 605 if (!is_off_the_record_) { | 603 if (!is_off_the_record_) { |
| 606 PrefService* prefs = profile_->GetPrefs(); | 604 PrefService* prefs = profile_->GetPrefs(); |
| 607 prefs->RemovePrefObserver(prefs::kDefaultContentSettings, this); | 605 prefs->RemovePrefObserver(prefs::kDefaultContentSettings, this); |
| 608 prefs->RemovePrefObserver(prefs::kContentSettingsPatterns, this); | 606 prefs->RemovePrefObserver(prefs::kContentSettingsPatterns, this); |
| 609 prefs->RemovePrefObserver(prefs::kBlockThirdPartyCookies, this); | 607 prefs->RemovePrefObserver(prefs::kBlockThirdPartyCookies, this); |
| 610 } | 608 } |
| 611 notification_registrar_.Remove(this, NotificationType::PROFILE_DESTROYED, | 609 notification_registrar_.Remove(this, NotificationType::PROFILE_DESTROYED, |
| 612 NotificationService::AllSources()); | 610 NotificationService::AllSources()); |
| 613 profile_ = NULL; | 611 profile_ = NULL; |
| 614 } | 612 } |
| OLD | NEW |