Commit 179769cf authored by Hidehiko Abe's avatar Hidehiko Abe Committed by Commit Bot

Migrate ArcService to BrowserContextKeyedService part 20.

This CL migrates ArcStorageManager.

BUG=672829
TEST=Ran try.

Change-Id: If91bab2be503825fa3c10ee71053acf52c504057
Reviewed-on: https://chromium-review.googlesource.com/572886
Commit-Queue: Hidehiko Abe <hidehiko@chromium.org>
Reviewed-by: default avatarLuis Hector Chavez <lhchavez@chromium.org>
Reviewed-by: default avatarSteven Bennetts <stevenjb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#487189}
parent 1d837f7f
......@@ -98,8 +98,6 @@ void ArcServiceLauncher::Initialize() {
base::MakeUnique<ArcIntentHelperBridge>(arc_bridge_service));
arc_service_manager_->AddService(
base::MakeUnique<ArcSettingsService>(arc_bridge_service));
arc_service_manager_->AddService(
base::MakeUnique<ArcStorageManager>(arc_bridge_service));
arc_service_manager_->AddService(
base::MakeUnique<ArcTtsService>(arc_bridge_service));
arc_service_manager_->AddService(
......
......@@ -191,7 +191,10 @@ void StorageManagerHandler::HandleOpenDownloads(
void StorageManagerHandler::HandleOpenArcStorage(
const base::ListValue* unused_args) {
arc::ArcStorageManager::Get()->OpenPrivateVolumeSettings();
auto* arc_storage_manager = arc::ArcStorageManager::GetForBrowserContext(
Profile::FromWebUI(web_ui()));
if (arc_storage_manager)
arc_storage_manager->OpenPrivateVolumeSettings();
}
void StorageManagerHandler::HandleClearDriveCache(
......@@ -418,9 +421,13 @@ void StorageManagerHandler::UpdateArcSize() {
// Shows the item "Android apps and cache" and start calculating size.
web_ui()->CallJavascriptFunctionUnsafe(
"options.StorageManager.showArcItem");
bool success = arc::ArcStorageManager::Get()->GetApplicationsSize(
base::Bind(&StorageManagerHandler::OnGetArcSize,
weak_ptr_factory_.GetWeakPtr()));
bool success = false;
auto* arc_storage_manager =
arc::ArcStorageManager::GetForBrowserContext(profile);
if (arc_storage_manager) {
success = arc_storage_manager->GetApplicationsSize(base::Bind(
&StorageManagerHandler::OnGetArcSize, weak_ptr_factory_.GetWeakPtr()));
}
if (!success)
updating_arc_size_ = false;
}
......
......@@ -122,7 +122,10 @@ void StorageHandler::HandleOpenDownloads(
void StorageHandler::HandleOpenArcStorage(
const base::ListValue* unused_args) {
arc::ArcStorageManager::Get()->OpenPrivateVolumeSettings();
auto* arc_storage_manager = arc::ArcStorageManager::GetForBrowserContext(
Profile::FromWebUI(web_ui()));
if (arc_storage_manager)
arc_storage_manager->OpenPrivateVolumeSettings();
}
void StorageHandler::HandleClearDriveCache(
......@@ -340,8 +343,13 @@ void StorageHandler::UpdateAndroidSize() {
// Shows the item "Android apps and cache" and start calculating size.
FireWebUIListener("storage-android-enabled-changed", base::Value(true));
bool success = arc::ArcStorageManager::Get()->GetApplicationsSize(base::Bind(
&StorageHandler::OnGetAndroidSize, weak_ptr_factory_.GetWeakPtr()));
bool success = false;
auto* arc_storage_manager =
arc::ArcStorageManager::GetForBrowserContext(profile);
if (arc_storage_manager) {
success = arc_storage_manager->GetApplicationsSize(base::Bind(
&StorageHandler::OnGetAndroidSize, weak_ptr_factory_.GetWeakPtr()));
}
if (!success)
updating_android_size_ = false;
}
......
......@@ -8,38 +8,49 @@
#include "base/bind.h"
#include "base/logging.h"
#include "base/memory/singleton.h"
#include "components/arc/arc_bridge_service.h"
#include "components/arc/arc_browser_context_keyed_service_factory_base.h"
namespace arc {
namespace {
// This class is owned by ArcServiceManager so that it is safe to use this raw
// pointer as the singleton reference.
ArcStorageManager* g_arc_storage_manager = nullptr;
// Singleton factory for ArcStorageManager.
class ArcStorageManagerFactory
: public internal::ArcBrowserContextKeyedServiceFactoryBase<
ArcStorageManager,
ArcStorageManagerFactory> {
public:
// Factory name used by ArcBrowserContextKeyedServiceFactoryBase.
static constexpr const char* kName = "ArcStorageManagerFactory";
} // namespace
static ArcStorageManagerFactory* GetInstance() {
return base::Singleton<ArcStorageManagerFactory>::get();
}
ArcStorageManager::ArcStorageManager(ArcBridgeService* bridge_service)
: ArcService(bridge_service) {
DCHECK(!g_arc_storage_manager);
g_arc_storage_manager = this;
}
private:
friend base::DefaultSingletonTraits<ArcStorageManagerFactory>;
ArcStorageManagerFactory() = default;
~ArcStorageManagerFactory() override = default;
};
ArcStorageManager::~ArcStorageManager() {
DCHECK_EQ(this, g_arc_storage_manager);
g_arc_storage_manager = nullptr;
}
} // namespace
// static
ArcStorageManager* ArcStorageManager::Get() {
DCHECK(g_arc_storage_manager);
return g_arc_storage_manager;
ArcStorageManager* ArcStorageManager::GetForBrowserContext(
content::BrowserContext* context) {
return ArcStorageManagerFactory::GetForBrowserContext(context);
}
ArcStorageManager::ArcStorageManager(content::BrowserContext* context,
ArcBridgeService* bridge_service)
: arc_bridge_service_(bridge_service) {}
ArcStorageManager::~ArcStorageManager() = default;
bool ArcStorageManager::OpenPrivateVolumeSettings() {
auto* storage_manager_instance = ARC_GET_INSTANCE_FOR_METHOD(
arc_bridge_service()->storage_manager(), OpenPrivateVolumeSettings);
arc_bridge_service_->storage_manager(), OpenPrivateVolumeSettings);
if (!storage_manager_instance)
return false;
storage_manager_instance->OpenPrivateVolumeSettings();
......@@ -49,7 +60,7 @@ bool ArcStorageManager::OpenPrivateVolumeSettings() {
bool ArcStorageManager::GetApplicationsSize(
const GetApplicationsSizeCallback& callback) {
auto* storage_manager_instance = ARC_GET_INSTANCE_FOR_METHOD(
arc_bridge_service()->storage_manager(), GetApplicationsSize);
arc_bridge_service_->storage_manager(), GetApplicationsSize);
if (!storage_manager_instance)
return false;
storage_manager_instance->GetApplicationsSize(callback);
......@@ -59,7 +70,7 @@ bool ArcStorageManager::GetApplicationsSize(
bool ArcStorageManager::DeleteApplicationsCache(
const base::Callback<void()>& callback) {
auto* storage_manager_instance = ARC_GET_INSTANCE_FOR_METHOD(
arc_bridge_service()->storage_manager(), DeleteApplicationsCache);
arc_bridge_service_->storage_manager(), DeleteApplicationsCache);
if (!storage_manager_instance)
return false;
storage_manager_instance->DeleteApplicationsCache(callback);
......
......@@ -9,23 +9,28 @@
#include "base/callback.h"
#include "base/macros.h"
#include "components/arc/arc_service.h"
#include "components/arc/common/storage_manager.mojom.h"
#include "components/keyed_service/core/keyed_service.h"
namespace content {
class BrowserContext;
} // namespace content
namespace arc {
class ArcBridgeService;
// This class represents as a simple proxy of StorageManager to Chrome OS.
class ArcStorageManager : public ArcService {
class ArcStorageManager : public KeyedService {
public:
explicit ArcStorageManager(ArcBridgeService* bridge_service);
~ArcStorageManager() override;
// Returns singleton instance for the given BrowserContext,
// or nullptr if the browser |context| is not allowed to use ARC.
static ArcStorageManager* GetForBrowserContext(
content::BrowserContext* context);
// Gets the singleton instance of the ARC Storage Manager.
// MUST be called while ArcStorageManager is alive, otherwise it returns
// nullptr (or aborts on Debug build).
static ArcStorageManager* Get();
ArcStorageManager(content::BrowserContext* context,
ArcBridgeService* bridge_service);
~ArcStorageManager() override;
// Opens detailed preference screen of private volume on ARC.
// Returns false when an instance of ARC-side isn't ready yet.
......@@ -40,6 +45,8 @@ class ArcStorageManager : public ArcService {
bool DeleteApplicationsCache(const base::Callback<void()>& callback);
private:
ArcBridgeService* const arc_bridge_service_;
DISALLOW_COPY_AND_ASSIGN(ArcStorageManager);
};
......
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