OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <string> |
| 6 #include <vector> |
| 7 |
| 8 #include "base/bind.h" |
| 9 #include "chrome/browser/browsing_data/browsing_data_filter_builder.h" |
| 10 #include "components/content_settings/core/common/content_settings_pattern.h" |
| 11 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 12 |
| 13 using net::registry_controlled_domains::GetDomainAndRegistry; |
| 14 using net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES; |
| 15 using Relation = ContentSettingsPattern::Relation; |
| 16 |
| 17 namespace { |
| 18 |
| 19 bool NoopFilter(const GURL& url) { |
| 20 return true; |
| 21 } |
| 22 |
| 23 } // namespace |
| 24 |
| 25 BrowsingDataFilterBuilder::BrowsingDataFilterBuilder(Mode mode) : mode_(mode) {} |
| 26 |
| 27 BrowsingDataFilterBuilder::~BrowsingDataFilterBuilder() {} |
| 28 |
| 29 void BrowsingDataFilterBuilder::AddRegisterableDomain( |
| 30 const std::string& domain) { |
| 31 // We check that the domain we're given is actually a eTLD+1, or an IP |
| 32 // address. |
| 33 DCHECK(GetDomainAndRegistry("www." + domain, INCLUDE_PRIVATE_REGISTRIES) == |
| 34 domain || |
| 35 GURL("http://" + domain).HostIsIPAddress()); |
| 36 domain_list_.insert(domain); |
| 37 } |
| 38 |
| 39 void BrowsingDataFilterBuilder::SetMode(Mode mode) { |
| 40 mode_ = mode; |
| 41 } |
| 42 |
| 43 bool BrowsingDataFilterBuilder::IsEmptyBlacklist() const { |
| 44 return mode_ == Mode::BLACKLIST && domain_list_.empty(); |
| 45 } |
| 46 |
| 47 base::Callback<bool(const GURL&)> |
| 48 BrowsingDataFilterBuilder::BuildSameDomainFilter() const { |
| 49 std::set<std::string>* domains = new std::set<std::string>(domain_list_); |
| 50 return base::Bind(&BrowsingDataFilterBuilder::MatchesURL, |
| 51 base::Owned(domains), mode_); |
| 52 } |
| 53 |
| 54 base::Callback<bool(const ContentSettingsPattern& pattern)> |
| 55 BrowsingDataFilterBuilder::BuildWebsiteSettingsPatternMatchesFilter() const { |
| 56 std::vector<ContentSettingsPattern>* patterns_from_domains = |
| 57 new std::vector<ContentSettingsPattern>(); |
| 58 patterns_from_domains->reserve(domain_list_.size()); |
| 59 |
| 60 std::unique_ptr<ContentSettingsPattern::BuilderInterface> builder( |
| 61 ContentSettingsPattern::CreateBuilder(/* use_legacy_validate */ false)); |
| 62 for (const std::string& domain : domain_list_) { |
| 63 builder->WithSchemeWildcard() |
| 64 ->WithPortWildcard() |
| 65 ->WithPathWildcard() |
| 66 ->WithHost(domain); |
| 67 if (!GURL("http://" + domain).HostIsIPAddress()) { |
| 68 builder->WithDomainWildcard(); |
| 69 } |
| 70 patterns_from_domains->push_back(builder->Build()); |
| 71 } |
| 72 |
| 73 for (const ContentSettingsPattern& domain : *patterns_from_domains) { |
| 74 DCHECK(domain.IsValid()); |
| 75 } |
| 76 |
| 77 return base::Bind(&BrowsingDataFilterBuilder::MatchesWebsiteSettingsPattern, |
| 78 base::Owned(patterns_from_domains), mode_); |
| 79 } |
| 80 |
| 81 base::Callback<bool(const net::CanonicalCookie& pattern)> |
| 82 BrowsingDataFilterBuilder::BuildDomainCookieFilter() const { |
| 83 std::set<std::string>* domains_and_ips = |
| 84 new std::set<std::string>(domain_list_); |
| 85 return base::Bind( |
| 86 &BrowsingDataFilterBuilder::MatchesCookieForRegisterableDomainsAndIPs, |
| 87 base::Owned(domains_and_ips), mode_); |
| 88 } |
| 89 |
| 90 // static |
| 91 base::Callback<bool(const GURL&)> BrowsingDataFilterBuilder::BuildNoopFilter() { |
| 92 return base::Bind(&NoopFilter); |
| 93 } |
| 94 |
| 95 // static |
| 96 bool BrowsingDataFilterBuilder::MatchesURL( |
| 97 std::set<std::string>* registerable_domains, |
| 98 Mode mode, |
| 99 const GURL& url) { |
| 100 std::string url_registerable_domain = |
| 101 GetDomainAndRegistry(url, INCLUDE_PRIVATE_REGISTRIES); |
| 102 return (registerable_domains->find(url_registerable_domain) != |
| 103 registerable_domains->end() || |
| 104 (url.HostIsIPAddress() && (registerable_domains->find(url.host()) != |
| 105 registerable_domains->end()))) == |
| 106 (mode == WHITELIST); |
| 107 } |
| 108 |
| 109 // static |
| 110 bool BrowsingDataFilterBuilder::MatchesWebsiteSettingsPattern( |
| 111 std::vector<ContentSettingsPattern>* domain_patterns, |
| 112 Mode mode, |
| 113 const ContentSettingsPattern& pattern) { |
| 114 for (const ContentSettingsPattern& domain : *domain_patterns) { |
| 115 DCHECK(domain.IsValid()); |
| 116 Relation relation = pattern.Compare(domain); |
| 117 if (relation == Relation::IDENTITY || relation == Relation::PREDECESSOR) |
| 118 return mode == WHITELIST; |
| 119 } |
| 120 return mode != WHITELIST; |
| 121 } |
| 122 |
| 123 // static |
| 124 bool BrowsingDataFilterBuilder::MatchesCookieForRegisterableDomainsAndIPs( |
| 125 std::set<std::string>* domains_and_ips, |
| 126 Mode mode, |
| 127 const net::CanonicalCookie& cookie) { |
| 128 if (domains_and_ips->empty()) |
| 129 return mode == BLACKLIST; |
| 130 std::string cookie_domain = cookie.Domain(); |
| 131 if (cookie.IsDomainCookie()) |
| 132 cookie_domain = cookie_domain.substr(1); |
| 133 std::string parsed_cookie_domain = |
| 134 GetDomainAndRegistry(cookie_domain, INCLUDE_PRIVATE_REGISTRIES); |
| 135 // This means we're an IP address. |
| 136 if (parsed_cookie_domain.empty()) |
| 137 parsed_cookie_domain = cookie_domain; |
| 138 return (mode == WHITELIST) == (domains_and_ips->find(parsed_cookie_domain) != |
| 139 domains_and_ips->end()); |
| 140 } |
OLD | NEW |