blob: 055866802c3a3970fbd07552287b3f28df11408f [file] [log] [blame]
Dylan Cutler27307332021-08-02 21:00:411// 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 Cutler39fa227e2021-08-10 22:40:047#include "base/feature_list.h"
8#include "net/base/features.h"
Dylan Cutler27307332021-08-02 21:00:419#include "net/cookies/cookie_constants.h"
10
11namespace net {
12
13CookiePartitionKey::CookiePartitionKey() = default;
14
15CookiePartitionKey::CookiePartitionKey(const SchemefulSite& site)
16 : site_(site) {}
17
18CookiePartitionKey::CookiePartitionKey(const GURL& url)
19 : site_(SchemefulSite(url)) {}
20
21CookiePartitionKey::CookiePartitionKey(const CookiePartitionKey& other) =
22 default;
23
24CookiePartitionKey::CookiePartitionKey(CookiePartitionKey&& other) = default;
25
26CookiePartitionKey& CookiePartitionKey::operator=(
27 const CookiePartitionKey& other) = default;
28
29CookiePartitionKey& CookiePartitionKey::operator=(CookiePartitionKey&& other) =
30 default;
31
32CookiePartitionKey::~CookiePartitionKey() = default;
33
34bool CookiePartitionKey::operator==(const CookiePartitionKey& other) const {
35 return site_ == other.site_;
36}
37
Dylan Cutler8b928ad2021-08-11 16:24:0938bool CookiePartitionKey::operator<(const CookiePartitionKey& other) const {
39 return site_ < other.site_;
40}
41
Dylan Cutler27307332021-08-02 21:00:4142// static
43bool 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
60bool 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 Cutler39fa227e2021-08-10 22:40:0474absl::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 Cutler27307332021-08-02 21:00:4187} // namespace net