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

Unified Diff: net/reporting/reporting_delegate_impl.cc

Issue 2689953004: Reporting: Implement header parser. (Closed)
Patch Set: Finish DelegateImpl implementation Created 3 years, 10 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 side-by-side diff with in-line comments
Download patch
Index: net/reporting/reporting_delegate_impl.cc
diff --git a/net/reporting/reporting_delegate_impl.cc b/net/reporting/reporting_delegate_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..9caa5e12afb8a87205f539d2c52d95a4fd0a0eaf
--- /dev/null
+++ b/net/reporting/reporting_delegate_impl.cc
@@ -0,0 +1,82 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/reporting/reporting_delegate_impl.h"
+
+#include "base/json/json_reader.h"
+#include "base/time/time.h"
+#include "base/values.h"
+#include "net/reporting/reporting_cache.h"
+
+namespace net {
+
+ReportingDelegateImpl::ReportingDelegateImpl(base::TickClock* clock,
+ ReportingCache* cache)
+ : clock_(clock), cache_(cache) {}
+
+ReportingDelegateImpl::~ReportingDelegateImpl() {}
+
+void ReportingDelegateImpl::OnReportGenerated(
+ const GURL& url,
+ const std::string& group,
+ const std::string& type,
+ std::unique_ptr<const base::Value> body) {
+ cache_->AddReport(url, group, type, std::move(body), clock_->NowTicks(), 0);
+}
+
+void ReportingDelegateImpl::OnHeaderReceived(const GURL& url,
+ const std::string& value) {
+ std::unique_ptr<base::Value> json = base::JSONReader::Read("[" + value + "]");
+ if (!json)
+ return;
+
+ 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.
+ bool is_list = json->GetAsList(&list);
+ DCHECK(is_list);
+
+ 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
+ for (size_t i = 0; i < list->GetSize(); i++) {
+ 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".
+ DCHECK(got);
+ ProcessEndpoint(url, item);
+ }
+}
+
+void ReportingDelegateImpl::ProcessEndpoint(const GURL& url,
+ const base::Value* json) {
+ 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.
+ if (!json->GetAsDictionary(&dict))
+ return;
+
+ std::string endpoint_url_string;
+ if (!dict->GetString("url", &endpoint_url_string))
+ return;
shivanisha 2017/03/01 21:51:57 empty line after this return.
Julia Tuttle 2017/03/06 17:57:51 Done.
+ GURL endpoint_url(endpoint_url_string);
+ if (!endpoint_url.is_valid())
+ return;
+
+ bool subdomains = false;
+ dict->GetBoolean("includeSubdomains", &subdomains);
+ // This ignores if includeSubdomains is present but the wrong type.
+
+ std::string group = "default";
+ dict->GetString("group", &group);
+ // This ignores if group is present but the wrong type.
+
+ 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
+ if (!dict->GetInteger("max-age", &ttl_sec) || ttl_sec < 0)
+ return;
+
+ if (ttl_sec == 0) {
+ cache_->RemoveClientForOriginAndEndpoint(url::Origin(url), endpoint_url);
+ return;
+ }
+
+ base::TimeDelta ttl = base::TimeDelta::FromSeconds(ttl_sec);
+
+ cache_->SetClient(url::Origin(url), endpoint_url, subdomains, group,
+ clock_->NowTicks() + ttl);
+}
+
+} // namespace net
« 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