Commit 4a55c5ad authored by Ramin Halavati's avatar Ramin Halavati Committed by Commit Bot

Update DownloadManagerService to user separate coordinators per profile.

DownloadManagerService used a common coordinator for all OTR profiles.
It is updated to have separate coordinators per profile.

Bug: 1099577
Change-Id: I5532a0de7484670543a318335d4db07018bd170a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2297422Reviewed-by: default avatarXing Liu <xingliu@chromium.org>
Commit-Queue: Ramin Halavati <rhalavati@chromium.org>
Cr-Commit-Position: refs/heads/master@{#788543}
parent dfb8defe
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "base/metrics/field_trial_params.h" #include "base/metrics/field_trial_params.h"
#include "base/optional.h" #include "base/optional.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "base/time/default_clock.h" #include "base/time/default_clock.h"
...@@ -203,10 +204,7 @@ DownloadManagerService::DownloadActionParams::DownloadActionParams( ...@@ -203,10 +204,7 @@ DownloadManagerService::DownloadActionParams::DownloadActionParams(
: action(other.action), has_user_gesture(other.has_user_gesture) {} : action(other.action), has_user_gesture(other.has_user_gesture) {}
DownloadManagerService::DownloadManagerService() DownloadManagerService::DownloadManagerService()
: is_manager_initialized_(false), : is_manager_initialized_(false), is_pending_downloads_loaded_(false) {}
is_pending_downloads_loaded_(false),
original_coordinator_(nullptr),
off_the_record_coordinator_(nullptr) {}
DownloadManagerService::~DownloadManagerService() {} DownloadManagerService::~DownloadManagerService() {}
...@@ -454,10 +452,12 @@ void DownloadManagerService::OnDownloadsInitialized( ...@@ -454,10 +452,12 @@ void DownloadManagerService::OnDownloadsInitialized(
void DownloadManagerService::OnManagerGoingDown( void DownloadManagerService::OnManagerGoingDown(
download::SimpleDownloadManagerCoordinator* coordinator) { download::SimpleDownloadManagerCoordinator* coordinator) {
if (original_coordinator_ == coordinator) for (auto it = coordinators_.begin(); it != coordinators_.end(); it++) {
original_coordinator_ = nullptr; if (it->second == coordinator) {
else if (off_the_record_coordinator_ == coordinator) coordinators_.erase(it->first);
off_the_record_coordinator_ = nullptr; break;
}
}
} }
void DownloadManagerService::OnDownloadCreated( void DownloadManagerService::OnDownloadCreated(
...@@ -673,22 +673,21 @@ download::DownloadItem* DownloadManagerService::GetDownload( ...@@ -673,22 +673,21 @@ download::DownloadItem* DownloadManagerService::GetDownload(
void DownloadManagerService::OnPendingDownloadsLoaded() { void DownloadManagerService::OnPendingDownloadsLoaded() {
is_pending_downloads_loaded_ = true; is_pending_downloads_loaded_ = true;
ProfileKey* profile_key = use_existing_profile_key_for_testing_
? coordinators_.begin()->first
: ProfileManager::GetActiveUserProfile()
->GetOriginalProfile()
->GetProfileKey();
// Kick-off the auto-resumption handler. // Kick-off the auto-resumption handler.
content::DownloadManager::DownloadVector all_items; content::DownloadManager::DownloadVector all_items;
original_coordinator_->GetAllDownloads(&all_items); GetCoordinator(profile_key)->GetAllDownloads(&all_items);
if (!download::AutoResumptionHandler::Get()) if (!download::AutoResumptionHandler::Get())
CreateAutoResumptionHandler(); CreateAutoResumptionHandler();
download::AutoResumptionHandler::Get()->SetResumableDownloads(all_items); download::AutoResumptionHandler::Get()->SetResumableDownloads(all_items);
ProfileKey* profile_key =
use_startup_accessor_profile_key_for_testing_
? ProfileKeyStartupAccessor::GetInstance()->profile_key()
: ProfileManager::GetActiveUserProfile()
->GetOriginalProfile()
->GetProfileKey();
for (auto iter = pending_actions_.begin(); iter != pending_actions_.end(); for (auto iter = pending_actions_.begin(); iter != pending_actions_.end();
++iter) { ++iter) {
DownloadActionParams params = iter->second; DownloadActionParams params = iter->second;
...@@ -727,31 +726,25 @@ content::DownloadManager* DownloadManagerService::GetDownloadManager( ...@@ -727,31 +726,25 @@ content::DownloadManager* DownloadManagerService::GetDownloadManager(
void DownloadManagerService::ResetCoordinatorIfNeeded(ProfileKey* profile_key) { void DownloadManagerService::ResetCoordinatorIfNeeded(ProfileKey* profile_key) {
download::SimpleDownloadManagerCoordinator* coordinator = download::SimpleDownloadManagerCoordinator* coordinator =
SimpleDownloadManagerCoordinatorFactory::GetForKey(profile_key); SimpleDownloadManagerCoordinatorFactory::GetForKey(profile_key);
UpdateCoordinator(coordinator, profile_key->IsOffTheRecord()); UpdateCoordinator(coordinator, profile_key);
} }
void DownloadManagerService::UpdateCoordinator( void DownloadManagerService::UpdateCoordinator(
download::SimpleDownloadManagerCoordinator* new_coordinator, download::SimpleDownloadManagerCoordinator* new_coordinator,
bool is_off_the_record) { ProfileKey* profile_key) {
// TODO(https://crbug.com/1099577): Update to have separate coordinators per bool coordinator_exists = base::Contains(coordinators_, profile_key);
// OTR profile. if (!coordinator_exists || coordinators_[profile_key] != new_coordinator) {
auto*& coordinator = if (coordinator_exists)
is_off_the_record ? off_the_record_coordinator_ : original_coordinator_; coordinators_[profile_key]->GetNotifier()->RemoveObserver(this);
if (!coordinator || coordinator != new_coordinator) { coordinators_[profile_key] = new_coordinator;
if (coordinator) new_coordinator->GetNotifier()->AddObserver(this);
coordinator->GetNotifier()->RemoveObserver(this);
coordinator = new_coordinator;
coordinator->GetNotifier()->AddObserver(this);
} }
} }
download::SimpleDownloadManagerCoordinator* download::SimpleDownloadManagerCoordinator*
DownloadManagerService::GetCoordinator(ProfileKey* profile_key) { DownloadManagerService::GetCoordinator(ProfileKey* profile_key) {
// TODO(https://crbug.com/1099577): Update to have separate coordinators per DCHECK(base::Contains(coordinators_, profile_key));
// OTR profile. return coordinators_[profile_key];
bool use_original = use_startup_accessor_profile_key_for_testing_ ||
!profile_key->IsOffTheRecord();
return use_original ? original_coordinator_ : off_the_record_coordinator_;
} }
void DownloadManagerService::RenameDownload( void DownloadManagerService::RenameDownload(
...@@ -817,7 +810,7 @@ void DownloadManagerService::CreateInterruptedDownloadForTest( ...@@ -817,7 +810,7 @@ void DownloadManagerService::CreateInterruptedDownloadForTest(
download::InProgressDownloadManager* in_progress_manager = download::InProgressDownloadManager* in_progress_manager =
DownloadManagerUtils::GetInProgressDownloadManager( DownloadManagerUtils::GetInProgressDownloadManager(
ProfileKeyStartupAccessor::GetInstance()->profile_key()); ProfileKeyStartupAccessor::GetInstance()->profile_key());
UseStartupProfileKeyForTesting(); UseExistingProfileKeyForTesting();
std::vector<GURL> url_chain; std::vector<GURL> url_chain;
url_chain.emplace_back(ConvertJavaStringToUTF8(env, jurl)); url_chain.emplace_back(ConvertJavaStringToUTF8(env, jurl));
base::FilePath target_path(ConvertJavaStringToUTF8(env, jtarget_path)); base::FilePath target_path(ConvertJavaStringToUTF8(env, jtarget_path));
......
...@@ -201,8 +201,8 @@ class DownloadManagerService ...@@ -201,8 +201,8 @@ class DownloadManagerService
const JavaParamRef<jstring>& jdownload_guid, const JavaParamRef<jstring>& jdownload_guid,
jboolean download_started); jboolean download_started);
void UseStartupProfileKeyForTesting() { void UseExistingProfileKeyForTesting() {
use_startup_accessor_profile_key_for_testing_ = true; use_existing_profile_key_for_testing_ = true;
} }
private: private:
...@@ -255,7 +255,7 @@ class DownloadManagerService ...@@ -255,7 +255,7 @@ class DownloadManagerService
// profile type. // profile type.
void UpdateCoordinator( void UpdateCoordinator(
download::SimpleDownloadManagerCoordinator* coordinator, download::SimpleDownloadManagerCoordinator* coordinator,
bool is_off_the_record); ProfileKey* profile_key);
// Called to get the content::DownloadManager instance. // Called to get the content::DownloadManager instance.
content::DownloadManager* GetDownloadManager(ProfileKey* profile_key); content::DownloadManager* GetDownloadManager(ProfileKey* profile_key);
...@@ -298,10 +298,10 @@ class DownloadManagerService ...@@ -298,10 +298,10 @@ class DownloadManagerService
ScopedObserver<Profile, ProfileObserver> observed_profiles_{this}; ScopedObserver<Profile, ProfileObserver> observed_profiles_{this};
bool use_startup_accessor_profile_key_for_testing_{false}; bool use_existing_profile_key_for_testing_{false};
download::SimpleDownloadManagerCoordinator* original_coordinator_; std::map<ProfileKey*, download::SimpleDownloadManagerCoordinator*>
download::SimpleDownloadManagerCoordinator* off_the_record_coordinator_; coordinators_;
DISALLOW_COPY_AND_ASSIGN(DownloadManagerService); DISALLOW_COPY_AND_ASSIGN(DownloadManagerService);
}; };
......
...@@ -36,8 +36,8 @@ class DownloadManagerServiceTest : public testing::Test { ...@@ -36,8 +36,8 @@ class DownloadManagerServiceTest : public testing::Test {
.WillByDefault(::testing::Invoke( .WillByDefault(::testing::Invoke(
this, &DownloadManagerServiceTest::GetDownloadByGuid)); this, &DownloadManagerServiceTest::GetDownloadByGuid));
coordinator_.SetSimpleDownloadManager(&manager_, false); coordinator_.SetSimpleDownloadManager(&manager_, false);
service_->UpdateCoordinator(&coordinator_, false); service_->UpdateCoordinator(&coordinator_, profile_.GetProfileKey());
service_->UseStartupProfileKeyForTesting(); service_->UseExistingProfileKeyForTesting();
} }
void OnResumptionDone(bool success) { void OnResumptionDone(bool success) {
......
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