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" |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 11 | #include "base/time/tick_clock.h" |
| 12 | #include "base/time/time.h" |
| 13 | #include "base/values.h" |
juliatuttle | aeb1abc | 2017-05-04 21:14:38 | [diff] [blame] | 14 | #include "net/reporting/reporting_browsing_data_remover.h" |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 15 | #include "net/reporting/reporting_cache.h" |
| 16 | #include "net/reporting/reporting_context.h" |
juliatuttle | 58754891 | 2017-05-23 14:17:21 | [diff] [blame] | 17 | #include "net/reporting/reporting_delegate.h" |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 18 | #include "net/reporting/reporting_header_parser.h" |
Julia Tuttle | 4667c1c | 2017-12-19 18:27:38 | [diff] [blame^] | 19 | #include "net/reporting/reporting_uploader.h" |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 20 | #include "url/gurl.h" |
| 21 | |
| 22 | namespace net { |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | class ReportingServiceImpl : public ReportingService { |
| 27 | public: |
| 28 | ReportingServiceImpl(std::unique_ptr<ReportingContext> context) |
juliatuttle | 42c5731 | 2017-04-28 03:01:30 | [diff] [blame] | 29 | : context_(std::move(context)) {} |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 30 | |
Julia Tuttle | 4667c1c | 2017-12-19 18:27:38 | [diff] [blame^] | 31 | // ReportingService implementation: |
| 32 | |
Chris Watkins | 806691b | 2017-12-01 06:01:22 | [diff] [blame] | 33 | ~ReportingServiceImpl() override = default; |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 34 | |
| 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 { |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 39 | if (!context_->delegate()->CanQueueReport(url::Origin::Create(url))) |
juliatuttle | 58754891 | 2017-05-23 14:17:21 | [diff] [blame] | 40 | return; |
| 41 | |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 42 | context_->cache()->AddReport(url, group, type, std::move(body), |
| 43 | context_->tick_clock()->NowTicks(), 0); |
| 44 | } |
| 45 | |
| 46 | void ProcessHeader(const GURL& url, |
| 47 | const std::string& header_value) override { |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 48 | ReportingHeaderParser::ParseHeader(context_.get(), url, header_value); |
| 49 | } |
| 50 | |
juliatuttle | aeb1abc | 2017-05-04 21:14:38 | [diff] [blame] | 51 | void RemoveBrowsingData( |
| 52 | int data_type_mask, |
Julia Tuttle | ed8d84b | 2017-12-05 18:54:53 | [diff] [blame] | 53 | base::RepeatingCallback<bool(const GURL&)> origin_filter) override { |
juliatuttle | 91f43db | 2017-06-05 17:09:49 | [diff] [blame] | 54 | ReportingBrowsingDataRemover::RemoveBrowsingData( |
| 55 | context_->cache(), data_type_mask, origin_filter); |
juliatuttle | aeb1abc | 2017-05-04 21:14:38 | [diff] [blame] | 56 | } |
| 57 | |
Julia Tuttle | 4667c1c | 2017-12-19 18:27:38 | [diff] [blame^] | 58 | bool RequestIsUpload(const URLRequest& request) override { |
| 59 | return context_->uploader()->RequestIsUpload(request); |
| 60 | } |
| 61 | |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 62 | private: |
| 63 | std::unique_ptr<ReportingContext> context_; |
| 64 | |
| 65 | DISALLOW_COPY_AND_ASSIGN(ReportingServiceImpl); |
| 66 | }; |
| 67 | |
| 68 | } // namespace |
| 69 | |
Chris Watkins | 806691b | 2017-12-01 06:01:22 | [diff] [blame] | 70 | ReportingService::~ReportingService() = default; |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 71 | |
| 72 | // static |
| 73 | std::unique_ptr<ReportingService> ReportingService::Create( |
| 74 | const ReportingPolicy& policy, |
juliatuttle | 42c5731 | 2017-04-28 03:01:30 | [diff] [blame] | 75 | URLRequestContext* request_context) { |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 76 | return std::make_unique<ReportingServiceImpl>( |
juliatuttle | 42c5731 | 2017-04-28 03:01:30 | [diff] [blame] | 77 | ReportingContext::Create(policy, request_context)); |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // static |
| 81 | std::unique_ptr<ReportingService> ReportingService::CreateForTesting( |
| 82 | std::unique_ptr<ReportingContext> reporting_context) { |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 83 | return std::make_unique<ReportingServiceImpl>(std::move(reporting_context)); |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | } // namespace net |