Chromium Code Reviews| Index: content/browser/download/save_package.cc |
| =================================================================== |
| --- content/browser/download/save_package.cc (revision 93070) |
| +++ content/browser/download/save_package.cc (working copy) |
| @@ -19,6 +19,7 @@ |
| #include "base/threading/thread.h" |
| #include "base/utf_string_conversions.h" |
| #include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/download/download_history.h" |
| #include "chrome/browser/download/download_item.h" |
| #include "chrome/browser/download/download_item_model.h" |
| #include "chrome/browser/download/download_manager.h" |
| @@ -125,6 +126,7 @@ |
| const FilePath& directory_full_path) |
| : TabContentsObserver(tab_contents), |
| file_manager_(NULL), |
| + download_manager_(NULL), |
| download_(NULL), |
| page_url_(GetUrlToBeSaved()), |
| saved_main_file_path_(file_full_path), |
| @@ -152,6 +154,7 @@ |
| SavePackage::SavePackage(TabContents* tab_contents) |
| : TabContentsObserver(tab_contents), |
| file_manager_(NULL), |
| + download_manager_(NULL), |
| download_(NULL), |
| page_url_(GetUrlToBeSaved()), |
| title_(tab_contents->GetTitle()), |
| @@ -176,6 +179,7 @@ |
| const FilePath& directory_full_path) |
| : TabContentsObserver(tab_contents), |
| file_manager_(NULL), |
| + download_manager_(NULL), |
| download_(NULL), |
| saved_main_file_path_(file_full_path), |
| saved_main_directory_path_(directory_full_path), |
| @@ -197,6 +201,10 @@ |
| Cancel(true); |
| } |
| + // We should no longer be observing the DownloadManager at this point. |
| + CHECK(!download_manager_); |
| + CHECK(!download_); |
| + |
| DCHECK(all_save_items_count_ == (waiting_item_queue_.size() + |
| completed_count() + |
| in_process_count())); |
| @@ -212,9 +220,6 @@ |
| STLDeleteValues(&in_progress_items_); |
| STLDeleteValues(&saved_failed_items_); |
| - // The DownloadItem is owned by DownloadManager. |
| - download_ = NULL; |
| - |
| file_manager_ = NULL; |
| } |
| @@ -269,20 +274,23 @@ |
| return false; |
| } |
| - // Create the fake DownloadItem and display the view. |
| - DownloadManager* download_manager = |
| - tab_contents()->profile()->GetDownloadManager(); |
| - download_ = new DownloadItem(download_manager, |
| + // Get the download manager and add ourselves as an observer. |
| + download_manager_ = tab_contents()->profile()->GetDownloadManager(); |
| + if (!download_manager_) { |
| + NOTREACHED(); |
| + return false; |
| + } |
| + |
| + // Create the download item. |
| + 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_); |
| + // Transfer ownership to the download manager. |
| + download_manager_->SavePageDownloadStarted(download_); |
| + download_manager_->AddObserver(this); |
| - tab_contents()->OnStartDownload(download_); |
| - |
| // Check save type and process the save page job. |
| if (save_type_ == SAVE_AS_COMPLETE_HTML) { |
| // Get directory |
| @@ -643,7 +651,10 @@ |
| wait_state_ = FAILED; |
| // Inform the DownloadItem we have canceled whole save page job. |
| - download_->Cancel(false); |
| + if (download_) { |
| + download_->Cancel(false); |
| + FinalizeDownloadEntry(); |
| + } |
| } |
| void SavePackage::CheckFinish() { |
| @@ -696,8 +707,11 @@ |
| &SaveFileManager::RemoveSavedFileFromFileMap, |
| save_ids)); |
| - download_->OnAllDataSaved(all_save_items_count_); |
| - download_->MarkAsComplete(); |
| + if (download_) { |
| + download_->OnAllDataSaved(all_save_items_count_); |
| + download_->MarkAsComplete(); |
| + FinalizeDownloadEntry(); |
| + } |
| NotificationService::current()->Notify( |
| chrome::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
| @@ -722,7 +736,8 @@ |
| // Inform the DownloadItem to update UI. |
| // We use the received bytes as number of saved files. |
| - download_->Update(completed_count()); |
| + if (download_) |
| + download_->Update(completed_count()); |
| if (save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM && |
| save_item->url() == page_url_ && !save_item->received_bytes()) { |
| @@ -763,7 +778,8 @@ |
| // Inform the DownloadItem to update UI. |
| // We use the received bytes as number of saved files. |
| - download_->Update(completed_count()); |
| + if (download_) |
| + download_->Update(completed_count()); |
| if (save_type_ == SAVE_AS_ONLY_HTML || |
| save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM) { |
| @@ -1046,7 +1062,8 @@ |
| static_cast<int>(frames_list.size()); |
| // We use total bytes as the total number of files we want to save. |
| - download_->set_total_bytes(all_save_items_count_); |
| + if (download_) |
| + download_->set_total_bytes(all_save_items_count_); |
| if (all_save_items_count_) { |
| // Put all sub-resources to wait list. |
| @@ -1333,3 +1350,36 @@ |
| contents_mime_type == "text/css" || |
| net::IsSupportedJavascriptMimeType(contents_mime_type.c_str()); |
| } |
| + |
| +void SavePackage::StopObservation() { |
| + DCHECK(download_); |
| + DCHECK(download_manager_); |
| + if (download_manager_) |
| + download_manager_->RemoveObserver(this); |
| + download_ = NULL; |
| + download_manager_ = NULL; |
| +} |
| + |
| +void SavePackage::ModelChanged() { |
| + DCHECK(download_); |
| + DCHECK(download_manager_); |
| + |
| + // Check if our item has been deleted (after it was added). |
| + if (download_->db_handle() != DownloadHistory::kUninitializedHandle |
| + && download_manager_->GetDownloadItem(download_->id()) == NULL) |
|
Randy Smith (Not in Mondays)
2011/07/20 19:48:43
This is an ok way to do this, but the standard pat
achuithb
2011/07/20 21:39:00
That is much better. Thanks. Done.
|
| + StopObservation(); |
| +} |
| + |
| +void SavePackage::ManagerGoingDown() { |
| + // No instances of SavePackage should exist during profile cleanup, |
| + // which is when the manager shuts down. |
| + NOTREACHED(); |
| + StopObservation(); |
| +} |
| + |
| +void SavePackage::FinalizeDownloadEntry() { |
| + DCHECK(download_); |
| + DCHECK(download_manager_); |
| + download_manager_->SavePageDownloadFinished(download_); |
| + StopObservation(); |
| +} |