Home | History | Annotate | Download | only in 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 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_
      6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/callback.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/timer/timer.h"
     13 #include "content/browser/loader/resource_handler.h"
     14 #include "content/public/browser/download_manager.h"
     15 #include "content/public/browser/download_save_info.h"
     16 #include "content/public/browser/download_url_parameters.h"
     17 #include "content/public/browser/global_request_id.h"
     18 #include "net/base/net_errors.h"
     19 
     20 
     21 namespace net {
     22 class URLRequest;
     23 }  // namespace net
     24 
     25 namespace content {
     26 class ByteStreamWriter;
     27 class ByteStreamReader;
     28 class DownloadRequestHandle;
     29 struct DownloadCreateInfo;
     30 
     31 // Forwards data to the download thread.
     32 class CONTENT_EXPORT DownloadResourceHandler
     33     : public ResourceHandler,
     34       public base::SupportsWeakPtr<DownloadResourceHandler> {
     35  public:
     36   // Size of the buffer used between the DownloadResourceHandler and the
     37   // downstream receiver of its output.
     38   static const int kDownloadByteStreamSize;
     39 
     40   // started_cb will be called exactly once on the UI thread.
     41   // |id| should be invalid if the id should be automatically assigned.
     42   DownloadResourceHandler(
     43       uint32 id,
     44       net::URLRequest* request,
     45       const DownloadUrlParameters::OnStartedCallback& started_cb,
     46       scoped_ptr<DownloadSaveInfo> save_info);
     47 
     48   virtual bool OnUploadProgress(int request_id,
     49                                 uint64 position,
     50                                 uint64 size) OVERRIDE;
     51 
     52   // Not needed, as this event handler ought to be the final resource.
     53   virtual bool OnRequestRedirected(int request_id,
     54                                    const GURL& url,
     55                                    ResourceResponse* response,
     56                                    bool* defer) OVERRIDE;
     57 
     58   // Send the download creation information to the download thread.
     59   virtual bool OnResponseStarted(int request_id,
     60                                  ResourceResponse* response,
     61                                  bool* defer) OVERRIDE;
     62 
     63   // Pass-through implementation.
     64   virtual bool OnWillStart(int request_id,
     65                            const GURL& url,
     66                            bool* defer) OVERRIDE;
     67 
     68   // Create a new buffer, which will be handed to the download thread for file
     69   // writing and deletion.
     70   virtual bool OnWillRead(int request_id,
     71                           net::IOBuffer** buf,
     72                           int* buf_size,
     73                           int min_size) OVERRIDE;
     74 
     75   virtual bool OnReadCompleted(int request_id, int bytes_read,
     76                                bool* defer) OVERRIDE;
     77 
     78   virtual bool OnResponseCompleted(int request_id,
     79                                    const net::URLRequestStatus& status,
     80                                    const std::string& security_info) OVERRIDE;
     81 
     82   // N/A to this flavor of DownloadHandler.
     83   virtual void OnDataDownloaded(int request_id, int bytes_downloaded) OVERRIDE;
     84 
     85   void PauseRequest();
     86   void ResumeRequest();
     87   void CancelRequest();
     88 
     89   std::string DebugString() const;
     90 
     91  private:
     92   virtual ~DownloadResourceHandler();
     93 
     94   // Arrange for started_cb_ to be called on the UI thread with the
     95   // below values, nulling out started_cb_.  Should only be called
     96   // on the IO thread.
     97   void CallStartedCB(DownloadItem* item, net::Error error);
     98 
     99   // If the content-length header is not present (or contains something other
    100   // than numbers), the incoming content_length is -1 (unknown size).
    101   // Set the content length to 0 to indicate unknown size to DownloadManager.
    102   void SetContentLength(const int64& content_length);
    103 
    104   void SetContentDisposition(const std::string& content_disposition);
    105 
    106   uint32 download_id_;
    107   GlobalRequestID global_id_;
    108   int render_view_id_;
    109   std::string content_disposition_;
    110   int64 content_length_;
    111   net::URLRequest* request_;
    112   // This is read only on the IO thread, but may only
    113   // be called on the UI thread.
    114   DownloadUrlParameters::OnStartedCallback started_cb_;
    115   scoped_ptr<DownloadSaveInfo> save_info_;
    116 
    117   // Data flow
    118   scoped_refptr<net::IOBuffer> read_buffer_;       // From URLRequest.
    119   scoped_ptr<ByteStreamWriter> stream_writer_; // To rest of system.
    120 
    121   // The following are used to collect stats.
    122   base::TimeTicks download_start_time_;
    123   base::TimeTicks last_read_time_;
    124   base::TimeTicks last_stream_pause_time_;
    125   base::TimeDelta total_pause_time_;
    126   size_t last_buffer_size_;
    127   int64 bytes_read_;
    128   std::string accept_ranges_;
    129   std::string etag_;
    130 
    131   int pause_count_;
    132   bool was_deferred_;
    133 
    134   // For DCHECKing
    135   bool on_response_started_called_;
    136 
    137   static const int kReadBufSize = 32768;  // bytes
    138   static const int kThrottleTimeMs = 200;  // milliseconds
    139 
    140   DISALLOW_COPY_AND_ASSIGN(DownloadResourceHandler);
    141 };
    142 
    143 }  // namespace content
    144 
    145 #endif  // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_
    146