Chromium Code Reviews| Index: chrome/browser/download/save_package.cc |
| =================================================================== |
| --- chrome/browser/download/save_package.cc (revision 91883) |
| +++ chrome/browser/download/save_package.cc (working copy) |
| @@ -21,6 +21,7 @@ |
| #include "chrome/browser/browser_process.h" |
| #include "chrome/browser/download/download_item.h" |
| #include "chrome/browser/download/download_item_model.h" |
| +#include "chrome/browser/download/download_history.h" |
| #include "chrome/browser/download/download_manager.h" |
| #include "chrome/browser/download/download_prefs.h" |
| #include "chrome/browser/download/download_util.h" |
| @@ -173,6 +174,7 @@ |
| wait_state_(INITIALIZE), |
| tab_id_(tab_contents()->GetRenderProcessHost()->id()), |
| unique_id_(g_save_package_id++), |
| + download_manager_(NULL), |
| ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
| DCHECK(page_url_.is_valid()); |
| DCHECK(save_type_ == SAVE_AS_ONLY_HTML || |
| @@ -199,6 +201,7 @@ |
| wait_state_(INITIALIZE), |
| tab_id_(tab_contents()->GetRenderProcessHost()->id()), |
| unique_id_(g_save_package_id++), |
| + download_manager_(NULL), |
| ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
| DCHECK(page_url_.is_valid()); |
| InternalInit(); |
| @@ -224,6 +227,7 @@ |
| wait_state_(INITIALIZE), |
| tab_id_(0), |
| unique_id_(g_save_package_id++), |
| + download_manager_(NULL), |
| ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
| } |
| @@ -314,19 +318,27 @@ |
| return false; |
| } |
| + // Get the download manager and add ourselves as an observer. |
| + download_manager_ = tab_contents()->profile()->GetDownloadManager(); |
| + if (!download_manager_) { |
| + NOTREACHED(); |
| + return false; |
| + } |
| + download_manager_->AddObserver(this); |
| + |
| // Create the fake DownloadItem and display the view. |
| - DownloadManager* download_manager = |
| - tab_contents()->profile()->GetDownloadManager(); |
| - download_ = new DownloadItem(download_manager, |
| + download_ = new DownloadItem(download_manager_, |
| saved_main_file_path_, |
| page_url_, |
| profile->IsOffTheRecord()); |
| // Transfer the ownership to the download manager. We need the DownloadItem |
| // to be alive as long as the Profile is alive. |
| - download_manager->SavePageAsDownloadStarted(download_); |
| + download_manager_->SavePageAsDownloadStarted(download_); |
| - wrapper_->download_tab_helper()->OnStartDownload(download_); |
| + // Add this entry to the history service, which also notifies the UI. |
|
Randy Smith (Not in Mondays)
2011/07/10 19:37:40
I think it's worthwhile making clear that the UI i
achuithb
2011/07/13 21:48:16
Done.
|
| + download_manager_->download_history()->AddEntry(download_, |
| + NewCallback(this, &SavePackage::OnDownloadEntryAdded)); |
|
Randy Smith (Not in Mondays)
2011/07/10 19:37:40
I'm not an expert on this, but I think that NewCal
achuithb
2011/07/13 21:48:16
I decided to move this code into download_manager.
|
| // Check save type and process the save page job. |
| if (save_type_ == SAVE_AS_COMPLETE_HTML) { |
| @@ -691,6 +703,7 @@ |
| // Inform the DownloadItem we have canceled whole save page job. |
| download_->Cancel(false); |
| + FinalizeDownloadEntry(); |
| } |
| void SavePackage::CheckFinish() { |
| @@ -745,6 +758,7 @@ |
| download_->OnAllDataSaved(all_save_items_count_); |
| download_->MarkAsComplete(); |
| + FinalizeDownloadEntry(); |
| NotificationService::current()->Notify( |
| NotificationType::SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
| @@ -1474,3 +1488,26 @@ |
| void SavePackage::FileSelectionCanceled(void* params) { |
| } |
| + |
| +void SavePackage::ManagerGoingDown() { |
| + download_manager_ = NULL; |
| +} |
| + |
| +void SavePackage::OnDownloadEntryAdded(int32 download_id, int64 db_handle) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + // download_manager_ can be null if Finalize got called before this callback. |
| + // In that case, this item would not get added to the history db. |
|
Randy Smith (Not in Mondays)
2011/07/10 19:37:40
Nope, it's been added to the DB, it just won't be
achuithb
2011/07/13 21:48:16
This code is now in DownloadManager.
|
| + DCHECK(download_manager_); |
|
Randy Smith (Not in Mondays)
2011/07/10 19:37:40
This is the one case in which I think download_man
achuithb
2011/07/13 21:48:16
This code is now in DownloadManager.
|
| + if (download_manager_) |
| + download_manager_->AddDownloadItemToHistory(download_, db_handle); |
| +} |
| + |
| +void SavePackage::FinalizeDownloadEntry() { |
| + DCHECK(download_manager_); |
| + if (download_manager_) { |
| + download_manager_->download_history()->UpdateEntry(download_); |
|
Randy Smith (Not in Mondays)
2011/07/10 19:37:40
So there's a pretty subtle but important differenc
Randy Smith (Not in Mondays)
2011/07/10 19:37:40
If you could also take a look at how hard it would
achuithb
2011/07/13 21:48:16
I'm now handling the race between completion and h
achuithb
2011/07/13 21:48:16
I think adding a test for the races should be doab
|
| + download_manager_->RemoveObserver(this); |
| + download_manager_ = NULL; |
|
Randy Smith (Not in Mondays)
2011/07/10 19:37:40
I'd like a CHECK() in the destructor that the down
achuithb
2011/07/13 21:48:16
Done.
|
| + } |
| +} |
| + |