blob: a10ca1002b57fee524f1dffe2f2b0b63d49f91b4 [file] [log] [blame]
juliatuttle381d77e2017-04-07 18:54:121// Copyright 2017 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/reporting/reporting_service.h"
6
juliatuttlea35bbc82017-05-22 19:25:197#include <utility>
juliatuttle381d77e2017-04-07 18:54:128
juliatuttle1d92f0152017-04-28 17:19:219#include "base/bind.h"
Douglas Creager2b81901f2018-10-11 21:29:3610#include "base/json/json_reader.h"
juliatuttle381d77e2017-04-07 18:54:1211#include "base/macros.h"
Julia Tuttleec467a5f2018-02-22 20:22:4512#include "base/memory/weak_ptr.h"
juliatuttle381d77e2017-04-07 18:54:1213#include "base/time/tick_clock.h"
14#include "base/time/time.h"
15#include "base/values.h"
juliatuttleaeb1abc2017-05-04 21:14:3816#include "net/reporting/reporting_browsing_data_remover.h"
juliatuttle381d77e2017-04-07 18:54:1217#include "net/reporting/reporting_cache.h"
18#include "net/reporting/reporting_context.h"
juliatuttle587548912017-05-23 14:17:2119#include "net/reporting/reporting_delegate.h"
juliatuttle381d77e2017-04-07 18:54:1220#include "net/reporting/reporting_header_parser.h"
Julia Tuttle4667c1c2017-12-19 18:27:3821#include "net/reporting/reporting_uploader.h"
juliatuttle381d77e2017-04-07 18:54:1222#include "url/gurl.h"
23
24namespace net {
25
26namespace {
27
Douglas Creager2b81901f2018-10-11 21:29:3628constexpr int kMaxJsonSize = 16 * 1024;
29constexpr int kMaxJsonDepth = 5;
30
Sam Burnettf13c06b2019-07-30 15:53:4931// If constructed with a PersistentReportingStore, the first call to any of
32// QueueReport(), ProcessHeader(), RemoveBrowsingData(), or
33// RemoveAllBrowsingData() on a valid input will trigger a load from the store.
34// Tasks are queued pending completion of loading from the store.
juliatuttle381d77e2017-04-07 18:54:1235class ReportingServiceImpl : public ReportingService {
36 public:
37 ReportingServiceImpl(std::unique_ptr<ReportingContext> context)
Sam Burnettf13c06b2019-07-30 15:53:4938 : context_(std::move(context)),
39 shut_down_(false),
40 started_loading_from_store_(false),
Jeremy Roman2b7c9502019-08-21 22:34:0841 initialized_(false) {
Sam Burnettf13c06b2019-07-30 15:53:4942 if (!context_->IsClientDataPersisted())
43 initialized_ = true;
44 }
juliatuttle381d77e2017-04-07 18:54:1245
Julia Tuttle4667c1c2017-12-19 18:27:3846 // ReportingService implementation:
47
Sam Burnettf13c06b2019-07-30 15:53:4948 ~ReportingServiceImpl() override {
49 if (initialized_)
50 context_->cache()->Flush();
51 }
juliatuttle381d77e2017-04-07 18:54:1252
53 void QueueReport(const GURL& url,
Douglas Creagerf6cb49f72018-07-19 20:14:5354 const std::string& user_agent,
juliatuttle381d77e2017-04-07 18:54:1255 const std::string& group,
56 const std::string& type,
Julia Tuttle107e30672018-03-29 18:48:4257 std::unique_ptr<const base::Value> body,
58 int depth) override {
Julia Tuttle194e6112017-12-19 19:04:4959 DCHECK(context_);
60 DCHECK(context_->delegate());
61
Daniel Cheng88186bd52017-10-20 08:14:4662 if (!context_->delegate()->CanQueueReport(url::Origin::Create(url)))
juliatuttle587548912017-05-23 14:17:2163 return;
64
Lily Chen8fed0dd72019-01-23 17:13:2765 // Strip username, password, and ref fragment from the URL.
66 GURL sanitized_url = url.GetAsReferrer();
67 if (!sanitized_url.is_valid())
68 return;
69
Sam Burnettf13c06b2019-07-30 15:53:4970 base::TimeTicks queued_ticks = context_->tick_clock().NowTicks();
71
72 // base::Unretained is safe because the callback is stored in
73 // |task_backlog_| which will not outlive |this|.
74 DoOrBacklogTask(base::BindOnce(&ReportingServiceImpl::DoQueueReport,
75 base::Unretained(this),
76 std::move(sanitized_url), user_agent, group,
77 type, std::move(body), depth, queued_ticks));
juliatuttle381d77e2017-04-07 18:54:1278 }
79
80 void ProcessHeader(const GURL& url,
Douglas Creager2b81901f2018-10-11 21:29:3681 const std::string& header_string) override {
82 if (header_string.size() > kMaxJsonSize) {
83 ReportingHeaderParser::RecordHeaderDiscardedForJsonTooBig();
84 return;
85 }
86
Lei Zhanga8b4c5fb2019-02-16 03:02:0387 std::unique_ptr<base::Value> header_value =
88 base::JSONReader::ReadDeprecated("[" + header_string + "]",
89 base::JSON_PARSE_RFC, kMaxJsonDepth);
Douglas Creager2b81901f2018-10-11 21:29:3690 if (!header_value) {
91 ReportingHeaderParser::RecordHeaderDiscardedForJsonInvalid();
92 return;
93 }
94
Douglas Creager134b52e2018-11-09 18:00:1495 DVLOG(1) << "Received Reporting policy for " << url.GetOrigin();
Sam Burnettf13c06b2019-07-30 15:53:4996 DoOrBacklogTask(base::BindOnce(&ReportingServiceImpl::DoProcessHeader,
97 base::Unretained(this), url,
98 std::move(header_value)));
juliatuttle381d77e2017-04-07 18:54:1299 }
100
Julia Tuttle227a6ff2017-12-19 19:44:24101 void RemoveBrowsingData(int data_type_mask,
102 const base::RepeatingCallback<bool(const GURL&)>&
103 origin_filter) override {
Sam Burnettf13c06b2019-07-30 15:53:49104 DoOrBacklogTask(base::BindOnce(&ReportingServiceImpl::DoRemoveBrowsingData,
105 base::Unretained(this), data_type_mask,
106 origin_filter));
juliatuttleaeb1abc2017-05-04 21:14:38107 }
108
Eric Orthb812a442018-05-04 20:26:48109 void RemoveAllBrowsingData(int data_type_mask) override {
Sam Burnettf13c06b2019-07-30 15:53:49110 DoOrBacklogTask(
111 base::BindOnce(&ReportingServiceImpl::DoRemoveAllBrowsingData,
112 base::Unretained(this), data_type_mask));
Eric Orthb812a442018-05-04 20:26:48113 }
114
Lily Chen91b51e62019-03-01 16:46:07115 void OnShutdown() override {
116 shut_down_ = true;
117 context_->OnShutdown();
118 }
119
Julia Tuttle91a655d2018-01-26 18:03:03120 const ReportingPolicy& GetPolicy() const override {
121 return context_->policy();
122 }
123
Douglas Creager853b2092018-04-13 23:03:27124 base::Value StatusAsValue() const override {
125 base::Value dict(base::Value::Type::DICTIONARY);
126 dict.SetKey("reportingEnabled", base::Value(true));
127 dict.SetKey("clients", context_->cache()->GetClientsAsValue());
128 dict.SetKey("reports", context_->cache()->GetReportsAsValue());
129 return dict;
130 }
131
Lily Chen91b51e62019-03-01 16:46:07132 ReportingContext* GetContextForTesting() const override {
133 return context_.get();
134 }
135
juliatuttle381d77e2017-04-07 18:54:12136 private:
Sam Burnettf13c06b2019-07-30 15:53:49137 void DoOrBacklogTask(base::OnceClosure task) {
138 if (shut_down_)
139 return;
140
141 FetchAllClientsFromStoreIfNecessary();
142
143 if (!initialized_) {
144 task_backlog_.push_back(std::move(task));
145 return;
146 }
147
148 std::move(task).Run();
149 }
150
151 void DoQueueReport(GURL sanitized_url,
152 const std::string& user_agent,
153 const std::string& group,
154 const std::string& type,
155 std::unique_ptr<const base::Value> body,
156 int depth,
157 base::TimeTicks queued_ticks) {
158 DCHECK(initialized_);
159 context_->cache()->AddReport(sanitized_url, user_agent, group, type,
160 std::move(body), depth, queued_ticks,
161 0 /* attempts */);
162 }
163
164 void DoProcessHeader(const GURL& url,
165 std::unique_ptr<base::Value> header_value) {
166 DCHECK(initialized_);
167 ReportingHeaderParser::ParseHeader(context_.get(), url,
168 std::move(header_value));
169 }
170
171 void DoRemoveBrowsingData(
172 int data_type_mask,
173 const base::RepeatingCallback<bool(const GURL&)>& origin_filter) {
174 DCHECK(initialized_);
175 ReportingBrowsingDataRemover::RemoveBrowsingData(
176 context_->cache(), data_type_mask, origin_filter);
177 }
178
179 void DoRemoveAllBrowsingData(int data_type_mask) {
180 DCHECK(initialized_);
181 ReportingBrowsingDataRemover::RemoveAllBrowsingData(context_->cache(),
182 data_type_mask);
183 }
184
185 void ExecuteBacklog() {
186 DCHECK(initialized_);
187 DCHECK(context_);
188
189 if (shut_down_)
190 return;
191
192 for (base::OnceClosure& task : task_backlog_) {
193 std::move(task).Run();
194 }
195 task_backlog_.clear();
196 }
197
198 void FetchAllClientsFromStoreIfNecessary() {
199 if (!context_->IsClientDataPersisted() || started_loading_from_store_)
200 return;
201
202 started_loading_from_store_ = true;
203 FetchAllClientsFromStore();
204 }
205
206 void FetchAllClientsFromStore() {
207 DCHECK(context_->IsClientDataPersisted());
208 DCHECK(!initialized_);
209
210 context_->store()->LoadReportingClients(base::BindOnce(
211 &ReportingServiceImpl::OnClientsLoaded, weak_factory_.GetWeakPtr()));
212 }
213
214 void OnClientsLoaded(
215 std::vector<ReportingEndpoint> loaded_endpoints,
216 std::vector<CachedReportingEndpointGroup> loaded_endpoint_groups) {
217 initialized_ = true;
218 context_->cache()->AddClientsLoadedFromStore(
219 std::move(loaded_endpoints), std::move(loaded_endpoint_groups));
220 ExecuteBacklog();
221 }
222
juliatuttle381d77e2017-04-07 18:54:12223 std::unique_ptr<ReportingContext> context_;
Lily Chen91b51e62019-03-01 16:46:07224 bool shut_down_;
Sam Burnettf13c06b2019-07-30 15:53:49225 bool started_loading_from_store_;
226 bool initialized_;
227 std::vector<base::OnceClosure> task_backlog_;
Jeremy Roman2b7c9502019-08-21 22:34:08228 base::WeakPtrFactory<ReportingServiceImpl> weak_factory_{this};
juliatuttle381d77e2017-04-07 18:54:12229
230 DISALLOW_COPY_AND_ASSIGN(ReportingServiceImpl);
231};
232
233} // namespace
234
Chris Watkins806691b2017-12-01 06:01:22235ReportingService::~ReportingService() = default;
juliatuttle381d77e2017-04-07 18:54:12236
237// static
238std::unique_ptr<ReportingService> ReportingService::Create(
239 const ReportingPolicy& policy,
Lily Chen731a95c2019-05-06 22:27:22240 URLRequestContext* request_context,
241 ReportingCache::PersistentReportingStore* store) {
Douglas Creager2b81901f2018-10-11 21:29:36242 return std::make_unique<ReportingServiceImpl>(
Lily Chen731a95c2019-05-06 22:27:22243 ReportingContext::Create(policy, request_context, store));
juliatuttle381d77e2017-04-07 18:54:12244}
245
246// static
247std::unique_ptr<ReportingService> ReportingService::CreateForTesting(
248 std::unique_ptr<ReportingContext> reporting_context) {
Jeremy Roman0579ed62017-08-29 15:56:19249 return std::make_unique<ReportingServiceImpl>(std::move(reporting_context));
juliatuttle381d77e2017-04-07 18:54:12250}
251
Douglas Creager853b2092018-04-13 23:03:27252base::Value ReportingService::StatusAsValue() const {
253 NOTIMPLEMENTED();
254 return base::Value();
255}
256
juliatuttle381d77e2017-04-07 18:54:12257} // namespace net