OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 <Windows.h> | 5 #include <Windows.h> |
6 #include <objbase.h> | 6 #include <objbase.h> |
7 | 7 |
8 #include "chrome/browser/download/download_file.h" | 8 #include "chrome/browser/download/download_file.h" |
9 | 9 |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 if (file_) { | 78 if (file_) { |
79 fwrite(data, 1, data_len, file_); | 79 fwrite(data, 1, data_len, file_); |
80 bytes_so_far_ += data_len; | 80 bytes_so_far_ += data_len; |
81 return true; | 81 return true; |
82 } | 82 } |
83 return false; | 83 return false; |
84 } | 84 } |
85 | 85 |
86 void DownloadFile::Cancel() { | 86 void DownloadFile::Cancel() { |
87 Close(); | 87 Close(); |
88 DeleteFile(full_path_.c_str()); | 88 file_util::Delete(full_path_, false); |
89 } | 89 } |
90 | 90 |
91 // The UI has provided us with our finalized name. | 91 // The UI has provided us with our finalized name. |
92 bool DownloadFile::Rename(const std::wstring& new_path) { | 92 bool DownloadFile::Rename(const FilePath& new_path) { |
93 Close(); | 93 Close(); |
94 | 94 |
95 // We cannot rename because rename will keep the same security descriptor | 95 // We cannot rename because rename will keep the same security descriptor |
96 // on the destination file. We want to recreate the security descriptor | 96 // on the destination file. We want to recreate the security descriptor |
97 // with the security that makes sense in the new path. | 97 // with the security that makes sense in the new path. |
98 if (!file_util::RenameFileAndResetSecurityDescriptor(full_path_.c_str(), | 98 if (!file_util::RenameFileAndResetSecurityDescriptor(full_path_, new_path)) { |
99 new_path.c_str())) { | |
100 return false; | 99 return false; |
101 } | 100 } |
102 | 101 |
103 DeleteFile(full_path_.c_str()); | 102 file_util::Delete(full_path_, false); |
104 | 103 |
105 full_path_ = new_path; | 104 full_path_ = new_path; |
106 path_renamed_ = true; | 105 path_renamed_ = true; |
107 | 106 |
108 // We don't need to re-open the file if we're done (finished or canceled). | 107 // We don't need to re-open the file if we're done (finished or canceled). |
109 if (!in_progress_) | 108 if (!in_progress_) |
110 return true; | 109 return true; |
111 | 110 |
112 if (!Open("a+b")) | 111 if (!Open("a+b")) |
113 return false; | 112 return false; |
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
505 render_process_host_id, | 504 render_process_host_id, |
506 render_view_id, | 505 render_view_id, |
507 request_context); | 506 request_context); |
508 } | 507 } |
509 | 508 |
510 // Actions from the UI thread and run on the download thread | 509 // Actions from the UI thread and run on the download thread |
511 | 510 |
512 // Open a download, or show it in a Windows Explorer window. We run on this | 511 // Open a download, or show it in a Windows Explorer window. We run on this |
513 // thread to avoid blocking the UI with (potentially) slow Shell operations. | 512 // thread to avoid blocking the UI with (potentially) slow Shell operations. |
514 // TODO(paulg): File 'stat' operations. | 513 // TODO(paulg): File 'stat' operations. |
515 void DownloadFileManager::OnShowDownloadInShell(const std::wstring full_path) { | 514 void DownloadFileManager::OnShowDownloadInShell(const FilePath& full_path) { |
516 DCHECK(MessageLoop::current() == file_loop_); | 515 DCHECK(MessageLoop::current() == file_loop_); |
517 win_util::ShowItemInFolder(full_path); | 516 win_util::ShowItemInFolder(full_path.value()); |
518 } | 517 } |
519 | 518 |
520 // Launches the selected download using ShellExecute 'open' verb. If there is | 519 // Launches the selected download using ShellExecute 'open' verb. If there is |
521 // a valid parent window, the 'safer' version will be used which can | 520 // a valid parent window, the 'safer' version will be used which can |
522 // display a modal dialog asking for user consent on dangerous files. | 521 // display a modal dialog asking for user consent on dangerous files. |
523 void DownloadFileManager::OnOpenDownloadInShell(const std::wstring full_path, | 522 void DownloadFileManager::OnOpenDownloadInShell(const FilePath& full_path, |
524 const std::wstring& url, | 523 const std::wstring& url, |
525 HWND parent_window) { | 524 HWND parent_window) { |
526 DCHECK(MessageLoop::current() == file_loop_); | 525 DCHECK(MessageLoop::current() == file_loop_); |
527 if (NULL != parent_window) { | 526 if (NULL != parent_window) { |
528 win_util::SaferOpenItemViaShell(parent_window, L"", full_path, url, true); | 527 win_util::SaferOpenItemViaShell(parent_window, L"", full_path, url, true); |
529 } else { | 528 } else { |
530 win_util::OpenItemViaShell(full_path, true); | 529 win_util::OpenItemViaShell(full_path, true); |
531 } | 530 } |
532 } | 531 } |
533 | 532 |
534 // The DownloadManager in the UI thread has provided a final name for the | 533 // The DownloadManager in the UI thread has provided a final name for the |
535 // download specified by 'id'. Rename the in progress download, and remove it | 534 // download specified by 'id'. Rename the in progress download, and remove it |
536 // from our table if it has been completed or cancelled already. | 535 // from our table if it has been completed or cancelled already. |
537 void DownloadFileManager::OnFinalDownloadName(int id, | 536 void DownloadFileManager::OnFinalDownloadName(int id, |
538 const std::wstring& full_path) { | 537 const FilePath& full_path) { |
539 DCHECK(MessageLoop::current() == file_loop_); | 538 DCHECK(MessageLoop::current() == file_loop_); |
540 DownloadFileMap::iterator it = downloads_.find(id); | 539 DownloadFileMap::iterator it = downloads_.find(id); |
541 if (it == downloads_.end()) | 540 if (it == downloads_.end()) |
542 return; | 541 return; |
543 | 542 |
544 std::wstring download_dir = file_util::GetDirectoryFromPath(full_path); | 543 file_util::CreateDirectory(full_path.DirName()); |
545 if (!file_util::PathExists(download_dir)) | |
546 file_util::CreateDirectory(download_dir); | |
547 | 544 |
548 DownloadFile* download = it->second; | 545 DownloadFile* download = it->second; |
549 if (!download->Rename(full_path)) { | 546 if (!download->Rename(full_path)) { |
550 // Error. Between the time the UI thread generated 'full_path' to the time | 547 // Error. Between the time the UI thread generated 'full_path' to the time |
551 // this code runs, something happened that prevents us from renaming. | 548 // this code runs, something happened that prevents us from renaming. |
552 DownloadManagerMap::iterator dmit = managers_.find(download->id()); | 549 DownloadManagerMap::iterator dmit = managers_.find(download->id()); |
553 if (dmit != managers_.end()) { | 550 if (dmit != managers_.end()) { |
554 DownloadManager* dlm = dmit->second; | 551 DownloadManager* dlm = dmit->second; |
555 ui_loop_->PostTask(FROM_HERE, | 552 ui_loop_->PostTask(FROM_HERE, |
556 NewRunnableMethod(dlm, | 553 NewRunnableMethod(dlm, |
(...skipping 12 matching lines...) Expand all Loading... |
569 if (!download->in_progress()) { | 566 if (!download->in_progress()) { |
570 downloads_.erase(it); | 567 downloads_.erase(it); |
571 delete download; | 568 delete download; |
572 } | 569 } |
573 | 570 |
574 if (downloads_.empty()) | 571 if (downloads_.empty()) |
575 ui_loop_->PostTask(FROM_HERE, NewRunnableMethod( | 572 ui_loop_->PostTask(FROM_HERE, NewRunnableMethod( |
576 this, &DownloadFileManager::StopUpdateTimer)); | 573 this, &DownloadFileManager::StopUpdateTimer)); |
577 } | 574 } |
578 | 575 |
579 void DownloadFileManager::CreateDirectory(const std::wstring& directory) { | 576 // static |
580 if (!file_util::PathExists(directory)) | 577 void DownloadFileManager::DeleteFile(const FilePath& path) { |
581 file_util::CreateDirectory(directory); | |
582 } | |
583 | |
584 void DownloadFileManager::DeleteFile(const std::wstring& path) { | |
585 // Make sure we only delete files. | 578 // Make sure we only delete files. |
586 if (file_util::PathExists(path) && !file_util::DirectoryExists(path)) | 579 if (!file_util::DirectoryExists(path)) |
587 file_util::Delete(path, false); | 580 file_util::Delete(path, false); |
588 } | 581 } |
OLD | NEW |