Lily Chen | ab36a11 | 2019-09-19 20:17:28 | [diff] [blame] | 1 | // Copyright 2019 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 | #ifndef NET_COOKIES_COOKIE_ACCESS_DELEGATE_H_ |
| 6 | #define NET_COOKIES_COOKIE_ACCESS_DELEGATE_H_ |
| 7 | |
Peter Varga | ec19305 | 2021-12-01 10:25:05 | [diff] [blame] | 8 | #include <set> |
| 9 | |
cfredric | 326a0bc | 2022-01-12 18:51:30 | [diff] [blame] | 10 | #include "base/callback_forward.h" |
cfredric | cee9749 | 2021-01-29 18:48:45 | [diff] [blame] | 11 | #include "base/containers/flat_map.h" |
Lily Chen | ab36a11 | 2019-09-19 20:17:28 | [diff] [blame] | 12 | #include "net/base/net_export.h" |
cfredric | 326a0bc | 2022-01-12 18:51:30 | [diff] [blame] | 13 | #include "net/base/schemeful_site.h" |
Lily Chen | ab36a11 | 2019-09-19 20:17:28 | [diff] [blame] | 14 | #include "net/cookies/canonical_cookie.h" |
| 15 | #include "net/cookies/cookie_constants.h" |
Dylan Cutler | 69a51c9 | 2021-12-08 16:32:01 | [diff] [blame] | 16 | #include "net/cookies/cookie_partition_key.h" |
cfredric | b7ae6d3 | 2022-01-05 22:08:31 | [diff] [blame] | 17 | #include "net/cookies/first_party_set_metadata.h" |
cfredric | 362c4a0 | 2021-07-09 22:40:40 | [diff] [blame] | 18 | #include "net/cookies/same_party_context.h" |
Lei Zhang | 698df03c | 2021-05-21 04:23:34 | [diff] [blame] | 19 | #include "third_party/abseil-cpp/absl/types/optional.h" |
Lily Chen | 0db055b | 2019-11-15 20:29:02 | [diff] [blame] | 20 | #include "url/gurl.h" |
Lily Chen | ab36a11 | 2019-09-19 20:17:28 | [diff] [blame] | 21 | |
| 22 | namespace net { |
| 23 | |
cfredric | 716f3f80 | 2020-12-14 22:19:50 | [diff] [blame] | 24 | class SchemefulSite; |
Maks Orlovich | 8be0e25 | 2019-12-09 18:35:49 | [diff] [blame] | 25 | class SiteForCookies; |
| 26 | |
Lily Chen | ab36a11 | 2019-09-19 20:17:28 | [diff] [blame] | 27 | class NET_EXPORT CookieAccessDelegate { |
| 28 | public: |
| 29 | CookieAccessDelegate(); |
Peter Boström | 293b134 | 2021-09-22 17:31:43 | [diff] [blame] | 30 | |
| 31 | CookieAccessDelegate(const CookieAccessDelegate&) = delete; |
| 32 | CookieAccessDelegate& operator=(const CookieAccessDelegate&) = delete; |
| 33 | |
Lily Chen | ab36a11 | 2019-09-19 20:17:28 | [diff] [blame] | 34 | virtual ~CookieAccessDelegate(); |
| 35 | |
Maks Orlovich | bd04d78 | 2020-11-17 21:23:34 | [diff] [blame] | 36 | // Returns true if the passed in |url| should be permitted to access secure |
| 37 | // cookies in addition to URLs that normally do so. Returning false from this |
| 38 | // method on a URL that would already be treated as secure by default, e.g. an |
| 39 | // https:// one has no effect. |
| 40 | virtual bool ShouldTreatUrlAsTrustworthy(const GURL& url) const; |
| 41 | |
Lily Chen | ab36a11 | 2019-09-19 20:17:28 | [diff] [blame] | 42 | // Gets the access semantics to apply to |cookie|, based on its domain (i.e., |
| 43 | // whether a policy specifies that legacy access semantics should apply). |
| 44 | virtual CookieAccessSemantics GetAccessSemantics( |
| 45 | const CanonicalCookie& cookie) const = 0; |
| 46 | |
Lily Chen | 0db055b | 2019-11-15 20:29:02 | [diff] [blame] | 47 | // Returns whether a cookie should be attached regardless of its SameSite |
| 48 | // value vs the request context. |
| 49 | virtual bool ShouldIgnoreSameSiteRestrictions( |
| 50 | const GURL& url, |
Maks Orlovich | 8be0e25 | 2019-12-09 18:35:49 | [diff] [blame] | 51 | const SiteForCookies& site_for_cookies) const = 0; |
Lily Chen | 0db055b | 2019-11-15 20:29:02 | [diff] [blame] | 52 | |
cfredric | 0b52285 | 2022-01-19 18:44:53 | [diff] [blame^] | 53 | // Calls `callback` with metadata indicating whether `site` is same-party with |
cfredric | b7ae6d3 | 2022-01-05 22:08:31 | [diff] [blame] | 54 | // `party_context` and `top_frame_site`; and `site`'s owner, if applicable.. |
| 55 | // If `top_frame_site` is nullptr, then `site` will be checked only against |
| 56 | // `party_context`. |
cfredric | 0b52285 | 2022-01-19 18:44:53 | [diff] [blame^] | 57 | // |
| 58 | // `callback` may be invoked synchronously or asynchronously. |
| 59 | virtual void ComputeFirstPartySetMetadataMaybeAsync( |
cfredric | 716f3f80 | 2020-12-14 22:19:50 | [diff] [blame] | 60 | const net::SchemefulSite& site, |
cfredric | 362c4a0 | 2021-07-09 22:40:40 | [diff] [blame] | 61 | const net::SchemefulSite* top_frame_site, |
cfredric | 0b52285 | 2022-01-19 18:44:53 | [diff] [blame^] | 62 | const std::set<net::SchemefulSite>& party_context, |
| 63 | base::OnceCallback<void(FirstPartySetMetadata)> callback) const = 0; |
cfredric | 716f3f80 | 2020-12-14 22:19:50 | [diff] [blame] | 64 | |
Dylan Cutler | 69a51c9 | 2021-12-08 16:32:01 | [diff] [blame] | 65 | // Returns the owner of a `site`'s First-Party Set if `site` is in a |
| 66 | // non-trivial set. Returns nullopt otherwise. |
| 67 | virtual absl::optional<net::SchemefulSite> FindFirstPartySetOwner( |
| 68 | const net::SchemefulSite& site) const = 0; |
| 69 | |
| 70 | // Creates a CookiePartitionKey that takes whether the top-frame site is in a |
| 71 | // First-Party Set into account. If FPS are not enabled, it returns a cookie |
| 72 | // partition key that does not take FPS into account. |
| 73 | // |
| 74 | // Should always return nullopt if partitioned cookies are disabled or if |
| 75 | // the NIK has no top-frame site. |
| 76 | static absl::optional<CookiePartitionKey> CreateCookiePartitionKey( |
| 77 | const CookieAccessDelegate* delegate, |
| 78 | const NetworkIsolationKey& network_isolation_key); |
| 79 | |
| 80 | // Converts the CookiePartitionKey's site to its First-Party Set owner if |
| 81 | // the site is in a nontrivial set. |
cfredric | 1580f61 | 2022-01-13 19:53:30 | [diff] [blame] | 82 | // |
| 83 | // May invoke `callback` either synchronously or asynchronously. |
| 84 | static void FirstPartySetifyPartitionKey( |
Dylan Cutler | 69a51c9 | 2021-12-08 16:32:01 | [diff] [blame] | 85 | const CookieAccessDelegate* delegate, |
cfredric | 1580f61 | 2022-01-13 19:53:30 | [diff] [blame] | 86 | const CookiePartitionKey& cookie_partition_key, |
| 87 | base::OnceCallback<void(absl::optional<CookiePartitionKey>)> callback); |
Dylan Cutler | 69a51c9 | 2021-12-08 16:32:01 | [diff] [blame] | 88 | |
cfredric | 326a0bc | 2022-01-12 18:51:30 | [diff] [blame] | 89 | // Computes the First-Party Sets. |
| 90 | // |
| 91 | // May invoke `callback` either synchronously or asynchronously. |
| 92 | virtual void RetrieveFirstPartySets( |
| 93 | base::OnceCallback<void( |
| 94 | base::flat_map<net::SchemefulSite, std::set<net::SchemefulSite>>)> |
| 95 | callback) const = 0; |
Lily Chen | ab36a11 | 2019-09-19 20:17:28 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | } // namespace net |
| 99 | |
| 100 | #endif // NET_COOKIES_COOKIE_ACCESS_DELEGATE_H_ |