Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(105)

Side by Side Diff: chrome/browser/download/download_file.cc

Issue 16522: Unbreak unit tests. Revert r7564. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 11 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/download/download_file.h ('k') | chrome/browser/download/download_manager.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 file_util::Delete(full_path_, false); 88 DeleteFile(full_path_.c_str());
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 FilePath& new_path) { 92 bool DownloadFile::Rename(const std::wstring& 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( 98 if (!file_util::RenameFileAndResetSecurityDescriptor(full_path_.c_str(),
99 full_path_.value().c_str(), new_path.value().c_str())) { 99 new_path.c_str())) {
100 return false; 100 return false;
101 } 101 }
102 102
103 file_util::Delete(full_path_, false); 103 DeleteFile(full_path_.c_str());
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_.value()); 132 win_util::SetInternetZoneIdentifier(full_path_);
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
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 FilePath& full_path) { 515 void DownloadFileManager::OnShowDownloadInShell(const std::wstring full_path) {
516 DCHECK(MessageLoop::current() == file_loop_); 516 DCHECK(MessageLoop::current() == file_loop_);
517 win_util::ShowItemInFolder(full_path.value()); 517 win_util::ShowItemInFolder(full_path);
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 FilePath& full_path, 523 void DownloadFileManager::OnOpenDownloadInShell(const std::wstring 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.value(), 528 win_util::SaferOpenItemViaShell(parent_window, L"", full_path, url, true);
529 url, true);
530 } else { 529 } else {
531 win_util::OpenItemViaShell(full_path.value(), true); 530 win_util::OpenItemViaShell(full_path, true);
532 } 531 }
533 } 532 }
534 533
535 // The DownloadManager in the UI thread has provided a final name for the 534 // The DownloadManager in the UI thread has provided a final name for the
536 // download specified by 'id'. Rename the in progress download, and remove it 535 // download specified by 'id'. Rename the in progress download, and remove it
537 // from our table if it has been completed or cancelled already. 536 // from our table if it has been completed or cancelled already.
538 void DownloadFileManager::OnFinalDownloadName(int id, 537 void DownloadFileManager::OnFinalDownloadName(int id,
539 const FilePath& full_path) { 538 const std::wstring& full_path) {
540 DCHECK(MessageLoop::current() == file_loop_); 539 DCHECK(MessageLoop::current() == file_loop_);
541 DownloadFileMap::iterator it = downloads_.find(id); 540 DownloadFileMap::iterator it = downloads_.find(id);
542 if (it == downloads_.end()) 541 if (it == downloads_.end())
543 return; 542 return;
544 543
545 file_util::CreateDirectory(full_path.DirName()); 544 std::wstring download_dir = file_util::GetDirectoryFromPath(full_path);
545 if (!file_util::PathExists(download_dir))
546 file_util::CreateDirectory(download_dir);
546 547
547 DownloadFile* download = it->second; 548 DownloadFile* download = it->second;
548 if (!download->Rename(full_path)) { 549 if (!download->Rename(full_path)) {
549 // Error. Between the time the UI thread generated 'full_path' to the time 550 // Error. Between the time the UI thread generated 'full_path' to the time
550 // this code runs, something happened that prevents us from renaming. 551 // this code runs, something happened that prevents us from renaming.
551 DownloadManagerMap::iterator dmit = managers_.find(download->id()); 552 DownloadManagerMap::iterator dmit = managers_.find(download->id());
552 if (dmit != managers_.end()) { 553 if (dmit != managers_.end()) {
553 DownloadManager* dlm = dmit->second; 554 DownloadManager* dlm = dmit->second;
554 ui_loop_->PostTask(FROM_HERE, 555 ui_loop_->PostTask(FROM_HERE,
555 NewRunnableMethod(dlm, 556 NewRunnableMethod(dlm,
(...skipping 12 matching lines...) Expand all
568 if (!download->in_progress()) { 569 if (!download->in_progress()) {
569 downloads_.erase(it); 570 downloads_.erase(it);
570 delete download; 571 delete download;
571 } 572 }
572 573
573 if (downloads_.empty()) 574 if (downloads_.empty())
574 ui_loop_->PostTask(FROM_HERE, NewRunnableMethod( 575 ui_loop_->PostTask(FROM_HERE, NewRunnableMethod(
575 this, &DownloadFileManager::StopUpdateTimer)); 576 this, &DownloadFileManager::StopUpdateTimer));
576 } 577 }
577 578
578 // static 579 void DownloadFileManager::CreateDirectory(const std::wstring& directory) {
579 void DownloadFileManager::DeleteFile(const FilePath& path) { 580 if (!file_util::PathExists(directory))
581 file_util::CreateDirectory(directory);
582 }
583
584 void DownloadFileManager::DeleteFile(const std::wstring& path) {
580 // Make sure we only delete files. 585 // Make sure we only delete files.
581 if (!file_util::DirectoryExists(path)) 586 if (file_util::PathExists(path) && !file_util::DirectoryExists(path))
582 file_util::Delete(path, false); 587 file_util::Delete(path, false);
583 } 588 }
OLDNEW
« no previous file with comments | « chrome/browser/download/download_file.h ('k') | chrome/browser/download/download_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698