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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/attribution_reporting/destination_set.h"
#include <algorithm>
#include <optional>
#include <string>
#include <utility>
#include "base/check.h"
#include "base/containers/flat_set.h"
#include "base/types/expected.h"
#include "base/types/expected_macros.h"
#include "base/values.h"
#include "components/attribution_reporting/constants.h"
#include "components/attribution_reporting/source_registration_error.mojom-shared.h"
#include "components/attribution_reporting/suitable_origin.h"
#include "mojo/public/cpp/bindings/default_construct_tag.h"
#include "net/base/schemeful_site.h"
#include "third_party/abseil-cpp/absl/functional/overload.h"
namespace attribution_reporting {
namespace {
using ::attribution_reporting::mojom::SourceRegistrationError;
bool DestinationsValid(const DestinationSet::Destinations& destinations) {
return !destinations.empty() && destinations.size() <= kMaxDestinations &&
std::ranges::all_of(destinations, &IsSitePotentiallySuitable);
}
} // namespace
// static
std::optional<DestinationSet> DestinationSet::Create(
Destinations destinations) {
if (!DestinationsValid(destinations)) {
return std::nullopt;
}
return DestinationSet(std::move(destinations));
}
// static
base::expected<DestinationSet, SourceRegistrationError>
DestinationSet::FromJSON(const base::Value* v) {
if (!v) {
return base::unexpected(SourceRegistrationError::kDestinationMissing);
}
// Although we build this set iteratively, which results in O(n^2)
// construction, n is very small, so this is fine.
static_assert(kMaxDestinations == 3,
"Consider using more performant set construction if the size "
"limit increases.");
base::flat_set<net::SchemefulSite> destination_sites;
using AppendIfValidResult = base::expected<void, SourceRegistrationError>;
const auto append_if_valid =
[&](const std::string& str,
SourceRegistrationError error) -> AppendIfValidResult {
auto origin = SuitableOrigin::Deserialize(str);
if (!origin.has_value()) {
return base::unexpected(error);
}
destination_sites.emplace(*origin);
return base::ok();
};
RETURN_IF_ERROR(v->Visit(absl::Overload{
[&](const std::string& str) {
return append_if_valid(
str, SourceRegistrationError::kDestinationUntrustworthy);
},
[&](const base::Value::List& list) -> AppendIfValidResult {
if (list.empty()) {
return base::unexpected(
SourceRegistrationError::kDestinationWrongType);
}
destination_sites.reserve(std::min(list.size(), kMaxDestinations));
for (const auto& item : list) {
const std::string* str = item.GetIfString();
if (!str) {
return base::unexpected(
SourceRegistrationError::kDestinationWrongType);
}
RETURN_IF_ERROR(append_if_valid(
*str, SourceRegistrationError::kDestinationListUntrustworthy));
if (destination_sites.size() > kMaxDestinations) {
return base::unexpected(
SourceRegistrationError::kDestinationWrongType);
}
}
return base::ok();
},
[](const auto&) -> AppendIfValidResult {
return base::unexpected(SourceRegistrationError::kDestinationWrongType);
},
}));
return DestinationSet(std::move(destination_sites));
}
DestinationSet::DestinationSet(Destinations destinations)
: destinations_(std::move(destinations)) {
CHECK(IsValid());
}
DestinationSet::DestinationSet(mojo::DefaultConstruct::Tag) {
CHECK(!IsValid());
}
DestinationSet::~DestinationSet() = default;
DestinationSet::DestinationSet(const DestinationSet&) = default;
DestinationSet::DestinationSet(DestinationSet&&) = default;
DestinationSet& DestinationSet::operator=(const DestinationSet&) = default;
DestinationSet& DestinationSet::operator=(DestinationSet&&) = default;
bool DestinationSet::IsValid() const {
return DestinationsValid(destinations_);
}
base::Value DestinationSet::ToJson() const {
CHECK(IsValid());
if (destinations_.size() == 1) {
return base::Value(destinations_.begin()->Serialize());
}
auto list = base::Value::List::with_capacity(destinations_.size());
for (const auto& destination : destinations_) {
list.Append(destination.Serialize());
}
return base::Value(std::move(list));
}
} // namespace attribution_reporting
|