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