Chromium Code Reviews| 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 domain_list_.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 std::set<std::string> domains(domain_list_); |
| 57 return base::Bind(&RegistrableDomainFilterBuilder::MatchesURL, | 139 return base::Bind(MatchesURL, domains, mode()); |
|
Bernhard Bauer
2016/11/23 15:53:07
This will copy the set. That could be fine if you'
msramek
2016/11/24 17:38:08
The copy on the previous line (|domain_list_|->|do
Bernhard Bauer
2016/11/25 13:53:19
Ok, that makes sense. Thanks!
Actually, now that
msramek
2016/11/25 17:03:43
Well, I see three options:
1. Status quo. Don't be
Bernhard Bauer
2016/11/25 17:18:31
Yes, that's what I understand from https://chromiu
msramek
2016/12/07 13:21:34
Alright! I changed all base::Bind to base::BindRep
| |
| 58 base::Owned(domains), mode()); | |
| 59 } | 140 } |
| 60 | 141 |
| 61 base::Callback<bool(const ContentSettingsPattern& pattern)> | 142 base::Callback<bool(const ContentSettingsPattern& pattern)> |
| 62 RegistrableDomainFilterBuilder | 143 RegistrableDomainFilterBuilder |
| 63 ::BuildWebsiteSettingsPatternMatchesFilter() const { | 144 ::BuildWebsiteSettingsPatternMatchesFilter() const { |
| 64 std::vector<ContentSettingsPattern>* patterns_from_domains = | 145 std::vector<ContentSettingsPattern> patterns_from_domains; |
| 65 new std::vector<ContentSettingsPattern>(); | 146 patterns_from_domains.reserve(domain_list_.size()); |
| 66 patterns_from_domains->reserve(domain_list_.size()); | |
| 67 | 147 |
| 68 for (const std::string& domain : domain_list_) { | 148 for (const std::string& domain : domain_list_) { |
| 69 std::unique_ptr<ContentSettingsPattern::BuilderInterface> builder( | 149 std::unique_ptr<ContentSettingsPattern::BuilderInterface> builder( |
| 70 ContentSettingsPattern::CreateBuilder(/* use_legacy_validate */ false)); | 150 ContentSettingsPattern::CreateBuilder(/* use_legacy_validate */ false)); |
| 71 builder->WithSchemeWildcard() | 151 builder->WithSchemeWildcard() |
| 72 ->WithPortWildcard() | 152 ->WithPortWildcard() |
| 73 ->WithPathWildcard() | 153 ->WithPathWildcard() |
| 74 ->WithHost(domain); | 154 ->WithHost(domain); |
| 75 if (IsRegistrableDomain(domain)) | 155 if (IsRegistrableDomain(domain)) |
| 76 builder->WithDomainWildcard(); | 156 builder->WithDomainWildcard(); |
| 77 | 157 |
| 78 patterns_from_domains->push_back(builder->Build()); | 158 patterns_from_domains.push_back(builder->Build()); |
| 79 } | 159 } |
| 80 | 160 |
| 81 for (const ContentSettingsPattern& domain : *patterns_from_domains) { | 161 for (const ContentSettingsPattern& domain : patterns_from_domains) { |
| 82 DCHECK(domain.IsValid()); | 162 DCHECK(domain.IsValid()); |
| 83 } | 163 } |
| 84 | 164 |
| 85 return base::Bind( | 165 return base::Bind(&MatchesWebsiteSettingsPattern, |
| 86 &RegistrableDomainFilterBuilder::MatchesWebsiteSettingsPattern, | 166 std::move(patterns_from_domains), mode()); |
| 87 base::Owned(patterns_from_domains), mode()); | |
| 88 } | 167 } |
| 89 | 168 |
| 90 base::Callback<bool(const net::CanonicalCookie& cookie)> | 169 base::Callback<bool(const net::CanonicalCookie& cookie)> |
| 91 RegistrableDomainFilterBuilder::BuildCookieFilter() const { | 170 RegistrableDomainFilterBuilder::BuildCookieFilter() const { |
| 92 std::set<std::string>* domains_and_ips = | 171 std::set<std::string> domains_and_ips(domain_list_); |
| 93 new std::set<std::string>(domain_list_); | 172 return base::Bind(&MatchesCookieForRegisterableDomainsAndIPs, |
| 94 return base::Bind( | 173 std::move(domains_and_ips), mode()); |
| 95 &RegistrableDomainFilterBuilder | |
| 96 ::MatchesCookieForRegisterableDomainsAndIPs, | |
| 97 base::Owned(domains_and_ips), mode()); | |
| 98 } | 174 } |
| 99 | 175 |
| 100 base::Callback<bool(const std::string& cookie)> | 176 base::Callback<bool(const std::string& cookie)> |
| 101 RegistrableDomainFilterBuilder::BuildChannelIDFilter() const { | 177 RegistrableDomainFilterBuilder::BuildChannelIDFilter() const { |
| 102 std::set<std::string>* domains_and_ips = | 178 std::set<std::string> domains_and_ips(domain_list_); |
| 103 new std::set<std::string>(domain_list_); | 179 return base::Bind(&MatchesChannelIDForRegisterableDomainsAndIPs, |
| 104 return base::Bind( | 180 std::move(domains_and_ips), mode()); |
| 105 &RegistrableDomainFilterBuilder | |
| 106 ::MatchesChannelIDForRegisterableDomainsAndIPs, | |
| 107 base::Owned(domains_and_ips), mode()); | |
| 108 } | 181 } |
| 109 | 182 |
| 110 base::Callback<bool(const std::string& site)> | 183 base::Callback<bool(const std::string& site)> |
| 111 RegistrableDomainFilterBuilder::BuildPluginFilter() const { | 184 RegistrableDomainFilterBuilder::BuildPluginFilter() const { |
| 112 std::set<std::string>* domains_and_ips = | 185 std::set<std::string> domains_and_ips(domain_list_); |
| 113 new std::set<std::string>(domain_list_); | 186 return base::Bind(&MatchesPluginSiteForRegisterableDomainsAndIPs, |
| 114 return base::Bind( | 187 std::move(domains_and_ips), mode()); |
| 115 &RegistrableDomainFilterBuilder | |
| 116 ::MatchesPluginSiteForRegisterableDomainsAndIPs, | |
| 117 base::Owned(domains_and_ips), mode()); | |
| 118 } | 188 } |
| 119 | 189 |
| 120 bool RegistrableDomainFilterBuilder::operator==( | 190 bool RegistrableDomainFilterBuilder::operator==( |
| 121 const RegistrableDomainFilterBuilder& other) const { | 191 const RegistrableDomainFilterBuilder& other) const { |
| 122 return domain_list_ == other.domain_list_ && mode() == other.mode(); | 192 return domain_list_ == other.domain_list_ && mode() == other.mode(); |
| 123 } | 193 } |
| 124 | 194 |
| 125 bool RegistrableDomainFilterBuilder::IsEmpty() const { | 195 bool RegistrableDomainFilterBuilder::IsEmpty() const { |
| 126 return domain_list_.empty(); | 196 return domain_list_.empty(); |
| 127 } | 197 } |
| 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 |