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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
// Copyright (c) 2011 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 "content/browser/download/download_resource_handler.h"
#include <string>
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/metrics/stats_counters.h"
#include "base/stringprintf.h"
#include "content/browser/download/download_buffer.h"
#include "content/browser/download/download_create_info.h"
#include "content/browser/download/download_file_manager.h"
#include "content/browser/download/download_item.h"
#include "content/browser/download/download_request_handle.h"
#include "content/browser/download/download_request_handle.h"
#include "content/browser/download/download_stats.h"
#include "content/browser/download/interrupt_reasons.h"
#include "content/browser/renderer_host/global_request_id.h"
#include "content/browser/renderer_host/resource_dispatcher_host.h"
#include "content/browser/renderer_host/resource_dispatcher_host_request_info.h"
#include "content/common/resource_response.h"
#include "content/public/browser/browser_thread.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/http/http_response_headers.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,
DownloadId dl_id,
DownloadFileManager* download_file_manager,
net::URLRequest* request,
bool save_as,
const DownloadResourceHandler::OnStartedCallback& started_cb,
const DownloadSaveInfo& save_info)
: download_id_(dl_id),
global_id_(render_process_host_id, request_id),
render_view_id_(render_view_id),
content_length_(0),
download_file_manager_(download_file_manager),
request_(request),
save_as_(save_as),
started_cb_(started_cb),
save_info_(save_info),
buffer_(new content::DownloadBuffer),
rdh_(rdh),
is_paused_(false) {
DCHECK(dl_id.IsValid());
download_stats::RecordDownloadCount(download_stats::UNTHROTTLED_COUNT);
}
bool DownloadResourceHandler::OnUploadProgress(int request_id,
uint64 position,
uint64 size) {
return true;
}
// 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) {
return true;
}
// Send the download creation information to the download thread.
bool DownloadResourceHandler::OnResponseStarted(int request_id,
ResourceResponse* response) {
DCHECK(download_id_.IsValid());
VLOG(20) << __FUNCTION__ << "()" << DebugString()
<< " request_id = " << request_id;
download_start_time_ = base::TimeTicks::Now();
std::string content_disposition;
request_->GetResponseHeaderByName("content-disposition",
&content_disposition);
set_content_disposition(content_disposition);
set_content_length(response->response_head.content_length);
const ResourceDispatcherHostRequestInfo* request_info =
ResourceDispatcherHost::InfoForRequest(request_);
// Deleted in DownloadManager.
DownloadCreateInfo* info = new DownloadCreateInfo(FilePath(), GURL(),
base::Time::Now(), 0, content_length_, DownloadItem::IN_PROGRESS,
download_id_.local(), request_info->has_user_gesture(),
request_info->transition_type());
info->url_chain = request_->url_chain();
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_.local();
info->has_user_gesture = request_info->has_user_gesture();
info->content_disposition = content_disposition_;
info->mime_type = response->response_head.mime_type;
download_stats::RecordDownloadMimeType(info->mime_type);
DownloadRequestHandle request_handle(rdh_, global_id_.child_id,
render_view_id_, global_id_.request_id);
// TODO(ahendrickson) -- Get the last modified time and etag, so we can
// resume downloading.
CallStartedCB(net::OK);
std::string content_type_header;
if (!response->response_head.headers ||
!response->response_head.headers->GetMimeType(&content_type_header))
content_type_header = "";
info->original_mime_type = content_type_header;
info->prompt_user_for_save_location =
save_as_ && save_info_.file_path.empty();
info->referrer_charset = request_->context()->referrer_charset();
info->save_info = save_info_;
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
NewRunnableMethod(
download_file_manager_, &DownloadFileManager::StartDownload,
info, request_handle));
// We can't start saving the data before we create the file on disk.
// The request will be un-paused in DownloadFileManager::CreateDownloadFile.
rdh_->PauseRequest(global_id_.child_id, global_id_.request_id, true);
return true;
}
void DownloadResourceHandler::CallStartedCB(net::Error error) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (started_cb_.is_null())
return;
started_cb_.Run(download_id_, error);
started_cb_.Reset();
}
bool DownloadResourceHandler::OnWillStart(int request_id,
const GURL& url,
bool* defer) {
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();
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_);
// Swap the data.
net::IOBuffer* io_buffer = NULL;
read_buffer_.swap(&io_buffer);
size_t vector_size = buffer_->AddData(io_buffer, *bytes_read);
bool need_update = (vector_size == 1); // Buffer was empty.
// We are passing ownership of this buffer to the download file manager.
if (need_update) {
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
NewRunnableMethod(download_file_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 (vector_size > kLoadsToWrite)
StartPauseTimer();
return true;
}
bool DownloadResourceHandler::OnResponseCompleted(
int request_id,
const net::URLRequestStatus& status,
const std::string& security_info) {
VLOG(20) << __FUNCTION__ << "()" << DebugString()
<< " request_id = " << request_id
<< " status.status() = " << status.status()
<< " status.error() = " << status.error();
net::Error error_code = net::OK;
if (status.status() == net::URLRequestStatus::FAILED)
error_code = static_cast<net::Error>(status.error()); // Normal case.
// ERR_CONNECTION_CLOSED is allowed since a number of servers in the wild
// advertise a larger Content-Length than the amount of bytes in the message
// body, and then close the connection. Other browsers - IE8, Firefox 4.0.1,
// and Safari 5.0.4 - treat the download as complete in this case, so we
// follow their lead.
if (error_code == net::ERR_CONNECTION_CLOSED)
error_code = net::OK;
InterruptReason reason =
ConvertNetErrorToInterruptReason(error_code,
DOWNLOAD_INTERRUPT_FROM_NETWORK);
if ((status.status() == net::URLRequestStatus::CANCELED) &&
(status.error() == net::ERR_ABORTED)) {
// TODO(ahendrickson) -- Find a better set of codes to use here, as
// CANCELED/ERR_ABORTED can occur for reasons other than user cancel.
reason = DOWNLOAD_INTERRUPT_REASON_USER_CANCELED; // User canceled.
}
if (!download_id_.IsValid())
CallStartedCB(error_code);
// We transfer ownership to |DownloadFileManager| to delete |buffer_|,
// so that any functions queued up on the FILE thread are executed
// before deletion.
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
NewRunnableMethod(download_file_manager_,
&DownloadFileManager::OnResponseCompleted,
download_id_,
reason,
security_info));
buffer_ = NULL; // The buffer is longer needed by |DownloadResourceHandler|.
read_buffer_ = NULL;
return true;
}
void DownloadResourceHandler::OnRequestClosed() {
UMA_HISTOGRAM_TIMES("SB2.DownloadDuration",
base::TimeTicks::Now() - download_start_time_);
}
// 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_.get())
return; // The download completed while we were waiting to run.
size_t contents_size = buffer_->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;
}
}
DownloadResourceHandler::~DownloadResourceHandler() {
}
void DownloadResourceHandler::StartPauseTimer() {
if (!pause_timer_.IsRunning())
pause_timer_.Start(FROM_HERE,
base::TimeDelta::FromMilliseconds(kThrottleTimeMs), this,
&DownloadResourceHandler::CheckWriteProgress);
}
std::string DownloadResourceHandler::DebugString() const {
return base::StringPrintf("{"
" url_ = " "\"%s\""
" download_id_ = " "%d"
" global_id_ = {"
" child_id = " "%d"
" request_id = " "%d"
" }"
" render_view_id_ = " "%d"
" save_info_.file_path = \"%" PRFilePath "\""
" }",
request_->url().spec().c_str(),
download_id_.local(),
global_id_.child_id,
global_id_.request_id,
render_view_id_,
save_info_.file_path.value().c_str());
}
|