summaryrefslogtreecommitdiffstats
path: root/chrome/browser/renderer_host/download_resource_handler.cc
blob: e414587617cdaf14f1ff91e582fb6911c20ea105 (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
// 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/download_resource_handler.h"

#include "base/logging.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/download/download_file.h"
#include "chrome/browser/download/download_manager.h"
#include "chrome/browser/renderer_host/global_request_id.h"
#include "chrome/browser/renderer_host/resource_dispatcher_host.h"
#include "net/base/io_buffer.h"
#include "net/url_request/url_request_context.h"

DownloadResourceHandler::DownloadResourceHandler(ResourceDispatcherHost* rdh,
                                                 int render_process_host_id,
                                                 int render_view_id,
                                                 int request_id,
                                                 const GURL& url,
                                                 DownloadFileManager* manager,
                                                 URLRequest* request,
                                                 bool save_as,
                                                 const FilePath& save_file_path)
    : download_id_(-1),
      global_id_(render_process_host_id, request_id),
      render_view_id_(render_view_id),
      url_(url),
      content_length_(0),
      download_manager_(manager),
      request_(request),
      save_as_(save_as),
      save_file_path_(save_file_path),
      buffer_(new DownloadBuffer),
      rdh_(rdh),
      is_paused_(false) {
}

// Not needed, as this event handler ought to be the final resource.
bool DownloadResourceHandler::OnRequestRedirected(int request_id,
                                                  const GURL& url,
                                                  ResourceResponse* response,
                                                  bool* defer) {
  url_ = url;
  return true;
}

// Send the download creation information to the download thread.
bool DownloadResourceHandler::OnResponseStarted(int request_id,
                                                ResourceResponse* response) {
  std::string content_disposition;
  request_->GetResponseHeaderByName("content-disposition",
                                    &content_disposition);
  set_content_disposition(content_disposition);
  set_content_length(response->response_head.content_length);

  download_id_ = download_manager_->GetNextId();
  // |download_manager_| consumes (deletes):
  DownloadCreateInfo* info = new DownloadCreateInfo;
  info->url = url_;
  info->referrer_url = GURL(request_->referrer());
  info->start_time = base::Time::Now();
  info->received_bytes = 0;
  info->total_bytes = content_length_;
  info->state = DownloadItem::IN_PROGRESS;
  info->download_id = download_id_;
  info->child_id = global_id_.child_id;
  info->render_view_id = render_view_id_;
  info->request_id = global_id_.request_id;
  info->content_disposition = content_disposition_;
  info->mime_type = response->response_head.mime_type;
  info->save_as = save_as_ && save_file_path_.empty();
  info->is_dangerous = false;
  info->referrer_charset = request_->context()->referrer_charset();
  info->save_file_path = save_file_path_;
  ChromeThread::PostTask(
      ChromeThread::FILE, FROM_HERE,
      NewRunnableMethod(
          download_manager_, &DownloadFileManager::StartDownload, info));
  return true;
}

// Create a new buffer, which will be handed to the download thread for file
// writing and deletion.
bool DownloadResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf,
                                         int* buf_size, int min_size) {
  DCHECK(buf && buf_size);
  if (!read_buffer_) {
    *buf_size = min_size < 0 ? kReadBufSize : min_size;
    read_buffer_ = new net::IOBuffer(*buf_size);
  }
  *buf = read_buffer_.get();
  // TODO(willchan): Remove after debugging bug 16371.
  CHECK(read_buffer_->data());
  return true;
}

// Pass the buffer to the download file writer.
bool DownloadResourceHandler::OnReadCompleted(int request_id, int* bytes_read) {
  if (!*bytes_read)
    return true;
  DCHECK(read_buffer_);
  AutoLock auto_lock(buffer_->lock);
  bool need_update = buffer_->contents.empty();

  // We are passing ownership of this buffer to the download file manager.
  net::IOBuffer* buffer = NULL;
  read_buffer_.swap(&buffer);
  buffer_->contents.push_back(std::make_pair(buffer, *bytes_read));
  if (need_update) {
    ChromeThread::PostTask(
        ChromeThread::FILE, FROM_HERE,
        NewRunnableMethod(download_manager_,
                          &DownloadFileManager::UpdateDownload,
                          download_id_,
                          buffer_));
  }

  // We schedule a pause outside of the read loop if there is too much file
  // writing work to do.
  if (buffer_->contents.size() > kLoadsToWrite)
    StartPauseTimer();

  return true;
}

bool DownloadResourceHandler::OnResponseCompleted(
    int request_id,
    const URLRequestStatus& status,
    const std::string& security_info) {
  ChromeThread::PostTask(
      ChromeThread::FILE, FROM_HERE,
      NewRunnableMethod(download_manager_,
                        &DownloadFileManager::DownloadFinished,
                        download_id_,
                        buffer_));
  read_buffer_ = NULL;

  // 'buffer_' is deleted by the DownloadFileManager.
  buffer_ = NULL;
  return true;
}

// If the content-length header is not present (or contains something other
// than numbers), the incoming content_length is -1 (unknown size).
// Set the content length to 0 to indicate unknown size to DownloadManager.
void DownloadResourceHandler::set_content_length(const int64& content_length) {
  content_length_ = 0;
  if (content_length > 0)
    content_length_ = content_length;
}

void DownloadResourceHandler::set_content_disposition(
    const std::string& content_disposition) {
  content_disposition_ = content_disposition;
}

void DownloadResourceHandler::CheckWriteProgress() {
  if (!buffer_)
    return;  // The download completed while we were waiting to run.

  size_t contents_size;
  {
    AutoLock lock(buffer_->lock);
    contents_size = buffer_->contents.size();
  }

  bool should_pause = contents_size > kLoadsToWrite;

  // We'll come back later and see if it's okay to unpause the request.
  if (should_pause)
    StartPauseTimer();

  if (is_paused_ != should_pause) {
    rdh_->PauseRequest(global_id_.child_id, global_id_.request_id,
                       should_pause);
    is_paused_ = should_pause;
  }
}

void DownloadResourceHandler::StartPauseTimer() {
  if (!pause_timer_.IsRunning())
    pause_timer_.Start(base::TimeDelta::FromMilliseconds(kThrottleTimeMs), this,
                       &DownloadResourceHandler::CheckWriteProgress);
}