| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/renderer_host/safe_browsing_resource_throttle.h" | 5 #include "chrome/browser/renderer_host/safe_browsing_resource_throttle.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| 11 #include "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/prerender/prerender_contents.h" | 14 #include "chrome/browser/prerender/prerender_contents.h" |
| 15 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 15 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 16 #include "components/safe_browsing_db/util.h" | 16 #include "components/safe_browsing_db/util.h" |
| 17 #include "components/subresource_filter/content/browser/content_subresource_filt
er_driver_factory.h" | 17 #include "components/subresource_filter/content/browser/content_subresource_filt
er_driver_factory.h" |
| 18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/render_frame_host.h" | 19 #include "content/public/browser/render_frame_host.h" |
| 20 #include "content/public/browser/resource_controller.h" | 20 #include "content/public/browser/resource_controller.h" |
| 21 #include "content/public/browser/resource_request_info.h" | 21 #include "content/public/browser/resource_request_info.h" |
| 22 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
| 23 #include "net/base/load_flags.h" | 23 #include "net/base/load_flags.h" |
| 24 #include "net/log/net_log.h" | 24 #include "net/log/net_log.h" |
| 25 #include "net/log/net_log_source_type.h" |
| 25 #include "net/url_request/redirect_info.h" | 26 #include "net/url_request/redirect_info.h" |
| 26 #include "net/url_request/url_request.h" | 27 #include "net/url_request/url_request.h" |
| 27 | 28 |
| 28 using net::NetLog; | 29 using net::NetLogEventType; |
| 30 using net::NetLogSourceType; |
| 29 using safe_browsing::SafeBrowsingUIManager; | 31 using safe_browsing::SafeBrowsingUIManager; |
| 30 | 32 |
| 31 namespace { | 33 namespace { |
| 32 | 34 |
| 33 // Maximum time in milliseconds to wait for the safe browsing service to | 35 // Maximum time in milliseconds to wait for the safe browsing service to |
| 34 // verify a URL. After this amount of time the outstanding check will be | 36 // verify a URL. After this amount of time the outstanding check will be |
| 35 // aborted, and the URL will be treated as if it were safe. | 37 // aborted, and the URL will be treated as if it were safe. |
| 36 const int kCheckUrlTimeoutMs = 5000; | 38 const int kCheckUrlTimeoutMs = 5000; |
| 37 | 39 |
| 38 // Return a dictionary with "url"=|url-spec| and optionally | 40 // Return a dictionary with "url"=|url-spec| and optionally |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 content::ResourceType resource_type, | 87 content::ResourceType resource_type, |
| 86 safe_browsing::SafeBrowsingService* sb_service) | 88 safe_browsing::SafeBrowsingService* sb_service) |
| 87 : state_(STATE_NONE), | 89 : state_(STATE_NONE), |
| 88 defer_state_(DEFERRED_NONE), | 90 defer_state_(DEFERRED_NONE), |
| 89 threat_type_(safe_browsing::SB_THREAT_TYPE_SAFE), | 91 threat_type_(safe_browsing::SB_THREAT_TYPE_SAFE), |
| 90 database_manager_(sb_service->database_manager()), | 92 database_manager_(sb_service->database_manager()), |
| 91 ui_manager_(sb_service->ui_manager()), | 93 ui_manager_(sb_service->ui_manager()), |
| 92 request_(request), | 94 request_(request), |
| 93 resource_type_(resource_type), | 95 resource_type_(resource_type), |
| 94 bound_net_log_(net::BoundNetLog::Make(request->net_log().net_log(), | 96 bound_net_log_(net::BoundNetLog::Make(request->net_log().net_log(), |
| 95 NetLog::SOURCE_SAFE_BROWSING)) {} | 97 NetLogSourceType::SAFE_BROWSING)) {} |
| 96 | 98 |
| 97 SafeBrowsingResourceThrottle::~SafeBrowsingResourceThrottle() { | 99 SafeBrowsingResourceThrottle::~SafeBrowsingResourceThrottle() { |
| 98 if (defer_state_ != DEFERRED_NONE) { | 100 if (defer_state_ != DEFERRED_NONE) { |
| 99 EndNetLogEvent(NetLog::TYPE_SAFE_BROWSING_DEFERRED, nullptr, nullptr); | 101 EndNetLogEvent(NetLogEventType::SAFE_BROWSING_DEFERRED, nullptr, nullptr); |
| 100 } | 102 } |
| 101 | 103 |
| 102 if (state_ == STATE_CHECKING_URL) { | 104 if (state_ == STATE_CHECKING_URL) { |
| 103 database_manager_->CancelCheck(this); | 105 database_manager_->CancelCheck(this); |
| 104 EndNetLogEvent(NetLog::TYPE_SAFE_BROWSING_CHECKING_URL, "result", | 106 EndNetLogEvent(NetLogEventType::SAFE_BROWSING_CHECKING_URL, "result", |
| 105 "request_canceled"); | 107 "request_canceled"); |
| 106 } | 108 } |
| 107 } | 109 } |
| 108 | 110 |
| 109 // Note on net_log calls: TYPE_SAFE_BROWSING_DEFERRED events must be wholly | 111 // Note on net_log calls: SAFE_BROWSING_DEFERRED events must be wholly |
| 110 // nested within TYPE_SAFE_BROWSING_CHECKING_URL events. Synchronous checks | 112 // nested within SAFE_BROWSING_CHECKING_URL events. Synchronous checks |
| 111 // are not logged at all. | 113 // are not logged at all. |
| 112 void SafeBrowsingResourceThrottle::BeginNetLogEvent(NetLog::EventType type, | 114 void SafeBrowsingResourceThrottle::BeginNetLogEvent(NetLogEventType type, |
| 113 const GURL& url, | 115 const GURL& url, |
| 114 const char* name, | 116 const char* name, |
| 115 const char* value) { | 117 const char* value) { |
| 116 bound_net_log_.BeginEvent( | 118 bound_net_log_.BeginEvent( |
| 117 type, base::Bind(&NetLogUrlCallback, request_, url, name, value)); | 119 type, base::Bind(&NetLogUrlCallback, request_, url, name, value)); |
| 118 request_->net_log().AddEvent( | 120 request_->net_log().AddEvent( |
| 119 type, bound_net_log_.source().ToEventParametersCallback()); | 121 type, bound_net_log_.source().ToEventParametersCallback()); |
| 120 } | 122 } |
| 121 | 123 |
| 122 void SafeBrowsingResourceThrottle::EndNetLogEvent(NetLog::EventType type, | 124 void SafeBrowsingResourceThrottle::EndNetLogEvent(NetLogEventType type, |
| 123 const char* name, | 125 const char* name, |
| 124 const char* value) { | 126 const char* value) { |
| 125 bound_net_log_.EndEvent( | 127 bound_net_log_.EndEvent( |
| 126 type, base::Bind(&NetLogStringCallback, name, value)); | 128 type, base::Bind(&NetLogStringCallback, name, value)); |
| 127 request_->net_log().AddEvent( | 129 request_->net_log().AddEvent( |
| 128 type, bound_net_log_.source().ToEventParametersCallback()); | 130 type, bound_net_log_.source().ToEventParametersCallback()); |
| 129 } | 131 } |
| 130 | 132 |
| 131 void SafeBrowsingResourceThrottle::WillStartRequest(bool* defer) { | 133 void SafeBrowsingResourceThrottle::WillStartRequest(bool* defer) { |
| 132 // We need to check the new URL before starting the request. | 134 // We need to check the new URL before starting the request. |
| 133 if (CheckUrl(request_->url())) | 135 if (CheckUrl(request_->url())) |
| 134 return; | 136 return; |
| 135 | 137 |
| 136 // We let the check run in parallel with resource load only if this | 138 // We let the check run in parallel with resource load only if this |
| 137 // db_manager only supports asynchronous checks, like on mobile. | 139 // db_manager only supports asynchronous checks, like on mobile. |
| 138 // Otherwise, we defer now. | 140 // Otherwise, we defer now. |
| 139 if (database_manager_->ChecksAreAlwaysAsync()) | 141 if (database_manager_->ChecksAreAlwaysAsync()) |
| 140 return; | 142 return; |
| 141 | 143 |
| 142 // If the URL couldn't be verified synchronously, defer starting the | 144 // If the URL couldn't be verified synchronously, defer starting the |
| 143 // request until the check has completed. | 145 // request until the check has completed. |
| 144 defer_state_ = DEFERRED_START; | 146 defer_state_ = DEFERRED_START; |
| 145 defer_start_time_ = base::TimeTicks::Now(); | 147 defer_start_time_ = base::TimeTicks::Now(); |
| 146 *defer = true; | 148 *defer = true; |
| 147 BeginNetLogEvent(NetLog::TYPE_SAFE_BROWSING_DEFERRED, request_->url(), | 149 BeginNetLogEvent(NetLogEventType::SAFE_BROWSING_DEFERRED, request_->url(), |
| 148 "defer_reason", "at_start"); | 150 "defer_reason", "at_start"); |
| 149 } | 151 } |
| 150 | 152 |
| 151 void SafeBrowsingResourceThrottle::WillProcessResponse(bool* defer) { | 153 void SafeBrowsingResourceThrottle::WillProcessResponse(bool* defer) { |
| 152 CHECK_EQ(defer_state_, DEFERRED_NONE); | 154 CHECK_EQ(defer_state_, DEFERRED_NONE); |
| 153 // TODO(nparker): Maybe remove this check, since it should have no effect. | 155 // TODO(nparker): Maybe remove this check, since it should have no effect. |
| 154 if (!database_manager_->ChecksAreAlwaysAsync()) | 156 if (!database_manager_->ChecksAreAlwaysAsync()) |
| 155 return; | 157 return; |
| 156 | 158 |
| 157 if (state_ == STATE_CHECKING_URL || | 159 if (state_ == STATE_CHECKING_URL || |
| 158 state_ == STATE_DISPLAYING_BLOCKING_PAGE) { | 160 state_ == STATE_DISPLAYING_BLOCKING_PAGE) { |
| 159 defer_state_ = DEFERRED_PROCESSING; | 161 defer_state_ = DEFERRED_PROCESSING; |
| 160 defer_start_time_ = base::TimeTicks::Now(); | 162 defer_start_time_ = base::TimeTicks::Now(); |
| 161 *defer = true; | 163 *defer = true; |
| 162 BeginNetLogEvent(NetLog::TYPE_SAFE_BROWSING_DEFERRED, request_->url(), | 164 BeginNetLogEvent(NetLogEventType::SAFE_BROWSING_DEFERRED, request_->url(), |
| 163 "defer_reason", "at_response"); | 165 "defer_reason", "at_response"); |
| 164 } | 166 } |
| 165 } | 167 } |
| 166 | 168 |
| 167 bool SafeBrowsingResourceThrottle::MustProcessResponseBeforeReadingBody() { | 169 bool SafeBrowsingResourceThrottle::MustProcessResponseBeforeReadingBody() { |
| 168 // On Android, SafeBrowsing may only decide to cancel the request when the | 170 // On Android, SafeBrowsing may only decide to cancel the request when the |
| 169 // response has been received. Therefore, no part of it should be cached | 171 // response has been received. Therefore, no part of it should be cached |
| 170 // until this ResourceThrottle has been able to check the response. This | 172 // until this ResourceThrottle has been able to check the response. This |
| 171 // prevents the following scenario: | 173 // prevents the following scenario: |
| 172 // 1) A request is made for foo.com which has been hacked. | 174 // 1) A request is made for foo.com which has been hacked. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 201 state_ == STATE_DISPLAYING_BLOCKING_PAGE); | 203 state_ == STATE_DISPLAYING_BLOCKING_PAGE); |
| 202 // We can't check this new URL until we have finished checking | 204 // We can't check this new URL until we have finished checking |
| 203 // the prev one, or resumed from the blocking page. | 205 // the prev one, or resumed from the blocking page. |
| 204 unchecked_redirect_url_ = redirect_info.new_url; | 206 unchecked_redirect_url_ = redirect_info.new_url; |
| 205 defer_state_ = DEFERRED_UNCHECKED_REDIRECT; | 207 defer_state_ = DEFERRED_UNCHECKED_REDIRECT; |
| 206 } | 208 } |
| 207 | 209 |
| 208 defer_start_time_ = base::TimeTicks::Now(); | 210 defer_start_time_ = base::TimeTicks::Now(); |
| 209 *defer = true; | 211 *defer = true; |
| 210 BeginNetLogEvent( | 212 BeginNetLogEvent( |
| 211 NetLog::TYPE_SAFE_BROWSING_DEFERRED, redirect_info.new_url, | 213 NetLogEventType::SAFE_BROWSING_DEFERRED, redirect_info.new_url, |
| 212 "defer_reason", | 214 "defer_reason", |
| 213 defer_state_ == DEFERRED_REDIRECT ? "redirect" : "unchecked_redirect"); | 215 defer_state_ == DEFERRED_REDIRECT ? "redirect" : "unchecked_redirect"); |
| 214 } | 216 } |
| 215 | 217 |
| 216 const char* SafeBrowsingResourceThrottle::GetNameForLogging() const { | 218 const char* SafeBrowsingResourceThrottle::GetNameForLogging() const { |
| 217 return "SafeBrowsingResourceThrottle"; | 219 return "SafeBrowsingResourceThrottle"; |
| 218 } | 220 } |
| 219 | 221 |
| 220 // SafeBrowsingService::Client implementation, called on the IO thread once | 222 // SafeBrowsingService::Client implementation, called on the IO thread once |
| 221 // the URL has been classified. | 223 // the URL has been classified. |
| 222 void SafeBrowsingResourceThrottle::OnCheckBrowseUrlResult( | 224 void SafeBrowsingResourceThrottle::OnCheckBrowseUrlResult( |
| 223 const GURL& url, | 225 const GURL& url, |
| 224 safe_browsing::SBThreatType threat_type, | 226 safe_browsing::SBThreatType threat_type, |
| 225 const safe_browsing::ThreatMetadata& metadata) { | 227 const safe_browsing::ThreatMetadata& metadata) { |
| 226 CHECK_EQ(state_, STATE_CHECKING_URL); | 228 CHECK_EQ(state_, STATE_CHECKING_URL); |
| 227 CHECK_EQ(url, url_being_checked_); | 229 CHECK_EQ(url, url_being_checked_); |
| 228 | 230 |
| 229 timer_.Stop(); // Cancel the timeout timer. | 231 timer_.Stop(); // Cancel the timeout timer. |
| 230 threat_type_ = threat_type; | 232 threat_type_ = threat_type; |
| 231 state_ = STATE_NONE; | 233 state_ = STATE_NONE; |
| 232 | 234 |
| 233 if (defer_state_ != DEFERRED_NONE) { | 235 if (defer_state_ != DEFERRED_NONE) { |
| 234 EndNetLogEvent(NetLog::TYPE_SAFE_BROWSING_DEFERRED, nullptr, nullptr); | 236 EndNetLogEvent(NetLogEventType::SAFE_BROWSING_DEFERRED, nullptr, nullptr); |
| 235 } | 237 } |
| 236 EndNetLogEvent( | 238 EndNetLogEvent( |
| 237 NetLog::TYPE_SAFE_BROWSING_CHECKING_URL, "result", | 239 NetLogEventType::SAFE_BROWSING_CHECKING_URL, "result", |
| 238 threat_type_ == safe_browsing::SB_THREAT_TYPE_SAFE ? "safe" : "unsafe"); | 240 threat_type_ == safe_browsing::SB_THREAT_TYPE_SAFE ? "safe" : "unsafe"); |
| 239 | 241 |
| 240 if (threat_type == safe_browsing::SB_THREAT_TYPE_SAFE) { | 242 if (threat_type == safe_browsing::SB_THREAT_TYPE_SAFE) { |
| 241 if (defer_state_ != DEFERRED_NONE) { | 243 if (defer_state_ != DEFERRED_NONE) { |
| 242 // Log how much time the safe browsing check cost us. | 244 // Log how much time the safe browsing check cost us. |
| 243 ui_manager_->LogPauseDelay(base::TimeTicks::Now() - defer_start_time_); | 245 ui_manager_->LogPauseDelay(base::TimeTicks::Now() - defer_start_time_); |
| 244 ResumeRequest(); | 246 ResumeRequest(); |
| 245 } else { | 247 } else { |
| 246 ui_manager_->LogPauseDelay(base::TimeDelta()); | 248 ui_manager_->LogPauseDelay(base::TimeDelta()); |
| 247 } | 249 } |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 content::RESOURCE_TYPE_LAST_TYPE); | 357 content::RESOURCE_TYPE_LAST_TYPE); |
| 356 | 358 |
| 357 if (succeeded_synchronously) { | 359 if (succeeded_synchronously) { |
| 358 threat_type_ = safe_browsing::SB_THREAT_TYPE_SAFE; | 360 threat_type_ = safe_browsing::SB_THREAT_TYPE_SAFE; |
| 359 ui_manager_->LogPauseDelay(base::TimeDelta()); // No delay. | 361 ui_manager_->LogPauseDelay(base::TimeDelta()); // No delay. |
| 360 return true; | 362 return true; |
| 361 } | 363 } |
| 362 | 364 |
| 363 state_ = STATE_CHECKING_URL; | 365 state_ = STATE_CHECKING_URL; |
| 364 url_being_checked_ = url; | 366 url_being_checked_ = url; |
| 365 BeginNetLogEvent(NetLog::TYPE_SAFE_BROWSING_CHECKING_URL, url, nullptr, | 367 BeginNetLogEvent(NetLogEventType::SAFE_BROWSING_CHECKING_URL, url, nullptr, |
| 366 nullptr); | 368 nullptr); |
| 367 | 369 |
| 368 // Start a timer to abort the check if it takes too long. | 370 // Start a timer to abort the check if it takes too long. |
| 369 // TODO(nparker): Set this only when we defer, based on remaining time, | 371 // TODO(nparker): Set this only when we defer, based on remaining time, |
| 370 // so we don't cancel earlier than necessary. | 372 // so we don't cancel earlier than necessary. |
| 371 timer_.Start(FROM_HERE, | 373 timer_.Start(FROM_HERE, |
| 372 base::TimeDelta::FromMilliseconds(kCheckUrlTimeoutMs), | 374 base::TimeDelta::FromMilliseconds(kCheckUrlTimeoutMs), |
| 373 this, &SafeBrowsingResourceThrottle::OnCheckUrlTimeout); | 375 this, &SafeBrowsingResourceThrottle::OnCheckUrlTimeout); |
| 374 | 376 |
| 375 return false; | 377 return false; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 388 CHECK_NE(defer_state_, DEFERRED_NONE); | 390 CHECK_NE(defer_state_, DEFERRED_NONE); |
| 389 | 391 |
| 390 bool resume = true; | 392 bool resume = true; |
| 391 if (defer_state_ == DEFERRED_UNCHECKED_REDIRECT) { | 393 if (defer_state_ == DEFERRED_UNCHECKED_REDIRECT) { |
| 392 // Save the redirect urls for possible malware detail reporting later. | 394 // Save the redirect urls for possible malware detail reporting later. |
| 393 redirect_urls_.push_back(unchecked_redirect_url_); | 395 redirect_urls_.push_back(unchecked_redirect_url_); |
| 394 if (!CheckUrl(unchecked_redirect_url_)) { | 396 if (!CheckUrl(unchecked_redirect_url_)) { |
| 395 // We're now waiting for the unchecked_redirect_url_. | 397 // We're now waiting for the unchecked_redirect_url_. |
| 396 defer_state_ = DEFERRED_REDIRECT; | 398 defer_state_ = DEFERRED_REDIRECT; |
| 397 resume = false; | 399 resume = false; |
| 398 BeginNetLogEvent(NetLog::TYPE_SAFE_BROWSING_DEFERRED, | 400 BeginNetLogEvent(NetLogEventType::SAFE_BROWSING_DEFERRED, |
| 399 unchecked_redirect_url_, "defer_reason", | 401 unchecked_redirect_url_, "defer_reason", |
| 400 "resumed_redirect"); | 402 "resumed_redirect"); |
| 401 } | 403 } |
| 402 } | 404 } |
| 403 | 405 |
| 404 if (resume) { | 406 if (resume) { |
| 405 defer_state_ = DEFERRED_NONE; | 407 defer_state_ = DEFERRED_NONE; |
| 406 controller()->Resume(); | 408 controller()->Resume(); |
| 407 } | 409 } |
| 408 } | 410 } |
| OLD | NEW |