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