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 #ifndef CHROME_BROWSER_RENDERER_HOST_SAFE_BROWSING_RESOURCE_THROTTLE_H_
6 #define CHROME_BROWSER_RENDERER_HOST_SAFE_BROWSING_RESOURCE_THROTTLE_H_
11 #include "base/memory/ref_counted.h"
12 #include "base/time/time.h"
13 #include "base/timer/timer.h"
14 #include "chrome/browser/safe_browsing/database_manager.h"
15 #include "chrome/browser/safe_browsing/ui_manager.h"
16 #include "content/public/browser/resource_throttle.h"
17 #include "content/public/common/resource_type.h"
19 class ResourceDispatcherHost
;
25 // SafeBrowsingResourceThrottle checks that URLs are "safe" before navigating
26 // to them. To be considered "safe", a URL must not appear in the
27 // malware/phishing blacklists (see SafeBrowsingService for details).
29 // This check is done before requesting the original URL, and additionally
30 // before following any subsequent redirect.
32 // In the common case, the check completes synchronously (no match in the bloom
33 // filter), so the request's flow is un-interrupted.
35 // However if the URL fails this quick check, it has the possibility of being
36 // on the blacklist. Now the request is suspended (prevented from starting),
37 // and a more expensive safe browsing check is begun (fetches the full hashes).
39 // Note that the safe browsing check takes at most kCheckUrlTimeoutMs
40 // milliseconds. If it takes longer than this, then the system defaults to
41 // treating the URL as safe.
43 // Once the safe browsing check has completed, if the URL was decided to be
44 // dangerous, a warning page is thrown up and the request remains suspended.
45 // If on the other hand the URL was decided to be safe, the request is
47 class SafeBrowsingResourceThrottle
48 : public content::ResourceThrottle
,
49 public SafeBrowsingDatabaseManager::Client
,
50 public base::SupportsWeakPtr
<SafeBrowsingResourceThrottle
> {
52 SafeBrowsingResourceThrottle(const net::URLRequest
* request
,
53 content::ResourceType resource_type
,
54 SafeBrowsingService
* safe_browsing
);
56 // content::ResourceThrottle implementation (called on IO thread):
57 void WillStartRequest(bool* defer
) override
;
58 void WillRedirectRequest(const net::RedirectInfo
& redirect_info
,
59 bool* defer
) override
;
60 const char* GetNameForLogging() const override
;
62 // SafeBrowsingDabaseManager::Client implementation (called on IO thread):
63 void OnCheckBrowseUrlResult(const GURL
& url
,
65 const std::string
& metadata
) override
;
68 // Describes what phase of the check a throttle is in.
72 STATE_DISPLAYING_BLOCKING_PAGE
,
75 // Describes what stage of the request got paused by the check.
82 ~SafeBrowsingResourceThrottle() override
;
84 // SafeBrowsingService::UrlCheckCallback implementation.
85 void OnBlockingPageComplete(bool proceed
);
87 // Starts running |url| through the safe browsing check. Returns true if the
88 // URL is safe to visit. Otherwise returns false and will call
89 // OnBrowseUrlResult() when the check has completed.
90 bool CheckUrl(const GURL
& url
);
92 // Callback for when the safe browsing check (which was initiated by
93 // StartCheckingUrl()) has taken longer than kCheckUrlTimeoutMs.
94 void OnCheckUrlTimeout();
96 // Starts displaying the safe browsing interstitial page if it's not
97 // prerendering. Called on the UI thread.
98 static void StartDisplayingBlockingPage(
99 const base::WeakPtr
<SafeBrowsingResourceThrottle
>& throttle
,
100 scoped_refptr
<SafeBrowsingUIManager
> ui_manager
,
101 const SafeBrowsingUIManager::UnsafeResource
& resource
);
103 // Called on the IO thread if the request turned out to be for a prerendered
107 // Resumes the request, by continuing the deferred action (either starting the
108 // request, or following a redirect).
109 void ResumeRequest();
112 DeferState defer_state_
;
114 // The result of the most recent safe browsing check. Only valid to read this
115 // when state_ != STATE_CHECKING_URL.
116 SBThreatType threat_type_
;
118 // The time when the outstanding safe browsing check was started.
119 base::TimeTicks url_check_start_time_
;
121 // Timer to abort the safe browsing check if it takes too long.
122 base::OneShotTimer
<SafeBrowsingResourceThrottle
> timer_
;
124 // The redirect chain for this resource
125 std::vector
<GURL
> redirect_urls_
;
127 GURL url_being_checked_
;
129 scoped_refptr
<SafeBrowsingDatabaseManager
> database_manager_
;
130 scoped_refptr
<SafeBrowsingUIManager
> ui_manager_
;
131 const net::URLRequest
* request_
;
132 const bool is_subresource_
;
133 const bool is_subframe_
;
135 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingResourceThrottle
);
139 #endif // CHROME_BROWSER_RENDERER_HOST_SAFE_BROWSING_RESOURCE_THROTTLE_H_