1 // Copyright 2019 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_COOKIES_TEST_COOKIE_ACCESS_DELEGATE_H_ 6 #define NET_COOKIES_TEST_COOKIE_ACCESS_DELEGATE_H_ 7 8 #include <map> 9 #include <optional> 10 #include <set> 11 #include <string> 12 13 #include "base/containers/flat_map.h" 14 #include "base/containers/flat_set.h" 15 #include "base/functional/callback_forward.h" 16 #include "net/base/schemeful_site.h" 17 #include "net/cookies/cookie_access_delegate.h" 18 #include "net/cookies/cookie_constants.h" 19 #include "net/first_party_sets/first_party_set_entry.h" 20 #include "net/first_party_sets/first_party_set_metadata.h" 21 #include "net/first_party_sets/first_party_sets_cache_filter.h" 22 23 namespace net { 24 25 // CookieAccessDelegate for testing. You can set the return value for a given 26 // cookie_domain (modulo any leading dot). Calling GetAccessSemantics() will 27 // then return the given value, or UNKNOWN if you haven't set one. 28 class TestCookieAccessDelegate : public CookieAccessDelegate { 29 public: 30 TestCookieAccessDelegate(); 31 32 TestCookieAccessDelegate(const TestCookieAccessDelegate&) = delete; 33 TestCookieAccessDelegate& operator=(const TestCookieAccessDelegate&) = delete; 34 35 ~TestCookieAccessDelegate() override; 36 37 // CookieAccessDelegate implementation: 38 CookieAccessSemantics GetAccessSemantics( 39 const CanonicalCookie& cookie) const override; 40 bool ShouldIgnoreSameSiteRestrictions( 41 const GURL& url, 42 const SiteForCookies& site_for_cookies) const override; 43 bool ShouldTreatUrlAsTrustworthy(const GURL& url) const override; 44 std::optional< 45 std::pair<FirstPartySetMetadata, FirstPartySetsCacheFilter::MatchInfo>> 46 ComputeFirstPartySetMetadataMaybeAsync( 47 const SchemefulSite& site, 48 const SchemefulSite* top_frame_site, 49 base::OnceCallback<void(FirstPartySetMetadata, 50 FirstPartySetsCacheFilter::MatchInfo)> callback) 51 const override; 52 std::optional<base::flat_map<SchemefulSite, FirstPartySetEntry>> 53 FindFirstPartySetEntries( 54 const base::flat_set<SchemefulSite>& sites, 55 base::OnceCallback< 56 void(base::flat_map<SchemefulSite, FirstPartySetEntry>)> callback) 57 const override; 58 59 // Sets the expected return value for any cookie whose Domain 60 // matches |cookie_domain|. Pass the value of |cookie.Domain()| and any 61 // leading dot will be discarded. 62 void SetExpectationForCookieDomain(const std::string& cookie_domain, 63 CookieAccessSemantics access_semantics); 64 65 // Sets the expected return value for ShouldAlwaysAttachSameSiteCookies. 66 // Can set schemes that always attach SameSite cookies, or schemes that always 67 // attach SameSite cookies if the request URL is secure. 68 void SetIgnoreSameSiteRestrictionsScheme( 69 const std::string& site_for_cookies_scheme, 70 bool require_secure_origin); 71 72 // Set the test delegate's First-Party Sets. The map's keys are the sites in 73 // the sets. Primary sites must be included among the keys for a given set. 74 void SetFirstPartySets( 75 const base::flat_map<SchemefulSite, FirstPartySetEntry>& sets); 76 set_invoke_callbacks_asynchronously(bool async)77 void set_invoke_callbacks_asynchronously(bool async) { 78 invoke_callbacks_asynchronously_ = async; 79 } 80 set_first_party_sets_cache_filter(FirstPartySetsCacheFilter filter)81 void set_first_party_sets_cache_filter(FirstPartySetsCacheFilter filter) { 82 first_party_sets_cache_filter_ = std::move(filter); 83 } 84 85 private: 86 // Finds a FirstPartySetEntry for the given site, if one exists. 87 std::optional<FirstPartySetEntry> FindFirstPartySetEntry( 88 const SchemefulSite& site) const; 89 90 // Discard any leading dot in the domain string. 91 std::string GetKeyForDomainValue(const std::string& domain) const; 92 93 // Invokes the given `callback` asynchronously or returns the result 94 // synchronously, depending on the configuration of this instance. 95 template <class T> 96 std::optional<T> RunMaybeAsync(T result, 97 base::OnceCallback<void(T)> callback) const; 98 99 std::map<std::string, CookieAccessSemantics> expectations_; 100 std::map<std::string, bool> ignore_samesite_restrictions_schemes_; 101 base::flat_map<SchemefulSite, FirstPartySetEntry> first_party_sets_; 102 FirstPartySetsCacheFilter first_party_sets_cache_filter_; 103 bool invoke_callbacks_asynchronously_ = false; 104 SchemefulSite trustworthy_site_ = 105 SchemefulSite(GURL("http://trustworthysitefortestdelegate.example")); 106 }; 107 108 } // namespace net 109 110 #endif // NET_COOKIES_TEST_COOKIE_ACCESS_DELEGATE_H_ 111