Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / renderer_host / safe_browsing_resource_throttle.h
bloba21e92a11243b4e44dc46c237f36e1595ed0d681
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 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,
64 SBThreatType result,
65 const std::string& metadata) override;
67 private:
68 // Describes what phase of the check a throttle is in.
69 enum State {
70 STATE_NONE,
71 STATE_CHECKING_URL,
72 STATE_DISPLAYING_BLOCKING_PAGE,
75 // Describes what stage of the request got paused by the check.
76 enum DeferState {
77 DEFERRED_NONE,
78 DEFERRED_START,
79 DEFERRED_REDIRECT,
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
104 // page.
105 void Cancel();
107 // Resumes the request, by continuing the deferred action (either starting the
108 // request, or following a redirect).
109 void ResumeRequest();
111 State state_;
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_