[sql] Remove _HAS_EXCEPTIONS=0 from build info.
[chromium-blink-merge.git] / chrome / browser / renderer_host / safe_browsing_resource_throttle.h
blob7e4b9f40bbdfe727ceb8dbb6824e8984b079c2a5
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
26 // navigating to them. To be considered "safe", a URL must not appear in the
27 // malware/phishing blacklists (see SafeBrowsingService for details).
29 // On desktop (ifdef SAFE_BROWSING_DB_LOCAL)
30 // -----------------------------------------
31 // This check is done before requesting the original URL, and additionally
32 // before following any subsequent redirect. In the common case the check
33 // completes synchronously (no match in the in-memory DB), so the request's
34 // flow is un-interrupted. However if the URL fails this quick check, it
35 // has the possibility of being on the blacklist. Now the request is
36 // deferred (prevented from starting), and a more expensive safe browsing
37 // check is begun (fetches the full hashes).
39 // On mobile (ifdef SAFE_BROWSING_DB_REMOTE):
40 // -----------------------------------------
41 // The check is started and runs in parallel with the resource load. If the
42 // check is not complete by the time the headers are loaded, the request is
43 // suspended until the URL is classified. We let the headers load on mobile
44 // since the RemoteSafeBrowsingDatabase checks always have some non-zero
45 // latency -- there no synchronous pass. This parallelism helps
46 // performance. Redirects are handled the same way as desktop so they
47 // always defer.
50 // Note that the safe browsing check takes at most kCheckUrlTimeoutMs
51 // milliseconds. If it takes longer than this, then the system defaults to
52 // treating the URL as safe.
54 // If the URL is classified as dangerous, a warning page is thrown up and
55 // the request remains suspended. If the user clicks "proceed" on warning
56 // page, we resume the request.
58 // Note: The ResourceThrottle interface is called in this order:
59 // WillStartRequest once, WillRedirectRequest zero or more times, and then
60 // WillProcessReponse once.
61 class SafeBrowsingResourceThrottle
62 : public content::ResourceThrottle,
63 public SafeBrowsingDatabaseManager::Client,
64 public base::SupportsWeakPtr<SafeBrowsingResourceThrottle> {
65 public:
66 SafeBrowsingResourceThrottle(const net::URLRequest* request,
67 content::ResourceType resource_type,
68 SafeBrowsingService* safe_browsing,
69 bool defer_at_start);
71 // content::ResourceThrottle implementation (called on IO thread):
72 void WillStartRequest(bool* defer) override;
73 void WillRedirectRequest(const net::RedirectInfo& redirect_info,
74 bool* defer) override;
75 void WillProcessResponse(bool* defer) override;
77 const char* GetNameForLogging() const override;
79 // SafeBrowsingDabaseManager::Client implementation (called on IO thread):
80 void OnCheckBrowseUrlResult(const GURL& url,
81 SBThreatType result,
82 const std::string& metadata) override;
84 private:
85 // Describes what phase of the check a throttle is in.
86 enum State {
87 // Haven't started checking or checking is complete. Not deferred.
88 STATE_NONE,
89 // We have one outstanding URL-check. Could be deferred.
90 STATE_CHECKING_URL,
91 // We're displaying a blocking page. Could be deferred.
92 STATE_DISPLAYING_BLOCKING_PAGE,
95 // Describes what stage of the request got paused by the check.
96 enum DeferState {
97 DEFERRED_NONE,
98 DEFERRED_START,
99 DEFERRED_REDIRECT,
100 DEFERRED_UNCHECKED_REDIRECT, // unchecked_redirect_url_ is populated.
101 DEFERRED_PROCESSING,
104 ~SafeBrowsingResourceThrottle() override;
106 // SafeBrowsingService::UrlCheckCallback implementation.
107 void OnBlockingPageComplete(bool proceed);
109 // Starts running |url| through the safe browsing check. Returns true if the
110 // URL is safe to visit. Otherwise returns false and will call
111 // OnBrowseUrlResult() when the check has completed.
112 bool CheckUrl(const GURL& url);
114 // Callback for when the safe browsing check (which was initiated by
115 // StartCheckingUrl()) has taken longer than kCheckUrlTimeoutMs.
116 void OnCheckUrlTimeout();
118 // Starts displaying the safe browsing interstitial page if it's not
119 // prerendering. Called on the UI thread.
120 static void StartDisplayingBlockingPage(
121 const base::WeakPtr<SafeBrowsingResourceThrottle>& throttle,
122 scoped_refptr<SafeBrowsingUIManager> ui_manager,
123 const SafeBrowsingUIManager::UnsafeResource& resource);
125 // Called on the IO thread if the request turned out to be for a prerendered
126 // page.
127 void Cancel();
129 // Resumes the request, by continuing the deferred action (either starting the
130 // request, or following a redirect).
131 void ResumeRequest();
133 // True if we want to block the starting of requests until they're
134 // deemed safe. Otherwise we let the resource partially load.
135 const bool defer_at_start_;
137 State state_;
138 DeferState defer_state_;
140 // The result of the most recent safe browsing check. Only valid to read this
141 // when state_ != STATE_CHECKING_URL.
142 SBThreatType threat_type_;
144 // The time when we started deferring the request.
145 base::TimeTicks defer_start_time_;
147 // Timer to abort the safe browsing check if it takes too long.
148 base::OneShotTimer<SafeBrowsingResourceThrottle> timer_;
150 // The redirect chain for this resource
151 std::vector<GURL> redirect_urls_;
153 // If in DEFERRED_UNCHECKED_REDIRECT state, this is the
154 // URL we still need to check before resuming.
155 GURL unchecked_redirect_url_;
156 GURL url_being_checked_;
158 scoped_refptr<SafeBrowsingDatabaseManager> database_manager_;
159 scoped_refptr<SafeBrowsingUIManager> ui_manager_;
160 const net::URLRequest* request_;
161 const bool is_subresource_;
162 const bool is_subframe_;
164 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingResourceThrottle);
166 #endif // CHROME_BROWSER_RENDERER_HOST_SAFE_BROWSING_RESOURCE_THROTTLE_H_