Fix broken path in extensions/common/PRESUBMIT.py
[chromium-blink-merge.git] / content / browser / download / download_resource_handler.cc
blob045c0f0e17e90979a55dd42e1662903a220a6046
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"
7 #include <string>
9 #include "base/bind.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"
36 namespace content {
38 struct DownloadResourceHandler::DownloadTabInfo {
39 GURL tab_url;
40 GURL tab_referrer_url;
43 namespace {
45 void CallStartedCBOnUIThread(
46 const DownloadUrlParameters::OnStartedCallback& started_cb,
47 DownloadItem* item,
48 DownloadInterruptReason interrupt_reason) {
49 DCHECK_CURRENTLY_ON(BrowserThread::UI);
51 if (started_cb.is_null())
52 return;
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 scoped_ptr<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
68 // download.
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.
75 return;
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();
90 if (web_contents) {
91 NavigationEntry* entry = web_contents->GetController().GetVisibleEntry();
92 if (entry) {
93 tab_info->tab_url = entry->GetURL();
94 tab_info->tab_referrer_url = entry->GetReferrer().url;
99 void DeleteOnUIThread(
100 scoped_ptr<DownloadResourceHandler::DownloadTabInfo> tab_info) {}
102 } // namespace
104 const int DownloadResourceHandler::kDownloadByteStreamSize = 100 * 1024;
106 DownloadResourceHandler::DownloadResourceHandler(
107 uint32 id,
108 net::URLRequest* request,
109 const DownloadUrlParameters::OnStartedCallback& started_cb,
110 scoped_ptr<DownloadSaveInfo> save_info)
111 : ResourceHandler(request),
112 download_id_(id),
113 started_cb_(started_cb),
114 save_info_(save_info.Pass()),
115 tab_info_(new DownloadTabInfo()),
116 last_buffer_size_(0),
117 bytes_read_(0),
118 pause_count_(0),
119 was_deferred_(false),
120 on_response_started_called_(false) {
121 RecordDownloadCount(UNTHROTTLED_COUNT);
123 // Do UI thread initialization for tab_info_ asap after
124 // DownloadResourceHandler creation since the tab could be navigated
125 // before StartOnUIThread gets called. This is safe because deletion
126 // will occur via PostTask() as well, which will serialized behind this
127 // PostTask()
128 const ResourceRequestInfoImpl* request_info = GetRequestInfo();
129 BrowserThread::PostTask(
130 BrowserThread::UI,
131 FROM_HERE,
132 base::Bind(&InitializeDownloadTabInfoOnUIThread,
133 DownloadRequestHandle(AsWeakPtr(),
134 request_info->GetChildID(),
135 request_info->GetRouteID(),
136 request_info->GetRequestID()),
137 tab_info_.get()));
138 power_save_blocker_ = PowerSaveBlocker::Create(
139 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension,
140 PowerSaveBlocker::kReasonOther, "Download in progress");
143 bool DownloadResourceHandler::OnUploadProgress(uint64 position,
144 uint64 size) {
145 return true;
148 bool DownloadResourceHandler::OnRequestRedirected(
149 const net::RedirectInfo& redirect_info,
150 ResourceResponse* response,
151 bool* defer) {
152 return true;
155 // Send the download creation information to the download thread.
156 bool DownloadResourceHandler::OnResponseStarted(
157 ResourceResponse* response,
158 bool* defer) {
159 DCHECK_CURRENTLY_ON(BrowserThread::IO);
160 // There can be only one (call)
161 DCHECK(!on_response_started_called_);
162 on_response_started_called_ = true;
164 DVLOG(20) << __FUNCTION__ << "()" << DebugString();
165 download_start_time_ = base::TimeTicks::Now();
167 // If it's a download, we don't want to poison the cache with it.
168 request()->StopCaching();
170 // Lower priority as well, so downloads don't contend for resources
171 // with main frames.
172 request()->SetPriority(net::IDLE);
174 // If the content-length header is not present (or contains something other
175 // than numbers), the incoming content_length is -1 (unknown size).
176 // Set the content length to 0 to indicate unknown size to DownloadManager.
177 int64 content_length =
178 response->head.content_length > 0 ? response->head.content_length : 0;
180 const ResourceRequestInfoImpl* request_info = GetRequestInfo();
182 // Deleted in DownloadManager.
183 scoped_ptr<DownloadCreateInfo> info(
184 new DownloadCreateInfo(base::Time::Now(),
185 content_length,
186 request()->net_log(),
187 request_info->HasUserGesture(),
188 request_info->GetPageTransition(),
189 save_info_.Pass()));
191 // Create the ByteStream for sending data to the download sink.
192 scoped_ptr<ByteStreamReader> stream_reader;
193 CreateByteStream(
194 base::MessageLoopProxy::current(),
195 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
196 kDownloadByteStreamSize, &stream_writer_, &stream_reader);
197 stream_writer_->RegisterCallback(
198 base::Bind(&DownloadResourceHandler::ResumeRequest, AsWeakPtr()));
200 info->download_id = download_id_;
201 info->url_chain = request()->url_chain();
202 info->referrer_url = GURL(request()->referrer());
203 info->mime_type = response->head.mime_type;
204 info->remote_address = request()->GetSocketAddress().host();
205 request()->GetResponseHeaderByName("content-disposition",
206 &info->content_disposition);
207 RecordDownloadMimeType(info->mime_type);
208 RecordDownloadContentDisposition(info->content_disposition);
210 info->request_handle =
211 DownloadRequestHandle(AsWeakPtr(), request_info->GetChildID(),
212 request_info->GetRouteID(),
213 request_info->GetRequestID());
215 // Get the last modified time and etag.
216 const net::HttpResponseHeaders* headers = request()->response_headers();
217 if (headers) {
218 if (headers->HasStrongValidators()) {
219 // If we don't have strong validators as per RFC 2616 section 13.3.3, then
220 // we neither store nor use them for range requests.
221 if (!headers->EnumerateHeader(NULL, "Last-Modified",
222 &info->last_modified))
223 info->last_modified.clear();
224 if (!headers->EnumerateHeader(NULL, "ETag", &info->etag))
225 info->etag.clear();
228 int status = headers->response_code();
229 if (2 == status / 100 && status != net::HTTP_PARTIAL_CONTENT) {
230 // Success & not range response; if we asked for a range, we didn't
231 // get it--reset the file pointers to reflect that.
232 info->save_info->offset = 0;
233 info->save_info->hash_state = "";
236 if (!headers->GetMimeType(&info->original_mime_type))
237 info->original_mime_type.clear();
240 // Blink verifies that the requester of this download is allowed to set a
241 // suggested name for the security origin of the downlaod URL. However, this
242 // assumption doesn't hold if there were cross origin redirects. Therefore,
243 // clear the suggested_name for such requests.
244 if (info->url_chain.size() > 1 &&
245 info->url_chain.front().GetOrigin() != info->url_chain.back().GetOrigin())
246 info->save_info->suggested_name.clear();
248 BrowserThread::PostTask(
249 BrowserThread::UI, FROM_HERE,
250 base::Bind(&StartOnUIThread,
251 base::Passed(&info),
252 base::Passed(&tab_info_),
253 base::Passed(&stream_reader),
254 // Pass to StartOnUIThread so that variable
255 // access is always on IO thread but function
256 // is called on UI thread.
257 started_cb_));
258 // Guaranteed to be called in StartOnUIThread
259 started_cb_.Reset();
261 return true;
264 void DownloadResourceHandler::CallStartedCB(
265 DownloadItem* item,
266 DownloadInterruptReason interrupt_reason) {
267 DCHECK_CURRENTLY_ON(BrowserThread::IO);
268 if (started_cb_.is_null())
269 return;
270 BrowserThread::PostTask(
271 BrowserThread::UI,
272 FROM_HERE,
273 base::Bind(
274 &CallStartedCBOnUIThread, started_cb_, item, interrupt_reason));
275 started_cb_.Reset();
278 bool DownloadResourceHandler::OnWillStart(const GURL& url, bool* defer) {
279 return true;
282 bool DownloadResourceHandler::OnBeforeNetworkStart(const GURL& url,
283 bool* defer) {
284 return true;
287 // Create a new buffer, which will be handed to the download thread for file
288 // writing and deletion.
289 bool DownloadResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
290 int* buf_size,
291 int min_size) {
292 DCHECK_CURRENTLY_ON(BrowserThread::IO);
293 DCHECK(buf && buf_size);
294 DCHECK(!read_buffer_.get());
296 *buf_size = min_size < 0 ? kReadBufSize : min_size;
297 last_buffer_size_ = *buf_size;
298 read_buffer_ = new net::IOBuffer(*buf_size);
299 *buf = read_buffer_.get();
300 return true;
303 // Pass the buffer to the download file writer.
304 bool DownloadResourceHandler::OnReadCompleted(int bytes_read, bool* defer) {
305 DCHECK_CURRENTLY_ON(BrowserThread::IO);
306 DCHECK(read_buffer_.get());
308 base::TimeTicks now(base::TimeTicks::Now());
309 if (!last_read_time_.is_null()) {
310 double seconds_since_last_read = (now - last_read_time_).InSecondsF();
311 if (now == last_read_time_)
312 // Use 1/10 ms as a "very small number" so that we avoid
313 // divide-by-zero error and still record a very high potential bandwidth.
314 seconds_since_last_read = 0.00001;
316 double actual_bandwidth = (bytes_read)/seconds_since_last_read;
317 double potential_bandwidth = last_buffer_size_/seconds_since_last_read;
318 RecordBandwidth(actual_bandwidth, potential_bandwidth);
320 last_read_time_ = now;
322 if (!bytes_read)
323 return true;
324 bytes_read_ += bytes_read;
325 DCHECK(read_buffer_.get());
327 // Take the data ship it down the stream. If the stream is full, pause the
328 // request; the stream callback will resume it.
329 if (!stream_writer_->Write(read_buffer_, bytes_read)) {
330 PauseRequest();
331 *defer = was_deferred_ = true;
332 last_stream_pause_time_ = now;
335 read_buffer_ = NULL; // Drop our reference.
337 if (pause_count_ > 0)
338 *defer = was_deferred_ = true;
340 return true;
343 void DownloadResourceHandler::OnResponseCompleted(
344 const net::URLRequestStatus& status,
345 const std::string& security_info,
346 bool* defer) {
347 DCHECK_CURRENTLY_ON(BrowserThread::IO);
348 int response_code = status.is_success() ? request()->GetResponseCode() : 0;
349 DVLOG(20) << __FUNCTION__ << "()" << DebugString()
350 << " status.status() = " << status.status()
351 << " status.error() = " << status.error()
352 << " response_code = " << response_code;
354 net::Error error_code = net::OK;
355 if (status.status() == net::URLRequestStatus::FAILED ||
356 // Note cancels as failures too.
357 status.status() == net::URLRequestStatus::CANCELED) {
358 error_code = static_cast<net::Error>(status.error()); // Normal case.
359 // Make sure that at least the fact of failure comes through.
360 if (error_code == net::OK)
361 error_code = net::ERR_FAILED;
364 // ERR_CONTENT_LENGTH_MISMATCH and ERR_INCOMPLETE_CHUNKED_ENCODING are
365 // allowed since a number of servers in the wild close the connection too
366 // early by mistake. Other browsers - IE9, Firefox 11.0, and Safari 5.1.4 -
367 // treat downloads as complete in both cases, so we follow their lead.
368 if (error_code == net::ERR_CONTENT_LENGTH_MISMATCH ||
369 error_code == net::ERR_INCOMPLETE_CHUNKED_ENCODING) {
370 error_code = net::OK;
372 DownloadInterruptReason reason =
373 ConvertNetErrorToInterruptReason(
374 error_code, DOWNLOAD_INTERRUPT_FROM_NETWORK);
376 if (status.status() == net::URLRequestStatus::CANCELED &&
377 status.error() == net::ERR_ABORTED) {
378 // CANCELED + ERR_ABORTED == something outside of the network
379 // stack cancelled the request. There aren't that many things that
380 // could do this to a download request (whose lifetime is separated from
381 // the tab from which it came). We map this to USER_CANCELLED as the
382 // case we know about (system suspend because of laptop close) corresponds
383 // to a user action.
384 // TODO(ahendrickson) -- Find a better set of codes to use here, as
385 // CANCELED/ERR_ABORTED can occur for reasons other than user cancel.
386 if (net::IsCertStatusError(request()->ssl_info().cert_status))
387 reason = DOWNLOAD_INTERRUPT_REASON_SERVER_CERT_PROBLEM;
388 else
389 reason = DOWNLOAD_INTERRUPT_REASON_USER_CANCELED;
392 if (status.is_success() &&
393 reason == DOWNLOAD_INTERRUPT_REASON_NONE &&
394 request()->response_headers()) {
395 // Handle server's response codes.
396 switch(response_code) {
397 case -1: // Non-HTTP request.
398 case net::HTTP_OK:
399 case net::HTTP_CREATED:
400 case net::HTTP_ACCEPTED:
401 case net::HTTP_NON_AUTHORITATIVE_INFORMATION:
402 case net::HTTP_RESET_CONTENT:
403 case net::HTTP_PARTIAL_CONTENT:
404 // Expected successful codes.
405 break;
406 case net::HTTP_NO_CONTENT:
407 case net::HTTP_NOT_FOUND:
408 reason = DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT;
409 break;
410 case net::HTTP_PRECONDITION_FAILED:
411 // Failed our 'If-Unmodified-Since' or 'If-Match'; see
412 // download_manager_impl.cc BeginDownload()
413 reason = DOWNLOAD_INTERRUPT_REASON_SERVER_PRECONDITION;
414 break;
415 case net::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE:
416 // Retry by downloading from the start automatically:
417 // If we haven't received data when we get this error, we won't.
418 reason = DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE;
419 break;
420 case net::HTTP_UNAUTHORIZED:
421 // Server didn't authorize this request.
422 reason = DOWNLOAD_INTERRUPT_REASON_SERVER_UNAUTHORIZED;
423 break;
424 default: // All other errors.
425 // Redirection and informational codes should have been handled earlier
426 // in the stack.
427 DCHECK_NE(3, response_code / 100);
428 DCHECK_NE(1, response_code / 100);
429 reason = DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED;
430 break;
434 std::string accept_ranges;
435 bool has_strong_validators = false;
436 if (request()->response_headers()) {
437 request()->response_headers()->EnumerateHeader(
438 NULL, "Accept-Ranges", &accept_ranges);
439 has_strong_validators =
440 request()->response_headers()->HasStrongValidators();
442 RecordAcceptsRanges(accept_ranges, bytes_read_, has_strong_validators);
443 RecordNetworkBlockage(base::TimeTicks::Now() - download_start_time_,
444 total_pause_time_);
446 CallStartedCB(NULL, reason);
448 // Send the info down the stream. Conditional is in case we get
449 // OnResponseCompleted without OnResponseStarted.
450 if (stream_writer_)
451 stream_writer_->Close(reason);
453 // If the error mapped to something unknown, record it so that
454 // we can drill down.
455 if (reason == DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED) {
456 UMA_HISTOGRAM_CUSTOM_ENUMERATION("Download.MapErrorNetworkFailed",
457 std::abs(status.error()),
458 net::GetAllErrorCodesForUma());
461 stream_writer_.reset(); // We no longer need the stream.
462 read_buffer_ = NULL;
465 void DownloadResourceHandler::OnDataDownloaded(int bytes_downloaded) {
466 NOTREACHED();
469 void DownloadResourceHandler::PauseRequest() {
470 DCHECK_CURRENTLY_ON(BrowserThread::IO);
472 ++pause_count_;
475 void DownloadResourceHandler::ResumeRequest() {
476 DCHECK_CURRENTLY_ON(BrowserThread::IO);
477 DCHECK_LT(0, pause_count_);
479 --pause_count_;
481 if (!was_deferred_)
482 return;
483 if (pause_count_ > 0)
484 return;
486 was_deferred_ = false;
487 if (!last_stream_pause_time_.is_null()) {
488 total_pause_time_ += (base::TimeTicks::Now() - last_stream_pause_time_);
489 last_stream_pause_time_ = base::TimeTicks();
492 controller()->Resume();
495 void DownloadResourceHandler::CancelRequest() {
496 DCHECK_CURRENTLY_ON(BrowserThread::IO);
498 const ResourceRequestInfo* info = GetRequestInfo();
499 ResourceDispatcherHostImpl::Get()->CancelRequest(
500 info->GetChildID(),
501 info->GetRequestID());
502 // This object has been deleted.
505 std::string DownloadResourceHandler::DebugString() const {
506 const ResourceRequestInfo* info = GetRequestInfo();
507 return base::StringPrintf("{"
508 " url_ = " "\"%s\""
509 " info = {"
510 " child_id = " "%d"
511 " request_id = " "%d"
512 " route_id = " "%d"
513 " }"
514 " }",
515 request() ?
516 request()->url().spec().c_str() :
517 "<NULL request>",
518 info->GetChildID(),
519 info->GetRequestID(),
520 info->GetRouteID());
523 DownloadResourceHandler::~DownloadResourceHandler() {
524 DCHECK_CURRENTLY_ON(BrowserThread::IO);
526 // This won't do anything if the callback was called before.
527 // If it goes through, it will likely be because OnWillStart() returned
528 // false somewhere in the chain of resource handlers.
529 CallStartedCB(NULL, DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED);
531 // Remove output stream callback if a stream exists.
532 if (stream_writer_)
533 stream_writer_->RegisterCallback(base::Closure());
535 // tab_info_ must be destroyed on UI thread, since
536 // InitializeDownloadTabInfoOnUIThread might still be using it.
537 if (tab_info_.get()) {
538 BrowserThread::PostTask(
539 BrowserThread::UI, FROM_HERE,
540 base::Bind(&DeleteOnUIThread, base::Passed(&tab_info_)));
543 UMA_HISTOGRAM_TIMES("SB2.DownloadDuration",
544 base::TimeTicks::Now() - download_start_time_);
547 } // namespace content