| 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/download/download_file_manager.h" | 5 #include "chrome/browser/download/download_file_manager.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/stl_util-inl.h" | 9 #include "base/stl_util-inl.h" |
| 10 #include "base/task.h" | 10 #include "base/task.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 #include "chrome/browser/download/download_manager.h" | 13 #include "chrome/browser/download/download_manager.h" |
| 14 #include "chrome/browser/download/download_process_handle.h" |
| 14 #include "chrome/browser/download/download_util.h" | 15 #include "chrome/browser/download/download_util.h" |
| 15 #include "chrome/browser/history/download_create_info.h" | 16 #include "chrome/browser/history/download_create_info.h" |
| 16 #include "chrome/browser/net/chrome_url_request_context.h" | 17 #include "chrome/browser/net/chrome_url_request_context.h" |
| 17 #include "chrome/browser/platform_util.h" | 18 #include "chrome/browser/platform_util.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 20 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 20 #include "chrome/browser/tab_contents/tab_util.h" | 21 #include "chrome/browser/tab_contents/tab_util.h" |
| 21 #include "content/browser/browser_thread.h" | 22 #include "content/browser/browser_thread.h" |
| 22 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 23 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
| 23 #include "content/browser/tab_contents/tab_contents.h" | 24 #include "content/browser/tab_contents/tab_contents.h" |
| 24 #include "googleurl/src/gurl.h" | 25 #include "googleurl/src/gurl.h" |
| 25 #include "net/base/io_buffer.h" | 26 #include "net/base/io_buffer.h" |
| 26 | 27 |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| 29 // Throttle updates to the UI thread so that a fast moving download doesn't | 30 // Throttle updates to the UI thread so that a fast moving download doesn't |
| 30 // cause it to become unresponsive (in milliseconds). | 31 // cause it to become unresponsive (in milliseconds). |
| 31 const int kUpdatePeriodMs = 500; | 32 const int kUpdatePeriodMs = 500; |
| 32 | 33 |
| 33 DownloadManager* DownloadManagerForRenderViewHost(int render_process_id, | |
| 34 int render_view_id) { | |
| 35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 36 | |
| 37 TabContents* contents = tab_util::GetTabContentsByID(render_process_id, | |
| 38 render_view_id); | |
| 39 if (contents) { | |
| 40 Profile* profile = contents->profile(); | |
| 41 if (profile) | |
| 42 return profile->GetDownloadManager(); | |
| 43 } | |
| 44 | |
| 45 return NULL; | |
| 46 } | |
| 47 | |
| 48 } // namespace | 34 } // namespace |
| 49 | 35 |
| 50 DownloadFileManager::DownloadFileManager(ResourceDispatcherHost* rdh) | 36 DownloadFileManager::DownloadFileManager(ResourceDispatcherHost* rdh) |
| 51 : next_id_(0), | 37 : next_id_(0), |
| 52 resource_dispatcher_host_(rdh) { | 38 resource_dispatcher_host_(rdh) { |
| 53 } | 39 } |
| 54 | 40 |
| 55 DownloadFileManager::~DownloadFileManager() { | 41 DownloadFileManager::~DownloadFileManager() { |
| 56 DCHECK(downloads_.empty()); | 42 DCHECK(downloads_.empty()); |
| 57 } | 43 } |
| 58 | 44 |
| 59 void DownloadFileManager::Shutdown() { | 45 void DownloadFileManager::Shutdown() { |
| 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 61 BrowserThread::PostTask( | 47 BrowserThread::PostTask( |
| 62 BrowserThread::FILE, FROM_HERE, | 48 BrowserThread::FILE, FROM_HERE, |
| 63 NewRunnableMethod(this, &DownloadFileManager::OnShutdown)); | 49 NewRunnableMethod(this, &DownloadFileManager::OnShutdown)); |
| 64 } | 50 } |
| 65 | 51 |
| 66 void DownloadFileManager::OnShutdown() { | 52 void DownloadFileManager::OnShutdown() { |
| 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 68 StopUpdateTimer(); | 54 StopUpdateTimer(); |
| 69 STLDeleteValues(&downloads_); | 55 STLDeleteValues(&downloads_); |
| 70 } | 56 } |
| 71 | 57 |
| 72 void DownloadFileManager::CreateDownloadFile(DownloadCreateInfo* info, | 58 void DownloadFileManager::CreateDownloadFile(DownloadCreateInfo* info, |
| 59 DownloadProcessHandle process, |
| 73 DownloadManager* download_manager, | 60 DownloadManager* download_manager, |
| 74 bool get_hash) { | 61 bool get_hash) { |
| 75 VLOG(20) << __FUNCTION__ << "()" << " info = " << info->DebugString(); | 62 VLOG(20) << __FUNCTION__ << "()" << " info = " << info->DebugString(); |
| 76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 77 | 64 |
| 78 scoped_ptr<DownloadFile> | 65 scoped_ptr<DownloadFile> |
| 79 download_file(new DownloadFile(info, download_manager)); | 66 download_file(new DownloadFile(info, download_manager)); |
| 80 if (!download_file->Initialize(get_hash)) { | 67 if (!download_file->Initialize(get_hash)) { |
| 81 BrowserThread::PostTask( | 68 process.CancelDownload(resource_dispatcher_host_); |
| 82 BrowserThread::IO, FROM_HERE, | |
| 83 NewRunnableFunction(&download_util::CancelDownloadRequest, | |
| 84 resource_dispatcher_host_, | |
| 85 info->child_id, | |
| 86 info->request_id)); | |
| 87 delete info; | |
| 88 return; | 69 return; |
| 89 } | 70 } |
| 90 | 71 |
| 91 DCHECK(GetDownloadFile(info->download_id) == NULL); | 72 DCHECK(GetDownloadFile(info->download_id) == NULL); |
| 92 downloads_[info->download_id] = download_file.release(); | 73 downloads_[info->download_id] = download_file.release(); |
| 93 // TODO(phajdan.jr): fix the duplication of path info below. | 74 // TODO(phajdan.jr): fix the duplication of path info below. |
| 94 info->path = info->save_info.file_path; | 75 info->path = info->save_info.file_path; |
| 95 | 76 |
| 96 // The file is now ready, we can un-pause the request and start saving data. | 77 // The file is now ready, we can un-pause the request and start saving data. |
| 97 BrowserThread::PostTask( | 78 BrowserThread::PostTask( |
| 98 BrowserThread::IO, FROM_HERE, | 79 BrowserThread::IO, FROM_HERE, |
| 99 NewRunnableMethod(this, &DownloadFileManager::ResumeDownloadRequest, | 80 NewRunnableMethod(this, &DownloadFileManager::ResumeDownloadRequest, |
| 100 info->child_id, info->request_id)); | 81 process)); |
| 101 | 82 |
| 102 StartUpdateTimer(); | 83 StartUpdateTimer(); |
| 103 | 84 |
| 104 BrowserThread::PostTask( | 85 BrowserThread::PostTask( |
| 105 BrowserThread::UI, FROM_HERE, | 86 BrowserThread::UI, FROM_HERE, |
| 106 NewRunnableMethod(download_manager, | 87 NewRunnableMethod(download_manager, |
| 107 &DownloadManager::StartDownload, info)); | 88 &DownloadManager::StartDownload, info)); |
| 108 } | 89 } |
| 109 | 90 |
| 110 void DownloadFileManager::ResumeDownloadRequest(int child_id, int request_id) { | 91 void DownloadFileManager::ResumeDownloadRequest( |
| 92 DownloadProcessHandle process) { |
| 111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 112 | 94 |
| 113 // This balances the pause in DownloadResourceHandler::OnResponseStarted. | 95 // This balances the pause in DownloadResourceHandler::OnResponseStarted. |
| 114 resource_dispatcher_host_->PauseRequest(child_id, request_id, false); | 96 resource_dispatcher_host_->PauseRequest(process.child_id(), |
| 97 process.request_id(), |
| 98 false); |
| 115 } | 99 } |
| 116 | 100 |
| 117 DownloadFile* DownloadFileManager::GetDownloadFile(int id) { | 101 DownloadFile* DownloadFileManager::GetDownloadFile(int id) { |
| 118 DownloadFileMap::iterator it = downloads_.find(id); | 102 DownloadFileMap::iterator it = downloads_.find(id); |
| 119 return it == downloads_.end() ? NULL : it->second; | 103 return it == downloads_.end() ? NULL : it->second; |
| 120 } | 104 } |
| 121 | 105 |
| 122 void DownloadFileManager::StartUpdateTimer() { | 106 void DownloadFileManager::StartUpdateTimer() { |
| 123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 124 if (!update_timer_.IsRunning()) { | 108 if (!update_timer_.IsRunning()) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 151 // request is a download. | 135 // request is a download. |
| 152 int DownloadFileManager::GetNextId() { | 136 int DownloadFileManager::GetNextId() { |
| 153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 154 return next_id_++; | 138 return next_id_++; |
| 155 } | 139 } |
| 156 | 140 |
| 157 void DownloadFileManager::StartDownload(DownloadCreateInfo* info) { | 141 void DownloadFileManager::StartDownload(DownloadCreateInfo* info) { |
| 158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 159 DCHECK(info); | 143 DCHECK(info); |
| 160 | 144 |
| 161 DownloadManager* manager = DownloadManagerForRenderViewHost( | 145 DownloadManager* manager = info->process_handle.GetDownloadManager(); |
| 162 info->child_id, info->render_view_id); | |
| 163 if (!manager) { | 146 if (!manager) { |
| 164 BrowserThread::PostTask( | 147 info->process_handle.CancelDownload(resource_dispatcher_host_); |
| 165 BrowserThread::IO, FROM_HERE, | |
| 166 NewRunnableFunction(&download_util::CancelDownloadRequest, | |
| 167 resource_dispatcher_host_, | |
| 168 info->child_id, | |
| 169 info->request_id)); | |
| 170 delete info; | 148 delete info; |
| 171 return; | 149 return; |
| 172 } | 150 } |
| 173 | 151 |
| 152 // TODO(phajdan.jr): fix the duplication of path info below. |
| 153 info->path = info->save_info.file_path; |
| 154 |
| 174 manager->CreateDownloadItem(info); | 155 manager->CreateDownloadItem(info); |
| 175 | 156 |
| 176 bool hash_needed = resource_dispatcher_host_->safe_browsing_service()-> | 157 bool hash_needed = resource_dispatcher_host_->safe_browsing_service()-> |
| 177 DownloadBinHashNeeded(); | 158 DownloadBinHashNeeded(); |
| 178 | 159 |
| 179 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 160 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 180 NewRunnableMethod(this, &DownloadFileManager::CreateDownloadFile, | 161 NewRunnableMethod(this, &DownloadFileManager::CreateDownloadFile, |
| 181 info, make_scoped_refptr(manager), hash_needed)); | 162 info, info->process_handle, |
| 163 make_scoped_refptr(manager), hash_needed)); |
| 182 } | 164 } |
| 183 | 165 |
| 184 // We don't forward an update to the UI thread here, since we want to throttle | 166 // We don't forward an update to the UI thread here, since we want to throttle |
| 185 // the UI update rate via a periodic timer. If the user has cancelled the | 167 // the UI update rate via a periodic timer. If the user has cancelled the |
| 186 // download (in the UI thread), we may receive a few more updates before the IO | 168 // download (in the UI thread), we may receive a few more updates before the IO |
| 187 // thread gets the cancel message: we just delete the data since the | 169 // thread gets the cancel message: we just delete the data since the |
| 188 // DownloadFile has been deleted. | 170 // DownloadFile has been deleted. |
| 189 void DownloadFileManager::UpdateDownload(int id, DownloadBuffer* buffer) { | 171 void DownloadFileManager::UpdateDownload(int id, DownloadBuffer* buffer) { |
| 190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 191 std::vector<DownloadBuffer::Contents> contents; | 173 std::vector<DownloadBuffer::Contents> contents; |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 << " id = " << id | 401 << " id = " << id |
| 420 << " download_file = " << download_file->DebugString(); | 402 << " download_file = " << download_file->DebugString(); |
| 421 | 403 |
| 422 downloads_.erase(id); | 404 downloads_.erase(id); |
| 423 | 405 |
| 424 delete download_file; | 406 delete download_file; |
| 425 | 407 |
| 426 if (downloads_.empty()) | 408 if (downloads_.empty()) |
| 427 StopUpdateTimer(); | 409 StopUpdateTimer(); |
| 428 } | 410 } |
| OLD | NEW |