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(
36 site_counts,
37 [](const std::pair<SchemefulSite, int> p) { return p.second == 1; }));
38 }
39
40 SetsMutation::SetsMutation() = default;
41 SetsMutation::SetsMutation(SetsMutation&&) = default;
42 SetsMutation& SetsMutation::operator=(SetsMutation&&) = default;
43 SetsMutation::SetsMutation(const SetsMutation&) = default;
44 SetsMutation& SetsMutation::operator=(const SetsMutation&) = default;
45 SetsMutation::~SetsMutation() = default;
46
47 bool SetsMutation::operator==(const SetsMutation& other) const = default;
48
operator <<(std::ostream & os,const SetsMutation & mutation)49 std::ostream& operator<<(std::ostream& os, const SetsMutation& mutation) {
50 os << "replacements: {";
51 for (const auto& set : mutation.replacements()) {
52 for (const auto& pair : set) {
53 os << pair.first << " -> " << pair.second << ", ";
54 }
55 }
56 os << "}, additions: {";
57 for (const auto& set : mutation.additions()) {
58 for (const auto& pair : set) {
59 os << pair.first << " -> " << pair.second << ", ";
60 }
61 }
62 os << "}";
63 return os;
64 }
65
66 } // namespace net
67