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
3 // found in the LICENSE file.
5 #include "chrome/browser/renderer_host/safe_browsing_resource_throttle.h"
7 #include "base/logging.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/prerender/prerender_contents.h"
10 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/render_view_host.h"
13 #include "content/public/browser/resource_controller.h"
14 #include "content/public/browser/resource_request_info.h"
15 #include "content/public/browser/web_contents.h"
16 #include "net/base/load_flags.h"
17 #include "net/url_request/redirect_info.h"
18 #include "net/url_request/url_request.h"
20 // Maximum time in milliseconds to wait for the safe browsing service to
21 // verify a URL. After this amount of time the outstanding check will be
22 // aborted, and the URL will be treated as if it were safe.
23 static const int kCheckUrlTimeoutMs
= 5000;
25 // TODO(eroman): Downgrade these CHECK()s to DCHECKs once there is more
26 // unit test coverage.
28 SafeBrowsingResourceThrottle::SafeBrowsingResourceThrottle(
29 const net::URLRequest
* request
,
30 content::ResourceType resource_type
,
31 SafeBrowsingService
* safe_browsing
)
33 defer_state_(DEFERRED_NONE
),
34 threat_type_(SB_THREAT_TYPE_SAFE
),
35 database_manager_(safe_browsing
->database_manager()),
36 ui_manager_(safe_browsing
->ui_manager()),
38 is_subresource_(resource_type
!= content::RESOURCE_TYPE_MAIN_FRAME
),
39 is_subframe_(resource_type
== content::RESOURCE_TYPE_SUB_FRAME
) {
42 SafeBrowsingResourceThrottle::~SafeBrowsingResourceThrottle() {
43 if (state_
== STATE_CHECKING_URL
)
44 database_manager_
->CancelCheck(this);
47 void SafeBrowsingResourceThrottle::WillStartRequest(bool* defer
) {
48 // We need to check the new URL before starting the request.
49 if (CheckUrl(request_
->url()))
52 // If the URL couldn't be verified synchronously, defer starting the
53 // request until the check has completed.
54 defer_state_
= DEFERRED_START
;
58 void SafeBrowsingResourceThrottle::WillRedirectRequest(
59 const net::RedirectInfo
& redirect_info
,
61 CHECK(state_
== STATE_NONE
);
62 CHECK(defer_state_
== DEFERRED_NONE
);
64 // Save the redirect urls for possible malware detail reporting later.
65 redirect_urls_
.push_back(redirect_info
.new_url
);
67 // We need to check the new URL before following the redirect.
68 if (CheckUrl(redirect_info
.new_url
))
71 // If the URL couldn't be verified synchronously, defer following the
72 // redirect until the SafeBrowsing check is complete. Store the redirect
73 // context so we can pass it on to other handlers once we have completed
75 defer_state_
= DEFERRED_REDIRECT
;
79 const char* SafeBrowsingResourceThrottle::GetNameForLogging() const {
80 return "SafeBrowsingResourceThrottle";
83 // SafeBrowsingService::Client implementation, called on the IO thread once
84 // the URL has been classified.
85 void SafeBrowsingResourceThrottle::OnCheckBrowseUrlResult(
87 SBThreatType threat_type
,
88 const std::string
& metadata
) {
89 CHECK(state_
== STATE_CHECKING_URL
);
90 CHECK(defer_state_
!= DEFERRED_NONE
);
91 CHECK(url
== url_being_checked_
) << "Was expecting: " << url_being_checked_
92 << " but got: " << url
;
94 timer_
.Stop(); // Cancel the timeout timer.
95 threat_type_
= threat_type
;
98 if (threat_type
== SB_THREAT_TYPE_SAFE
) {
99 // Log how much time the safe browsing check cost us.
100 ui_manager_
->LogPauseDelay(base::TimeTicks::Now() - url_check_start_time_
);
102 // Continue the request.
107 if (request_
->load_flags() & net::LOAD_PREFETCH
) {
108 // Don't prefetch resources that fail safe browsing, disallow
110 controller()->Cancel();
114 const content::ResourceRequestInfo
* info
=
115 content::ResourceRequestInfo::ForRequest(request_
);
117 SafeBrowsingUIManager::UnsafeResource resource
;
119 resource
.original_url
= request_
->original_url();
120 resource
.redirect_urls
= redirect_urls_
;
121 resource
.is_subresource
= is_subresource_
;
122 resource
.is_subframe
= is_subframe_
;
123 resource
.threat_type
= threat_type
;
124 resource
.threat_metadata
= metadata
;
125 resource
.callback
= base::Bind(
126 &SafeBrowsingResourceThrottle::OnBlockingPageComplete
, AsWeakPtr());
127 resource
.render_process_host_id
= info
->GetChildID();
128 resource
.render_view_id
= info
->GetRouteID();
130 state_
= STATE_DISPLAYING_BLOCKING_PAGE
;
132 content::BrowserThread::PostTask(
133 content::BrowserThread::UI
,
135 base::Bind(&SafeBrowsingResourceThrottle::StartDisplayingBlockingPage
,
136 AsWeakPtr(), ui_manager_
, resource
));
139 void SafeBrowsingResourceThrottle::StartDisplayingBlockingPage(
140 const base::WeakPtr
<SafeBrowsingResourceThrottle
>& throttle
,
141 scoped_refptr
<SafeBrowsingUIManager
> ui_manager
,
142 const SafeBrowsingUIManager::UnsafeResource
& resource
) {
143 bool should_show_blocking_page
= true;
145 content::RenderViewHost
* rvh
= content::RenderViewHost::FromID(
146 resource
.render_process_host_id
, resource
.render_view_id
);
148 content::WebContents
* web_contents
=
149 content::WebContents::FromRenderViewHost(rvh
);
150 prerender::PrerenderContents
* prerender_contents
=
151 prerender::PrerenderContents::FromWebContents(web_contents
);
152 if (prerender_contents
) {
153 prerender_contents
->Destroy(prerender::FINAL_STATUS_SAFE_BROWSING
);
154 should_show_blocking_page
= false;
157 if (should_show_blocking_page
) {
158 ui_manager
->DisplayBlockingPage(resource
);
163 // Tab is gone or it's being prerendered.
164 content::BrowserThread::PostTask(
165 content::BrowserThread::IO
,
167 base::Bind(&SafeBrowsingResourceThrottle::Cancel
, throttle
));
170 void SafeBrowsingResourceThrottle::Cancel() {
171 controller()->Cancel();
174 // SafeBrowsingService::UrlCheckCallback implementation, called on the IO
175 // thread when the user has decided to proceed with the current request, or
177 void SafeBrowsingResourceThrottle::OnBlockingPageComplete(bool proceed
) {
178 CHECK(state_
== STATE_DISPLAYING_BLOCKING_PAGE
);
182 threat_type_
= SB_THREAT_TYPE_SAFE
;
185 controller()->Cancel();
189 bool SafeBrowsingResourceThrottle::CheckUrl(const GURL
& url
) {
190 CHECK(state_
== STATE_NONE
);
191 bool succeeded_synchronously
= database_manager_
->CheckBrowseUrl(url
, this);
192 if (succeeded_synchronously
) {
193 threat_type_
= SB_THREAT_TYPE_SAFE
;
194 ui_manager_
->LogPauseDelay(base::TimeDelta()); // No delay.
198 state_
= STATE_CHECKING_URL
;
199 url_being_checked_
= url
;
201 // Record the start time of the check.
202 url_check_start_time_
= base::TimeTicks::Now();
204 // Start a timer to abort the check if it takes too long.
205 timer_
.Start(FROM_HERE
,
206 base::TimeDelta::FromMilliseconds(kCheckUrlTimeoutMs
),
207 this, &SafeBrowsingResourceThrottle::OnCheckUrlTimeout
);
212 void SafeBrowsingResourceThrottle::OnCheckUrlTimeout() {
213 CHECK(state_
== STATE_CHECKING_URL
);
214 CHECK(defer_state_
!= DEFERRED_NONE
);
216 database_manager_
->CancelCheck(this);
217 OnCheckBrowseUrlResult(
218 url_being_checked_
, SB_THREAT_TYPE_SAFE
, std::string());
221 void SafeBrowsingResourceThrottle::ResumeRequest() {
222 CHECK(state_
== STATE_NONE
);
223 CHECK(defer_state_
!= DEFERRED_NONE
);
225 defer_state_
= DEFERRED_NONE
;
226 controller()->Resume();