| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/browsing_data/registrable_domain_filter_builder.h" | 5 #include "chrome/browser/browsing_data/registrable_domain_filter_builder.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 std::string registrable_domain = | 28 std::string registrable_domain = |
| 29 GetDomainAndRegistry(domain, INCLUDE_PRIVATE_REGISTRIES); | 29 GetDomainAndRegistry(domain, INCLUDE_PRIVATE_REGISTRIES); |
| 30 return registrable_domain != domain && registrable_domain != ""; | 30 return registrable_domain != domain && registrable_domain != ""; |
| 31 } | 31 } |
| 32 | 32 |
| 33 // Note that for every domain, exactly one of the following holds: | 33 // Note that for every domain, exactly one of the following holds: |
| 34 // 1. IsRegistrableDomain(domain) - e.g. google.com | 34 // 1. IsRegistrableDomain(domain) - e.g. google.com |
| 35 // 2. IsSubdomainOfARegistrableDomain(domain) - e.g. www.google.com | 35 // 2. IsSubdomainOfARegistrableDomain(domain) - e.g. www.google.com |
| 36 // 3. GetDomainAndRegistry(domain, _) == "" - e.g. localhost, 127.0.0.1 | 36 // 3. GetDomainAndRegistry(domain, _) == "" - e.g. localhost, 127.0.0.1 |
| 37 | 37 |
| 38 |
| 39 // True if the domain of |url| is in the whitelist, or isn't in the blacklist. |
| 40 // The whitelist or blacklist is represented as |registerable_domains| |
| 41 // and |mode|. |
| 42 bool MatchesURL( |
| 43 const std::set<std::string>& registerable_domains, |
| 44 BrowsingDataFilterBuilder::Mode mode, |
| 45 const GURL& url) { |
| 46 std::string url_registerable_domain = |
| 47 GetDomainAndRegistry(url, INCLUDE_PRIVATE_REGISTRIES); |
| 48 return (registerable_domains.find( |
| 49 url_registerable_domain != "" ? url_registerable_domain |
| 50 : url.host()) != |
| 51 registerable_domains.end()) == |
| 52 (mode == BrowsingDataFilterBuilder::WHITELIST); |
| 53 } |
| 54 |
| 55 // True if the pattern something in the whitelist, or doesn't match something |
| 56 // in the blacklist. |
| 57 // The whitelist or blacklist is represented as |patterns|, and |mode|. |
| 58 bool MatchesWebsiteSettingsPattern( |
| 59 const std::vector<ContentSettingsPattern>& domain_patterns, |
| 60 BrowsingDataFilterBuilder::Mode mode, |
| 61 const ContentSettingsPattern& pattern) { |
| 62 for (const ContentSettingsPattern& domain : domain_patterns) { |
| 63 DCHECK(domain.IsValid()); |
| 64 Relation relation = pattern.Compare(domain); |
| 65 if (relation == Relation::IDENTITY || relation == Relation::PREDECESSOR) |
| 66 return mode == BrowsingDataFilterBuilder::WHITELIST; |
| 67 } |
| 68 return mode != BrowsingDataFilterBuilder::WHITELIST; |
| 69 } |
| 70 |
| 71 // True if no domains can see the given cookie and we're a blacklist, or any |
| 72 // domains can see the cookie and we're a whitelist. |
| 73 // The whitelist or blacklist is represented as |domains_and_ips| and |mode|. |
| 74 bool MatchesCookieForRegisterableDomainsAndIPs( |
| 75 const std::set<std::string>& domains_and_ips, |
| 76 BrowsingDataFilterBuilder::Mode mode, |
| 77 const net::CanonicalCookie& cookie) { |
| 78 if (domains_and_ips.empty()) |
| 79 return mode == BrowsingDataFilterBuilder::BLACKLIST; |
| 80 std::string cookie_domain = cookie.Domain(); |
| 81 if (cookie.IsDomainCookie()) |
| 82 cookie_domain = cookie_domain.substr(1); |
| 83 std::string parsed_cookie_domain = |
| 84 GetDomainAndRegistry(cookie_domain, INCLUDE_PRIVATE_REGISTRIES); |
| 85 // This means we're an IP address or an internal hostname. |
| 86 if (parsed_cookie_domain.empty()) |
| 87 parsed_cookie_domain = cookie_domain; |
| 88 return (mode == BrowsingDataFilterBuilder::WHITELIST) == |
| 89 (domains_and_ips.find(parsed_cookie_domain) != domains_and_ips.end()); |
| 90 } |
| 91 |
| 92 // True if none of the supplied domains matches this Channel ID's server ID |
| 93 // and we're a blacklist, or one of them does and we're a whitelist. |
| 94 // The whitelist or blacklist is represented as |domains_and_ips| and |mode|. |
| 95 bool MatchesChannelIDForRegisterableDomainsAndIPs( |
| 96 const std::set<std::string>& domains_and_ips, |
| 97 BrowsingDataFilterBuilder::Mode mode, |
| 98 const std::string& channel_id_server_id) { |
| 99 return ((mode == BrowsingDataFilterBuilder::WHITELIST) == |
| 100 (domains_and_ips.find(channel_id_server_id) != domains_and_ips.end())); |
| 101 } |
| 102 |
| 103 // True if none of the supplied domains matches this plugin's |site| and we're |
| 104 // a blacklist, or one of them does and we're a whitelist. The whitelist or |
| 105 // blacklist is represented by |domains_and_ips| and |mode|. |
| 106 bool MatchesPluginSiteForRegisterableDomainsAndIPs( |
| 107 const std::set<std::string>& domains_and_ips, |
| 108 BrowsingDataFilterBuilder::Mode mode, |
| 109 const std::string& site) { |
| 110 // If |site| is a third- or lower-level domain, find the corresponding eTLD+1. |
| 111 std::string domain_or_ip = |
| 112 GetDomainAndRegistry(site, INCLUDE_PRIVATE_REGISTRIES); |
| 113 if (domain_or_ip.empty()) |
| 114 domain_or_ip = site; |
| 115 |
| 116 return ((mode == BrowsingDataFilterBuilder::WHITELIST) == |
| 117 (domains_and_ips.find(domain_or_ip) != domains_and_ips.end())); |
| 118 } |
| 119 |
| 38 } // namespace | 120 } // namespace |
| 39 | 121 |
| 40 RegistrableDomainFilterBuilder::RegistrableDomainFilterBuilder(Mode mode) | 122 RegistrableDomainFilterBuilder::RegistrableDomainFilterBuilder(Mode mode) |
| 41 : BrowsingDataFilterBuilder(mode) { | 123 : BrowsingDataFilterBuilder(mode) { |
| 42 } | 124 } |
| 43 | 125 |
| 44 RegistrableDomainFilterBuilder::~RegistrableDomainFilterBuilder() {} | 126 RegistrableDomainFilterBuilder::~RegistrableDomainFilterBuilder() {} |
| 45 | 127 |
| 46 void RegistrableDomainFilterBuilder::AddRegisterableDomain( | 128 void RegistrableDomainFilterBuilder::AddRegisterableDomain( |
| 47 const std::string& domain) { | 129 const std::string& domain) { |
| 48 // We check that the domain we're given is actually a eTLD+1, an IP address, | 130 // We check that the domain we're given is actually a eTLD+1, an IP address, |
| 49 // or an internal hostname. | 131 // or an internal hostname. |
| 50 DCHECK(!IsSubdomainOfARegistrableDomain(domain)); | 132 DCHECK(!IsSubdomainOfARegistrableDomain(domain)); |
| 51 domain_list_.insert(domain); | 133 domains_.insert(domain); |
| 52 } | 134 } |
| 53 | 135 |
| 54 base::Callback<bool(const GURL&)> | 136 base::Callback<bool(const GURL&)> |
| 55 RegistrableDomainFilterBuilder::BuildGeneralFilter() const { | 137 RegistrableDomainFilterBuilder::BuildGeneralFilter() const { |
| 56 std::set<std::string>* domains = new std::set<std::string>(domain_list_); | 138 return base::BindRepeating(MatchesURL, domains_, mode()); |
| 57 return base::Bind(&RegistrableDomainFilterBuilder::MatchesURL, | |
| 58 base::Owned(domains), mode()); | |
| 59 } | 139 } |
| 60 | 140 |
| 61 base::Callback<bool(const ContentSettingsPattern& pattern)> | 141 base::Callback<bool(const ContentSettingsPattern& pattern)> |
| 62 RegistrableDomainFilterBuilder | 142 RegistrableDomainFilterBuilder |
| 63 ::BuildWebsiteSettingsPatternMatchesFilter() const { | 143 ::BuildWebsiteSettingsPatternMatchesFilter() const { |
| 64 std::vector<ContentSettingsPattern>* patterns_from_domains = | 144 std::vector<ContentSettingsPattern> patterns_from_domains; |
| 65 new std::vector<ContentSettingsPattern>(); | 145 patterns_from_domains.reserve(domains_.size()); |
| 66 patterns_from_domains->reserve(domain_list_.size()); | |
| 67 | 146 |
| 68 for (const std::string& domain : domain_list_) { | 147 for (const std::string& domain : domains_) { |
| 69 std::unique_ptr<ContentSettingsPattern::BuilderInterface> builder( | 148 std::unique_ptr<ContentSettingsPattern::BuilderInterface> builder( |
| 70 ContentSettingsPattern::CreateBuilder(/* use_legacy_validate */ false)); | 149 ContentSettingsPattern::CreateBuilder(/* use_legacy_validate */ false)); |
| 71 builder->WithSchemeWildcard() | 150 builder->WithSchemeWildcard() |
| 72 ->WithPortWildcard() | 151 ->WithPortWildcard() |
| 73 ->WithPathWildcard() | 152 ->WithPathWildcard() |
| 74 ->WithHost(domain); | 153 ->WithHost(domain); |
| 75 if (IsRegistrableDomain(domain)) | 154 if (IsRegistrableDomain(domain)) |
| 76 builder->WithDomainWildcard(); | 155 builder->WithDomainWildcard(); |
| 77 | 156 |
| 78 patterns_from_domains->push_back(builder->Build()); | 157 patterns_from_domains.push_back(builder->Build()); |
| 79 } | 158 } |
| 80 | 159 |
| 81 for (const ContentSettingsPattern& domain : *patterns_from_domains) { | 160 for (const ContentSettingsPattern& domain : patterns_from_domains) { |
| 82 DCHECK(domain.IsValid()); | 161 DCHECK(domain.IsValid()); |
| 83 } | 162 } |
| 84 | 163 |
| 85 return base::Bind( | 164 return base::BindRepeating(&MatchesWebsiteSettingsPattern, |
| 86 &RegistrableDomainFilterBuilder::MatchesWebsiteSettingsPattern, | 165 std::move(patterns_from_domains), mode()); |
| 87 base::Owned(patterns_from_domains), mode()); | |
| 88 } | 166 } |
| 89 | 167 |
| 90 base::Callback<bool(const net::CanonicalCookie& cookie)> | 168 base::Callback<bool(const net::CanonicalCookie& cookie)> |
| 91 RegistrableDomainFilterBuilder::BuildCookieFilter() const { | 169 RegistrableDomainFilterBuilder::BuildCookieFilter() const { |
| 92 std::set<std::string>* domains_and_ips = | 170 return base::BindRepeating(&MatchesCookieForRegisterableDomainsAndIPs, |
| 93 new std::set<std::string>(domain_list_); | 171 domains_, mode()); |
| 94 return base::Bind( | |
| 95 &RegistrableDomainFilterBuilder | |
| 96 ::MatchesCookieForRegisterableDomainsAndIPs, | |
| 97 base::Owned(domains_and_ips), mode()); | |
| 98 } | 172 } |
| 99 | 173 |
| 100 base::Callback<bool(const std::string& cookie)> | 174 base::Callback<bool(const std::string& cookie)> |
| 101 RegistrableDomainFilterBuilder::BuildChannelIDFilter() const { | 175 RegistrableDomainFilterBuilder::BuildChannelIDFilter() const { |
| 102 std::set<std::string>* domains_and_ips = | 176 return base::BindRepeating(&MatchesChannelIDForRegisterableDomainsAndIPs, |
| 103 new std::set<std::string>(domain_list_); | 177 domains_, mode()); |
| 104 return base::Bind( | |
| 105 &RegistrableDomainFilterBuilder | |
| 106 ::MatchesChannelIDForRegisterableDomainsAndIPs, | |
| 107 base::Owned(domains_and_ips), mode()); | |
| 108 } | 178 } |
| 109 | 179 |
| 110 base::Callback<bool(const std::string& site)> | 180 base::Callback<bool(const std::string& site)> |
| 111 RegistrableDomainFilterBuilder::BuildPluginFilter() const { | 181 RegistrableDomainFilterBuilder::BuildPluginFilter() const { |
| 112 std::set<std::string>* domains_and_ips = | 182 return base::BindRepeating(&MatchesPluginSiteForRegisterableDomainsAndIPs, |
| 113 new std::set<std::string>(domain_list_); | 183 domains_, mode()); |
| 114 return base::Bind( | |
| 115 &RegistrableDomainFilterBuilder | |
| 116 ::MatchesPluginSiteForRegisterableDomainsAndIPs, | |
| 117 base::Owned(domains_and_ips), mode()); | |
| 118 } | 184 } |
| 119 | 185 |
| 120 bool RegistrableDomainFilterBuilder::operator==( | 186 bool RegistrableDomainFilterBuilder::operator==( |
| 121 const RegistrableDomainFilterBuilder& other) const { | 187 const RegistrableDomainFilterBuilder& other) const { |
| 122 return domain_list_ == other.domain_list_ && mode() == other.mode(); | 188 return domains_ == other.domains_ && mode() == other.mode(); |
| 123 } | 189 } |
| 124 | 190 |
| 125 bool RegistrableDomainFilterBuilder::IsEmpty() const { | 191 bool RegistrableDomainFilterBuilder::IsEmpty() const { |
| 126 return domain_list_.empty(); | 192 return domains_.empty(); |
| 127 } | 193 } |
| 128 | |
| 129 // static | |
| 130 bool RegistrableDomainFilterBuilder::MatchesURL( | |
| 131 std::set<std::string>* registerable_domains, | |
| 132 Mode mode, | |
| 133 const GURL& url) { | |
| 134 std::string url_registerable_domain = | |
| 135 GetDomainAndRegistry(url, INCLUDE_PRIVATE_REGISTRIES); | |
| 136 return (registerable_domains->find( | |
| 137 url_registerable_domain != "" ? url_registerable_domain | |
| 138 : url.host()) != | |
| 139 registerable_domains->end()) == | |
| 140 (mode == WHITELIST); | |
| 141 } | |
| 142 | |
| 143 // static | |
| 144 bool RegistrableDomainFilterBuilder::MatchesWebsiteSettingsPattern( | |
| 145 std::vector<ContentSettingsPattern>* domain_patterns, | |
| 146 Mode mode, | |
| 147 const ContentSettingsPattern& pattern) { | |
| 148 for (const ContentSettingsPattern& domain : *domain_patterns) { | |
| 149 DCHECK(domain.IsValid()); | |
| 150 Relation relation = pattern.Compare(domain); | |
| 151 if (relation == Relation::IDENTITY || relation == Relation::PREDECESSOR) | |
| 152 return mode == WHITELIST; | |
| 153 } | |
| 154 return mode != WHITELIST; | |
| 155 } | |
| 156 | |
| 157 // static | |
| 158 bool RegistrableDomainFilterBuilder::MatchesCookieForRegisterableDomainsAndIPs( | |
| 159 std::set<std::string>* domains_and_ips, | |
| 160 Mode mode, | |
| 161 const net::CanonicalCookie& cookie) { | |
| 162 if (domains_and_ips->empty()) | |
| 163 return mode == BLACKLIST; | |
| 164 std::string cookie_domain = cookie.Domain(); | |
| 165 if (cookie.IsDomainCookie()) | |
| 166 cookie_domain = cookie_domain.substr(1); | |
| 167 std::string parsed_cookie_domain = | |
| 168 GetDomainAndRegistry(cookie_domain, INCLUDE_PRIVATE_REGISTRIES); | |
| 169 // This means we're an IP address or an internal hostname. | |
| 170 if (parsed_cookie_domain.empty()) | |
| 171 parsed_cookie_domain = cookie_domain; | |
| 172 return (mode == WHITELIST) == (domains_and_ips->find(parsed_cookie_domain) != | |
| 173 domains_and_ips->end()); | |
| 174 } | |
| 175 | |
| 176 // static | |
| 177 bool | |
| 178 RegistrableDomainFilterBuilder::MatchesChannelIDForRegisterableDomainsAndIPs( | |
| 179 std::set<std::string>* domains_and_ips, | |
| 180 Mode mode, | |
| 181 const std::string& channel_id_server_id) { | |
| 182 return ((mode == WHITELIST) == (domains_and_ips->find(channel_id_server_id) != | |
| 183 domains_and_ips->end())); | |
| 184 } | |
| 185 | |
| 186 // static | |
| 187 bool | |
| 188 RegistrableDomainFilterBuilder::MatchesPluginSiteForRegisterableDomainsAndIPs( | |
| 189 std::set<std::string>* domains_and_ips, | |
| 190 Mode mode, | |
| 191 const std::string& site) { | |
| 192 // If |site| is a third- or lower-level domain, find the corresponding eTLD+1. | |
| 193 std::string domain_or_ip = | |
| 194 GetDomainAndRegistry(site, INCLUDE_PRIVATE_REGISTRIES); | |
| 195 if (domain_or_ip.empty()) | |
| 196 domain_or_ip = site; | |
| 197 | |
| 198 return ((mode == WHITELIST) == (domains_and_ips->find(domain_or_ip) != | |
| 199 domains_and_ips->end())); | |
| 200 } | |
| OLD | NEW |