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 #include "net/first_party_sets/local_set_declaration.h"
6
7 #include "base/ranges/algorithm.h"
8 #include "net/base/schemeful_site.h"
9 #include "net/first_party_sets/first_party_set_entry.h"
10
11 namespace net {
12
13 LocalSetDeclaration::LocalSetDeclaration() = default;
14
LocalSetDeclaration(base::flat_map<SchemefulSite,FirstPartySetEntry> set_entries,base::flat_map<SchemefulSite,SchemefulSite> aliases)15 LocalSetDeclaration::LocalSetDeclaration(
16 base::flat_map<SchemefulSite, FirstPartySetEntry> set_entries,
17 base::flat_map<SchemefulSite, SchemefulSite> aliases)
18 : entries_(std::move(set_entries)), aliases_(std::move(aliases)) {
19 // Every alias must map to some canonical site in `entries_`.
20 CHECK(base::ranges::all_of(
21 aliases_, [&](const auto& p) { return entries_.contains(p.second); }));
22
23 if (!entries_.empty()) {
24 // Must not be a singleton set (i.e. must have more than one entry).
25 CHECK_GT(entries_.size() + aliases_.size(), 1u);
26
27 // All provided entries must have the same primary site. I.e., there must
28 // only be one set.
29 const SchemefulSite& primary = entries_.begin()->second.primary();
30 CHECK(base::ranges::all_of(
31 entries_,
32 [&](const std::pair<SchemefulSite, FirstPartySetEntry>& pair) {
33 return pair.second.primary() == primary;
34 }));
35 }
36 }
37
38 LocalSetDeclaration::~LocalSetDeclaration() = default;
39
40 LocalSetDeclaration::LocalSetDeclaration(const LocalSetDeclaration&) = default;
41 LocalSetDeclaration& LocalSetDeclaration::operator=(
42 const LocalSetDeclaration&) = default;
43
44 LocalSetDeclaration::LocalSetDeclaration(LocalSetDeclaration&&) = default;
45 LocalSetDeclaration& LocalSetDeclaration::operator=(LocalSetDeclaration&&) =
46 default;
47
48 } // namespace net
49