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

Unified Diff: net/reporting/reporting_delegate_impl.cc

Issue 2689953004: Reporting: Implement header parser. (Closed)
Patch Set: DISALLOW_COPY_AND_ASSIGN 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 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..1a20028560a23600d82a883efad56cf9b066de01
--- /dev/null
+++ b/net/reporting/reporting_delegate_impl.cc
@@ -0,0 +1,93 @@
+// 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.GetAsReferrer(), group, type, std::move(body),
+ clock_->NowTicks(), 0);
+}
+
+void ReportingDelegateImpl::OnHeaderReceived(const GURL& url,
+ const std::string& json_value) {
+ if (!url.SchemeIsCryptographic())
+ return;
+
+ std::unique_ptr<base::Value> value =
+ base::JSONReader::Read("[" + json_value + "]");
+ if (!value)
+ return;
+
+ const base::ListValue* list = nullptr;
+ bool is_list = value->GetAsList(&list);
+ DCHECK(is_list && list);
+
+ for (size_t i = 0; i < list->GetSize(); i++) {
+ const base::Value* endpoint = nullptr;
+ bool got_endpoint = list->Get(i, &endpoint);
+ DCHECK(got_endpoint && endpoint);
+ ProcessEndpoint(url, *endpoint);
+ }
+}
+
+void ReportingDelegateImpl::ProcessEndpoint(const GURL& url,
+ const base::Value& value) {
+ const base::DictionaryValue* dict = nullptr;
+ if (!value.GetAsDictionary(&dict))
+ return;
+ DCHECK(dict);
+
+ std::string endpoint_url_string;
+ if (!dict->GetString("url", &endpoint_url_string))
+ return;
+
+ GURL endpoint_url(endpoint_url_string);
+ if (!endpoint_url.is_valid())
+ return;
+ if (!endpoint_url.SchemeIsCryptographic())
+ return;
+
+ bool subdomains = false;
+ if (dict->HasKey("includeSubdomains") &&
+ !dict->GetBoolean("includeSubdomains", &subdomains)) {
+ return;
+ }
+
+ std::string group = "default";
+ if (dict->HasKey("group") && !dict->GetString("group", &group))
+ return;
+
+ int ttl_sec = -1;
+ 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

Powered by Google App Engine
This is Rietveld 408576698