| 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 // File method ordering: Methods in this file are in the same order as | 5 // File method ordering: Methods in this file are in the same order as |
| 6 // in download_item_impl.h, with the following exception: The public | 6 // in download_item_impl.h, with the following exception: The public |
| 7 // interface Start is placed in chronological order with the other | 7 // interface Start is placed in chronological order with the other |
| 8 // (private) routines that together define a DownloadItem's state | 8 // (private) routines that together define a DownloadItem's state |
| 9 // transitions as the download progresses. See "Download progression | 9 // transitions as the download progresses. See "Download progression |
| 10 // cascade" later in this file. | 10 // cascade" later in this file. |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 | 59 |
| 60 bool DeleteDownloadedFile(const base::FilePath& path) { | 60 bool DeleteDownloadedFile(const base::FilePath& path) { |
| 61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 62 | 62 |
| 63 // Make sure we only delete files. | 63 // Make sure we only delete files. |
| 64 if (base::DirectoryExists(path)) | 64 if (base::DirectoryExists(path)) |
| 65 return true; | 65 return true; |
| 66 return base::DeleteFile(path, false); | 66 return base::DeleteFile(path, false); |
| 67 } | 67 } |
| 68 | 68 |
| 69 void DeleteDownloadedFileDone( |
| 70 base::WeakPtr<DownloadItemImpl> item, |
| 71 const base::Callback<void(bool)>& callback, |
| 72 bool success) { |
| 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 74 if (success && item.get()) |
| 75 item->OnDownloadedFileRemoved(); |
| 76 callback.Run(success); |
| 77 } |
| 78 |
| 69 // Wrapper around DownloadFile::Detach and DownloadFile::Cancel that | 79 // Wrapper around DownloadFile::Detach and DownloadFile::Cancel that |
| 70 // takes ownership of the DownloadFile and hence implicitly destroys it | 80 // takes ownership of the DownloadFile and hence implicitly destroys it |
| 71 // at the end of the function. | 81 // at the end of the function. |
| 72 static base::FilePath DownloadFileDetach( | 82 static base::FilePath DownloadFileDetach( |
| 73 scoped_ptr<DownloadFile> download_file) { | 83 scoped_ptr<DownloadFile> download_file) { |
| 74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 75 base::FilePath full_path = download_file->FullPath(); | 85 base::FilePath full_path = download_file->FullPath(); |
| 76 download_file->Detach(); | 86 download_file->Detach(); |
| 77 return full_path; | 87 return full_path; |
| 78 } | 88 } |
| (...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 609 } | 619 } |
| 610 | 620 |
| 611 const std::string& DownloadItemImpl::GetHashState() const { | 621 const std::string& DownloadItemImpl::GetHashState() const { |
| 612 return hash_state_; | 622 return hash_state_; |
| 613 } | 623 } |
| 614 | 624 |
| 615 bool DownloadItemImpl::GetFileExternallyRemoved() const { | 625 bool DownloadItemImpl::GetFileExternallyRemoved() const { |
| 616 return file_externally_removed_; | 626 return file_externally_removed_; |
| 617 } | 627 } |
| 618 | 628 |
| 619 void DownloadItemImpl::DeleteFile() { | 629 void DownloadItemImpl::DeleteFile(const base::Callback<void(bool)>& callback) { |
| 620 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 630 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 621 if ((GetState() != DownloadItem::COMPLETE) || | 631 if (GetState() != DownloadItem::COMPLETE) { |
| 622 file_externally_removed_) { | 632 // Pass a null WeakPtr so it doesn't call OnDownloadedFileRemoved. |
| 633 BrowserThread::PostTask( |
| 634 BrowserThread::UI, FROM_HERE, |
| 635 base::Bind(&DeleteDownloadedFileDone, |
| 636 base::WeakPtr<DownloadItemImpl>(), callback, false)); |
| 637 return; |
| 638 } |
| 639 if (current_path_.empty() || file_externally_removed_) { |
| 640 // Pass a null WeakPtr so it doesn't call OnDownloadedFileRemoved. |
| 641 BrowserThread::PostTask( |
| 642 BrowserThread::UI, FROM_HERE, |
| 643 base::Bind(&DeleteDownloadedFileDone, |
| 644 base::WeakPtr<DownloadItemImpl>(), callback, true)); |
| 623 return; | 645 return; |
| 624 } | 646 } |
| 625 BrowserThread::PostTaskAndReplyWithResult( | 647 BrowserThread::PostTaskAndReplyWithResult( |
| 626 BrowserThread::FILE, FROM_HERE, | 648 BrowserThread::FILE, FROM_HERE, |
| 627 base::Bind(&DeleteDownloadedFile, current_path_), | 649 base::Bind(&DeleteDownloadedFile, current_path_), |
| 628 base::Bind(&DownloadItemImpl::OnDownloadedFileRemoved, | 650 base::Bind(&DeleteDownloadedFileDone, |
| 629 weak_ptr_factory_.GetWeakPtr())); | 651 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 630 current_path_.clear(); | 652 current_path_.clear(); |
| 631 } | 653 } |
| 632 | 654 |
| 633 bool DownloadItemImpl::IsDangerous() const { | 655 bool DownloadItemImpl::IsDangerous() const { |
| 634 #if defined(OS_WIN) | 656 #if defined(OS_WIN) |
| 635 // TODO(noelutz): At this point only the windows views UI supports | 657 // TODO(noelutz): At this point only the windows views UI supports |
| 636 // warnings based on dangerous content. | 658 // warnings based on dangerous content. |
| 637 return (danger_type_ == DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE || | 659 return (danger_type_ == DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE || |
| 638 danger_type_ == DOWNLOAD_DANGER_TYPE_DANGEROUS_URL || | 660 danger_type_ == DOWNLOAD_DANGER_TYPE_DANGEROUS_URL || |
| 639 danger_type_ == DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT || | 661 danger_type_ == DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT || |
| (...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 940 | 962 |
| 941 // Don't update observers. This method is expected to be called just before a | 963 // Don't update observers. This method is expected to be called just before a |
| 942 // DownloadFile is created and Start() is called. The observers will be | 964 // DownloadFile is created and Start() is called. The observers will be |
| 943 // notified when the download transitions to the IN_PROGRESS state. | 965 // notified when the download transitions to the IN_PROGRESS state. |
| 944 } | 966 } |
| 945 | 967 |
| 946 void DownloadItemImpl::NotifyRemoved() { | 968 void DownloadItemImpl::NotifyRemoved() { |
| 947 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadRemoved(this)); | 969 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadRemoved(this)); |
| 948 } | 970 } |
| 949 | 971 |
| 950 void DownloadItemImpl::OnDownloadedFileRemoved(bool success) { | 972 void DownloadItemImpl::OnDownloadedFileRemoved() { |
| 951 if (!success) | |
| 952 return; | |
| 953 file_externally_removed_ = true; | 973 file_externally_removed_ = true; |
| 954 VLOG(20) << __FUNCTION__ << " download=" << DebugString(true); | 974 VLOG(20) << __FUNCTION__ << " download=" << DebugString(true); |
| 955 UpdateObservers(); | 975 UpdateObservers(); |
| 956 } | 976 } |
| 957 | 977 |
| 958 base::WeakPtr<DownloadDestinationObserver> | 978 base::WeakPtr<DownloadDestinationObserver> |
| 959 DownloadItemImpl::DestinationObserverAsWeakPtr() { | 979 DownloadItemImpl::DestinationObserverAsWeakPtr() { |
| 960 return weak_ptr_factory_.GetWeakPtr(); | 980 return weak_ptr_factory_.GetWeakPtr(); |
| 961 } | 981 } |
| 962 | 982 |
| (...skipping 799 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1762 case RESUME_MODE_USER_CONTINUE: | 1782 case RESUME_MODE_USER_CONTINUE: |
| 1763 return "USER_CONTINUE"; | 1783 return "USER_CONTINUE"; |
| 1764 case RESUME_MODE_USER_RESTART: | 1784 case RESUME_MODE_USER_RESTART: |
| 1765 return "USER_RESTART"; | 1785 return "USER_RESTART"; |
| 1766 } | 1786 } |
| 1767 NOTREACHED() << "Unknown resume mode " << mode; | 1787 NOTREACHED() << "Unknown resume mode " << mode; |
| 1768 return "unknown"; | 1788 return "unknown"; |
| 1769 } | 1789 } |
| 1770 | 1790 |
| 1771 } // namespace content | 1791 } // namespace content |
| OLD | NEW |