| 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/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/download/download_create_info.h" | 14 #include "chrome/browser/download/download_create_info.h" |
| 15 #include "chrome/browser/download/download_manager.h" | 15 #include "chrome/browser/download/download_manager.h" |
| 16 #include "chrome/browser/download/download_prefs.h" |
| 16 #include "chrome/browser/download/download_request_handle.h" | 17 #include "chrome/browser/download/download_request_handle.h" |
| 17 #include "chrome/browser/download/download_util.h" | 18 #include "chrome/browser/download/download_util.h" |
| 18 #include "chrome/browser/net/chrome_url_request_context.h" | 19 #include "chrome/browser/net/chrome_url_request_context.h" |
| 19 #include "chrome/browser/platform_util.h" | 20 #include "chrome/browser/platform_util.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 22 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 22 #include "chrome/browser/tab_contents/tab_util.h" | 23 #include "chrome/browser/tab_contents/tab_util.h" |
| 23 #include "content/browser/browser_thread.h" | 24 #include "content/browser/browser_thread.h" |
| 24 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 25 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
| 25 #include "content/browser/tab_contents/tab_contents.h" | 26 #include "content/browser/tab_contents/tab_contents.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 49 BrowserThread::FILE, FROM_HERE, | 50 BrowserThread::FILE, FROM_HERE, |
| 50 NewRunnableMethod(this, &DownloadFileManager::OnShutdown)); | 51 NewRunnableMethod(this, &DownloadFileManager::OnShutdown)); |
| 51 } | 52 } |
| 52 | 53 |
| 53 void DownloadFileManager::OnShutdown() { | 54 void DownloadFileManager::OnShutdown() { |
| 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 55 StopUpdateTimer(); | 56 StopUpdateTimer(); |
| 56 STLDeleteValues(&downloads_); | 57 STLDeleteValues(&downloads_); |
| 57 } | 58 } |
| 58 | 59 |
| 59 void DownloadFileManager::CreateDownloadFile(DownloadCreateInfo* info, | 60 void DownloadFileManager::CreateDownloadFile( |
| 60 DownloadManager* download_manager, | 61 DownloadCreateInfo* info, DownloadManager* download_manager, |
| 61 bool get_hash) { | 62 bool get_hash, const FilePath& default_download_dir) { |
| 62 DCHECK(info); | 63 DCHECK(info); |
| 63 VLOG(20) << __FUNCTION__ << "()" << " info = " << info->DebugString(); | 64 VLOG(20) << __FUNCTION__ << "()" << " info = " << info->DebugString(); |
| 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 65 | 66 |
| 66 // Life of |info| ends here. No more references to it after this method. | 67 // Life of |info| ends here. No more references to it after this method. |
| 67 scoped_ptr<DownloadCreateInfo> infop(info); | 68 scoped_ptr<DownloadCreateInfo> infop(info); |
| 68 | 69 |
| 69 scoped_ptr<DownloadFile> | 70 scoped_ptr<DownloadFile> |
| 70 download_file(new DownloadFile(info, download_manager)); | 71 download_file(new DownloadFile(info, download_manager)); |
| 71 if (!download_file->Initialize(get_hash)) { | 72 if (!download_file->Initialize(get_hash, default_download_dir)) { |
| 72 info->request_handle.CancelRequest(); | 73 info->request_handle.CancelRequest(); |
| 73 return; | 74 return; |
| 74 } | 75 } |
| 75 | 76 |
| 76 int32 id = info->download_id; | 77 int32 id = info->download_id; |
| 77 DCHECK(GetDownloadFile(id) == NULL); | 78 DCHECK(GetDownloadFile(id) == NULL); |
| 78 downloads_[id] = download_file.release(); | 79 downloads_[id] = download_file.release(); |
| 79 | 80 |
| 80 // The file is now ready, we can un-pause the request and start saving data. | 81 // The file is now ready, we can un-pause the request and start saving data. |
| 81 info->request_handle.ResumeRequest(); | 82 info->request_handle.ResumeRequest(); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 return; | 140 return; |
| 140 } | 141 } |
| 141 | 142 |
| 142 // TODO(phajdan.jr): fix the duplication of path info below. | 143 // TODO(phajdan.jr): fix the duplication of path info below. |
| 143 info->path = info->save_info.file_path; | 144 info->path = info->save_info.file_path; |
| 144 | 145 |
| 145 manager->CreateDownloadItem(info); | 146 manager->CreateDownloadItem(info); |
| 146 | 147 |
| 147 bool hash_needed = g_browser_process->safe_browsing_service()-> | 148 bool hash_needed = g_browser_process->safe_browsing_service()-> |
| 148 DownloadBinHashNeeded(); | 149 DownloadBinHashNeeded(); |
| 150 FilePath default_download_dir = |
| 151 manager->download_prefs()->GetDefaultDownloadDirectory(); |
| 149 | 152 |
| 150 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 153 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 151 NewRunnableMethod(this, &DownloadFileManager::CreateDownloadFile, | 154 NewRunnableMethod(this, &DownloadFileManager::CreateDownloadFile, |
| 152 info, make_scoped_refptr(manager), hash_needed)); | 155 info, make_scoped_refptr(manager), |
| 156 hash_needed, default_download_dir)); |
| 153 } | 157 } |
| 154 | 158 |
| 155 // We don't forward an update to the UI thread here, since we want to throttle | 159 // We don't forward an update to the UI thread here, since we want to throttle |
| 156 // the UI update rate via a periodic timer. If the user has cancelled the | 160 // the UI update rate via a periodic timer. If the user has cancelled the |
| 157 // download (in the UI thread), we may receive a few more updates before the IO | 161 // download (in the UI thread), we may receive a few more updates before the IO |
| 158 // thread gets the cancel message: we just delete the data since the | 162 // thread gets the cancel message: we just delete the data since the |
| 159 // DownloadFile has been deleted. | 163 // DownloadFile has been deleted. |
| 160 void DownloadFileManager::UpdateDownload(int id, DownloadBuffer* buffer) { | 164 void DownloadFileManager::UpdateDownload(int id, DownloadBuffer* buffer) { |
| 161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 165 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 162 std::vector<DownloadBuffer::Contents> contents; | 166 std::vector<DownloadBuffer::Contents> contents; |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 } | 271 } |
| 268 } | 272 } |
| 269 | 273 |
| 270 // Actions from the UI thread and run on the download thread | 274 // Actions from the UI thread and run on the download thread |
| 271 | 275 |
| 272 // The DownloadManager in the UI thread has provided an intermediate .crdownload | 276 // The DownloadManager in the UI thread has provided an intermediate .crdownload |
| 273 // name for the download specified by 'id'. Rename the in progress download. | 277 // name for the download specified by 'id'. Rename the in progress download. |
| 274 // | 278 // |
| 275 // There are 2 possible rename cases where this method can be called: | 279 // There are 2 possible rename cases where this method can be called: |
| 276 // 1. tmp -> foo.crdownload (not final, safe) | 280 // 1. tmp -> foo.crdownload (not final, safe) |
| 277 // 2. tmp-> Unconfirmed.xxx.crdownload (not final, dangerous) | 281 // 2. tmp -> Unconfirmed.xxx.crdownload (not final, dangerous) |
| 278 void DownloadFileManager::RenameInProgressDownloadFile( | 282 void DownloadFileManager::RenameInProgressDownloadFile( |
| 279 int id, const FilePath& full_path) { | 283 int id, const FilePath& full_path) { |
| 280 VLOG(20) << __FUNCTION__ << "()" << " id = " << id | 284 VLOG(20) << __FUNCTION__ << "()" << " id = " << id |
| 281 << " full_path = \"" << full_path.value() << "\""; | 285 << " full_path = \"" << full_path.value() << "\""; |
| 282 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 286 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 283 | 287 |
| 284 DownloadFile* download_file = GetDownloadFile(id); | 288 DownloadFile* download_file = GetDownloadFile(id); |
| 285 if (!download_file) | 289 if (!download_file) |
| 286 return; | 290 return; |
| 287 | 291 |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 392 << " id = " << id | 396 << " id = " << id |
| 393 << " download_file = " << download_file->DebugString(); | 397 << " download_file = " << download_file->DebugString(); |
| 394 | 398 |
| 395 downloads_.erase(id); | 399 downloads_.erase(id); |
| 396 | 400 |
| 397 delete download_file; | 401 delete download_file; |
| 398 | 402 |
| 399 if (downloads_.empty()) | 403 if (downloads_.empty()) |
| 400 StopUpdateTimer(); | 404 StopUpdateTimer(); |
| 401 } | 405 } |
| OLD | NEW |