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