Commit 7c2a2894 authored by Min Qin's avatar Min Qin Committed by Commit Bot

Allow DownloadManagerService to create InProgressDownloadManager.

This CL allows DownloadManagerService to create InProgressDownloadManager
under cirtain circumstances.
The created InProgressDownloadManager can be used to resume download
without launching full browser process. And can be passed to the
DownloadManager when the latter is created.

BUG=695115

Change-Id: I050c1e5f5247cc1550e59117f7943a66a71656ce
Reviewed-on: https://chromium-review.googlesource.com/c/1182613Reviewed-by: default avatarXing Liu <xingliu@chromium.org>
Commit-Queue: Min Qin <qinmin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#596403}
parent 213fa288
......@@ -7,6 +7,7 @@
#include <memory>
#include "base/android/jni_string.h"
#include "base/android/path_utils.h"
#include "base/location.h"
#include "base/metrics/field_trial_params.h"
#include "base/single_thread_task_runner.h"
......@@ -20,7 +21,9 @@
#include "chrome/browser/download/download_core_service_factory.h"
#include "chrome/browser/download/offline_item_utils.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/chrome_constants.h"
#include "components/download/public/common/download_item.h"
#include "components/download/public/common/download_item_impl.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/download_item_utils.h"
#include "content/public/browser/notification_service.h"
......@@ -219,7 +222,10 @@ void DownloadManagerService::Observe(
download::InProgressDownloadManager*
DownloadManagerService::RetriveInProgressDownloadManager(
content::BrowserContext* context) {
// TODO(qinmin): return pre-created InProgressDownloadManager here.
if (in_progress_manager_) {
DCHECK(!context->IsOffTheRecord());
return in_progress_manager_.release();
}
return nullptr;
}
......@@ -472,10 +478,7 @@ void DownloadManagerService::ResumeDownloadInternal(
void DownloadManagerService::CancelDownloadInternal(
const std::string& download_guid, bool is_off_the_record) {
content::DownloadManager* manager = GetDownloadManager(is_off_the_record);
if (!manager)
return;
download::DownloadItem* item = manager->GetDownloadByGuid(download_guid);
download::DownloadItem* item = GetDownload(download_guid, is_off_the_record);
if (item) {
// Remove the observer first to avoid item->Cancel() causing re-entrance
// issue.
......@@ -486,10 +489,7 @@ void DownloadManagerService::CancelDownloadInternal(
void DownloadManagerService::PauseDownloadInternal(
const std::string& download_guid, bool is_off_the_record) {
content::DownloadManager* manager = GetDownloadManager(is_off_the_record);
if (!manager)
return;
download::DownloadItem* item = manager->GetDownloadByGuid(download_guid);
download::DownloadItem* item = GetDownload(download_guid, is_off_the_record);
if (item) {
item->Pause();
item->RemoveObserver(DownloadControllerBase::Get());
......@@ -498,10 +498,7 @@ void DownloadManagerService::PauseDownloadInternal(
void DownloadManagerService::RemoveDownloadInternal(
const std::string& download_guid, bool is_off_the_record) {
content::DownloadManager* manager = GetDownloadManager(is_off_the_record);
if (!manager)
return;
download::DownloadItem* item = manager->GetDownloadByGuid(download_guid);
download::DownloadItem* item = GetDownload(download_guid, is_off_the_record);
if (item)
item->Remove();
}
......@@ -555,6 +552,40 @@ void DownloadManagerService::OnResumptionFailedInternal(
resume_callback_for_testing_.Run(false);
}
download::DownloadItem* DownloadManagerService::GetDownload(
const std::string& download_guid,
bool is_off_the_record) {
if (in_progress_manager_) {
DCHECK(!is_off_the_record);
return in_progress_manager_->GetInProgressDownload(download_guid);
}
content::DownloadManager* manager = GetDownloadManager(is_off_the_record);
if (manager)
return manager->GetDownloadByGuid(download_guid);
return nullptr;
}
void DownloadManagerService::CreateInProgressDownloadManager() {
DCHECK(!in_progress_manager_);
base::FilePath data_dir;
base::android::GetDataDirectory(&data_dir);
in_progress_manager_ = std::make_unique<download::InProgressDownloadManager>(
nullptr, data_dir.Append(chrome::kInitialProfile),
download::InProgressDownloadManager::IsOriginSecureCallback());
// TODO(qinmin): construct the URLLoaderFactoryGetter and pass it to
// |in_progress_manager_|
in_progress_manager_->NotifyWhenInitialized(
base::BindOnce(&DownloadManagerService::OnInProgressManagerInitiailized,
base::Unretained(this)));
in_progress_manager_->set_download_start_observer(
DownloadControllerBase::Get());
}
void DownloadManagerService::OnInProgressManagerInitiailized() {
// TODO(qinmin): carry out all the pending actions to be performed.
}
content::DownloadManager* DownloadManagerService::GetDownloadManager(
bool is_off_the_record) {
Profile* profile = ProfileManager::GetActiveUserProfile();
......
......@@ -179,6 +179,17 @@ class DownloadManagerService
void OnResumptionFailedInternal(const std::string& download_guid);
// Gets a download item from DownloadManager or InProgressManager.
download::DownloadItem* GetDownload(const std::string& download_guid,
bool is_off_the_record);
// Creates the InProgressDownloadmanager when running with ServiceManager
// only mode.
void CreateInProgressDownloadManager();
// Called to when |in_progress_manager_| is initialized.
void OnInProgressManagerInitiailized();
typedef base::Callback<void(bool)> ResumeCallback;
void set_resume_callback_for_testing(const ResumeCallback& resume_cb) {
resume_callback_for_testing_ = resume_cb;
......@@ -217,6 +228,10 @@ class DownloadManagerService
std::unique_ptr<download::AllDownloadItemNotifier> original_notifier_;
std::unique_ptr<download::AllDownloadItemNotifier> off_the_record_notifier_;
// In-progress download manager when download is running as a service. Will
// pass this object to DownloadManagerImpl once it is created.
std::unique_ptr<download::InProgressDownloadManager> in_progress_manager_;
DISALLOW_COPY_AND_ASSIGN(DownloadManagerService);
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment