download_resource_handler.h revision 3551c9c881056c480085172ff9840cab31610854
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 21namespace net { 22class URLRequest; 23} // namespace net 24 25namespace content { 26class ByteStreamWriter; 27class ByteStreamReader; 28class DownloadRequestHandle; 29struct DownloadCreateInfo; 30 31// Forwards data to the download thread. 32class 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 virtual bool OnRequestRedirected(int request_id, 53 const GURL& url, 54 ResourceResponse* response, 55 bool* defer) OVERRIDE; 56 57 // Send the download creation information to the download thread. 58 virtual bool OnResponseStarted(int request_id, 59 ResourceResponse* response, 60 bool* defer) OVERRIDE; 61 62 // Pass-through implementation. 63 virtual bool OnWillStart(int request_id, 64 const GURL& url, 65 bool* defer) OVERRIDE; 66 67 // Create a new buffer, which will be handed to the download thread for file 68 // writing and deletion. 69 virtual bool OnWillRead(int request_id, 70 net::IOBuffer** buf, 71 int* buf_size, 72 int min_size) OVERRIDE; 73 74 virtual bool OnReadCompleted(int request_id, int bytes_read, 75 bool* defer) OVERRIDE; 76 77 virtual bool OnResponseCompleted(int request_id, 78 const net::URLRequestStatus& status, 79 const std::string& security_info) OVERRIDE; 80 81 // N/A to this flavor of DownloadHandler. 82 virtual void OnDataDownloaded(int request_id, int bytes_downloaded) OVERRIDE; 83 84 void PauseRequest(); 85 void ResumeRequest(); 86 void CancelRequest(); 87 88 std::string DebugString() const; 89 90 private: 91 virtual ~DownloadResourceHandler(); 92 93 // Arrange for started_cb_ to be called on the UI thread with the 94 // below values, nulling out started_cb_. Should only be called 95 // on the IO thread. 96 void CallStartedCB(DownloadItem* item, net::Error error); 97 98 // If the content-length header is not present (or contains something other 99 // than numbers), the incoming content_length is -1 (unknown size). 100 // Set the content length to 0 to indicate unknown size to DownloadManager. 101 void SetContentLength(const int64& content_length); 102 103 void SetContentDisposition(const std::string& content_disposition); 104 105 uint32 download_id_; 106 GlobalRequestID global_id_; 107 int render_view_id_; 108 std::string content_disposition_; 109 int64 content_length_; 110 net::URLRequest* request_; 111 // This is read only on the IO thread, but may only 112 // be called on the UI thread. 113 DownloadUrlParameters::OnStartedCallback started_cb_; 114 scoped_ptr<DownloadSaveInfo> save_info_; 115 116 // Data flow 117 scoped_refptr<net::IOBuffer> read_buffer_; // From URLRequest. 118 scoped_ptr<ByteStreamWriter> stream_writer_; // To rest of system. 119 120 // The following are used to collect stats. 121 base::TimeTicks download_start_time_; 122 base::TimeTicks last_read_time_; 123 base::TimeTicks last_stream_pause_time_; 124 base::TimeDelta total_pause_time_; 125 size_t last_buffer_size_; 126 int64 bytes_read_; 127 std::string accept_ranges_; 128 std::string etag_; 129 130 int pause_count_; 131 bool was_deferred_; 132 133 // For DCHECKing 134 bool on_response_started_called_; 135 136 static const int kReadBufSize = 32768; // bytes 137 static const int kThrottleTimeMs = 200; // milliseconds 138 139 DISALLOW_COPY_AND_ASSIGN(DownloadResourceHandler); 140}; 141 142} // namespace content 143 144#endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_ 145