xref: /aosp_15_r20/external/cronet/net/first_party_sets/first_party_set_entry.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 "base/strings/strcat.h"
11 #include "net/base/schemeful_site.h"
12 
13 namespace net {
14 
15 namespace {
16 
SiteTypeToString(SiteType site_type)17 std::string SiteTypeToString(SiteType site_type) {
18   switch (site_type) {
19     case SiteType::kPrimary:
20       return "kPrimary";
21     case SiteType::kAssociated:
22       return "kAssociated";
23     case SiteType::kService:
24       return "kService";
25   }
26 }
27 
28 }  // namespace
29 
30 FirstPartySetEntry::SiteIndex::SiteIndex() = default;
31 
SiteIndex(uint32_t value)32 FirstPartySetEntry::SiteIndex::SiteIndex(uint32_t value) : value_(value) {}
33 
34 bool FirstPartySetEntry::SiteIndex::operator==(const SiteIndex& other) const =
35     default;
36 
37 FirstPartySetEntry::FirstPartySetEntry() = default;
38 
FirstPartySetEntry(SchemefulSite primary,SiteType site_type,std::optional<FirstPartySetEntry::SiteIndex> site_index)39 FirstPartySetEntry::FirstPartySetEntry(
40     SchemefulSite primary,
41     SiteType site_type,
42     std::optional<FirstPartySetEntry::SiteIndex> site_index)
43     : primary_(primary), site_type_(site_type), site_index_(site_index) {
44   switch (site_type_) {
45     case SiteType::kPrimary:
46     case SiteType::kService:
47       CHECK(!site_index_.has_value());
48       break;
49     case SiteType::kAssociated:
50       break;
51   }
52 }
53 
FirstPartySetEntry(SchemefulSite primary,SiteType site_type,uint32_t site_index)54 FirstPartySetEntry::FirstPartySetEntry(SchemefulSite primary,
55                                        SiteType site_type,
56                                        uint32_t site_index)
57     : FirstPartySetEntry(
58           primary,
59           site_type,
60           std::make_optional(FirstPartySetEntry::SiteIndex(site_index))) {}
61 
62 FirstPartySetEntry::FirstPartySetEntry(const FirstPartySetEntry&) = default;
63 FirstPartySetEntry& FirstPartySetEntry::operator=(const FirstPartySetEntry&) =
64     default;
65 FirstPartySetEntry::FirstPartySetEntry(FirstPartySetEntry&&) = default;
66 FirstPartySetEntry& FirstPartySetEntry::operator=(FirstPartySetEntry&&) =
67     default;
68 
69 FirstPartySetEntry::~FirstPartySetEntry() = default;
70 
71 bool FirstPartySetEntry::operator==(const FirstPartySetEntry& other) const =
72     default;
73 
74 bool FirstPartySetEntry::operator!=(const FirstPartySetEntry& other) const =
75     default;
76 
77 // static
DeserializeSiteType(int value)78 std::optional<net::SiteType> FirstPartySetEntry::DeserializeSiteType(
79     int value) {
80   switch (value) {
81     case static_cast<int>(net::SiteType::kPrimary):
82       return net::SiteType::kPrimary;
83     case static_cast<int>(net::SiteType::kAssociated):
84       return net::SiteType::kAssociated;
85     case static_cast<int>(net::SiteType::kService):
86       return net::SiteType::kService;
87     default:
88       NOTREACHED() << "Unknown SiteType: " << value;
89   }
90   return std::nullopt;
91 }
92 
GetDebugString() const93 std::string FirstPartySetEntry::GetDebugString() const {
94   return base::StrCat({"{primary: ", primary_.GetDebugString(),
95                        ", site_type: ", SiteTypeToString(site_type_), "}"});
96 }
97 
operator <<(std::ostream & os,const FirstPartySetEntry::SiteIndex & index)98 std::ostream& operator<<(std::ostream& os,
99                          const FirstPartySetEntry::SiteIndex& index) {
100   os << index.value();
101   return os;
102 }
103 
operator <<(std::ostream & os,const FirstPartySetEntry & entry)104 std::ostream& operator<<(std::ostream& os, const FirstPartySetEntry& entry) {
105   os << "{" << entry.primary() << ", " << static_cast<int>(entry.site_type())
106      << ", ";
107   if (entry.site_index().has_value()) {
108     os << entry.site_index().value();
109   } else {
110     os << "{}";
111   }
112   os << "}";
113   return os;
114 }
115 
116 }  // namespace net
117