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