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

Unified Diff: content/browser/browsing_data/clear_site_data_throttle.cc

Issue 2025683003: First experimental implementation of the Clear-Site-Data header (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comment, file URLs Created 4 years, 6 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: content/browser/browsing_data/clear_site_data_throttle.cc
diff --git a/content/browser/browsing_data/clear_site_data_throttle.cc b/content/browser/browsing_data/clear_site_data_throttle.cc
new file mode 100644
index 0000000000000000000000000000000000000000..7d2b99ebcb162f4fdfaeac541f5bf538e1055517
--- /dev/null
+++ b/content/browser/browsing_data/clear_site_data_throttle.cc
@@ -0,0 +1,233 @@
+// Copyright 2016 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 "content/browser/browsing_data/clear_site_data_throttle.h"
+
+#include "base/json/json_reader.h"
+#include "base/json/json_string_value_serializer.h"
+#include "base/strings/stringprintf.h"
+#include "base/values.h"
+#include "content/browser/frame_host/navigation_handle_impl.h"
+#include "content/public/browser/browser_context.h"
+#include "content/public/browser/content_browser_client.h"
+#include "content/public/browser/navigation_handle.h"
+#include "content/public/browser/web_contents.h"
+#include "content/public/common/content_client.h"
+#include "content/public/common/origin_util.h"
+#include "net/http/http_response_headers.h"
+#include "url/gurl.h"
+#include "url/origin.h"
+
+namespace content {
+
+namespace {
+
+static const char* kClearSiteDataHeader = "Clear-Site-Data";
+
+static const char* kTypesKey = "types";
+
+// Pretty-printed log output.
+static const char* kConsoleMessageFormat =
+ "Clear-Site-Data header on '%s': %s";
+static const char* kClearingOneType = "Clearing %s.";
+static const char* kClearingTwoTypes = "Clearing %s and %s.";
+static const char* kClearingThreeTypes = "Clearing %s, %s and %s.";
Mike West 2016/06/20 07:57:37 Nit: Oxford comma, plz.
msramek 2016/07/15 16:47:39 Done.
+
+} // namespace
+
+// static
+std::unique_ptr<NavigationThrottle>
+ ClearSiteDataThrottle::CreateThrottleFor(NavigationHandle* handle) {
+ return std::unique_ptr<NavigationThrottle>(new ClearSiteDataThrottle(handle));
+}
+
+ClearSiteDataThrottle::~ClearSiteDataThrottle() {}
+
+ClearSiteDataThrottle::ThrottleCheckResult
+ ClearSiteDataThrottle::WillStartRequest() {
+ current_url_ = navigation_handle()->GetURL();
+ return PROCEED;
+}
+
+ClearSiteDataThrottle::ThrottleCheckResult
+ ClearSiteDataThrottle::WillRedirectRequest() {
+ // We are processing a redirect from url1 to url2. GetResponseHeaders()
+ // contains headers from url1, but GetURL() is already equal to url2. Handle
+ // the headers before updating the URL, so that |current_url_| corresponds
+ // to the URL that sent the headers.
+ HandleHeader();
+ current_url_ = navigation_handle()->GetURL();
+
+ return PROCEED;
+}
+
+ClearSiteDataThrottle::ThrottleCheckResult
+ ClearSiteDataThrottle::WillProcessResponse() {
+ HandleHeader();
+
+ // Now that RenderFrameHost is ready, output the console messages.
+ for (const ConsoleMessage& message : messages_) {
+ navigation_handle()->GetRenderFrameHost()->AddMessageToConsole(
+ message.level,
+ base::StringPrintf(
+ kConsoleMessageFormat, navigation_handle()->GetURL().spec().c_str(),
+ message.text.c_str()));
+ }
+
+ return PROCEED;
+}
+
+ClearSiteDataThrottle::ClearSiteDataThrottle(NavigationHandle* handle)
+ : NavigationThrottle(handle) {}
+
+void ClearSiteDataThrottle::HandleHeader() {
+ NavigationHandleImpl* handle =
+ static_cast<NavigationHandleImpl*>(navigation_handle());
+
+ // Ignore file URL navigations, as those have no response headers.
+ if (current_url_.SchemeIsFile())
+ return;
+
+ // Only accept the header on secure origins.
+ if (!IsOriginSecure(current_url_)) {
+ ConsoleLog(&messages_,
+ "Not supported for insecure origins.",
+ CONSOLE_MESSAGE_LEVEL_ERROR);
+ return;
+ }
+
+ // Extract the instances of the header and parse them.
+ size_t iter = 0;
+ std::string header_name;
+ std::string header_value;
+ while (handle->GetResponseHeaders()->EnumerateHeaderLines(
+ &iter, &header_name, &header_value)) {
+ if (header_name != kClearSiteDataHeader)
+ continue;
+
+ bool clear_cookies;
+ bool clear_storage;
+ bool clear_cache;
+
+ if (!ParseHeader(header_value, &clear_cookies, &clear_storage, &clear_cache,
+ &messages_)) {
+ continue;
+ }
+
+ // If the header is valid, clear the data for this browser context
+ // and origin.
+ BrowserContext* browser_context =
+ handle->GetWebContents()->GetBrowserContext();
+ url::Origin origin(current_url_);
+
+ GetContentClient()->browser()->ClearSiteData(
+ browser_context, origin, clear_cookies, clear_storage, clear_cache);
+ }
+}
+
+bool ClearSiteDataThrottle::ParseHeader(
+ const std::string& header,
+ bool* clear_cookies, bool* clear_storage, bool* clear_cache,
+ std::vector<ConsoleMessage>* messages) {
+ std::unique_ptr<base::Value> parsed_header =
+ base::JSONReader::Read(header);
+
+ if (!parsed_header) {
+ ConsoleLog(messages,
+ base::StringPrintf("%s is not a valid JSON.", header.c_str()),
+ CONSOLE_MESSAGE_LEVEL_ERROR);
+ return false;
+ }
+
+ if (!parsed_header->GetAsDictionary(nullptr)) {
+ ConsoleLog(messages,
+ base::StringPrintf("%s is not a dictionary.", header.c_str()),
+ CONSOLE_MESSAGE_LEVEL_ERROR);
+ return false;
+ }
Mike West 2016/06/20 07:57:38 It doesn't seem worthwhile to distinguish between
msramek 2016/07/15 16:47:39 Done. But I would still call it a JSON dictionary
+
+ const base::ListValue* types;
+ if (!static_cast<base::DictionaryValue*>(parsed_header.get())
+ ->GetListWithoutPathExpansion(kTypesKey, &types)) {
+ ConsoleLog(messages,
+ base::StringPrintf(
+ "No 'types' field present in %s.", header.c_str()),
+ CONSOLE_MESSAGE_LEVEL_ERROR);
+ return false;
+ }
+
+ *clear_cookies = false;
+ *clear_storage = false;
+ *clear_cache = false;
+
+ std::vector<std::string> type_names;
+ for (const std::unique_ptr<base::Value>& value : *types) {
+ std::string type;
+ value->GetAsString(&type);
+
+ bool* datatype = nullptr;
+
+ if (type == "cookies") {
+ datatype = clear_cookies;
+ } else if (type == "storage") {
+ datatype = clear_storage;
+ } else if (type == "cache") {
+ datatype = clear_cache;
+ } else {
+ std::string serialized_type;
+ JSONStringValueSerializer serializer(&serialized_type);
+ serializer.Serialize(*value);
Mike West 2016/06/20 07:57:38 Nit: It doesn't look like you use |serialized_type
msramek 2016/07/15 16:47:39 Done. Thanks for catching. If |value| is e.g. anot
+ ConsoleLog(messages,
+ base::StringPrintf("Invalid type: '%s'.", type.c_str()),
+ CONSOLE_MESSAGE_LEVEL_ERROR);
+ continue;
+ }
+
+ // Each data type should only be
Mike West 2016/06/20 07:57:37 Nit: Should only be... ?
msramek 2016/07/15 16:47:39 Correct. Every data type should exist, and nothing
+ DCHECK(datatype != nullptr);
Mike West 2016/06/20 07:57:37 Nit: `DCHECK(datatype)`
msramek 2016/07/15 16:47:39 Done.
+ if (*datatype)
+ continue;
+
+ *datatype = true;
+ type_names.push_back(type);
+ }
+
+ if (!*clear_cookies && !*clear_storage && !*clear_cache) {
+ ConsoleLog(messages,
+ base::StringPrintf(
+ "No valid types specified in %s.", header.c_str()),
+ CONSOLE_MESSAGE_LEVEL_ERROR);
+ return false;
+ }
+
+ // Pretty-print which types are to be cleared.
+ std::string output;
+ switch (type_names.size()) {
+ case 1:
+ output = base::StringPrintf(kClearingOneType, type_names[0].c_str());
+ break;
+ case 2:
+ output = base::StringPrintf(
+ kClearingTwoTypes, type_names[0].c_str(), type_names[1].c_str());
+ break;
+ case 3:
+ output = base::StringPrintf(
+ kClearingThreeTypes,
+ type_names[0].c_str(), type_names[1].c_str(), type_names[2].c_str());
+ break;
+ default:
+ NOTREACHED();
+ }
+ ConsoleLog(messages, output, CONSOLE_MESSAGE_LEVEL_LOG);
Mike West 2016/06/20 07:57:37 Nit: Would you mind pulling this out into a static
msramek 2016/07/15 16:47:39 Done. That also requires ConsoleMessage to be publ
+
+ return true;
+}
+
+void ClearSiteDataThrottle::ConsoleLog(
+ std::vector<ConsoleMessage>* messages,
+ const std::string& text, ConsoleMessageLevel level) {
+ messages->push_back({text, level});
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698