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 DownloadUrlParameters::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 DownloadUrlParameters::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 download_manager
->StartDownload(info
.Pass(), stream
.Pass(), started_cb
);
70 const int DownloadResourceHandler::kDownloadByteStreamSize
= 100 * 1024;
72 DownloadResourceHandler::DownloadResourceHandler(
74 net::URLRequest
* request
,
75 const DownloadUrlParameters::OnStartedCallback
& started_cb
,
76 scoped_ptr
<DownloadSaveInfo
> save_info
)
77 : ResourceHandler(request
),
79 started_cb_(started_cb
),
80 save_info_(save_info
.Pass()),
85 on_response_started_called_(false) {
86 RecordDownloadCount(UNTHROTTLED_COUNT
);
89 bool DownloadResourceHandler::OnUploadProgress(int request_id
,
95 bool DownloadResourceHandler::OnRequestRedirected(
98 ResourceResponse
* response
,
100 // We treat a download as a main frame load, and thus update the policy URL
102 request()->set_first_party_for_cookies(url
);
106 // Send the download creation information to the download thread.
107 bool DownloadResourceHandler::OnResponseStarted(
109 ResourceResponse
* response
,
111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
112 // There can be only one (call)
113 DCHECK(!on_response_started_called_
);
114 on_response_started_called_
= true;
116 VLOG(20) << __FUNCTION__
<< "()" << DebugString()
117 << " request_id = " << request_id
;
118 download_start_time_
= base::TimeTicks::Now();
120 // If it's a download, we don't want to poison the cache with it.
121 request()->StopCaching();
123 // Lower priority as well, so downloads don't contend for resources
125 request()->SetPriority(net::IDLE
);
127 // If the content-length header is not present (or contains something other
128 // than numbers), the incoming content_length is -1 (unknown size).
129 // Set the content length to 0 to indicate unknown size to DownloadManager.
130 int64 content_length
=
131 response
->head
.content_length
> 0 ? response
->head
.content_length
: 0;
133 const ResourceRequestInfoImpl
* request_info
= GetRequestInfo();
135 // Deleted in DownloadManager.
136 scoped_ptr
<DownloadCreateInfo
> info(
137 new DownloadCreateInfo(base::Time::Now(),
139 request()->net_log(),
140 request_info
->HasUserGesture(),
141 request_info
->GetPageTransition(),
144 // Create the ByteStream for sending data to the download sink.
145 scoped_ptr
<ByteStreamReader
> stream_reader
;
147 base::MessageLoopProxy::current(),
148 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
149 kDownloadByteStreamSize
, &stream_writer_
, &stream_reader
);
150 stream_writer_
->RegisterCallback(
151 base::Bind(&DownloadResourceHandler::ResumeRequest
, AsWeakPtr()));
153 info
->download_id
= download_id_
;
154 info
->url_chain
= request()->url_chain();
155 info
->referrer_url
= GURL(request()->referrer());
156 info
->mime_type
= response
->head
.mime_type
;
157 info
->remote_address
= request()->GetSocketAddress().host();
158 request()->GetResponseHeaderByName("content-disposition",
159 &info
->content_disposition
);
160 RecordDownloadMimeType(info
->mime_type
);
161 RecordDownloadContentDisposition(info
->content_disposition
);
163 info
->request_handle
=
164 DownloadRequestHandle(AsWeakPtr(), request_info
->GetChildID(),
165 request_info
->GetRouteID(),
166 request_info
->GetRequestID());
168 // Get the last modified time and etag.
169 const net::HttpResponseHeaders
* headers
= request()->response_headers();
171 if (headers
->HasStrongValidators()) {
172 // If we don't have strong validators as per RFC 2616 section 13.3.3, then
173 // we neither store nor use them for range requests.
174 if (!headers
->EnumerateHeader(NULL
, "Last-Modified",
175 &info
->last_modified
))
176 info
->last_modified
.clear();
177 if (!headers
->EnumerateHeader(NULL
, "ETag", &info
->etag
))
181 int status
= headers
->response_code();
182 if (2 == status
/ 100 && status
!= net::HTTP_PARTIAL_CONTENT
) {
183 // Success & not range response; if we asked for a range, we didn't
184 // get it--reset the file pointers to reflect that.
185 info
->save_info
->offset
= 0;
186 info
->save_info
->hash_state
= "";
189 if (!headers
->GetMimeType(&info
->original_mime_type
))
190 info
->original_mime_type
.clear();
193 BrowserThread::PostTask(
194 BrowserThread::UI
, FROM_HERE
,
195 base::Bind(&StartOnUIThread
,
197 base::Passed(&stream_reader
),
198 // Pass to StartOnUIThread so that variable
199 // access is always on IO thread but function
200 // is called on UI thread.
202 // Guaranteed to be called in StartOnUIThread
208 void DownloadResourceHandler::CallStartedCB(
209 DownloadItem
* item
, net::Error error
) {
210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
211 if (started_cb_
.is_null())
213 BrowserThread::PostTask(
214 BrowserThread::UI
, FROM_HERE
,
215 base::Bind(&CallStartedCBOnUIThread
, started_cb_
, item
, error
));
219 bool DownloadResourceHandler::OnWillStart(int request_id
,
225 bool DownloadResourceHandler::OnBeforeNetworkStart(int request_id
,
231 // Create a new buffer, which will be handed to the download thread for file
232 // writing and deletion.
233 bool DownloadResourceHandler::OnWillRead(int request_id
,
234 scoped_refptr
<net::IOBuffer
>* buf
,
237 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
238 DCHECK(buf
&& buf_size
);
239 DCHECK(!read_buffer_
.get());
241 *buf_size
= min_size
< 0 ? kReadBufSize
: min_size
;
242 last_buffer_size_
= *buf_size
;
243 read_buffer_
= new net::IOBuffer(*buf_size
);
244 *buf
= read_buffer_
.get();
248 // Pass the buffer to the download file writer.
249 bool DownloadResourceHandler::OnReadCompleted(int request_id
, int bytes_read
,
251 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
252 DCHECK(read_buffer_
.get());
254 base::TimeTicks
now(base::TimeTicks::Now());
255 if (!last_read_time_
.is_null()) {
256 double seconds_since_last_read
= (now
- last_read_time_
).InSecondsF();
257 if (now
== last_read_time_
)
258 // Use 1/10 ms as a "very small number" so that we avoid
259 // divide-by-zero error and still record a very high potential bandwidth.
260 seconds_since_last_read
= 0.00001;
262 double actual_bandwidth
= (bytes_read
)/seconds_since_last_read
;
263 double potential_bandwidth
= last_buffer_size_
/seconds_since_last_read
;
264 RecordBandwidth(actual_bandwidth
, potential_bandwidth
);
266 last_read_time_
= now
;
270 bytes_read_
+= bytes_read
;
271 DCHECK(read_buffer_
.get());
273 // Take the data ship it down the stream. If the stream is full, pause the
274 // request; the stream callback will resume it.
275 if (!stream_writer_
->Write(read_buffer_
, bytes_read
)) {
277 *defer
= was_deferred_
= true;
278 last_stream_pause_time_
= now
;
281 read_buffer_
= NULL
; // Drop our reference.
283 if (pause_count_
> 0)
284 *defer
= was_deferred_
= true;
289 void DownloadResourceHandler::OnResponseCompleted(
291 const net::URLRequestStatus
& status
,
292 const std::string
& security_info
,
294 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
295 int response_code
= status
.is_success() ? request()->GetResponseCode() : 0;
296 VLOG(20) << __FUNCTION__
<< "()" << DebugString()
297 << " request_id = " << request_id
298 << " status.status() = " << status
.status()
299 << " status.error() = " << status
.error()
300 << " response_code = " << response_code
;
302 net::Error error_code
= net::OK
;
303 if (status
.status() == net::URLRequestStatus::FAILED
||
304 // Note cancels as failures too.
305 status
.status() == net::URLRequestStatus::CANCELED
) {
306 error_code
= static_cast<net::Error
>(status
.error()); // Normal case.
307 // Make sure that at least the fact of failure comes through.
308 if (error_code
== net::OK
)
309 error_code
= net::ERR_FAILED
;
312 // ERR_CONTENT_LENGTH_MISMATCH and ERR_INCOMPLETE_CHUNKED_ENCODING are
313 // allowed since a number of servers in the wild close the connection too
314 // early by mistake. Other browsers - IE9, Firefox 11.0, and Safari 5.1.4 -
315 // treat downloads as complete in both cases, so we follow their lead.
316 if (error_code
== net::ERR_CONTENT_LENGTH_MISMATCH
||
317 error_code
== net::ERR_INCOMPLETE_CHUNKED_ENCODING
) {
318 error_code
= net::OK
;
320 DownloadInterruptReason reason
=
321 ConvertNetErrorToInterruptReason(
322 error_code
, DOWNLOAD_INTERRUPT_FROM_NETWORK
);
324 if (status
.status() == net::URLRequestStatus::CANCELED
&&
325 status
.error() == net::ERR_ABORTED
) {
326 // CANCELED + ERR_ABORTED == something outside of the network
327 // stack cancelled the request. There aren't that many things that
328 // could do this to a download request (whose lifetime is separated from
329 // the tab from which it came). We map this to USER_CANCELLED as the
330 // case we know about (system suspend because of laptop close) corresponds
332 // TODO(ahendrickson) -- Find a better set of codes to use here, as
333 // CANCELED/ERR_ABORTED can occur for reasons other than user cancel.
334 reason
= DOWNLOAD_INTERRUPT_REASON_USER_CANCELED
;
337 if (status
.is_success() &&
338 reason
== DOWNLOAD_INTERRUPT_REASON_NONE
&&
339 request()->response_headers()) {
340 // Handle server's response codes.
341 switch(response_code
) {
342 case -1: // Non-HTTP request.
344 case net::HTTP_CREATED
:
345 case net::HTTP_ACCEPTED
:
346 case net::HTTP_NON_AUTHORITATIVE_INFORMATION
:
347 case net::HTTP_RESET_CONTENT
:
348 case net::HTTP_PARTIAL_CONTENT
:
349 // Expected successful codes.
351 case net::HTTP_NO_CONTENT
:
352 case net::HTTP_NOT_FOUND
:
353 reason
= DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT
;
355 case net::HTTP_PRECONDITION_FAILED
:
356 // Failed our 'If-Unmodified-Since' or 'If-Match'; see
357 // download_manager_impl.cc BeginDownload()
358 reason
= DOWNLOAD_INTERRUPT_REASON_SERVER_PRECONDITION
;
360 case net::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE
:
361 // Retry by downloading from the start automatically:
362 // If we haven't received data when we get this error, we won't.
363 reason
= DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE
;
365 default: // All other errors.
366 // Redirection and informational codes should have been handled earlier
368 DCHECK_NE(3, response_code
/ 100);
369 DCHECK_NE(1, response_code
/ 100);
370 reason
= DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED
;
375 std::string accept_ranges
;
376 bool has_strong_validators
= false;
377 if (request()->response_headers()) {
378 request()->response_headers()->EnumerateHeader(
379 NULL
, "Accept-Ranges", &accept_ranges
);
380 has_strong_validators
=
381 request()->response_headers()->HasStrongValidators();
383 RecordAcceptsRanges(accept_ranges
, bytes_read_
, has_strong_validators
);
384 RecordNetworkBlockage(base::TimeTicks::Now() - download_start_time_
,
387 CallStartedCB(NULL
, error_code
);
389 // Send the info down the stream. Conditional is in case we get
390 // OnResponseCompleted without OnResponseStarted.
392 stream_writer_
->Close(reason
);
394 // If the error mapped to something unknown, record it so that
395 // we can drill down.
396 if (reason
== DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED
) {
397 UMA_HISTOGRAM_CUSTOM_ENUMERATION("Download.MapErrorNetworkFailed",
398 std::abs(status
.error()),
399 net::GetAllErrorCodesForUma());
402 stream_writer_
.reset(); // We no longer need the stream.
406 void DownloadResourceHandler::OnDataDownloaded(
408 int bytes_downloaded
) {
412 void DownloadResourceHandler::PauseRequest() {
413 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
418 void DownloadResourceHandler::ResumeRequest() {
419 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
420 DCHECK_LT(0, pause_count_
);
426 if (pause_count_
> 0)
429 was_deferred_
= false;
430 if (!last_stream_pause_time_
.is_null()) {
431 total_pause_time_
+= (base::TimeTicks::Now() - last_stream_pause_time_
);
432 last_stream_pause_time_
= base::TimeTicks();
435 controller()->Resume();
438 void DownloadResourceHandler::CancelRequest() {
439 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
441 const ResourceRequestInfo
* info
= GetRequestInfo();
442 ResourceDispatcherHostImpl::Get()->CancelRequest(
444 info
->GetRequestID(),
448 std::string
DownloadResourceHandler::DebugString() const {
449 const ResourceRequestInfo
* info
= GetRequestInfo();
450 return base::StringPrintf("{"
454 " request_id = " "%d"
459 request()->url().spec().c_str() :
462 info
->GetRequestID(),
466 DownloadResourceHandler::~DownloadResourceHandler() {
467 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
469 // This won't do anything if the callback was called before.
470 // If it goes through, it will likely be because OnWillStart() returned
471 // false somewhere in the chain of resource handlers.
472 CallStartedCB(NULL
, net::ERR_ACCESS_DENIED
);
474 // Remove output stream callback if a stream exists.
476 stream_writer_
->RegisterCallback(base::Closure());
478 UMA_HISTOGRAM_TIMES("SB2.DownloadDuration",
479 base::TimeTicks::Now() - download_start_time_
);
482 } // namespace content