OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/download/download_manager.h" | 5 #include "chrome/browser/download/download_manager.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
11 #include "base/rand_util.h" | 11 #include "base/rand_util.h" |
12 #include "base/stl_util-inl.h" | 12 #include "base/stl_util-inl.h" |
13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
14 #include "base/sys_string_conversions.h" | 14 #include "base/sys_string_conversions.h" |
15 #include "base/task.h" | 15 #include "base/task.h" |
16 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
18 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
19 #include "chrome/browser/download/download_extensions.h" | 19 #include "chrome/browser/download/download_extensions.h" |
20 #include "chrome/browser/download/download_file_manager.h" | 20 #include "chrome/browser/download/download_file_manager.h" |
21 #include "chrome/browser/download/download_history.h" | 21 #include "chrome/browser/download/download_history.h" |
22 #include "chrome/browser/download/download_item.h" | 22 #include "chrome/browser/download/download_item.h" |
23 #include "chrome/browser/download/download_prefs.h" | 23 #include "chrome/browser/download/download_prefs.h" |
| 24 #include "chrome/browser/download/download_process_handle.h" |
24 #include "chrome/browser/download/download_safe_browsing_client.h" | 25 #include "chrome/browser/download/download_safe_browsing_client.h" |
25 #include "chrome/browser/download/download_status_updater.h" | 26 #include "chrome/browser/download/download_status_updater.h" |
26 #include "chrome/browser/download/download_util.h" | 27 #include "chrome/browser/download/download_util.h" |
27 #include "chrome/browser/extensions/extension_service.h" | 28 #include "chrome/browser/extensions/extension_service.h" |
28 #include "chrome/browser/history/download_create_info.h" | 29 #include "chrome/browser/history/download_create_info.h" |
29 #include "chrome/browser/net/chrome_url_request_context.h" | 30 #include "chrome/browser/net/chrome_url_request_context.h" |
30 #include "chrome/browser/platform_util.h" | 31 #include "chrome/browser/platform_util.h" |
31 #include "chrome/browser/profiles/profile.h" | 32 #include "chrome/browser/profiles/profile.h" |
32 #include "chrome/browser/tab_contents/tab_util.h" | 33 #include "chrome/browser/tab_contents/tab_util.h" |
33 #include "chrome/browser/ui/browser.h" | 34 #include "chrome/browser/ui/browser.h" |
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 void DownloadManager::OnPathExistenceAvailable(DownloadCreateInfo* info) { | 426 void DownloadManager::OnPathExistenceAvailable(DownloadCreateInfo* info) { |
426 VLOG(20) << __FUNCTION__ << "()" << " info = " << info->DebugString(); | 427 VLOG(20) << __FUNCTION__ << "()" << " info = " << info->DebugString(); |
427 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 428 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
428 DCHECK(info); | 429 DCHECK(info); |
429 | 430 |
430 if (info->prompt_user_for_save_location) { | 431 if (info->prompt_user_for_save_location) { |
431 // We must ask the user for the place to put the download. | 432 // We must ask the user for the place to put the download. |
432 if (!select_file_dialog_.get()) | 433 if (!select_file_dialog_.get()) |
433 select_file_dialog_ = SelectFileDialog::Create(this); | 434 select_file_dialog_ = SelectFileDialog::Create(this); |
434 | 435 |
435 TabContents* contents = tab_util::GetTabContentsByID(info->child_id, | 436 TabContents* contents = info->process_handle.GetTabContents(); |
436 info->render_view_id); | |
437 SelectFileDialog::FileTypeInfo file_type_info; | 437 SelectFileDialog::FileTypeInfo file_type_info; |
438 FilePath::StringType extension = info->suggested_path.Extension(); | 438 FilePath::StringType extension = info->suggested_path.Extension(); |
439 if (!extension.empty()) { | 439 if (!extension.empty()) { |
440 extension.erase(extension.begin()); // drop the . | 440 extension.erase(extension.begin()); // drop the . |
441 file_type_info.extensions.resize(1); | 441 file_type_info.extensions.resize(1); |
442 file_type_info.extensions[0].push_back(extension); | 442 file_type_info.extensions[0].push_back(extension); |
443 } | 443 } |
444 file_type_info.include_all_files = true; | 444 file_type_info.include_all_files = true; |
445 gfx::NativeWindow owning_window = | 445 gfx::NativeWindow owning_window = |
446 contents ? platform_util::GetTopLevel(contents->GetNativeView()) : NULL; | 446 contents ? platform_util::GetTopLevel(contents->GetNativeView()) : NULL; |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
696 | 696 |
697 // Clean up will happen when the history system create callback runs if we | 697 // Clean up will happen when the history system create callback runs if we |
698 // don't have a valid db_handle yet. | 698 // don't have a valid db_handle yet. |
699 if (download->db_handle() != DownloadHistory::kUninitializedHandle) { | 699 if (download->db_handle() != DownloadHistory::kUninitializedHandle) { |
700 in_progress_.erase(it); | 700 in_progress_.erase(it); |
701 active_downloads_.erase(download_id); | 701 active_downloads_.erase(download_id); |
702 UpdateAppIcon(); // Reflect removal from in_progress_. | 702 UpdateAppIcon(); // Reflect removal from in_progress_. |
703 download_history_->UpdateEntry(download); | 703 download_history_->UpdateEntry(download); |
704 } | 704 } |
705 | 705 |
706 DownloadCancelledInternal(download_id, | 706 DownloadCancelledInternal(download_id, download->process_handle()); |
707 download->render_process_id(), | |
708 download->request_id()); | |
709 } | 707 } |
710 | 708 |
711 void DownloadManager::DownloadCancelledInternal(int download_id, | 709 void DownloadManager::DownloadCancelledInternal( |
712 int render_process_id, | 710 int download_id, DownloadProcessHandle process_handle) { |
713 int request_id) { | |
714 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 711 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
715 // Cancel the network request. RDH is guaranteed to outlive the IO thread. | 712 // Cancel the network request. RDH is guaranteed to outlive the IO thread. |
716 BrowserThread::PostTask( | 713 download_util::CancelDownloadRequest( |
717 BrowserThread::IO, FROM_HERE, | 714 g_browser_process->resource_dispatcher_host(), process_handle); |
718 NewRunnableFunction(&download_util::CancelDownloadRequest, | |
719 g_browser_process->resource_dispatcher_host(), | |
720 render_process_id, | |
721 request_id)); | |
722 | 715 |
723 BrowserThread::PostTask( | 716 BrowserThread::PostTask( |
724 BrowserThread::FILE, FROM_HERE, | 717 BrowserThread::FILE, FROM_HERE, |
725 NewRunnableMethod( | 718 NewRunnableMethod( |
726 file_manager_, &DownloadFileManager::CancelDownload, download_id)); | 719 file_manager_, &DownloadFileManager::CancelDownload, download_id)); |
727 } | 720 } |
728 | 721 |
729 void DownloadManager::OnDownloadError(int32 download_id, | 722 void DownloadManager::OnDownloadError(int32 download_id, |
730 int64 size, | 723 int64 size, |
731 int os_error) { | 724 int os_error) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
769 | 762 |
770 DownloadItem* download = it->second; | 763 DownloadItem* download = it->second; |
771 if (pause == download->is_paused()) | 764 if (pause == download->is_paused()) |
772 return; | 765 return; |
773 | 766 |
774 BrowserThread::PostTask( | 767 BrowserThread::PostTask( |
775 BrowserThread::IO, FROM_HERE, | 768 BrowserThread::IO, FROM_HERE, |
776 NewRunnableMethod(this, | 769 NewRunnableMethod(this, |
777 &DownloadManager::PauseDownloadRequest, | 770 &DownloadManager::PauseDownloadRequest, |
778 g_browser_process->resource_dispatcher_host(), | 771 g_browser_process->resource_dispatcher_host(), |
779 download->render_process_id(), | 772 download->process_handle(), |
780 download->request_id(), | |
781 pause)); | 773 pause)); |
782 } | 774 } |
783 | 775 |
784 void DownloadManager::UpdateAppIcon() { | 776 void DownloadManager::UpdateAppIcon() { |
785 if (status_updater_) | 777 if (status_updater_) |
786 status_updater_->Update(); | 778 status_updater_->Update(); |
787 } | 779 } |
788 | 780 |
789 void DownloadManager::PauseDownloadRequest(ResourceDispatcherHost* rdh, | 781 void DownloadManager::PauseDownloadRequest(ResourceDispatcherHost* rdh, |
790 int render_process_id, | 782 DownloadProcessHandle process_handle, |
791 int request_id, | |
792 bool pause) { | 783 bool pause) { |
793 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 784 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
794 rdh->PauseRequest(render_process_id, request_id, pause); | 785 rdh->PauseRequest(process_handle.child_id(), |
| 786 process_handle.request_id(), |
| 787 pause); |
795 } | 788 } |
796 | 789 |
797 void DownloadManager::RemoveDownload(int64 download_handle) { | 790 void DownloadManager::RemoveDownload(int64 download_handle) { |
798 DownloadMap::iterator it = history_downloads_.find(download_handle); | 791 DownloadMap::iterator it = history_downloads_.find(download_handle); |
799 if (it == history_downloads_.end()) | 792 if (it == history_downloads_.end()) |
800 return; | 793 return; |
801 | 794 |
802 // Make history update. | 795 // Make history update. |
803 DownloadItem* download = it->second; | 796 DownloadItem* download = it->second; |
804 download_history_->RemoveEntry(download); | 797 download_history_->RemoveEntry(download); |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
975 last_download_path_ = path.DirName(); | 968 last_download_path_ = path.DirName(); |
976 | 969 |
977 info->path = path; | 970 info->path = path; |
978 AttachDownloadItem(info); | 971 AttachDownloadItem(info); |
979 } | 972 } |
980 | 973 |
981 void DownloadManager::FileSelectionCanceled(void* params) { | 974 void DownloadManager::FileSelectionCanceled(void* params) { |
982 // The user didn't pick a place to save the file, so need to cancel the | 975 // The user didn't pick a place to save the file, so need to cancel the |
983 // download that's already in progress to the temporary location. | 976 // download that's already in progress to the temporary location. |
984 DownloadCreateInfo* info = reinterpret_cast<DownloadCreateInfo*>(params); | 977 DownloadCreateInfo* info = reinterpret_cast<DownloadCreateInfo*>(params); |
985 DownloadCancelledInternal(info->download_id, | 978 DownloadCancelledInternal(info->download_id, info->process_handle); |
986 info->child_id, | |
987 info->request_id); | |
988 } | 979 } |
989 | 980 |
990 void DownloadManager::DangerousDownloadValidated(DownloadItem* download) { | 981 void DownloadManager::DangerousDownloadValidated(DownloadItem* download) { |
991 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 982 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
992 DCHECK_EQ(DownloadItem::DANGEROUS, download->safety_state()); | 983 DCHECK_EQ(DownloadItem::DANGEROUS, download->safety_state()); |
993 download->set_safety_state(DownloadItem::DANGEROUS_BUT_VALIDATED); | 984 download->set_safety_state(DownloadItem::DANGEROUS_BUT_VALIDATED); |
994 download->UpdateObservers(); | 985 download->UpdateObservers(); |
995 | 986 |
996 MaybeCompleteDownload(download); | 987 MaybeCompleteDownload(download); |
997 } | 988 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1037 db_handle = download_history_->GetNextFakeDbHandle(); | 1028 db_handle = download_history_->GetNextFakeDbHandle(); |
1038 | 1029 |
1039 DCHECK(download->db_handle() == DownloadHistory::kUninitializedHandle); | 1030 DCHECK(download->db_handle() == DownloadHistory::kUninitializedHandle); |
1040 download->set_db_handle(db_handle); | 1031 download->set_db_handle(db_handle); |
1041 | 1032 |
1042 DCHECK(!ContainsKey(history_downloads_, download->db_handle())); | 1033 DCHECK(!ContainsKey(history_downloads_, download->db_handle())); |
1043 history_downloads_[download->db_handle()] = download; | 1034 history_downloads_[download->db_handle()] = download; |
1044 | 1035 |
1045 // Show in the appropriate browser UI. | 1036 // Show in the appropriate browser UI. |
1046 // This includes buttons to save or cancel, for a dangerous download. | 1037 // This includes buttons to save or cancel, for a dangerous download. |
1047 ShowDownloadInBrowser(info, download); | 1038 ShowDownloadInBrowser(info.process_handle, download); |
1048 | 1039 |
1049 // Inform interested objects about the new download. | 1040 // Inform interested objects about the new download. |
1050 NotifyModelChanged(); | 1041 NotifyModelChanged(); |
1051 | 1042 |
1052 // If the download is still in progress, try to complete it. | 1043 // If the download is still in progress, try to complete it. |
1053 // | 1044 // |
1054 // Otherwise, download has been cancelled or interrupted before we've | 1045 // Otherwise, download has been cancelled or interrupted before we've |
1055 // received the DB handle. We post one final message to the history | 1046 // received the DB handle. We post one final message to the history |
1056 // service so that it can be properly in sync with the DownloadItem's | 1047 // service so that it can be properly in sync with the DownloadItem's |
1057 // completion status, and also inform any observers so that they get | 1048 // completion status, and also inform any observers so that they get |
1058 // more than just the start notification. | 1049 // more than just the start notification. |
1059 if (download->IsInProgress()) { | 1050 if (download->IsInProgress()) { |
1060 MaybeCompleteDownload(download); | 1051 MaybeCompleteDownload(download); |
1061 } else { | 1052 } else { |
1062 DCHECK(download->IsCancelled()) | 1053 DCHECK(download->IsCancelled()) |
1063 << " download = " << download->DebugString(true); | 1054 << " download = " << download->DebugString(true); |
1064 in_progress_.erase(it); | 1055 in_progress_.erase(it); |
1065 active_downloads_.erase(info.download_id); | 1056 active_downloads_.erase(info.download_id); |
1066 download_history_->UpdateEntry(download); | 1057 download_history_->UpdateEntry(download); |
1067 download->UpdateObservers(); | 1058 download->UpdateObservers(); |
1068 } | 1059 } |
1069 } | 1060 } |
1070 | 1061 |
1071 void DownloadManager::ShowDownloadInBrowser(const DownloadCreateInfo& info, | 1062 void DownloadManager::ShowDownloadInBrowser( |
1072 DownloadItem* download) { | 1063 DownloadProcessHandle process_handle, DownloadItem* download) { |
1073 // The 'contents' may no longer exist if the user closed the tab before we | 1064 // The 'contents' may no longer exist if the user closed the tab before we |
1074 // get this start completion event. If it does, tell the origin TabContents | 1065 // get this start completion event. If it does, tell the origin TabContents |
1075 // to display its download shelf. | 1066 // to display its download shelf. |
1076 TabContents* contents = tab_util::GetTabContentsByID(info.child_id, | 1067 TabContents* contents = process_handle.GetTabContents(); |
1077 info.render_view_id); | |
1078 | 1068 |
1079 // If the contents no longer exists, we start the download in the last active | 1069 // If the contents no longer exists, we start the download in the last active |
1080 // browser. This is not ideal but better than fully hiding the download from | 1070 // browser. This is not ideal but better than fully hiding the download from |
1081 // the user. | 1071 // the user. |
1082 if (!contents) { | 1072 if (!contents) { |
1083 Browser* last_active = BrowserList::GetLastActive(); | 1073 Browser* last_active = BrowserList::GetLastActive(); |
1084 if (last_active) | 1074 if (last_active) |
1085 contents = last_active->GetSelectedTabContents(); | 1075 contents = last_active->GetSelectedTabContents(); |
1086 } | 1076 } |
1087 | 1077 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1174 observed_download_manager_->RemoveObserver(this); | 1164 observed_download_manager_->RemoveObserver(this); |
1175 } | 1165 } |
1176 | 1166 |
1177 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { | 1167 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { |
1178 observing_download_manager_->NotifyModelChanged(); | 1168 observing_download_manager_->NotifyModelChanged(); |
1179 } | 1169 } |
1180 | 1170 |
1181 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { | 1171 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { |
1182 observed_download_manager_ = NULL; | 1172 observed_download_manager_ = NULL; |
1183 } | 1173 } |
OLD | NEW |