File: attribution_report.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (299 lines) | stat: -rw-r--r-- 10,442 bytes parent folder | download | duplicates (2)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright 2020 The Chromium Authors
// 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_report.h"

#include <algorithm>
#include <cmath>
#include <optional>
#include <string>
#include <utility>
#include <variant>

#include "base/check.h"
#include "base/check_op.h"
#include "base/functional/overloaded.h"
#include "base/numerics/checked_math.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/values.h"
#include "components/attribution_reporting/aggregatable_trigger_config.h"
#include "components/attribution_reporting/destination_set.h"
#include "components/attribution_reporting/source_type.h"
#include "components/attribution_reporting/suitable_origin.h"
#include "content/browser/attribution_reporting/aggregatable_attribution_utils.h"
#include "content/browser/attribution_reporting/common_source_info.h"
#include "content/browser/attribution_reporting/stored_source.h"
#include "third_party/blink/public/mojom/aggregation_service/aggregatable_report.mojom.h"
#include "url/gurl.h"
#include "url/origin.h"
#include "url/url_canon.h"

namespace content {

namespace {

using ::attribution_reporting::SuitableOrigin;

void PopulateReportBody(base::Value::Dict& dict,
                        const AttributionReport::AggregatableData& data) {
  if (const auto& assembled_report = data.assembled_report();
      assembled_report.has_value()) {
    dict = assembled_report->GetAsJson();
  } else {
    // This generally should only be called when displaying the report
    // for debugging/internals.
    dict.Set("shared_info", "not generated prior to send");
    dict.Set("aggregation_service_payloads", "not generated prior to send");
  }

  if (const auto& trigger_context_id =
          data.aggregatable_trigger_config().trigger_context_id();
      trigger_context_id.has_value()) {
    dict.Set("trigger_context_id", *trigger_context_id);
  }
}

}  // namespace

AttributionReport::EventLevelData::EventLevelData(uint32_t trigger_data,
                                                  int64_t priority,
                                                  const StoredSource& source)
    : trigger_data(trigger_data),
      priority(priority),
      source_origin(source.common_info().source_origin()),
      destinations(source.destination_sites()),
      source_event_id(source.source_event_id()),
      source_type(source.common_info().source_type()),
      randomized_response_rate(source.randomized_response_rate()),
      attributed_truthfully(source.attribution_logic() ==
                            StoredSource::AttributionLogic::kTruthfully) {}

AttributionReport::EventLevelData::EventLevelData(const EventLevelData&) =
    default;

AttributionReport::EventLevelData& AttributionReport::EventLevelData::operator=(
    const EventLevelData&) = default;

AttributionReport::EventLevelData::EventLevelData(EventLevelData&&) = default;

AttributionReport::EventLevelData& AttributionReport::EventLevelData::operator=(
    EventLevelData&&) = default;

AttributionReport::EventLevelData::~EventLevelData() = default;

AttributionReport::AggregatableData::AggregatableData(
    std::optional<SuitableOrigin> aggregation_coordinator_origin,
    attribution_reporting::AggregatableTriggerConfig
        aggregatable_trigger_config,
    base::Time source_time,
    std::vector<blink::mojom::AggregatableReportHistogramContribution>
        contributions,
    std::optional<attribution_reporting::SuitableOrigin> source_origin)
    : aggregation_coordinator_origin_(
          std::move(aggregation_coordinator_origin)),
      aggregatable_trigger_config_(std::move(aggregatable_trigger_config)),
      source_time_(source_time),
      contributions_(std::move(contributions)),
      source_origin_(std::move(source_origin)) {
  CHECK(source_origin_.has_value() || contributions_.empty());
}

AttributionReport::AggregatableData::AggregatableData(const AggregatableData&) =
    default;

AttributionReport::AggregatableData&
AttributionReport::AggregatableData::operator=(const AggregatableData&) =
    default;

AttributionReport::AggregatableData::AggregatableData(AggregatableData&&) =
    default;

AttributionReport::AggregatableData&
AttributionReport::AggregatableData::operator=(AggregatableData&&) = default;

void AttributionReport::AggregatableData::SetContributions(
    std::vector<blink::mojom::AggregatableReportHistogramContribution>
        contributions) {
  CHECK(source_origin_.has_value());
  CHECK(!contributions.empty());
  contributions_ = std::move(contributions);
}

void AttributionReport::AggregatableData::SetAssembledReport(
    std::optional<AggregatableReport> assembled_report) {
  DCHECK(!assembled_report_.has_value());
  assembled_report_ = std::move(assembled_report);
}

AttributionReport::AggregatableData::~AggregatableData() = default;

base::CheckedNumeric<int64_t>
AttributionReport::AggregatableData::BudgetRequired() const {
  return GetTotalAggregatableValues(contributions_);
}

AttributionReport::AttributionReport(
    AttributionInfo attribution_info,
    Id id,
    base::Time report_time,
    base::Time initial_report_time,
    base::Uuid external_report_id,
    int failed_send_attempts,
    Data data,
    attribution_reporting::SuitableOrigin reporting_origin,
    std::optional<uint64_t> source_debug_key)
    : attribution_info_(std::move(attribution_info)),
      id_(id),
      report_time_(report_time),
      initial_report_time_(initial_report_time),
      external_report_id_(std::move(external_report_id)),
      failed_send_attempts_(failed_send_attempts),
      data_(std::move(data)),
      reporting_origin_(std::move(reporting_origin)),
      source_debug_key_(source_debug_key) {
  DCHECK(external_report_id_.is_valid());
  DCHECK_GE(failed_send_attempts_, 0);
}

AttributionReport::AttributionReport(const AttributionReport&) = default;

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

AttributionReport::AttributionReport(AttributionReport&&) = default;

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

AttributionReport::~AttributionReport() = default;

GURL AttributionReport::ReportURL(bool debug) const {
  static constexpr char kBasePath[] = "/.well-known/attribution-reporting/";
  static constexpr char kDebugPath[] = "debug/";

  const char* endpoint_path;
  switch (GetReportType()) {
    case Type::kEventLevel:
      endpoint_path = "report-event-attribution";
      break;
    case Type::kAggregatableAttribution:
    case Type::kNullAggregatable:
      endpoint_path = "report-aggregate-attribution";
      break;
  }

  std::string path =
      base::StrCat({kBasePath, debug ? kDebugPath : "", endpoint_path});

  GURL::Replacements replacements;
  replacements.SetPathStr(path);
  return reporting_origin_->GetURL().ReplaceComponents(replacements);
}

base::Value::Dict AttributionReport::ReportBody() const {
  base::Value::Dict dict;

  std::visit(
      base::Overloaded{
          [&](const EventLevelData& data) {
            dict.Set("attribution_destination", data.destinations.ToJson());

            // The API denotes these values as strings; a `uint64_t` cannot be
            // put in a dict as an integer in order to be opaque to various API
            // configurations.
            dict.Set("source_event_id",
                     base::NumberToString(data.source_event_id));

            dict.Set("trigger_data", base::NumberToString(data.trigger_data));

            dict.Set("source_type",
                     attribution_reporting::SourceTypeName(data.source_type));

            dict.Set("report_id", external_report_id_.AsLowercaseString());

            // Round to 7 digits of precision, which allows us to express binary
            // randomized response with epsilon = 14 without rounding to 0
            // (0.00000166305 -> 0.0000017).
            double rounded_rate =
                round(data.randomized_response_rate * 10000000) / 10000000.0;
            dict.Set("randomized_trigger_rate", rounded_rate);

            dict.Set("scheduled_report_time",
                     base::NumberToString(
                         (initial_report_time_ - base::Time::UnixEpoch())
                             .InSeconds()));
          },

          [&](const AggregatableData& data) { PopulateReportBody(dict, data); },
      },
      data_);

  if (CanDebuggingBeEnabled()) {
    CHECK(source_debug_key_.has_value());
    std::optional<uint64_t> trigger_debug_key = attribution_info_.debug_key;
    CHECK(trigger_debug_key.has_value());
    dict.Set("source_debug_key", base::NumberToString(*source_debug_key_));
    dict.Set("trigger_debug_key", base::NumberToString(*trigger_debug_key));
  }

  return dict;
}

AttributionReport::Type AttributionReport::GetReportType() const {
  return std::visit(
      base::Overloaded{
          [](const EventLevelData&) {
            return AttributionReport::Type::kEventLevel;
          },

          [](const AggregatableData& data) {
            return data.is_null()
                       ? AttributionReport::Type::kNullAggregatable
                       : AttributionReport::Type::kAggregatableAttribution;
          },
      },
      data_);
}

void AttributionReport::set_report_time(base::Time report_time) {
  report_time_ = report_time;
}

// static
std::optional<base::Time> AttributionReport::MinReportTime(
    std::optional<base::Time> a,
    std::optional<base::Time> b) {
  if (!a.has_value()) {
    return b;
  }

  if (!b.has_value()) {
    return a;
  }

  return std::min(*a, *b);
}

const SuitableOrigin& AttributionReport::GetSourceOrigin() const {
  return std::visit(
      base::Overloaded{
          [](const AttributionReport::EventLevelData& data)
              -> const SuitableOrigin& { return data.source_origin; },
          [&](const AttributionReport::AggregatableData& data)
              -> const SuitableOrigin& {
            if (data.source_origin().has_value()) {
              return *data.source_origin();
            }
            return attribution_info_.context_origin;
          },
      },
      data_);
}

bool AttributionReport::CanDebuggingBeEnabled() const {
  return attribution_info_.debug_key.has_value() &&
         source_debug_key_.has_value();
}

}  // namespace content