safe_browsing_resource_handler.h revision 3f50c38dc070f4bb515c1b64450dae14f316474e
1// Copyright (c) 2011 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.
4
5#ifndef CHROME_BROWSER_RENDERER_HOST_SAFE_BROWSING_RESOURCE_HANDLER_H_
6#define CHROME_BROWSER_RENDERER_HOST_SAFE_BROWSING_RESOURCE_HANDLER_H_
7#pragma once
8
9#include <string>
10
11#include "base/ref_counted.h"
12#include "base/time.h"
13#include "base/timer.h"
14#include "chrome/browser/renderer_host/resource_handler.h"
15#include "chrome/browser/safe_browsing/safe_browsing_service.h"
16
17class ResourceDispatcherHost;
18
19// SafeBrowsingResourceHandler checks that URLs are "safe" before navigating
20// to them. To be considered "safe", a URL must not appear in the
21// malware/phishing blacklists (see SafeBrowsingService for details).
22//
23// This check is done before requesting the original URL, and additionally
24// before following any subsequent redirect.
25//
26// In the common case, the check completes synchronously (no match in the bloom
27// filter), so the request's flow is un-interrupted.
28//
29// However if the URL fails this quick check, it has the possibility of being
30// on the blacklist. Now the request is suspended (prevented from starting),
31// and a more expensive safe browsing check is begun (fetches the full hashes).
32//
33// Note that the safe browsing check takes at most kCheckUrlTimeoutMs
34// milliseconds. If it takes longer than this, then the system defaults to
35// treating the URL as safe.
36//
37// Once the safe browsing check has completed, if the URL was decided to be
38// dangerous, a warning page is thrown up and the request remains suspended.
39// If on the other hand the URL was decided to be safe, the request is
40// resumed.
41class SafeBrowsingResourceHandler : public ResourceHandler,
42                                    public SafeBrowsingService::Client {
43 public:
44  SafeBrowsingResourceHandler(ResourceHandler* handler,
45                              int render_process_host_id,
46                              int render_view_id,
47                              ResourceType::Type resource_type,
48                              SafeBrowsingService* safe_browsing,
49                              ResourceDispatcherHost* resource_dispatcher_host);
50
51  // ResourceHandler implementation:
52  virtual bool OnUploadProgress(int request_id, uint64 position, uint64 size);
53  virtual bool OnRequestRedirected(int request_id, const GURL& new_url,
54                                   ResourceResponse* response, bool* defer);
55  virtual bool OnResponseStarted(int request_id, ResourceResponse* response);
56  virtual bool OnWillStart(int request_id, const GURL& url, bool* defer);
57  virtual bool OnWillRead(int request_id, net::IOBuffer** buf, int* buf_size,
58                          int min_size);
59  virtual bool OnReadCompleted(int request_id, int* bytes_read);
60  virtual bool OnResponseCompleted(int request_id,
61                                   const net::URLRequestStatus& status,
62                                   const std::string& security_info);
63  virtual void OnRequestClosed();
64
65  // SafeBrowsingService::Client implementation, called on the IO thread once
66  // the URL has been classified.
67  virtual void OnBrowseUrlCheckResult(
68      const GURL& url, SafeBrowsingService::UrlCheckResult result);
69
70  // SafeBrowsingService::Client implementation, called on the IO thread when
71  // the user has decided to proceed with the current request, or go back.
72  virtual void OnBlockingPageComplete(bool proceed);
73
74 private:
75  // Describes what phase of the check a handler is in.
76  enum State {
77    STATE_NONE,
78    STATE_CHECKING_URL,
79    STATE_DISPLAYING_BLOCKING_PAGE,
80  };
81
82  // Describes what stage of the request got paused by the check.
83  enum DeferState {
84    DEFERRED_NONE,
85    DEFERRED_START,
86    DEFERRED_REDIRECT,
87  };
88
89  ~SafeBrowsingResourceHandler();
90
91  // Cancels any in progress safe browsing actions.
92  void Shutdown();
93
94  // Starts running |url| through the safe browsing check. Returns true if the
95  // URL is safe to visit. Otherwise returns false and will call
96  // OnUrlCheckResult() when the check has completed.
97  bool CheckUrl(const GURL& url);
98
99  // Callback for when the safe browsing check (which was initiated by
100  // StartCheckingUrl()) has taken longer than kCheckUrlTimeoutMs.
101  void OnCheckUrlTimeout();
102
103  // Starts displaying the safe browsing interstitial page.
104  void StartDisplayingBlockingPage(const GURL& url,
105                                   SafeBrowsingService::UrlCheckResult result);
106
107  // Resumes the request, by continuing the deferred action (either starting the
108  // request, or following a redirect).
109  void ResumeRequest();
110
111  // Resumes the deferred "start".
112  void ResumeStart();
113
114  // Resumes the deferred redirect.
115  void ResumeRedirect();
116
117  // Erases the state associated with a deferred "start" or redirect
118  // (i.e. the deferred URL and request ID).
119  void ClearDeferredRequestInfo();
120
121  State state_;
122  DeferState defer_state_;
123
124  // The result of the most recent safe browsing check. Only valid to read this
125  // when state_ != STATE_CHECKING_URL.
126  SafeBrowsingService::UrlCheckResult safe_browsing_result_;
127
128  // The time when the outstanding safe browsing check was started.
129  base::TimeTicks url_check_start_time_;
130
131  // Timer to abort the safe browsing check if it takes too long.
132  base::OneShotTimer<SafeBrowsingResourceHandler> timer_;
133
134  // Details on the deferred request (either a start or redirect). It is only
135  // valid to access these members when defer_state_ != DEFERRED_NONE.
136  GURL deferred_url_;
137  int deferred_request_id_;
138  scoped_refptr<ResourceResponse> deferred_redirect_response_;
139
140  scoped_refptr<ResourceHandler> next_handler_;
141  int render_process_host_id_;
142  int render_view_id_;
143  scoped_refptr<SafeBrowsingService> safe_browsing_;
144  ResourceDispatcherHost* rdh_;
145  ResourceType::Type resource_type_;
146
147  DISALLOW_COPY_AND_ASSIGN(SafeBrowsingResourceHandler);
148};
149
150
151#endif  // CHROME_BROWSER_RENDERER_HOST_SAFE_BROWSING_RESOURCE_HANDLER_H_
152