Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(710)

Side by Side Diff: chrome/browser/download/download_manager.cc

Issue 6932046: Added DownloadProcessHandle class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: download_process_handle Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
706 DownloadCancelledInternal(download_id, 706 DownloadCancelledInternal(download_id,
707 download->render_process_id(), 707 download->render_process_id(),
708 download->request_id()); 708 download->request_id());
709 } 709 }
710 710
711 void DownloadManager::DownloadCancelledInternal(int download_id, 711 void DownloadManager::DownloadCancelledInternal(int download_id,
712 int render_process_id, 712 int render_process_id,
713 int request_id) { 713 int request_id) {
714 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 714 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
715 // 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.
716 BrowserThread::PostTask( 716 DownloadProcessHandle process_handle(-1, render_process_id, request_id);
717 BrowserThread::IO, FROM_HERE, 717 process_handle.CancelDownload(
718 NewRunnableFunction(&download_util::CancelDownloadRequest, 718 g_browser_process->resource_dispatcher_host());
719 g_browser_process->resource_dispatcher_host(),
720 render_process_id,
721 request_id));
722 719
723 BrowserThread::PostTask( 720 BrowserThread::PostTask(
724 BrowserThread::FILE, FROM_HERE, 721 BrowserThread::FILE, FROM_HERE,
725 NewRunnableMethod( 722 NewRunnableMethod(
726 file_manager_, &DownloadFileManager::CancelDownload, download_id)); 723 file_manager_, &DownloadFileManager::CancelDownload, download_id));
727 } 724 }
728 725
729 void DownloadManager::OnDownloadError(int32 download_id, 726 void DownloadManager::OnDownloadError(int32 download_id,
730 int64 size, 727 int64 size,
731 int os_error) { 728 int os_error) {
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after
976 973
977 info->path = path; 974 info->path = path;
978 AttachDownloadItem(info); 975 AttachDownloadItem(info);
979 } 976 }
980 977
981 void DownloadManager::FileSelectionCanceled(void* params) { 978 void DownloadManager::FileSelectionCanceled(void* params) {
982 // The user didn't pick a place to save the file, so need to cancel the 979 // 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. 980 // download that's already in progress to the temporary location.
984 DownloadCreateInfo* info = reinterpret_cast<DownloadCreateInfo*>(params); 981 DownloadCreateInfo* info = reinterpret_cast<DownloadCreateInfo*>(params);
985 DownloadCancelledInternal(info->download_id, 982 DownloadCancelledInternal(info->download_id,
986 info->child_id, 983 info->process_handle.child_id(),
Paweł Hajdan Jr. 2011/05/05 20:13:51 nit: Please convert DownloadCancelledInternal to t
ahendrickson 2011/05/06 16:16:50 Converted to use DownloadProcessHandle process_han
987 info->request_id); 984 info->process_handle.child_id());
988 } 985 }
989 986
990 void DownloadManager::DangerousDownloadValidated(DownloadItem* download) { 987 void DownloadManager::DangerousDownloadValidated(DownloadItem* download) {
991 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 988 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
992 DCHECK_EQ(DownloadItem::DANGEROUS, download->safety_state()); 989 DCHECK_EQ(DownloadItem::DANGEROUS, download->safety_state());
993 download->set_safety_state(DownloadItem::DANGEROUS_BUT_VALIDATED); 990 download->set_safety_state(DownloadItem::DANGEROUS_BUT_VALIDATED);
994 download->UpdateObservers(); 991 download->UpdateObservers();
995 992
996 MaybeCompleteDownload(download); 993 MaybeCompleteDownload(download);
997 } 994 }
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1037 db_handle = download_history_->GetNextFakeDbHandle(); 1034 db_handle = download_history_->GetNextFakeDbHandle();
1038 1035
1039 DCHECK(download->db_handle() == DownloadHistory::kUninitializedHandle); 1036 DCHECK(download->db_handle() == DownloadHistory::kUninitializedHandle);
1040 download->set_db_handle(db_handle); 1037 download->set_db_handle(db_handle);
1041 1038
1042 DCHECK(!ContainsKey(history_downloads_, download->db_handle())); 1039 DCHECK(!ContainsKey(history_downloads_, download->db_handle()));
1043 history_downloads_[download->db_handle()] = download; 1040 history_downloads_[download->db_handle()] = download;
1044 1041
1045 // Show in the appropriate browser UI. 1042 // Show in the appropriate browser UI.
1046 // This includes buttons to save or cancel, for a dangerous download. 1043 // This includes buttons to save or cancel, for a dangerous download.
1047 ShowDownloadInBrowser(info, download); 1044 ShowDownloadInBrowser(info.process_handle, download);
1048 1045
1049 // Inform interested objects about the new download. 1046 // Inform interested objects about the new download.
1050 NotifyModelChanged(); 1047 NotifyModelChanged();
1051 1048
1052 // If the download is still in progress, try to complete it. 1049 // If the download is still in progress, try to complete it.
1053 // 1050 //
1054 // Otherwise, download has been cancelled or interrupted before we've 1051 // Otherwise, download has been cancelled or interrupted before we've
1055 // received the DB handle. We post one final message to the history 1052 // 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 1053 // 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 1054 // completion status, and also inform any observers so that they get
1058 // more than just the start notification. 1055 // more than just the start notification.
1059 if (download->IsInProgress()) { 1056 if (download->IsInProgress()) {
1060 MaybeCompleteDownload(download); 1057 MaybeCompleteDownload(download);
1061 } else { 1058 } else {
1062 DCHECK(download->IsCancelled()) 1059 DCHECK(download->IsCancelled())
1063 << " download = " << download->DebugString(true); 1060 << " download = " << download->DebugString(true);
1064 in_progress_.erase(it); 1061 in_progress_.erase(it);
1065 active_downloads_.erase(info.download_id); 1062 active_downloads_.erase(info.download_id);
1066 download_history_->UpdateEntry(download); 1063 download_history_->UpdateEntry(download);
1067 download->UpdateObservers(); 1064 download->UpdateObservers();
1068 } 1065 }
1069 } 1066 }
1070 1067
1071 void DownloadManager::ShowDownloadInBrowser(const DownloadCreateInfo& info, 1068 void DownloadManager::ShowDownloadInBrowser(
1072 DownloadItem* download) { 1069 DownloadProcessHandle process_handle, DownloadItem* download) {
1073 // 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
1074 // 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
1075 // to display its download shelf. 1072 // to display its download shelf.
1076 TabContents* contents = tab_util::GetTabContentsByID(info.child_id, 1073 TabContents* contents = process_handle.GetTabContents();
1077 info.render_view_id);
1078 1074
1079 // 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
1080 // 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
1081 // the user. 1077 // the user.
1082 if (!contents) { 1078 if (!contents) {
1083 Browser* last_active = BrowserList::GetLastActive(); 1079 Browser* last_active = BrowserList::GetLastActive();
1084 if (last_active) 1080 if (last_active)
1085 contents = last_active->GetSelectedTabContents(); 1081 contents = last_active->GetSelectedTabContents();
1086 } 1082 }
1087 1083
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
1174 observed_download_manager_->RemoveObserver(this); 1170 observed_download_manager_->RemoveObserver(this);
1175 } 1171 }
1176 1172
1177 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { 1173 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() {
1178 observing_download_manager_->NotifyModelChanged(); 1174 observing_download_manager_->NotifyModelChanged();
1179 } 1175 }
1180 1176
1181 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { 1177 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() {
1182 observed_download_manager_ = NULL; 1178 observed_download_manager_ = NULL;
1183 } 1179 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698