Dylan Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 1 | // Copyright 2021 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 "net/cookies/cookie_partition_key.h" |
| 6 | |
Dylan Cutler | 39fa227e | 2021-08-10 22:40:04 | [diff] [blame] | 7 | #include "base/feature_list.h" |
| 8 | #include "net/base/features.h" |
Dylan Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 9 | #include "net/cookies/cookie_constants.h" |
| 10 | |
| 11 | namespace net { |
| 12 | |
| 13 | CookiePartitionKey::CookiePartitionKey() = default; |
| 14 | |
| 15 | CookiePartitionKey::CookiePartitionKey(const SchemefulSite& site) |
| 16 | : site_(site) {} |
| 17 | |
| 18 | CookiePartitionKey::CookiePartitionKey(const GURL& url) |
| 19 | : site_(SchemefulSite(url)) {} |
| 20 | |
| 21 | CookiePartitionKey::CookiePartitionKey(const CookiePartitionKey& other) = |
| 22 | default; |
| 23 | |
| 24 | CookiePartitionKey::CookiePartitionKey(CookiePartitionKey&& other) = default; |
| 25 | |
| 26 | CookiePartitionKey& CookiePartitionKey::operator=( |
| 27 | const CookiePartitionKey& other) = default; |
| 28 | |
| 29 | CookiePartitionKey& CookiePartitionKey::operator=(CookiePartitionKey&& other) = |
| 30 | default; |
| 31 | |
| 32 | CookiePartitionKey::~CookiePartitionKey() = default; |
| 33 | |
| 34 | bool CookiePartitionKey::operator==(const CookiePartitionKey& other) const { |
| 35 | return site_ == other.site_; |
| 36 | } |
| 37 | |
Dylan Cutler | 8b928ad | 2021-08-11 16:24:09 | [diff] [blame^] | 38 | bool CookiePartitionKey::operator<(const CookiePartitionKey& other) const { |
| 39 | return site_ < other.site_; |
| 40 | } |
| 41 | |
Dylan Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 42 | // static |
| 43 | bool CookiePartitionKey::Serialize(const absl::optional<CookiePartitionKey>& in, |
| 44 | std::string& out) { |
| 45 | if (!in) { |
| 46 | out = kEmptyCookiePartitionKey; |
| 47 | return true; |
| 48 | } |
| 49 | if (in->site_.GetURL().SchemeIsFile()) { |
| 50 | out = in->site_.SerializeFileSiteWithHost(); |
| 51 | return true; |
| 52 | } |
| 53 | if (in->site_.opaque()) |
| 54 | return false; |
| 55 | out = in->site_.Serialize(); |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | // static |
| 60 | bool CookiePartitionKey::Deserialize(const std::string& in, |
| 61 | absl::optional<CookiePartitionKey>& out) { |
| 62 | if (in == kEmptyCookiePartitionKey) { |
| 63 | out = absl::nullopt; |
| 64 | return true; |
| 65 | } |
| 66 | auto schemeful_site = SchemefulSite::Deserialize(in); |
| 67 | // SchemfulSite is opaque if the input is invalid. |
| 68 | if (schemeful_site.opaque()) |
| 69 | return false; |
| 70 | out = absl::make_optional(CookiePartitionKey(schemeful_site)); |
| 71 | return true; |
| 72 | } |
| 73 | |
Dylan Cutler | 39fa227e | 2021-08-10 22:40:04 | [diff] [blame] | 74 | absl::optional<CookiePartitionKey> CookiePartitionKey::FromNetworkIsolationKey( |
| 75 | const NetworkIsolationKey& network_isolation_key) { |
| 76 | if (!base::FeatureList::IsEnabled(features::kPartitionedCookies)) |
| 77 | return absl::nullopt; |
| 78 | // TODO(crbug.com/1225444): Check if the top frame site is in a First-Party |
| 79 | // Set or if it is an extension URL. |
| 80 | absl::optional<net::SchemefulSite> top_frame_site = |
| 81 | network_isolation_key.GetTopFrameSite(); |
| 82 | if (!top_frame_site) |
| 83 | return absl::nullopt; |
| 84 | return absl::make_optional(net::CookiePartitionKey(top_frame_site.value())); |
| 85 | } |
| 86 | |
Dylan Cutler | 2730733 | 2021-08-02 21:00:41 | [diff] [blame] | 87 | } // namespace net |