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/location.h"
11 #include "base/logging.h"
12 #include "base/metrics/histogram_macros.h"
13 #include "base/metrics/sparse_histogram.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/thread_task_runner_handle.h"
17 #include "content/browser/byte_stream.h"
18 #include "content/browser/download/download_create_info.h"
19 #include "content/browser/download/download_interrupt_reasons_impl.h"
20 #include "content/browser/download/download_manager_impl.h"
21 #include "content/browser/download/download_request_handle.h"
22 #include "content/browser/download/download_stats.h"
23 #include "content/browser/loader/resource_dispatcher_host_impl.h"
24 #include "content/browser/loader/resource_request_info_impl.h"
25 #include "content/public/browser/browser_thread.h"
26 #include "content/public/browser/download_interrupt_reasons.h"
27 #include "content/public/browser/download_item.h"
28 #include "content/public/browser/download_manager_delegate.h"
29 #include "content/public/browser/navigation_entry.h"
30 #include "content/public/browser/power_save_blocker.h"
31 #include "content/public/browser/web_contents.h"
32 #include "content/public/common/resource_response.h"
33 #include "net/base/io_buffer.h"
34 #include "net/base/net_errors.h"
35 #include "net/http/http_response_headers.h"
36 #include "net/http/http_status_code.h"
37 #include "net/url_request/url_request_context.h"
41 struct DownloadResourceHandler::DownloadTabInfo
{
43 GURL tab_referrer_url
;
48 void CallStartedCBOnUIThread(
49 const DownloadUrlParameters::OnStartedCallback
& started_cb
,
51 DownloadInterruptReason interrupt_reason
) {
52 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
54 if (started_cb
.is_null())
56 started_cb
.Run(item
, interrupt_reason
);
59 // Static function in order to prevent any accidental accesses to
60 // DownloadResourceHandler members from the UI thread.
61 static void StartOnUIThread(
62 scoped_ptr
<DownloadCreateInfo
> info
,
63 scoped_ptr
<DownloadResourceHandler::DownloadTabInfo
> tab_info
,
64 scoped_ptr
<ByteStreamReader
> stream
,
65 const DownloadUrlParameters::OnStartedCallback
& started_cb
) {
66 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
68 DownloadManager
* download_manager
= info
->request_handle
.GetDownloadManager();
69 if (!download_manager
) {
70 // NULL in unittests or if the page closed right after starting the
72 if (!started_cb
.is_null())
73 started_cb
.Run(NULL
, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED
);
75 // |stream| gets deleted on non-FILE thread, but it's ok since
76 // we're not using stream_writer_ yet.
81 info
->tab_url
= tab_info
->tab_url
;
82 info
->tab_referrer_url
= tab_info
->tab_referrer_url
;
84 download_manager
->StartDownload(info
.Pass(), stream
.Pass(), started_cb
);
87 void InitializeDownloadTabInfoOnUIThread(
88 const DownloadRequestHandle
& request_handle
,
89 DownloadResourceHandler::DownloadTabInfo
* tab_info
) {
90 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
92 WebContents
* web_contents
= request_handle
.GetWebContents();
94 NavigationEntry
* entry
= web_contents
->GetController().GetVisibleEntry();
96 tab_info
->tab_url
= entry
->GetURL();
97 tab_info
->tab_referrer_url
= entry
->GetReferrer().url
;
102 void DeleteOnUIThread(
103 scoped_ptr
<DownloadResourceHandler::DownloadTabInfo
> tab_info
) {}
107 const int DownloadResourceHandler::kDownloadByteStreamSize
= 100 * 1024;
109 DownloadResourceHandler::DownloadResourceHandler(
111 net::URLRequest
* request
,
112 const DownloadUrlParameters::OnStartedCallback
& started_cb
,
113 scoped_ptr
<DownloadSaveInfo
> save_info
)
114 : ResourceHandler(request
),
116 started_cb_(started_cb
),
117 save_info_(save_info
.Pass()),
118 tab_info_(new DownloadTabInfo()),
119 last_buffer_size_(0),
122 was_deferred_(false),
123 on_response_started_called_(false) {
124 RecordDownloadCount(UNTHROTTLED_COUNT
);
126 // Do UI thread initialization for tab_info_ asap after
127 // DownloadResourceHandler creation since the tab could be navigated
128 // before StartOnUIThread gets called. This is safe because deletion
129 // will occur via PostTask() as well, which will serialized behind this
131 const ResourceRequestInfoImpl
* request_info
= GetRequestInfo();
132 BrowserThread::PostTask(
133 BrowserThread::UI
, FROM_HERE
,
134 base::Bind(&InitializeDownloadTabInfoOnUIThread
,
135 DownloadRequestHandle(AsWeakPtr(), request_info
->GetChildID(),
136 request_info
->GetRouteID(),
137 request_info
->GetRequestID(),
138 request_info
->frame_tree_node_id()),
140 power_save_blocker_
= PowerSaveBlocker::Create(
141 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension
,
142 PowerSaveBlocker::kReasonOther
, "Download in progress");
145 bool DownloadResourceHandler::OnUploadProgress(uint64 position
,
150 bool DownloadResourceHandler::OnRequestRedirected(
151 const net::RedirectInfo
& redirect_info
,
152 ResourceResponse
* response
,
157 // Send the download creation information to the download thread.
158 bool DownloadResourceHandler::OnResponseStarted(
159 ResourceResponse
* response
,
161 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
162 // There can be only one (call)
163 DCHECK(!on_response_started_called_
);
164 on_response_started_called_
= true;
166 DVLOG(20) << __FUNCTION__
<< "()" << DebugString();
167 download_start_time_
= base::TimeTicks::Now();
169 // If it's a download, we don't want to poison the cache with it.
170 request()->StopCaching();
172 // Lower priority as well, so downloads don't contend for resources
174 request()->SetPriority(net::IDLE
);
176 // If the content-length header is not present (or contains something other
177 // than numbers), the incoming content_length is -1 (unknown size).
178 // Set the content length to 0 to indicate unknown size to DownloadManager.
179 int64 content_length
=
180 response
->head
.content_length
> 0 ? response
->head
.content_length
: 0;
182 const ResourceRequestInfoImpl
* request_info
= GetRequestInfo();
184 // Deleted in DownloadManager.
185 scoped_ptr
<DownloadCreateInfo
> info(
186 new DownloadCreateInfo(base::Time::Now(),
188 request()->net_log(),
189 request_info
->HasUserGesture(),
190 request_info
->GetPageTransition(),
193 // Create the ByteStream for sending data to the download sink.
194 scoped_ptr
<ByteStreamReader
> stream_reader
;
196 base::ThreadTaskRunnerHandle::Get(),
197 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
198 kDownloadByteStreamSize
, &stream_writer_
, &stream_reader
);
199 stream_writer_
->RegisterCallback(
200 base::Bind(&DownloadResourceHandler::ResumeRequest
, AsWeakPtr()));
202 info
->download_id
= download_id_
;
203 info
->url_chain
= request()->url_chain();
204 info
->referrer_url
= GURL(request()->referrer());
205 info
->mime_type
= response
->head
.mime_type
;
206 info
->remote_address
= request()->GetSocketAddress().host();
207 request()->GetResponseHeaderByName("content-disposition",
208 &info
->content_disposition
);
209 RecordDownloadMimeType(info
->mime_type
);
210 RecordDownloadContentDisposition(info
->content_disposition
);
212 info
->request_handle
= DownloadRequestHandle(
213 AsWeakPtr(), request_info
->GetChildID(), request_info
->GetRouteID(),
214 request_info
->GetRequestID(), request_info
->frame_tree_node_id());
216 // Get the last modified time and etag.
217 const net::HttpResponseHeaders
* headers
= request()->response_headers();
219 if (headers
->HasStrongValidators()) {
220 // If we don't have strong validators as per RFC 2616 section 13.3.3, then
221 // we neither store nor use them for range requests.
222 if (!headers
->EnumerateHeader(NULL
, "Last-Modified",
223 &info
->last_modified
))
224 info
->last_modified
.clear();
225 if (!headers
->EnumerateHeader(NULL
, "ETag", &info
->etag
))
229 int status
= headers
->response_code();
230 if (2 == status
/ 100 && status
!= net::HTTP_PARTIAL_CONTENT
) {
231 // Success & not range response; if we asked for a range, we didn't
232 // get it--reset the file pointers to reflect that.
233 info
->save_info
->offset
= 0;
234 info
->save_info
->hash_state
= "";
237 if (!headers
->GetMimeType(&info
->original_mime_type
))
238 info
->original_mime_type
.clear();
241 // Blink verifies that the requester of this download is allowed to set a
242 // suggested name for the security origin of the downlaod URL. However, this
243 // assumption doesn't hold if there were cross origin redirects. Therefore,
244 // clear the suggested_name for such requests.
245 if (info
->url_chain
.size() > 1 &&
246 info
->url_chain
.front().GetOrigin() != info
->url_chain
.back().GetOrigin())
247 info
->save_info
->suggested_name
.clear();
249 BrowserThread::PostTask(
250 BrowserThread::UI
, FROM_HERE
,
251 base::Bind(&StartOnUIThread
,
253 base::Passed(&tab_info_
),
254 base::Passed(&stream_reader
),
255 // Pass to StartOnUIThread so that variable
256 // access is always on IO thread but function
257 // is called on UI thread.
259 // Guaranteed to be called in StartOnUIThread
265 void DownloadResourceHandler::CallStartedCB(
267 DownloadInterruptReason interrupt_reason
) {
268 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
269 if (started_cb_
.is_null())
271 BrowserThread::PostTask(
275 &CallStartedCBOnUIThread
, started_cb_
, item
, interrupt_reason
));
279 bool DownloadResourceHandler::OnWillStart(const GURL
& url
, bool* defer
) {
283 bool DownloadResourceHandler::OnBeforeNetworkStart(const GURL
& url
,
288 // Create a new buffer, which will be handed to the download thread for file
289 // writing and deletion.
290 bool DownloadResourceHandler::OnWillRead(scoped_refptr
<net::IOBuffer
>* buf
,
293 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
294 DCHECK(buf
&& buf_size
);
295 DCHECK(!read_buffer_
.get());
297 *buf_size
= min_size
< 0 ? kReadBufSize
: min_size
;
298 last_buffer_size_
= *buf_size
;
299 read_buffer_
= new net::IOBuffer(*buf_size
);
300 *buf
= read_buffer_
.get();
304 // Pass the buffer to the download file writer.
305 bool DownloadResourceHandler::OnReadCompleted(int bytes_read
, bool* defer
) {
306 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
307 DCHECK(read_buffer_
.get());
309 base::TimeTicks
now(base::TimeTicks::Now());
310 if (!last_read_time_
.is_null()) {
311 double seconds_since_last_read
= (now
- last_read_time_
).InSecondsF();
312 if (now
== last_read_time_
)
313 // Use 1/10 ms as a "very small number" so that we avoid
314 // divide-by-zero error and still record a very high potential bandwidth.
315 seconds_since_last_read
= 0.00001;
317 double actual_bandwidth
= (bytes_read
)/seconds_since_last_read
;
318 double potential_bandwidth
= last_buffer_size_
/seconds_since_last_read
;
319 RecordBandwidth(actual_bandwidth
, potential_bandwidth
);
321 last_read_time_
= now
;
325 bytes_read_
+= bytes_read
;
326 DCHECK(read_buffer_
.get());
328 // Take the data ship it down the stream. If the stream is full, pause the
329 // request; the stream callback will resume it.
330 if (!stream_writer_
->Write(read_buffer_
, bytes_read
)) {
332 *defer
= was_deferred_
= true;
333 last_stream_pause_time_
= now
;
336 read_buffer_
= NULL
; // Drop our reference.
338 if (pause_count_
> 0)
339 *defer
= was_deferred_
= true;
344 void DownloadResourceHandler::OnResponseCompleted(
345 const net::URLRequestStatus
& status
,
346 const std::string
& security_info
,
348 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
349 int response_code
= status
.is_success() ? request()->GetResponseCode() : 0;
350 DVLOG(20) << __FUNCTION__
<< "()" << DebugString()
351 << " status.status() = " << status
.status()
352 << " status.error() = " << status
.error()
353 << " response_code = " << response_code
;
355 net::Error error_code
= net::OK
;
356 if (status
.status() == net::URLRequestStatus::FAILED
||
357 // Note cancels as failures too.
358 status
.status() == net::URLRequestStatus::CANCELED
) {
359 error_code
= static_cast<net::Error
>(status
.error()); // Normal case.
360 // Make sure that at least the fact of failure comes through.
361 if (error_code
== net::OK
)
362 error_code
= net::ERR_FAILED
;
365 // ERR_CONTENT_LENGTH_MISMATCH and ERR_INCOMPLETE_CHUNKED_ENCODING are
366 // allowed since a number of servers in the wild close the connection too
367 // early by mistake. Other browsers - IE9, Firefox 11.0, and Safari 5.1.4 -
368 // treat downloads as complete in both cases, so we follow their lead.
369 if (error_code
== net::ERR_CONTENT_LENGTH_MISMATCH
||
370 error_code
== net::ERR_INCOMPLETE_CHUNKED_ENCODING
) {
371 error_code
= net::OK
;
373 DownloadInterruptReason reason
=
374 ConvertNetErrorToInterruptReason(
375 error_code
, DOWNLOAD_INTERRUPT_FROM_NETWORK
);
377 if (status
.status() == net::URLRequestStatus::CANCELED
&&
378 status
.error() == net::ERR_ABORTED
) {
379 // CANCELED + ERR_ABORTED == something outside of the network
380 // stack cancelled the request. There aren't that many things that
381 // could do this to a download request (whose lifetime is separated from
382 // the tab from which it came). We map this to USER_CANCELLED as the
383 // case we know about (system suspend because of laptop close) corresponds
385 // TODO(ahendrickson) -- Find a better set of codes to use here, as
386 // CANCELED/ERR_ABORTED can occur for reasons other than user cancel.
387 if (net::IsCertStatusError(request()->ssl_info().cert_status
))
388 reason
= DOWNLOAD_INTERRUPT_REASON_SERVER_CERT_PROBLEM
;
390 reason
= DOWNLOAD_INTERRUPT_REASON_USER_CANCELED
;
393 if (status
.is_success() &&
394 reason
== DOWNLOAD_INTERRUPT_REASON_NONE
&&
395 request()->response_headers()) {
396 // Handle server's response codes.
397 switch(response_code
) {
398 case -1: // Non-HTTP request.
400 case net::HTTP_CREATED
:
401 case net::HTTP_ACCEPTED
:
402 case net::HTTP_NON_AUTHORITATIVE_INFORMATION
:
403 case net::HTTP_RESET_CONTENT
:
404 case net::HTTP_PARTIAL_CONTENT
:
405 // Expected successful codes.
407 case net::HTTP_NO_CONTENT
:
408 case net::HTTP_NOT_FOUND
:
409 reason
= DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT
;
411 case net::HTTP_PRECONDITION_FAILED
:
412 // Failed our 'If-Unmodified-Since' or 'If-Match'; see
413 // download_manager_impl.cc BeginDownload()
414 reason
= DOWNLOAD_INTERRUPT_REASON_SERVER_PRECONDITION
;
416 case net::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE
:
417 // Retry by downloading from the start automatically:
418 // If we haven't received data when we get this error, we won't.
419 reason
= DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE
;
421 case net::HTTP_UNAUTHORIZED
:
422 // Server didn't authorize this request.
423 reason
= DOWNLOAD_INTERRUPT_REASON_SERVER_UNAUTHORIZED
;
425 case net::HTTP_FORBIDDEN
:
426 // Server forbids access to this resource.
427 reason
= DOWNLOAD_INTERRUPT_REASON_SERVER_FORBIDDEN
;
429 default: // All other errors.
430 // Redirection and informational codes should have been handled earlier
432 DCHECK_NE(3, response_code
/ 100);
433 DCHECK_NE(1, response_code
/ 100);
434 reason
= DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED
;
439 std::string accept_ranges
;
440 bool has_strong_validators
= false;
441 if (request()->response_headers()) {
442 request()->response_headers()->EnumerateHeader(
443 NULL
, "Accept-Ranges", &accept_ranges
);
444 has_strong_validators
=
445 request()->response_headers()->HasStrongValidators();
447 RecordAcceptsRanges(accept_ranges
, bytes_read_
, has_strong_validators
);
448 RecordNetworkBlockage(base::TimeTicks::Now() - download_start_time_
,
451 CallStartedCB(NULL
, reason
);
453 // Send the info down the stream. Conditional is in case we get
454 // OnResponseCompleted without OnResponseStarted.
456 stream_writer_
->Close(reason
);
458 // If the error mapped to something unknown, record it so that
459 // we can drill down.
460 if (reason
== DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED
) {
461 UMA_HISTOGRAM_SPARSE_SLOWLY("Download.MapErrorNetworkFailed",
462 std::abs(status
.error()));
465 stream_writer_
.reset(); // We no longer need the stream.
469 void DownloadResourceHandler::OnDataDownloaded(int bytes_downloaded
) {
473 void DownloadResourceHandler::PauseRequest() {
474 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
479 void DownloadResourceHandler::ResumeRequest() {
480 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
481 DCHECK_LT(0, pause_count_
);
487 if (pause_count_
> 0)
490 was_deferred_
= false;
491 if (!last_stream_pause_time_
.is_null()) {
492 total_pause_time_
+= (base::TimeTicks::Now() - last_stream_pause_time_
);
493 last_stream_pause_time_
= base::TimeTicks();
496 controller()->Resume();
499 void DownloadResourceHandler::CancelRequest() {
500 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
502 const ResourceRequestInfo
* info
= GetRequestInfo();
503 ResourceDispatcherHostImpl::Get()->CancelRequest(
505 info
->GetRequestID());
506 // This object has been deleted.
509 std::string
DownloadResourceHandler::DebugString() const {
510 const ResourceRequestInfo
* info
= GetRequestInfo();
511 return base::StringPrintf("{"
515 " request_id = " "%d"
520 request()->url().spec().c_str() :
523 info
->GetRequestID(),
527 DownloadResourceHandler::~DownloadResourceHandler() {
528 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
530 // This won't do anything if the callback was called before.
531 // If it goes through, it will likely be because OnWillStart() returned
532 // false somewhere in the chain of resource handlers.
533 CallStartedCB(NULL
, DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED
);
535 // Remove output stream callback if a stream exists.
537 stream_writer_
->RegisterCallback(base::Closure());
539 // tab_info_ must be destroyed on UI thread, since
540 // InitializeDownloadTabInfoOnUIThread might still be using it.
541 if (tab_info_
.get()) {
542 BrowserThread::PostTask(
543 BrowserThread::UI
, FROM_HERE
,
544 base::Bind(&DeleteOnUIThread
, base::Passed(&tab_info_
)));
547 UMA_HISTOGRAM_TIMES("SB2.DownloadDuration",
548 base::TimeTicks::Now() - download_start_time_
);
551 } // namespace content