diff options
author | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-02 21:44:37 +0000 |
---|---|---|
committer | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-02 21:44:37 +0000 |
commit | d83d03aa8eea8f6173e161f134816746f8384e47 (patch) | |
tree | 10208e844592117a7d65fc3557a49e9126997a27 /chrome/browser/download | |
parent | b3f541017baf3eaaf4fc52d392630f9f040bae5e (diff) | |
download | chromium_src-d83d03aa8eea8f6173e161f134816746f8384e47.zip chromium_src-d83d03aa8eea8f6173e161f134816746f8384e47.tar.gz chromium_src-d83d03aa8eea8f6173e161f134816746f8384e47.tar.bz2 |
Fourth patch in getting rid of caching MessageLoop pointers.
BUG=25354
Review URL: http://codereview.chromium.org/348037
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@30751 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/download')
-rw-r--r-- | chrome/browser/download/download_manager.cc | 94 | ||||
-rw-r--r-- | chrome/browser/download/download_manager.h | 8 | ||||
-rw-r--r-- | chrome/browser/download/save_file_manager.cc | 207 | ||||
-rw-r--r-- | chrome/browser/download/save_file_manager.h | 30 | ||||
-rw-r--r-- | chrome/browser/download/save_package.cc | 33 |
5 files changed, 147 insertions, 225 deletions
diff --git a/chrome/browser/download/download_manager.cc b/chrome/browser/download/download_manager.cc index 3ded94e..eef8b04 100644 --- a/chrome/browser/download/download_manager.cc +++ b/chrome/browser/download/download_manager.cc @@ -18,6 +18,7 @@ #include "base/timer.h" #include "chrome/browser/browser_list.h" #include "chrome/browser/browser_process.h" +#include "chrome/browser/chrome_thread.h" #include "chrome/browser/download/download_file.h" #include "chrome/browser/download/download_util.h" #include "chrome/browser/extensions/crx_installer.h" @@ -347,9 +348,7 @@ void DownloadManager::RegisterUserPrefs(PrefService* prefs) { DownloadManager::DownloadManager() : shutdown_needed_(false), profile_(NULL), - file_manager_(NULL), - ui_loop_(MessageLoop::current()), - file_loop_(NULL) { + file_manager_(NULL) { } DownloadManager::~DownloadManager() { @@ -494,12 +493,6 @@ bool DownloadManager::Init(Profile* profile) { return false; } - file_loop_ = g_browser_process->file_thread()->message_loop(); - if (!file_loop_) { - NOTREACHED(); - return false; - } - // Get our user preference state. PrefService* prefs = profile_->GetPrefs(); DCHECK(prefs); @@ -513,8 +506,9 @@ bool DownloadManager::Init(Profile* profile) { // get rid of |CreateDirectoryPtr|. bool (*CreateDirectoryPtr)(const FilePath&) = &file_util::CreateDirectory; // Ensure that the download directory specified in the preferences exists. - file_loop_->PostTask(FROM_HERE, NewRunnableFunction( - CreateDirectoryPtr, download_path())); + ChromeThread::PostTask( + ChromeThread::FILE, FROM_HERE, + NewRunnableFunction(CreateDirectoryPtr, download_path())); // We use this to determine possibly dangerous downloads. download_util::InitializeExeTypes(&exe_types_); @@ -551,7 +545,7 @@ void DownloadManager::QueryHistoryForDownloads() { // point. OnCreateDatabaseEntryComplete() handles that finalization of the the // download creation as a callback from the history thread. void DownloadManager::StartDownload(DownloadCreateInfo* info) { - DCHECK(MessageLoop::current() == ui_loop_); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); DCHECK(info); // Freeze the user's preference for showing a Save As dialog. We're going to @@ -590,10 +584,10 @@ void DownloadManager::StartDownload(DownloadCreateInfo* info) { // We need to move over to the download thread because we don't want to stat // the suggested path on the UI thread. - file_loop_->PostTask(FROM_HERE, - NewRunnableMethod(this, - &DownloadManager::CheckIfSuggestedPathExists, - info)); + ChromeThread::PostTask( + ChromeThread::FILE, FROM_HERE, + NewRunnableMethod( + this, &DownloadManager::CheckIfSuggestedPathExists, info)); } void DownloadManager::CheckIfSuggestedPathExists(DownloadCreateInfo* info) { @@ -647,14 +641,15 @@ void DownloadManager::CheckIfSuggestedPathExists(DownloadCreateInfo* info) { } // Now we return to the UI thread. - ui_loop_->PostTask(FROM_HERE, + ChromeThread::PostTask( + ChromeThread::UI, FROM_HERE, NewRunnableMethod(this, &DownloadManager::OnPathExistenceAvailable, info)); } void DownloadManager::OnPathExistenceAvailable(DownloadCreateInfo* info) { - DCHECK(MessageLoop::current() == ui_loop_); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); DCHECK(info); if (info->save_as) { @@ -713,12 +708,11 @@ void DownloadManager::ContinueStartDownload(DownloadCreateInfo* info, // Called before DownloadFinished in order to avoid a race condition where we // attempt to open a completed download before it has been renamed. - file_loop_->PostTask(FROM_HERE, - NewRunnableMethod(file_manager_, - &DownloadFileManager::OnFinalDownloadName, - download->id(), - target_path, - this)); + ChromeThread::PostTask( + ChromeThread::FILE, FROM_HERE, + NewRunnableMethod( + file_manager_, &DownloadFileManager::OnFinalDownloadName, + download->id(), target_path, this)); // If the download already completed by the time we reached this point, then // notify observers that it did. @@ -836,7 +830,8 @@ void DownloadManager::DownloadFinished(int32 download_id, int64 size) { if (download->safety_state() == DownloadItem::DANGEROUS_BUT_VALIDATED) { // We first need to rename the downloaded file from its temporary name to // its final name before we can continue. - file_loop_->PostTask(FROM_HERE, + ChromeThread::PostTask( + ChromeThread::FILE, FROM_HERE, NewRunnableMethod( this, &DownloadManager::ProceedWithFinishedDangerousDownload, download->db_handle(), @@ -896,7 +891,8 @@ void DownloadManager::ProceedWithFinishedDangerousDownload( NOTREACHED(); } - ui_loop_->PostTask(FROM_HERE, + ChromeThread::PostTask( + ChromeThread::UI, FROM_HERE, NewRunnableMethod(this, &DownloadManager::DangerousDownloadRenamed, download_handle, success, new_path, uniquifier)); } @@ -964,10 +960,10 @@ void DownloadManager::DownloadCancelledInternal(int download_id, // Tell the file manager to cancel the download. file_manager_->RemoveDownload(download_id, this); // On the UI thread - file_loop_->PostTask(FROM_HERE, - NewRunnableMethod(file_manager_, - &DownloadFileManager::CancelDownload, - download_id)); + ChromeThread::PostTask( + ChromeThread::FILE, FROM_HERE, + NewRunnableMethod( + file_manager_, &DownloadFileManager::CancelDownload, download_id)); } void DownloadManager::PauseDownload(int32 download_id, bool pause) { @@ -1212,15 +1208,16 @@ void DownloadManager::RemoveObserver(Observer* observer) { // user interface. void DownloadManager::ShowDownloadInShell(const DownloadItem* download) { DCHECK(file_manager_); - DCHECK(MessageLoop::current() == ui_loop_); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); #if defined(OS_MACOSX) // Mac needs to run this operation on the UI thread. platform_util::ShowItemInFolder(download->full_path()); #else - file_loop_->PostTask(FROM_HERE, - NewRunnableMethod(file_manager_, - &DownloadFileManager::OnShowDownloadInShell, - FilePath(download->full_path()))); + ChromeThread::PostTask( + ChromeThread::FILE, FROM_HERE, + NewRunnableMethod( + file_manager_, &DownloadFileManager::OnShowDownloadInShell, + FilePath(download->full_path()))); #endif } @@ -1260,15 +1257,16 @@ void DownloadManager::OpenChromeExtension(const FilePath& full_path, void DownloadManager::OpenDownloadInShell(const DownloadItem* download, gfx::NativeView parent_window) { DCHECK(file_manager_); - DCHECK(MessageLoop::current() == ui_loop_); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); #if defined(OS_MACOSX) // Mac OS X requires opening downloads on the UI thread. platform_util::OpenItem(download->full_path()); #else - file_loop_->PostTask(FROM_HERE, - NewRunnableMethod(file_manager_, - &DownloadFileManager::OnOpenDownloadInShell, - download->full_path(), download->url(), parent_window)); + ChromeThread::PostTask( + ChromeThread::FILE, FROM_HERE, + NewRunnableMethod( + file_manager_, &DownloadFileManager::OnOpenDownloadInShell, + download->full_path(), download->url(), parent_window)); #endif } @@ -1407,8 +1405,9 @@ void DownloadManager::FileSelectionCanceled(void* params) { } void DownloadManager::DeleteDownload(const FilePath& path) { - file_loop_->PostTask(FROM_HERE, NewRunnableFunction( - &DownloadFileManager::DeleteFile, FilePath(path))); + ChromeThread::PostTask( + ChromeThread::FILE, FROM_HERE, + NewRunnableFunction(&DownloadFileManager::DeleteFile, FilePath(path))); } @@ -1422,11 +1421,12 @@ void DownloadManager::DangerousDownloadValidated(DownloadItem* download) { if (download->state() != DownloadItem::COMPLETE) return; - file_loop_->PostTask(FROM_HERE, - NewRunnableMethod(this, - &DownloadManager::ProceedWithFinishedDangerousDownload, - download->db_handle(), download->full_path(), - download->original_name())); + ChromeThread::PostTask( + ChromeThread::FILE, FROM_HERE, + NewRunnableMethod( + this, &DownloadManager::ProceedWithFinishedDangerousDownload, + download->db_handle(), download->full_path(), + download->original_name())); } void DownloadManager::GenerateSafeFilename(const std::string& mime_type, diff --git a/chrome/browser/download/download_manager.h b/chrome/browser/download/download_manager.h index 5f613a8..3974df6 100644 --- a/chrome/browser/download/download_manager.h +++ b/chrome/browser/download/download_manager.h @@ -59,7 +59,6 @@ class DownloadFileManager; class DownloadItemView; class DownloadManager; class GURL; -class MessageLoop; class PrefService; class Profile; class ResourceDispatcherHost; @@ -592,13 +591,6 @@ class DownloadManager : public base::RefCountedThreadSafe<DownloadManager>, // Non-owning pointer for handling file writing on the download_thread_. DownloadFileManager* file_manager_; - // A pointer to the main UI loop. - MessageLoop* ui_loop_; - - // A pointer to the file thread's loop. The file thread lives longer than - // the DownloadManager, so this is safe to cache. - MessageLoop* file_loop_; - // User preferences BooleanPrefMember prompt_for_download_; StringPrefMember download_path_; diff --git a/chrome/browser/download/save_file_manager.cc b/chrome/browser/download/save_file_manager.cc index 3289799..a985071 100644 --- a/chrome/browser/download/save_file_manager.cc +++ b/chrome/browser/download/save_file_manager.cc @@ -12,7 +12,7 @@ #include "base/string_util.h" #include "base/task.h" #include "base/thread.h" -#include "chrome/browser/browser_process.h" +#include "chrome/browser/chrome_thread.h" #include "chrome/browser/download/save_file.h" #include "chrome/browser/download/save_package.h" #include "chrome/browser/net/url_request_context_getter.h" @@ -27,25 +27,9 @@ #include "net/url_request/url_request_context.h" -SaveFileManager::SaveFileManager(MessageLoop* ui_loop, - MessageLoop* io_loop, - ResourceDispatcherHost* rdh) +SaveFileManager::SaveFileManager(ResourceDispatcherHost* rdh) : next_id_(0), - ui_loop_(ui_loop), - io_loop_(io_loop), resource_dispatcher_host_(rdh) { - DCHECK(ui_loop_); - // Need to make sure that we are in UI thread because using g_browser_process - // on a non-UI thread can cause crashes during shutdown. - DCHECK_EQ(ui_loop_, MessageLoop::current()); - // Cache the message loop of file thread. - base::Thread* thread = g_browser_process->file_thread(); - if (thread) - file_loop_ = thread->message_loop(); - else - // It could be NULL when it is created in unit test of - // ResourceDispatcherHost. - file_loop_ = NULL; DCHECK(resource_dispatcher_host_); } @@ -57,16 +41,14 @@ SaveFileManager::~SaveFileManager() { // Called during the browser shutdown process to clean up any state (open files, // timers) that live on the saving thread (file thread). void SaveFileManager::Shutdown() { - MessageLoop* loop = file_loop(); - if (loop) { - loop->PostTask(FROM_HERE, - NewRunnableMethod(this, &SaveFileManager::OnShutdown)); - } + ChromeThread::PostTask( + ChromeThread::FILE, FROM_HERE, + NewRunnableMethod(this, &SaveFileManager::OnShutdown)); } // Stop file thread operations. void SaveFileManager::OnShutdown() { - DCHECK_EQ(MessageLoop::current(), file_loop()); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); STLDeleteValues(&save_file_map_); } @@ -82,14 +64,14 @@ SaveFile* SaveFileManager::LookupSaveFile(int save_id) { // file a request from the file thread to the IO thread to generate a // unique save ID. int SaveFileManager::GetNextId() { - DCHECK_EQ(MessageLoop::current(), io_loop_); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); return next_id_++; } void SaveFileManager::RegisterStartingRequest(const GURL& save_url, SavePackage* save_package) { // Make sure it runs in the UI thread. - DCHECK_EQ(MessageLoop::current(), ui_loop_); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); int tab_id = save_package->tab_id(); // Register this starting request. @@ -102,7 +84,7 @@ void SaveFileManager::RegisterStartingRequest(const GURL& save_url, SavePackage* SaveFileManager::UnregisterStartingRequest( const GURL& save_url, int tab_id) { // Make sure it runs in UI thread. - DCHECK_EQ(MessageLoop::current(), ui_loop_); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); TabToStartingRequestsMap::iterator it = tab_starting_requests_.find(tab_id); if (it != tab_starting_requests_.end()) { @@ -123,24 +105,9 @@ SavePackage* SaveFileManager::UnregisterStartingRequest( return NULL; } -void SaveFileManager::RequireSaveJobFromOtherSource(SaveFileCreateInfo* info) { - // This function must be called on the UI thread, because the io_loop_ - // pointer may be junk when we use it on file thread. We can only rely on the - // io_loop_ pointer being valid when we run code on the UI thread (or on - // the IO thread. - DCHECK_EQ(MessageLoop::current(), ui_loop_); - DCHECK_EQ(info->save_id, -1); - // Since the data will come from render process, so we need to start - // this kind of save job by ourself. - io_loop_->PostTask(FROM_HERE, - NewRunnableMethod(this, - &SaveFileManager::OnRequireSaveJobFromOtherSource, - info)); -} - // Look up a SavePackage according to a save id. SavePackage* SaveFileManager::LookupPackage(int save_id) { - DCHECK_EQ(MessageLoop::current(), ui_loop_); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); SavePackageMap::iterator it = packages_.find(save_id); if (it != packages_.end()) return it->second; @@ -156,18 +123,15 @@ void SaveFileManager::SaveURL(const GURL& url, const FilePath& file_full_path, URLRequestContextGetter* request_context_getter, SavePackage* save_package) { - DCHECK_EQ(MessageLoop::current(), ui_loop_); - if (!io_loop_) { - NOTREACHED(); // Net IO thread must exist. - return; - } + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); // Register a saving job. RegisterStartingRequest(url, save_package); if (save_source == SaveFileCreateInfo::SAVE_FILE_FROM_NET) { DCHECK(url.is_valid()); - io_loop_->PostTask(FROM_HERE, + ChromeThread::PostTask( + ChromeThread::IO, FROM_HERE, NewRunnableMethod(this, &SaveFileManager::OnSaveURL, url, @@ -183,7 +147,13 @@ void SaveFileManager::SaveURL(const GURL& url, -1); info->render_process_id = render_process_host_id; info->render_view_id = render_view_id; - RequireSaveJobFromOtherSource(info); + + // Since the data will come from render process, so we need to start + // this kind of save job by ourself. + ChromeThread::PostTask( + ChromeThread::IO, FROM_HERE, + NewRunnableMethod( + this, &SaveFileManager::OnRequireSaveJobFromOtherSource, info)); } } @@ -196,7 +166,7 @@ void SaveFileManager::SaveURL(const GURL& url, void SaveFileManager::RemoveSaveFile(int save_id, const GURL& save_url, SavePackage* package) { DCHECK(package); - DCHECK_EQ(MessageLoop::current(), ui_loop_); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); // A save page job(SavePackage) can only have one manager, // so remove it if it exists. if (save_id == -1) { @@ -226,25 +196,19 @@ SavePackage* SaveFileManager::GetSavePackageFromRenderIds( // Utility function for deleting specified file. void SaveFileManager::DeleteDirectoryOrFile(const FilePath& full_path, bool is_dir) { - DCHECK_EQ(MessageLoop::current(), ui_loop_); - MessageLoop* loop = file_loop(); - DCHECK(loop); - loop->PostTask(FROM_HERE, - NewRunnableMethod(this, - &SaveFileManager::OnDeleteDirectoryOrFile, - full_path, - is_dir)); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); + ChromeThread::PostTask( + ChromeThread::FILE, FROM_HERE, + NewRunnableMethod( + this, &SaveFileManager::OnDeleteDirectoryOrFile, full_path, is_dir)); } void SaveFileManager::SendCancelRequest(int save_id) { // Cancel the request which has specific save id. DCHECK_GT(save_id, -1); - MessageLoop* loop = file_loop(); - DCHECK(loop); - loop->PostTask(FROM_HERE, - NewRunnableMethod(this, - &SaveFileManager::CancelSave, - save_id)); + ChromeThread::PostTask( + ChromeThread::FILE, FROM_HERE, + NewRunnableMethod(this, &SaveFileManager::CancelSave, save_id)); } // Notifications sent from the IO thread and run on the file thread: @@ -253,17 +217,16 @@ void SaveFileManager::SendCancelRequest(int save_id) { // to create a SaveFile which will hold and finally destroy |info|. It will // then passes |info| to the UI thread for reporting saving status. void SaveFileManager::StartSave(SaveFileCreateInfo* info) { - DCHECK_EQ(MessageLoop::current(), file_loop()); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); DCHECK(info); SaveFile* save_file = new SaveFile(info); DCHECK(!LookupSaveFile(info->save_id)); save_file_map_[info->save_id] = save_file; info->path = save_file->full_path(); - ui_loop_->PostTask(FROM_HERE, - NewRunnableMethod(this, - &SaveFileManager::OnStartSave, - info)); + ChromeThread::PostTask( + ChromeThread::UI, FROM_HERE, + NewRunnableMethod(this, &SaveFileManager::OnStartSave, info)); } // We do forward an update to the UI thread here, since we do not use timer to @@ -273,16 +236,15 @@ void SaveFileManager::StartSave(SaveFileCreateInfo* info) { void SaveFileManager::UpdateSaveProgress(int save_id, net::IOBuffer* data, int data_len) { - DCHECK_EQ(MessageLoop::current(), file_loop()); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); SaveFile* save_file = LookupSaveFile(save_id); if (save_file) { bool write_success = save_file->AppendDataToFile(data->data(), data_len); - ui_loop_->PostTask(FROM_HERE, - NewRunnableMethod(this, - &SaveFileManager::OnUpdateSaveProgress, - save_file->save_id(), - save_file->bytes_so_far(), - write_success)); + ChromeThread::PostTask( + ChromeThread::UI, FROM_HERE, + NewRunnableMethod( + this, &SaveFileManager::OnUpdateSaveProgress, save_file->save_id(), + save_file->bytes_so_far(), write_success)); } data->Release(); } @@ -297,33 +259,32 @@ void SaveFileManager::SaveFinished(int save_id, GURL save_url, int render_process_id, bool is_success) { - DCHECK_EQ(MessageLoop::current(), file_loop()); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); SaveFileMap::iterator it = save_file_map_.find(save_id); if (it != save_file_map_.end()) { SaveFile* save_file = it->second; - ui_loop_->PostTask(FROM_HERE, - NewRunnableMethod(this, - &SaveFileManager::OnSaveFinished, - save_id, - save_file->bytes_so_far(), - is_success)); + ChromeThread::PostTask( + ChromeThread::UI, FROM_HERE, + NewRunnableMethod( + this, &SaveFileManager::OnSaveFinished, save_id, + save_file->bytes_so_far(), is_success)); save_file->Finish(); } else if (save_id == -1) { // Before saving started, we got error. We still call finish process. DCHECK(!save_url.is_empty()); - ui_loop_->PostTask(FROM_HERE, - NewRunnableMethod(this, - &SaveFileManager::OnErrorFinished, - save_url, - render_process_id)); + ChromeThread::PostTask( + ChromeThread::UI, FROM_HERE, + NewRunnableMethod( + this, &SaveFileManager::OnErrorFinished, save_url, + render_process_id)); } } // Notifications sent from the file thread and run on the UI thread. void SaveFileManager::OnStartSave(const SaveFileCreateInfo* info) { - DCHECK_EQ(MessageLoop::current(), ui_loop_); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); SavePackage* save_package = GetSavePackageFromRenderIds(info->render_process_id, info->render_view_id); @@ -357,7 +318,7 @@ void SaveFileManager::OnStartSave(const SaveFileCreateInfo* info) { void SaveFileManager::OnUpdateSaveProgress(int save_id, int64 bytes_so_far, bool write_success) { - DCHECK_EQ(MessageLoop::current(), ui_loop_); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); SavePackage* package = LookupPackage(save_id); if (package) package->UpdateSaveProgress(save_id, bytes_so_far, write_success); @@ -368,30 +329,19 @@ void SaveFileManager::OnUpdateSaveProgress(int save_id, int64 bytes_so_far, void SaveFileManager::OnSaveFinished(int save_id, int64 bytes_so_far, bool is_success) { - DCHECK_EQ(MessageLoop::current(), ui_loop_); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); SavePackage* package = LookupPackage(save_id); if (package) package->SaveFinished(save_id, bytes_so_far, is_success); } void SaveFileManager::OnErrorFinished(GURL save_url, int tab_id) { - DCHECK_EQ(MessageLoop::current(), ui_loop_); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); SavePackage* save_package = UnregisterStartingRequest(save_url, tab_id); if (save_package) save_package->SaveFailed(save_url); } -void SaveFileManager::OnCancelSaveRequest(int render_process_id, - int request_id) { - DCHECK_EQ(MessageLoop::current(), ui_loop_); - DCHECK(io_loop_); - io_loop_->PostTask(FROM_HERE, - NewRunnableMethod(this, - &SaveFileManager::ExecuteCancelSaveRequest, - render_process_id, - request_id)); -} - // Notifications sent from the UI thread and run on the IO thread. void SaveFileManager::OnSaveURL( @@ -400,7 +350,7 @@ void SaveFileManager::OnSaveURL( int render_process_host_id, int render_view_id, URLRequestContextGetter* request_context_getter) { - DCHECK_EQ(MessageLoop::current(), io_loop_); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); URLRequestContext* context = request_context_getter->GetURLRequestContext(); resource_dispatcher_host_->BeginSaveFile(url, referrer, @@ -411,22 +361,19 @@ void SaveFileManager::OnSaveURL( void SaveFileManager::OnRequireSaveJobFromOtherSource( SaveFileCreateInfo* info) { - DCHECK_EQ(MessageLoop::current(), io_loop_); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); DCHECK_EQ(info->save_id, -1); // Generate a unique save id. info->save_id = GetNextId(); // Start real saving action. - MessageLoop* loop = file_loop(); - DCHECK(loop); - loop->PostTask(FROM_HERE, - NewRunnableMethod(this, - &SaveFileManager::StartSave, - info)); + ChromeThread::PostTask( + ChromeThread::FILE, FROM_HERE, + NewRunnableMethod(this, &SaveFileManager::StartSave, info)); } void SaveFileManager::ExecuteCancelSaveRequest(int render_process_id, int request_id) { - DCHECK_EQ(MessageLoop::current(), io_loop_); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); resource_dispatcher_host_->CancelRequest(render_process_id, request_id, false); @@ -440,7 +387,7 @@ void SaveFileManager::ExecuteCancelSaveRequest(int render_process_id, // sent from the UI thread, the saving job may have already completed and // won't exist in our map. void SaveFileManager::CancelSave(int save_id) { - DCHECK_EQ(MessageLoop::current(), file_loop()); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); SaveFileMap::iterator it = save_file_map_.find(save_id); if (it != save_file_map_.end()) { SaveFile* save_file = it->second; @@ -449,11 +396,11 @@ void SaveFileManager::CancelSave(int save_id) { // message to IO thread. If the data comes from other sources, just // ignore the cancel message. if (save_file->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_NET) { - ui_loop_->PostTask(FROM_HERE, - NewRunnableMethod(this, - &SaveFileManager::OnCancelSaveRequest, - save_file->render_process_id(), - save_file->request_id())); + ChromeThread::PostTask( + ChromeThread::IO, FROM_HERE, + NewRunnableMethod( + this, &SaveFileManager::ExecuteCancelSaveRequest, + save_file->render_process_id(), save_file->request_id())); // UI thread will notify the render process to stop sending data, // so in here, we need not to do anything, just close the save file. @@ -477,7 +424,7 @@ void SaveFileManager::CancelSave(int save_id) { void SaveFileManager::SaveLocalFile(const GURL& original_file_url, int save_id, int render_process_id) { - DCHECK_EQ(MessageLoop::current(), file_loop()); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); SaveFile* save_file = LookupSaveFile(save_id); if (!save_file) return; @@ -507,7 +454,7 @@ void SaveFileManager::SaveLocalFile(const GURL& original_file_url, void SaveFileManager::OnDeleteDirectoryOrFile(const FilePath& full_path, bool is_dir) { - DCHECK_EQ(MessageLoop::current(), file_loop()); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); DCHECK(!full_path.empty()); file_util::Delete(full_path, is_dir); @@ -517,7 +464,7 @@ void SaveFileManager::OnDeleteDirectoryOrFile(const FilePath& full_path, // We run on this thread to avoid blocking the UI with slow Shell operations. #if !defined(OS_MACOSX) void SaveFileManager::OnShowSavedFileInShell(const FilePath full_path) { - DCHECK_EQ(MessageLoop::current(), file_loop()); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); platform_util::ShowItemInFolder(full_path); } #endif @@ -527,7 +474,7 @@ void SaveFileManager::RenameAllFiles( const FilePath& resource_dir, int render_process_id, int render_view_id) { - DCHECK_EQ(MessageLoop::current(), file_loop()); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); if (!resource_dir.empty() && !file_util::PathExists(resource_dir)) file_util::CreateDirectory(resource_dir); @@ -544,16 +491,16 @@ void SaveFileManager::RenameAllFiles( } } - ui_loop_->PostTask(FROM_HERE, - NewRunnableMethod(this, - &SaveFileManager::OnFinishSavePageJob, - render_process_id, - render_view_id)); + ChromeThread::PostTask( + ChromeThread::UI, FROM_HERE, + NewRunnableMethod( + this, &SaveFileManager::OnFinishSavePageJob, render_process_id, + render_view_id)); } void SaveFileManager::OnFinishSavePageJob(int render_process_id, int render_view_id) { - DCHECK_EQ(MessageLoop::current(), ui_loop_); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); SavePackage* save_package = GetSavePackageFromRenderIds(render_process_id, render_view_id); @@ -566,7 +513,7 @@ void SaveFileManager::OnFinishSavePageJob(int render_process_id, void SaveFileManager::RemoveSavedFileFromFileMap( const SaveIDList& save_ids) { - DCHECK_EQ(MessageLoop::current(), file_loop()); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); for (SaveIDList::const_iterator i = save_ids.begin(); i != save_ids.end(); ++i) { diff --git a/chrome/browser/download/save_file_manager.h b/chrome/browser/download/save_file_manager.h index 5f3d564..a77f052 100644 --- a/chrome/browser/download/save_file_manager.h +++ b/chrome/browser/download/save_file_manager.h @@ -72,7 +72,6 @@ class IOBuffer; class GURL; class SaveFile; class SavePackage; -class MessageLoop; class ResourceDispatcherHost; class Task; class URLRequestContextGetter; @@ -80,9 +79,7 @@ class URLRequestContextGetter; class SaveFileManager : public base::RefCountedThreadSafe<SaveFileManager> { public: - SaveFileManager(MessageLoop* ui_loop, - MessageLoop* io_loop, - ResourceDispatcherHost* rdh); + SaveFileManager(ResourceDispatcherHost* rdh); ~SaveFileManager(); // Lifetime management. @@ -127,10 +124,6 @@ class SaveFileManager // Helper function for deleting specified file. void DeleteDirectoryOrFile(const FilePath& full_path, bool is_dir); - // For posting notifications from the UI and file threads. - MessageLoop* ui_loop() const { return ui_loop_; } - MessageLoop* file_loop() const { return file_loop_; } - // Runs on file thread to save a file by copying from file system when // original url is using file scheme. void SaveLocalFile(const GURL& original_file_url, @@ -154,12 +147,6 @@ class SaveFileManager // A cleanup helper that runs on the file thread. void OnShutdown(); - // The resource does not come from the network, but we still needs to call - // this function for getting unique save ID by calling - // OnRequireSaveJobFromOtherSource in the net IO thread and start saving - // operation. This function is called on the UI thread. - void RequireSaveJobFromOtherSource(SaveFileCreateInfo* info); - // Called only on UI thread to get the SavePackage for a tab's profile. static SavePackage* GetSavePackageFromRenderIds(int render_process_id, int review_view_id); @@ -199,10 +186,6 @@ class SaveFileManager // For those requests that do not have valid save id, use // map:(url, SavePackage) to find the request and remove it. void OnErrorFinished(GURL save_url, int tab_id); - // Handler for a notification sent to the UI thread. - // The user has requested a cancel in the UI thread, so send a cancel request - // to stop the network requests in net IO thread. - void OnCancelSaveRequest(int render_process_id, int request_id); // Notifies SavePackage that the whole page saving job is finished. void OnFinishSavePageJob(int render_process_id, int render_view_id); @@ -232,17 +215,6 @@ class SaveFileManager typedef base::hash_map<int, SaveFile*> SaveFileMap; SaveFileMap save_file_map_; - // Message loop that the SavePackages live on. - MessageLoop* ui_loop_; - - // We cache the IO loop, we will use it to request resources from network. - MessageLoop* io_loop_; - - // We cache the file loop, we will use it to do real file operation. - // We guarantee that we won't access them incorrectly during the shutdown - // process - MessageLoop* file_loop_; - ResourceDispatcherHost* resource_dispatcher_host_; // Tracks which SavePackage to send data to, called only on UI thread. diff --git a/chrome/browser/download/save_package.cc b/chrome/browser/download/save_package.cc index 20cb15b..15e4787 100644 --- a/chrome/browser/download/save_package.cc +++ b/chrome/browser/download/save_package.cc @@ -14,6 +14,7 @@ #include "base/task.h" #include "base/thread.h" #include "chrome/browser/browser_process.h" +#include "chrome/browser/chrome_thread.h" #include "chrome/browser/download/download_item_model.h" #include "chrome/browser/download/download_manager.h" #include "chrome/browser/download/download_shelf.h" @@ -478,7 +479,8 @@ void SavePackage::StartSave(const SaveFileCreateInfo* info) { // If the save source is from file system, inform SaveFileManager to copy // corresponding file to the file path which this SaveItem specifies. if (info->save_source == SaveFileCreateInfo::SAVE_FILE_FROM_FILE) { - file_manager_->file_loop()->PostTask(FROM_HERE, + ChromeThread::PostTask( + ChromeThread::FILE, FROM_HERE, NewRunnableMethod(file_manager_, &SaveFileManager::SaveLocalFile, save_item->url(), @@ -584,7 +586,8 @@ void SavePackage::Stop() { it != saved_failed_items_.end(); ++it) save_ids.push_back(it->second->save_id()); - file_manager_->file_loop()->PostTask(FROM_HERE, + ChromeThread::PostTask( + ChromeThread::FILE, FROM_HERE, NewRunnableMethod(file_manager_, &SaveFileManager::RemoveSavedFileFromFileMap, save_ids)); @@ -613,7 +616,8 @@ void SavePackage::CheckFinish() { final_names.push_back(std::make_pair(it->first, it->second->full_path())); - file_manager_->file_loop()->PostTask(FROM_HERE, + ChromeThread::PostTask( + ChromeThread::FILE, FROM_HERE, NewRunnableMethod(file_manager_, &SaveFileManager::RenameAllFiles, final_names, @@ -638,7 +642,8 @@ void SavePackage::Finish() { it != saved_failed_items_.end(); ++it) save_ids.push_back(it->second->save_id()); - file_manager_->file_loop()->PostTask(FROM_HERE, + ChromeThread::PostTask( + ChromeThread::FILE, FROM_HERE, NewRunnableMethod(file_manager_, &SaveFileManager::RemoveSavedFileFromFileMap, save_ids)); @@ -734,7 +739,8 @@ void SavePackage::SaveCanceled(SaveItem* save_item) { save_item->url(), this); if (save_item->save_id() != -1) - file_manager_->file_loop()->PostTask(FROM_HERE, + ChromeThread::PostTask( + ChromeThread::FILE, FROM_HERE, NewRunnableMethod(file_manager_, &SaveFileManager::CancelSave, save_item->save_id())); @@ -776,12 +782,13 @@ void SavePackage::SaveNextFile(bool process_all_remaining_items) { void SavePackage::ShowDownloadInShell() { DCHECK(file_manager_); DCHECK(finished_ && !canceled() && !saved_main_file_path_.empty()); - DCHECK(MessageLoop::current() == file_manager_->ui_loop()); + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); #if defined(OS_MACOSX) // Mac OS X requires opening downloads on the UI thread. platform_util::ShowItemInFolder(saved_main_file_path_); #else - file_manager_->file_loop()->PostTask(FROM_HERE, + ChromeThread::PostTask( + ChromeThread::FILE, FROM_HERE, NewRunnableMethod(file_manager_, &SaveFileManager::OnShowSavedFileInShell, saved_main_file_path_)); @@ -902,7 +909,8 @@ void SavePackage::OnReceivedSerializedHtmlData(const GURL& frame_url, if (flag == webkit_glue::DomSerializerDelegate::ALL_FRAMES_ARE_FINISHED) { for (SaveUrlItemMap::iterator it = in_progress_items_.begin(); it != in_progress_items_.end(); ++it) { - file_manager_->file_loop()->PostTask(FROM_HERE, + ChromeThread::PostTask( + ChromeThread::FILE, FROM_HERE, NewRunnableMethod(file_manager_, &SaveFileManager::SaveFinished, it->second->save_id(), @@ -926,7 +934,8 @@ void SavePackage::OnReceivedSerializedHtmlData(const GURL& frame_url, memcpy(new_data->data(), data.data(), data.size()); // Call write file functionality in file thread. - file_manager_->file_loop()->PostTask(FROM_HERE, + ChromeThread::PostTask( + ChromeThread::FILE, FROM_HERE, NewRunnableMethod(file_manager_, &SaveFileManager::UpdateSaveProgress, save_item->save_id(), @@ -936,7 +945,8 @@ void SavePackage::OnReceivedSerializedHtmlData(const GURL& frame_url, // Current frame is completed saving, call finish in file thread. if (flag == webkit_glue::DomSerializerDelegate::CURRENT_FRAME_IS_FINISHED) { - file_manager_->file_loop()->PostTask(FROM_HERE, + ChromeThread::PostTask( + ChromeThread::FILE, FROM_HERE, NewRunnableMethod(file_manager_, &SaveFileManager::SaveFinished, save_item->save_id(), @@ -1081,7 +1091,8 @@ void SavePackage::GetSaveInfo() { FilePath save_dir = GetSaveDirPreference(tab_contents_->profile()->GetPrefs()); - file_manager_->file_loop()->PostTask(FROM_HERE, + ChromeThread::PostTask( + ChromeThread::FILE, FROM_HERE, new CreateDownloadDirectoryTask(save_dir, method_factory_.NewRunnableMethod( &SavePackage::ContinueGetSaveInfo, save_dir), |