• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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"
33 
34 namespace content {
35 namespace {
36 
CallStartedCBOnUIThread(const DownloadUrlParameters::OnStartedCallback & started_cb,DownloadItem * item,net::Error error)37 void CallStartedCBOnUIThread(
38     const DownloadUrlParameters::OnStartedCallback& started_cb,
39     DownloadItem* item,
40     net::Error error) {
41   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
42 
43   if (started_cb.is_null())
44     return;
45   started_cb.Run(item, error);
46 }
47 
48 // Static function in order to prevent any accidental accesses to
49 // DownloadResourceHandler members from the UI thread.
StartOnUIThread(scoped_ptr<DownloadCreateInfo> info,scoped_ptr<ByteStreamReader> stream,const DownloadUrlParameters::OnStartedCallback & started_cb)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));
55 
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
59     // download.
60     if (!started_cb.is_null())
61       started_cb.Run(NULL, net::ERR_ACCESS_DENIED);
62     return;
63   }
64 
65   download_manager->StartDownload(info.Pass(), stream.Pass(), started_cb);
66 }
67 
68 }  // namespace
69 
70 const int DownloadResourceHandler::kDownloadByteStreamSize = 100 * 1024;
71 
DownloadResourceHandler(uint32 id,net::URLRequest * request,const DownloadUrlParameters::OnStartedCallback & started_cb,scoped_ptr<DownloadSaveInfo> save_info)72 DownloadResourceHandler::DownloadResourceHandler(
73     uint32 id,
74     net::URLRequest* request,
75     const DownloadUrlParameters::OnStartedCallback& started_cb,
76     scoped_ptr<DownloadSaveInfo> save_info)
77     : ResourceHandler(request),
78       download_id_(id),
79       started_cb_(started_cb),
80       save_info_(save_info.Pass()),
81       last_buffer_size_(0),
82       bytes_read_(0),
83       pause_count_(0),
84       was_deferred_(false),
85       on_response_started_called_(false) {
86   RecordDownloadCount(UNTHROTTLED_COUNT);
87 }
88 
OnUploadProgress(int request_id,uint64 position,uint64 size)89 bool DownloadResourceHandler::OnUploadProgress(int request_id,
90                                                uint64 position,
91                                                uint64 size) {
92   return true;
93 }
94 
OnRequestRedirected(int request_id,const GURL & url,ResourceResponse * response,bool * defer)95 bool DownloadResourceHandler::OnRequestRedirected(
96     int request_id,
97     const GURL& url,
98     ResourceResponse* response,
99     bool* defer) {
100   // We treat a download as a main frame load, and thus update the policy URL
101   // on redirects.
102   request()->set_first_party_for_cookies(url);
103   return true;
104 }
105 
106 // Send the download creation information to the download thread.
OnResponseStarted(int request_id,ResourceResponse * response,bool * defer)107 bool DownloadResourceHandler::OnResponseStarted(
108     int request_id,
109     ResourceResponse* response,
110     bool* defer) {
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;
115 
116   VLOG(20) << __FUNCTION__ << "()" << DebugString()
117            << " request_id = " << request_id;
118   download_start_time_ = base::TimeTicks::Now();
119 
120   // If it's a download, we don't want to poison the cache with it.
121   request()->StopCaching();
122 
123   // Lower priority as well, so downloads don't contend for resources
124   // with main frames.
125   request()->SetPriority(net::IDLE);
126 
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;
132 
133   const ResourceRequestInfoImpl* request_info = GetRequestInfo();
134 
135   // Deleted in DownloadManager.
136   scoped_ptr<DownloadCreateInfo> info(
137       new DownloadCreateInfo(base::Time::Now(),
138                              content_length,
139                              request()->net_log(),
140                              request_info->HasUserGesture(),
141                              request_info->GetPageTransition(),
142                              save_info_.Pass()));
143 
144   // Create the ByteStream for sending data to the download sink.
145   scoped_ptr<ByteStreamReader> stream_reader;
146   CreateByteStream(
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()));
152 
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);
162 
163   info->request_handle =
164       DownloadRequestHandle(AsWeakPtr(), request_info->GetChildID(),
165                             request_info->GetRouteID(),
166                             request_info->GetRequestID());
167 
168   // Get the last modified time and etag.
169   const net::HttpResponseHeaders* headers = request()->response_headers();
170   if (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))
178         info->etag.clear();
179     }
180 
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 = "";
187     }
188 
189     if (!headers->GetMimeType(&info->original_mime_type))
190       info->original_mime_type.clear();
191   }
192 
193   BrowserThread::PostTask(
194       BrowserThread::UI, FROM_HERE,
195       base::Bind(&StartOnUIThread,
196                  base::Passed(&info),
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.
201                  started_cb_));
202   // Guaranteed to be called in StartOnUIThread
203   started_cb_.Reset();
204 
205   return true;
206 }
207 
CallStartedCB(DownloadItem * item,net::Error error)208 void DownloadResourceHandler::CallStartedCB(
209     DownloadItem* item, net::Error error) {
210   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
211   if (started_cb_.is_null())
212     return;
213   BrowserThread::PostTask(
214       BrowserThread::UI, FROM_HERE,
215       base::Bind(&CallStartedCBOnUIThread, started_cb_, item, error));
216   started_cb_.Reset();
217 }
218 
OnWillStart(int request_id,const GURL & url,bool * defer)219 bool DownloadResourceHandler::OnWillStart(int request_id,
220                                           const GURL& url,
221                                           bool* defer) {
222   return true;
223 }
224 
225 // Create a new buffer, which will be handed to the download thread for file
226 // writing and deletion.
OnWillRead(int request_id,scoped_refptr<net::IOBuffer> * buf,int * buf_size,int min_size)227 bool DownloadResourceHandler::OnWillRead(int request_id,
228                                          scoped_refptr<net::IOBuffer>* buf,
229                                          int* buf_size,
230                                          int min_size) {
231   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
232   DCHECK(buf && buf_size);
233   DCHECK(!read_buffer_.get());
234 
235   *buf_size = min_size < 0 ? kReadBufSize : min_size;
236   last_buffer_size_ = *buf_size;
237   read_buffer_ = new net::IOBuffer(*buf_size);
238   *buf = read_buffer_.get();
239   return true;
240 }
241 
242 // Pass the buffer to the download file writer.
OnReadCompleted(int request_id,int bytes_read,bool * defer)243 bool DownloadResourceHandler::OnReadCompleted(int request_id, int bytes_read,
244                                               bool* defer) {
245   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
246   DCHECK(read_buffer_.get());
247 
248   base::TimeTicks now(base::TimeTicks::Now());
249   if (!last_read_time_.is_null()) {
250     double seconds_since_last_read = (now - last_read_time_).InSecondsF();
251     if (now == last_read_time_)
252       // Use 1/10 ms as a "very small number" so that we avoid
253       // divide-by-zero error and still record a very high potential bandwidth.
254       seconds_since_last_read = 0.00001;
255 
256     double actual_bandwidth = (bytes_read)/seconds_since_last_read;
257     double potential_bandwidth = last_buffer_size_/seconds_since_last_read;
258     RecordBandwidth(actual_bandwidth, potential_bandwidth);
259   }
260   last_read_time_ = now;
261 
262   if (!bytes_read)
263     return true;
264   bytes_read_ += bytes_read;
265   DCHECK(read_buffer_.get());
266 
267   // Take the data ship it down the stream.  If the stream is full, pause the
268   // request; the stream callback will resume it.
269   if (!stream_writer_->Write(read_buffer_, bytes_read)) {
270     PauseRequest();
271     *defer = was_deferred_ = true;
272     last_stream_pause_time_ = now;
273   }
274 
275   read_buffer_ = NULL;  // Drop our reference.
276 
277   if (pause_count_ > 0)
278     *defer = was_deferred_ = true;
279 
280   return true;
281 }
282 
OnResponseCompleted(int request_id,const net::URLRequestStatus & status,const std::string & security_info,bool * defer)283 void DownloadResourceHandler::OnResponseCompleted(
284     int request_id,
285     const net::URLRequestStatus& status,
286     const std::string& security_info,
287     bool* defer) {
288   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
289   int response_code = status.is_success() ? request()->GetResponseCode() : 0;
290   VLOG(20) << __FUNCTION__ << "()" << DebugString()
291            << " request_id = " << request_id
292            << " status.status() = " << status.status()
293            << " status.error() = " << status.error()
294            << " response_code = " << response_code;
295 
296   net::Error error_code = net::OK;
297   if (status.status() == net::URLRequestStatus::FAILED ||
298       // Note cancels as failures too.
299       status.status() == net::URLRequestStatus::CANCELED) {
300     error_code = static_cast<net::Error>(status.error());  // Normal case.
301     // Make sure that at least the fact of failure comes through.
302     if (error_code == net::OK)
303       error_code = net::ERR_FAILED;
304   }
305 
306   // ERR_CONTENT_LENGTH_MISMATCH and ERR_INCOMPLETE_CHUNKED_ENCODING are
307   // allowed since a number of servers in the wild close the connection too
308   // early by mistake. Other browsers - IE9, Firefox 11.0, and Safari 5.1.4 -
309   // treat downloads as complete in both cases, so we follow their lead.
310   if (error_code == net::ERR_CONTENT_LENGTH_MISMATCH ||
311       error_code == net::ERR_INCOMPLETE_CHUNKED_ENCODING) {
312     error_code = net::OK;
313   }
314   DownloadInterruptReason reason =
315       ConvertNetErrorToInterruptReason(
316         error_code, DOWNLOAD_INTERRUPT_FROM_NETWORK);
317 
318   if (status.status() == net::URLRequestStatus::CANCELED &&
319       status.error() == net::ERR_ABORTED) {
320     // CANCELED + ERR_ABORTED == something outside of the network
321     // stack cancelled the request.  There aren't that many things that
322     // could do this to a download request (whose lifetime is separated from
323     // the tab from which it came).  We map this to USER_CANCELLED as the
324     // case we know about (system suspend because of laptop close) corresponds
325     // to a user action.
326     // TODO(ahendrickson) -- Find a better set of codes to use here, as
327     // CANCELED/ERR_ABORTED can occur for reasons other than user cancel.
328     reason = DOWNLOAD_INTERRUPT_REASON_USER_CANCELED;
329   }
330 
331   if (status.is_success() &&
332       reason == DOWNLOAD_INTERRUPT_REASON_NONE &&
333       request()->response_headers()) {
334     // Handle server's response codes.
335     switch(response_code) {
336       case -1:                          // Non-HTTP request.
337       case net::HTTP_OK:
338       case net::HTTP_CREATED:
339       case net::HTTP_ACCEPTED:
340       case net::HTTP_NON_AUTHORITATIVE_INFORMATION:
341       case net::HTTP_RESET_CONTENT:
342       case net::HTTP_PARTIAL_CONTENT:
343         // Expected successful codes.
344         break;
345       case net::HTTP_NO_CONTENT:
346       case net::HTTP_NOT_FOUND:
347         reason = DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT;
348         break;
349       case net::HTTP_PRECONDITION_FAILED:
350         // Failed our 'If-Unmodified-Since' or 'If-Match'; see
351         // download_manager_impl.cc BeginDownload()
352         reason = DOWNLOAD_INTERRUPT_REASON_SERVER_PRECONDITION;
353         break;
354       case net::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE:
355         // Retry by downloading from the start automatically:
356         // If we haven't received data when we get this error, we won't.
357         reason = DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE;
358         break;
359       default:    // All other errors.
360         // Redirection and informational codes should have been handled earlier
361         // in the stack.
362         DCHECK_NE(3, response_code / 100);
363         DCHECK_NE(1, response_code / 100);
364         reason = DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED;
365         break;
366     }
367   }
368 
369   std::string accept_ranges;
370   bool has_strong_validators = false;
371   if (request()->response_headers()) {
372     request()->response_headers()->EnumerateHeader(
373         NULL, "Accept-Ranges", &accept_ranges);
374     has_strong_validators =
375         request()->response_headers()->HasStrongValidators();
376   }
377   RecordAcceptsRanges(accept_ranges, bytes_read_, has_strong_validators);
378   RecordNetworkBlockage(base::TimeTicks::Now() - download_start_time_,
379                         total_pause_time_);
380 
381   CallStartedCB(NULL, error_code);
382 
383   // Send the info down the stream.  Conditional is in case we get
384   // OnResponseCompleted without OnResponseStarted.
385   if (stream_writer_)
386     stream_writer_->Close(reason);
387 
388   // If the error mapped to something unknown, record it so that
389   // we can drill down.
390   if (reason == DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED) {
391     UMA_HISTOGRAM_CUSTOM_ENUMERATION("Download.MapErrorNetworkFailed",
392                                      std::abs(status.error()),
393                                      net::GetAllErrorCodesForUma());
394   }
395 
396   stream_writer_.reset();  // We no longer need the stream.
397   read_buffer_ = NULL;
398 }
399 
OnDataDownloaded(int request_id,int bytes_downloaded)400 void DownloadResourceHandler::OnDataDownloaded(
401     int request_id,
402     int bytes_downloaded) {
403   NOTREACHED();
404 }
405 
PauseRequest()406 void DownloadResourceHandler::PauseRequest() {
407   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
408 
409   ++pause_count_;
410 }
411 
ResumeRequest()412 void DownloadResourceHandler::ResumeRequest() {
413   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
414   DCHECK_LT(0, pause_count_);
415 
416   --pause_count_;
417 
418   if (!was_deferred_)
419     return;
420   if (pause_count_ > 0)
421     return;
422 
423   was_deferred_ = false;
424   if (!last_stream_pause_time_.is_null()) {
425     total_pause_time_ += (base::TimeTicks::Now() - last_stream_pause_time_);
426     last_stream_pause_time_ = base::TimeTicks();
427   }
428 
429   controller()->Resume();
430 }
431 
CancelRequest()432 void DownloadResourceHandler::CancelRequest() {
433   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
434 
435   const ResourceRequestInfo* info = GetRequestInfo();
436   ResourceDispatcherHostImpl::Get()->CancelRequest(
437       info->GetChildID(),
438       info->GetRequestID(),
439       false);
440 }
441 
DebugString() const442 std::string DownloadResourceHandler::DebugString() const {
443   const ResourceRequestInfo* info = GetRequestInfo();
444   return base::StringPrintf("{"
445                             " url_ = " "\"%s\""
446                             " info = {"
447                             " child_id = " "%d"
448                             " request_id = " "%d"
449                             " route_id = " "%d"
450                             " }"
451                             " }",
452                             request() ?
453                                 request()->url().spec().c_str() :
454                                 "<NULL request>",
455                             info->GetChildID(),
456                             info->GetRequestID(),
457                             info->GetRouteID());
458 }
459 
~DownloadResourceHandler()460 DownloadResourceHandler::~DownloadResourceHandler() {
461   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
462 
463   // This won't do anything if the callback was called before.
464   // If it goes through, it will likely be because OnWillStart() returned
465   // false somewhere in the chain of resource handlers.
466   CallStartedCB(NULL, net::ERR_ACCESS_DENIED);
467 
468   // Remove output stream callback if a stream exists.
469   if (stream_writer_)
470     stream_writer_->RegisterCallback(base::Closure());
471 
472   UMA_HISTOGRAM_TIMES("SB2.DownloadDuration",
473                       base::TimeTicks::Now() - download_start_time_);
474 }
475 
476 }  // namespace content
477