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
content / browser / attribution_reporting / rate_limit_table.h [blame]
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_BROWSER_ATTRIBUTION_REPORTING_RATE_LIMIT_TABLE_H_
#define CONTENT_BROWSER_ATTRIBUTION_REPORTING_RATE_LIMIT_TABLE_H_
#include <stdint.h>
#include <optional>
#include <set>
#include <vector>
#include "base/containers/flat_set.h"
#include "base/containers/span.h"
#include "base/memory/raw_ref.h"
#include "base/sequence_checker.h"
#include "base/thread_annotations.h"
#include "base/time/time.h"
#include "base/types/expected.h"
#include "content/browser/attribution_reporting/attribution_report.h"
#include "content/browser/attribution_reporting/stored_source.h"
#include "content/common/content_export.h"
#include "content/public/browser/attribution_data_model.h"
#include "content/public/browser/storage_partition.h"
namespace attribution_reporting {
class SuitableOrigin;
} // namespace attribution_reporting
namespace net {
class SchemefulSite;
} // namespace net
namespace sql {
class Database;
} // namespace sql
namespace content {
struct AttributionInfo;
class AttributionResolverDelegate;
class AttributionTrigger;
class CommonSourceInfo;
class StorableSource;
enum class RateLimitResult : int;
// Manages storage for rate-limiting sources and attributions.
// This class may be constructed on any sequence but must be accessed and
// destroyed on the same sequence. The sequence must outlive |this|.
class CONTENT_EXPORT RateLimitTable {
public:
// We have separate reporting origin rate limits for sources and attributions,
// and separate attribution rate limits for event-level and aggregatable
// attributions. This enum helps us differentiate between these cases in the
// database.
//
// These values are persisted to the DB. Entries should not be renumbered and
// numeric values should never be reused.
enum class Scope {
kSource = 0,
kEventLevelAttribution = 1,
kAggregatableAttribution = 2,
};
enum class DestinationRateLimitResult {
kAllowed = 0,
kHitGlobalLimit = 1,
kHitReportingLimit = 2,
kHitBothLimits = 3,
kError = 4,
kMaxValue = kError,
};
struct Error {};
explicit RateLimitTable(const AttributionResolverDelegate*);
RateLimitTable(const RateLimitTable&) = delete;
RateLimitTable& operator=(const RateLimitTable&) = delete;
RateLimitTable(RateLimitTable&&) = delete;
RateLimitTable& operator=(RateLimitTable&&) = delete;
~RateLimitTable();
// Creates the table in |db| if it doesn't exist.
// Returns false on failure.
[[nodiscard]] bool CreateTable(sql::Database* db);
// Returns false on failure.
[[nodiscard]] bool AddRateLimitForSource(sql::Database* db,
const StoredSource& source,
int64_t destination_limit_priority);
// Returns false on failure.
[[nodiscard]] bool AddRateLimitForAttribution(
sql::Database* db,
const AttributionInfo& attribution_info,
const StoredSource&,
Scope,
AttributionReport::Id);
[[nodiscard]] RateLimitResult SourceAllowedForReportingOriginLimit(
sql::Database* db,
const StorableSource& source,
base::Time source_time);
[[nodiscard]] RateLimitResult SourceAllowedForReportingOriginPerSiteLimit(
sql::Database* db,
const StorableSource& source,
base::Time source_time);
[[nodiscard]] base::expected<std::vector<StoredSource::Id>, Error>
GetSourcesToDeactivateForDestinationLimit(sql::Database* db,
const StorableSource& source,
base::Time source_time);
[[nodiscard]] bool DeactivateSourcesForDestinationLimit(
sql::Database* db,
base::span<const StoredSource::Id>);
[[nodiscard]] DestinationRateLimitResult SourceAllowedForDestinationRateLimit(
sql::Database* db,
const StorableSource& source,
base::Time source_time);
[[nodiscard]] RateLimitResult SourceAllowedForDestinationPerDayRateLimit(
sql::Database* db,
const StorableSource& source,
base::Time source_time);
[[nodiscard]] RateLimitResult AttributionAllowedForReportingOriginLimit(
sql::Database* db,
const AttributionInfo& attribution_info,
const StoredSource&);
[[nodiscard]] RateLimitResult AttributionAllowedForAttributionLimit(
sql::Database* db,
const AttributionInfo& attribution_info,
const StoredSource&,
Scope scope);
// Returns a negative value on failure.
int64_t CountUniqueReportingOriginsPerSiteForAttribution(
sql::Database* db,
const AttributionTrigger&,
base::Time trigger_time);
[[nodiscard]] bool DeleteAttributionRateLimit(sql::Database* db,
Scope scope,
AttributionReport::Id);
// These should be 1:1 with |AttributionStorageSql|'s |ClearData| functions.
// Returns false on failure.
[[nodiscard]] bool ClearAllDataAllTime(sql::Database* db);
// Returns false on failure.
[[nodiscard]] bool ClearDataForOriginsInRange(
sql::Database* db,
base::Time delete_begin,
base::Time delete_end,
StoragePartition::StorageKeyMatcherFunction filter);
// Returns false on failure.
[[nodiscard]] bool ClearDataForSourceIds(sql::Database* db,
base::span<const StoredSource::Id>);
void AppendRateLimitDataKeys(sql::Database* db,
std::set<AttributionDataModel::DataKey>& keys);
void SetDelegate(const AttributionResolverDelegate&);
static constexpr int64_t kUnsetRecordId = -1;
private:
[[nodiscard]] bool AddRateLimit(
sql::Database* db,
const StoredSource& source,
std::optional<base::Time> trigger_time,
const attribution_reporting::SuitableOrigin& context_origin,
Scope,
std::optional<AttributionReport::Id>,
std::optional<int64_t> destination_limit_priority)
VALID_CONTEXT_REQUIRED(sequence_checker_);
[[nodiscard]] RateLimitResult AllowedForReportingOriginLimit(
sql::Database* db,
bool is_source,
const CommonSourceInfo& common_info,
base::Time time,
const base::flat_set<net::SchemefulSite>& destination_sites)
VALID_CONTEXT_REQUIRED(sequence_checker_);
// Returns false on failure.
[[nodiscard]] bool ClearAllDataInRange(sql::Database* db,
base::Time delete_begin,
base::Time delete_end)
VALID_CONTEXT_REQUIRED(sequence_checker_);
// Deletes data in the table older than the window determined by
// |delegate_->GetRateLimits()|.
// Returns false on failure.
[[nodiscard]] bool DeleteExpiredRateLimits(sql::Database* db)
VALID_CONTEXT_REQUIRED(sequence_checker_);
raw_ref<const AttributionResolverDelegate> delegate_
GUARDED_BY_CONTEXT(sequence_checker_);
// Time at which `DeleteExpiredRateLimits()` was last called. Initialized to
// the NULL time.
base::Time last_cleared_ GUARDED_BY_CONTEXT(sequence_checker_);
SEQUENCE_CHECKER(sequence_checker_);
};
} // namespace content
#endif // CONTENT_BROWSER_ATTRIBUTION_REPORTING_RATE_LIMIT_TABLE_H_