1 // Copyright 2018 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CONTENT_BROWSER_BROWSING_DATA_CLEAR_SITE_DATA_HANDLER_H_ 6 #define CONTENT_BROWSER_BROWSING_DATA_CLEAR_SITE_DATA_HANDLER_H_ 7 8 #include <memory> 9 #include <string> 10 #include <vector> 11 12 #include "base/callback.h" 13 #include "base/macros.h" 14 #include "base/time/time.h" 15 #include "content/common/content_export.h" 16 #include "third_party/blink/public/mojom/devtools/console_message.mojom.h" 17 #include "url/gurl.h" 18 #include "url/origin.h" 19 20 namespace content { 21 22 class WebContents; 23 class BrowserContext; 24 25 // This handler parses the Clear-Site-Data header and executes the clearing 26 // of browsing data. The resource load is delayed until the header is parsed 27 // and, if valid, until the browsing data are deleted. See the W3C working draft 28 // at https://w3c.github.io/webappsec-clear-site-data/. 29 class CONTENT_EXPORT ClearSiteDataHandler { 30 public: 31 // Stores and outputs console messages. 32 class CONTENT_EXPORT ConsoleMessagesDelegate { 33 public: 34 struct Message { 35 GURL url; 36 std::string text; 37 blink::mojom::ConsoleMessageLevel level; 38 }; 39 40 using OutputFormattedMessageFunction = 41 base::RepeatingCallback<void(WebContents*, 42 blink::mojom::ConsoleMessageLevel, 43 const std::string&)>; 44 45 ConsoleMessagesDelegate(); 46 virtual ~ConsoleMessagesDelegate(); 47 48 // Logs a |text| message from |url| with |level|. 49 virtual void AddMessage(const GURL& url, 50 const std::string& text, 51 blink::mojom::ConsoleMessageLevel level); 52 53 // Outputs stored messages to the console of WebContents identified by 54 // |web_contents_getter|. 55 virtual void OutputMessages( 56 const base::RepeatingCallback<WebContents*()>& web_contents_getter); 57 GetMessagesForTesting()58 const std::vector<Message>& GetMessagesForTesting() const { 59 return messages_; 60 } 61 62 protected: 63 void SetOutputFormattedMessageFunctionForTesting( 64 const OutputFormattedMessageFunction& function); 65 66 private: 67 std::vector<Message> messages_; 68 OutputFormattedMessageFunction output_formatted_message_function_; 69 }; 70 71 // |header_value| is the string value of the 'Clear-Site-Data' header. This 72 // method calls ParseHeader() to parse it, and then ExecuteClearingTask() if 73 // applicable. 74 static void HandleHeader( 75 base::RepeatingCallback<BrowserContext*()> browser_context_getter, 76 base::RepeatingCallback<WebContents*()> web_contents_getter, 77 const GURL& url, 78 const std::string& header_value, 79 int load_flags, 80 base::OnceClosure callback); 81 82 // Exposes ParseHeader() publicly for testing. 83 static bool ParseHeaderForTesting(const std::string& header, 84 bool* clear_cookies, 85 bool* clear_storage, 86 bool* clear_cache, 87 ConsoleMessagesDelegate* delegate, 88 const GURL& current_url); 89 90 protected: 91 ClearSiteDataHandler( 92 base::RepeatingCallback<BrowserContext*()> browser_context_getter, 93 base::RepeatingCallback<WebContents*()> web_contents_getter, 94 const GURL& url, 95 const std::string& header_value, 96 int load_flags, 97 base::OnceClosure callback, 98 std::unique_ptr<ConsoleMessagesDelegate> delegate); 99 virtual ~ClearSiteDataHandler(); 100 101 // Calls |HandleHeaderImpl| to handle headers, and output console message if 102 // not deferred. Returns |true| if the request was deferred. 103 bool HandleHeaderAndOutputConsoleMessages(); 104 105 // Handles headers and maybe execute clearing task. Returns |true| if the 106 // request was deferred. 107 bool Run(); 108 109 // Parses the value of the 'Clear-Site-Data' header and outputs whether 110 // the header requests to |clear_cookies|, |clear_storage|, and |clear_cache|. 111 // The |delegate| will be filled with messages to be output in the console, 112 // prepended by the |current_url|. Returns true if parsing was successful. 113 static bool ParseHeader(const std::string& header, 114 bool* clear_cookies, 115 bool* clear_storage, 116 bool* clear_cache, 117 ConsoleMessagesDelegate* delegate, 118 const GURL& current_url); 119 120 // Executes the clearing task. Can be overridden for testing. 121 virtual void ExecuteClearingTask(const url::Origin& origin, 122 bool clear_cookies, 123 bool clear_storage, 124 bool clear_cache, 125 base::OnceClosure callback); 126 127 // Signals that a parsing and deletion task was finished. 128 // |clearing_started| is the time when the last clearing operation started. 129 // Used when clearing finishes to compute the duration. 130 static void TaskFinished( 131 base::TimeTicks clearing_started, 132 std::unique_ptr<ConsoleMessagesDelegate> delegate, 133 base::RepeatingCallback<WebContents*()> web_contents_getter, 134 base::OnceClosure callback); 135 136 // Outputs the console messages in the |delegate_|. 137 void OutputConsoleMessages(); 138 139 // Run the callback to resume loading. No clearing actions were conducted. 140 void RunCallbackNotDeferred(); 141 142 const GURL& GetURLForTesting(); 143 144 private: 145 // Required to clear the data. 146 base::RepeatingCallback<BrowserContext*()> browser_context_getter_; 147 base::RepeatingCallback<WebContents*()> web_contents_getter_; 148 149 // Target URL whose data will be cleared. 150 GURL url_; 151 152 // Raw string value of the 'Clear-Site-Data' header. 153 std::string header_value_; 154 155 // Load flags of the current request, used to check cookie policies. 156 int load_flags_; 157 158 // Used to notify that the clearing has completed. Callers could resuming 159 // loading after this point. 160 base::OnceClosure callback_; 161 162 // The delegate that stores and outputs console messages. 163 std::unique_ptr<ConsoleMessagesDelegate> delegate_; 164 165 DISALLOW_COPY_AND_ASSIGN(ClearSiteDataHandler); 166 }; 167 168 } // namespace content 169 170 #endif // CONTENT_BROWSER_BROWSING_DATA_CLEAR_SITE_DATA_HANDLER_H_ 171