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