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 <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 |request|. The caller must |
| 37 NavigationHandle* handle); | 69 // guarantee that |request| outlives the throttle. |
| 70 static std::unique_ptr<ResourceThrottle> CreateThrottleForRequest( | |
|
mmenke
2017/05/22 19:36:08
MaybeCreateThrottleForRequest?
msramek
2017/05/24 22:59:52
Done.
| |
| 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; |
| 46 | |
| 47 private: | |
| 48 friend class ClearSiteDataFuzzerTest; | |
| 49 friend class ClearSiteDataThrottleTest; | |
| 50 FRIEND_TEST_ALL_PREFIXES(ClearSiteDataThrottleTest, ParseHeader); | |
| 51 FRIEND_TEST_ALL_PREFIXES(ClearSiteDataThrottleTest, InvalidHeader); | |
| 52 | |
| 53 explicit ClearSiteDataThrottle(NavigationHandle* navigation_handle); | |
| 54 | |
| 55 // 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 | |
| 57 // the common logic of WillRedirectRequest() and WillProcessResponse(). | |
| 58 void HandleHeader(); | |
| 59 | 80 |
| 60 // Parses the value of the 'Clear-Site-Data' header and outputs whether | 81 // Parses the value of the 'Clear-Site-Data' header and outputs whether |
| 61 // the header requests to |clear_cookies|, |clear_storage|, and |clear_cache|. | 82 // 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 | 83 // The |delegate| will be filled with messages to be output in the console, |
| 63 // console. Returns true if parsing was successful. | 84 // prepended by the |current_url|. Returns true if parsing was successful. |
| 64 bool ParseHeader(const std::string& header, | 85 static bool ParseHeader(const std::string& header, |
| 65 bool* clear_cookies, | 86 bool* clear_cookies, |
| 66 bool* clear_storage, | 87 bool* clear_storage, |
| 67 bool* clear_cache, | 88 bool* clear_cache, |
| 68 std::vector<ConsoleMessage>* messages); | 89 ConsoleMessagesDelegate* delegate, |
| 90 const GURL& current_url); | |
|
mmenke
2017/05/22 18:35:35
I'd suggest making this a public *ForTest / ForTes
msramek
2017/05/24 22:59:52
Done. (Although I'd say that since the method is s
| |
| 91 | |
| 92 protected: | |
| 93 ClearSiteDataThrottle(net::URLRequest* request, | |
| 94 std::unique_ptr<ConsoleMessagesDelegate> delegate); | |
| 95 | |
| 96 // Returns HTTP response headers of the underlying URLRequest. | |
| 97 // Can be overriden for testing. | |
| 98 virtual const net::HttpResponseHeaders* GetResponseHeaders() const; | |
| 99 | |
| 100 // Executes the clearing task. Can be overriden for testing. | |
| 101 virtual void ExecuteClearingTask(const url::Origin& origin, | |
| 102 bool clear_cookies, | |
| 103 bool clear_storage, | |
| 104 bool clear_cache, | |
| 105 const base::Closure& callback); | |
|
mmenke
2017/05/22 18:35:35
These can both be private (private virtual methods
msramek
2017/05/24 22:59:52
Done.
| |
| 106 | |
| 107 private: | |
| 108 // Scans for the first occurrence of the 'Clear-Site-Data' header, calls | |
| 109 // ParseHeader() to parse it, and then ExecuteClearingTask() if applicable. | |
| 110 // This is the common logic of WillRedirectRequest() | |
| 111 // and WillProcessResponse(). Returns true if a valid header was found and | |
| 112 // the clearing was executed. | |
| 113 bool HandleHeader(); | |
| 69 | 114 |
| 70 // Signals that a parsing and deletion task was finished. | 115 // Signals that a parsing and deletion task was finished. |
| 71 void TaskFinished(); | 116 void TaskFinished(); |
| 72 | 117 |
| 73 // Cached console messages to be output when the RenderFrameHost is ready. | 118 // The request this throttle is observing. |
| 74 std::vector<ConsoleMessage> messages_; | 119 net::URLRequest* request_; |
| 75 GURL current_url_; | |
| 76 | 120 |
| 77 // Whether we are currently waiting for a callback that data clearing has | 121 // The delegate that stores and outputs console messages. |
| 78 // been completed; | 122 std::unique_ptr<ConsoleMessagesDelegate> delegate_; |
| 79 bool clearing_in_progress_; | |
| 80 | 123 |
| 81 // The time when the last clearing operation started. Used when clearing | 124 // The time when the last clearing operation started. Used when clearing |
| 82 // finishes to compute the duration. | 125 // finishes to compute the duration. |
| 83 base::TimeTicks clearing_started_; | 126 base::TimeTicks clearing_started_; |
| 84 | 127 |
| 85 // Needed for asynchronous parsing and deletion tasks. | 128 // Needed for asynchronous parsing and deletion tasks. |
| 86 base::WeakPtrFactory<ClearSiteDataThrottle> weak_ptr_factory_; | 129 base::WeakPtrFactory<ClearSiteDataThrottle> weak_ptr_factory_; |
| 87 | 130 |
| 88 DISALLOW_COPY_AND_ASSIGN(ClearSiteDataThrottle); | 131 DISALLOW_COPY_AND_ASSIGN(ClearSiteDataThrottle); |
| 89 }; | 132 }; |
| 90 | 133 |
| 91 } // namespace content | 134 } // namespace content |
| 92 | 135 |
| 93 #endif // CONTENT_BROWSER_BROWSING_DATA_CLEAR_SITE_DATA_THROTTLE_H_ | 136 #endif // CONTENT_BROWSER_BROWSING_DATA_CLEAR_SITE_DATA_THROTTLE_H_ |
| OLD | NEW |