xref: /aosp_15_r20/external/cronet/net/first_party_sets/first_party_set_entry_override.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_OVERRIDE_H_
6 #define NET_FIRST_PARTY_SETS_FIRST_PARTY_SET_ENTRY_OVERRIDE_H_
7 
8 #include <optional>
9 
10 #include "net/base/net_export.h"
11 #include "net/first_party_sets/first_party_set_entry.h"
12 
13 namespace mojo {
14 template <typename DataViewType, typename T>
15 struct StructTraits;
16 }  // namespace mojo
17 namespace network::mojom {
18 class FirstPartySetEntryOverrideDataView;
19 }  // namespace network::mojom
20 
21 namespace net {
22 
23 // This class represents a single modification to be applied on top of the
24 // global First-Party Sets list. A modifications may be a deletion, remapping,
25 // or new mapping.
26 class NET_EXPORT FirstPartySetEntryOverride {
27  public:
28   // Creates a new modification representing a deletion.
29   FirstPartySetEntryOverride();
30   // Creates a new modification representing a remapping/additional mapping.
31   explicit FirstPartySetEntryOverride(FirstPartySetEntry entry);
32 
33   FirstPartySetEntryOverride(FirstPartySetEntryOverride&& other);
34   FirstPartySetEntryOverride& operator=(FirstPartySetEntryOverride&& other);
35   FirstPartySetEntryOverride(const FirstPartySetEntryOverride& other);
36   FirstPartySetEntryOverride& operator=(
37       const FirstPartySetEntryOverride& other);
38 
39   ~FirstPartySetEntryOverride();
40 
41   bool operator==(const FirstPartySetEntryOverride& other) const;
42 
43   // Returns true iff this override is a deletion.
IsDeletion()44   bool IsDeletion() const { return !entry_.has_value(); }
45 
46   // Returns the new target entry, if this override is not a deletion. Must not
47   // be called if `IsDeletion()` is true.
GetEntry()48   const FirstPartySetEntry& GetEntry() const {
49     CHECK(!IsDeletion());
50     return entry_.value();
51   }
52 
53  private:
54   // mojo (de)serialization needs access to private details.
55   friend struct mojo::StructTraits<
56       network::mojom::FirstPartySetEntryOverrideDataView,
57       FirstPartySetEntryOverride>;
58 
59   std::optional<FirstPartySetEntry> entry_;
60 };
61 
62 NET_EXPORT std::ostream& operator<<(std::ostream& os,
63                                     const FirstPartySetEntryOverride& override);
64 
65 }  // namespace net
66 
67 #endif  // NET_FIRST_PARTY_SETS_FIRST_PARTY_SET_ENTRY_OVERRIDE_H_