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/browser/navigation_entry.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(BrowserThread::CurrentlyOn(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(BrowserThread::CurrentlyOn(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(BrowserThread::CurrentlyOn(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()),
134 bool DownloadResourceHandler::OnUploadProgress(int request_id,
140 bool DownloadResourceHandler::OnRequestRedirected(
143 ResourceResponse* response,
145 // We treat a download as a main frame load, and thus update the policy URL
147 request()->set_first_party_for_cookies(url);
151 // Send the download creation information to the download thread.
152 bool DownloadResourceHandler::OnResponseStarted(
154 ResourceResponse* response,
156 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
157 // There can be only one (call)
158 DCHECK(!on_response_started_called_);
159 on_response_started_called_ = true;
161 VLOG(20) << __FUNCTION__ << "()" << DebugString()
162 << " request_id = " << request_id;
163 download_start_time_ = base::TimeTicks::Now();
165 // If it's a download, we don't want to poison the cache with it.
166 request()->StopCaching();
168 // Lower priority as well, so downloads don't contend for resources
170 request()->SetPriority(net::IDLE);
172 // If the content-length header is not present (or contains something other
173 // than numbers), the incoming content_length is -1 (unknown size).
174 // Set the content length to 0 to indicate unknown size to DownloadManager.
175 int64 content_length =
176 response->head.content_length > 0 ? response->head.content_length : 0;
178 const ResourceRequestInfoImpl* request_info = GetRequestInfo();
180 // Deleted in DownloadManager.
181 scoped_ptr<DownloadCreateInfo> info(
182 new DownloadCreateInfo(base::Time::Now(),
184 request()->net_log(),
185 request_info->HasUserGesture(),
186 request_info->GetPageTransition(),
189 // Create the ByteStream for sending data to the download sink.
190 scoped_ptr<ByteStreamReader> stream_reader;
192 base::MessageLoopProxy::current(),
193 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
194 kDownloadByteStreamSize, &stream_writer_, &stream_reader);
195 stream_writer_->RegisterCallback(
196 base::Bind(&DownloadResourceHandler::ResumeRequest, AsWeakPtr()));
198 info->download_id = download_id_;
199 info->url_chain = request()->url_chain();
200 info->referrer_url = GURL(request()->referrer());
201 info->mime_type = response->head.mime_type;
202 info->remote_address = request()->GetSocketAddress().host();
203 request()->GetResponseHeaderByName("content-disposition",
204 &info->content_disposition);
205 RecordDownloadMimeType(info->mime_type);
206 RecordDownloadContentDisposition(info->content_disposition);
208 info->request_handle =
209 DownloadRequestHandle(AsWeakPtr(), request_info->GetChildID(),
210 request_info->GetRouteID(),
211 request_info->GetRequestID());
213 // Get the last modified time and etag.
214 const net::HttpResponseHeaders* headers = request()->response_headers();
216 if (headers->HasStrongValidators()) {
217 // If we don't have strong validators as per RFC 2616 section 13.3.3, then
218 // we neither store nor use them for range requests.
219 if (!headers->EnumerateHeader(NULL, "Last-Modified",
220 &info->last_modified))
221 info->last_modified.clear();
222 if (!headers->EnumerateHeader(NULL, "ETag", &info->etag))
226 int status = headers->response_code();
227 if (2 == status / 100 && status != net::HTTP_PARTIAL_CONTENT) {
228 // Success & not range response; if we asked for a range, we didn't
229 // get it--reset the file pointers to reflect that.
230 info->save_info->offset = 0;
231 info->save_info->hash_state = "";
234 if (!headers->GetMimeType(&info->original_mime_type))
235 info->original_mime_type.clear();
238 BrowserThread::PostTask(
239 BrowserThread::UI, FROM_HERE,
240 base::Bind(&StartOnUIThread,
242 base::Owned(tab_info_),
243 base::Passed(&stream_reader),
244 // Pass to StartOnUIThread so that variable
245 // access is always on IO thread but function
246 // is called on UI thread.
248 // Now owned by the task that was just posted.
250 // Guaranteed to be called in StartOnUIThread
256 void DownloadResourceHandler::CallStartedCB(
258 DownloadInterruptReason interrupt_reason) {
259 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
260 if (started_cb_.is_null())
262 BrowserThread::PostTask(
266 &CallStartedCBOnUIThread, started_cb_, item, interrupt_reason));
270 bool DownloadResourceHandler::OnWillStart(int request_id,
276 bool DownloadResourceHandler::OnBeforeNetworkStart(int request_id,
282 // Create a new buffer, which will be handed to the download thread for file
283 // writing and deletion.
284 bool DownloadResourceHandler::OnWillRead(int request_id,
285 scoped_refptr<net::IOBuffer>* buf,
288 DCHECK(BrowserThread::CurrentlyOn(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 request_id, int bytes_read,
302 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
303 DCHECK(read_buffer_.get());
305 base::TimeTicks now(base::TimeTicks::Now());
306 if (!last_read_time_.is_null()) {
307 double seconds_since_last_read = (now - last_read_time_).InSecondsF();
308 if (now == last_read_time_)
309 // Use 1/10 ms as a "very small number" so that we avoid
310 // divide-by-zero error and still record a very high potential bandwidth.
311 seconds_since_last_read = 0.00001;
313 double actual_bandwidth = (bytes_read)/seconds_since_last_read;
314 double potential_bandwidth = last_buffer_size_/seconds_since_last_read;
315 RecordBandwidth(actual_bandwidth, potential_bandwidth);
317 last_read_time_ = now;
321 bytes_read_ += bytes_read;
322 DCHECK(read_buffer_.get());
324 // Take the data ship it down the stream. If the stream is full, pause the
325 // request; the stream callback will resume it.
326 if (!stream_writer_->Write(read_buffer_, bytes_read)) {
328 *defer = was_deferred_ = true;
329 last_stream_pause_time_ = now;
332 read_buffer_ = NULL; // Drop our reference.
334 if (pause_count_ > 0)
335 *defer = was_deferred_ = true;
340 void DownloadResourceHandler::OnResponseCompleted(
342 const net::URLRequestStatus& status,
343 const std::string& security_info,
345 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
346 int response_code = status.is_success() ? request()->GetResponseCode() : 0;
347 VLOG(20) << __FUNCTION__ << "()" << DebugString()
348 << " request_id = " << request_id
349 << " status.status() = " << status.status()
350 << " status.error() = " << status.error()
351 << " response_code = " << response_code;
353 net::Error error_code = net::OK;
354 if (status.status() == net::URLRequestStatus::FAILED ||
355 // Note cancels as failures too.
356 status.status() == net::URLRequestStatus::CANCELED) {
357 error_code = static_cast<net::Error>(status.error()); // Normal case.
358 // Make sure that at least the fact of failure comes through.
359 if (error_code == net::OK)
360 error_code = net::ERR_FAILED;
363 // ERR_CONTENT_LENGTH_MISMATCH and ERR_INCOMPLETE_CHUNKED_ENCODING are
364 // allowed since a number of servers in the wild close the connection too
365 // early by mistake. Other browsers - IE9, Firefox 11.0, and Safari 5.1.4 -
366 // treat downloads as complete in both cases, so we follow their lead.
367 if (error_code == net::ERR_CONTENT_LENGTH_MISMATCH ||
368 error_code == net::ERR_INCOMPLETE_CHUNKED_ENCODING) {
369 error_code = net::OK;
371 DownloadInterruptReason reason =
372 ConvertNetErrorToInterruptReason(
373 error_code, DOWNLOAD_INTERRUPT_FROM_NETWORK);
375 if (status.status() == net::URLRequestStatus::CANCELED &&
376 status.error() == net::ERR_ABORTED) {
377 // CANCELED + ERR_ABORTED == something outside of the network
378 // stack cancelled the request. There aren't that many things that
379 // could do this to a download request (whose lifetime is separated from
380 // the tab from which it came). We map this to USER_CANCELLED as the
381 // case we know about (system suspend because of laptop close) corresponds
383 // TODO(ahendrickson) -- Find a better set of codes to use here, as
384 // CANCELED/ERR_ABORTED can occur for reasons other than user cancel.
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 default: // All other errors.
417 // Redirection and informational codes should have been handled earlier
419 DCHECK_NE(3, response_code / 100);
420 DCHECK_NE(1, response_code / 100);
421 reason = DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED;
426 std::string accept_ranges;
427 bool has_strong_validators = false;
428 if (request()->response_headers()) {
429 request()->response_headers()->EnumerateHeader(
430 NULL, "Accept-Ranges", &accept_ranges);
431 has_strong_validators =
432 request()->response_headers()->HasStrongValidators();
434 RecordAcceptsRanges(accept_ranges, bytes_read_, has_strong_validators);
435 RecordNetworkBlockage(base::TimeTicks::Now() - download_start_time_,
438 CallStartedCB(NULL, reason);
440 // Send the info down the stream. Conditional is in case we get
441 // OnResponseCompleted without OnResponseStarted.
443 stream_writer_->Close(reason);
445 // If the error mapped to something unknown, record it so that
446 // we can drill down.
447 if (reason == DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED) {
448 UMA_HISTOGRAM_CUSTOM_ENUMERATION("Download.MapErrorNetworkFailed",
449 std::abs(status.error()),
450 net::GetAllErrorCodesForUma());
453 stream_writer_.reset(); // We no longer need the stream.
457 void DownloadResourceHandler::OnDataDownloaded(
459 int bytes_downloaded) {
463 void DownloadResourceHandler::PauseRequest() {
464 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
469 void DownloadResourceHandler::ResumeRequest() {
470 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
471 DCHECK_LT(0, pause_count_);
477 if (pause_count_ > 0)
480 was_deferred_ = false;
481 if (!last_stream_pause_time_.is_null()) {
482 total_pause_time_ += (base::TimeTicks::Now() - last_stream_pause_time_);
483 last_stream_pause_time_ = base::TimeTicks();
486 controller()->Resume();
489 void DownloadResourceHandler::CancelRequest() {
490 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
492 const ResourceRequestInfo* info = GetRequestInfo();
493 ResourceDispatcherHostImpl::Get()->CancelRequest(
495 info->GetRequestID());
496 // This object has been deleted.
499 std::string DownloadResourceHandler::DebugString() const {
500 const ResourceRequestInfo* info = GetRequestInfo();
501 return base::StringPrintf("{"
505 " request_id = " "%d"
510 request()->url().spec().c_str() :
513 info->GetRequestID(),
517 DownloadResourceHandler::~DownloadResourceHandler() {
518 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
520 // This won't do anything if the callback was called before.
521 // If it goes through, it will likely be because OnWillStart() returned
522 // false somewhere in the chain of resource handlers.
523 CallStartedCB(NULL, DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED);
525 // Remove output stream callback if a stream exists.
527 stream_writer_->RegisterCallback(base::Closure());
529 // tab_info_ must be destroyed on UI thread, since
530 // InitializeDownloadTabInfoOnUIThread might still be using it.
532 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, tab_info_);
534 UMA_HISTOGRAM_TIMES("SB2.DownloadDuration",
535 base::TimeTicks::Now() - download_start_time_);
538 } // namespace content