Fix UnlockEndpointIsDelayed again.
[chromium-blink-merge.git] / chrome / browser / renderer_host / safe_browsing_resource_throttle.h
blobc11db662915de6f7f9db976c0f844349e774a01a
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_
8 #include <string>
9 #include <vector>
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;
21 namespace net {
22 class URLRequest;
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
46 // resumed.
47 class SafeBrowsingResourceThrottle
48 : public content::ResourceThrottle,
49 public SafeBrowsingDatabaseManager::Client,
50 public base::SupportsWeakPtr<SafeBrowsingResourceThrottle> {
51 public:
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 GURL& new_url, bool* defer) override;
59 const char* GetNameForLogging() const override;
61 // SafeBrowsingDabaseManager::Client implementation (called on IO thread):
62 void OnCheckBrowseUrlResult(const GURL& url,
63 SBThreatType result,
64 const std::string& metadata) override;
66 private:
67 // Describes what phase of the check a throttle is in.
68 enum State {
69 STATE_NONE,
70 STATE_CHECKING_URL,
71 STATE_DISPLAYING_BLOCKING_PAGE,
74 // Describes what stage of the request got paused by the check.
75 enum DeferState {
76 DEFERRED_NONE,
77 DEFERRED_START,
78 DEFERRED_REDIRECT,
81 ~SafeBrowsingResourceThrottle() override;
83 // SafeBrowsingService::UrlCheckCallback implementation.
84 void OnBlockingPageComplete(bool proceed);
86 // Starts running |url| through the safe browsing check. Returns true if the
87 // URL is safe to visit. Otherwise returns false and will call
88 // OnBrowseUrlResult() when the check has completed.
89 bool CheckUrl(const GURL& url);
91 // Callback for when the safe browsing check (which was initiated by
92 // StartCheckingUrl()) has taken longer than kCheckUrlTimeoutMs.
93 void OnCheckUrlTimeout();
95 // Starts displaying the safe browsing interstitial page if it's not
96 // prerendering. Called on the UI thread.
97 static void StartDisplayingBlockingPage(
98 const base::WeakPtr<SafeBrowsingResourceThrottle>& throttle,
99 scoped_refptr<SafeBrowsingUIManager> ui_manager,
100 const SafeBrowsingUIManager::UnsafeResource& resource);
102 // Called on the IO thread if the request turned out to be for a prerendered
103 // page.
104 void Cancel();
106 // Resumes the request, by continuing the deferred action (either starting the
107 // request, or following a redirect).
108 void ResumeRequest();
110 State state_;
111 DeferState defer_state_;
113 // The result of the most recent safe browsing check. Only valid to read this
114 // when state_ != STATE_CHECKING_URL.
115 SBThreatType threat_type_;
117 // The time when the outstanding safe browsing check was started.
118 base::TimeTicks url_check_start_time_;
120 // Timer to abort the safe browsing check if it takes too long.
121 base::OneShotTimer<SafeBrowsingResourceThrottle> timer_;
123 // The redirect chain for this resource
124 std::vector<GURL> redirect_urls_;
126 GURL url_being_checked_;
128 scoped_refptr<SafeBrowsingDatabaseManager> database_manager_;
129 scoped_refptr<SafeBrowsingUIManager> ui_manager_;
130 const net::URLRequest* request_;
131 const bool is_subresource_;
132 const bool is_subframe_;
134 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingResourceThrottle);
138 #endif // CHROME_BROWSER_RENDERER_HOST_SAFE_BROWSING_RESOURCE_THROTTLE_H_