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( | 198 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
199 BrowserThread::UI, FROM_HERE, | 199 base::Bind(callback, FilePath())); |
200 base::Bind(callback, content::DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, | |
201 FilePath())); | |
202 return; | 200 return; |
203 } | 201 } |
204 | 202 |
205 download_file->Rename(full_path, overwrite_existing_file, callback); | 203 FilePath new_path(full_path); |
| 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)); |
206 } | 234 } |
207 | 235 |
208 int DownloadFileManager::NumberOfActiveDownloads() const { | 236 int DownloadFileManager::NumberOfActiveDownloads() const { |
209 return downloads_.size(); | 237 return downloads_.size(); |
210 } | 238 } |
211 | 239 |
212 void DownloadFileManager::EraseDownload(DownloadId global_id) { | 240 void DownloadFileManager::EraseDownload(DownloadId global_id) { |
213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 241 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
214 | 242 |
215 if (!ContainsKey(downloads_, global_id)) | 243 if (!ContainsKey(downloads_, global_id)) |
216 return; | 244 return; |
217 | 245 |
218 DownloadFile* download_file = downloads_[global_id]; | 246 DownloadFile* download_file = downloads_[global_id]; |
219 | 247 |
220 VLOG(20) << " " << __FUNCTION__ << "()" | 248 VLOG(20) << " " << __FUNCTION__ << "()" |
221 << " id = " << global_id | 249 << " id = " << global_id |
222 << " download_file = " << download_file->DebugString(); | 250 << " download_file = " << download_file->DebugString(); |
223 | 251 |
224 downloads_.erase(global_id); | 252 downloads_.erase(global_id); |
225 | 253 |
226 delete download_file; | 254 delete download_file; |
227 } | 255 } |
OLD | NEW |