| 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 "content/browser/download/download_resource_handler.h" | 5 #include "content/browser/download/download_resource_handler.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 11 #include "base/metrics/stats_counters.h" | 12 #include "base/metrics/stats_counters.h" |
| 12 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
| 13 #include "content/browser/browser_thread.h" | 14 #include "content/browser/browser_thread.h" |
| 14 #include "content/browser/download/download_buffer.h" | 15 #include "content/browser/download/download_buffer.h" |
| 15 #include "content/browser/download/download_create_info.h" | 16 #include "content/browser/download/download_create_info.h" |
| 16 #include "content/browser/download/download_file_manager.h" | 17 #include "content/browser/download/download_file_manager.h" |
| 17 #include "content/browser/download/download_item.h" | 18 #include "content/browser/download/download_item.h" |
| 18 #include "content/browser/download/download_request_handle.h" | 19 #include "content/browser/download/download_request_handle.h" |
| 19 #include "content/browser/download/download_request_handle.h" | |
| 20 #include "content/browser/download/download_stats.h" | 20 #include "content/browser/download/download_stats.h" |
| 21 #include "content/browser/download/interrupt_reasons.h" | 21 #include "content/browser/download/interrupt_reasons.h" |
| 22 #include "content/browser/renderer_host/global_request_id.h" | 22 #include "content/browser/renderer_host/global_request_id.h" |
| 23 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 23 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
| 24 #include "content/browser/renderer_host/resource_dispatcher_host_request_info.h" | 24 #include "content/browser/renderer_host/resource_dispatcher_host_request_info.h" |
| 25 #include "content/common/resource_response.h" | 25 #include "content/common/resource_response.h" |
| 26 #include "net/base/io_buffer.h" | 26 #include "net/base/io_buffer.h" |
| 27 #include "net/base/net_errors.h" | 27 #include "net/base/net_errors.h" |
| 28 #include "net/http/http_response_headers.h" | 28 #include "net/http/http_response_headers.h" |
| 29 #include "net/url_request/url_request_context.h" | 29 #include "net/url_request/url_request_context.h" |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 !response->response_head.headers->GetMimeType(&content_type_header)) | 116 !response->response_head.headers->GetMimeType(&content_type_header)) |
| 117 content_type_header = ""; | 117 content_type_header = ""; |
| 118 info->original_mime_type = content_type_header; | 118 info->original_mime_type = content_type_header; |
| 119 | 119 |
| 120 info->prompt_user_for_save_location = | 120 info->prompt_user_for_save_location = |
| 121 save_as_ && save_info_.file_path.empty(); | 121 save_as_ && save_info_.file_path.empty(); |
| 122 info->referrer_charset = request_->context()->referrer_charset(); | 122 info->referrer_charset = request_->context()->referrer_charset(); |
| 123 info->save_info = save_info_; | 123 info->save_info = save_info_; |
| 124 BrowserThread::PostTask( | 124 BrowserThread::PostTask( |
| 125 BrowserThread::UI, FROM_HERE, | 125 BrowserThread::UI, FROM_HERE, |
| 126 NewRunnableMethod( | 126 base::Bind(&DownloadFileManager::StartDownload, |
| 127 download_file_manager_, &DownloadFileManager::StartDownload, | 127 download_file_manager_, info, request_handle)); |
| 128 info, request_handle)); | |
| 129 | 128 |
| 130 // We can't start saving the data before we create the file on disk. | 129 // We can't start saving the data before we create the file on disk. |
| 131 // The request will be un-paused in DownloadFileManager::CreateDownloadFile. | 130 // The request will be un-paused in DownloadFileManager::CreateDownloadFile. |
| 132 rdh_->PauseRequest(global_id_.child_id, global_id_.request_id, true); | 131 rdh_->PauseRequest(global_id_.child_id, global_id_.request_id, true); |
| 133 | 132 |
| 134 return true; | 133 return true; |
| 135 } | 134 } |
| 136 | 135 |
| 137 void DownloadResourceHandler::CallStartedCB(net::Error error) { | 136 void DownloadResourceHandler::CallStartedCB(net::Error error) { |
| 138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 169 // Swap the data. | 168 // Swap the data. |
| 170 net::IOBuffer* io_buffer = NULL; | 169 net::IOBuffer* io_buffer = NULL; |
| 171 read_buffer_.swap(&io_buffer); | 170 read_buffer_.swap(&io_buffer); |
| 172 size_t vector_size = buffer_->AddData(io_buffer, *bytes_read); | 171 size_t vector_size = buffer_->AddData(io_buffer, *bytes_read); |
| 173 bool need_update = (vector_size == 1); // Buffer was empty. | 172 bool need_update = (vector_size == 1); // Buffer was empty. |
| 174 | 173 |
| 175 // We are passing ownership of this buffer to the download file manager. | 174 // We are passing ownership of this buffer to the download file manager. |
| 176 if (need_update) { | 175 if (need_update) { |
| 177 BrowserThread::PostTask( | 176 BrowserThread::PostTask( |
| 178 BrowserThread::FILE, FROM_HERE, | 177 BrowserThread::FILE, FROM_HERE, |
| 179 NewRunnableMethod(download_file_manager_, | 178 base::Bind(&DownloadFileManager::UpdateDownload, |
| 180 &DownloadFileManager::UpdateDownload, | 179 download_file_manager_, download_id_, buffer_)); |
| 181 download_id_, | |
| 182 buffer_)); | |
| 183 } | 180 } |
| 184 | 181 |
| 185 // We schedule a pause outside of the read loop if there is too much file | 182 // We schedule a pause outside of the read loop if there is too much file |
| 186 // writing work to do. | 183 // writing work to do. |
| 187 if (vector_size > kLoadsToWrite) | 184 if (vector_size > kLoadsToWrite) |
| 188 StartPauseTimer(); | 185 StartPauseTimer(); |
| 189 | 186 |
| 190 return true; | 187 return true; |
| 191 } | 188 } |
| 192 | 189 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 217 // CANCELED/ERR_ABORTED can occur for reasons other than user cancel. | 214 // CANCELED/ERR_ABORTED can occur for reasons other than user cancel. |
| 218 reason = DOWNLOAD_INTERRUPT_REASON_USER_CANCELED; // User canceled. | 215 reason = DOWNLOAD_INTERRUPT_REASON_USER_CANCELED; // User canceled. |
| 219 } | 216 } |
| 220 if (!download_id_.IsValid()) | 217 if (!download_id_.IsValid()) |
| 221 CallStartedCB(error_code); | 218 CallStartedCB(error_code); |
| 222 // We transfer ownership to |DownloadFileManager| to delete |buffer_|, | 219 // We transfer ownership to |DownloadFileManager| to delete |buffer_|, |
| 223 // so that any functions queued up on the FILE thread are executed | 220 // so that any functions queued up on the FILE thread are executed |
| 224 // before deletion. | 221 // before deletion. |
| 225 BrowserThread::PostTask( | 222 BrowserThread::PostTask( |
| 226 BrowserThread::FILE, FROM_HERE, | 223 BrowserThread::FILE, FROM_HERE, |
| 227 NewRunnableMethod(download_file_manager_, | 224 base::Bind(&DownloadFileManager::OnResponseCompleted, |
| 228 &DownloadFileManager::OnResponseCompleted, | 225 download_file_manager_, download_id_, reason, security_info)); |
| 229 download_id_, | |
| 230 reason, | |
| 231 security_info)); | |
| 232 buffer_ = NULL; // The buffer is longer needed by |DownloadResourceHandler|. | 226 buffer_ = NULL; // The buffer is longer needed by |DownloadResourceHandler|. |
| 233 read_buffer_ = NULL; | 227 read_buffer_ = NULL; |
| 234 return true; | 228 return true; |
| 235 } | 229 } |
| 236 | 230 |
| 237 void DownloadResourceHandler::OnRequestClosed() { | 231 void DownloadResourceHandler::OnRequestClosed() { |
| 238 UMA_HISTOGRAM_TIMES("SB2.DownloadDuration", | 232 UMA_HISTOGRAM_TIMES("SB2.DownloadDuration", |
| 239 base::TimeTicks::Now() - download_start_time_); | 233 base::TimeTicks::Now() - download_start_time_); |
| 240 } | 234 } |
| 241 | 235 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 " render_view_id_ = " "%d" | 287 " render_view_id_ = " "%d" |
| 294 " save_info_.file_path = \"%" PRFilePath "\"" | 288 " save_info_.file_path = \"%" PRFilePath "\"" |
| 295 " }", | 289 " }", |
| 296 request_->url().spec().c_str(), | 290 request_->url().spec().c_str(), |
| 297 download_id_.local(), | 291 download_id_.local(), |
| 298 global_id_.child_id, | 292 global_id_.child_id, |
| 299 global_id_.request_id, | 293 global_id_.request_id, |
| 300 render_view_id_, | 294 render_view_id_, |
| 301 save_info_.file_path.value().c_str()); | 295 save_info_.file_path.value().c_str()); |
| 302 } | 296 } |
| OLD | NEW |