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 #include "content/browser/download/download_resource_handler.h"
10 #include "base/logging.h"
11 #include "base/message_loop/message_loop_proxy.h"
12 #include "base/metrics/histogram.h"
13 #include "base/metrics/stats_counters.h"
14 #include "base/strings/stringprintf.h"
15 #include "content/browser/byte_stream.h"
16 #include "content/browser/download/download_create_info.h"
17 #include "content/browser/download/download_interrupt_reasons_impl.h"
18 #include "content/browser/download/download_manager_impl.h"
19 #include "content/browser/download/download_request_handle.h"
20 #include "content/browser/download/download_stats.h"
21 #include "content/browser/loader/resource_dispatcher_host_impl.h"
22 #include "content/browser/loader/resource_request_info_impl.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/download_interrupt_reasons.h"
25 #include "content/public/browser/download_item.h"
26 #include "content/public/browser/download_manager_delegate.h"
27 #include "content/public/common/resource_response.h"
28 #include "net/base/io_buffer.h"
29 #include "net/base/net_errors.h"
30 #include "net/http/http_response_headers.h"
31 #include "net/http/http_status_code.h"
32 #include "net/url_request/url_request_context.h"
37 void CallStartedCBOnUIThread(
38 const DownloadResourceHandler::OnStartedCallback
& started_cb
,
41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
43 if (started_cb
.is_null())
45 started_cb
.Run(item
, error
);
48 // Static function in order to prevent any accidental accesses to
49 // DownloadResourceHandler members from the UI thread.
50 static void StartOnUIThread(
51 scoped_ptr
<DownloadCreateInfo
> info
,
52 scoped_ptr
<ByteStreamReader
> stream
,
53 const DownloadResourceHandler::OnStartedCallback
& started_cb
) {
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
56 DownloadManager
* download_manager
= info
->request_handle
.GetDownloadManager();
57 if (!download_manager
) {
58 // NULL in unittests or if the page closed right after starting the
60 if (!started_cb
.is_null())
61 started_cb
.Run(NULL
, net::ERR_ACCESS_DENIED
);
65 DownloadItem
* item
= download_manager
->StartDownload(
66 info
.Pass(), stream
.Pass());
68 // |item| can be NULL if the download has been removed.
69 if (!started_cb
.is_null())
70 started_cb
.Run(item
, item
? net::OK
: net::ERR_ABORTED
);
75 const int DownloadResourceHandler::kDownloadByteStreamSize
= 100 * 1024;
77 DownloadResourceHandler::DownloadResourceHandler(
79 net::URLRequest
* request
,
80 const DownloadResourceHandler::OnStartedCallback
& started_cb
,
81 scoped_ptr
<DownloadSaveInfo
> save_info
)
83 render_view_id_(0), // Actually initialized below.
86 started_cb_(started_cb
),
87 save_info_(save_info
.Pass()),
92 on_response_started_called_(false) {
93 ResourceRequestInfoImpl
* info(ResourceRequestInfoImpl::ForRequest(request
));
94 global_id_
= info
->GetGlobalRequestID();
95 render_view_id_
= info
->GetRouteID();
97 RecordDownloadCount(UNTHROTTLED_COUNT
);
100 bool DownloadResourceHandler::OnUploadProgress(int request_id
,
106 bool DownloadResourceHandler::OnRequestRedirected(
109 ResourceResponse
* response
,
114 // Send the download creation information to the download thread.
115 bool DownloadResourceHandler::OnResponseStarted(
117 ResourceResponse
* response
,
119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
120 // There can be only one (call)
121 DCHECK(!on_response_started_called_
);
122 on_response_started_called_
= true;
124 VLOG(20) << __FUNCTION__
<< "()" << DebugString()
125 << " request_id = " << request_id
;
126 download_start_time_
= base::TimeTicks::Now();
128 // If it's a download, we don't want to poison the cache with it.
129 request_
->StopCaching();
131 // Lower priority as well, so downloads don't contend for resources
133 request_
->SetPriority(net::IDLE
);
135 std::string content_disposition
;
136 request_
->GetResponseHeaderByName("content-disposition",
137 &content_disposition
);
138 SetContentDisposition(content_disposition
);
139 SetContentLength(response
->head
.content_length
);
141 const ResourceRequestInfoImpl
* request_info
=
142 ResourceRequestInfoImpl::ForRequest(request_
);
144 // Deleted in DownloadManager.
145 scoped_ptr
<DownloadCreateInfo
> info(new DownloadCreateInfo(
146 base::Time::Now(), content_length_
,
147 request_
->net_log(), request_info
->HasUserGesture(),
148 request_info
->GetPageTransition()));
150 // Create the ByteStream for sending data to the download sink.
151 scoped_ptr
<ByteStreamReader
> stream_reader
;
153 base::MessageLoopProxy::current(),
154 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
155 kDownloadByteStreamSize
, &stream_writer_
, &stream_reader
);
156 stream_writer_
->RegisterCallback(
157 base::Bind(&DownloadResourceHandler::ResumeRequest
, AsWeakPtr()));
159 info
->download_id
= download_id_
;
160 info
->url_chain
= request_
->url_chain();
161 info
->referrer_url
= GURL(request_
->referrer());
162 info
->start_time
= base::Time::Now();
163 info
->total_bytes
= content_length_
;
164 info
->has_user_gesture
= request_info
->HasUserGesture();
165 info
->content_disposition
= content_disposition_
;
166 info
->mime_type
= response
->head
.mime_type
;
167 info
->remote_address
= request_
->GetSocketAddress().host();
168 RecordDownloadMimeType(info
->mime_type
);
169 RecordDownloadContentDisposition(info
->content_disposition
);
171 info
->request_handle
=
172 DownloadRequestHandle(AsWeakPtr(), global_id_
.child_id
,
173 render_view_id_
, global_id_
.request_id
);
175 // Get the last modified time and etag.
176 const net::HttpResponseHeaders
* headers
= request_
->response_headers();
178 std::string last_modified_hdr
;
180 if (headers
->EnumerateHeader(NULL
, "Last-Modified", &last_modified_hdr
))
181 info
->last_modified
= last_modified_hdr
;
182 if (headers
->EnumerateHeader(NULL
, "ETag", &etag
))
185 int status
= headers
->response_code();
186 if (2 == status
/ 100 && status
!= net::HTTP_PARTIAL_CONTENT
) {
187 // Success & not range response; if we asked for a range, we didn't
188 // get it--reset the file pointers to reflect that.
189 save_info_
->offset
= 0;
190 save_info_
->hash_state
= "";
194 std::string content_type_header
;
195 if (!response
->head
.headers
.get() ||
196 !response
->head
.headers
->GetMimeType(&content_type_header
))
197 content_type_header
= "";
198 info
->original_mime_type
= content_type_header
;
200 if (!response
->head
.headers
.get() ||
201 !response
->head
.headers
->EnumerateHeader(
202 NULL
, "Accept-Ranges", &accept_ranges_
)) {
206 info
->save_info
= save_info_
.Pass();
208 BrowserThread::PostTask(
209 BrowserThread::UI
, FROM_HERE
,
210 base::Bind(&StartOnUIThread
,
212 base::Passed(&stream_reader
),
213 // Pass to StartOnUIThread so that variable
214 // access is always on IO thread but function
215 // is called on UI thread.
217 // Guaranteed to be called in StartOnUIThread
223 void DownloadResourceHandler::CallStartedCB(
224 DownloadItem
* item
, net::Error error
) {
225 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
226 if (started_cb_
.is_null())
228 BrowserThread::PostTask(
229 BrowserThread::UI
, FROM_HERE
,
230 base::Bind(&CallStartedCBOnUIThread
, started_cb_
, item
, error
));
234 bool DownloadResourceHandler::OnWillStart(int request_id
,
240 // Create a new buffer, which will be handed to the download thread for file
241 // writing and deletion.
242 bool DownloadResourceHandler::OnWillRead(int request_id
, net::IOBuffer
** buf
,
243 int* buf_size
, int min_size
) {
244 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
245 DCHECK(buf
&& buf_size
);
246 DCHECK(!read_buffer_
.get());
248 *buf_size
= min_size
< 0 ? kReadBufSize
: min_size
;
249 last_buffer_size_
= *buf_size
;
250 read_buffer_
= new net::IOBuffer(*buf_size
);
251 *buf
= read_buffer_
.get();
255 // Pass the buffer to the download file writer.
256 bool DownloadResourceHandler::OnReadCompleted(int request_id
, int bytes_read
,
258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
259 DCHECK(read_buffer_
.get());
261 base::TimeTicks
now(base::TimeTicks::Now());
262 if (!last_read_time_
.is_null()) {
263 double seconds_since_last_read
= (now
- last_read_time_
).InSecondsF();
264 if (now
== last_read_time_
)
265 // Use 1/10 ms as a "very small number" so that we avoid
266 // divide-by-zero error and still record a very high potential bandwidth.
267 seconds_since_last_read
= 0.00001;
269 double actual_bandwidth
= (bytes_read
)/seconds_since_last_read
;
270 double potential_bandwidth
= last_buffer_size_
/seconds_since_last_read
;
271 RecordBandwidth(actual_bandwidth
, potential_bandwidth
);
273 last_read_time_
= now
;
277 bytes_read_
+= bytes_read
;
278 DCHECK(read_buffer_
.get());
280 // Take the data ship it down the stream. If the stream is full, pause the
281 // request; the stream callback will resume it.
282 if (!stream_writer_
->Write(read_buffer_
, bytes_read
)) {
284 *defer
= was_deferred_
= true;
285 last_stream_pause_time_
= now
;
288 read_buffer_
= NULL
; // Drop our reference.
290 if (pause_count_
> 0)
291 *defer
= was_deferred_
= true;
296 bool DownloadResourceHandler::OnResponseCompleted(
298 const net::URLRequestStatus
& status
,
299 const std::string
& security_info
) {
300 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
301 int response_code
= status
.is_success() ? request_
->GetResponseCode() : 0;
302 VLOG(20) << __FUNCTION__
<< "()" << DebugString()
303 << " request_id = " << request_id
304 << " status.status() = " << status
.status()
305 << " status.error() = " << status
.error()
306 << " response_code = " << response_code
;
308 net::Error error_code
= net::OK
;
309 if (status
.status() == net::URLRequestStatus::FAILED
||
310 // Note cancels as failures too.
311 status
.status() == net::URLRequestStatus::CANCELED
) {
312 error_code
= static_cast<net::Error
>(status
.error()); // Normal case.
313 // Make sure that at least the fact of failure comes through.
314 if (error_code
== net::OK
)
315 error_code
= net::ERR_FAILED
;
318 // ERR_CONTENT_LENGTH_MISMATCH and ERR_INCOMPLETE_CHUNKED_ENCODING are
319 // allowed since a number of servers in the wild close the connection too
320 // early by mistake. Other browsers - IE9, Firefox 11.0, and Safari 5.1.4 -
321 // treat downloads as complete in both cases, so we follow their lead.
322 if (error_code
== net::ERR_CONTENT_LENGTH_MISMATCH
||
323 error_code
== net::ERR_INCOMPLETE_CHUNKED_ENCODING
) {
324 error_code
= net::OK
;
326 DownloadInterruptReason reason
=
327 ConvertNetErrorToInterruptReason(
328 error_code
, DOWNLOAD_INTERRUPT_FROM_NETWORK
);
330 if (status
.status() == net::URLRequestStatus::CANCELED
&&
331 status
.error() == net::ERR_ABORTED
) {
332 // CANCELED + ERR_ABORTED == something outside of the network
333 // stack cancelled the request. There aren't that many things that
334 // could do this to a download request (whose lifetime is separated from
335 // the tab from which it came). We map this to USER_CANCELLED as the
336 // case we know about (system suspend because of laptop close) corresponds
338 // TODO(ahendrickson) -- Find a better set of codes to use here, as
339 // CANCELED/ERR_ABORTED can occur for reasons other than user cancel.
340 reason
= DOWNLOAD_INTERRUPT_REASON_USER_CANCELED
;
343 if (status
.is_success() &&
344 reason
== DOWNLOAD_INTERRUPT_REASON_NONE
&&
345 request_
->response_headers()) {
346 // Handle server's response codes.
347 switch(response_code
) {
348 case -1: // Non-HTTP request.
350 case net::HTTP_CREATED
:
351 case net::HTTP_ACCEPTED
:
352 case net::HTTP_NON_AUTHORITATIVE_INFORMATION
:
353 case net::HTTP_RESET_CONTENT
:
354 case net::HTTP_PARTIAL_CONTENT
:
355 // Expected successful codes.
357 case net::HTTP_NO_CONTENT
:
358 case net::HTTP_NOT_FOUND
:
359 reason
= DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT
;
361 case net::HTTP_PRECONDITION_FAILED
:
362 // Failed our 'If-Unmodified-Since' or 'If-Match'; see
363 // download_manager_impl.cc BeginDownload()
364 reason
= DOWNLOAD_INTERRUPT_REASON_SERVER_PRECONDITION
;
366 case net::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE
:
367 // Retry by downloading from the start automatically:
368 // If we haven't received data when we get this error, we won't.
369 reason
= DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE
;
371 default: // All other errors.
372 // Redirection and informational codes should have been handled earlier
374 DCHECK_NE(3, response_code
/ 100);
375 DCHECK_NE(1, response_code
/ 100);
376 reason
= DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED
;
381 RecordAcceptsRanges(accept_ranges_
, bytes_read_
);
382 RecordNetworkBlockage(
383 base::TimeTicks::Now() - download_start_time_
, total_pause_time_
);
385 CallStartedCB(NULL
, error_code
);
387 // Send the info down the stream. Conditional is in case we get
388 // OnResponseCompleted without OnResponseStarted.
390 stream_writer_
->Close(reason
);
392 // If the error mapped to something unknown, record it so that
393 // we can drill down.
394 if (reason
== DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED
) {
395 UMA_HISTOGRAM_CUSTOM_ENUMERATION("Download.MapErrorNetworkFailed",
396 std::abs(status
.error()),
397 net::GetAllErrorCodesForUma());
400 stream_writer_
.reset(); // We no longer need the stream.
406 void DownloadResourceHandler::OnDataDownloaded(
408 int bytes_downloaded
) {
412 // If the content-length header is not present (or contains something other
413 // than numbers), the incoming content_length is -1 (unknown size).
414 // Set the content length to 0 to indicate unknown size to DownloadManager.
415 void DownloadResourceHandler::SetContentLength(const int64
& content_length
) {
416 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
418 if (content_length
> 0)
419 content_length_
= content_length
;
422 void DownloadResourceHandler::SetContentDisposition(
423 const std::string
& content_disposition
) {
424 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
425 content_disposition_
= content_disposition
;
428 void DownloadResourceHandler::PauseRequest() {
429 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
434 void DownloadResourceHandler::ResumeRequest() {
435 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
436 DCHECK_LT(0, pause_count_
);
442 if (pause_count_
> 0)
445 was_deferred_
= false;
446 if (!last_stream_pause_time_
.is_null()) {
447 total_pause_time_
+= (base::TimeTicks::Now() - last_stream_pause_time_
);
448 last_stream_pause_time_
= base::TimeTicks();
451 controller()->Resume();
454 void DownloadResourceHandler::CancelRequest() {
455 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
457 ResourceDispatcherHostImpl::Get()->CancelRequest(
459 global_id_
.request_id
,
463 std::string
DownloadResourceHandler::DebugString() const {
464 return base::StringPrintf("{"
468 " request_id = " "%d"
470 " render_view_id_ = " "%d"
473 request_
->url().spec().c_str() :
476 global_id_
.request_id
,
480 DownloadResourceHandler::~DownloadResourceHandler() {
481 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
483 // This won't do anything if the callback was called before.
484 // If it goes through, it will likely be because OnWillStart() returned
485 // false somewhere in the chain of resource handlers.
486 CallStartedCB(NULL
, net::ERR_ACCESS_DENIED
);
488 // Remove output stream callback if a stream exists.
490 stream_writer_
->RegisterCallback(base::Closure());
492 UMA_HISTOGRAM_TIMES("SB2.DownloadDuration",
493 base::TimeTicks::Now() - download_start_time_
);
496 } // namespace content