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_item_impl.h" | 5 #include "content/browser/download/download_item_impl.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 682 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
693 | 693 |
694 DCHECK(IsInProgress()); | 694 DCHECK(IsInProgress()); |
695 if (is_paused_) | 695 if (is_paused_) |
696 request_handle_->ResumeRequest(); | 696 request_handle_->ResumeRequest(); |
697 else | 697 else |
698 request_handle_->PauseRequest(); | 698 request_handle_->PauseRequest(); |
699 is_paused_ = !is_paused_; | 699 is_paused_ = !is_paused_; |
700 UpdateObservers(); | 700 UpdateObservers(); |
701 } | 701 } |
702 | 702 |
703 void DownloadItemImpl::OnDownloadCompleting(DownloadFileManager* file_manager) { | 703 void DownloadItemImpl::OnDownloadCompleting() { |
704 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 704 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
705 | 705 |
706 VLOG(20) << __FUNCTION__ << "()" | 706 VLOG(20) << __FUNCTION__ << "()" |
707 << " needs rename = " << NeedsRename() | 707 << " needs rename = " << NeedsRename() |
708 << " " << DebugString(true); | 708 << " " << DebugString(true); |
709 DCHECK(!GetTargetName().empty()); | 709 DCHECK(!GetTargetName().empty()); |
710 DCHECK_NE(DANGEROUS, GetSafetyState()); | 710 DCHECK_NE(DANGEROUS, GetSafetyState()); |
711 DCHECK(file_manager); | |
712 | 711 |
713 if (NeedsRename()) { | 712 if (NeedsRename()) { |
714 DownloadFileManager::RenameCompletionCallback callback = | 713 DownloadFileManager::RenameCompletionCallback callback = |
715 base::Bind(&DownloadItemImpl::OnDownloadRenamedToFinalName, | 714 base::Bind(&DownloadItemImpl::OnDownloadRenamedToFinalName, |
716 weak_ptr_factory_.GetWeakPtr(), | 715 weak_ptr_factory_.GetWeakPtr()); |
717 base::Unretained(file_manager)); | |
718 BrowserThread::PostTask( | 716 BrowserThread::PostTask( |
719 BrowserThread::FILE, FROM_HERE, | 717 BrowserThread::FILE, FROM_HERE, |
720 base::Bind(&DownloadFileManager::RenameDownloadFile, | 718 base::Bind(&DownloadFileManager::RenameDownloadFile, |
721 file_manager, GetGlobalId(), GetTargetFilePath(), | 719 delegate_->GetDownloadFileManager(), GetGlobalId(), |
722 true, callback)); | 720 GetTargetFilePath(), true, callback)); |
723 } else { | 721 } else { |
724 // Complete the download and release the DownloadFile. | 722 // Complete the download and release the DownloadFile. |
725 BrowserThread::PostTask( | 723 BrowserThread::PostTask( |
726 BrowserThread::FILE, FROM_HERE, | 724 BrowserThread::FILE, FROM_HERE, |
727 base::Bind(&DownloadFileManager::CompleteDownload, | 725 base::Bind(&DownloadFileManager::CompleteDownload, |
728 file_manager, GetGlobalId(), | 726 delegate_->GetDownloadFileManager(), GetGlobalId(), |
729 base::Bind(&DownloadItemImpl::OnDownloadFileReleased, | 727 base::Bind(&DownloadItemImpl::OnDownloadFileReleased, |
730 weak_ptr_factory_.GetWeakPtr()))); | 728 weak_ptr_factory_.GetWeakPtr()))); |
731 } | 729 } |
732 } | 730 } |
733 | 731 |
734 void DownloadItemImpl::OnDownloadRenamedToFinalName( | 732 void DownloadItemImpl::OnDownloadRenamedToFinalName( |
735 DownloadFileManager* file_manager, | |
736 content::DownloadInterruptReason reason, | 733 content::DownloadInterruptReason reason, |
737 const FilePath& full_path) { | 734 const FilePath& full_path) { |
738 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 735 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
739 | 736 |
740 VLOG(20) << __FUNCTION__ << "()" | 737 VLOG(20) << __FUNCTION__ << "()" |
741 << " full_path = \"" << full_path.value() << "\"" | 738 << " full_path = \"" << full_path.value() << "\"" |
742 << " needed rename = " << NeedsRename() | 739 << " needed rename = " << NeedsRename() |
743 << " " << DebugString(false); | 740 << " " << DebugString(false); |
744 DCHECK(NeedsRename()); | 741 DCHECK(NeedsRename()); |
745 | 742 |
746 if (content::DOWNLOAD_INTERRUPT_REASON_NONE != reason) { | 743 if (content::DOWNLOAD_INTERRUPT_REASON_NONE != reason) { |
747 Interrupt(reason); | 744 Interrupt(reason); |
748 return; | 745 return; |
749 } | 746 } |
750 | 747 |
751 // full_path is now the current and target file path. | 748 // full_path is now the current and target file path. |
752 DCHECK(!full_path.empty()); | 749 DCHECK(!full_path.empty()); |
753 target_path_ = full_path; | 750 target_path_ = full_path; |
754 SetFullPath(full_path); | 751 SetFullPath(full_path); |
755 delegate_->DownloadRenamedToFinalName(this); | 752 delegate_->DownloadRenamedToFinalName(this); |
756 | 753 |
757 // Complete the download and release the DownloadFile. | 754 // Complete the download and release the DownloadFile. |
758 BrowserThread::PostTask( | 755 BrowserThread::PostTask( |
759 BrowserThread::FILE, FROM_HERE, | 756 BrowserThread::FILE, FROM_HERE, |
760 base::Bind(&DownloadFileManager::CompleteDownload, | 757 base::Bind(&DownloadFileManager::CompleteDownload, |
761 file_manager, GetGlobalId(), | 758 delegate_->GetDownloadFileManager(), GetGlobalId(), |
762 base::Bind(&DownloadItemImpl::OnDownloadFileReleased, | 759 base::Bind(&DownloadItemImpl::OnDownloadFileReleased, |
763 weak_ptr_factory_.GetWeakPtr()))); | 760 weak_ptr_factory_.GetWeakPtr()))); |
764 } | 761 } |
765 | 762 |
766 void DownloadItemImpl::OnDownloadFileReleased() { | 763 void DownloadItemImpl::OnDownloadFileReleased() { |
767 if (delegate_->ShouldOpenDownload(this)) | 764 if (delegate_->ShouldOpenDownload(this)) |
768 Completed(); | 765 Completed(); |
769 else | 766 else |
770 delegate_delayed_complete_ = true; | 767 delegate_delayed_complete_ = true; |
771 } | 768 } |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
860 } | 857 } |
861 | 858 |
862 const FilePath& DownloadItemImpl::GetTargetFilePath() const { | 859 const FilePath& DownloadItemImpl::GetTargetFilePath() const { |
863 return target_path_; | 860 return target_path_; |
864 } | 861 } |
865 | 862 |
866 DownloadItem::TargetDisposition DownloadItemImpl::GetTargetDisposition() const { | 863 DownloadItem::TargetDisposition DownloadItemImpl::GetTargetDisposition() const { |
867 return target_disposition_; | 864 return target_disposition_; |
868 } | 865 } |
869 | 866 |
870 void DownloadItemImpl::OnTargetPathDetermined( | 867 void DownloadItemImpl::OnDownloadTargetDetermined( |
871 const FilePath& target_path, | 868 const FilePath& target_path, |
872 TargetDisposition disposition, | 869 TargetDisposition disposition, |
873 content::DownloadDangerType danger_type) { | 870 content::DownloadDangerType danger_type, |
| 871 const FilePath& intermediate_path) { |
874 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 872 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
875 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 873 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 874 |
| 875 // If the |target_path| is empty, then we consider this download to be |
| 876 // canceled. |
| 877 if (target_path.empty()) { |
| 878 Cancel(true); |
| 879 return; |
| 880 } |
| 881 |
876 target_path_ = target_path; | 882 target_path_ = target_path; |
877 target_disposition_ = disposition; | 883 target_disposition_ = disposition; |
878 SetDangerType(danger_type); | 884 SetDangerType(danger_type); |
879 } | 885 // TODO(asanka): SetDangerType() doesn't need to send a notification here. |
880 | 886 |
881 void DownloadItemImpl::OnTargetPathSelected(const FilePath& target_path) { | 887 // We want the intermediate and target paths to refer to the same directory so |
882 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 888 // that they are both on the same device and subject to same |
883 DCHECK_EQ(TARGET_DISPOSITION_PROMPT, target_disposition_); | 889 // space/permission/availability constraints. |
884 target_path_ = target_path; | 890 DCHECK(intermediate_path.DirName() == target_path.DirName()); |
| 891 |
| 892 // Rename to intermediate name. |
| 893 // TODO(asanka): Skip this rename if AllDataSaved() is true. This avoids a |
| 894 // spurious rename when we can just rename to the final |
| 895 // filename. Unnecessary renames may cause bugs like |
| 896 // http://crbug.com/74187. |
| 897 DownloadFileManager::RenameCompletionCallback callback = |
| 898 base::Bind(&DownloadItemImpl::OnDownloadRenamedToIntermediateName, |
| 899 weak_ptr_factory_.GetWeakPtr()); |
| 900 BrowserThread::PostTask( |
| 901 BrowserThread::FILE, FROM_HERE, |
| 902 base::Bind(&DownloadFileManager::RenameDownloadFile, |
| 903 delegate_->GetDownloadFileManager(), GetGlobalId(), |
| 904 intermediate_path, false, callback)); |
885 } | 905 } |
886 | 906 |
887 void DownloadItemImpl::OnContentCheckCompleted( | 907 void DownloadItemImpl::OnContentCheckCompleted( |
888 content::DownloadDangerType danger_type) { | 908 content::DownloadDangerType danger_type) { |
889 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 909 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
890 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 910 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
891 DCHECK(AllDataSaved()); | 911 DCHECK(AllDataSaved()); |
892 SetDangerType(danger_type); | 912 SetDangerType(danger_type); |
893 } | 913 } |
894 | 914 |
895 void DownloadItemImpl::OnIntermediatePathDetermined( | |
896 DownloadFileManager* file_manager, | |
897 const FilePath& intermediate_path) { | |
898 DownloadFileManager::RenameCompletionCallback callback = | |
899 base::Bind(&DownloadItemImpl::OnDownloadRenamedToIntermediateName, | |
900 weak_ptr_factory_.GetWeakPtr()); | |
901 BrowserThread::PostTask( | |
902 BrowserThread::FILE, FROM_HERE, | |
903 base::Bind(&DownloadFileManager::RenameDownloadFile, | |
904 file_manager, GetGlobalId(), intermediate_path, | |
905 false, callback)); | |
906 } | |
907 | |
908 const FilePath& DownloadItemImpl::GetFullPath() const { | 915 const FilePath& DownloadItemImpl::GetFullPath() const { |
909 return current_path_; | 916 return current_path_; |
910 } | 917 } |
911 | 918 |
912 FilePath DownloadItemImpl::GetFileNameToReportUser() const { | 919 FilePath DownloadItemImpl::GetFileNameToReportUser() const { |
913 if (!display_name_.empty()) | 920 if (!display_name_.empty()) |
914 return display_name_; | 921 return display_name_; |
915 return target_path_.BaseName(); | 922 return target_path_.BaseName(); |
916 } | 923 } |
917 | 924 |
918 void DownloadItemImpl::SetDisplayName(const FilePath& name) { | 925 void DownloadItemImpl::SetDisplayName(const FilePath& name) { |
919 display_name_ = name; | 926 display_name_ = name; |
920 } | 927 } |
921 | 928 |
922 FilePath DownloadItemImpl::GetUserVerifiedFilePath() const { | 929 FilePath DownloadItemImpl::GetUserVerifiedFilePath() const { |
923 return (safety_state_ == DownloadItem::SAFE) ? | 930 return (safety_state_ == DownloadItem::SAFE) ? |
924 GetTargetFilePath() : GetFullPath(); | 931 GetTargetFilePath() : GetFullPath(); |
925 } | 932 } |
926 | 933 |
927 void DownloadItemImpl::OffThreadCancel(DownloadFileManager* file_manager) { | 934 void DownloadItemImpl::OffThreadCancel() { |
928 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 935 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
929 request_handle_->CancelRequest(); | 936 request_handle_->CancelRequest(); |
930 | 937 |
931 BrowserThread::PostTask( | 938 BrowserThread::PostTask( |
932 BrowserThread::FILE, FROM_HERE, | 939 BrowserThread::FILE, FROM_HERE, |
933 base::Bind(&DownloadFileManager::CancelDownload, | 940 base::Bind(&DownloadFileManager::CancelDownload, |
934 file_manager, download_id_)); | 941 delegate_->GetDownloadFileManager(), download_id_)); |
935 } | 942 } |
936 | 943 |
937 void DownloadItemImpl::Init(bool active, | 944 void DownloadItemImpl::Init(bool active, |
938 download_net_logs::DownloadType download_type) { | 945 download_net_logs::DownloadType download_type) { |
939 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 946 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
940 | 947 |
941 if (active) | 948 if (active) |
942 download_stats::RecordDownloadCount(download_stats::START_COUNT); | 949 download_stats::RecordDownloadCount(download_stats::START_COUNT); |
943 | 950 |
944 if (target_path_.empty()) | 951 if (target_path_.empty()) |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1186 std::map<const void*, ExternalData*>::iterator it = | 1193 std::map<const void*, ExternalData*>::iterator it = |
1187 external_data_map_.find(key); | 1194 external_data_map_.find(key); |
1188 | 1195 |
1189 if (it == external_data_map_.end()) { | 1196 if (it == external_data_map_.end()) { |
1190 external_data_map_[key] = data; | 1197 external_data_map_[key] = data; |
1191 } else if (it->second != data) { | 1198 } else if (it->second != data) { |
1192 delete it->second; | 1199 delete it->second; |
1193 it->second = data; | 1200 it->second = data; |
1194 } | 1201 } |
1195 } | 1202 } |
OLD | NEW |