1 // Copyright 2022 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 #include "net/first_party_sets/first_party_set_entry.h"
6
7 #include <tuple>
8
9 #include "base/notreached.h"
10 #include "net/base/schemeful_site.h"
11
12 namespace net {
13
14 FirstPartySetEntry::SiteIndex::SiteIndex() = default;
15
SiteIndex(uint32_t value)16 FirstPartySetEntry::SiteIndex::SiteIndex(uint32_t value) : value_(value) {}
17
operator ==(const SiteIndex & other) const18 bool FirstPartySetEntry::SiteIndex::operator==(const SiteIndex& other) const {
19 return value_ == other.value_;
20 }
21
22 FirstPartySetEntry::FirstPartySetEntry() = default;
23
FirstPartySetEntry(SchemefulSite primary,SiteType site_type,absl::optional<FirstPartySetEntry::SiteIndex> site_index)24 FirstPartySetEntry::FirstPartySetEntry(
25 SchemefulSite primary,
26 SiteType site_type,
27 absl::optional<FirstPartySetEntry::SiteIndex> site_index)
28 : primary_(primary), site_type_(site_type), site_index_(site_index) {
29 switch (site_type_) {
30 case SiteType::kPrimary:
31 case SiteType::kService:
32 CHECK(!site_index_.has_value());
33 break;
34 case SiteType::kAssociated:
35 break;
36 }
37 }
38
FirstPartySetEntry(SchemefulSite primary,SiteType site_type,uint32_t site_index)39 FirstPartySetEntry::FirstPartySetEntry(SchemefulSite primary,
40 SiteType site_type,
41 uint32_t site_index)
42 : FirstPartySetEntry(
43 primary,
44 site_type,
45 absl::make_optional(FirstPartySetEntry::SiteIndex(site_index))) {}
46
47 FirstPartySetEntry::FirstPartySetEntry(const FirstPartySetEntry&) = default;
48 FirstPartySetEntry& FirstPartySetEntry::operator=(const FirstPartySetEntry&) =
49 default;
50 FirstPartySetEntry::FirstPartySetEntry(FirstPartySetEntry&&) = default;
51 FirstPartySetEntry& FirstPartySetEntry::operator=(FirstPartySetEntry&&) =
52 default;
53
54 FirstPartySetEntry::~FirstPartySetEntry() = default;
55
operator ==(const FirstPartySetEntry & other) const56 bool FirstPartySetEntry::operator==(const FirstPartySetEntry& other) const {
57 return std::tie(primary_, site_type_, site_index_) ==
58 std::tie(other.primary_, other.site_type_, other.site_index_);
59 }
60
operator !=(const FirstPartySetEntry & other) const61 bool FirstPartySetEntry::operator!=(const FirstPartySetEntry& other) const {
62 return !(*this == other);
63 }
64
65 // static
DeserializeSiteType(int value)66 absl::optional<net::SiteType> FirstPartySetEntry::DeserializeSiteType(
67 int value) {
68 switch (value) {
69 case static_cast<int>(net::SiteType::kPrimary):
70 return net::SiteType::kPrimary;
71 case static_cast<int>(net::SiteType::kAssociated):
72 return net::SiteType::kAssociated;
73 case static_cast<int>(net::SiteType::kService):
74 return net::SiteType::kService;
75 default:
76 NOTREACHED() << "Unknown SiteType: " << value;
77 }
78 return absl::nullopt;
79 }
80
operator <<(std::ostream & os,const FirstPartySetEntry::SiteIndex & index)81 std::ostream& operator<<(std::ostream& os,
82 const FirstPartySetEntry::SiteIndex& index) {
83 os << index.value();
84 return os;
85 }
86
operator <<(std::ostream & os,const FirstPartySetEntry & entry)87 std::ostream& operator<<(std::ostream& os, const FirstPartySetEntry& entry) {
88 os << "{" << entry.primary() << ", " << static_cast<int>(entry.site_type())
89 << ", ";
90 if (entry.site_index().has_value()) {
91 os << entry.site_index().value();
92 } else {
93 os << "{}";
94 }
95 os << "}";
96 return os;
97 }
98
99 } // namespace net
100