0a73a958e03260ad2fd8ac39e620fb03cd9e11a8
[platform/framework/web/crosswalk.git] / src / content / browser / download / download_resource_handler.cc
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.
4
5 #include "content/browser/download/download_resource_handler.h"
6
7 #include <string>
8
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/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"
35
36 namespace content {
37
38 struct DownloadResourceHandler::DownloadTabInfo {
39   GURL tab_url;
40   GURL tab_referrer_url;
41 };
42
43 namespace {
44
45 void CallStartedCBOnUIThread(
46     const DownloadUrlParameters::OnStartedCallback& started_cb,
47     DownloadItem* item,
48     DownloadInterruptReason interrupt_reason) {
49   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
50
51   if (started_cb.is_null())
52     return;
53   started_cb.Run(item, interrupt_reason);
54 }
55
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));
64
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);
71
72     // |stream| gets deleted on non-FILE thread, but it's ok since
73     // we're not using stream_writer_ yet.
74
75     return;
76   }
77
78   info->tab_url = tab_info->tab_url;
79   info->tab_referrer_url = tab_info->tab_referrer_url;
80
81   download_manager->StartDownload(info.Pass(), stream.Pass(), started_cb);
82 }
83
84 void InitializeDownloadTabInfoOnUIThread(
85     const DownloadRequestHandle& request_handle,
86     DownloadResourceHandler::DownloadTabInfo* tab_info) {
87   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
88
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;
95     }
96   }
97 }
98
99 }  // namespace
100
101 const int DownloadResourceHandler::kDownloadByteStreamSize = 100 * 1024;
102
103 DownloadResourceHandler::DownloadResourceHandler(
104     uint32 id,
105     net::URLRequest* request,
106     const DownloadUrlParameters::OnStartedCallback& started_cb,
107     scoped_ptr<DownloadSaveInfo> save_info)
108     : ResourceHandler(request),
109       download_id_(id),
110       started_cb_(started_cb),
111       save_info_(save_info.Pass()),
112       last_buffer_size_(0),
113       bytes_read_(0),
114       pause_count_(0),
115       was_deferred_(false),
116       on_response_started_called_(false) {
117   RecordDownloadCount(UNTHROTTLED_COUNT);
118
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(
124       BrowserThread::UI,
125       FROM_HERE,
126       base::Bind(&InitializeDownloadTabInfoOnUIThread,
127                  DownloadRequestHandle(AsWeakPtr(),
128                                        request_info->GetChildID(),
129                                        request_info->GetRouteID(),
130                                        request_info->GetRequestID()),
131                  tab_info_));
132 }
133
134 bool DownloadResourceHandler::OnUploadProgress(int request_id,
135                                                uint64 position,
136                                                uint64 size) {
137   return true;
138 }
139
140 bool DownloadResourceHandler::OnRequestRedirected(
141     int request_id,
142     const GURL& url,
143     ResourceResponse* response,
144     bool* defer) {
145   // We treat a download as a main frame load, and thus update the policy URL
146   // on redirects.
147   request()->set_first_party_for_cookies(url);
148   return true;
149 }
150
151 // Send the download creation information to the download thread.
152 bool DownloadResourceHandler::OnResponseStarted(
153     int request_id,
154     ResourceResponse* response,
155     bool* defer) {
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;
160
161   VLOG(20) << __FUNCTION__ << "()" << DebugString()
162            << " request_id = " << request_id;
163   download_start_time_ = base::TimeTicks::Now();
164
165   // If it's a download, we don't want to poison the cache with it.
166   request()->StopCaching();
167
168   // Lower priority as well, so downloads don't contend for resources
169   // with main frames.
170   request()->SetPriority(net::IDLE);
171
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;
177
178   const ResourceRequestInfoImpl* request_info = GetRequestInfo();
179
180   // Deleted in DownloadManager.
181   scoped_ptr<DownloadCreateInfo> info(
182       new DownloadCreateInfo(base::Time::Now(),
183                              content_length,
184                              request()->net_log(),
185                              request_info->HasUserGesture(),
186                              request_info->GetPageTransition(),
187                              save_info_.Pass()));
188
189   // Create the ByteStream for sending data to the download sink.
190   scoped_ptr<ByteStreamReader> stream_reader;
191   CreateByteStream(
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()));
197
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);
207
208   info->request_handle =
209       DownloadRequestHandle(AsWeakPtr(), request_info->GetChildID(),
210                             request_info->GetRouteID(),
211                             request_info->GetRequestID());
212
213   // Get the last modified time and etag.
214   const net::HttpResponseHeaders* headers = request()->response_headers();
215   if (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))
223         info->etag.clear();
224     }
225
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 = "";
232     }
233
234     if (!headers->GetMimeType(&info->original_mime_type))
235       info->original_mime_type.clear();
236   }
237
238   BrowserThread::PostTask(
239       BrowserThread::UI, FROM_HERE,
240       base::Bind(&StartOnUIThread,
241                  base::Passed(&info),
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.
247                  started_cb_));
248   // Now owned by the task that was just posted.
249   tab_info_ = NULL;
250   // Guaranteed to be called in StartOnUIThread
251   started_cb_.Reset();
252
253   return true;
254 }
255
256 void DownloadResourceHandler::CallStartedCB(
257     DownloadItem* item,
258     DownloadInterruptReason interrupt_reason) {
259   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
260   if (started_cb_.is_null())
261     return;
262   BrowserThread::PostTask(
263       BrowserThread::UI,
264       FROM_HERE,
265       base::Bind(
266           &CallStartedCBOnUIThread, started_cb_, item, interrupt_reason));
267   started_cb_.Reset();
268 }
269
270 bool DownloadResourceHandler::OnWillStart(int request_id,
271                                           const GURL& url,
272                                           bool* defer) {
273   return true;
274 }
275
276 bool DownloadResourceHandler::OnBeforeNetworkStart(int request_id,
277                                                    const GURL& url,
278                                                    bool* defer) {
279   return true;
280 }
281
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,
286                                          int* buf_size,
287                                          int min_size) {
288   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
289   DCHECK(buf && buf_size);
290   DCHECK(!read_buffer_.get());
291
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();
296   return true;
297 }
298
299 // Pass the buffer to the download file writer.
300 bool DownloadResourceHandler::OnReadCompleted(int request_id, int bytes_read,
301                                               bool* defer) {
302   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
303   DCHECK(read_buffer_.get());
304
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;
312
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);
316   }
317   last_read_time_ = now;
318
319   if (!bytes_read)
320     return true;
321   bytes_read_ += bytes_read;
322   DCHECK(read_buffer_.get());
323
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)) {
327     PauseRequest();
328     *defer = was_deferred_ = true;
329     last_stream_pause_time_ = now;
330   }
331
332   read_buffer_ = NULL;  // Drop our reference.
333
334   if (pause_count_ > 0)
335     *defer = was_deferred_ = true;
336
337   return true;
338 }
339
340 void DownloadResourceHandler::OnResponseCompleted(
341     int request_id,
342     const net::URLRequestStatus& status,
343     const std::string& security_info,
344     bool* defer) {
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;
352
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;
361   }
362
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;
370   }
371   DownloadInterruptReason reason =
372       ConvertNetErrorToInterruptReason(
373         error_code, DOWNLOAD_INTERRUPT_FROM_NETWORK);
374
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
382     // to a user action.
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;
386   }
387
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.
394       case net::HTTP_OK:
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.
401         break;
402       case net::HTTP_NO_CONTENT:
403       case net::HTTP_NOT_FOUND:
404         reason = DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT;
405         break;
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;
410         break;
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;
415         break;
416       default:    // All other errors.
417         // Redirection and informational codes should have been handled earlier
418         // in the stack.
419         DCHECK_NE(3, response_code / 100);
420         DCHECK_NE(1, response_code / 100);
421         reason = DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED;
422         break;
423     }
424   }
425
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();
433   }
434   RecordAcceptsRanges(accept_ranges, bytes_read_, has_strong_validators);
435   RecordNetworkBlockage(base::TimeTicks::Now() - download_start_time_,
436                         total_pause_time_);
437
438   CallStartedCB(NULL, reason);
439
440   // Send the info down the stream.  Conditional is in case we get
441   // OnResponseCompleted without OnResponseStarted.
442   if (stream_writer_)
443     stream_writer_->Close(reason);
444
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());
451   }
452
453   stream_writer_.reset();  // We no longer need the stream.
454   read_buffer_ = NULL;
455 }
456
457 void DownloadResourceHandler::OnDataDownloaded(
458     int request_id,
459     int bytes_downloaded) {
460   NOTREACHED();
461 }
462
463 void DownloadResourceHandler::PauseRequest() {
464   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
465
466   ++pause_count_;
467 }
468
469 void DownloadResourceHandler::ResumeRequest() {
470   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
471   DCHECK_LT(0, pause_count_);
472
473   --pause_count_;
474
475   if (!was_deferred_)
476     return;
477   if (pause_count_ > 0)
478     return;
479
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();
484   }
485
486   controller()->Resume();
487 }
488
489 void DownloadResourceHandler::CancelRequest() {
490   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
491
492   const ResourceRequestInfo* info = GetRequestInfo();
493   ResourceDispatcherHostImpl::Get()->CancelRequest(
494       info->GetChildID(),
495       info->GetRequestID());
496   // This object has been deleted.
497 }
498
499 std::string DownloadResourceHandler::DebugString() const {
500   const ResourceRequestInfo* info = GetRequestInfo();
501   return base::StringPrintf("{"
502                             " url_ = " "\"%s\""
503                             " info = {"
504                             " child_id = " "%d"
505                             " request_id = " "%d"
506                             " route_id = " "%d"
507                             " }"
508                             " }",
509                             request() ?
510                                 request()->url().spec().c_str() :
511                                 "<NULL request>",
512                             info->GetChildID(),
513                             info->GetRequestID(),
514                             info->GetRouteID());
515 }
516
517 DownloadResourceHandler::~DownloadResourceHandler() {
518   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
519
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);
524
525   // Remove output stream callback if a stream exists.
526   if (stream_writer_)
527     stream_writer_->RegisterCallback(base::Closure());
528
529   // tab_info_ must be destroyed on UI thread, since
530   // InitializeDownloadTabInfoOnUIThread might still be using it.
531   if (tab_info_)
532     BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, tab_info_);
533
534   UMA_HISTOGRAM_TIMES("SB2.DownloadDuration",
535                       base::TimeTicks::Now() - download_start_time_);
536 }
537
538 }  // namespace content