OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/reporting/reporting_header_parser.h" | 5 #include "net/reporting/reporting_header_parser.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/time/tick_clock.h" | 11 #include "base/time/tick_clock.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "net/reporting/reporting_cache.h" | 14 #include "net/reporting/reporting_cache.h" |
15 #include "net/reporting/reporting_context.h" | 15 #include "net/reporting/reporting_context.h" |
| 16 #include "net/reporting/reporting_delegate.h" |
16 | 17 |
17 namespace net { | 18 namespace net { |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 const char kUrlKey[] = "url"; | 22 const char kUrlKey[] = "url"; |
22 const char kIncludeSubdomainsKey[] = "includeSubdomains"; | 23 const char kIncludeSubdomainsKey[] = "includeSubdomains"; |
23 const char kGroupKey[] = "group"; | 24 const char kGroupKey[] = "group"; |
24 const char kGroupDefaultValue[] = "default"; | 25 const char kGroupDefaultValue[] = "default"; |
25 const char kMaxAgeKey[] = "max-age"; | 26 const char kMaxAgeKey[] = "max-age"; |
26 | 27 |
27 } // namespace | 28 void ProcessEndpoint(ReportingDelegate* delegate, |
28 | 29 ReportingCache* cache, |
29 // static | 30 base::TimeTicks now, |
30 void ReportingHeaderParser::ParseHeader(ReportingContext* context, | 31 const GURL& url, |
31 const GURL& url, | 32 const base::Value& value) { |
32 const std::string& json_value) { | |
33 DCHECK(url.SchemeIsCryptographic()); | |
34 | |
35 std::unique_ptr<base::Value> value = | |
36 base::JSONReader::Read("[" + json_value + "]"); | |
37 if (!value) | |
38 return; | |
39 | |
40 const base::ListValue* list = nullptr; | |
41 bool is_list = value->GetAsList(&list); | |
42 DCHECK(is_list); | |
43 | |
44 ReportingCache* cache = context->cache(); | |
45 base::TimeTicks now = context->tick_clock()->NowTicks(); | |
46 for (size_t i = 0; i < list->GetSize(); i++) { | |
47 const base::Value* endpoint = nullptr; | |
48 bool got_endpoint = list->Get(i, &endpoint); | |
49 DCHECK(got_endpoint); | |
50 ProcessEndpoint(cache, now, url, *endpoint); | |
51 } | |
52 } | |
53 | |
54 // static | |
55 void ReportingHeaderParser::ProcessEndpoint(ReportingCache* cache, | |
56 base::TimeTicks now, | |
57 const GURL& url, | |
58 const base::Value& value) { | |
59 const base::DictionaryValue* dict = nullptr; | 33 const base::DictionaryValue* dict = nullptr; |
60 if (!value.GetAsDictionary(&dict)) | 34 if (!value.GetAsDictionary(&dict)) |
61 return; | 35 return; |
62 DCHECK(dict); | 36 DCHECK(dict); |
63 | 37 |
64 std::string endpoint_url_string; | 38 std::string endpoint_url_string; |
65 if (!dict->GetString(kUrlKey, &endpoint_url_string)) | 39 if (!dict->GetString(kUrlKey, &endpoint_url_string)) |
66 return; | 40 return; |
67 | 41 |
68 GURL endpoint_url(endpoint_url_string); | 42 GURL endpoint_url(endpoint_url_string); |
(...skipping 11 matching lines...) Expand all Loading... |
80 return; | 54 return; |
81 | 55 |
82 ReportingClient::Subdomains subdomains = ReportingClient::Subdomains::EXCLUDE; | 56 ReportingClient::Subdomains subdomains = ReportingClient::Subdomains::EXCLUDE; |
83 bool subdomains_bool = false; | 57 bool subdomains_bool = false; |
84 if (dict->HasKey(kIncludeSubdomainsKey) && | 58 if (dict->HasKey(kIncludeSubdomainsKey) && |
85 dict->GetBoolean(kIncludeSubdomainsKey, &subdomains_bool) && | 59 dict->GetBoolean(kIncludeSubdomainsKey, &subdomains_bool) && |
86 subdomains_bool == true) { | 60 subdomains_bool == true) { |
87 subdomains = ReportingClient::Subdomains::INCLUDE; | 61 subdomains = ReportingClient::Subdomains::INCLUDE; |
88 } | 62 } |
89 | 63 |
| 64 url::Origin origin(url); |
| 65 |
| 66 if (!delegate->CanSetClient(origin, endpoint_url)) |
| 67 return; |
| 68 |
90 if (ttl_sec > 0) { | 69 if (ttl_sec > 0) { |
91 cache->SetClient(url::Origin(url), endpoint_url, subdomains, group, | 70 cache->SetClient(origin, endpoint_url, subdomains, group, |
92 now + base::TimeDelta::FromSeconds(ttl_sec)); | 71 now + base::TimeDelta::FromSeconds(ttl_sec)); |
93 } else { | 72 } else { |
94 cache->RemoveClientForOriginAndEndpoint(url::Origin(url), endpoint_url); | 73 cache->RemoveClientForOriginAndEndpoint(origin, endpoint_url); |
95 } | 74 } |
96 } | 75 } |
97 | 76 |
| 77 } // namespace |
| 78 |
| 79 // static |
| 80 void ReportingHeaderParser::ParseHeader(ReportingContext* context, |
| 81 const GURL& url, |
| 82 const std::string& json_value) { |
| 83 DCHECK(url.SchemeIsCryptographic()); |
| 84 |
| 85 std::unique_ptr<base::Value> value = |
| 86 base::JSONReader::Read("[" + json_value + "]"); |
| 87 if (!value) |
| 88 return; |
| 89 |
| 90 const base::ListValue* list = nullptr; |
| 91 bool is_list = value->GetAsList(&list); |
| 92 DCHECK(is_list); |
| 93 |
| 94 ReportingDelegate* delegate = context->delegate(); |
| 95 ReportingCache* cache = context->cache(); |
| 96 base::TimeTicks now = context->tick_clock()->NowTicks(); |
| 97 for (size_t i = 0; i < list->GetSize(); i++) { |
| 98 const base::Value* endpoint = nullptr; |
| 99 bool got_endpoint = list->Get(i, &endpoint); |
| 100 DCHECK(got_endpoint); |
| 101 ProcessEndpoint(delegate, cache, now, url, *endpoint); |
| 102 } |
| 103 } |
| 104 |
98 } // namespace net | 105 } // namespace net |
OLD | NEW |