Chromium Code Reviews| Index: chrome/browser/chromeos/drive/download_handler.cc |
| diff --git a/chrome/browser/chromeos/drive/download_handler.cc b/chrome/browser/chromeos/drive/download_handler.cc |
| index f9032531549f1fca2cdec78b8c15309a8bdb58de..7cbb879ba3256d47184ff0c5858aff673a4c87d1 100644 |
| --- a/chrome/browser/chromeos/drive/download_handler.cc |
| +++ b/chrome/browser/chromeos/drive/download_handler.cc |
| @@ -141,11 +141,19 @@ void DownloadHandler::Initialize( |
| download_manager->GetAllDownloads(&downloads); |
| for (size_t i = 0; i < downloads.size(); ++i) { |
| if (IsPersistedDriveDownload(drive_tmp_download_path_, downloads[i])) |
| - RemoveDownload(downloads[i]->GetId()); |
| + downloads[i]->Remove(); |
| } |
| } |
| } |
| +void DownloadHandler::ObserveIncognitoDownloadManager( |
| + DownloadManager* download_manager) { |
| + if (!notifier_incognito_) { |
|
hashimoto
2014/06/03 06:10:32
nit: DCHECK(!notifier_incognito_) is enough?
kinaba
2014/06/03 06:50:32
Done.
|
| + notifier_incognito_.reset(new AllDownloadItemNotifier(download_manager, |
| + this)); |
| + } |
| +} |
| + |
| void DownloadHandler::SubstituteDriveDownloadPath( |
| const base::FilePath& drive_path, |
| content::DownloadItem* download, |
| @@ -223,12 +231,21 @@ void DownloadHandler::OnDownloadCreated(DownloadManager* manager, |
| FROM_HERE, |
| base::Bind(&DownloadHandler::RemoveDownload, |
| weak_ptr_factory_.GetWeakPtr(), |
| + static_cast<void*>(manager), |
| download->GetId())); |
| } |
| } |
| -void DownloadHandler::RemoveDownload(int id) { |
| - DownloadManager* manager = notifier_->GetManager(); |
| +void DownloadHandler::RemoveDownload(void* manager_id, int id) { |
| + // During the asynchronous task posting, the original download manager may |
| + // have gone. To verify the validity, we compare with managers held by |
| + // notifiers which are ensured to be alive. |
| + DownloadManager* manager = NULL; |
| + if (manager_id == notifier_->GetManager()) |
| + manager = notifier_->GetManager(); |
| + else if (notifier_incognito_ && manager == notifier_incognito_->GetManager()) |
| + manager = notifier_incognito_->GetManager(); |
| + |
| if (!manager) |
| return; |
| DownloadItem* download = manager->GetDownload(id); |
| @@ -253,7 +270,7 @@ void DownloadHandler::OnDownloadUpdated( |
| break; |
| case DownloadItem::COMPLETE: |
| - UploadDownloadItem(download); |
| + UploadDownloadItem(manager, download); |
| data->set_complete(); |
| break; |
| @@ -289,7 +306,8 @@ void DownloadHandler::OnCreateDirectory( |
| } |
| } |
| -void DownloadHandler::UploadDownloadItem(DownloadItem* download) { |
| +void DownloadHandler::UploadDownloadItem(DownloadManager* manager, |
| + DownloadItem* download) { |
| DCHECK_EQ(DownloadItem::COMPLETE, download->GetState()); |
| base::FilePath* cache_file_path = new base::FilePath; |
| WriteOnCacheFileAndReply( |
| @@ -300,16 +318,24 @@ void DownloadHandler::UploadDownloadItem(DownloadItem* download) { |
| cache_file_path), |
| base::Bind(&DownloadHandler::SetCacheFilePath, |
| weak_ptr_factory_.GetWeakPtr(), |
| + static_cast<void*>(manager), |
| download->GetId(), |
| base::Owned(cache_file_path))); |
| } |
| -void DownloadHandler::SetCacheFilePath(int id, |
| +void DownloadHandler::SetCacheFilePath(void* manager_id, |
| + int id, |
| const base::FilePath* cache_file_path, |
| FileError error) { |
| + // Validate the download manager. See the comment in RemoveDownload(). |
| + DownloadManager* manager = NULL; |
| + if (manager_id == notifier_->GetManager()) |
| + manager = notifier_->GetManager(); |
| + else if (notifier_incognito_ && manager == notifier_incognito_->GetManager()) |
| + manager = notifier_incognito_->GetManager(); |
|
hashimoto
2014/06/03 06:10:32
nit: How about adding a private method which maps
kinaba
2014/06/03 06:50:32
Done.
|
| + |
| if (error != FILE_ERROR_OK) |
| return; |
| - DownloadManager* manager = notifier_->GetManager(); |
| if (!manager) |
| return; |
| DownloadItem* download = manager->GetDownload(id); |