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

Unified Diff: net/reporting/reporting_header_parser.cc

Issue 2689953004: Reporting: Implement header parser. (Closed)
Patch Set: Make requested changes. 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_header_parser.cc
diff --git a/net/reporting/reporting_header_parser.cc b/net/reporting/reporting_header_parser.cc
new file mode 100644
index 0000000000000000000000000000000000000000..7fc4dfd2d5df0833cb973ac514557361b0016ce3
--- /dev/null
+++ b/net/reporting/reporting_header_parser.cc
@@ -0,0 +1,87 @@
+// 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_header_parser.h"
+
+#include <string>
+
+#include "base/json/json_reader.h"
+#include "base/time/time.h"
+#include "base/values.h"
+#include "net/reporting/reporting_cache.h"
+#include "url/gurl.h"
+#include "url/origin.h"
+
+namespace net {
+
+// static
+void ReportingHeaderParser::ParseHeader(ReportingCache* cache,
+ base::TimeTicks now,
+ const GURL& url,
+ const std::string& json_value) {
+ if (!url.SchemeIsCryptographic())
jkarlin 2017/03/28 18:29:50 You DCHECK this in the cache, should DCHECK here a
Julia Tuttle 2017/03/28 20:28:54 Done.
+ 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);
jkarlin 2017/03/28 18:29:50 Prefer two separate into two DCHECKS so you know w
Julia Tuttle 2017/03/28 20:28:54 Come to think of it, I don't actually need to chec
+
+ 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);
jkarlin 2017/03/28 18:29:50 Prefer two separate into two DCHECKS so you know w
Julia Tuttle 2017/03/28 20:28:55 Ditto.
+ ProcessEndpoint(cache, now, url, *endpoint);
+ }
+}
+
+// static
+void ReportingHeaderParser::ProcessEndpoint(ReportingCache* cache,
+ base::TimeTicks now,
+ 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;
+
+ ReportingClient::Subdomains subdomains = ReportingClient::Subdomains::EXCLUDE;
+ bool subdomains_bool = false;
+ if (dict->HasKey("includeSubdomains") &&
jkarlin 2017/03/28 18:29:50 "includeSubdomains" should should be a const char[
Julia Tuttle 2017/03/28 20:28:55 Done.
+ dict->GetBoolean("includeSubdomains", &subdomains_bool) &&
+ subdomains_bool == true) {
+ subdomains = ReportingClient::Subdomains::INCLUDE;
+ }
+
+ 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->SetClient(url::Origin(url), endpoint_url, subdomains, group,
+ now + base::TimeDelta::FromSeconds(ttl_sec));
+ } else {
+ cache->RemoveClientForOriginAndEndpoint(url::Origin(url), endpoint_url);
+ }
+}
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698