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_SETS_CONTEXT_CONFIG_H_ 6 #define NET_FIRST_PARTY_SETS_FIRST_PARTY_SETS_CONTEXT_CONFIG_H_ 7 8 #include <optional> 9 10 #include "base/containers/flat_map.h" 11 #include "base/functional/function_ref.h" 12 #include "net/base/schemeful_site.h" 13 #include "net/first_party_sets/first_party_set_entry_override.h" 14 15 namespace mojo { 16 template <typename DataViewType, typename T> 17 struct StructTraits; 18 } // namespace mojo 19 namespace network::mojom { 20 class FirstPartySetsContextConfigDataView; 21 } // namespace network::mojom 22 23 namespace net { 24 25 // This struct bundles together the customized settings to First-Party Sets 26 // info in the given network context. 27 class NET_EXPORT FirstPartySetsContextConfig { 28 public: 29 FirstPartySetsContextConfig(); 30 explicit FirstPartySetsContextConfig( 31 base::flat_map<SchemefulSite, FirstPartySetEntryOverride> customizations); 32 33 FirstPartySetsContextConfig(FirstPartySetsContextConfig&& other); 34 FirstPartySetsContextConfig& operator=(FirstPartySetsContextConfig&& other); 35 36 ~FirstPartySetsContextConfig(); 37 38 FirstPartySetsContextConfig Clone() const; 39 40 bool operator==(const FirstPartySetsContextConfig& other) const; 41 empty()42 bool empty() const { return customizations_.empty(); } 43 44 // Finds an override for the given site, in this context. Returns: 45 // - nullopt if no override was found. 46 // - optional(override) if an override was found. The override may be a 47 // deletion or a modification/addition. 48 std::optional<FirstPartySetEntryOverride> FindOverride( 49 const SchemefulSite& site) const; 50 51 // Returns whether an override can be found for the given site in this 52 // context. 53 bool Contains(const SchemefulSite& site) const; 54 55 // Synchronously iterate over all the override entries. Each iteration will be 56 // invoked with the relevant site and the override that applies to it. 57 // 58 // Returns early if any of the iterations returns false. Returns false if 59 // iteration was incomplete; true if all iterations returned true. No 60 // guarantees are made re: iteration order. 61 bool ForEachCustomizationEntry( 62 base::FunctionRef<bool(const SchemefulSite&, 63 const FirstPartySetEntryOverride&)> f) const; 64 65 private: 66 // mojo (de)serialization needs access to private details. 67 friend struct mojo::StructTraits< 68 network::mojom::FirstPartySetsContextConfigDataView, 69 FirstPartySetsContextConfig>; 70 71 base::flat_map<SchemefulSite, FirstPartySetEntryOverride> customizations_; 72 }; 73 74 } // namespace net 75 76 #endif // NET_FIRST_PARTY_SETS_FIRST_PARTY_SETS_CONTEXT_CONFIG_H_