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" |
juliatuttle | 1d92f015 | 2017-04-28 17:19:21 | [diff] [blame] | 19 | #include "net/reporting/reporting_persister.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 | |
| 31 | ~ReportingServiceImpl() override {} |
| 32 | |
| 33 | void QueueReport(const GURL& url, |
| 34 | const std::string& group, |
| 35 | const std::string& type, |
| 36 | std::unique_ptr<const base::Value> body) override { |
juliatuttle | 58754891 | 2017-05-23 14:17:21 | [diff] [blame] | 37 | if (!context_->delegate()->CanQueueReport(url::Origin(url))) |
| 38 | return; |
| 39 | |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 40 | context_->cache()->AddReport(url, group, type, std::move(body), |
| 41 | context_->tick_clock()->NowTicks(), 0); |
| 42 | } |
| 43 | |
| 44 | void ProcessHeader(const GURL& url, |
| 45 | const std::string& header_value) override { |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 46 | ReportingHeaderParser::ParseHeader(context_.get(), url, header_value); |
| 47 | } |
| 48 | |
juliatuttle | aeb1abc | 2017-05-04 21:14:38 | [diff] [blame] | 49 | void RemoveBrowsingData( |
| 50 | int data_type_mask, |
| 51 | base::Callback<bool(const GURL&)> origin_filter) override { |
juliatuttle | 91f43db | 2017-06-05 17:09:49 | [diff] [blame] | 52 | ReportingBrowsingDataRemover::RemoveBrowsingData( |
| 53 | context_->cache(), data_type_mask, origin_filter); |
juliatuttle | aeb1abc | 2017-05-04 21:14:38 | [diff] [blame] | 54 | } |
| 55 | |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 56 | private: |
| 57 | std::unique_ptr<ReportingContext> context_; |
| 58 | |
| 59 | DISALLOW_COPY_AND_ASSIGN(ReportingServiceImpl); |
| 60 | }; |
| 61 | |
| 62 | } // namespace |
| 63 | |
| 64 | ReportingService::~ReportingService() {} |
| 65 | |
| 66 | // static |
| 67 | std::unique_ptr<ReportingService> ReportingService::Create( |
| 68 | const ReportingPolicy& policy, |
juliatuttle | 42c5731 | 2017-04-28 03:01:30 | [diff] [blame] | 69 | URLRequestContext* request_context) { |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 70 | return std::make_unique<ReportingServiceImpl>( |
juliatuttle | 42c5731 | 2017-04-28 03:01:30 | [diff] [blame] | 71 | ReportingContext::Create(policy, request_context)); |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // static |
| 75 | std::unique_ptr<ReportingService> ReportingService::CreateForTesting( |
| 76 | std::unique_ptr<ReportingContext> reporting_context) { |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 77 | return std::make_unique<ReportingServiceImpl>(std::move(reporting_context)); |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | } // namespace net |