| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/download/download_file_manager.h" | |
| 6 | |
| 7 #include <set> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/file_util.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/stl_util.h" | |
| 14 #include "base/utf_string_conversions.h" | |
| 15 #include "content/browser/download/base_file.h" | |
| 16 #include "content/browser/download/download_create_info.h" | |
| 17 #include "content/browser/download/download_file_impl.h" | |
| 18 #include "content/browser/download/download_interrupt_reasons_impl.h" | |
| 19 #include "content/browser/download/download_request_handle.h" | |
| 20 #include "content/browser/download/download_stats.h" | |
| 21 #include "content/browser/power_save_blocker.h" | |
| 22 #include "content/browser/web_contents/web_contents_impl.h" | |
| 23 #include "content/public/browser/browser_thread.h" | |
| 24 #include "content/public/browser/download_manager.h" | |
| 25 #include "content/public/browser/download_manager_delegate.h" | |
| 26 #include "googleurl/src/gurl.h" | |
| 27 #include "net/base/io_buffer.h" | |
| 28 | |
| 29 using content::BrowserThread; | |
| 30 using content::DownloadFile; | |
| 31 using content::DownloadId; | |
| 32 using content::DownloadManager; | |
| 33 | |
| 34 DownloadFileManager::DownloadFileManager(content::DownloadFileFactory* factory) | |
| 35 : download_file_factory_(factory) { | |
| 36 if (download_file_factory_ == NULL) | |
| 37 download_file_factory_.reset(new content::DownloadFileFactory); | |
| 38 } | |
| 39 | |
| 40 DownloadFileManager::~DownloadFileManager() { | |
| 41 DCHECK(downloads_.empty()); | |
| 42 } | |
| 43 | |
| 44 void DownloadFileManager::Shutdown() { | |
| 45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 46 BrowserThread::PostTask( | |
| 47 BrowserThread::FILE, FROM_HERE, | |
| 48 base::Bind(&DownloadFileManager::OnShutdown, this)); | |
| 49 } | |
| 50 | |
| 51 void DownloadFileManager::OnShutdown() { | |
| 52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 53 STLDeleteValues(&downloads_); | |
| 54 } | |
| 55 | |
| 56 void DownloadFileManager::CreateDownloadFile( | |
| 57 scoped_ptr<DownloadCreateInfo> info, | |
| 58 scoped_ptr<content::ByteStreamReader> stream, | |
| 59 scoped_refptr<DownloadManager> download_manager, bool get_hash, | |
| 60 const net::BoundNetLog& bound_net_log, | |
| 61 const CreateDownloadFileCallback& callback) { | |
| 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 63 DCHECK(info.get()); | |
| 64 VLOG(20) << __FUNCTION__ << "()" << " info = " << info->DebugString(); | |
| 65 | |
| 66 // Can't dereference info after info.Pass(), so we need to do it here. | |
| 67 DownloadId id(info->download_id); | |
| 68 | |
| 69 scoped_ptr<DownloadFile> download_file(download_file_factory_->CreateFile( | |
| 70 info.Pass(), stream.Pass(), download_manager, get_hash, bound_net_log)); | |
| 71 | |
| 72 content::DownloadInterruptReason interrupt_reason( | |
| 73 download_file->Initialize()); | |
| 74 if (interrupt_reason == content::DOWNLOAD_INTERRUPT_REASON_NONE) { | |
| 75 DCHECK(GetDownloadFile(id) == NULL); | |
| 76 downloads_[id] = download_file.release(); | |
| 77 } | |
| 78 | |
| 79 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 80 base::Bind(callback, interrupt_reason)); | |
| 81 } | |
| 82 | |
| 83 DownloadFile* DownloadFileManager::GetDownloadFile( | |
| 84 DownloadId global_id) { | |
| 85 DownloadFileMap::iterator it = downloads_.find(global_id); | |
| 86 return it == downloads_.end() ? NULL : it->second; | |
| 87 } | |
| 88 | |
| 89 // This method will be sent via a user action, or shutdown on the UI thread, and | |
| 90 // run on the download thread. Since this message has been sent from the UI | |
| 91 // thread, the download may have already completed and won't exist in our map. | |
| 92 void DownloadFileManager::CancelDownload(DownloadId global_id) { | |
| 93 VLOG(20) << __FUNCTION__ << "()" << " id = " << global_id; | |
| 94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 95 DownloadFileMap::iterator it = downloads_.find(global_id); | |
| 96 if (it == downloads_.end()) | |
| 97 return; | |
| 98 | |
| 99 DownloadFile* download_file = it->second; | |
| 100 VLOG(20) << __FUNCTION__ << "()" | |
| 101 << " download_file = " << download_file->DebugString(); | |
| 102 download_file->Cancel(); | |
| 103 | |
| 104 EraseDownload(global_id); | |
| 105 } | |
| 106 | |
| 107 void DownloadFileManager::CompleteDownload( | |
| 108 DownloadId global_id, const base::Closure& callback) { | |
| 109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 110 | |
| 111 if (!ContainsKey(downloads_, global_id)) | |
| 112 return; | |
| 113 | |
| 114 DownloadFile* download_file = downloads_[global_id]; | |
| 115 | |
| 116 VLOG(20) << " " << __FUNCTION__ << "()" | |
| 117 << " id = " << global_id | |
| 118 << " download_file = " << download_file->DebugString(); | |
| 119 | |
| 120 download_file->Detach(callback); | |
| 121 | |
| 122 EraseDownload(global_id); | |
| 123 } | |
| 124 | |
| 125 void DownloadFileManager::OnDownloadManagerShutdown(DownloadManager* manager) { | |
| 126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 127 DCHECK(manager); | |
| 128 | |
| 129 std::set<DownloadFile*> to_remove; | |
| 130 | |
| 131 for (DownloadFileMap::iterator i = downloads_.begin(); | |
| 132 i != downloads_.end(); ++i) { | |
| 133 DownloadFile* download_file = i->second; | |
| 134 if (download_file->GetDownloadManager() == manager) { | |
| 135 download_file->CancelDownloadRequest(); | |
| 136 to_remove.insert(download_file); | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 for (std::set<DownloadFile*>::iterator i = to_remove.begin(); | |
| 141 i != to_remove.end(); ++i) { | |
| 142 downloads_.erase((*i)->GlobalId()); | |
| 143 delete *i; | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 // Actions from the UI thread and run on the download thread | |
| 148 | |
| 149 void DownloadFileManager::RenameDownloadFile( | |
| 150 DownloadId global_id, | |
| 151 const FilePath& full_path, | |
| 152 bool overwrite_existing_file, | |
| 153 const RenameCompletionCallback& callback) { | |
| 154 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 155 DownloadFile* download_file = GetDownloadFile(global_id); | |
| 156 if (!download_file) { | |
| 157 BrowserThread::PostTask( | |
| 158 BrowserThread::UI, FROM_HERE, | |
| 159 base::Bind(callback, content::DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, | |
| 160 FilePath())); | |
| 161 return; | |
| 162 } | |
| 163 | |
| 164 download_file->Rename(full_path, overwrite_existing_file, callback); | |
| 165 } | |
| 166 | |
| 167 int DownloadFileManager::NumberOfActiveDownloads() const { | |
| 168 return downloads_.size(); | |
| 169 } | |
| 170 | |
| 171 void DownloadFileManager::EraseDownload(DownloadId global_id) { | |
| 172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 173 | |
| 174 if (!ContainsKey(downloads_, global_id)) | |
| 175 return; | |
| 176 | |
| 177 DownloadFile* download_file = downloads_[global_id]; | |
| 178 | |
| 179 VLOG(20) << " " << __FUNCTION__ << "()" | |
| 180 << " id = " << global_id | |
| 181 << " download_file = " << download_file->DebugString(); | |
| 182 | |
| 183 downloads_.erase(global_id); | |
| 184 | |
| 185 delete download_file; | |
| 186 } | |
| OLD | NEW |