| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_BROWSING_DATA_CLEAR_SITE_DATA_THROTTLE_H_ | 5 #ifndef CONTENT_BROWSER_BROWSING_DATA_CLEAR_SITE_DATA_THROTTLE_H_ |
| 6 #define CONTENT_BROWSER_BROWSING_DATA_CLEAR_SITE_DATA_THROTTLE_H_ | 6 #define CONTENT_BROWSER_BROWSING_DATA_CLEAR_SITE_DATA_THROTTLE_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> |
| 9 #include <vector> | 10 #include <vector> |
| 10 | 11 |
| 11 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 13 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 14 #include "base/values.h" | 15 #include "base/time/time.h" |
| 15 #include "content/public/browser/navigation_throttle.h" | |
| 16 #include "content/public/browser/resource_request_info.h" | 16 #include "content/public/browser/resource_request_info.h" |
| 17 #include "content/public/browser/resource_throttle.h" |
| 17 #include "content/public/common/console_message_level.h" | 18 #include "content/public/common/console_message_level.h" |
| 19 #include "net/http/http_response_headers.h" |
| 18 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 19 | 21 |
| 22 namespace net { |
| 23 class HttpResponseHeaders; |
| 24 struct RedirectInfo; |
| 25 class URLRequest; |
| 26 } |
| 27 |
| 28 namespace url { |
| 29 class Origin; |
| 30 } |
| 31 |
| 20 namespace content { | 32 namespace content { |
| 21 | 33 |
| 22 class NavigationHandle; | 34 // This throttle parses the Clear-Site-Data header and executes the clearing |
| 35 // of browsing data. The resource load is delayed until the header is parsed |
| 36 // and, if valid, until the browsing data are deleted. See the W3C working draft |
| 37 // at https://w3c.github.io/webappsec-clear-site-data/. |
| 38 class CONTENT_EXPORT ClearSiteDataThrottle : public ResourceThrottle { |
| 39 public: |
| 40 // Stores and outputs console messages. |
| 41 class CONTENT_EXPORT ConsoleMessagesDelegate { |
| 42 public: |
| 43 struct Message { |
| 44 GURL url; |
| 45 std::string text; |
| 46 ConsoleMessageLevel level; |
| 47 }; |
| 23 | 48 |
| 24 // This throttle parses the Clear-Site-Data header and executes the clearing | 49 ConsoleMessagesDelegate(); |
| 25 // of browsing data. The navigation is delayed until the header is parsed and, | 50 virtual ~ConsoleMessagesDelegate(); |
| 26 // if valid, until the browsing data are deleted. See the W3C working draft at | 51 |
| 27 // https://www.w3.org/TR/clear-site-data/. | 52 // Logs a |text| message from |url| with |level|. |
| 28 class CONTENT_EXPORT ClearSiteDataThrottle : public NavigationThrottle { | 53 virtual void AddMessage(const GURL& url, |
| 29 public: | 54 const std::string& text, |
| 30 struct ConsoleMessage { | 55 ConsoleMessageLevel level); |
| 31 GURL url; | 56 |
| 32 std::string text; | 57 // Outputs stored messages to the console of WebContents identified by |
| 33 ConsoleMessageLevel level; | 58 // |web_contents_getter|. |
| 59 virtual void OutputMessages( |
| 60 const ResourceRequestInfo::WebContentsGetter& web_contents_getter); |
| 61 |
| 62 const std::vector<Message>& messages() const { return messages_; } |
| 63 |
| 64 private: |
| 65 std::vector<Message> messages_; |
| 34 }; | 66 }; |
| 35 | 67 |
| 36 static std::unique_ptr<NavigationThrottle> CreateThrottleForNavigation( | 68 // Instantiates a throttle for the given if it's supported for the given |
| 37 NavigationHandle* handle); | 69 // |request|. The caller must guarantee that |request| outlives the throttle. |
| 70 static std::unique_ptr<ResourceThrottle> MaybeCreateThrottleForRequest( |
| 71 net::URLRequest* request); |
| 38 | 72 |
| 39 ~ClearSiteDataThrottle() override; | 73 ~ClearSiteDataThrottle() override; |
| 40 | 74 |
| 41 // NavigationThrottle implementation: | 75 // ResourceThrottle implementation: |
| 42 ThrottleCheckResult WillStartRequest() override; | 76 const char* GetNameForLogging() const override; |
| 43 ThrottleCheckResult WillRedirectRequest() override; | 77 void WillRedirectRequest(const net::RedirectInfo& redirect_info, |
| 44 ThrottleCheckResult WillProcessResponse() override; | 78 bool* defer) override; |
| 45 const char* GetNameForLogging() override; | 79 void WillProcessResponse(bool* defer) override; |
| 80 |
| 81 // Exposes ParseHeader() publicly for testing. |
| 82 static bool ParseHeaderForTesting(const std::string& header, |
| 83 bool* clear_cookies, |
| 84 bool* clear_storage, |
| 85 bool* clear_cache, |
| 86 ConsoleMessagesDelegate* delegate, |
| 87 const GURL& current_url); |
| 88 |
| 89 protected: |
| 90 ClearSiteDataThrottle(net::URLRequest* request, |
| 91 std::unique_ptr<ConsoleMessagesDelegate> delegate); |
| 46 | 92 |
| 47 private: | 93 private: |
| 48 friend class ClearSiteDataFuzzerTest; | 94 // Returns HTTP response headers of the underlying URLRequest. |
| 49 friend class ClearSiteDataThrottleTest; | 95 // Can be overriden for testing. |
| 50 FRIEND_TEST_ALL_PREFIXES(ClearSiteDataThrottleTest, ParseHeader); | 96 virtual const net::HttpResponseHeaders* GetResponseHeaders() const; |
| 51 FRIEND_TEST_ALL_PREFIXES(ClearSiteDataThrottleTest, InvalidHeader); | |
| 52 | |
| 53 explicit ClearSiteDataThrottle(NavigationHandle* navigation_handle); | |
| 54 | 97 |
| 55 // Scans for the first occurrence of the 'Clear-Site-Data' header, calls | 98 // Scans for the first occurrence of the 'Clear-Site-Data' header, calls |
| 56 // ParseHeader() to parse it, and requests the actual data clearing. This is | 99 // ParseHeader() to parse it, and then ExecuteClearingTask() if applicable. |
| 57 // the common logic of WillRedirectRequest() and WillProcessResponse(). | 100 // This is the common logic of WillRedirectRequest() |
| 58 void HandleHeader(); | 101 // and WillProcessResponse(). Returns true if a valid header was found and |
| 102 // the clearing was executed. |
| 103 bool HandleHeader(); |
| 59 | 104 |
| 60 // Parses the value of the 'Clear-Site-Data' header and outputs whether | 105 // Parses the value of the 'Clear-Site-Data' header and outputs whether |
| 61 // the header requests to |clear_cookies|, |clear_storage|, and |clear_cache|. | 106 // the header requests to |clear_cookies|, |clear_storage|, and |clear_cache|. |
| 62 // The |messages| vector will be filled with messages to be output in the | 107 // The |delegate| will be filled with messages to be output in the console, |
| 63 // console. Returns true if parsing was successful. | 108 // prepended by the |current_url|. Returns true if parsing was successful. |
| 64 bool ParseHeader(const std::string& header, | 109 static bool ParseHeader(const std::string& header, |
| 65 bool* clear_cookies, | 110 bool* clear_cookies, |
| 66 bool* clear_storage, | 111 bool* clear_storage, |
| 67 bool* clear_cache, | 112 bool* clear_cache, |
| 68 std::vector<ConsoleMessage>* messages); | 113 ConsoleMessagesDelegate* delegate, |
| 114 const GURL& current_url); |
| 115 |
| 116 // Executes the clearing task. Can be overriden for testing. |
| 117 virtual void ExecuteClearingTask(const url::Origin& origin, |
| 118 bool clear_cookies, |
| 119 bool clear_storage, |
| 120 bool clear_cache, |
| 121 base::OnceClosure callback); |
| 69 | 122 |
| 70 // Signals that a parsing and deletion task was finished. | 123 // Signals that a parsing and deletion task was finished. |
| 71 void TaskFinished(); | 124 void TaskFinished(); |
| 72 | 125 |
| 73 // Cached console messages to be output when the RenderFrameHost is ready. | 126 // The request this throttle is observing. |
| 74 std::vector<ConsoleMessage> messages_; | 127 net::URLRequest* request_; |
| 75 GURL current_url_; | |
| 76 | 128 |
| 77 // Whether we are currently waiting for a callback that data clearing has | 129 // The delegate that stores and outputs console messages. |
| 78 // been completed; | 130 std::unique_ptr<ConsoleMessagesDelegate> delegate_; |
| 79 bool clearing_in_progress_; | |
| 80 | 131 |
| 81 // The time when the last clearing operation started. Used when clearing | 132 // The time when the last clearing operation started. Used when clearing |
| 82 // finishes to compute the duration. | 133 // finishes to compute the duration. |
| 83 base::TimeTicks clearing_started_; | 134 base::TimeTicks clearing_started_; |
| 84 | 135 |
| 85 // Needed for asynchronous parsing and deletion tasks. | 136 // Needed for asynchronous parsing and deletion tasks. |
| 86 base::WeakPtrFactory<ClearSiteDataThrottle> weak_ptr_factory_; | 137 base::WeakPtrFactory<ClearSiteDataThrottle> weak_ptr_factory_; |
| 87 | 138 |
| 88 DISALLOW_COPY_AND_ASSIGN(ClearSiteDataThrottle); | 139 DISALLOW_COPY_AND_ASSIGN(ClearSiteDataThrottle); |
| 89 }; | 140 }; |
| 90 | 141 |
| 91 } // namespace content | 142 } // namespace content |
| 92 | 143 |
| 93 #endif // CONTENT_BROWSER_BROWSING_DATA_CLEAR_SITE_DATA_THROTTLE_H_ | 144 #endif // CONTENT_BROWSER_BROWSING_DATA_CLEAR_SITE_DATA_THROTTLE_H_ |
| OLD | NEW |