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

content / browser / attribution_reporting / stored_source.cc [blame]

// 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 "content/browser/attribution_reporting/stored_source.h"

#include <stdint.h>

#include <limits>
#include <optional>
#include <utility>

#include "base/check.h"
#include "base/check_op.h"
#include "base/time/time.h"
#include "components/attribution_reporting/aggregatable_utils.h"
#include "components/attribution_reporting/aggregation_keys.h"
#include "components/attribution_reporting/attribution_scopes_data.h"
#include "components/attribution_reporting/constants.h"
#include "components/attribution_reporting/destination_set.h"
#include "components/attribution_reporting/event_level_epsilon.h"
#include "components/attribution_reporting/filters.h"
#include "components/attribution_reporting/trigger_config.h"
#include "components/attribution_reporting/trigger_data_matching.mojom-forward.h"
#include "content/browser/attribution_reporting/common_source_info.h"

namespace content {

namespace {

bool IsExpiryOrReportWindowTimeValid(base::Time expiry_or_report_window_time,
                                     base::Time source_time) {
  // The source must expire strictly after it occurred.
  return expiry_or_report_window_time > source_time &&
         expiry_or_report_window_time - source_time <=
             attribution_reporting::kMaxSourceExpiry;
}

bool AreFieldsValid(int remaining_aggregatable_attribution_budget,
                    int remaining_aggregatable_debug_budget,
                    double randomized_response_rate,
                    base::Time source_time,
                    base::Time expiry_time,
                    base::Time aggregatable_report_window_time,
                    std::optional<uint64_t> debug_key,
                    bool cookie_based_debug_allowed) {
  static_assert(attribution_reporting::kMaxAggregatableValue <=
                std::numeric_limits<int>::max() / 2);

  return attribution_reporting::IsRemainingAggregatableBudgetInRange(
             remaining_aggregatable_attribution_budget) &&
         attribution_reporting::IsRemainingAggregatableBudgetInRange(
             remaining_aggregatable_debug_budget) &&
         attribution_reporting::IsRemainingAggregatableBudgetInRange(
             remaining_aggregatable_attribution_budget +
             remaining_aggregatable_debug_budget) &&
         randomized_response_rate >= 0 && randomized_response_rate <= 1 &&
         IsExpiryOrReportWindowTimeValid(expiry_time, source_time) &&
         IsExpiryOrReportWindowTimeValid(aggregatable_report_window_time,
                                         source_time) &&
         (!debug_key.has_value() || cookie_based_debug_allowed);
}

}  // namespace

// static
std::optional<StoredSource> StoredSource::Create(
    CommonSourceInfo common_info,
    uint64_t source_event_id,
    attribution_reporting::DestinationSet destination_sites,
    base::Time source_time,
    base::Time expiry_time,
    attribution_reporting::TriggerSpecs trigger_specs,
    base::Time aggregatable_report_window_time,
    int64_t priority,
    attribution_reporting::FilterData filter_data,
    std::optional<uint64_t> debug_key,
    attribution_reporting::AggregationKeys aggregation_keys,
    AttributionLogic attribution_logic,
    ActiveState active_state,
    Id source_id,
    int remaining_aggregatable_attribution_budget,
    double randomized_response_rate,
    attribution_reporting::mojom::TriggerDataMatching trigger_data_matching,
    attribution_reporting::EventLevelEpsilon event_level_epsilon,
    absl::uint128 aggregatable_debug_key_piece,
    int remaining_aggregatable_debug_budget,
    std::optional<attribution_reporting::AttributionScopesData>
        attribution_scopes_data) {
  if (!AreFieldsValid(remaining_aggregatable_attribution_budget,
                      remaining_aggregatable_debug_budget,
                      randomized_response_rate, source_time, expiry_time,
                      aggregatable_report_window_time, debug_key,
                      common_info.cookie_based_debug_allowed())) {
    return std::nullopt;
  }

  return StoredSource(
      std::move(common_info), source_event_id, std::move(destination_sites),
      source_time, expiry_time, std::move(trigger_specs),
      aggregatable_report_window_time, priority, std::move(filter_data),
      debug_key, std::move(aggregation_keys), attribution_logic, active_state,
      source_id, remaining_aggregatable_attribution_budget,
      randomized_response_rate, trigger_data_matching, event_level_epsilon,
      aggregatable_debug_key_piece, remaining_aggregatable_debug_budget,
      std::move(attribution_scopes_data));
}

StoredSource::StoredSource(
    CommonSourceInfo common_info,
    uint64_t source_event_id,
    attribution_reporting::DestinationSet destination_sites,
    base::Time source_time,
    base::Time expiry_time,
    attribution_reporting::TriggerSpecs trigger_specs,
    base::Time aggregatable_report_window_time,
    int64_t priority,
    attribution_reporting::FilterData filter_data,
    std::optional<uint64_t> debug_key,
    attribution_reporting::AggregationKeys aggregation_keys,
    AttributionLogic attribution_logic,
    ActiveState active_state,
    Id source_id,
    int remaining_aggregatable_attribution_budget,
    double randomized_response_rate,
    attribution_reporting::mojom::TriggerDataMatching trigger_data_matching,
    attribution_reporting::EventLevelEpsilon event_level_epsilon,
    absl::uint128 aggregatable_debug_key_piece,
    int remaining_aggregatable_debug_budget,
    std::optional<attribution_reporting::AttributionScopesData>
        attribution_scopes_data)
    : common_info_(std::move(common_info)),
      source_event_id_(source_event_id),
      destination_sites_(std::move(destination_sites)),
      source_time_(source_time),
      expiry_time_(expiry_time),
      trigger_specs_(std::move(trigger_specs)),
      aggregatable_report_window_time_(aggregatable_report_window_time),
      priority_(priority),
      filter_data_(std::move(filter_data)),
      debug_key_(debug_key),
      aggregation_keys_(std::move(aggregation_keys)),
      attribution_logic_(attribution_logic),
      active_state_(active_state),
      source_id_(source_id),
      remaining_aggregatable_attribution_budget_(
          remaining_aggregatable_attribution_budget),
      randomized_response_rate_(randomized_response_rate),
      trigger_data_matching_(std::move(trigger_data_matching)),
      event_level_epsilon_(event_level_epsilon),
      aggregatable_debug_key_piece_(aggregatable_debug_key_piece),
      remaining_aggregatable_debug_budget_(remaining_aggregatable_debug_budget),
      attribution_scopes_data_(std::move(attribution_scopes_data)) {
  DCHECK(AreFieldsValid(remaining_aggregatable_attribution_budget_,
                        remaining_aggregatable_debug_budget_,
                        randomized_response_rate_, source_time_, expiry_time_,
                        aggregatable_report_window_time_, debug_key_,
                        common_info_.cookie_based_debug_allowed()));
}

StoredSource::~StoredSource() = default;

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

StoredSource::StoredSource(StoredSource&&) = default;

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

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

}  // namespace content