juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 1 | // 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 | |
juliatuttle | a35bbc8 | 2017-05-22 19:25:19 | [diff] [blame] | 7 | #include <utility> |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 8 | |
juliatuttle | 1d92f015 | 2017-04-28 17:19:21 | [diff] [blame] | 9 | #include "base/bind.h" |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 10 | #include "base/macros.h" |
Julia Tuttle | ec467a5f | 2018-02-22 20:22:45 | [diff] [blame] | 11 | #include "base/memory/weak_ptr.h" |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 12 | #include "base/time/tick_clock.h" |
| 13 | #include "base/time/time.h" |
| 14 | #include "base/values.h" |
juliatuttle | aeb1abc | 2017-05-04 21:14:38 | [diff] [blame] | 15 | #include "net/reporting/reporting_browsing_data_remover.h" |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 16 | #include "net/reporting/reporting_cache.h" |
| 17 | #include "net/reporting/reporting_context.h" |
juliatuttle | 58754891 | 2017-05-23 14:17:21 | [diff] [blame] | 18 | #include "net/reporting/reporting_delegate.h" |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 19 | #include "net/reporting/reporting_header_parser.h" |
Julia Tuttle | 4667c1c | 2017-12-19 18:27:38 | [diff] [blame] | 20 | #include "net/reporting/reporting_uploader.h" |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 21 | #include "url/gurl.h" |
| 22 | |
| 23 | namespace net { |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | class ReportingServiceImpl : public ReportingService { |
| 28 | public: |
| 29 | ReportingServiceImpl(std::unique_ptr<ReportingContext> context) |
Julia Tuttle | ec467a5f | 2018-02-22 20:22:45 | [diff] [blame] | 30 | : context_(std::move(context)), weak_factory_(this) {} |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 31 | |
Julia Tuttle | 4667c1c | 2017-12-19 18:27:38 | [diff] [blame] | 32 | // ReportingService implementation: |
| 33 | |
Chris Watkins | 806691b | 2017-12-01 06:01:22 | [diff] [blame] | 34 | ~ReportingServiceImpl() override = default; |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 35 | |
| 36 | void QueueReport(const GURL& url, |
| 37 | const std::string& group, |
| 38 | const std::string& type, |
Julia Tuttle | 107e3067 | 2018-03-29 18:48:42 | [diff] [blame] | 39 | std::unique_ptr<const base::Value> body, |
| 40 | int depth) override { |
Julia Tuttle | 194e611 | 2017-12-19 19:04:49 | [diff] [blame] | 41 | DCHECK(context_); |
| 42 | DCHECK(context_->delegate()); |
| 43 | |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 44 | if (!context_->delegate()->CanQueueReport(url::Origin::Create(url))) |
juliatuttle | 58754891 | 2017-05-23 14:17:21 | [diff] [blame] | 45 | return; |
| 46 | |
Julia Tuttle | 107e3067 | 2018-03-29 18:48:42 | [diff] [blame] | 47 | context_->cache()->AddReport(url, group, type, std::move(body), depth, |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 48 | context_->tick_clock()->NowTicks(), 0); |
| 49 | } |
| 50 | |
| 51 | void ProcessHeader(const GURL& url, |
| 52 | const std::string& header_value) override { |
Julia Tuttle | ec467a5f | 2018-02-22 20:22:45 | [diff] [blame] | 53 | context_->delegate()->ParseJson( |
| 54 | "[" + header_value + "]", |
| 55 | base::BindRepeating(&ReportingServiceImpl::ProcessHeaderValue, |
| 56 | weak_factory_.GetWeakPtr(), url), |
| 57 | base::BindRepeating( |
Julia Tuttle | ef19cb5 | 2018-03-16 16:58:35 | [diff] [blame] | 58 | &ReportingHeaderParser::RecordHeaderDiscardedForJsonInvalid)); |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 59 | } |
| 60 | |
Julia Tuttle | 227a6ff | 2017-12-19 19:44:24 | [diff] [blame] | 61 | void RemoveBrowsingData(int data_type_mask, |
| 62 | const base::RepeatingCallback<bool(const GURL&)>& |
| 63 | origin_filter) override { |
juliatuttle | 91f43db | 2017-06-05 17:09:49 | [diff] [blame] | 64 | ReportingBrowsingDataRemover::RemoveBrowsingData( |
| 65 | context_->cache(), data_type_mask, origin_filter); |
juliatuttle | aeb1abc | 2017-05-04 21:14:38 | [diff] [blame] | 66 | } |
| 67 | |
Julia Tuttle | 107e3067 | 2018-03-29 18:48:42 | [diff] [blame] | 68 | int GetUploadDepth(const URLRequest& request) override { |
| 69 | return context_->uploader()->GetUploadDepth(request); |
Julia Tuttle | 4667c1c | 2017-12-19 18:27:38 | [diff] [blame] | 70 | } |
| 71 | |
Julia Tuttle | 91a655d | 2018-01-26 18:03:03 | [diff] [blame] | 72 | const ReportingPolicy& GetPolicy() const override { |
| 73 | return context_->policy(); |
| 74 | } |
| 75 | |
Douglas Creager | 853b209 | 2018-04-13 23:03:27 | [diff] [blame^] | 76 | base::Value StatusAsValue() const override { |
| 77 | base::Value dict(base::Value::Type::DICTIONARY); |
| 78 | dict.SetKey("reportingEnabled", base::Value(true)); |
| 79 | dict.SetKey("clients", context_->cache()->GetClientsAsValue()); |
| 80 | dict.SetKey("reports", context_->cache()->GetReportsAsValue()); |
| 81 | return dict; |
| 82 | } |
| 83 | |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 84 | private: |
Julia Tuttle | ec467a5f | 2018-02-22 20:22:45 | [diff] [blame] | 85 | void ProcessHeaderValue(const GURL& url, std::unique_ptr<base::Value> value) { |
| 86 | ReportingHeaderParser::ParseHeader(context_.get(), url, std::move(value)); |
| 87 | } |
| 88 | |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 89 | std::unique_ptr<ReportingContext> context_; |
Julia Tuttle | ec467a5f | 2018-02-22 20:22:45 | [diff] [blame] | 90 | base::WeakPtrFactory<ReportingServiceImpl> weak_factory_; |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 91 | |
| 92 | DISALLOW_COPY_AND_ASSIGN(ReportingServiceImpl); |
| 93 | }; |
| 94 | |
| 95 | } // namespace |
| 96 | |
Chris Watkins | 806691b | 2017-12-01 06:01:22 | [diff] [blame] | 97 | ReportingService::~ReportingService() = default; |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 98 | |
| 99 | // static |
| 100 | std::unique_ptr<ReportingService> ReportingService::Create( |
| 101 | const ReportingPolicy& policy, |
juliatuttle | 42c5731 | 2017-04-28 03:01:30 | [diff] [blame] | 102 | URLRequestContext* request_context) { |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 103 | return std::make_unique<ReportingServiceImpl>( |
juliatuttle | 42c5731 | 2017-04-28 03:01:30 | [diff] [blame] | 104 | ReportingContext::Create(policy, request_context)); |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // static |
| 108 | std::unique_ptr<ReportingService> ReportingService::CreateForTesting( |
| 109 | std::unique_ptr<ReportingContext> reporting_context) { |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 110 | return std::make_unique<ReportingServiceImpl>(std::move(reporting_context)); |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 111 | } |
| 112 | |
Douglas Creager | 853b209 | 2018-04-13 23:03:27 | [diff] [blame^] | 113 | base::Value ReportingService::StatusAsValue() const { |
| 114 | NOTIMPLEMENTED(); |
| 115 | return base::Value(); |
| 116 | } |
| 117 | |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 118 | } // namespace net |