forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreporting_delivery_agent.cc
290 lines (237 loc) · 10 KB
/
reporting_delivery_agent.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
// 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_delivery_agent.h"
#include <map>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
#include "base/bind.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/time/tick_clock.h"
#include "base/timer/timer.h"
#include "base/values.h"
#include "net/reporting/reporting_cache.h"
#include "net/reporting/reporting_delegate.h"
#include "net/reporting/reporting_endpoint_manager.h"
#include "net/reporting/reporting_observer.h"
#include "net/reporting/reporting_report.h"
#include "net/reporting/reporting_uploader.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace net {
namespace {
void SerializeReports(const std::vector<const ReportingReport*>& reports,
base::TimeTicks now,
std::string* json_out) {
base::ListValue reports_value;
for (const ReportingReport* report : reports) {
std::unique_ptr<base::DictionaryValue> report_value =
std::make_unique<base::DictionaryValue>();
report_value->SetInteger("age", (now - report->queued).InMilliseconds());
report_value->SetString("type", report->type);
report_value->SetString("url", report->url.spec());
report_value->SetString("user_agent", report->user_agent);
report_value->SetKey("body", report->body->Clone());
reports_value.Append(std::move(report_value));
}
bool json_written = base::JSONWriter::Write(reports_value, json_out);
DCHECK(json_written);
}
class ReportingDeliveryAgentImpl : public ReportingDeliveryAgent,
public ReportingObserver {
public:
ReportingDeliveryAgentImpl(ReportingContext* context)
: context_(context),
timer_(std::make_unique<base::OneShotTimer>()),
weak_factory_(this) {
context_->AddObserver(this);
}
// ReportingDeliveryAgent implementation:
~ReportingDeliveryAgentImpl() override { context_->RemoveObserver(this); }
void SetTimerForTesting(std::unique_ptr<base::OneShotTimer> timer) override {
DCHECK(!timer_->IsRunning());
timer_ = std::move(timer);
}
// ReportingObserver implementation:
void OnCacheUpdated() override {
if (CacheHasReports() && !timer_->IsRunning()) {
SendReports();
StartTimer();
}
}
private:
using OriginGroup = std::pair<url::Origin, std::string>;
using OriginEndpoint = std::pair<url::Origin, GURL>;
class Delivery {
public:
Delivery(const OriginEndpoint& report_origin_endpoint)
: report_origin(report_origin_endpoint.first),
endpoint(report_origin_endpoint.second) {}
~Delivery() = default;
void AddReports(const ReportingClient* client,
const std::vector<const ReportingReport*>& to_add) {
reports_per_client[client->origin][client->endpoint] += to_add.size();
reports.insert(reports.end(), to_add.begin(), to_add.end());
}
const url::Origin report_origin;
const GURL endpoint;
std::vector<const ReportingReport*> reports;
std::map<url::Origin, std::map<GURL, int>> reports_per_client;
};
bool CacheHasReports() {
std::vector<const ReportingReport*> reports;
context_->cache()->GetReports(&reports);
return !reports.empty();
}
void StartTimer() {
timer_->Start(FROM_HERE, policy().delivery_interval,
base::BindRepeating(&ReportingDeliveryAgentImpl::OnTimerFired,
base::Unretained(this)));
}
void OnTimerFired() {
if (CacheHasReports()) {
SendReports();
StartTimer();
}
}
void SendReports() {
std::vector<const ReportingReport*> reports;
cache()->GetNonpendingReports(&reports);
// Mark all of these reports as pending, so that they're not deleted out
// from under us while we're checking permissions (possibly on another
// thread).
cache()->SetReportsPending(reports);
// First determine which origins we're allowed to upload reports about.
std::set<url::Origin> report_origins;
for (const ReportingReport* report : reports) {
report_origins.insert(url::Origin::Create(report->url));
}
delegate()->CanSendReports(
std::move(report_origins),
base::BindOnce(&ReportingDeliveryAgentImpl::OnSendPermissionsChecked,
weak_factory_.GetWeakPtr(), std::move(reports)));
}
void OnSendPermissionsChecked(std::vector<const ReportingReport*> reports,
std::set<url::Origin> allowed_report_origins) {
// Sort reports into (origin, group) buckets.
std::map<OriginGroup, std::vector<const ReportingReport*>>
origin_group_reports;
for (const ReportingReport* report : reports) {
url::Origin report_origin = url::Origin::Create(report->url);
if (allowed_report_origins.find(report_origin) ==
allowed_report_origins.end())
continue;
OriginGroup origin_group(report_origin, report->group);
origin_group_reports[origin_group].push_back(report);
}
// Find an endpoint for each (origin, group) bucket and sort reports into
// endpoint buckets. Don't allow concurrent deliveries to the same (origin,
// group) bucket.
std::map<OriginEndpoint, std::unique_ptr<Delivery>> deliveries;
for (auto& it : origin_group_reports) {
const OriginGroup& origin_group = it.first;
const url::Origin& report_origin = origin_group.first;
const std::string& group = origin_group.second;
if (base::ContainsKey(pending_origin_groups_, origin_group))
continue;
const ReportingClient* client =
endpoint_manager()->FindClientForOriginAndGroup(report_origin, group);
if (client == nullptr) {
continue;
}
cache()->MarkClientUsed(client);
OriginEndpoint report_origin_endpoint(report_origin, client->endpoint);
Delivery* delivery;
auto delivery_it = deliveries.find(report_origin_endpoint);
if (delivery_it == deliveries.end()) {
auto new_delivery = std::make_unique<Delivery>(report_origin_endpoint);
delivery = new_delivery.get();
deliveries[report_origin_endpoint] = std::move(new_delivery);
} else {
delivery = delivery_it->second.get();
}
delivery->AddReports(client, it.second);
pending_origin_groups_.insert(origin_group);
}
// Keep track of which of these reports we don't queue for delivery; we'll
// need to mark them as not-pending.
std::unordered_set<const ReportingReport*> undelivered_reports(
reports.begin(), reports.end());
// Start an upload for each delivery.
for (auto& it : deliveries) {
const OriginEndpoint& report_origin_endpoint = it.first;
const url::Origin& report_origin = report_origin_endpoint.first;
const GURL& endpoint = report_origin_endpoint.second;
std::unique_ptr<Delivery>& delivery = it.second;
std::string json;
SerializeReports(delivery->reports, tick_clock()->NowTicks(), &json);
int max_depth = 0;
for (const ReportingReport* report : delivery->reports) {
undelivered_reports.erase(report);
if (report->depth > max_depth)
max_depth = report->depth;
}
// TODO: Calculate actual max depth.
uploader()->StartUpload(
report_origin, endpoint, json, max_depth,
base::BindOnce(&ReportingDeliveryAgentImpl::OnUploadComplete,
weak_factory_.GetWeakPtr(), std::move(delivery)));
}
cache()->ClearReportsPending(
{undelivered_reports.begin(), undelivered_reports.end()});
}
void OnUploadComplete(std::unique_ptr<Delivery> delivery,
ReportingUploader::Outcome outcome) {
for (const auto& origin_and_pair : delivery->reports_per_client) {
const url::Origin& client_origin = origin_and_pair.first;
for (const auto& endpoint_and_count : origin_and_pair.second) {
const GURL& endpoint = endpoint_and_count.first;
int report_count = endpoint_and_count.second;
cache()->IncrementEndpointDeliveries(
client_origin, endpoint, report_count,
outcome == ReportingUploader::Outcome::SUCCESS);
}
}
if (outcome == ReportingUploader::Outcome::SUCCESS) {
cache()->RemoveReports(delivery->reports,
ReportingReport::Outcome::DELIVERED);
endpoint_manager()->InformOfEndpointRequest(delivery->endpoint, true);
} else {
cache()->IncrementReportsAttempts(delivery->reports);
endpoint_manager()->InformOfEndpointRequest(delivery->endpoint, false);
}
if (outcome == ReportingUploader::Outcome::REMOVE_ENDPOINT)
cache()->RemoveClientsForEndpoint(delivery->endpoint);
for (const ReportingReport* report : delivery->reports) {
pending_origin_groups_.erase(
OriginGroup(delivery->report_origin, report->group));
}
cache()->ClearReportsPending(delivery->reports);
}
const ReportingPolicy& policy() { return context_->policy(); }
const base::TickClock* tick_clock() { return context_->tick_clock(); }
ReportingDelegate* delegate() { return context_->delegate(); }
ReportingCache* cache() { return context_->cache(); }
ReportingUploader* uploader() { return context_->uploader(); }
ReportingEndpointManager* endpoint_manager() {
return context_->endpoint_manager();
}
ReportingContext* context_;
std::unique_ptr<base::OneShotTimer> timer_;
// Tracks OriginGroup tuples for which there is a pending delivery running.
// (Would be an unordered_set, but there's no hash on pair.)
std::set<OriginGroup> pending_origin_groups_;
base::WeakPtrFactory<ReportingDeliveryAgentImpl> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(ReportingDeliveryAgentImpl);
};
} // namespace
// static
std::unique_ptr<ReportingDeliveryAgent> ReportingDeliveryAgent::Create(
ReportingContext* context) {
return std::make_unique<ReportingDeliveryAgentImpl>(context);
}
ReportingDeliveryAgent::~ReportingDeliveryAgent() = default;
} // namespace net