| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_resource_handler.h" | 5 #include "chrome/browser/renderer_host/download_resource_handler.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "chrome/browser/chrome_thread.h" |
| 8 #include "chrome/browser/download/download_file.h" | 9 #include "chrome/browser/download/download_file.h" |
| 9 #include "chrome/browser/download/download_manager.h" | 10 #include "chrome/browser/download/download_manager.h" |
| 10 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" | 11 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" |
| 11 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
| 12 #include "net/url_request/url_request_context.h" | 13 #include "net/url_request/url_request_context.h" |
| 13 | 14 |
| 14 DownloadResourceHandler::DownloadResourceHandler(ResourceDispatcherHost* rdh, | 15 DownloadResourceHandler::DownloadResourceHandler(ResourceDispatcherHost* rdh, |
| 15 int render_process_host_id, | 16 int render_process_host_id, |
| 16 int render_view_id, | 17 int render_view_id, |
| 17 int request_id, | 18 int request_id, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 info->state = DownloadItem::IN_PROGRESS; | 63 info->state = DownloadItem::IN_PROGRESS; |
| 63 info->download_id = download_id_; | 64 info->download_id = download_id_; |
| 64 info->child_id = global_id_.child_id; | 65 info->child_id = global_id_.child_id; |
| 65 info->render_view_id = render_view_id_; | 66 info->render_view_id = render_view_id_; |
| 66 info->request_id = global_id_.request_id; | 67 info->request_id = global_id_.request_id; |
| 67 info->content_disposition = content_disposition_; | 68 info->content_disposition = content_disposition_; |
| 68 info->mime_type = response->response_head.mime_type; | 69 info->mime_type = response->response_head.mime_type; |
| 69 info->save_as = save_as_; | 70 info->save_as = save_as_; |
| 70 info->is_dangerous = false; | 71 info->is_dangerous = false; |
| 71 info->referrer_charset = request_->context()->referrer_charset(); | 72 info->referrer_charset = request_->context()->referrer_charset(); |
| 72 download_manager_->file_loop()->PostTask(FROM_HERE, | 73 ChromeThread::PostTask( |
| 73 NewRunnableMethod(download_manager_, | 74 ChromeThread::FILE, FROM_HERE, |
| 74 &DownloadFileManager::StartDownload, | 75 NewRunnableMethod( |
| 75 info)); | 76 download_manager_, &DownloadFileManager::StartDownload, info)); |
| 76 return true; | 77 return true; |
| 77 } | 78 } |
| 78 | 79 |
| 79 // Create a new buffer, which will be handed to the download thread for file | 80 // Create a new buffer, which will be handed to the download thread for file |
| 80 // writing and deletion. | 81 // writing and deletion. |
| 81 bool DownloadResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf, | 82 bool DownloadResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf, |
| 82 int* buf_size, int min_size) { | 83 int* buf_size, int min_size) { |
| 83 DCHECK(buf && buf_size); | 84 DCHECK(buf && buf_size); |
| 84 if (!read_buffer_) { | 85 if (!read_buffer_) { |
| 85 *buf_size = min_size < 0 ? kReadBufSize : min_size; | 86 *buf_size = min_size < 0 ? kReadBufSize : min_size; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 97 return true; | 98 return true; |
| 98 DCHECK(read_buffer_); | 99 DCHECK(read_buffer_); |
| 99 AutoLock auto_lock(buffer_->lock); | 100 AutoLock auto_lock(buffer_->lock); |
| 100 bool need_update = buffer_->contents.empty(); | 101 bool need_update = buffer_->contents.empty(); |
| 101 | 102 |
| 102 // We are passing ownership of this buffer to the download file manager. | 103 // We are passing ownership of this buffer to the download file manager. |
| 103 net::IOBuffer* buffer = NULL; | 104 net::IOBuffer* buffer = NULL; |
| 104 read_buffer_.swap(&buffer); | 105 read_buffer_.swap(&buffer); |
| 105 buffer_->contents.push_back(std::make_pair(buffer, *bytes_read)); | 106 buffer_->contents.push_back(std::make_pair(buffer, *bytes_read)); |
| 106 if (need_update) { | 107 if (need_update) { |
| 107 download_manager_->file_loop()->PostTask(FROM_HERE, | 108 ChromeThread::PostTask( |
| 109 ChromeThread::FILE, FROM_HERE, |
| 108 NewRunnableMethod(download_manager_, | 110 NewRunnableMethod(download_manager_, |
| 109 &DownloadFileManager::UpdateDownload, | 111 &DownloadFileManager::UpdateDownload, |
| 110 download_id_, | 112 download_id_, |
| 111 buffer_)); | 113 buffer_)); |
| 112 } | 114 } |
| 113 | 115 |
| 114 // We schedule a pause outside of the read loop if there is too much file | 116 // We schedule a pause outside of the read loop if there is too much file |
| 115 // writing work to do. | 117 // writing work to do. |
| 116 if (buffer_->contents.size() > kLoadsToWrite) | 118 if (buffer_->contents.size() > kLoadsToWrite) |
| 117 StartPauseTimer(); | 119 StartPauseTimer(); |
| 118 | 120 |
| 119 return true; | 121 return true; |
| 120 } | 122 } |
| 121 | 123 |
| 122 bool DownloadResourceHandler::OnResponseCompleted( | 124 bool DownloadResourceHandler::OnResponseCompleted( |
| 123 int request_id, | 125 int request_id, |
| 124 const URLRequestStatus& status, | 126 const URLRequestStatus& status, |
| 125 const std::string& security_info) { | 127 const std::string& security_info) { |
| 126 download_manager_->file_loop()->PostTask(FROM_HERE, | 128 ChromeThread::PostTask( |
| 129 ChromeThread::FILE, FROM_HERE, |
| 127 NewRunnableMethod(download_manager_, | 130 NewRunnableMethod(download_manager_, |
| 128 &DownloadFileManager::DownloadFinished, | 131 &DownloadFileManager::DownloadFinished, |
| 129 download_id_, | 132 download_id_, |
| 130 buffer_)); | 133 buffer_)); |
| 131 read_buffer_ = NULL; | 134 read_buffer_ = NULL; |
| 132 | 135 |
| 133 // 'buffer_' is deleted by the DownloadFileManager. | 136 // 'buffer_' is deleted by the DownloadFileManager. |
| 134 buffer_ = NULL; | 137 buffer_ = NULL; |
| 135 return true; | 138 return true; |
| 136 } | 139 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 should_pause); | 173 should_pause); |
| 171 is_paused_ = should_pause; | 174 is_paused_ = should_pause; |
| 172 } | 175 } |
| 173 } | 176 } |
| 174 | 177 |
| 175 void DownloadResourceHandler::StartPauseTimer() { | 178 void DownloadResourceHandler::StartPauseTimer() { |
| 176 if (!pause_timer_.IsRunning()) | 179 if (!pause_timer_.IsRunning()) |
| 177 pause_timer_.Start(base::TimeDelta::FromMilliseconds(kThrottleTimeMs), this, | 180 pause_timer_.Start(base::TimeDelta::FromMilliseconds(kThrottleTimeMs), this, |
| 178 &DownloadResourceHandler::CheckWriteProgress); | 181 &DownloadResourceHandler::CheckWriteProgress); |
| 179 } | 182 } |
| OLD | NEW |