• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(const SetsMutation&) = default;
43 SetsMutation::~SetsMutation() = default;
44 
45 bool SetsMutation::operator==(const SetsMutation& other) const = default;
46 
operator <<(std::ostream & os,const SetsMutation & mutation)47 std::ostream& operator<<(std::ostream& os, const SetsMutation& mutation) {
48   os << "replacements: {";
49   for (const auto& set : mutation.replacements()) {
50     for (const auto& pair : set) {
51       os << pair.first << " -> " << pair.second << ", ";
52     }
53   }
54   os << "}, additions: {";
55   for (const auto& set : mutation.additions()) {
56     for (const auto& pair : set) {
57       os << pair.first << " -> " << pair.second << ", ";
58     }
59   }
60   os << "}";
61   return os;
62 }
63 
64 }  // namespace net
65