OLD | NEW |
(Empty) | |
| 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_delegate_impl.h" |
| 6 |
| 7 #include "base/json/json_reader.h" |
| 8 #include "base/time/time.h" |
| 9 #include "base/values.h" |
| 10 #include "net/reporting/reporting_cache.h" |
| 11 |
| 12 namespace net { |
| 13 |
| 14 ReportingDelegateImpl::ReportingDelegateImpl(base::TickClock* clock, |
| 15 ReportingCache* cache) |
| 16 : clock_(clock), cache_(cache) {} |
| 17 |
| 18 ReportingDelegateImpl::~ReportingDelegateImpl() {} |
| 19 |
| 20 void ReportingDelegateImpl::OnReportGenerated( |
| 21 const GURL& url, |
| 22 const std::string& group, |
| 23 const std::string& type, |
| 24 std::unique_ptr<const base::Value> body) { |
| 25 cache_->AddReport(url.GetAsReferrer(), group, type, std::move(body), |
| 26 clock_->NowTicks(), 0); |
| 27 } |
| 28 |
| 29 void ReportingDelegateImpl::OnHeaderReceived(const GURL& url, |
| 30 const std::string& json_value) { |
| 31 if (!url.SchemeIsCryptographic()) |
| 32 return; |
| 33 |
| 34 std::unique_ptr<base::Value> value = |
| 35 base::JSONReader::Read("[" + json_value + "]"); |
| 36 if (!value) |
| 37 return; |
| 38 |
| 39 const base::ListValue* list = nullptr; |
| 40 bool is_list = value->GetAsList(&list); |
| 41 DCHECK(is_list && list); |
| 42 |
| 43 for (size_t i = 0; i < list->GetSize(); i++) { |
| 44 const base::Value* endpoint = nullptr; |
| 45 bool got_endpoint = list->Get(i, &endpoint); |
| 46 DCHECK(got_endpoint && endpoint); |
| 47 ProcessEndpoint(url, *endpoint); |
| 48 } |
| 49 } |
| 50 |
| 51 void ReportingDelegateImpl::ProcessEndpoint(const GURL& url, |
| 52 const base::Value& value) { |
| 53 const base::DictionaryValue* dict = nullptr; |
| 54 if (!value.GetAsDictionary(&dict)) |
| 55 return; |
| 56 DCHECK(dict); |
| 57 |
| 58 std::string endpoint_url_string; |
| 59 if (!dict->GetString("url", &endpoint_url_string)) |
| 60 return; |
| 61 |
| 62 GURL endpoint_url(endpoint_url_string); |
| 63 if (!endpoint_url.is_valid()) |
| 64 return; |
| 65 if (!endpoint_url.SchemeIsCryptographic()) |
| 66 return; |
| 67 |
| 68 bool subdomains = false; |
| 69 if (dict->HasKey("includeSubdomains") && |
| 70 !dict->GetBoolean("includeSubdomains", &subdomains)) { |
| 71 return; |
| 72 } |
| 73 |
| 74 std::string group = "default"; |
| 75 if (dict->HasKey("group") && !dict->GetString("group", &group)) |
| 76 return; |
| 77 |
| 78 int ttl_sec = -1; |
| 79 if (!dict->GetInteger("max-age", &ttl_sec) || ttl_sec < 0) |
| 80 return; |
| 81 |
| 82 if (ttl_sec == 0) { |
| 83 cache_->RemoveClientForOriginAndEndpoint(url::Origin(url), endpoint_url); |
| 84 return; |
| 85 } |
| 86 |
| 87 base::TimeDelta ttl = base::TimeDelta::FromSeconds(ttl_sec); |
| 88 |
| 89 cache_->SetClient(url::Origin(url), endpoint_url, subdomains, group, |
| 90 clock_->NowTicks() + ttl); |
| 91 } |
| 92 |
| 93 } // namespace net |
OLD | NEW |