Commit d0a70a57 authored by David Black's avatar David Black Committed by Commit Bot

Migrate from `BrowserContext` to `Profile`.

Previously the `HoldingSpaceKeyedService` was constructed with a
`BrowserContext` and internally converted to `Profile` as needed. Now,
we'll take a `Profile` at construction time and avoid any downstream
conversions.

This CL also addresses a race condition that could occur when
partitioning `HoldingSpaceItems` by existence. Previously, input order
was not guaranteed to match output order. Now they will which should
stabilize browsertests.

Bug: 1119496
Change-Id: I8e213eeed8f4b4169bc033130fcdebf473c1fd73
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2386383
Commit-Queue: David Black <dmblack@google.com>
Reviewed-by: default avatarAlex Newcomer <newcomer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#803367}
parent bf433292
...@@ -29,17 +29,16 @@ ProfileManager* GetProfileManager() { ...@@ -29,17 +29,16 @@ ProfileManager* GetProfileManager() {
} // namespace } // namespace
// TODO(dmblack): Add a delegate for downloads. // TODO(dmblack): Add a delegate for downloads.
HoldingSpaceKeyedService::HoldingSpaceKeyedService( HoldingSpaceKeyedService::HoldingSpaceKeyedService(Profile* profile,
content::BrowserContext* context, const AccountId& account_id)
const AccountId& account_id) : profile_(profile),
: browser_context_(context),
account_id_(account_id), account_id_(account_id),
holding_space_client_(Profile::FromBrowserContext(context)), holding_space_client_(profile),
thumbnail_loader_(Profile::FromBrowserContext(context)) { thumbnail_loader_(profile) {
// The associated profile may not be ready yet. If it is, we can immediately // The associated profile may not be ready yet. If it is, we can immediately
// proceed with profile dependent initialization. // proceed with profile dependent initialization.
ProfileManager* const profile_manager = GetProfileManager(); ProfileManager* const profile_manager = GetProfileManager();
if (profile_manager->IsValidProfile(Profile::FromBrowserContext(context))) { if (profile_manager->IsValidProfile(profile)) {
OnProfileReady(); OnProfileReady();
return; return;
} }
...@@ -87,8 +86,8 @@ std::vector<GURL> HoldingSpaceKeyedService::GetPinnedFiles() const { ...@@ -87,8 +86,8 @@ std::vector<GURL> HoldingSpaceKeyedService::GetPinnedFiles() const {
void HoldingSpaceKeyedService::AddScreenshot( void HoldingSpaceKeyedService::AddScreenshot(
const base::FilePath& screenshot_file, const base::FilePath& screenshot_file,
const gfx::ImageSkia& image) { const gfx::ImageSkia& image) {
GURL file_system_url = holding_space_util::ResolveFileSystemUrl( GURL file_system_url =
Profile::FromBrowserContext(browser_context_), screenshot_file); holding_space_util::ResolveFileSystemUrl(profile_, screenshot_file);
if (file_system_url.is_empty()) if (file_system_url.is_empty())
return; return;
...@@ -99,8 +98,8 @@ void HoldingSpaceKeyedService::AddScreenshot( ...@@ -99,8 +98,8 @@ void HoldingSpaceKeyedService::AddScreenshot(
void HoldingSpaceKeyedService::AddDownload( void HoldingSpaceKeyedService::AddDownload(
const base::FilePath& download_file) { const base::FilePath& download_file) {
GURL file_system_url = holding_space_util::ResolveFileSystemUrl( GURL file_system_url =
Profile::FromBrowserContext(browser_context_), download_file); holding_space_util::ResolveFileSystemUrl(profile_, download_file);
if (file_system_url.is_empty()) if (file_system_url.is_empty())
return; return;
...@@ -126,7 +125,7 @@ void HoldingSpaceKeyedService::Shutdown() { ...@@ -126,7 +125,7 @@ void HoldingSpaceKeyedService::Shutdown() {
} }
void HoldingSpaceKeyedService::OnProfileAdded(Profile* profile) { void HoldingSpaceKeyedService::OnProfileAdded(Profile* profile) {
if (profile == Profile::FromBrowserContext(browser_context_)) { if (profile == profile_) {
profile_manager_observer_.Remove(GetProfileManager()); profile_manager_observer_.Remove(GetProfileManager());
OnProfileReady(); OnProfileReady();
} }
...@@ -166,18 +165,16 @@ void HoldingSpaceKeyedService::RemoveDownloadManagerObservers() { ...@@ -166,18 +165,16 @@ void HoldingSpaceKeyedService::RemoveDownloadManagerObservers() {
} }
void HoldingSpaceKeyedService::OnProfileReady() { void HoldingSpaceKeyedService::OnProfileReady() {
Profile* profile = Profile::FromBrowserContext(browser_context_);
// The `HoldingSpaceFileSystemDelegate` monitors the file system for changes. // The `HoldingSpaceFileSystemDelegate` monitors the file system for changes.
delegates_.push_back(std::make_unique<HoldingSpaceFileSystemDelegate>( delegates_.push_back(std::make_unique<HoldingSpaceFileSystemDelegate>(
profile, &holding_space_model_, profile_, &holding_space_model_,
/*file_removed_callback=*/ /*file_removed_callback=*/
base::BindRepeating(&HoldingSpaceKeyedService::OnFileRemoved, base::BindRepeating(&HoldingSpaceKeyedService::OnFileRemoved,
weak_factory_.GetWeakPtr()))); weak_factory_.GetWeakPtr())));
// The `HoldingSpacePersistenceDelegate` manages holding space persistence. // The `HoldingSpacePersistenceDelegate` manages holding space persistence.
delegates_.push_back(std::make_unique<HoldingSpacePersistenceDelegate>( delegates_.push_back(std::make_unique<HoldingSpacePersistenceDelegate>(
profile, &holding_space_model_, profile_, &holding_space_model_,
/*item_restored_callback=*/ /*item_restored_callback=*/
base::BindRepeating(&HoldingSpaceKeyedService::AddItem, base::BindRepeating(&HoldingSpaceKeyedService::AddItem,
weak_factory_.GetWeakPtr()), weak_factory_.GetWeakPtr()),
...@@ -214,8 +211,7 @@ void HoldingSpaceKeyedService::OnModelRestored() { ...@@ -214,8 +211,7 @@ void HoldingSpaceKeyedService::OnModelRestored() {
// Once the `holding_space_model_` has been restored from persistence, we can // Once the `holding_space_model_` has been restored from persistence, we can
// start to observe the `download_manager_` to track downloaded files. // start to observe the `download_manager_` to track downloaded files.
download_manager_ = download_manager_ = content::BrowserContext::GetDownloadManager(profile_);
content::BrowserContext::GetDownloadManager(browser_context_);
download_manager_->AddObserver(this); download_manager_->AddObserver(this);
RetrieveDownloadHistory(); RetrieveDownloadHistory();
} }
......
...@@ -28,10 +28,6 @@ namespace base { ...@@ -28,10 +28,6 @@ namespace base {
class FilePath; class FilePath;
} // namespace base } // namespace base
namespace content {
class BrowserContext;
} // namespace content
namespace user_prefs { namespace user_prefs {
class PrefRegistrySyncable; class PrefRegistrySyncable;
} // namespace user_prefs } // namespace user_prefs
...@@ -54,8 +50,7 @@ class HoldingSpaceKeyedService : public KeyedService, ...@@ -54,8 +50,7 @@ class HoldingSpaceKeyedService : public KeyedService,
public content::DownloadManager::Observer, public content::DownloadManager::Observer,
public download::DownloadItem::Observer { public download::DownloadItem::Observer {
public: public:
HoldingSpaceKeyedService(content::BrowserContext* context, HoldingSpaceKeyedService(Profile* profile, const AccountId& account_id);
const AccountId& account_id);
HoldingSpaceKeyedService(const HoldingSpaceKeyedService& other) = delete; HoldingSpaceKeyedService(const HoldingSpaceKeyedService& other) = delete;
HoldingSpaceKeyedService& operator=(const HoldingSpaceKeyedService& other) = HoldingSpaceKeyedService& operator=(const HoldingSpaceKeyedService& other) =
delete; delete;
...@@ -136,7 +131,7 @@ class HoldingSpaceKeyedService : public KeyedService, ...@@ -136,7 +131,7 @@ class HoldingSpaceKeyedService : public KeyedService,
void RetrieveDownloadHistory(); void RetrieveDownloadHistory();
content::BrowserContext* const browser_context_; Profile* const profile_;
const AccountId account_id_; const AccountId account_id_;
HoldingSpaceClientImpl holding_space_client_; HoldingSpaceClientImpl holding_space_client_;
......
...@@ -37,17 +37,16 @@ KeyedService* HoldingSpaceKeyedServiceFactory::BuildServiceInstanceFor( ...@@ -37,17 +37,16 @@ KeyedService* HoldingSpaceKeyedServiceFactory::BuildServiceInstanceFor(
if (!features::IsTemporaryHoldingSpaceEnabled()) if (!features::IsTemporaryHoldingSpaceEnabled())
return nullptr; return nullptr;
user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile( Profile* profile = Profile::FromBrowserContext(context);
Profile::FromBrowserContext(context)); user_manager::User* user =
chromeos::ProfileHelper::Get()->GetUserByProfile(profile);
if (!user) if (!user)
return nullptr; return nullptr;
if (user->GetType() == user_manager::USER_TYPE_KIOSK_APP) if (user->GetType() == user_manager::USER_TYPE_KIOSK_APP)
return nullptr; return nullptr;
auto* service = new HoldingSpaceKeyedService(context, user->GetAccountId()); return new HoldingSpaceKeyedService(profile, user->GetAccountId());
return service;
} }
bool HoldingSpaceKeyedServiceFactory::ServiceIsCreatedWithBrowserContext() bool HoldingSpaceKeyedServiceFactory::ServiceIsCreatedWithBrowserContext()
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "chrome/browser/ui/ash/holding_space/holding_space_util.h" #include "chrome/browser/ui/ash/holding_space/holding_space_util.h"
#include <map>
#include "ash/public/cpp/file_icon_util.h" #include "ash/public/cpp/file_icon_util.h"
#include "ash/public/cpp/holding_space/holding_space_item.h" #include "ash/public/cpp/holding_space/holding_space_item.h"
#include "base/barrier_closure.h" #include "base/barrier_closure.h"
...@@ -45,6 +47,12 @@ void PartitionItemsByExistence(Profile* profile, ...@@ -45,6 +47,12 @@ void PartitionItemsByExistence(Profile* profile,
return; return;
} }
// Cache the original indices of the `items` being partitioned so that we can
// return them back in the same order after checking for existence.
std::map<std::string, size_t> indices_by_id;
for (size_t i = 0; i < items.size(); ++i)
indices_by_id[items.at(i)->id()] = i;
auto existing_items = std::make_unique<HoldingSpaceItemPtrList>(); auto existing_items = std::make_unique<HoldingSpaceItemPtrList>();
auto non_existing_items = std::make_unique<HoldingSpaceItemPtrList>(); auto non_existing_items = std::make_unique<HoldingSpaceItemPtrList>();
...@@ -59,12 +67,28 @@ void PartitionItemsByExistence(Profile* profile, ...@@ -59,12 +67,28 @@ void PartitionItemsByExistence(Profile* profile,
base::BindOnce( base::BindOnce(
[](std::unique_ptr<HoldingSpaceItemPtrList> existing_items, [](std::unique_ptr<HoldingSpaceItemPtrList> existing_items,
std::unique_ptr<HoldingSpaceItemPtrList> non_existing_items, std::unique_ptr<HoldingSpaceItemPtrList> non_existing_items,
std::map<std::string, size_t> indices_by_id,
PartitionItemsByExistenceCallback callback) { PartitionItemsByExistenceCallback callback) {
// We need to sort our partitioned vectors to match the original
// order that was provided at call time. This is necessary as the
// original order may have been lost due to race conditions when
// checking for item existence.
auto sort = [&indices_by_id](HoldingSpaceItemPtrList* items) {
std::sort(items->begin(), items->end(),
[&indices_by_id](const auto& a, const auto& b) {
return indices_by_id[a->id()] <
indices_by_id[b->id()];
});
};
sort(existing_items.get());
sort(non_existing_items.get());
// Ownership of the partitioned vectors is passed to `callback`.
std::move(callback).Run(std::move(*existing_items), std::move(callback).Run(std::move(*existing_items),
std::move(*non_existing_items)); std::move(*non_existing_items));
}, },
std::move(existing_items), std::move(non_existing_items), std::move(existing_items), std::move(non_existing_items),
std::move(callback))); std::move(indices_by_id), std::move(callback)));
// Verify existence of each holding space `item`. Upon successful check of // Verify existence of each holding space `item`. Upon successful check of
// existence, each `item` should be pushed into either `existing_items` or // existence, each `item` should be pushed into either `existing_items` or
......
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