forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 23
/
reporting_service.cc
342 lines (284 loc) · 11.5 KB
/
reporting_service.cc
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/reporting/reporting_service.h"
#include <utility>
#include "base/bind.h"
#include "base/feature_list.h"
#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/memory/weak_ptr.h"
#include "base/time/tick_clock.h"
#include "base/time/time.h"
#include "base/values.h"
#include "net/base/features.h"
#include "net/base/isolation_info.h"
#include "net/http/structured_headers.h"
#include "net/reporting/reporting_browsing_data_remover.h"
#include "net/reporting/reporting_cache.h"
#include "net/reporting/reporting_context.h"
#include "net/reporting/reporting_delegate.h"
#include "net/reporting/reporting_delivery_agent.h"
#include "net/reporting/reporting_header_parser.h"
#include "net/reporting/reporting_uploader.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace net {
namespace {
constexpr int kMaxJsonSize = 16 * 1024;
constexpr int kMaxJsonDepth = 5;
// If constructed with a PersistentReportingStore, the first call to any of
// QueueReport(), ProcessHeader(), RemoveBrowsingData(), or
// RemoveAllBrowsingData() on a valid input will trigger a load from the store.
// Tasks are queued pending completion of loading from the store.
class ReportingServiceImpl : public ReportingService {
public:
ReportingServiceImpl(std::unique_ptr<ReportingContext> context)
: context_(std::move(context)),
shut_down_(false),
started_loading_from_store_(false),
initialized_(false) {
if (!context_->IsClientDataPersisted())
initialized_ = true;
}
ReportingServiceImpl(const ReportingServiceImpl&) = delete;
ReportingServiceImpl& operator=(const ReportingServiceImpl&) = delete;
// ReportingService implementation:
~ReportingServiceImpl() override {
if (initialized_)
context_->cache()->Flush();
}
void SetDocumentReportingEndpoints(
const base::UnguessableToken& reporting_source,
const url::Origin& origin,
const IsolationInfo& isolation_info,
const base::flat_map<std::string, std::string>& endpoints) override {
DCHECK(!reporting_source.is_empty());
DoOrBacklogTask(base::BindOnce(
&ReportingServiceImpl::DoSetDocumentReportingEndpoints,
base::Unretained(this), reporting_source, isolation_info,
FixupNetworkIsolationKey(isolation_info.network_isolation_key()),
origin, std::move(endpoints)));
}
void SendReportsAndRemoveSource(
const base::UnguessableToken& reporting_source) override {
DCHECK(!reporting_source.is_empty());
context_->delivery_agent()->SendReportsForSource(reporting_source);
context_->cache()->SetExpiredSource(reporting_source);
}
void QueueReport(
const GURL& url,
const absl::optional<base::UnguessableToken>& reporting_source,
const NetworkIsolationKey& network_isolation_key,
const std::string& user_agent,
const std::string& group,
const std::string& type,
std::unique_ptr<const base::Value> body,
int depth) override {
DCHECK(context_);
DCHECK(context_->delegate());
// If |reporting_source| is provided, it must not be empty.
DCHECK(!(reporting_source.has_value() && reporting_source->is_empty()));
if (!context_->delegate()->CanQueueReport(url::Origin::Create(url)))
return;
// Strip username, password, and ref fragment from the URL.
GURL sanitized_url = url.GetAsReferrer();
if (!sanitized_url.is_valid())
return;
base::TimeTicks queued_ticks = context_->tick_clock().NowTicks();
// base::Unretained is safe because the callback is stored in
// |task_backlog_| which will not outlive |this|.
DoOrBacklogTask(base::BindOnce(
&ReportingServiceImpl::DoQueueReport, base::Unretained(this),
reporting_source, FixupNetworkIsolationKey(network_isolation_key),
std::move(sanitized_url), user_agent, group, type, std::move(body),
depth, queued_ticks));
}
void ProcessReportToHeader(const url::Origin& origin,
const NetworkIsolationKey& network_isolation_key,
const std::string& header_string) override {
if (header_string.size() > kMaxJsonSize)
return;
std::unique_ptr<base::Value> header_value =
base::JSONReader::ReadDeprecated("[" + header_string + "]",
base::JSON_PARSE_RFC, kMaxJsonDepth);
if (!header_value)
return;
DVLOG(1) << "Received Reporting policy for " << origin;
DoOrBacklogTask(base::BindOnce(
&ReportingServiceImpl::DoProcessReportToHeader, base::Unretained(this),
FixupNetworkIsolationKey(network_isolation_key), origin,
std::move(header_value)));
}
void RemoveBrowsingData(uint64_t data_type_mask,
const base::RepeatingCallback<bool(const GURL&)>&
origin_filter) override {
DoOrBacklogTask(base::BindOnce(&ReportingServiceImpl::DoRemoveBrowsingData,
base::Unretained(this), data_type_mask,
origin_filter));
}
void RemoveAllBrowsingData(uint64_t data_type_mask) override {
DoOrBacklogTask(
base::BindOnce(&ReportingServiceImpl::DoRemoveAllBrowsingData,
base::Unretained(this), data_type_mask));
}
void OnShutdown() override {
shut_down_ = true;
context_->OnShutdown();
}
const ReportingPolicy& GetPolicy() const override {
return context_->policy();
}
base::Value StatusAsValue() const override {
base::Value dict(base::Value::Type::DICTIONARY);
dict.SetKey("reportingEnabled", base::Value(true));
dict.SetKey("clients", context_->cache()->GetClientsAsValue());
dict.SetKey("reports", context_->cache()->GetReportsAsValue());
return dict;
}
std::vector<const ReportingReport*> GetReports() const override {
std::vector<const net::ReportingReport*> reports;
context_->cache()->GetReports(&reports);
return reports;
}
base::flat_map<url::Origin, std::vector<ReportingEndpoint>>
GetV1ReportingEndpointsByOrigin() const override {
return context_->cache()->GetV1ReportingEndpointsByOrigin();
}
void AddReportingCacheObserver(ReportingCacheObserver* observer) override {
context_->AddCacheObserver(observer);
}
void RemoveReportingCacheObserver(ReportingCacheObserver* observer) override {
context_->RemoveCacheObserver(observer);
}
ReportingContext* GetContextForTesting() const override {
return context_.get();
}
private:
void DoOrBacklogTask(base::OnceClosure task) {
if (shut_down_)
return;
FetchAllClientsFromStoreIfNecessary();
if (!initialized_) {
task_backlog_.push_back(std::move(task));
return;
}
std::move(task).Run();
}
void DoQueueReport(
const absl::optional<base::UnguessableToken>& reporting_source,
const NetworkIsolationKey& network_isolation_key,
GURL sanitized_url,
const std::string& user_agent,
const std::string& group,
const std::string& type,
std::unique_ptr<const base::Value> body,
int depth,
base::TimeTicks queued_ticks) {
DCHECK(initialized_);
context_->cache()->AddReport(
reporting_source, network_isolation_key, sanitized_url, user_agent,
group, type, std::move(body), depth, queued_ticks, 0 /* attempts */);
}
void DoProcessReportToHeader(const NetworkIsolationKey& network_isolation_key,
const url::Origin& origin,
std::unique_ptr<base::Value> header_value) {
DCHECK(initialized_);
ReportingHeaderParser::ParseReportToHeader(
context_.get(), network_isolation_key, origin, std::move(header_value));
}
void DoSetDocumentReportingEndpoints(
const base::UnguessableToken& reporting_source,
const IsolationInfo& isolation_info,
const NetworkIsolationKey& network_isolation_key,
const url::Origin& origin,
base::flat_map<std::string, std::string> header_value) {
DCHECK(initialized_);
ReportingHeaderParser::ProcessParsedReportingEndpointsHeader(
context_.get(), reporting_source, isolation_info, network_isolation_key,
origin, std::move(header_value));
}
void DoRemoveBrowsingData(
uint64_t data_type_mask,
const base::RepeatingCallback<bool(const GURL&)>& origin_filter) {
DCHECK(initialized_);
ReportingBrowsingDataRemover::RemoveBrowsingData(
context_->cache(), data_type_mask, origin_filter);
}
void DoRemoveAllBrowsingData(uint64_t data_type_mask) {
DCHECK(initialized_);
ReportingBrowsingDataRemover::RemoveAllBrowsingData(context_->cache(),
data_type_mask);
}
void ExecuteBacklog() {
DCHECK(initialized_);
DCHECK(context_);
if (shut_down_)
return;
for (base::OnceClosure& task : task_backlog_) {
std::move(task).Run();
}
task_backlog_.clear();
}
void FetchAllClientsFromStoreIfNecessary() {
if (!context_->IsClientDataPersisted() || started_loading_from_store_)
return;
started_loading_from_store_ = true;
FetchAllClientsFromStore();
}
void FetchAllClientsFromStore() {
DCHECK(context_->IsClientDataPersisted());
DCHECK(!initialized_);
context_->store()->LoadReportingClients(base::BindOnce(
&ReportingServiceImpl::OnClientsLoaded, weak_factory_.GetWeakPtr()));
}
void OnClientsLoaded(
std::vector<ReportingEndpoint> loaded_endpoints,
std::vector<CachedReportingEndpointGroup> loaded_endpoint_groups) {
initialized_ = true;
context_->cache()->AddClientsLoadedFromStore(
std::move(loaded_endpoints), std::move(loaded_endpoint_groups));
ExecuteBacklog();
}
// Returns either |network_isolation_key| or an empty NetworkIsolationKey,
// based on |respect_network_isolation_key_|. Should be used on all
// NetworkIsolationKeys passed in through public API calls.
const NetworkIsolationKey& FixupNetworkIsolationKey(
const NetworkIsolationKey& network_isolation_key) {
if (respect_network_isolation_key_)
return network_isolation_key;
return empty_nik_;
}
std::unique_ptr<ReportingContext> context_;
bool shut_down_;
bool started_loading_from_store_;
bool initialized_;
std::vector<base::OnceClosure> task_backlog_;
bool respect_network_isolation_key_ = base::FeatureList::IsEnabled(
features::kPartitionNelAndReportingByNetworkIsolationKey);
// Allows returning a NetworkIsolationKey by reference when
// |respect_network_isolation_key_| is false.
NetworkIsolationKey empty_nik_;
base::WeakPtrFactory<ReportingServiceImpl> weak_factory_{this};
};
} // namespace
ReportingService::~ReportingService() = default;
// static
std::unique_ptr<ReportingService> ReportingService::Create(
const ReportingPolicy& policy,
URLRequestContext* request_context,
ReportingCache::PersistentReportingStore* store) {
return std::make_unique<ReportingServiceImpl>(
ReportingContext::Create(policy, request_context, store));
}
// static
std::unique_ptr<ReportingService> ReportingService::CreateForTesting(
std::unique_ptr<ReportingContext> reporting_context) {
return std::make_unique<ReportingServiceImpl>(std::move(reporting_context));
}
base::Value ReportingService::StatusAsValue() const {
NOTIMPLEMENTED();
return base::Value();
}
} // namespace net