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/strings/stringprintf.h"
14 #include "content/browser/byte_stream.h"
15 #include "content/browser/download/download_create_info.h"
16 #include "content/browser/download/download_interrupt_reasons_impl.h"
17 #include "content/browser/download/download_manager_impl.h"
18 #include "content/browser/download/download_request_handle.h"
19 #include "content/browser/download/download_stats.h"
20 #include "content/browser/loader/resource_dispatcher_host_impl.h"
21 #include "content/browser/loader/resource_request_info_impl.h"
22 #include "content/public/browser/browser_thread.h"
23 #include "content/public/browser/download_interrupt_reasons.h"
24 #include "content/public/browser/download_item.h"
25 #include "content/public/browser/download_manager_delegate.h"
26 #include "content/public/browser/navigation_entry.h"
27 #include "content/public/browser/power_save_blocker.h"
28 #include "content/public/browser/web_contents.h"
29 #include "content/public/common/resource_response.h"
30 #include "net/base/io_buffer.h"
31 #include "net/base/net_errors.h"
32 #include "net/http/http_response_headers.h"
33 #include "net/http/http_status_code.h"
34 #include "net/url_request/url_request_context.h"
38 struct DownloadResourceHandler::DownloadTabInfo
{
40 GURL tab_referrer_url
;
45 void CallStartedCBOnUIThread(
46 const DownloadUrlParameters::OnStartedCallback
& started_cb
,
48 DownloadInterruptReason interrupt_reason
) {
49 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
51 if (started_cb
.is_null())
53 started_cb
.Run(item
, interrupt_reason
);
56 // Static function in order to prevent any accidental accesses to
57 // DownloadResourceHandler members from the UI thread.
58 static void StartOnUIThread(
59 scoped_ptr
<DownloadCreateInfo
> info
,
60 DownloadResourceHandler::DownloadTabInfo
* tab_info
,
61 scoped_ptr
<ByteStreamReader
> stream
,
62 const DownloadUrlParameters::OnStartedCallback
& started_cb
) {
63 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
65 DownloadManager
* download_manager
= info
->request_handle
.GetDownloadManager();
66 if (!download_manager
) {
67 // NULL in unittests or if the page closed right after starting the
69 if (!started_cb
.is_null())
70 started_cb
.Run(NULL
, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED
);
72 // |stream| gets deleted on non-FILE thread, but it's ok since
73 // we're not using stream_writer_ yet.
78 info
->tab_url
= tab_info
->tab_url
;
79 info
->tab_referrer_url
= tab_info
->tab_referrer_url
;
81 download_manager
->StartDownload(info
.Pass(), stream
.Pass(), started_cb
);
84 void InitializeDownloadTabInfoOnUIThread(
85 const DownloadRequestHandle
& request_handle
,
86 DownloadResourceHandler::DownloadTabInfo
* tab_info
) {
87 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
89 WebContents
* web_contents
= request_handle
.GetWebContents();
91 NavigationEntry
* entry
= web_contents
->GetController().GetVisibleEntry();
93 tab_info
->tab_url
= entry
->GetURL();
94 tab_info
->tab_referrer_url
= entry
->GetReferrer().url
;
101 const int DownloadResourceHandler::kDownloadByteStreamSize
= 100 * 1024;
103 DownloadResourceHandler::DownloadResourceHandler(
105 net::URLRequest
* request
,
106 const DownloadUrlParameters::OnStartedCallback
& started_cb
,
107 scoped_ptr
<DownloadSaveInfo
> save_info
)
108 : ResourceHandler(request
),
110 started_cb_(started_cb
),
111 save_info_(save_info
.Pass()),
112 last_buffer_size_(0),
115 was_deferred_(false),
116 on_response_started_called_(false) {
117 RecordDownloadCount(UNTHROTTLED_COUNT
);
119 // Do UI thread initialization asap after DownloadResourceHandler creation
120 // since the tab could be navigated before StartOnUIThread gets called.
121 const ResourceRequestInfoImpl
* request_info
= GetRequestInfo();
122 tab_info_
= new DownloadTabInfo();
123 BrowserThread::PostTask(
126 base::Bind(&InitializeDownloadTabInfoOnUIThread
,
127 DownloadRequestHandle(AsWeakPtr(),
128 request_info
->GetChildID(),
129 request_info
->GetRouteID(),
130 request_info
->GetRequestID()),
132 power_save_blocker_
= PowerSaveBlocker::Create(
133 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension
,
134 PowerSaveBlocker::kReasonOther
, "Download in progress");
137 bool DownloadResourceHandler::OnUploadProgress(uint64 position
,
142 bool DownloadResourceHandler::OnRequestRedirected(
143 const net::RedirectInfo
& redirect_info
,
144 ResourceResponse
* response
,
149 // Send the download creation information to the download thread.
150 bool DownloadResourceHandler::OnResponseStarted(
151 ResourceResponse
* response
,
153 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
154 // There can be only one (call)
155 DCHECK(!on_response_started_called_
);
156 on_response_started_called_
= true;
158 DVLOG(20) << __FUNCTION__
<< "()" << DebugString();
159 download_start_time_
= base::TimeTicks::Now();
161 // If it's a download, we don't want to poison the cache with it.
162 request()->StopCaching();
164 // Lower priority as well, so downloads don't contend for resources
166 request()->SetPriority(net::IDLE
);
168 // If the content-length header is not present (or contains something other
169 // than numbers), the incoming content_length is -1 (unknown size).
170 // Set the content length to 0 to indicate unknown size to DownloadManager.
171 int64 content_length
=
172 response
->head
.content_length
> 0 ? response
->head
.content_length
: 0;
174 const ResourceRequestInfoImpl
* request_info
= GetRequestInfo();
176 // Deleted in DownloadManager.
177 scoped_ptr
<DownloadCreateInfo
> info(
178 new DownloadCreateInfo(base::Time::Now(),
180 request()->net_log(),
181 request_info
->HasUserGesture(),
182 request_info
->GetPageTransition(),
185 // Create the ByteStream for sending data to the download sink.
186 scoped_ptr
<ByteStreamReader
> stream_reader
;
188 base::MessageLoopProxy::current(),
189 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
190 kDownloadByteStreamSize
, &stream_writer_
, &stream_reader
);
191 stream_writer_
->RegisterCallback(
192 base::Bind(&DownloadResourceHandler::ResumeRequest
, AsWeakPtr()));
194 info
->download_id
= download_id_
;
195 info
->url_chain
= request()->url_chain();
196 info
->referrer_url
= GURL(request()->referrer());
197 info
->mime_type
= response
->head
.mime_type
;
198 info
->remote_address
= request()->GetSocketAddress().host();
199 request()->GetResponseHeaderByName("content-disposition",
200 &info
->content_disposition
);
201 RecordDownloadMimeType(info
->mime_type
);
202 RecordDownloadContentDisposition(info
->content_disposition
);
204 info
->request_handle
=
205 DownloadRequestHandle(AsWeakPtr(), request_info
->GetChildID(),
206 request_info
->GetRouteID(),
207 request_info
->GetRequestID());
209 // Get the last modified time and etag.
210 const net::HttpResponseHeaders
* headers
= request()->response_headers();
212 if (headers
->HasStrongValidators()) {
213 // If we don't have strong validators as per RFC 2616 section 13.3.3, then
214 // we neither store nor use them for range requests.
215 if (!headers
->EnumerateHeader(NULL
, "Last-Modified",
216 &info
->last_modified
))
217 info
->last_modified
.clear();
218 if (!headers
->EnumerateHeader(NULL
, "ETag", &info
->etag
))
222 int status
= headers
->response_code();
223 if (2 == status
/ 100 && status
!= net::HTTP_PARTIAL_CONTENT
) {
224 // Success & not range response; if we asked for a range, we didn't
225 // get it--reset the file pointers to reflect that.
226 info
->save_info
->offset
= 0;
227 info
->save_info
->hash_state
= "";
230 if (!headers
->GetMimeType(&info
->original_mime_type
))
231 info
->original_mime_type
.clear();
234 // Blink verifies that the requester of this download is allowed to set a
235 // suggested name for the security origin of the downlaod URL. However, this
236 // assumption doesn't hold if there were cross origin redirects. Therefore,
237 // clear the suggested_name for such requests.
238 if (info
->url_chain
.size() > 1 &&
239 info
->url_chain
.front().GetOrigin() != info
->url_chain
.back().GetOrigin())
240 info
->save_info
->suggested_name
.clear();
242 BrowserThread::PostTask(
243 BrowserThread::UI
, FROM_HERE
,
244 base::Bind(&StartOnUIThread
,
246 base::Owned(tab_info_
),
247 base::Passed(&stream_reader
),
248 // Pass to StartOnUIThread so that variable
249 // access is always on IO thread but function
250 // is called on UI thread.
252 // Now owned by the task that was just posted.
254 // Guaranteed to be called in StartOnUIThread
260 void DownloadResourceHandler::CallStartedCB(
262 DownloadInterruptReason interrupt_reason
) {
263 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
264 if (started_cb_
.is_null())
266 BrowserThread::PostTask(
270 &CallStartedCBOnUIThread
, started_cb_
, item
, interrupt_reason
));
274 bool DownloadResourceHandler::OnWillStart(const GURL
& url
, bool* defer
) {
278 bool DownloadResourceHandler::OnBeforeNetworkStart(const GURL
& url
,
283 // Create a new buffer, which will be handed to the download thread for file
284 // writing and deletion.
285 bool DownloadResourceHandler::OnWillRead(scoped_refptr
<net::IOBuffer
>* buf
,
288 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
289 DCHECK(buf
&& buf_size
);
290 DCHECK(!read_buffer_
.get());
292 *buf_size
= min_size
< 0 ? kReadBufSize
: min_size
;
293 last_buffer_size_
= *buf_size
;
294 read_buffer_
= new net::IOBuffer(*buf_size
);
295 *buf
= read_buffer_
.get();
299 // Pass the buffer to the download file writer.
300 bool DownloadResourceHandler::OnReadCompleted(int bytes_read
, bool* defer
) {
301 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
302 DCHECK(read_buffer_
.get());
304 base::TimeTicks
now(base::TimeTicks::Now());
305 if (!last_read_time_
.is_null()) {
306 double seconds_since_last_read
= (now
- last_read_time_
).InSecondsF();
307 if (now
== last_read_time_
)
308 // Use 1/10 ms as a "very small number" so that we avoid
309 // divide-by-zero error and still record a very high potential bandwidth.
310 seconds_since_last_read
= 0.00001;
312 double actual_bandwidth
= (bytes_read
)/seconds_since_last_read
;
313 double potential_bandwidth
= last_buffer_size_
/seconds_since_last_read
;
314 RecordBandwidth(actual_bandwidth
, potential_bandwidth
);
316 last_read_time_
= now
;
320 bytes_read_
+= bytes_read
;
321 DCHECK(read_buffer_
.get());
323 // Take the data ship it down the stream. If the stream is full, pause the
324 // request; the stream callback will resume it.
325 if (!stream_writer_
->Write(read_buffer_
, bytes_read
)) {
327 *defer
= was_deferred_
= true;
328 last_stream_pause_time_
= now
;
331 read_buffer_
= NULL
; // Drop our reference.
333 if (pause_count_
> 0)
334 *defer
= was_deferred_
= true;
339 void DownloadResourceHandler::OnResponseCompleted(
340 const net::URLRequestStatus
& status
,
341 const std::string
& security_info
,
343 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
344 int response_code
= status
.is_success() ? request()->GetResponseCode() : 0;
345 DVLOG(20) << __FUNCTION__
<< "()" << DebugString()
346 << " status.status() = " << status
.status()
347 << " status.error() = " << status
.error()
348 << " response_code = " << response_code
;
350 net::Error error_code
= net::OK
;
351 if (status
.status() == net::URLRequestStatus::FAILED
||
352 // Note cancels as failures too.
353 status
.status() == net::URLRequestStatus::CANCELED
) {
354 error_code
= static_cast<net::Error
>(status
.error()); // Normal case.
355 // Make sure that at least the fact of failure comes through.
356 if (error_code
== net::OK
)
357 error_code
= net::ERR_FAILED
;
360 // ERR_CONTENT_LENGTH_MISMATCH and ERR_INCOMPLETE_CHUNKED_ENCODING are
361 // allowed since a number of servers in the wild close the connection too
362 // early by mistake. Other browsers - IE9, Firefox 11.0, and Safari 5.1.4 -
363 // treat downloads as complete in both cases, so we follow their lead.
364 if (error_code
== net::ERR_CONTENT_LENGTH_MISMATCH
||
365 error_code
== net::ERR_INCOMPLETE_CHUNKED_ENCODING
) {
366 error_code
= net::OK
;
368 DownloadInterruptReason reason
=
369 ConvertNetErrorToInterruptReason(
370 error_code
, DOWNLOAD_INTERRUPT_FROM_NETWORK
);
372 if (status
.status() == net::URLRequestStatus::CANCELED
&&
373 status
.error() == net::ERR_ABORTED
) {
374 // CANCELED + ERR_ABORTED == something outside of the network
375 // stack cancelled the request. There aren't that many things that
376 // could do this to a download request (whose lifetime is separated from
377 // the tab from which it came). We map this to USER_CANCELLED as the
378 // case we know about (system suspend because of laptop close) corresponds
380 // TODO(ahendrickson) -- Find a better set of codes to use here, as
381 // CANCELED/ERR_ABORTED can occur for reasons other than user cancel.
382 if (net::IsCertStatusError(request()->ssl_info().cert_status
))
383 reason
= DOWNLOAD_INTERRUPT_REASON_SERVER_CERT_PROBLEM
;
385 reason
= DOWNLOAD_INTERRUPT_REASON_USER_CANCELED
;
388 if (status
.is_success() &&
389 reason
== DOWNLOAD_INTERRUPT_REASON_NONE
&&
390 request()->response_headers()) {
391 // Handle server's response codes.
392 switch(response_code
) {
393 case -1: // Non-HTTP request.
395 case net::HTTP_CREATED
:
396 case net::HTTP_ACCEPTED
:
397 case net::HTTP_NON_AUTHORITATIVE_INFORMATION
:
398 case net::HTTP_RESET_CONTENT
:
399 case net::HTTP_PARTIAL_CONTENT
:
400 // Expected successful codes.
402 case net::HTTP_NO_CONTENT
:
403 case net::HTTP_NOT_FOUND
:
404 reason
= DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT
;
406 case net::HTTP_PRECONDITION_FAILED
:
407 // Failed our 'If-Unmodified-Since' or 'If-Match'; see
408 // download_manager_impl.cc BeginDownload()
409 reason
= DOWNLOAD_INTERRUPT_REASON_SERVER_PRECONDITION
;
411 case net::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE
:
412 // Retry by downloading from the start automatically:
413 // If we haven't received data when we get this error, we won't.
414 reason
= DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE
;
416 case net::HTTP_UNAUTHORIZED
:
417 // Server didn't authorize this request.
418 reason
= DOWNLOAD_INTERRUPT_REASON_SERVER_UNAUTHORIZED
;
420 default: // All other errors.
421 // Redirection and informational codes should have been handled earlier
423 DCHECK_NE(3, response_code
/ 100);
424 DCHECK_NE(1, response_code
/ 100);
425 reason
= DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED
;
430 std::string accept_ranges
;
431 bool has_strong_validators
= false;
432 if (request()->response_headers()) {
433 request()->response_headers()->EnumerateHeader(
434 NULL
, "Accept-Ranges", &accept_ranges
);
435 has_strong_validators
=
436 request()->response_headers()->HasStrongValidators();
438 RecordAcceptsRanges(accept_ranges
, bytes_read_
, has_strong_validators
);
439 RecordNetworkBlockage(base::TimeTicks::Now() - download_start_time_
,
442 CallStartedCB(NULL
, reason
);
444 // Send the info down the stream. Conditional is in case we get
445 // OnResponseCompleted without OnResponseStarted.
447 stream_writer_
->Close(reason
);
449 // If the error mapped to something unknown, record it so that
450 // we can drill down.
451 if (reason
== DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED
) {
452 UMA_HISTOGRAM_CUSTOM_ENUMERATION("Download.MapErrorNetworkFailed",
453 std::abs(status
.error()),
454 net::GetAllErrorCodesForUma());
457 stream_writer_
.reset(); // We no longer need the stream.
461 void DownloadResourceHandler::OnDataDownloaded(int bytes_downloaded
) {
465 void DownloadResourceHandler::PauseRequest() {
466 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
471 void DownloadResourceHandler::ResumeRequest() {
472 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
473 DCHECK_LT(0, pause_count_
);
479 if (pause_count_
> 0)
482 was_deferred_
= false;
483 if (!last_stream_pause_time_
.is_null()) {
484 total_pause_time_
+= (base::TimeTicks::Now() - last_stream_pause_time_
);
485 last_stream_pause_time_
= base::TimeTicks();
488 controller()->Resume();
491 void DownloadResourceHandler::CancelRequest() {
492 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
494 const ResourceRequestInfo
* info
= GetRequestInfo();
495 ResourceDispatcherHostImpl::Get()->CancelRequest(
497 info
->GetRequestID());
498 // This object has been deleted.
501 std::string
DownloadResourceHandler::DebugString() const {
502 const ResourceRequestInfo
* info
= GetRequestInfo();
503 return base::StringPrintf("{"
507 " request_id = " "%d"
512 request()->url().spec().c_str() :
515 info
->GetRequestID(),
519 DownloadResourceHandler::~DownloadResourceHandler() {
520 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
522 // This won't do anything if the callback was called before.
523 // If it goes through, it will likely be because OnWillStart() returned
524 // false somewhere in the chain of resource handlers.
525 CallStartedCB(NULL
, DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED
);
527 // Remove output stream callback if a stream exists.
529 stream_writer_
->RegisterCallback(base::Closure());
531 // tab_info_ must be destroyed on UI thread, since
532 // InitializeDownloadTabInfoOnUIThread might still be using it.
534 BrowserThread::DeleteSoon(BrowserThread::UI
, FROM_HERE
, tab_info_
);
536 UMA_HISTOGRAM_TIMES("SB2.DownloadDuration",
537 base::TimeTicks::Now() - download_start_time_
);
540 } // namespace content