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

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

Issue 7112011: Change DownloadProcessHandle to be more of an encapsulated class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merged to LKGR. Created 9 years, 6 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/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_create_info.h" 20 #include "chrome/browser/download/download_create_info.h"
21 #include "chrome/browser/download/download_extensions.h" 21 #include "chrome/browser/download/download_extensions.h"
22 #include "chrome/browser/download/download_file_manager.h" 22 #include "chrome/browser/download/download_file_manager.h"
23 #include "chrome/browser/download/download_history.h" 23 #include "chrome/browser/download/download_history.h"
24 #include "chrome/browser/download/download_item.h" 24 #include "chrome/browser/download/download_item.h"
25 #include "chrome/browser/download/download_prefs.h" 25 #include "chrome/browser/download/download_prefs.h"
26 #include "chrome/browser/download/download_process_handle.h" 26 #include "chrome/browser/download/download_request_handle.h"
27 #include "chrome/browser/download/download_safe_browsing_client.h" 27 #include "chrome/browser/download/download_safe_browsing_client.h"
28 #include "chrome/browser/download/download_status_updater.h" 28 #include "chrome/browser/download/download_status_updater.h"
29 #include "chrome/browser/download/download_util.h" 29 #include "chrome/browser/download/download_util.h"
30 #include "chrome/browser/extensions/extension_service.h" 30 #include "chrome/browser/extensions/extension_service.h"
31 #include "chrome/browser/history/download_history_info.h" 31 #include "chrome/browser/history/download_history_info.h"
32 #include "chrome/browser/platform_util.h" 32 #include "chrome/browser/platform_util.h"
33 #include "chrome/browser/profiles/profile.h" 33 #include "chrome/browser/profiles/profile.h"
34 #include "chrome/browser/tab_contents/tab_util.h" 34 #include "chrome/browser/tab_contents/tab_util.h"
35 #include "chrome/browser/ui/browser.h" 35 #include "chrome/browser/ui/browser.h"
36 #include "chrome/browser/ui/browser_list.h" 36 #include "chrome/browser/ui/browser_list.h"
(...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after
465 465
466 download->SetFileCheckResults(new_state); 466 download->SetFileCheckResults(new_state);
467 467
468 FilePath suggested_path = download->suggested_path(); 468 FilePath suggested_path = download->suggested_path();
469 469
470 if (download->save_as()) { 470 if (download->save_as()) {
471 // We must ask the user for the place to put the download. 471 // We must ask the user for the place to put the download.
472 if (!select_file_dialog_.get()) 472 if (!select_file_dialog_.get())
473 select_file_dialog_ = SelectFileDialog::Create(this); 473 select_file_dialog_ = SelectFileDialog::Create(this);
474 474
475 DownloadProcessHandle process_handle = download->process_handle(); 475 DownloadRequestHandle request_handle = download->request_handle();
476 TabContents* contents = process_handle.GetTabContents(); 476 TabContents* contents = request_handle.GetTabContents();
477 SelectFileDialog::FileTypeInfo file_type_info; 477 SelectFileDialog::FileTypeInfo file_type_info;
478 FilePath::StringType extension = suggested_path.Extension(); 478 FilePath::StringType extension = suggested_path.Extension();
479 if (!extension.empty()) { 479 if (!extension.empty()) {
480 extension.erase(extension.begin()); // drop the . 480 extension.erase(extension.begin()); // drop the .
481 file_type_info.extensions.resize(1); 481 file_type_info.extensions.resize(1);
482 file_type_info.extensions[0].push_back(extension); 482 file_type_info.extensions[0].push_back(extension);
483 } 483 }
484 file_type_info.include_all_files = true; 484 file_type_info.include_all_files = true;
485 gfx::NativeWindow owning_window = 485 gfx::NativeWindow owning_window =
486 contents ? platform_util::GetTopLevel(contents->GetNativeView()) : NULL; 486 contents ? platform_util::GetTopLevel(contents->GetNativeView()) : NULL;
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
742 742
743 // Clean up will happen when the history system create callback runs if we 743 // Clean up will happen when the history system create callback runs if we
744 // don't have a valid db_handle yet. 744 // don't have a valid db_handle yet.
745 if (download->db_handle() != DownloadHistory::kUninitializedHandle) { 745 if (download->db_handle() != DownloadHistory::kUninitializedHandle) {
746 in_progress_.erase(it); 746 in_progress_.erase(it);
747 active_downloads_.erase(download_id); 747 active_downloads_.erase(download_id);
748 UpdateAppIcon(); // Reflect removal from in_progress_. 748 UpdateAppIcon(); // Reflect removal from in_progress_.
749 download_history_->UpdateEntry(download); 749 download_history_->UpdateEntry(download);
750 } 750 }
751 751
752 DownloadCancelledInternal(download_id, download->process_handle()); 752 DownloadCancelledInternal(download_id, download->request_handle());
753 } 753 }
754 754
755 void DownloadManager::DownloadCancelledInternal( 755 void DownloadManager::DownloadCancelledInternal(
756 int download_id, DownloadProcessHandle process_handle) { 756 int download_id, DownloadRequestHandle request_handle) {
757 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 757 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
758 // Cancel the network request. RDH is guaranteed to outlive the IO thread. 758 request_handle.CancelRequest();
759 download_util::CancelDownloadRequest(
760 g_browser_process->resource_dispatcher_host(), process_handle);
761 759
762 BrowserThread::PostTask( 760 BrowserThread::PostTask(
763 BrowserThread::FILE, FROM_HERE, 761 BrowserThread::FILE, FROM_HERE,
764 NewRunnableMethod( 762 NewRunnableMethod(
765 file_manager_, &DownloadFileManager::CancelDownload, download_id)); 763 file_manager_, &DownloadFileManager::CancelDownload, download_id));
766 } 764 }
767 765
768 void DownloadManager::OnDownloadError(int32 download_id, 766 void DownloadManager::OnDownloadError(int32 download_id,
769 int64 size, 767 int64 size,
770 int os_error) { 768 int os_error) {
(...skipping 23 matching lines...) Expand all
794 UpdateAppIcon(); // Reflect removal from in_progress_. 792 UpdateAppIcon(); // Reflect removal from in_progress_.
795 download_history_->UpdateEntry(download); 793 download_history_->UpdateEntry(download);
796 } 794 }
797 795
798 BrowserThread::PostTask( 796 BrowserThread::PostTask(
799 BrowserThread::FILE, FROM_HERE, 797 BrowserThread::FILE, FROM_HERE,
800 NewRunnableMethod( 798 NewRunnableMethod(
801 file_manager_, &DownloadFileManager::CancelDownload, download_id)); 799 file_manager_, &DownloadFileManager::CancelDownload, download_id));
802 } 800 }
803 801
804 void DownloadManager::PauseDownload(int32 download_id, bool pause) {
805 DownloadMap::iterator it = in_progress_.find(download_id);
806 if (it == in_progress_.end())
807 return;
808
809 DownloadItem* download = it->second;
810 if (pause == download->is_paused())
811 return;
812
813 BrowserThread::PostTask(
814 BrowserThread::IO, FROM_HERE,
815 NewRunnableMethod(this,
816 &DownloadManager::PauseDownloadRequest,
817 g_browser_process->resource_dispatcher_host(),
818 download->process_handle(),
819 pause));
820 }
821
822 void DownloadManager::UpdateAppIcon() { 802 void DownloadManager::UpdateAppIcon() {
823 if (status_updater_) 803 if (status_updater_)
824 status_updater_->Update(); 804 status_updater_->Update();
825 } 805 }
826 806
827 void DownloadManager::PauseDownloadRequest(ResourceDispatcherHost* rdh,
828 DownloadProcessHandle process_handle,
829 bool pause) {
830 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
831 rdh->PauseRequest(process_handle.child_id(),
832 process_handle.request_id(),
833 pause);
834 }
835
836 void DownloadManager::RemoveDownload(int64 download_handle) { 807 void DownloadManager::RemoveDownload(int64 download_handle) {
837 DownloadMap::iterator it = history_downloads_.find(download_handle); 808 DownloadMap::iterator it = history_downloads_.find(download_handle);
838 if (it == history_downloads_.end()) 809 if (it == history_downloads_.end())
839 return; 810 return;
840 811
841 // Make history update. 812 // Make history update.
842 DownloadItem* download = it->second; 813 DownloadItem* download = it->second;
843 download_history_->RemoveEntry(download); 814 download_history_->RemoveEntry(download);
844 815
845 // Remove from our tables and delete. 816 // Remove from our tables and delete.
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
1040 int32 download_id = *id_ptr; 1011 int32 download_id = *id_ptr;
1041 delete id_ptr; 1012 delete id_ptr;
1042 1013
1043 DownloadItem* download = GetActiveDownloadItem(download_id); 1014 DownloadItem* download = GetActiveDownloadItem(download_id);
1044 if (!download) 1015 if (!download)
1045 return; 1016 return;
1046 1017
1047 VLOG(20) << __FUNCTION__ << "()" 1018 VLOG(20) << __FUNCTION__ << "()"
1048 << " download = " << download->DebugString(true); 1019 << " download = " << download->DebugString(true);
1049 1020
1050 DownloadCancelledInternal(download_id, download->process_handle()); 1021 DownloadCancelledInternal(download_id, download->request_handle());
1051 } 1022 }
1052 1023
1053 // TODO(phajdan.jr): This is apparently not being exercised in tests. 1024 // TODO(phajdan.jr): This is apparently not being exercised in tests.
1054 bool DownloadManager::IsDangerous(const DownloadItem& download, 1025 bool DownloadManager::IsDangerous(const DownloadItem& download,
1055 const DownloadStateInfo& state, 1026 const DownloadStateInfo& state,
1056 bool visited_referrer_before) { 1027 bool visited_referrer_before) {
1057 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1028 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1058 1029
1059 bool auto_open = ShouldOpenFileBasedOnExtension(state.suggested_path); 1030 bool auto_open = ShouldOpenFileBasedOnExtension(state.suggested_path);
1060 download_util::DownloadDangerLevel danger_level = 1031 download_util::DownloadDangerLevel danger_level =
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
1155 download_history_->UpdateEntry(download); 1126 download_history_->UpdateEntry(download);
1156 download->UpdateObservers(); 1127 download->UpdateObservers();
1157 } 1128 }
1158 } 1129 }
1159 1130
1160 void DownloadManager::ShowDownloadInBrowser(DownloadItem* download) { 1131 void DownloadManager::ShowDownloadInBrowser(DownloadItem* download) {
1161 1132
1162 // The 'contents' may no longer exist if the user closed the tab before we 1133 // The 'contents' may no longer exist if the user closed the tab before we
1163 // get this start completion event. If it does, tell the origin TabContents 1134 // get this start completion event. If it does, tell the origin TabContents
1164 // to display its download shelf. 1135 // to display its download shelf.
1165 DownloadProcessHandle process_handle = download->process_handle(); 1136 DownloadRequestHandle request_handle = download->request_handle();
1166 TabContents* contents = process_handle.GetTabContents(); 1137 TabContents* contents = request_handle.GetTabContents();
1167 TabContentsWrapper* wrapper = NULL; 1138 TabContentsWrapper* wrapper = NULL;
1168 if (contents) 1139 if (contents)
1169 wrapper = TabContentsWrapper::GetCurrentWrapperForContents(contents); 1140 wrapper = TabContentsWrapper::GetCurrentWrapperForContents(contents);
1170 1141
1171 // If the contents no longer exists, we start the download in the last active 1142 // If the contents no longer exists, we start the download in the last active
1172 // browser. This is not ideal but better than fully hiding the download from 1143 // browser. This is not ideal but better than fully hiding the download from
1173 // the user. 1144 // the user.
1174 if (!wrapper) { 1145 if (!wrapper) {
1175 Browser* last_active = BrowserList::GetLastActive(); 1146 Browser* last_active = BrowserList::GetLastActive();
1176 if (last_active) 1147 if (last_active)
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
1277 observed_download_manager_->RemoveObserver(this); 1248 observed_download_manager_->RemoveObserver(this);
1278 } 1249 }
1279 1250
1280 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { 1251 void DownloadManager::OtherDownloadManagerObserver::ModelChanged() {
1281 observing_download_manager_->NotifyModelChanged(); 1252 observing_download_manager_->NotifyModelChanged();
1282 } 1253 }
1283 1254
1284 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { 1255 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() {
1285 observed_download_manager_ = NULL; 1256 observed_download_manager_ = NULL;
1286 } 1257 }
OLDNEW
« no previous file with comments | « chrome/browser/download/download_manager.h ('k') | chrome/browser/download/download_process_handle.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698