| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/renderer_host/download_throttling_resource_handler.h" | 5 #include "chrome/browser/download/download_throttling_resource_handler.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "chrome/browser/download/download_util.h" | 8 #include "chrome/browser/download/download_util.h" |
| 9 #include "content/browser/download/download_resource_handler.h" | |
| 10 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 9 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
| 11 #include "content/common/resource_response.h" | 10 #include "content/common/resource_response.h" |
| 12 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
| 13 #include "net/base/mime_sniffer.h" | 12 #include "net/base/mime_sniffer.h" |
| 14 | 13 |
| 15 DownloadThrottlingResourceHandler::DownloadThrottlingResourceHandler( | 14 DownloadThrottlingResourceHandler::DownloadThrottlingResourceHandler( |
| 15 ResourceHandler* next_handler, |
| 16 ResourceDispatcherHost* host, | 16 ResourceDispatcherHost* host, |
| 17 DownloadRequestLimiter* limiter, |
| 17 net::URLRequest* request, | 18 net::URLRequest* request, |
| 18 const GURL& url, | 19 const GURL& url, |
| 19 int render_process_host_id, | 20 int render_process_host_id, |
| 20 int render_view_id, | 21 int render_view_id, |
| 21 int request_id, | 22 int request_id, |
| 22 bool in_complete) | 23 bool in_complete) |
| 23 : host_(host), | 24 : host_(host), |
| 24 request_(request), | 25 request_(request), |
| 25 url_(url), | 26 url_(url), |
| 26 render_process_host_id_(render_process_host_id), | 27 render_process_host_id_(render_process_host_id), |
| 27 render_view_id_(render_view_id), | 28 render_view_id_(render_view_id), |
| 28 request_id_(request_id), | 29 request_id_(request_id), |
| 30 next_handler_(next_handler), |
| 31 request_allowed_(false), |
| 29 tmp_buffer_length_(0), | 32 tmp_buffer_length_(0), |
| 30 ignore_on_read_complete_(in_complete), | 33 ignore_on_read_complete_(in_complete), |
| 31 request_closed_(false) { | 34 request_closed_(false) { |
| 32 download_util::RecordDownloadCount( | 35 download_util::RecordDownloadCount( |
| 33 download_util::INITIATED_BY_NAVIGATION_COUNT); | 36 download_util::INITIATED_BY_NAVIGATION_COUNT); |
| 34 | 37 |
| 35 // Pause the request. | 38 // Pause the request. |
| 36 host_->PauseRequest(render_process_host_id_, request_id_, true); | 39 host_->PauseRequest(render_process_host_id_, request_id_, true); |
| 37 | 40 |
| 38 // Add a reference to ourselves to keep this object alive until we | 41 // Add a reference to ourselves to keep this object alive until we |
| 39 // receive a callback from DownloadRequestLimiter. The reference is | 42 // receive a callback from DownloadRequestLimiter. The reference is |
| 40 // released in ContinueDownload() and CancelDownload(). | 43 // released in ContinueDownload() and CancelDownload(). |
| 41 AddRef(); | 44 AddRef(); |
| 42 | 45 |
| 43 host_->download_request_limiter()->CanDownloadOnIOThread( | 46 limiter->CanDownloadOnIOThread( |
| 44 render_process_host_id_, render_view_id, request_id, this); | 47 render_process_host_id_, render_view_id, request_id, this); |
| 45 BrowserThread::PostTask( | 48 BrowserThread::PostTask( |
| 46 BrowserThread::UI, FROM_HERE, | 49 BrowserThread::UI, FROM_HERE, |
| 47 NewRunnableFunction(&download_util::NotifyDownloadInitiated, | 50 NewRunnableFunction(&download_util::NotifyDownloadInitiated, |
| 48 render_process_host_id_, render_view_id_)); | 51 render_process_host_id_, render_view_id_)); |
| 49 } | 52 } |
| 50 | 53 |
| 51 DownloadThrottlingResourceHandler::~DownloadThrottlingResourceHandler() { | 54 DownloadThrottlingResourceHandler::~DownloadThrottlingResourceHandler() { |
| 52 } | 55 } |
| 53 | 56 |
| 54 bool DownloadThrottlingResourceHandler::OnUploadProgress(int request_id, | 57 bool DownloadThrottlingResourceHandler::OnUploadProgress(int request_id, |
| 55 uint64 position, | 58 uint64 position, |
| 56 uint64 size) { | 59 uint64 size) { |
| 57 DCHECK(!request_closed_); | 60 DCHECK(!request_closed_); |
| 58 if (download_handler_.get()) | 61 if (request_allowed_) |
| 59 return download_handler_->OnUploadProgress(request_id, position, size); | 62 return next_handler_->OnUploadProgress(request_id, position, size); |
| 60 return true; | 63 return true; |
| 61 } | 64 } |
| 62 | 65 |
| 63 bool DownloadThrottlingResourceHandler::OnRequestRedirected( | 66 bool DownloadThrottlingResourceHandler::OnRequestRedirected( |
| 64 int request_id, | 67 int request_id, |
| 65 const GURL& url, | 68 const GURL& url, |
| 66 ResourceResponse* response, | 69 ResourceResponse* response, |
| 67 bool* defer) { | 70 bool* defer) { |
| 68 DCHECK(!request_closed_); | 71 DCHECK(!request_closed_); |
| 69 if (download_handler_.get()) { | 72 if (request_allowed_) { |
| 70 return download_handler_->OnRequestRedirected( | 73 return next_handler_->OnRequestRedirected(request_id, url, response, defer); |
| 71 request_id, url, response, defer); | |
| 72 } | 74 } |
| 73 url_ = url; | 75 url_ = url; |
| 74 return true; | 76 return true; |
| 75 } | 77 } |
| 76 | 78 |
| 77 bool DownloadThrottlingResourceHandler::OnResponseStarted( | 79 bool DownloadThrottlingResourceHandler::OnResponseStarted( |
| 78 int request_id, | 80 int request_id, |
| 79 ResourceResponse* response) { | 81 ResourceResponse* response) { |
| 80 DCHECK(!request_closed_); | 82 DCHECK(!request_closed_); |
| 81 if (download_handler_.get()) | 83 if (request_allowed_) |
| 82 return download_handler_->OnResponseStarted(request_id, response); | 84 return next_handler_->OnResponseStarted(request_id, response); |
| 83 response_ = response; | 85 response_ = response; |
| 84 return true; | 86 return true; |
| 85 } | 87 } |
| 86 | 88 |
| 87 bool DownloadThrottlingResourceHandler::OnWillStart(int request_id, | 89 bool DownloadThrottlingResourceHandler::OnWillStart(int request_id, |
| 88 const GURL& url, | 90 const GURL& url, |
| 89 bool* defer) { | 91 bool* defer) { |
| 90 DCHECK(!request_closed_); | 92 DCHECK(!request_closed_); |
| 91 if (download_handler_.get()) | 93 if (request_allowed_) |
| 92 return download_handler_->OnWillStart(request_id, url, defer); | 94 return next_handler_->OnWillStart(request_id, url, defer); |
| 93 return true; | 95 return true; |
| 94 } | 96 } |
| 95 | 97 |
| 96 bool DownloadThrottlingResourceHandler::OnWillRead(int request_id, | 98 bool DownloadThrottlingResourceHandler::OnWillRead(int request_id, |
| 97 net::IOBuffer** buf, | 99 net::IOBuffer** buf, |
| 98 int* buf_size, | 100 int* buf_size, |
| 99 int min_size) { | 101 int min_size) { |
| 100 DCHECK(!request_closed_); | 102 DCHECK(!request_closed_); |
| 101 if (download_handler_.get()) | 103 if (request_allowed_) |
| 102 return download_handler_->OnWillRead(request_id, buf, buf_size, min_size); | 104 return next_handler_->OnWillRead(request_id, buf, buf_size, min_size); |
| 103 | 105 |
| 104 // We should only have this invoked once, as such we only deal with one | 106 // We should only have this invoked once, as such we only deal with one |
| 105 // tmp buffer. | 107 // tmp buffer. |
| 106 DCHECK(!tmp_buffer_.get()); | 108 DCHECK(!tmp_buffer_.get()); |
| 107 // If the caller passed a negative |min_size| then chose an appropriate | 109 // If the caller passed a negative |min_size| then chose an appropriate |
| 108 // default. The BufferedResourceHandler requires this to be at least 2 times | 110 // default. The BufferedResourceHandler requires this to be at least 2 times |
| 109 // the size required for mime detection. | 111 // the size required for mime detection. |
| 110 if (min_size < 0) | 112 if (min_size < 0) |
| 111 min_size = 2 * net::kMaxBytesToSniff; | 113 min_size = 2 * net::kMaxBytesToSniff; |
| 112 tmp_buffer_ = new net::IOBuffer(min_size); | 114 tmp_buffer_ = new net::IOBuffer(min_size); |
| 113 *buf = tmp_buffer_.get(); | 115 *buf = tmp_buffer_.get(); |
| 114 *buf_size = min_size; | 116 *buf_size = min_size; |
| 115 return true; | 117 return true; |
| 116 } | 118 } |
| 117 | 119 |
| 118 bool DownloadThrottlingResourceHandler::OnReadCompleted(int request_id, | 120 bool DownloadThrottlingResourceHandler::OnReadCompleted(int request_id, |
| 119 int* bytes_read) { | 121 int* bytes_read) { |
| 120 DCHECK(!request_closed_); | 122 DCHECK(!request_closed_); |
| 121 if (ignore_on_read_complete_) { | 123 if (ignore_on_read_complete_) { |
| 122 // See comments above definition for details on this. | 124 // See comments above definition for details on this. |
| 123 ignore_on_read_complete_ = false; | 125 ignore_on_read_complete_ = false; |
| 124 return true; | 126 return true; |
| 125 } | 127 } |
| 126 if (!*bytes_read) | 128 if (!*bytes_read) |
| 127 return true; | 129 return true; |
| 128 | 130 |
| 129 if (tmp_buffer_.get()) { | 131 if (tmp_buffer_.get()) { |
| 130 DCHECK(!tmp_buffer_length_); | 132 DCHECK(!tmp_buffer_length_); |
| 131 tmp_buffer_length_ = *bytes_read; | 133 tmp_buffer_length_ = *bytes_read; |
| 132 if (download_handler_.get()) | 134 if (request_allowed_) |
| 133 CopyTmpBufferToDownloadHandler(); | 135 CopyTmpBufferToDownloadHandler(); |
| 134 return true; | 136 return true; |
| 135 } | 137 } |
| 136 if (download_handler_.get()) | 138 if (request_allowed_) |
| 137 return download_handler_->OnReadCompleted(request_id, bytes_read); | 139 return next_handler_->OnReadCompleted(request_id, bytes_read); |
| 138 return true; | 140 return true; |
| 139 } | 141 } |
| 140 | 142 |
| 141 bool DownloadThrottlingResourceHandler::OnResponseCompleted( | 143 bool DownloadThrottlingResourceHandler::OnResponseCompleted( |
| 142 int request_id, | 144 int request_id, |
| 143 const net::URLRequestStatus& status, | 145 const net::URLRequestStatus& status, |
| 144 const std::string& security_info) { | 146 const std::string& security_info) { |
| 145 DCHECK(!request_closed_); | 147 DCHECK(!request_closed_); |
| 146 if (download_handler_.get()) | 148 if (request_allowed_) |
| 147 return download_handler_->OnResponseCompleted(request_id, status, | 149 return next_handler_->OnResponseCompleted(request_id, status, |
| 148 security_info); | 150 security_info); |
| 149 | 151 |
| 150 // For a download, if ResourceDispatcher::Read() fails, | 152 // For a download, if ResourceDispatcher::Read() fails, |
| 151 // ResourceDispatcher::OnresponseStarted() will call | 153 // ResourceDispatcher::OnresponseStarted() will call |
| 152 // OnResponseCompleted(), and we will end up here with an error | 154 // OnResponseCompleted(), and we will end up here with an error |
| 153 // status. | 155 // status. |
| 154 if (!status.is_success()) | 156 if (!status.is_success()) |
| 155 return false; | 157 return false; |
| 156 NOTREACHED(); | 158 NOTREACHED(); |
| 157 return true; | 159 return true; |
| 158 } | 160 } |
| 159 | 161 |
| 160 void DownloadThrottlingResourceHandler::OnRequestClosed() { | 162 void DownloadThrottlingResourceHandler::OnRequestClosed() { |
| 161 DCHECK(!request_closed_); | 163 DCHECK(!request_closed_); |
| 162 if (download_handler_.get()) | 164 if (request_allowed_) |
| 163 download_handler_->OnRequestClosed(); | 165 next_handler_->OnRequestClosed(); |
| 164 request_closed_ = true; | 166 request_closed_ = true; |
| 165 } | 167 } |
| 166 | 168 |
| 167 void DownloadThrottlingResourceHandler::CancelDownload() { | 169 void DownloadThrottlingResourceHandler::CancelDownload() { |
| 168 if (!request_closed_) | 170 if (!request_closed_) |
| 169 host_->CancelRequest(render_process_host_id_, request_id_, false); | 171 host_->CancelRequest(render_process_host_id_, request_id_, false); |
| 170 Release(); // Release the additional reference from constructor. | 172 Release(); // Release the additional reference from constructor. |
| 171 } | 173 } |
| 172 | 174 |
| 173 void DownloadThrottlingResourceHandler::ContinueDownload() { | 175 void DownloadThrottlingResourceHandler::ContinueDownload() { |
| 174 DCHECK(!download_handler_.get()); | |
| 175 if (!request_closed_) { | 176 if (!request_closed_) { |
| 176 download_handler_ = | 177 request_allowed_ = true; |
| 177 new DownloadResourceHandler(host_, | |
| 178 render_process_host_id_, | |
| 179 render_view_id_, | |
| 180 request_id_, | |
| 181 url_, | |
| 182 host_->download_file_manager(), | |
| 183 request_, | |
| 184 false, | |
| 185 DownloadSaveInfo()); | |
| 186 if (response_.get()) | 178 if (response_.get()) |
| 187 download_handler_->OnResponseStarted(request_id_, response_.get()); | 179 next_handler_->OnResponseStarted(request_id_, response_.get()); |
| 188 | 180 |
| 189 if (tmp_buffer_length_) | 181 if (tmp_buffer_length_) |
| 190 CopyTmpBufferToDownloadHandler(); | 182 CopyTmpBufferToDownloadHandler(); |
| 191 | 183 |
| 192 // And let the request continue. | 184 // And let the request continue. |
| 193 host_->PauseRequest(render_process_host_id_, request_id_, false); | 185 host_->PauseRequest(render_process_host_id_, request_id_, false); |
| 194 } | 186 } |
| 195 Release(); // Release the addtional reference from constructor. | 187 Release(); // Release the addtional reference from constructor. |
| 196 } | 188 } |
| 197 | 189 |
| 198 void DownloadThrottlingResourceHandler::CopyTmpBufferToDownloadHandler() { | 190 void DownloadThrottlingResourceHandler::CopyTmpBufferToDownloadHandler() { |
| 199 // Copy over the tmp buffer. | 191 // Copy over the tmp buffer. |
| 200 net::IOBuffer* buffer; | 192 net::IOBuffer* buffer; |
| 201 int buf_size; | 193 int buf_size; |
| 202 if (download_handler_->OnWillRead(request_id_, &buffer, &buf_size, | 194 if (next_handler_->OnWillRead(request_id_, &buffer, &buf_size, |
| 203 tmp_buffer_length_)) { | 195 tmp_buffer_length_)) { |
| 204 CHECK(buf_size >= tmp_buffer_length_); | 196 CHECK(buf_size >= tmp_buffer_length_); |
| 205 memcpy(buffer->data(), tmp_buffer_->data(), tmp_buffer_length_); | 197 memcpy(buffer->data(), tmp_buffer_->data(), tmp_buffer_length_); |
| 206 download_handler_->OnReadCompleted(request_id_, &tmp_buffer_length_); | 198 next_handler_->OnReadCompleted(request_id_, &tmp_buffer_length_); |
| 207 } | 199 } |
| 208 tmp_buffer_length_ = 0; | 200 tmp_buffer_length_ = 0; |
| 209 tmp_buffer_ = NULL; | 201 tmp_buffer_ = NULL; |
| 210 } | 202 } |
| OLD | NEW |