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