blob: 59976e23224841d37625e0de36e8a4917f106a8a [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"
juliatuttle381d77e2017-04-07 18:54:1210#include "base/macros.h"
juliatuttle381d77e2017-04-07 18:54:1211#include "base/time/tick_clock.h"
12#include "base/time/time.h"
13#include "base/values.h"
juliatuttleaeb1abc2017-05-04 21:14:3814#include "net/reporting/reporting_browsing_data_remover.h"
juliatuttle381d77e2017-04-07 18:54:1215#include "net/reporting/reporting_cache.h"
16#include "net/reporting/reporting_context.h"
juliatuttle587548912017-05-23 14:17:2117#include "net/reporting/reporting_delegate.h"
juliatuttle381d77e2017-04-07 18:54:1218#include "net/reporting/reporting_header_parser.h"
Julia Tuttle4667c1c2017-12-19 18:27:3819#include "net/reporting/reporting_uploader.h"
juliatuttle381d77e2017-04-07 18:54:1220#include "url/gurl.h"
21
22namespace net {
23
24namespace {
25
26class ReportingServiceImpl : public ReportingService {
27 public:
28 ReportingServiceImpl(std::unique_ptr<ReportingContext> context)
juliatuttle42c57312017-04-28 03:01:3029 : context_(std::move(context)) {}
juliatuttle381d77e2017-04-07 18:54:1230
Julia Tuttle4667c1c2017-12-19 18:27:3831 // ReportingService implementation:
32
Chris Watkins806691b2017-12-01 06:01:2233 ~ReportingServiceImpl() override = default;
juliatuttle381d77e2017-04-07 18:54:1234
35 void QueueReport(const GURL& url,
36 const std::string& group,
37 const std::string& type,
38 std::unique_ptr<const base::Value> body) override {
Julia Tuttle194e6112017-12-19 19:04:4939 DCHECK(context_);
40 DCHECK(context_->delegate());
41
Daniel Cheng88186bd52017-10-20 08:14:4642 if (!context_->delegate()->CanQueueReport(url::Origin::Create(url)))
juliatuttle587548912017-05-23 14:17:2143 return;
44
juliatuttle381d77e2017-04-07 18:54:1245 context_->cache()->AddReport(url, group, type, std::move(body),
46 context_->tick_clock()->NowTicks(), 0);
47 }
48
49 void ProcessHeader(const GURL& url,
50 const std::string& header_value) override {
juliatuttle381d77e2017-04-07 18:54:1251 ReportingHeaderParser::ParseHeader(context_.get(), url, header_value);
52 }
53
Julia Tuttle227a6ff2017-12-19 19:44:2454 void RemoveBrowsingData(int data_type_mask,
55 const base::RepeatingCallback<bool(const GURL&)>&
56 origin_filter) override {
juliatuttle91f43db2017-06-05 17:09:4957 ReportingBrowsingDataRemover::RemoveBrowsingData(
58 context_->cache(), data_type_mask, origin_filter);
juliatuttleaeb1abc2017-05-04 21:14:3859 }
60
Julia Tuttle4667c1c2017-12-19 18:27:3861 bool RequestIsUpload(const URLRequest& request) override {
62 return context_->uploader()->RequestIsUpload(request);
63 }
64
juliatuttle381d77e2017-04-07 18:54:1265 private:
66 std::unique_ptr<ReportingContext> context_;
67
68 DISALLOW_COPY_AND_ASSIGN(ReportingServiceImpl);
69};
70
71} // namespace
72
Chris Watkins806691b2017-12-01 06:01:2273ReportingService::~ReportingService() = default;
juliatuttle381d77e2017-04-07 18:54:1274
75// static
76std::unique_ptr<ReportingService> ReportingService::Create(
77 const ReportingPolicy& policy,
juliatuttle42c57312017-04-28 03:01:3078 URLRequestContext* request_context) {
Jeremy Roman0579ed62017-08-29 15:56:1979 return std::make_unique<ReportingServiceImpl>(
juliatuttle42c57312017-04-28 03:01:3080 ReportingContext::Create(policy, request_context));
juliatuttle381d77e2017-04-07 18:54:1281}
82
83// static
84std::unique_ptr<ReportingService> ReportingService::CreateForTesting(
85 std::unique_ptr<ReportingContext> reporting_context) {
Jeremy Roman0579ed62017-08-29 15:56:1986 return std::make_unique<ReportingServiceImpl>(std::move(reporting_context));
juliatuttle381d77e2017-04-07 18:54:1287}
88
89} // namespace net