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( |
99 new_path.c_str())) { | 99 full_path_.value().c_str(), new_path.value().c_str())) { |
100 return false; | 100 return false; |
101 } | 101 } |
102 | 102 |
103 DeleteFile(full_path_.c_str()); | 103 file_util::Delete(full_path_, false); |
104 | 104 |
105 full_path_ = new_path; | 105 full_path_ = new_path; |
106 path_renamed_ = true; | 106 path_renamed_ = true; |
107 | 107 |
108 // We don't need to re-open the file if we're done (finished or canceled). | 108 // We don't need to re-open the file if we're done (finished or canceled). |
109 if (!in_progress_) | 109 if (!in_progress_) |
110 return true; | 110 return true; |
111 | 111 |
112 if (!Open("a+b")) | 112 if (!Open("a+b")) |
113 return false; | 113 return false; |
114 return true; | 114 return true; |
115 } | 115 } |
116 | 116 |
117 void DownloadFile::Close() { | 117 void DownloadFile::Close() { |
118 if (file_) { | 118 if (file_) { |
119 file_util::CloseFile(file_); | 119 file_util::CloseFile(file_); |
120 file_ = NULL; | 120 file_ = NULL; |
121 } | 121 } |
122 } | 122 } |
123 | 123 |
124 bool DownloadFile::Open(const char* open_mode) { | 124 bool DownloadFile::Open(const char* open_mode) { |
125 DCHECK(!full_path_.empty()); | 125 DCHECK(!full_path_.empty()); |
126 file_ = file_util::OpenFile(full_path_, open_mode); | 126 file_ = file_util::OpenFile(full_path_, open_mode); |
127 if (!file_) { | 127 if (!file_) { |
128 return false; | 128 return false; |
129 } | 129 } |
130 // Sets the Zone to tell Windows that this file comes from the internet. | 130 // Sets the Zone to tell Windows that this file comes from the internet. |
131 // We ignore the return value because a failure is not fatal. | 131 // We ignore the return value because a failure is not fatal. |
132 win_util::SetInternetZoneIdentifier(full_path_); | 132 win_util::SetInternetZoneIdentifier(full_path_.value()); |
133 return true; | 133 return true; |
134 } | 134 } |
135 | 135 |
136 // DownloadFileManager implementation ------------------------------------------ | 136 // DownloadFileManager implementation ------------------------------------------ |
137 | 137 |
138 DownloadFileManager::DownloadFileManager(MessageLoop* ui_loop, | 138 DownloadFileManager::DownloadFileManager(MessageLoop* ui_loop, |
139 ResourceDispatcherHost* rdh) | 139 ResourceDispatcherHost* rdh) |
140 : next_id_(0), | 140 : next_id_(0), |
141 ui_loop_(ui_loop), | 141 ui_loop_(ui_loop), |
142 resource_dispatcher_host_(rdh) { | 142 resource_dispatcher_host_(rdh) { |
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
505 render_process_host_id, | 505 render_process_host_id, |
506 render_view_id, | 506 render_view_id, |
507 request_context); | 507 request_context); |
508 } | 508 } |
509 | 509 |
510 // Actions from the UI thread and run on the download thread | 510 // Actions from the UI thread and run on the download thread |
511 | 511 |
512 // Open a download, or show it in a Windows Explorer window. We run on this | 512 // 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. | 513 // thread to avoid blocking the UI with (potentially) slow Shell operations. |
514 // TODO(paulg): File 'stat' operations. | 514 // TODO(paulg): File 'stat' operations. |
515 void DownloadFileManager::OnShowDownloadInShell(const std::wstring full_path) { | 515 void DownloadFileManager::OnShowDownloadInShell(const FilePath& full_path) { |
516 DCHECK(MessageLoop::current() == file_loop_); | 516 DCHECK(MessageLoop::current() == file_loop_); |
517 win_util::ShowItemInFolder(full_path); | 517 win_util::ShowItemInFolder(full_path.value()); |
518 } | 518 } |
519 | 519 |
520 // Launches the selected download using ShellExecute 'open' verb. If there is | 520 // Launches the selected download using ShellExecute 'open' verb. If there is |
521 // a valid parent window, the 'safer' version will be used which can | 521 // a valid parent window, the 'safer' version will be used which can |
522 // display a modal dialog asking for user consent on dangerous files. | 522 // display a modal dialog asking for user consent on dangerous files. |
523 void DownloadFileManager::OnOpenDownloadInShell(const std::wstring full_path, | 523 void DownloadFileManager::OnOpenDownloadInShell(const FilePath& full_path, |
524 const std::wstring& url, | 524 const std::wstring& url, |
525 HWND parent_window) { | 525 HWND parent_window) { |
526 DCHECK(MessageLoop::current() == file_loop_); | 526 DCHECK(MessageLoop::current() == file_loop_); |
527 if (NULL != parent_window) { | 527 if (NULL != parent_window) { |
528 win_util::SaferOpenItemViaShell(parent_window, L"", full_path, url, true); | 528 win_util::SaferOpenItemViaShell(parent_window, L"", full_path.value(), |
| 529 url, true); |
529 } else { | 530 } else { |
530 win_util::OpenItemViaShell(full_path, true); | 531 win_util::OpenItemViaShell(full_path.value(), true); |
531 } | 532 } |
532 } | 533 } |
533 | 534 |
534 // The DownloadManager in the UI thread has provided a final name for the | 535 // 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 | 536 // download specified by 'id'. Rename the in progress download, and remove it |
536 // from our table if it has been completed or cancelled already. | 537 // from our table if it has been completed or cancelled already. |
537 void DownloadFileManager::OnFinalDownloadName(int id, | 538 void DownloadFileManager::OnFinalDownloadName(int id, |
538 const std::wstring& full_path) { | 539 const FilePath& full_path) { |
539 DCHECK(MessageLoop::current() == file_loop_); | 540 DCHECK(MessageLoop::current() == file_loop_); |
540 DownloadFileMap::iterator it = downloads_.find(id); | 541 DownloadFileMap::iterator it = downloads_.find(id); |
541 if (it == downloads_.end()) | 542 if (it == downloads_.end()) |
542 return; | 543 return; |
543 | 544 |
544 std::wstring download_dir = file_util::GetDirectoryFromPath(full_path); | 545 file_util::CreateDirectory(full_path.DirName()); |
545 if (!file_util::PathExists(download_dir)) | |
546 file_util::CreateDirectory(download_dir); | |
547 | 546 |
548 DownloadFile* download = it->second; | 547 DownloadFile* download = it->second; |
549 if (!download->Rename(full_path)) { | 548 if (!download->Rename(full_path)) { |
550 // Error. Between the time the UI thread generated 'full_path' to the time | 549 // Error. Between the time the UI thread generated 'full_path' to the time |
551 // this code runs, something happened that prevents us from renaming. | 550 // this code runs, something happened that prevents us from renaming. |
552 DownloadManagerMap::iterator dmit = managers_.find(download->id()); | 551 DownloadManagerMap::iterator dmit = managers_.find(download->id()); |
553 if (dmit != managers_.end()) { | 552 if (dmit != managers_.end()) { |
554 DownloadManager* dlm = dmit->second; | 553 DownloadManager* dlm = dmit->second; |
555 ui_loop_->PostTask(FROM_HERE, | 554 ui_loop_->PostTask(FROM_HERE, |
556 NewRunnableMethod(dlm, | 555 NewRunnableMethod(dlm, |
(...skipping 12 matching lines...) Expand all Loading... |
569 if (!download->in_progress()) { | 568 if (!download->in_progress()) { |
570 downloads_.erase(it); | 569 downloads_.erase(it); |
571 delete download; | 570 delete download; |
572 } | 571 } |
573 | 572 |
574 if (downloads_.empty()) | 573 if (downloads_.empty()) |
575 ui_loop_->PostTask(FROM_HERE, NewRunnableMethod( | 574 ui_loop_->PostTask(FROM_HERE, NewRunnableMethod( |
576 this, &DownloadFileManager::StopUpdateTimer)); | 575 this, &DownloadFileManager::StopUpdateTimer)); |
577 } | 576 } |
578 | 577 |
579 void DownloadFileManager::CreateDirectory(const std::wstring& directory) { | 578 // static |
580 if (!file_util::PathExists(directory)) | 579 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. | 580 // Make sure we only delete files. |
586 if (file_util::PathExists(path) && !file_util::DirectoryExists(path)) | 581 if (!file_util::DirectoryExists(path)) |
587 file_util::Delete(path, false); | 582 file_util::Delete(path, false); |
588 } | 583 } |
OLD | NEW |