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

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

Issue 2368923003: Support the Clear-Site-Data header on resource requests (Closed)
Patch Set: Addressed comments, formatted. Created 3 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.h
diff --git a/content/browser/browsing_data/clear_site_data_throttle.h b/content/browser/browsing_data/clear_site_data_throttle.h
index ec26b7a7578d1359f17f132caad7f2d7944aad60..31d26541c9fc9d7ea4b9de1f9c45db00e21b2d82 100644
--- a/content/browser/browsing_data/clear_site_data_throttle.h
+++ b/content/browser/browsing_data/clear_site_data_throttle.h
@@ -6,77 +6,145 @@
#define CONTENT_BROWSER_BROWSING_DATA_CLEAR_SITE_DATA_THROTTLE_H_
#include <memory>
+#include <string>
#include <vector>
+#include "base/callback.h"
#include "base/gtest_prod_util.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
-#include "base/values.h"
-#include "content/public/browser/navigation_throttle.h"
+#include "base/time/time.h"
#include "content/public/browser/resource_request_info.h"
+#include "content/public/browser/resource_throttle.h"
#include "content/public/common/console_message_level.h"
+#include "net/http/http_response_headers.h"
#include "url/gurl.h"
+namespace net {
+class HttpResponseHeaders;
+struct RedirectInfo;
+class URLRequest;
+}
+
+namespace url {
+class Origin;
+}
+
namespace content {
-class NavigationHandle;
+class WebContents;
// This throttle parses the Clear-Site-Data header and executes the clearing
-// of browsing data. The navigation is delayed until the header is parsed and,
-// if valid, until the browsing data are deleted. See the W3C working draft at
-// https://www.w3.org/TR/clear-site-data/.
-class CONTENT_EXPORT ClearSiteDataThrottle : public NavigationThrottle {
+// of browsing data. The resource load is delayed until the header is parsed
+// and, if valid, until the browsing data are deleted. See the W3C working draft
+// at https://w3c.github.io/webappsec-clear-site-data/.
+class CONTENT_EXPORT ClearSiteDataThrottle : public ResourceThrottle {
public:
- struct ConsoleMessage {
- GURL url;
- std::string text;
- ConsoleMessageLevel level;
+ // Stores and outputs console messages.
+ class CONTENT_EXPORT ConsoleMessagesDelegate {
+ public:
+ struct Message {
+ GURL url;
+ std::string text;
+ ConsoleMessageLevel level;
+ };
+
+ typedef base::Callback<
+ void(WebContents*, ConsoleMessageLevel, const std::string&)>
+ OutputFormattedMessageFunction;
+
+ ConsoleMessagesDelegate();
+ virtual ~ConsoleMessagesDelegate();
+
+ // Logs a |text| message from |url| with |level|.
+ virtual void AddMessage(const GURL& url,
+ const std::string& text,
+ ConsoleMessageLevel level);
+
+ // Outputs stored messages to the console of WebContents identified by
+ // |web_contents_getter|.
+ virtual void OutputMessages(
+ const ResourceRequestInfo::WebContentsGetter& web_contents_getter);
+
+ const std::vector<Message>& messages() const { return messages_; }
+
+ protected:
+ void SetOutputFormattedMessageFunctionForTesting(
+ const OutputFormattedMessageFunction& function);
+
+ private:
+ std::vector<Message> messages_;
+ OutputFormattedMessageFunction output_formatted_message_function_;
};
- static std::unique_ptr<NavigationThrottle> CreateThrottleForNavigation(
- NavigationHandle* handle);
+ // Instantiates a throttle for the given if it's supported for the given
+ // |request|. The caller must guarantee that |request| outlives the throttle.
+ static std::unique_ptr<ResourceThrottle> MaybeCreateThrottleForRequest(
+ net::URLRequest* request);
~ClearSiteDataThrottle() override;
- // NavigationThrottle implementation:
- ThrottleCheckResult WillStartRequest() override;
- ThrottleCheckResult WillRedirectRequest() override;
- ThrottleCheckResult WillProcessResponse() override;
- const char* GetNameForLogging() override;
+ // ResourceThrottle implementation:
+ const char* GetNameForLogging() const override;
+ void WillRedirectRequest(const net::RedirectInfo& redirect_info,
+ bool* defer) override;
+ void WillProcessResponse(bool* defer) override;
- private:
- friend class ClearSiteDataFuzzerTest;
- friend class ClearSiteDataThrottleTest;
- FRIEND_TEST_ALL_PREFIXES(ClearSiteDataThrottleTest, ParseHeader);
- FRIEND_TEST_ALL_PREFIXES(ClearSiteDataThrottleTest, InvalidHeader);
+ // Exposes ParseHeader() publicly for testing.
+ static bool ParseHeaderForTesting(const std::string& header,
+ bool* clear_cookies,
+ bool* clear_storage,
+ bool* clear_cache,
+ ConsoleMessagesDelegate* delegate,
+ const GURL& current_url);
+
+ protected:
+ ClearSiteDataThrottle(net::URLRequest* request,
+ std::unique_ptr<ConsoleMessagesDelegate> delegate);
- explicit ClearSiteDataThrottle(NavigationHandle* navigation_handle);
+ virtual const GURL& GetCurrentURL() const;
+
+ private:
+ // Returns HTTP response headers of the underlying URLRequest.
+ // Can be overriden for testing.
+ virtual const net::HttpResponseHeaders* GetResponseHeaders() const;
// Scans for the first occurrence of the 'Clear-Site-Data' header, calls
- // ParseHeader() to parse it, and requests the actual data clearing. This is
- // the common logic of WillRedirectRequest() and WillProcessResponse().
- void HandleHeader();
+ // ParseHeader() to parse it, and then ExecuteClearingTask() if applicable.
+ // This is the common logic of WillRedirectRequest()
+ // and WillProcessResponse(). Returns true if a valid header was found and
+ // the clearing was executed.
+ bool HandleHeader();
// Parses the value of the 'Clear-Site-Data' header and outputs whether
// the header requests to |clear_cookies|, |clear_storage|, and |clear_cache|.
- // The |messages| vector will be filled with messages to be output in the
- // console. Returns true if parsing was successful.
- bool ParseHeader(const std::string& header,
- bool* clear_cookies,
- bool* clear_storage,
- bool* clear_cache,
- std::vector<ConsoleMessage>* messages);
+ // The |delegate| will be filled with messages to be output in the console,
+ // prepended by the |current_url|. Returns true if parsing was successful.
+ static bool ParseHeader(const std::string& header,
+ bool* clear_cookies,
+ bool* clear_storage,
+ bool* clear_cache,
+ ConsoleMessagesDelegate* delegate,
+ const GURL& current_url);
+
+ // Executes the clearing task. Can be overriden for testing.
+ virtual void ExecuteClearingTask(const url::Origin& origin,
+ bool clear_cookies,
+ bool clear_storage,
+ bool clear_cache,
+ base::OnceClosure callback);
// Signals that a parsing and deletion task was finished.
void TaskFinished();
- // Cached console messages to be output when the RenderFrameHost is ready.
- std::vector<ConsoleMessage> messages_;
- GURL current_url_;
+ // Outputs the console messages in the |delegate_|.
+ void OutputConsoleMessages();
+
+ // The request this throttle is observing.
+ net::URLRequest* request_;
- // Whether we are currently waiting for a callback that data clearing has
- // been completed;
- bool clearing_in_progress_;
+ // The delegate that stores and outputs console messages.
+ std::unique_ptr<ConsoleMessagesDelegate> delegate_;
// The time when the last clearing operation started. Used when clearing
// finishes to compute the duration.
« no previous file with comments | « chrome/browser/chrome_content_browser_client_unittest.cc ('k') | content/browser/browsing_data/clear_site_data_throttle.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698