Chromium Code Reviews| 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 <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "content/public/browser/navigation_throttle.h" | |
| 16 #include "content/public/browser/resource_request_info.h" | 15 #include "content/public/browser/resource_request_info.h" |
| 16 #include "content/public/browser/resource_throttle.h" | |
| 17 #include "content/public/common/console_message_level.h" | 17 #include "content/public/common/console_message_level.h" |
| 18 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 19 | 19 |
| 20 namespace net { | |
| 21 struct RedirectInfo; | |
| 22 class URLRequest; | |
| 23 } | |
| 24 | |
| 25 namespace url { | |
| 26 class Origin; | |
| 27 } | |
| 28 | |
| 20 namespace content { | 29 namespace content { |
| 21 | 30 |
| 22 class NavigationHandle; | |
| 23 | |
| 24 // This throttle parses the Clear-Site-Data header and executes the clearing | 31 // This throttle parses the Clear-Site-Data header and executes the clearing |
| 25 // of browsing data. The navigation is delayed until the header is parsed and, | 32 // of browsing data. The resource load is delayed until the header is parsed |
| 26 // if valid, until the browsing data are deleted. See the W3C working draft at | 33 // and, if valid, until the browsing data are deleted. See the W3C working draft |
| 27 // https://www.w3.org/TR/clear-site-data/. | 34 // at https://www.w3.org/TR/clear-site-data/. |
| 28 class CONTENT_EXPORT ClearSiteDataThrottle : public NavigationThrottle { | 35 class CONTENT_EXPORT ClearSiteDataThrottle : public ResourceThrottle { |
| 29 public: | 36 public: |
| 30 struct ConsoleMessage { | 37 struct ConsoleMessage { |
| 31 GURL url; | 38 GURL url; |
| 32 std::string text; | 39 std::string text; |
| 33 ConsoleMessageLevel level; | 40 ConsoleMessageLevel level; |
| 34 }; | 41 }; |
| 35 | 42 |
| 36 static std::unique_ptr<NavigationThrottle> CreateThrottleForNavigation( | 43 // Instantiates a throttle for the given |request|. The caller must |
| 37 NavigationHandle* handle); | 44 // guarantee that |request| outlives the throttle. |
| 45 static std::unique_ptr<ResourceThrottle> CreateThrottleForRequest( | |
| 46 net::URLRequest* request); | |
| 38 | 47 |
| 39 ~ClearSiteDataThrottle() override; | 48 ~ClearSiteDataThrottle() override; |
| 40 | 49 |
| 41 // NavigationThrottle implementation: | 50 // ResourceThrottle implementation: |
| 42 ThrottleCheckResult WillStartRequest() override; | 51 const char* GetNameForLogging() const override; |
| 43 ThrottleCheckResult WillRedirectRequest() override; | 52 void WillStartRequest(bool* defer) override; |
| 44 ThrottleCheckResult WillProcessResponse() override; | 53 void WillRedirectRequest(const net::RedirectInfo& redirect_info, |
| 54 bool* defer) override; | |
| 55 void WillProcessResponse(bool* defer) override; | |
| 45 | 56 |
| 46 private: | 57 private: |
| 47 friend class ClearSiteDataFuzzerTest; | 58 friend class ClearSiteDataFuzzerTest; |
| 48 friend class ClearSiteDataThrottleTest; | 59 friend class ClearSiteDataThrottleTest; |
| 49 FRIEND_TEST_ALL_PREFIXES(ClearSiteDataThrottleTest, ParseHeader); | 60 FRIEND_TEST_ALL_PREFIXES(ClearSiteDataThrottleTest, ParseHeader); |
| 50 FRIEND_TEST_ALL_PREFIXES(ClearSiteDataThrottleTest, InvalidHeader); | 61 FRIEND_TEST_ALL_PREFIXES(ClearSiteDataThrottleTest, InvalidHeader); |
| 51 | 62 |
| 52 explicit ClearSiteDataThrottle(NavigationHandle* navigation_handle); | 63 explicit ClearSiteDataThrottle(net::URLRequest* request); |
| 53 | 64 |
| 54 // Scans for the first occurrence of the 'Clear-Site-Data' header, calls | 65 // Scans for the first occurrence of the 'Clear-Site-Data' header, calls |
| 55 // ParseHeader() to parse it, and requests the actual data clearing. This is | 66 // ParseHeader() to parse it, and calls ClearSiteDataOnUIThread(). This is |
| 56 // the common logic of WillRedirectRequest() and WillProcessResponse(). | 67 // the common logic of WillRedirectRequest() and WillProcessResponse(). |
| 57 void HandleHeader(); | 68 void HandleHeader(); |
| 58 | 69 |
| 70 // Finds the BrowserContext associated with the request and requests | |
| 71 // the actual clearing of data for |origin|. The datatypes to be deleted | |
| 72 // are determined by |clear_cookies|, |clear_storage|, and |clear_cache|. | |
| 73 // |web_contents_getter| identifies the WebContents from which the request | |
| 74 // originated. Must be run on the UI thread. The |callback| will be executed | |
| 75 // on the IO thread. | |
| 76 static void ClearSiteDataOnUIThread( | |
| 77 const ResourceRequestInfo::WebContentsGetter& web_contents_getter, | |
| 78 url::Origin origin, | |
| 79 bool clear_cookies, | |
| 80 bool clear_storage, | |
| 81 bool clear_cache, | |
| 82 const base::Closure& callback); | |
|
mmenke
2016/09/27 18:24:02
Can we move these two static methods to an anonymo
msramek
2016/10/13 14:26:03
Done.
| |
| 83 | |
| 59 // Parses the value of the 'Clear-Site-Data' header and outputs whether | 84 // Parses the value of the 'Clear-Site-Data' header and outputs whether |
| 60 // the header requests to |clear_cookies|, |clear_storage|, and |clear_cache|. | 85 // the header requests to |clear_cookies|, |clear_storage|, and |clear_cache|. |
| 61 // The |messages| vector will be filled with messages to be output in the | 86 // The |messages| vector will be filled with messages to be output in the |
| 62 // console. Returns true if parsing was successful. | 87 // console. Returns true if parsing was successful. |
| 63 bool ParseHeader(const std::string& header, | 88 bool ParseHeader(const std::string& header, |
| 64 bool* clear_cookies, | 89 bool* clear_cookies, |
| 65 bool* clear_storage, | 90 bool* clear_storage, |
| 66 bool* clear_cache, | 91 bool* clear_cache, |
| 67 std::vector<ConsoleMessage>* messages); | 92 std::vector<ConsoleMessage>* messages); |
| 68 | 93 |
| 69 // Signals that a parsing and deletion task was finished. | 94 // Signals that a parsing and deletion task was finished. |
| 70 void TaskFinished(); | 95 void TaskFinished(); |
| 71 | 96 |
| 72 // Cached console messages to be output when the RenderFrameHost is ready. | 97 // Outputs |messages| to the console of WebContents retrieved from |
| 98 // |web_contents_getter|. Must be run on the UI thread. | |
| 99 static void OutputConsoleMessagesOnUIThread( | |
| 100 const ResourceRequestInfo::WebContentsGetter& web_contents_getter, | |
| 101 const std::vector<ConsoleMessage>& messages); | |
| 102 | |
| 103 // The request this throttle is observing. | |
| 104 net::URLRequest* request_; | |
| 105 | |
| 106 // Cached console messages to be output when the resource is loaded. | |
| 73 std::vector<ConsoleMessage> messages_; | 107 std::vector<ConsoleMessage> messages_; |
| 74 GURL current_url_; | 108 GURL current_url_; |
| 75 | 109 |
| 76 // Whether we are currently waiting for a callback that data clearing has | 110 // Whether we are currently waiting for a callback that data clearing has |
| 77 // been completed; | 111 // been completed; |
| 78 bool clearing_in_progress_; | 112 bool clearing_in_progress_; |
| 79 | 113 |
| 80 // The time when the last clearing operation started. Used when clearing | 114 // The time when the last clearing operation started. Used when clearing |
| 81 // finishes to compute the duration. | 115 // finishes to compute the duration. |
| 82 base::TimeTicks clearing_started_; | 116 base::TimeTicks clearing_started_; |
| 83 | 117 |
| 84 // Needed for asynchronous parsing and deletion tasks. | 118 // Needed for asynchronous parsing and deletion tasks. |
| 85 base::WeakPtrFactory<ClearSiteDataThrottle> weak_ptr_factory_; | 119 base::WeakPtrFactory<ClearSiteDataThrottle> weak_ptr_factory_; |
| 86 | 120 |
| 87 DISALLOW_COPY_AND_ASSIGN(ClearSiteDataThrottle); | 121 DISALLOW_COPY_AND_ASSIGN(ClearSiteDataThrottle); |
| 88 }; | 122 }; |
| 89 | 123 |
| 90 } // namespace content | 124 } // namespace content |
| 91 | 125 |
| 92 #endif // CONTENT_BROWSER_BROWSING_DATA_CLEAR_SITE_DATA_THROTTLE_H_ | 126 #endif // CONTENT_BROWSER_BROWSING_DATA_CLEAR_SITE_DATA_THROTTLE_H_ |
| OLD | NEW |