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 #include "content/browser/browsing_data/clear_site_data_throttle.h" | 5 #include "content/browser/browsing_data/clear_site_data_throttle.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/json/json_string_value_serializer.h" | 9 #include "base/json/json_string_value_serializer.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "content/browser/frame_host/navigation_handle_impl.h" | |
| 16 #include "content/public/browser/browser_context.h" | 15 #include "content/public/browser/browser_context.h" |
| 16 #include "content/public/browser/browser_thread.h" | |
| 17 #include "content/public/browser/content_browser_client.h" | 17 #include "content/public/browser/content_browser_client.h" |
| 18 #include "content/public/browser/navigation_handle.h" | 18 #include "content/public/browser/render_frame_host.h" |
| 19 #include "content/public/browser/resource_controller.h" | |
| 19 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
| 20 #include "content/public/common/content_client.h" | 21 #include "content/public/common/content_client.h" |
| 21 #include "content/public/common/content_switches.h" | 22 #include "content/public/common/content_switches.h" |
| 22 #include "content/public/common/origin_util.h" | 23 #include "content/public/common/origin_util.h" |
| 24 #include "net/base/load_flags.h" | |
| 23 #include "net/http/http_response_headers.h" | 25 #include "net/http/http_response_headers.h" |
| 26 #include "net/url_request/redirect_info.h" | |
| 24 #include "url/gurl.h" | 27 #include "url/gurl.h" |
| 25 #include "url/origin.h" | 28 #include "url/origin.h" |
| 26 | 29 |
| 27 namespace content { | 30 namespace content { |
| 28 | 31 |
| 29 namespace { | 32 namespace { |
| 30 | 33 |
| 34 static const char* kNameForLogging = "ClearSiteDataThrottle"; | |
| 35 | |
| 31 static const char* kClearSiteDataHeader = "Clear-Site-Data"; | 36 static const char* kClearSiteDataHeader = "Clear-Site-Data"; |
| 32 | 37 |
| 33 static const char* kTypesKey = "types"; | 38 static const char* kTypesKey = "types"; |
| 34 | 39 |
| 35 // Pretty-printed log output. | 40 // Pretty-printed log output. |
| 36 static const char* kConsoleMessagePrefix = "Clear-Site-Data header on '%s': %s"; | 41 static const char* kConsoleMessagePrefix = "Clear-Site-Data header on '%s': %s"; |
| 37 static const char* kClearingOneType = "Clearing %s."; | 42 static const char* kClearingOneType = "Clearing %s."; |
| 38 static const char* kClearingTwoTypes = "Clearing %s and %s."; | 43 static const char* kClearingTwoTypes = "Clearing %s and %s."; |
| 39 static const char* kClearingThreeTypes = "Clearing %s, %s, and %s."; | 44 static const char* kClearingThreeTypes = "Clearing %s, %s, and %s."; |
| 40 | 45 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 51 switches::kEnableExperimentalWebPlatformFeatures); | 56 switches::kEnableExperimentalWebPlatformFeatures); |
| 52 } | 57 } |
| 53 | 58 |
| 54 // Represents the parameters as a single number to be recorded in a histogram. | 59 // Represents the parameters as a single number to be recorded in a histogram. |
| 55 int ParametersMask(bool clear_cookies, bool clear_storage, bool clear_cache) { | 60 int ParametersMask(bool clear_cookies, bool clear_storage, bool clear_cache) { |
| 56 return static_cast<int>(clear_cookies) * (1 << 0) + | 61 return static_cast<int>(clear_cookies) * (1 << 0) + |
| 57 static_cast<int>(clear_storage) * (1 << 1) + | 62 static_cast<int>(clear_storage) * (1 << 1) + |
| 58 static_cast<int>(clear_cache) * (1 << 2); | 63 static_cast<int>(clear_cache) * (1 << 2); |
| 59 } | 64 } |
| 60 | 65 |
| 66 // A helper function to pass an IO thread callback to a method called on | |
| 67 // the UI thread. | |
| 68 void JumpFromUIToIOThread(const base::Closure& callback) { | |
| 69 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 70 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, callback); | |
| 71 } | |
| 72 | |
| 61 } // namespace | 73 } // namespace |
| 62 | 74 |
| 63 // static | 75 // static |
| 64 std::unique_ptr<NavigationThrottle> | 76 std::unique_ptr<ResourceThrottle> |
| 65 ClearSiteDataThrottle::CreateThrottleForNavigation(NavigationHandle* handle) { | 77 ClearSiteDataThrottle::CreateThrottleForRequest(net::URLRequest* request) { |
| 66 if (AreExperimentalFeaturesEnabled()) | 78 // This is an experimental feature. |
| 67 return base::WrapUnique(new ClearSiteDataThrottle(handle)); | 79 if (!AreExperimentalFeaturesEnabled()) |
| 80 return std::unique_ptr<ResourceThrottle>(); | |
| 68 | 81 |
| 69 return std::unique_ptr<NavigationThrottle>(); | 82 // The LOAD_DO_NOT_SAVE_COOKIES flag prohibits the request from doing any |
| 83 // modification to cookies. | |
| 84 std::unique_ptr<base::Value> value = request->GetStateAsValue(); | |
| 85 const base::DictionaryValue* state; | |
| 86 DCHECK(value->GetAsDictionary(&state)); | |
| 87 int load_flags = 0; | |
| 88 bool success = | |
| 89 state->GetIntegerWithoutPathExpansion("load_flags", &load_flags); | |
| 90 DCHECK(success); | |
| 91 if (load_flags & net::LOAD_DO_NOT_SAVE_COOKIES) | |
|
mmenke
2016/09/26 12:59:16
Using this load flag isn't reliable - NetworkDeleg
msramek
2016/10/13 14:26:03
Hmm... So I'm really not an expert on these things
mmenke
2016/10/13 17:16:31
I'll defer to you guys here.
| |
| 92 return std::unique_ptr<ResourceThrottle>(); | |
| 93 | |
| 94 // The throttle has no purpose if the request has no ResourceRequestInfo, | |
| 95 // because we won't be able to determine whose data should be deleted. | |
| 96 if (!ResourceRequestInfo::ForRequest(request)) | |
| 97 return std::unique_ptr<ResourceThrottle>(); | |
| 98 | |
| 99 // Service workers can handle fetches of third-party resources and inject | |
| 100 // arbitrary headers. Supporting Clear-Site-Data would give them the power | |
| 101 // to delete data from any website. | |
| 102 if (ResourceRequestInfo::OriginatedFromServiceWorker(request)) | |
|
mmenke
2016/09/26 12:59:17
This doesn't seem right. We don't care if the req
msramek
2016/10/13 14:26:02
Thanks for the explanation.
From the name of the
| |
| 103 return std::unique_ptr<ResourceThrottle>(); | |
| 104 | |
| 105 return base::WrapUnique(new ClearSiteDataThrottle(request)); | |
| 70 } | 106 } |
| 71 | 107 |
| 72 ClearSiteDataThrottle::ClearSiteDataThrottle( | 108 ClearSiteDataThrottle::ClearSiteDataThrottle(net::URLRequest* request) |
| 73 NavigationHandle* navigation_handle) | 109 : request_(request), |
| 74 : NavigationThrottle(navigation_handle), | |
| 75 clearing_in_progress_(false), | 110 clearing_in_progress_(false), |
| 76 weak_ptr_factory_(this) {} | 111 weak_ptr_factory_(this) {} |
| 77 | 112 |
| 78 ClearSiteDataThrottle::~ClearSiteDataThrottle() { | 113 ClearSiteDataThrottle::~ClearSiteDataThrottle() { |
| 79 // At the end of the navigation we finally have access to the correct | 114 // Output the cached console messages. Prefix each sequence of messages |
| 80 // RenderFrameHost. Output the cached console messages. Prefix each sequence | 115 // belonging to the same URL with |kConsoleMessagePrefix|. We output console |
| 81 // of messages belonging to the same URL with |kConsoleMessagePrefix|. | 116 // messages when the request is finished rather than in real time, since in |
| 82 GURL last_seen_url; | 117 // case of navigations swapping RenderFrameHost would cause the outputs to |
| 83 for (const ConsoleMessage& message : messages_) { | 118 // disappear. |
| 84 if (message.url == last_seen_url) { | 119 if (messages_.empty()) |
| 85 navigation_handle()->GetRenderFrameHost()->AddMessageToConsole( | 120 return; |
| 86 message.level, message.text); | |
| 87 } else { | |
| 88 navigation_handle()->GetRenderFrameHost()->AddMessageToConsole( | |
| 89 message.level, | |
| 90 base::StringPrintf(kConsoleMessagePrefix, message.url.spec().c_str(), | |
| 91 message.text.c_str())); | |
| 92 } | |
| 93 | 121 |
| 94 last_seen_url = message.url; | 122 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 95 } | 123 BrowserThread::PostTask( |
| 124 BrowserThread::UI, FROM_HERE, | |
| 125 base::Bind(&ClearSiteDataThrottle::OutputConsoleMessagesOnUIThread, | |
| 126 ResourceRequestInfo::ForRequest(request_) | |
| 127 ->GetWebContentsGetterForRequest(), | |
| 128 std::move(messages_))); | |
| 96 } | 129 } |
| 97 | 130 |
| 98 ClearSiteDataThrottle::ThrottleCheckResult | 131 void ClearSiteDataThrottle::WillStartRequest(bool* defer) { |
| 99 ClearSiteDataThrottle::WillStartRequest() { | 132 current_url_ = request_->original_url(); |
| 100 current_url_ = navigation_handle()->GetURL(); | 133 *defer = false; |
| 101 return PROCEED; | |
| 102 } | 134 } |
| 103 | 135 |
| 104 ClearSiteDataThrottle::ThrottleCheckResult | 136 void ClearSiteDataThrottle::WillRedirectRequest( |
| 105 ClearSiteDataThrottle::WillRedirectRequest() { | 137 const net::RedirectInfo& redirect_info, |
| 138 bool* defer) { | |
| 106 // We are processing a redirect from url1 to url2. GetResponseHeaders() | 139 // We are processing a redirect from url1 to url2. GetResponseHeaders() |
| 107 // contains headers from url1, but GetURL() is already equal to url2. Handle | 140 // contains headers from url1, but GetURL() is already equal to url2. Handle |
| 108 // the headers before updating the URL, so that |current_url_| corresponds | 141 // the headers before updating the URL, so that |current_url_| corresponds |
| 109 // to the URL that sent the headers. | 142 // to the URL that sent the headers. |
| 110 HandleHeader(); | 143 HandleHeader(); |
| 111 current_url_ = navigation_handle()->GetURL(); | 144 current_url_ = redirect_info.new_url; |
| 112 | 145 *defer = clearing_in_progress_; |
| 113 return clearing_in_progress_ ? DEFER : PROCEED; | |
| 114 } | 146 } |
| 115 | 147 |
| 116 ClearSiteDataThrottle::ThrottleCheckResult | 148 void ClearSiteDataThrottle::WillProcessResponse(bool* defer) { |
| 117 ClearSiteDataThrottle::WillProcessResponse() { | |
| 118 HandleHeader(); | 149 HandleHeader(); |
| 119 return clearing_in_progress_ ? DEFER : PROCEED; | 150 *defer = clearing_in_progress_; |
| 151 } | |
| 152 | |
| 153 const char* ClearSiteDataThrottle::GetNameForLogging() const { | |
| 154 return kNameForLogging; | |
| 120 } | 155 } |
| 121 | 156 |
| 122 void ClearSiteDataThrottle::HandleHeader() { | 157 void ClearSiteDataThrottle::HandleHeader() { |
| 123 NavigationHandleImpl* handle = | 158 const net::HttpResponseHeaders* headers = request_->response_headers(); |
| 124 static_cast<NavigationHandleImpl*>(navigation_handle()); | |
| 125 const net::HttpResponseHeaders* headers = handle->GetResponseHeaders(); | |
| 126 | 159 |
| 127 if (!headers || !headers->HasHeader(kClearSiteDataHeader)) | 160 if (!headers || !headers->HasHeader(kClearSiteDataHeader)) |
| 128 return; | 161 return; |
|
mmenke
2016/09/27 18:24:02
Do we get here for WebSockets? I don't think we d
msramek
2016/10/13 14:26:03
I played with some WebSockets demos on the web and
mmenke
2016/10/13 17:16:31
Websockets have an entirely different API - they d
| |
| 129 | 162 |
| 130 // Only accept the header on secure origins. | 163 // Only accept the header on secure origins. |
| 131 if (!IsOriginSecure(current_url_)) { | 164 if (!IsOriginSecure(current_url_)) { |
|
mmenke
2016/09/27 18:24:02
Is this the right check? localhost is considered
msramek
2016/10/13 14:26:03
Localhost over HTTP is hopefully fine, and useful
| |
| 132 ConsoleLog(&messages_, current_url_, "Not supported for insecure origins.", | 165 ConsoleLog(&messages_, current_url_, "Not supported for insecure origins.", |
| 133 CONSOLE_MESSAGE_LEVEL_ERROR); | 166 CONSOLE_MESSAGE_LEVEL_ERROR); |
| 134 return; | 167 return; |
| 135 } | 168 } |
| 136 | 169 |
| 137 std::string header_value; | 170 std::string header_value; |
| 138 headers->GetNormalizedHeader(kClearSiteDataHeader, &header_value); | 171 headers->GetNormalizedHeader(kClearSiteDataHeader, &header_value); |
| 139 | 172 |
| 140 bool clear_cookies; | 173 bool clear_cookies; |
| 141 bool clear_storage; | 174 bool clear_storage; |
| 142 bool clear_cache; | 175 bool clear_cache; |
| 143 | 176 |
| 144 if (!ParseHeader(header_value, &clear_cookies, &clear_storage, &clear_cache, | 177 if (!ParseHeader(header_value, &clear_cookies, &clear_storage, &clear_cache, |
| 145 &messages_)) { | 178 &messages_)) { |
| 146 return; | 179 return; |
| 147 } | 180 } |
| 148 | 181 |
| 149 // Record the call parameters. | 182 // Record the call parameters. |
| 150 UMA_HISTOGRAM_ENUMERATION( | 183 UMA_HISTOGRAM_ENUMERATION( |
| 151 "Navigation.ClearSiteData.Parameters", | 184 "Navigation.ClearSiteData.Parameters", |
| 152 ParametersMask(clear_cookies, clear_storage, clear_cache), (1 << 3)); | 185 ParametersMask(clear_cookies, clear_storage, clear_cache), (1 << 3)); |
| 153 | 186 |
| 154 // If the header is valid, clear the data for this browser context and origin. | 187 // If the header is valid, clear the data for this browser context and origin. |
| 155 BrowserContext* browser_context = | |
| 156 navigation_handle()->GetWebContents()->GetBrowserContext(); | |
| 157 url::Origin origin(current_url_); | 188 url::Origin origin(current_url_); |
| 158 | |
| 159 if (origin.unique()) { | 189 if (origin.unique()) { |
| 160 ConsoleLog(&messages_, current_url_, "Not supported for unique origins.", | 190 ConsoleLog(&messages_, current_url_, "Not supported for unique origins.", |
| 161 CONSOLE_MESSAGE_LEVEL_ERROR); | 191 CONSOLE_MESSAGE_LEVEL_ERROR); |
| 162 return; | 192 return; |
| 163 } | 193 } |
| 164 | 194 |
| 165 clearing_in_progress_ = true; | 195 clearing_in_progress_ = true; |
| 166 clearing_started_ = base::TimeTicks::Now(); | 196 clearing_started_ = base::TimeTicks::Now(); |
| 197 | |
| 198 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 199 BrowserThread::PostTask( | |
| 200 BrowserThread::UI, FROM_HERE, | |
| 201 base::Bind(&ClearSiteDataThrottle::ClearSiteDataOnUIThread, | |
| 202 ResourceRequestInfo::ForRequest(request_) | |
| 203 ->GetWebContentsGetterForRequest(), | |
| 204 origin, clear_cookies, clear_storage, clear_cache, | |
| 205 base::Bind(&ClearSiteDataThrottle::TaskFinished, | |
| 206 weak_ptr_factory_.GetWeakPtr()))); | |
| 207 } | |
| 208 | |
| 209 // static | |
| 210 void ClearSiteDataThrottle::ClearSiteDataOnUIThread( | |
| 211 const ResourceRequestInfo::WebContentsGetter& web_contents_getter, | |
| 212 url::Origin origin, | |
| 213 bool clear_cookies, | |
| 214 bool clear_storage, | |
| 215 bool clear_cache, | |
| 216 const base::Closure& callback) { | |
| 217 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 218 | |
| 219 WebContents* web_contents = web_contents_getter.Run(); | |
| 220 if (!web_contents) | |
| 221 return; | |
| 222 | |
| 223 BrowserContext* browser_context = web_contents->GetBrowserContext(); | |
| 224 | |
| 167 GetContentClient()->browser()->ClearSiteData( | 225 GetContentClient()->browser()->ClearSiteData( |
| 168 browser_context, origin, clear_cookies, clear_storage, clear_cache, | 226 browser_context, origin, clear_cookies, clear_storage, clear_cache, |
| 169 base::Bind(&ClearSiteDataThrottle::TaskFinished, | 227 base::Bind(&JumpFromUIToIOThread, callback)); |
| 170 weak_ptr_factory_.GetWeakPtr())); | |
| 171 } | 228 } |
| 172 | 229 |
| 173 bool ClearSiteDataThrottle::ParseHeader(const std::string& header, | 230 bool ClearSiteDataThrottle::ParseHeader(const std::string& header, |
| 174 bool* clear_cookies, | 231 bool* clear_cookies, |
| 175 bool* clear_storage, | 232 bool* clear_storage, |
| 176 bool* clear_cache, | 233 bool* clear_cache, |
| 177 std::vector<ConsoleMessage>* messages) { | 234 std::vector<ConsoleMessage>* messages) { |
| 178 if (!base::IsStringASCII(header)) { | 235 if (!base::IsStringASCII(header)) { |
| 179 ConsoleLog(messages, current_url_, "Must only contain ASCII characters.", | 236 ConsoleLog(messages, current_url_, "Must only contain ASCII characters.", |
| 180 CONSOLE_MESSAGE_LEVEL_ERROR); | 237 CONSOLE_MESSAGE_LEVEL_ERROR); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 261 break; | 318 break; |
| 262 default: | 319 default: |
| 263 NOTREACHED(); | 320 NOTREACHED(); |
| 264 } | 321 } |
| 265 ConsoleLog(messages, current_url_, output, CONSOLE_MESSAGE_LEVEL_LOG); | 322 ConsoleLog(messages, current_url_, output, CONSOLE_MESSAGE_LEVEL_LOG); |
| 266 | 323 |
| 267 return true; | 324 return true; |
| 268 } | 325 } |
| 269 | 326 |
| 270 void ClearSiteDataThrottle::TaskFinished() { | 327 void ClearSiteDataThrottle::TaskFinished() { |
| 328 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 271 DCHECK(clearing_in_progress_); | 329 DCHECK(clearing_in_progress_); |
| 272 clearing_in_progress_ = false; | 330 clearing_in_progress_ = false; |
| 273 | 331 |
| 274 UMA_HISTOGRAM_CUSTOM_TIMES("Navigation.ClearSiteData.Duration", | 332 UMA_HISTOGRAM_CUSTOM_TIMES("Navigation.ClearSiteData.Duration", |
| 275 base::TimeTicks::Now() - clearing_started_, | 333 base::TimeTicks::Now() - clearing_started_, |
| 276 base::TimeDelta::FromMilliseconds(1), | 334 base::TimeDelta::FromMilliseconds(1), |
| 277 base::TimeDelta::FromSeconds(1), 50); | 335 base::TimeDelta::FromSeconds(1), 50); |
| 278 | 336 |
| 279 navigation_handle()->Resume(); | 337 controller()->Resume(); |
| 338 } | |
| 339 | |
| 340 // static | |
| 341 void ClearSiteDataThrottle::OutputConsoleMessagesOnUIThread( | |
| 342 const ResourceRequestInfo::WebContentsGetter& web_contents_getter, | |
| 343 const std::vector<ConsoleMessage>& messages) { | |
| 344 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 345 | |
| 346 WebContents* web_contents = web_contents_getter.Run(); | |
| 347 if (!web_contents) | |
| 348 return; | |
| 349 | |
| 350 GURL last_seen_url; | |
| 351 for (const ConsoleMessage& message : messages) { | |
| 352 if (message.url == last_seen_url) { | |
| 353 web_contents->GetMainFrame()->AddMessageToConsole(message.level, | |
| 354 message.text); | |
| 355 } else { | |
| 356 web_contents->GetMainFrame()->AddMessageToConsole( | |
| 357 message.level, | |
| 358 base::StringPrintf(kConsoleMessagePrefix, message.url.spec().c_str(), | |
| 359 message.text.c_str())); | |
| 360 } | |
| 361 | |
| 362 last_seen_url = message.url; | |
| 363 } | |
| 280 } | 364 } |
| 281 | 365 |
| 282 } // namespace content | 366 } // namespace content |
| OLD | NEW |