Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(229)

Side by Side Diff: net/reporting/reporting_delegate_impl.cc

Issue 2689953004: Reporting: Implement header parser. (Closed)
Patch Set: Finish DelegateImpl implementation Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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, group, type, std::move(body), clock_->NowTicks(), 0);
26 }
27
28 void ReportingDelegateImpl::OnHeaderReceived(const GURL& url,
29 const std::string& value) {
30 std::unique_ptr<base::Value> json = base::JSONReader::Read("[" + value + "]");
31 if (!json)
32 return;
33
34 const base::ListValue* list;
shivanisha 2017/03/01 21:51:57 const base::ListValue* list = nullptr;
Julia Tuttle 2017/03/06 17:57:51 Done.
35 bool is_list = json->GetAsList(&list);
36 DCHECK(is_list);
37
38 const base::Value* item;
shivanisha 2017/03/01 21:51:56 const base::Value* item = nullptr; Also, may be re
Julia Tuttle 2017/03/06 17:57:50 Renaming to "endpoint", since it describes what sh
39 for (size_t i = 0; i < list->GetSize(); i++) {
40 bool got = list->Get(i, &item);
shivanisha 2017/03/01 21:51:56 nit: may be rename "got" to "is_present"
Julia Tuttle 2017/03/06 17:57:51 Renaming to "got_endpoint".
41 DCHECK(got);
42 ProcessEndpoint(url, item);
43 }
44 }
45
46 void ReportingDelegateImpl::ProcessEndpoint(const GURL& url,
47 const base::Value* json) {
48 const base::DictionaryValue* dict;
shivanisha 2017/03/01 21:51:56 const base::DictionaryValue* dict = nullptr;
Julia Tuttle 2017/03/06 17:57:51 Done.
49 if (!json->GetAsDictionary(&dict))
50 return;
51
52 std::string endpoint_url_string;
53 if (!dict->GetString("url", &endpoint_url_string))
54 return;
shivanisha 2017/03/01 21:51:57 empty line after this return.
Julia Tuttle 2017/03/06 17:57:51 Done.
55 GURL endpoint_url(endpoint_url_string);
56 if (!endpoint_url.is_valid())
57 return;
58
59 bool subdomains = false;
60 dict->GetBoolean("includeSubdomains", &subdomains);
61 // This ignores if includeSubdomains is present but the wrong type.
62
63 std::string group = "default";
64 dict->GetString("group", &group);
65 // This ignores if group is present but the wrong type.
66
67 int ttl_sec;
shivanisha 2017/03/01 21:51:56 int ttl_sec = 0;
Julia Tuttle 2017/03/06 17:57:51 Initializing to -1, so if GetInteger doesn't set i
68 if (!dict->GetInteger("max-age", &ttl_sec) || ttl_sec < 0)
69 return;
70
71 if (ttl_sec == 0) {
72 cache_->RemoveClientForOriginAndEndpoint(url::Origin(url), endpoint_url);
73 return;
74 }
75
76 base::TimeDelta ttl = base::TimeDelta::FromSeconds(ttl_sec);
77
78 cache_->SetClient(url::Origin(url), endpoint_url, subdomains, group,
79 clock_->NowTicks() + ttl);
80 }
81
82 } // namespace net
OLDNEW
« net/reporting/reporting_delegate_impl.h ('K') | « net/reporting/reporting_delegate_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698