Skip to content

Commit

Permalink
Move AttributionAggregationKeys to components/attribution_reporting
Browse files Browse the repository at this point in the history
In preparation for moving all related JSON parsing to that directory.

Bug: 1379009
Change-Id: Ic03f0b1419ac34d57d59c475c75e456e3660952a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4001889
Reviewed-by: Nan Lin <linnan@chromium.org>
Reviewed-by: Alex Moshchuk <alexmos@chromium.org>
Quick-Run: Andrew Paseltiner <apaseltiner@chromium.org>
Commit-Queue: Andrew Paseltiner <apaseltiner@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1067673}
  • Loading branch information
Andrew Paseltiner authored and Chromium LUCI CQ committed Nov 4, 2022
1 parent 8e3088a commit a652a71
Show file tree
Hide file tree
Showing 26 changed files with 201 additions and 150 deletions.
16 changes: 16 additions & 0 deletions components/attribution_reporting/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ mojom("source_registration_error_mojom") {

component("attribution_reporting") {
sources = [
"aggregation_keys.cc",
"aggregation_keys.h",
"constants.h",
"parse.cc",
"parse.h",
]

public_deps = [
":source_registration_error_mojom",
"//base",
"//url",
]
Expand All @@ -41,6 +44,19 @@ source_set("unit_tests") {
]
}

source_set("test_support") {
testonly = true

sources = [
"test_utils.cc",
"test_utils.h",
]

public_deps = [ ":attribution_reporting" ]

deps = [ "//base" ]
}

fuzzer_test("os_source_parse_fuzzer") {
sources = [ "os_source_parse_fuzzer.cc" ]
deps = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/attribution_reporting/attribution_aggregation_keys.h"
#include "components/attribution_reporting/aggregation_keys.h"

#include <string>
#include <utility>
Expand All @@ -16,33 +16,31 @@
#include "components/attribution_reporting/source_registration_error.mojom.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

namespace content {
namespace attribution_reporting {

namespace {
using ::attribution_reporting::mojom::SourceRegistrationError;
} // namespace

// static
absl::optional<AttributionAggregationKeys> AttributionAggregationKeys::FromKeys(
Keys keys) {
absl::optional<AggregationKeys> AggregationKeys::FromKeys(Keys keys) {
bool is_valid =
keys.size() <=
attribution_reporting::kMaxAggregationKeysPerSourceOrTrigger &&
base::ranges::all_of(keys, [](const auto& key) {
return key.first.size() <=
attribution_reporting::kMaxBytesPerAggregationKeyId;
});
return is_valid
? absl::make_optional(AttributionAggregationKeys(std::move(keys)))
: absl::nullopt;
return is_valid ? absl::make_optional(AggregationKeys(std::move(keys)))
: absl::nullopt;
}

// static
base::expected<AttributionAggregationKeys, SourceRegistrationError>
AttributionAggregationKeys::FromJSON(const base::Value* value) {
base::expected<AggregationKeys, SourceRegistrationError>
AggregationKeys::FromJSON(const base::Value* value) {
// TODO(johnidel): Consider logging registration JSON metrics here.
if (!value)
return AttributionAggregationKeys();
return AggregationKeys();

const base::Value::Dict* dict = value->GetIfDict();
if (!dict)
Expand Down Expand Up @@ -81,26 +79,21 @@ AttributionAggregationKeys::FromJSON(const base::Value* value) {
keys.emplace_back(key_id, key);
}

return AttributionAggregationKeys(Keys(base::sorted_unique, std::move(keys)));
return AggregationKeys(Keys(base::sorted_unique, std::move(keys)));
}

AttributionAggregationKeys::AttributionAggregationKeys(Keys keys)
: keys_(std::move(keys)) {}
AggregationKeys::AggregationKeys(Keys keys) : keys_(std::move(keys)) {}

AttributionAggregationKeys::AttributionAggregationKeys() = default;
AggregationKeys::AggregationKeys() = default;

AttributionAggregationKeys::~AttributionAggregationKeys() = default;
AggregationKeys::~AggregationKeys() = default;

AttributionAggregationKeys::AttributionAggregationKeys(
const AttributionAggregationKeys&) = default;
AggregationKeys::AggregationKeys(const AggregationKeys&) = default;

AttributionAggregationKeys::AttributionAggregationKeys(
AttributionAggregationKeys&&) = default;
AggregationKeys::AggregationKeys(AggregationKeys&&) = default;

AttributionAggregationKeys& AttributionAggregationKeys::operator=(
const AttributionAggregationKeys&) = default;
AggregationKeys& AggregationKeys::operator=(const AggregationKeys&) = default;

AttributionAggregationKeys& AttributionAggregationKeys::operator=(
AttributionAggregationKeys&&) = default;
AggregationKeys& AggregationKeys::operator=(AggregationKeys&&) = default;

} // namespace content
} // namespace attribution_reporting
52 changes: 52 additions & 0 deletions components/attribution_reporting/aggregation_keys.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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.

#ifndef COMPONENTS_ATTRIBUTION_REPORTING_AGGREGATION_KEYS_H_
#define COMPONENTS_ATTRIBUTION_REPORTING_AGGREGATION_KEYS_H_

#include <string>

#include "base/component_export.h"
#include "base/containers/flat_map.h"
#include "base/types/expected.h"
#include "components/attribution_reporting/source_registration_error.mojom-forward.h"
#include "third_party/abseil-cpp/absl/numeric/int128.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

namespace base {
class Value;
} // namespace base

namespace attribution_reporting {

class COMPONENT_EXPORT(ATTRIBUTION_REPORTING) AggregationKeys {
public:
using Keys = base::flat_map<std::string, absl::uint128>;

// Returns `absl::nullopt` if `keys` is invalid.
static absl::optional<AggregationKeys> FromKeys(Keys keys);

static base::expected<AggregationKeys, mojom::SourceRegistrationError>
FromJSON(const base::Value*);

AggregationKeys();
~AggregationKeys();

AggregationKeys(const AggregationKeys&);
AggregationKeys(AggregationKeys&&);

AggregationKeys& operator=(const AggregationKeys&);
AggregationKeys& operator=(AggregationKeys&&);

const Keys& keys() const { return keys_; }

private:
explicit AggregationKeys(Keys keys);

Keys keys_;
};

} // namespace attribution_reporting

#endif // COMPONENTS_ATTRIBUTION_REPORTING_AGGREGATION_KEYS_H_
29 changes: 29 additions & 0 deletions components/attribution_reporting/test_utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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 "components/attribution_reporting/test_utils.h"

#include <ostream>

#include "components/attribution_reporting/aggregation_keys.h"

namespace attribution_reporting {

bool operator==(const AggregationKeys& a, const AggregationKeys& b) {
return a.keys() == b.keys();
}

std::ostream& operator<<(std::ostream& out,
const AggregationKeys& aggregation_keys) {
out << "{";

const char* separator = "";
for (const auto& [key_id, key] : aggregation_keys.keys()) {
out << separator << key_id << ":" << key;
separator = ", ";
}
return out << "}";
}

} // namespace attribution_reporting
20 changes: 20 additions & 0 deletions components/attribution_reporting/test_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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.

#ifndef COMPONENTS_ATTRIBUTION_REPORTING_TEST_UTILS_H_
#define COMPONENTS_ATTRIBUTION_REPORTING_TEST_UTILS_H_

#include <ostream>

namespace attribution_reporting {

class AggregationKeys;

bool operator==(const AggregationKeys&, const AggregationKeys&);

std::ostream& operator<<(std::ostream&, const AggregationKeys&);

} // namespace attribution_reporting

#endif // COMPONENTS_ATTRIBUTION_REPORTING_TEST_UTILS_H_
2 changes: 0 additions & 2 deletions content/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,6 @@ source_set("browser") {
"attribution_reporting/attribution_aggregatable_trigger_data.h",
"attribution_reporting/attribution_aggregatable_values.cc",
"attribution_reporting/attribution_aggregatable_values.h",
"attribution_reporting/attribution_aggregation_keys.cc",
"attribution_reporting/attribution_aggregation_keys.h",
"attribution_reporting/attribution_config.cc",
"attribution_reporting/attribution_cookie_checker.h",
"attribution_reporting/attribution_cookie_checker_impl.cc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "base/values.h"
#include "components/attribution_reporting/aggregation_keys.h"
#include "components/attribution_reporting/constants.h"
#include "content/browser/aggregation_service/aggregatable_report.h"
#include "content/browser/attribution_reporting/aggregatable_histogram_contribution.h"
#include "content/browser/attribution_reporting/attribution_aggregatable_trigger_data.h"
#include "content/browser/attribution_reporting/attribution_aggregatable_values.h"
#include "content/browser/attribution_reporting/attribution_aggregation_keys.h"
#include "content/browser/attribution_reporting/attribution_filter_data.h"
#include "content/browser/attribution_reporting/attribution_info.h"
#include "content/browser/attribution_reporting/attribution_report.h"
Expand Down Expand Up @@ -52,13 +52,13 @@ std::string SerializeTimeRoundedDownToWholeDayInSeconds(base::Time time) {
std::vector<AggregatableHistogramContribution> CreateAggregatableHistogram(
const AttributionFilterData& source_filter_data,
AttributionSourceType source_type,
const AttributionAggregationKeys& keys,
const attribution_reporting::AggregationKeys& keys,
const std::vector<AttributionAggregatableTriggerData>&
aggregatable_trigger_data,
const AttributionAggregatableValues& aggregatable_values) {
int num_trigger_data_filtered = 0;

AttributionAggregationKeys::Keys buckets = keys.keys();
attribution_reporting::AggregationKeys::Keys buckets = keys.keys();

// For each piece of trigger data specified, check if its filters/not_filters
// match for the given source, and if applicable modify the bucket based on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ namespace absl {
class uint128;
} // namespace absl

namespace attribution_reporting {
class AggregationKeys;
} // namespace attribution_reporting

namespace content {

class AggregatableHistogramContribution;
class AggregatableReportRequest;
class AttributionAggregationKeys;
class AttributionAggregatableTriggerData;
class AttributionAggregatableValues;
class AttributionFilterData;
Expand All @@ -31,7 +34,7 @@ CONTENT_EXPORT std::vector<AggregatableHistogramContribution>
CreateAggregatableHistogram(
const AttributionFilterData& source_filter_data,
AttributionSourceType,
const AttributionAggregationKeys& keys,
const attribution_reporting::AggregationKeys& keys,
const std::vector<AttributionAggregatableTriggerData>&
aggregatable_trigger_data,
const AttributionAggregatableValues& aggregatable_values);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
#include "base/test/metrics/histogram_tester.h"
#include "base/time/time.h"
#include "base/values.h"
#include "components/attribution_reporting/aggregation_keys.h"
#include "content/browser/attribution_reporting/aggregatable_histogram_contribution.h"
#include "content/browser/attribution_reporting/attribution_aggregatable_trigger_data.h"
#include "content/browser/attribution_reporting/attribution_aggregatable_values.h"
#include "content/browser/attribution_reporting/attribution_aggregation_keys.h"
#include "content/browser/attribution_reporting/attribution_filter_data.h"
#include "content/browser/attribution_reporting/attribution_report.h"
#include "content/browser/attribution_reporting/attribution_source_type.h"
Expand All @@ -39,7 +39,7 @@ using FilterValues = base::flat_map<std::string, std::vector<std::string>>;
TEST(AggregatableAttributionUtilsTest, CreateAggregatableHistogram) {
base::HistogramTester histograms;

auto source = AttributionAggregationKeys::FromKeys(
auto source = attribution_reporting::AggregationKeys::FromKeys(
{{"key1", 345}, {"key2", 5}, {"key3", 123}});
ASSERT_TRUE(source.has_value());

Expand Down Expand Up @@ -129,7 +129,8 @@ TEST(AggregatableAttributionUtilsTest,
NoTriggerData_FilteredPercentageNotRecorded) {
base::HistogramTester histograms;

auto source = AttributionAggregationKeys::FromKeys({{"key1", 345}});
auto source =
attribution_reporting::AggregationKeys::FromKeys({{"key1", 345}});
ASSERT_TRUE(source.has_value());

std::vector<AggregatableHistogramContribution> contributions =
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
#include "base/metrics/histogram_functions.h"
#include "base/time/time.h"
#include "base/types/expected.h"
#include "components/attribution_reporting/aggregation_keys.h"
#include "components/attribution_reporting/constants.h"
#include "components/attribution_reporting/source_registration_error.mojom.h"
#include "content/browser/attribution_reporting/attribution_aggregatable_trigger_data.h"
#include "content/browser/attribution_reporting/attribution_aggregatable_values.h"
#include "content/browser/attribution_reporting/attribution_aggregation_keys.h"
#include "content/browser/attribution_reporting/attribution_filter_data.h"
#include "content/browser/attribution_reporting/attribution_header_utils.h"
#include "content/browser/attribution_reporting/attribution_manager.h"
Expand Down Expand Up @@ -402,8 +402,9 @@ void AttributionDataHostManagerImpl::SourceDataAvailable(
return;
}

absl::optional<AttributionAggregationKeys> aggregation_keys =
AttributionAggregationKeys::FromKeys(std::move(data->aggregation_keys));
absl::optional<attribution_reporting::AggregationKeys> aggregation_keys =
attribution_reporting::AggregationKeys::FromKeys(
std::move(data->aggregation_keys));
if (!aggregation_keys.has_value()) {
RecordSourceDataHandleStatus(DataHandleStatus::kInvalidData);
mojo::ReportBadMessage("AttributionDataHost: Invalid aggregatable source.");
Expand Down

0 comments on commit a652a71

Please sign in to comment.