File: local_set_declaration.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (117 lines) | stat: -rw-r--r-- 3,899 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/first_party_sets/local_set_declaration.h"

#include <algorithm>
#include <optional>

#include "base/containers/contains.h"
#include "base/logging.h"
#include "net/base/schemeful_site.h"
#include "net/first_party_sets/first_party_set_entry.h"
#include "net/first_party_sets/sets_mutation.h"

namespace net {

namespace {
bool CheckPreconditions(
    const base::flat_map<SchemefulSite, FirstPartySetEntry>& entries,
    const base::flat_map<SchemefulSite, SchemefulSite>& aliases,
    bool emit_errors) {
  auto emit = [&](std::string_view message) {
    if (emit_errors) {
      LOG(ERROR) << message;
    }
  };

  if (!std::ranges::all_of(aliases, [&](const auto& p) {
        const auto& [alias_site, canonical_site] = p;
        // The canonical entry must exist.
        if (!base::Contains(entries, canonical_site)) {
          emit(
              "Invalid local Related Website Set: alias names a site that has "
              "no entry in the set.");
          return false;
        }
        // The alias entry must not exist explicitly.
        if (base::Contains(entries, alias_site)) {
          emit(
              "Invalid local Related Website Set: alias site should not be "
              "listed in `entries`.");
          return false;
        }
        return true;
      })) {
    return false;
  }

  if (!entries.empty()) {
    // Must not be a singleton set.
    if (entries.size() + aliases.size() <= 1) {
      emit(
          "Invalid local Related Website Set: set must include more than one "
          "site.");
      return false;
    }

    // All provided entries must have the same primary site. I.e., there must
    // only be one set.
    const SchemefulSite& primary = entries.begin()->second.primary();
    if (!std::ranges::all_of(
            entries,
            [&](const std::pair<SchemefulSite, FirstPartySetEntry>& pair) {
              return pair.second.primary() == primary;
            })) {
      // More than one set was provided. That is (currently) unsupported.
      emit(
          "Invalid local Related Website Set: entries must all list the same "
          "primary site.");
      return false;
    }
  }
  return true;
}
}  // namespace

// static
std::optional<LocalSetDeclaration> LocalSetDeclaration::Create(
    base::flat_map<SchemefulSite, FirstPartySetEntry> set_entries,
    base::flat_map<SchemefulSite, SchemefulSite> aliases,
    bool emit_errors) {
  if (!CheckPreconditions(set_entries, aliases, emit_errors)) {
    return std::nullopt;
  }
  return LocalSetDeclaration(std::move(set_entries), std::move(aliases));
}

LocalSetDeclaration::LocalSetDeclaration() = default;

LocalSetDeclaration::LocalSetDeclaration(
    base::flat_map<SchemefulSite, FirstPartySetEntry> set_entries,
    base::flat_map<SchemefulSite, SchemefulSite> aliases)
    : entries_(std::move(set_entries)), aliases_(std::move(aliases)) {}

LocalSetDeclaration::~LocalSetDeclaration() = default;

LocalSetDeclaration::LocalSetDeclaration(const LocalSetDeclaration&) = default;
LocalSetDeclaration& LocalSetDeclaration::operator=(
    const LocalSetDeclaration&) = default;

LocalSetDeclaration::LocalSetDeclaration(LocalSetDeclaration&&) = default;
LocalSetDeclaration& LocalSetDeclaration::operator=(LocalSetDeclaration&&) =
    default;

SetsMutation LocalSetDeclaration::ComputeMutation() const {
  base::flat_map<SchemefulSite, FirstPartySetEntry> entries = entries_;

  for (const auto& [alias, canonical] : aliases_) {
    entries.emplace(alias, entries.find(canonical)->second);
  }
  // A local set declaration is treated as a "replacement" set.
  return SetsMutation(/*replacement_sets=*/{std::move(entries)},
                      /*addition_sets=*/{}, aliases_);
}

}  // namespace net