xref: /aosp_15_r20/external/cronet/net/first_party_sets/first_party_set_entry.h (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 #ifndef NET_FIRST_PARTY_SETS_FIRST_PARTY_SET_ENTRY_H_
6 #define NET_FIRST_PARTY_SETS_FIRST_PARTY_SET_ENTRY_H_
7 
8 #include "net/base/net_export.h"
9 #include "net/base/schemeful_site.h"
10 
11 namespace net {
12 
13 // These values are persisted to DB. Entries should not be renumbered and
14 // numeric values should never be reused.
15 enum class SiteType {
16   // The First-Party Set declaration listed this site as the "primary" site for
17   // the set.
18   kPrimary = 0,
19   // The First-Party Set declaration listed this site as an associated site in
20   // the set.
21   kAssociated = 1,
22   // The First-Party Set declaration listed this site as a service site in the
23   // set.
24   kService = 2,
25 
26   // Update FirstPartySetEntry::DeserializeSiteType if new SiteType is added.
27 };
28 
29 // This class bundles together metadata associated with an entry in a
30 // First-Party Set.
31 class NET_EXPORT FirstPartySetEntry {
32  public:
33   class NET_EXPORT SiteIndex {
34    public:
35     SiteIndex();
36     explicit SiteIndex(uint32_t value);
37 
38     bool operator==(const SiteIndex& other) const;
39 
value()40     uint32_t value() const { return value_; }
41 
42    private:
43     uint32_t value_;
44   };
45 
46   FirstPartySetEntry();
47   // `primary` is the primary site in the First-Party Set associated with this
48   // entry.
49   FirstPartySetEntry(SchemefulSite primary,
50                      SiteType site_type,
51                      std::optional<SiteIndex> site_index);
52   FirstPartySetEntry(SchemefulSite primary,
53                      SiteType site_type,
54                      uint32_t site_index);
55 
56   FirstPartySetEntry(const FirstPartySetEntry&);
57   FirstPartySetEntry& operator=(const FirstPartySetEntry&);
58   FirstPartySetEntry(FirstPartySetEntry&&);
59   FirstPartySetEntry& operator=(FirstPartySetEntry&&);
60 
61   ~FirstPartySetEntry();
62 
63   bool operator==(const FirstPartySetEntry& other) const;
64   bool operator!=(const FirstPartySetEntry& other) const;
65 
66   static std::optional<net::SiteType> DeserializeSiteType(int value);
67 
68   std::string GetDebugString() const;
69 
primary()70   const SchemefulSite& primary() const { return primary_; }
71 
site_type()72   SiteType site_type() const { return site_type_; }
73 
site_index()74   const std::optional<SiteIndex>& site_index() const { return site_index_; }
75 
76  private:
77   // The primary site associated with this site's set.
78   SchemefulSite primary_;
79   // The type associated with this site.
80   SiteType site_type_;
81   // The index of this site in the set declaration, if a meaningful index
82   // exists. Primary sites do not have indices, nor do sites that were defined
83   // or affected by an enterprise policy set.
84   std::optional<SiteIndex> site_index_;
85 };
86 
87 NET_EXPORT std::ostream& operator<<(
88     std::ostream& os,
89     const FirstPartySetEntry::SiteIndex& site_index);
90 NET_EXPORT std::ostream& operator<<(std::ostream& os,
91                                     const FirstPartySetEntry& fpse);
92 
93 }  // namespace net
94 
95 #endif  // NET_FIRST_PARTY_SETS_FIRST_PARTY_SET_ENTRY_H_
96