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