summaryrefslogtreecommitdiffstats
path: root/chrome/browser/renderer_host/safe_browsing_resource_handler.cc
blob: 22c6873d3b69237651413d4170f2a3e3ac1b5db9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/renderer_host/safe_browsing_resource_handler.h"

#include "chrome/browser/renderer_host/resource_dispatcher_host.h"

// Maximum time to wait for a gethash response from the Safe Browsing servers.
static const int kMaxGetHashMs = 1000;

SafeBrowsingResourceHandler::SafeBrowsingResourceHandler(
    ResourceHandler* handler,
    int render_process_host_id,
    int render_view_id,
    const GURL& url,
    ResourceType::Type resource_type,
    SafeBrowsingService* safe_browsing,
    ResourceDispatcherHost* resource_dispatcher_host)
    : next_handler_(handler),
      render_process_host_id_(render_process_host_id),
      render_view_id_(render_view_id),
      paused_request_id_(-1),
      in_safe_browsing_check_(false),
      displaying_blocking_page_(false),
      safe_browsing_(safe_browsing),
      queued_error_request_id_(-1),
      rdh_(resource_dispatcher_host),
      resource_type_(resource_type) {
  if (safe_browsing_->CheckUrl(url, this)) {
    safe_browsing_result_ = SafeBrowsingService::URL_SAFE;
    safe_browsing_->LogPauseDelay(base::TimeDelta());  // No delay.
  } else {
    AddRef();
    in_safe_browsing_check_ = true;
    // Can't pause now because it's too early, so we'll do it in OnWillRead.
  }
}

SafeBrowsingResourceHandler::~SafeBrowsingResourceHandler() {
  // If we're being deleted before the SafeBrowsing check has completed, cancel
  // the check.
  if (in_safe_browsing_check_)
    safe_browsing_->CancelCheck(this);
}

bool SafeBrowsingResourceHandler::OnUploadProgress(int request_id,
                                                   uint64 position,
                                                   uint64 size) {
  return next_handler_->OnUploadProgress(request_id, position, size);
}

bool SafeBrowsingResourceHandler::OnRequestRedirected(int request_id,
                                                      const GURL& new_url) {
  if (in_safe_browsing_check_) {
    Release();
    in_safe_browsing_check_ = false;
    safe_browsing_->CancelCheck(this);
  }

  if (safe_browsing_->CheckUrl(new_url, this)) {
    safe_browsing_result_ = SafeBrowsingService::URL_SAFE;
    safe_browsing_->LogPauseDelay(base::TimeDelta());  // No delay.
  } else {
    AddRef();
    in_safe_browsing_check_ = true;
    // Can't pause now because it's too early, so we'll do it in OnWillRead.
  }

  return next_handler_->OnRequestRedirected(request_id, new_url);
}

bool SafeBrowsingResourceHandler::OnResponseStarted(
    int request_id, ResourceResponse* response) {
  return next_handler_->OnResponseStarted(request_id, response);
}

void SafeBrowsingResourceHandler::OnGetHashTimeout() {
  if (!in_safe_browsing_check_)
    return;

  safe_browsing_->CancelCheck(this);
  OnUrlCheckResult(GURL::EmptyGURL(), SafeBrowsingService::URL_SAFE);
}

bool SafeBrowsingResourceHandler::OnWillRead(int request_id,
                                             net::IOBuffer** buf, int* buf_size,
                                             int min_size) {
  if (in_safe_browsing_check_ && pause_time_.is_null()) {
    pause_time_ = base::Time::Now();
    MessageLoop::current()->PostDelayedTask(
        FROM_HERE,
        NewRunnableMethod(this, &SafeBrowsingResourceHandler::OnGetHashTimeout),
        kMaxGetHashMs);
  }

  if (in_safe_browsing_check_ || displaying_blocking_page_) {
    rdh_->PauseRequest(render_process_host_id_, request_id, true);
    paused_request_id_ = request_id;
  }

  return next_handler_->OnWillRead(request_id, buf, buf_size, min_size);
}

bool SafeBrowsingResourceHandler::OnReadCompleted(int request_id,
                                                  int* bytes_read) {
  return next_handler_->OnReadCompleted(request_id, bytes_read);
}

bool SafeBrowsingResourceHandler::OnResponseCompleted(
    int request_id, const URLRequestStatus& status,
    const std::string& security_info) {
  if ((in_safe_browsing_check_ ||
       safe_browsing_result_ != SafeBrowsingService::URL_SAFE) &&
      status.status() == URLRequestStatus::FAILED &&
      status.os_error() == net::ERR_NAME_NOT_RESOLVED) {
    // Got a DNS error while the safebrowsing check is in progress or we
    // already know that the site is unsafe.  Don't show the the dns error
    // page.
    queued_error_.reset(new URLRequestStatus(status));
    queued_error_request_id_ = request_id;
    queued_security_info_ = security_info;
    return true;
  }

  return next_handler_->OnResponseCompleted(request_id, status, security_info);
}

// SafeBrowsingService::Client implementation, called on the IO thread once
// the URL has been classified.
void SafeBrowsingResourceHandler::OnUrlCheckResult(
    const GURL& url, SafeBrowsingService::UrlCheckResult result) {
  DCHECK(in_safe_browsing_check_);
  DCHECK(!displaying_blocking_page_);

  safe_browsing_result_ = result;
  in_safe_browsing_check_ = false;

  if (result == SafeBrowsingService::URL_SAFE) {
    if (paused_request_id_ != -1) {
      rdh_->PauseRequest(render_process_host_id_, paused_request_id_, false);
      paused_request_id_ = -1;
    }

    base::TimeDelta pause_delta;
    if (!pause_time_.is_null())
      pause_delta = base::Time::Now() - pause_time_;
    safe_browsing_->LogPauseDelay(pause_delta);

    if (queued_error_.get()) {
      next_handler_->OnResponseCompleted(
          queued_error_request_id_, *queued_error_.get(),
          queued_security_info_);
      queued_error_.reset();
      queued_security_info_.clear();
    }

    Release();
  } else {
    displaying_blocking_page_ = true;
    safe_browsing_->DisplayBlockingPage(
        url, resource_type_, result, this, rdh_->ui_loop(),
        render_process_host_id_, render_view_id_);
  }
}

// SafeBrowsingService::Client implementation, called on the IO thread when
// the user has decided to proceed with the current request, or go back.
void SafeBrowsingResourceHandler::OnBlockingPageComplete(bool proceed) {
  DCHECK(displaying_blocking_page_);
  displaying_blocking_page_ = false;

  if (proceed) {
    safe_browsing_result_ = SafeBrowsingService::URL_SAFE;
    if (paused_request_id_ != -1) {
      rdh_->PauseRequest(render_process_host_id_, paused_request_id_, false);
      paused_request_id_ = -1;
    }

    if (queued_error_.get()) {
      next_handler_->OnResponseCompleted(
          queued_error_request_id_, *queued_error_.get(),
          queued_security_info_);
      queued_error_.reset();
      queued_security_info_.clear();
    }
  } else {
    rdh_->CancelRequest(render_process_host_id_, paused_request_id_, false);
  }

  Release();
}