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 | |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 31 | // If constructed with a PersistentReportingStore, the first call to any of |
| 32 | // QueueReport(), ProcessHeader(), RemoveBrowsingData(), or |
| 33 | // RemoveAllBrowsingData() on a valid input will trigger a load from the store. |
| 34 | // Tasks are queued pending completion of loading from the store. |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 35 | class ReportingServiceImpl : public ReportingService { |
| 36 | public: |
| 37 | ReportingServiceImpl(std::unique_ptr<ReportingContext> context) |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 38 | : context_(std::move(context)), |
| 39 | shut_down_(false), |
| 40 | started_loading_from_store_(false), |
Jeremy Roman | 2b7c950 | 2019-08-21 22:34:08 | [diff] [blame] | 41 | initialized_(false) { |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 42 | if (!context_->IsClientDataPersisted()) |
| 43 | initialized_ = true; |
| 44 | } |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 45 | |
Julia Tuttle | 4667c1c | 2017-12-19 18:27:38 | [diff] [blame] | 46 | // ReportingService implementation: |
| 47 | |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 48 | ~ReportingServiceImpl() override { |
| 49 | if (initialized_) |
| 50 | context_->cache()->Flush(); |
| 51 | } |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 52 | |
| 53 | void QueueReport(const GURL& url, |
Douglas Creager | f6cb49f7 | 2018-07-19 20:14:53 | [diff] [blame] | 54 | const std::string& user_agent, |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 55 | const std::string& group, |
| 56 | const std::string& type, |
Julia Tuttle | 107e3067 | 2018-03-29 18:48:42 | [diff] [blame] | 57 | std::unique_ptr<const base::Value> body, |
| 58 | int depth) override { |
Julia Tuttle | 194e611 | 2017-12-19 19:04:49 | [diff] [blame] | 59 | DCHECK(context_); |
| 60 | DCHECK(context_->delegate()); |
| 61 | |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 62 | if (!context_->delegate()->CanQueueReport(url::Origin::Create(url))) |
juliatuttle | 58754891 | 2017-05-23 14:17:21 | [diff] [blame] | 63 | return; |
| 64 | |
Lily Chen | 8fed0dd7 | 2019-01-23 17:13:27 | [diff] [blame] | 65 | // Strip username, password, and ref fragment from the URL. |
| 66 | GURL sanitized_url = url.GetAsReferrer(); |
| 67 | if (!sanitized_url.is_valid()) |
| 68 | return; |
| 69 | |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 70 | base::TimeTicks queued_ticks = context_->tick_clock().NowTicks(); |
| 71 | |
| 72 | // base::Unretained is safe because the callback is stored in |
| 73 | // |task_backlog_| which will not outlive |this|. |
| 74 | DoOrBacklogTask(base::BindOnce(&ReportingServiceImpl::DoQueueReport, |
| 75 | base::Unretained(this), |
| 76 | std::move(sanitized_url), user_agent, group, |
| 77 | type, std::move(body), depth, queued_ticks)); |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void ProcessHeader(const GURL& url, |
Douglas Creager | 2b81901f | 2018-10-11 21:29:36 | [diff] [blame] | 81 | const std::string& header_string) override { |
| 82 | if (header_string.size() > kMaxJsonSize) { |
| 83 | ReportingHeaderParser::RecordHeaderDiscardedForJsonTooBig(); |
| 84 | return; |
| 85 | } |
| 86 | |
Lei Zhang | a8b4c5fb | 2019-02-16 03:02:03 | [diff] [blame] | 87 | std::unique_ptr<base::Value> header_value = |
| 88 | base::JSONReader::ReadDeprecated("[" + header_string + "]", |
| 89 | base::JSON_PARSE_RFC, kMaxJsonDepth); |
Douglas Creager | 2b81901f | 2018-10-11 21:29:36 | [diff] [blame] | 90 | if (!header_value) { |
| 91 | ReportingHeaderParser::RecordHeaderDiscardedForJsonInvalid(); |
| 92 | return; |
| 93 | } |
| 94 | |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 95 | DVLOG(1) << "Received Reporting policy for " << url.GetOrigin(); |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 96 | DoOrBacklogTask(base::BindOnce(&ReportingServiceImpl::DoProcessHeader, |
| 97 | base::Unretained(this), url, |
| 98 | std::move(header_value))); |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 99 | } |
| 100 | |
Julia Tuttle | 227a6ff | 2017-12-19 19:44:24 | [diff] [blame] | 101 | void RemoveBrowsingData(int data_type_mask, |
| 102 | const base::RepeatingCallback<bool(const GURL&)>& |
| 103 | origin_filter) override { |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 104 | DoOrBacklogTask(base::BindOnce(&ReportingServiceImpl::DoRemoveBrowsingData, |
| 105 | base::Unretained(this), data_type_mask, |
| 106 | origin_filter)); |
juliatuttle | aeb1abc | 2017-05-04 21:14:38 | [diff] [blame] | 107 | } |
| 108 | |
Eric Orth | b812a44 | 2018-05-04 20:26:48 | [diff] [blame] | 109 | void RemoveAllBrowsingData(int data_type_mask) override { |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 110 | DoOrBacklogTask( |
| 111 | base::BindOnce(&ReportingServiceImpl::DoRemoveAllBrowsingData, |
| 112 | base::Unretained(this), data_type_mask)); |
Eric Orth | b812a44 | 2018-05-04 20:26:48 | [diff] [blame] | 113 | } |
| 114 | |
Lily Chen | 91b51e6 | 2019-03-01 16:46:07 | [diff] [blame] | 115 | void OnShutdown() override { |
| 116 | shut_down_ = true; |
| 117 | context_->OnShutdown(); |
| 118 | } |
| 119 | |
Julia Tuttle | 91a655d | 2018-01-26 18:03:03 | [diff] [blame] | 120 | const ReportingPolicy& GetPolicy() const override { |
| 121 | return context_->policy(); |
| 122 | } |
| 123 | |
Douglas Creager | 853b209 | 2018-04-13 23:03:27 | [diff] [blame] | 124 | base::Value StatusAsValue() const override { |
| 125 | base::Value dict(base::Value::Type::DICTIONARY); |
| 126 | dict.SetKey("reportingEnabled", base::Value(true)); |
| 127 | dict.SetKey("clients", context_->cache()->GetClientsAsValue()); |
| 128 | dict.SetKey("reports", context_->cache()->GetReportsAsValue()); |
| 129 | return dict; |
| 130 | } |
| 131 | |
Lily Chen | 91b51e6 | 2019-03-01 16:46:07 | [diff] [blame] | 132 | ReportingContext* GetContextForTesting() const override { |
| 133 | return context_.get(); |
| 134 | } |
| 135 | |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 136 | private: |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 137 | void DoOrBacklogTask(base::OnceClosure task) { |
| 138 | if (shut_down_) |
| 139 | return; |
| 140 | |
| 141 | FetchAllClientsFromStoreIfNecessary(); |
| 142 | |
| 143 | if (!initialized_) { |
| 144 | task_backlog_.push_back(std::move(task)); |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | std::move(task).Run(); |
| 149 | } |
| 150 | |
| 151 | void DoQueueReport(GURL sanitized_url, |
| 152 | const std::string& user_agent, |
| 153 | const std::string& group, |
| 154 | const std::string& type, |
| 155 | std::unique_ptr<const base::Value> body, |
| 156 | int depth, |
| 157 | base::TimeTicks queued_ticks) { |
| 158 | DCHECK(initialized_); |
| 159 | context_->cache()->AddReport(sanitized_url, user_agent, group, type, |
| 160 | std::move(body), depth, queued_ticks, |
| 161 | 0 /* attempts */); |
| 162 | } |
| 163 | |
| 164 | void DoProcessHeader(const GURL& url, |
| 165 | std::unique_ptr<base::Value> header_value) { |
| 166 | DCHECK(initialized_); |
| 167 | ReportingHeaderParser::ParseHeader(context_.get(), url, |
| 168 | std::move(header_value)); |
| 169 | } |
| 170 | |
| 171 | void DoRemoveBrowsingData( |
| 172 | int data_type_mask, |
| 173 | const base::RepeatingCallback<bool(const GURL&)>& origin_filter) { |
| 174 | DCHECK(initialized_); |
| 175 | ReportingBrowsingDataRemover::RemoveBrowsingData( |
| 176 | context_->cache(), data_type_mask, origin_filter); |
| 177 | } |
| 178 | |
| 179 | void DoRemoveAllBrowsingData(int data_type_mask) { |
| 180 | DCHECK(initialized_); |
| 181 | ReportingBrowsingDataRemover::RemoveAllBrowsingData(context_->cache(), |
| 182 | data_type_mask); |
| 183 | } |
| 184 | |
| 185 | void ExecuteBacklog() { |
| 186 | DCHECK(initialized_); |
| 187 | DCHECK(context_); |
| 188 | |
| 189 | if (shut_down_) |
| 190 | return; |
| 191 | |
| 192 | for (base::OnceClosure& task : task_backlog_) { |
| 193 | std::move(task).Run(); |
| 194 | } |
| 195 | task_backlog_.clear(); |
| 196 | } |
| 197 | |
| 198 | void FetchAllClientsFromStoreIfNecessary() { |
| 199 | if (!context_->IsClientDataPersisted() || started_loading_from_store_) |
| 200 | return; |
| 201 | |
| 202 | started_loading_from_store_ = true; |
| 203 | FetchAllClientsFromStore(); |
| 204 | } |
| 205 | |
| 206 | void FetchAllClientsFromStore() { |
| 207 | DCHECK(context_->IsClientDataPersisted()); |
| 208 | DCHECK(!initialized_); |
| 209 | |
| 210 | context_->store()->LoadReportingClients(base::BindOnce( |
| 211 | &ReportingServiceImpl::OnClientsLoaded, weak_factory_.GetWeakPtr())); |
| 212 | } |
| 213 | |
| 214 | void OnClientsLoaded( |
| 215 | std::vector<ReportingEndpoint> loaded_endpoints, |
| 216 | std::vector<CachedReportingEndpointGroup> loaded_endpoint_groups) { |
| 217 | initialized_ = true; |
| 218 | context_->cache()->AddClientsLoadedFromStore( |
| 219 | std::move(loaded_endpoints), std::move(loaded_endpoint_groups)); |
| 220 | ExecuteBacklog(); |
| 221 | } |
| 222 | |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 223 | std::unique_ptr<ReportingContext> context_; |
Lily Chen | 91b51e6 | 2019-03-01 16:46:07 | [diff] [blame] | 224 | bool shut_down_; |
Sam Burnett | f13c06b | 2019-07-30 15:53:49 | [diff] [blame] | 225 | bool started_loading_from_store_; |
| 226 | bool initialized_; |
| 227 | std::vector<base::OnceClosure> task_backlog_; |
Jeremy Roman | 2b7c950 | 2019-08-21 22:34:08 | [diff] [blame] | 228 | base::WeakPtrFactory<ReportingServiceImpl> weak_factory_{this}; |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 229 | |
| 230 | DISALLOW_COPY_AND_ASSIGN(ReportingServiceImpl); |
| 231 | }; |
| 232 | |
| 233 | } // namespace |
| 234 | |
Chris Watkins | 806691b | 2017-12-01 06:01:22 | [diff] [blame] | 235 | ReportingService::~ReportingService() = default; |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 236 | |
| 237 | // static |
| 238 | std::unique_ptr<ReportingService> ReportingService::Create( |
| 239 | const ReportingPolicy& policy, |
Lily Chen | 731a95c | 2019-05-06 22:27:22 | [diff] [blame] | 240 | URLRequestContext* request_context, |
| 241 | ReportingCache::PersistentReportingStore* store) { |
Douglas Creager | 2b81901f | 2018-10-11 21:29:36 | [diff] [blame] | 242 | return std::make_unique<ReportingServiceImpl>( |
Lily Chen | 731a95c | 2019-05-06 22:27:22 | [diff] [blame] | 243 | ReportingContext::Create(policy, request_context, store)); |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | // static |
| 247 | std::unique_ptr<ReportingService> ReportingService::CreateForTesting( |
| 248 | std::unique_ptr<ReportingContext> reporting_context) { |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 249 | return std::make_unique<ReportingServiceImpl>(std::move(reporting_context)); |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 250 | } |
| 251 | |
Douglas Creager | 853b209 | 2018-04-13 23:03:27 | [diff] [blame] | 252 | base::Value ReportingService::StatusAsValue() const { |
| 253 | NOTIMPLEMENTED(); |
| 254 | return base::Value(); |
| 255 | } |
| 256 | |
juliatuttle | 381d77e | 2017-04-07 18:54:12 | [diff] [blame] | 257 | } // namespace net |