1 // Copyright 2023 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/sets_mutation.h"
6
7 #include <map>
8 #include <utility>
9
10 #include "base/ranges/algorithm.h"
11 #include "net/base/schemeful_site.h"
12 #include "net/first_party_sets/first_party_set_entry.h"
13
14 namespace net {
15
SetsMutation(std::vector<base::flat_map<SchemefulSite,FirstPartySetEntry>> replacement_sets,std::vector<base::flat_map<SchemefulSite,FirstPartySetEntry>> addition_sets)16 SetsMutation::SetsMutation(
17 std::vector<base::flat_map<SchemefulSite, FirstPartySetEntry>>
18 replacement_sets,
19 std::vector<base::flat_map<SchemefulSite, FirstPartySetEntry>>
20 addition_sets)
21 : replacements_(std::move(replacement_sets)),
22 additions_(std::move(addition_sets)) {
23 std::map<SchemefulSite, int> site_counts;
24
25 for (const auto& set : replacements_) {
26 for (const auto& [site, unused_entry] : set) {
27 site_counts[site]++;
28 }
29 }
30 for (const auto& set : additions_) {
31 for (const auto& [site, unused_entry] : set) {
32 site_counts[site]++;
33 }
34 }
35 CHECK(base::ranges::all_of(site_counts,
36 [](const std::pair<const SchemefulSite, int>& p) {
37 return p.second == 1;
38 }));
39 }
40
41 SetsMutation::SetsMutation() = default;
42 SetsMutation::SetsMutation(SetsMutation&&) = default;
43 SetsMutation& SetsMutation::operator=(SetsMutation&&) = default;
44 SetsMutation::SetsMutation(const SetsMutation&) = default;
45 SetsMutation& SetsMutation::operator=(const SetsMutation&) = default;
46 SetsMutation::~SetsMutation() = default;
47
48 bool SetsMutation::operator==(const SetsMutation& other) const = default;
49
operator <<(std::ostream & os,const SetsMutation & mutation)50 std::ostream& operator<<(std::ostream& os, const SetsMutation& mutation) {
51 os << "replacements: {";
52 for (const auto& set : mutation.replacements()) {
53 for (const auto& pair : set) {
54 os << pair.first << " -> " << pair.second << ", ";
55 }
56 }
57 os << "}, additions: {";
58 for (const auto& set : mutation.additions()) {
59 for (const auto& pair : set) {
60 os << pair.first << " -> " << pair.second << ", ";
61 }
62 }
63 os << "}";
64 return os;
65 }
66
67 } // namespace net
68