| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_file_manager.h" | 5 #include "content/browser/download/download_file_manager.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 // Actions from the UI thread and run on the download thread | 188 // Actions from the UI thread and run on the download thread |
| 189 | 189 |
| 190 void DownloadFileManager::RenameDownloadFile( | 190 void DownloadFileManager::RenameDownloadFile( |
| 191 DownloadId global_id, | 191 DownloadId global_id, |
| 192 const FilePath& full_path, | 192 const FilePath& full_path, |
| 193 bool overwrite_existing_file, | 193 bool overwrite_existing_file, |
| 194 const RenameCompletionCallback& callback) { | 194 const RenameCompletionCallback& callback) { |
| 195 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 195 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 196 DownloadFile* download_file = GetDownloadFile(global_id); | 196 DownloadFile* download_file = GetDownloadFile(global_id); |
| 197 if (!download_file) { | 197 if (!download_file) { |
| 198 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 198 BrowserThread::PostTask( |
| 199 base::Bind(callback, FilePath())); | 199 BrowserThread::UI, FROM_HERE, |
| 200 base::Bind(callback, content::DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, |
| 201 FilePath())); |
| 200 return; | 202 return; |
| 201 } | 203 } |
| 202 | 204 |
| 203 FilePath new_path(full_path); | 205 download_file->Rename(full_path, overwrite_existing_file, callback); |
| 204 if (!overwrite_existing_file) { | |
| 205 // Make the file unique if requested. | |
| 206 int uniquifier = | |
| 207 file_util::GetUniquePathNumber(new_path, FILE_PATH_LITERAL("")); | |
| 208 if (uniquifier > 0) { | |
| 209 new_path = new_path.InsertBeforeExtensionASCII( | |
| 210 StringPrintf(" (%d)", uniquifier)); | |
| 211 } | |
| 212 } | |
| 213 | |
| 214 // Do the actual rename | |
| 215 content::DownloadInterruptReason rename_error = | |
| 216 download_file->Rename(new_path); | |
| 217 if (content::DOWNLOAD_INTERRUPT_REASON_NONE != rename_error) { | |
| 218 DownloadManager* download_manager = download_file->GetDownloadManager(); | |
| 219 DCHECK(download_manager); | |
| 220 | |
| 221 BrowserThread::PostTask( | |
| 222 BrowserThread::UI, FROM_HERE, | |
| 223 base::Bind(&DownloadManager::OnDownloadInterrupted, | |
| 224 download_manager, | |
| 225 global_id.local(), | |
| 226 download_file->BytesSoFar(), | |
| 227 download_file->GetHashState(), | |
| 228 rename_error)); | |
| 229 | |
| 230 new_path.clear(); | |
| 231 } | |
| 232 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 233 base::Bind(callback, new_path)); | |
| 234 } | 206 } |
| 235 | 207 |
| 236 int DownloadFileManager::NumberOfActiveDownloads() const { | 208 int DownloadFileManager::NumberOfActiveDownloads() const { |
| 237 return downloads_.size(); | 209 return downloads_.size(); |
| 238 } | 210 } |
| 239 | 211 |
| 240 void DownloadFileManager::EraseDownload(DownloadId global_id) { | 212 void DownloadFileManager::EraseDownload(DownloadId global_id) { |
| 241 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 242 | 214 |
| 243 if (!ContainsKey(downloads_, global_id)) | 215 if (!ContainsKey(downloads_, global_id)) |
| 244 return; | 216 return; |
| 245 | 217 |
| 246 DownloadFile* download_file = downloads_[global_id]; | 218 DownloadFile* download_file = downloads_[global_id]; |
| 247 | 219 |
| 248 VLOG(20) << " " << __FUNCTION__ << "()" | 220 VLOG(20) << " " << __FUNCTION__ << "()" |
| 249 << " id = " << global_id | 221 << " id = " << global_id |
| 250 << " download_file = " << download_file->DebugString(); | 222 << " download_file = " << download_file->DebugString(); |
| 251 | 223 |
| 252 downloads_.erase(global_id); | 224 downloads_.erase(global_id); |
| 253 | 225 |
| 254 delete download_file; | 226 delete download_file; |
| 255 } | 227 } |
| OLD | NEW |