| 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/renderer_host/download_throttling_resource_handler.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "chrome/browser/download/download_id.h" |
| 8 #include "chrome/browser/download/download_util.h" | 9 #include "chrome/browser/download/download_util.h" |
| 9 #include "chrome/browser/renderer_host/download_resource_handler.h" | 10 #include "chrome/browser/renderer_host/download_resource_handler.h" |
| 10 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 11 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
| 12 #include "content/browser/renderer_host/resource_dispatcher_host_request_info.h" |
| 13 #include "content/browser/resource_context.h" |
| 11 #include "content/common/resource_response.h" | 14 #include "content/common/resource_response.h" |
| 12 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
| 13 #include "net/base/mime_sniffer.h" | 16 #include "net/base/mime_sniffer.h" |
| 14 | 17 |
| 15 DownloadThrottlingResourceHandler::DownloadThrottlingResourceHandler( | 18 DownloadThrottlingResourceHandler::DownloadThrottlingResourceHandler( |
| 16 ResourceDispatcherHost* host, | 19 ResourceDispatcherHost* host, |
| 17 net::URLRequest* request, | 20 net::URLRequest* request, |
| 18 const GURL& url, | 21 const GURL& url, |
| 19 int render_process_host_id, | 22 int render_process_host_id, |
| 20 int render_view_id, | 23 int render_view_id, |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 | 169 |
| 167 void DownloadThrottlingResourceHandler::CancelDownload() { | 170 void DownloadThrottlingResourceHandler::CancelDownload() { |
| 168 if (!request_closed_) | 171 if (!request_closed_) |
| 169 host_->CancelRequest(render_process_host_id_, request_id_, false); | 172 host_->CancelRequest(render_process_host_id_, request_id_, false); |
| 170 Release(); // Release the additional reference from constructor. | 173 Release(); // Release the additional reference from constructor. |
| 171 } | 174 } |
| 172 | 175 |
| 173 void DownloadThrottlingResourceHandler::ContinueDownload() { | 176 void DownloadThrottlingResourceHandler::ContinueDownload() { |
| 174 DCHECK(!download_handler_.get()); | 177 DCHECK(!download_handler_.get()); |
| 175 if (!request_closed_) { | 178 if (!request_closed_) { |
| 179 ResourceDispatcherHostRequestInfo* info = |
| 180 ResourceDispatcherHost::InfoForRequest(request_); |
| 181 // TODO(benjhayden) next_download_id_thunk can be a field in ResourceContext |
| 182 // when DownloadManager is moved to content. |
| 183 DownloadManager::GetNextIdThunkType* next_download_id_thunk = |
| 184 reinterpret_cast<DownloadManager::GetNextIdThunkType*>(info->context() |
| 185 ->GetUserData(reinterpret_cast<void*>(BASE_HASH_NAMESPACE:: |
| 186 #if defined(COMPILER_GCC) |
| 187 hash<std::string>() |
| 188 #elif defined(COMPILER_MSVC) |
| 189 hash_value |
| 190 #endif |
| 191 ("next_download_id_thunk")))); |
| 192 CHECK(next_download_id_thunk); |
| 193 CHECK(!next_download_id_thunk->is_null()); |
| 194 DownloadId dl_id = next_download_id_thunk->Run(); |
| 176 download_handler_ = | 195 download_handler_ = |
| 177 new DownloadResourceHandler(host_, | 196 new DownloadResourceHandler(host_, |
| 178 render_process_host_id_, | 197 render_process_host_id_, |
| 179 render_view_id_, | 198 render_view_id_, |
| 180 request_id_, | 199 request_id_, |
| 181 url_, | 200 url_, |
| 201 dl_id, |
| 182 host_->download_file_manager(), | 202 host_->download_file_manager(), |
| 183 request_, | 203 request_, |
| 184 false, | 204 false, |
| 185 DownloadSaveInfo()); | 205 DownloadSaveInfo()); |
| 186 if (response_.get()) | 206 if (response_.get()) |
| 187 download_handler_->OnResponseStarted(request_id_, response_.get()); | 207 download_handler_->OnResponseStarted(request_id_, response_.get()); |
| 188 | 208 |
| 189 if (tmp_buffer_length_) | 209 if (tmp_buffer_length_) |
| 190 CopyTmpBufferToDownloadHandler(); | 210 CopyTmpBufferToDownloadHandler(); |
| 191 | 211 |
| 192 // And let the request continue. | 212 // And let the request continue. |
| 193 host_->PauseRequest(render_process_host_id_, request_id_, false); | 213 host_->PauseRequest(render_process_host_id_, request_id_, false); |
| 194 } | 214 } |
| 195 Release(); // Release the addtional reference from constructor. | 215 Release(); // Release the addtional reference from constructor. |
| 196 } | 216 } |
| 197 | 217 |
| 198 void DownloadThrottlingResourceHandler::CopyTmpBufferToDownloadHandler() { | 218 void DownloadThrottlingResourceHandler::CopyTmpBufferToDownloadHandler() { |
| 199 // Copy over the tmp buffer. | 219 // Copy over the tmp buffer. |
| 200 net::IOBuffer* buffer; | 220 net::IOBuffer* buffer; |
| 201 int buf_size; | 221 int buf_size; |
| 202 if (download_handler_->OnWillRead(request_id_, &buffer, &buf_size, | 222 if (download_handler_->OnWillRead(request_id_, &buffer, &buf_size, |
| 203 tmp_buffer_length_)) { | 223 tmp_buffer_length_)) { |
| 204 CHECK(buf_size >= tmp_buffer_length_); | 224 CHECK(buf_size >= tmp_buffer_length_); |
| 205 memcpy(buffer->data(), tmp_buffer_->data(), tmp_buffer_length_); | 225 memcpy(buffer->data(), tmp_buffer_->data(), tmp_buffer_length_); |
| 206 download_handler_->OnReadCompleted(request_id_, &tmp_buffer_length_); | 226 download_handler_->OnReadCompleted(request_id_, &tmp_buffer_length_); |
| 207 } | 227 } |
| 208 tmp_buffer_length_ = 0; | 228 tmp_buffer_length_ = 0; |
| 209 tmp_buffer_ = NULL; | 229 tmp_buffer_ = NULL; |
| 210 } | 230 } |
| OLD | NEW |