Commit 099e7635 authored by Kyle Horimoto's avatar Kyle Horimoto Committed by Commit Bot

[CrOS PhoneHub] Add PhoneHubManager class

This class will implement the core functionality for Phone Hub.
Currently, it simply instantiates an instance of
FeatureStatusProviderImpl.

Additionally, we provide a static Get() function to be called by //ash
in ash::Shell::OnFirstSessionStarted() to get access to Phone Hub
without taking a dependency on //chrome (which is not allowed).

Bug: 1106937
Change-Id: I980237970349dc3b0ec47b0d144a59680c1c14bf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2350024
Commit-Queue: Kyle Horimoto <khorimoto@chromium.org>
Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>
Auto-Submit: Kyle Horimoto <khorimoto@chromium.org>
Reviewed-by: default avatarHenrique Nakashima <hnakashima@chromium.org>
Reviewed-by: default avatarJames Cook <jamescook@chromium.org>
Cr-Commit-Position: refs/heads/master@{#797416}
parent 2ef244b6
...@@ -113,6 +113,7 @@ source_set("chromeos") { ...@@ -113,6 +113,7 @@ source_set("chromeos") {
"//chromeos/components/mojo_bootstrap", "//chromeos/components/mojo_bootstrap",
"//chromeos/components/multidevice", "//chromeos/components/multidevice",
"//chromeos/components/multidevice/logging", "//chromeos/components/multidevice/logging",
"//chromeos/components/phonehub",
"//chromeos/components/power", "//chromeos/components/power",
"//chromeos/components/print_management/mojom", "//chromeos/components/print_management/mojom",
"//chromeos/components/proximity_auth", "//chromeos/components/proximity_auth",
...@@ -1881,6 +1882,8 @@ source_set("chromeos") { ...@@ -1881,6 +1882,8 @@ source_set("chromeos") {
"ownership/owner_settings_service_chromeos.h", "ownership/owner_settings_service_chromeos.h",
"ownership/owner_settings_service_chromeos_factory.cc", "ownership/owner_settings_service_chromeos_factory.cc",
"ownership/owner_settings_service_chromeos_factory.h", "ownership/owner_settings_service_chromeos_factory.h",
"phonehub/phone_hub_manager_factory.cc",
"phonehub/phone_hub_manager_factory.h",
"platform_keys/extension_platform_keys_service.cc", "platform_keys/extension_platform_keys_service.cc",
"platform_keys/extension_platform_keys_service.h", "platform_keys/extension_platform_keys_service.h",
"platform_keys/extension_platform_keys_service_factory.cc", "platform_keys/extension_platform_keys_service_factory.cc",
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "chrome/browser/chromeos/launcher_search_provider/launcher_search_provider_service_factory.h" #include "chrome/browser/chromeos/launcher_search_provider/launcher_search_provider_service_factory.h"
#include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_service_factory.h" #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_service_factory.h"
#include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos_factory.h" #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos_factory.h"
#include "chrome/browser/chromeos/phonehub/phone_hub_manager_factory.h"
#include "chrome/browser/chromeos/plugin_vm/plugin_vm_engagement_metrics_service.h" #include "chrome/browser/chromeos/plugin_vm/plugin_vm_engagement_metrics_service.h"
#include "chrome/browser/chromeos/policy/policy_cert_service_factory.h" #include "chrome/browser/chromeos/policy/policy_cert_service_factory.h"
#include "chrome/browser/chromeos/policy/user_cloud_policy_token_forwarder_factory.h" #include "chrome/browser/chromeos/policy/user_cloud_policy_token_forwarder_factory.h"
...@@ -70,6 +71,7 @@ void EnsureBrowserContextKeyedServiceFactoriesBuilt() { ...@@ -70,6 +71,7 @@ void EnsureBrowserContextKeyedServiceFactoriesBuilt() {
KerberosCredentialsManagerFactory::GetInstance(); KerberosCredentialsManagerFactory::GetInstance();
launcher_search_provider::ServiceFactory::GetInstance(); launcher_search_provider::ServiceFactory::GetInstance();
OwnerSettingsServiceChromeOSFactory::GetInstance(); OwnerSettingsServiceChromeOSFactory::GetInstance();
phonehub::PhoneHubManagerFactory::GetInstance();
plugin_vm::PluginVmEngagementMetricsService::Factory::GetInstance(); plugin_vm::PluginVmEngagementMetricsService::Factory::GetInstance();
policy::PolicyCertServiceFactory::GetInstance(); policy::PolicyCertServiceFactory::GetInstance();
policy::UserCloudPolicyTokenForwarderFactory::GetInstance(); policy::UserCloudPolicyTokenForwarderFactory::GetInstance();
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/phonehub/phone_hub_manager_factory.h"
#include "chrome/browser/chromeos/device_sync/device_sync_client_factory.h"
#include "chrome/browser/chromeos/multidevice_setup/multidevice_setup_client_factory.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "chromeos/components/phonehub/phone_hub_manager.h"
#include "chromeos/constants/chromeos_features.h"
#include "chromeos/services/multidevice_setup/public/cpp/prefs.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
namespace chromeos {
namespace phonehub {
namespace {
bool IsProhibitedByPolicy(Profile* profile) {
return !multidevice_setup::IsFeatureAllowed(
multidevice_setup::mojom::Feature::kPhoneHub, profile->GetPrefs());
}
bool IsLoggedInAsPrimaryUser(Profile* profile) {
// Guest/incognito profiles cannot use Phone Hub.
if (profile->IsOffTheRecord())
return false;
// Likewise, kiosk users are ineligible.
if (user_manager::UserManager::Get()->IsLoggedInAsAnyKioskApp())
return false;
return ProfileHelper::IsPrimaryProfile(profile);
}
} // namespace
// static
PhoneHubManager* PhoneHubManagerFactory::GetForProfile(Profile* profile) {
return static_cast<PhoneHubManager*>(
PhoneHubManagerFactory::GetInstance()->GetServiceForBrowserContext(
profile, /*create=*/true));
}
// static
PhoneHubManagerFactory* PhoneHubManagerFactory::GetInstance() {
return base::Singleton<PhoneHubManagerFactory>::get();
}
PhoneHubManagerFactory::PhoneHubManagerFactory()
: BrowserContextKeyedServiceFactory(
"PhoneHubManager",
BrowserContextDependencyManager::GetInstance()) {
DependsOn(device_sync::DeviceSyncClientFactory::GetInstance());
DependsOn(multidevice_setup::MultiDeviceSetupClientFactory::GetInstance());
}
PhoneHubManagerFactory::~PhoneHubManagerFactory() = default;
KeyedService* PhoneHubManagerFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
if (!features::IsPhoneHubEnabled())
return nullptr;
Profile* profile = Profile::FromBrowserContext(context);
// Only available to the primary profile.
if (!IsLoggedInAsPrimaryUser(profile))
return nullptr;
if (IsProhibitedByPolicy(profile))
return nullptr;
return new PhoneHubManager(
device_sync::DeviceSyncClientFactory::GetForProfile(profile),
multidevice_setup::MultiDeviceSetupClientFactory::GetForProfile(profile));
}
bool PhoneHubManagerFactory::ServiceIsCreatedWithBrowserContext() const {
return true;
}
} // namespace phonehub
} // namespace chromeos
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_CHROMEOS_PHONEHUB_PHONE_HUB_MANAGER_FACTORY_H_
#define CHROME_BROWSER_CHROMEOS_PHONEHUB_PHONE_HUB_MANAGER_FACTORY_H_
#include "base/memory/singleton.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
class Profile;
namespace chromeos {
namespace phonehub {
class PhoneHubManager;
class PhoneHubManagerFactory : public BrowserContextKeyedServiceFactory {
public:
// Returns the PhoneHubManager instance associated with |profile|. Null is
// returned if |profile| is not the primary Profile, if the kPhoneHub flag
// is disabled, or if the feature is prohibited by policy.
static PhoneHubManager* GetForProfile(Profile* profile);
static PhoneHubManagerFactory* GetInstance();
private:
friend struct base::DefaultSingletonTraits<PhoneHubManagerFactory>;
PhoneHubManagerFactory();
PhoneHubManagerFactory(const PhoneHubManagerFactory&) = delete;
PhoneHubManagerFactory& operator=(const PhoneHubManagerFactory&) = delete;
~PhoneHubManagerFactory() override;
// BrowserContextKeyedServiceFactory:
KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override;
bool ServiceIsCreatedWithBrowserContext() const override;
};
} // namespace phonehub
} // namespace chromeos
#endif // CHROME_BROWSER_CHROMEOS_PHONEHUB_PHONE_HUB_MANAGER_FACTORY_H_
...@@ -14,6 +14,8 @@ static_library("phonehub") { ...@@ -14,6 +14,8 @@ static_library("phonehub") {
"feature_status_provider.h", "feature_status_provider.h",
"feature_status_provider_impl.cc", "feature_status_provider_impl.cc",
"feature_status_provider_impl.h", "feature_status_provider_impl.h",
"phone_hub_manager.cc",
"phone_hub_manager.h",
] ]
deps = [ deps = [
...@@ -22,6 +24,7 @@ static_library("phonehub") { ...@@ -22,6 +24,7 @@ static_library("phonehub") {
"//chromeos/components/multidevice/logging", "//chromeos/components/multidevice/logging",
"//chromeos/services/device_sync/public/cpp", "//chromeos/services/device_sync/public/cpp",
"//chromeos/services/multidevice_setup/public/cpp", "//chromeos/services/multidevice_setup/public/cpp",
"//components/keyed_service/core",
"//device/bluetooth", "//device/bluetooth",
] ]
} }
......
...@@ -2,5 +2,6 @@ include_rules = [ ...@@ -2,5 +2,6 @@ include_rules = [
"+chromeos/components/multidevice", "+chromeos/components/multidevice",
"+chromeos/services/device_sync/public/cpp", "+chromeos/services/device_sync/public/cpp",
"+chromeos/services/multidevice_setup/public/cpp", "+chromeos/services/multidevice_setup/public/cpp",
"+components/keyed_service/core/keyed_service.h",
"+device/bluetooth", "+device/bluetooth",
] ]
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/components/phonehub/phone_hub_manager.h"
#include "base/callback.h"
#include "base/no_destructor.h"
#include "chromeos/components/phonehub/feature_status_provider_impl.h"
namespace chromeos {
namespace phonehub {
namespace {
PhoneHubManager* g_instance = nullptr;
} // namespace
// static
PhoneHubManager* PhoneHubManager::Get() {
return g_instance;
}
PhoneHubManager::PhoneHubManager(
device_sync::DeviceSyncClient* device_sync_client,
multidevice_setup::MultiDeviceSetupClient* multidevice_setup_client)
: feature_status_provider_(std::make_unique<FeatureStatusProviderImpl>(
device_sync_client,
multidevice_setup_client)) {
DCHECK(!g_instance);
g_instance = this;
}
PhoneHubManager::~PhoneHubManager() = default;
void PhoneHubManager::Shutdown() {
DCHECK(g_instance);
g_instance = nullptr;
feature_status_provider_.reset();
}
} // namespace phonehub
} // namespace chromeos
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMEOS_COMPONENTS_PHONEHUB_PHONE_HUB_MANAGER_H_
#define CHROMEOS_COMPONENTS_PHONEHUB_PHONE_HUB_MANAGER_H_
#include <memory>
#include "base/callback_forward.h"
#include "components/keyed_service/core/keyed_service.h"
namespace chromeos {
namespace device_sync {
class DeviceSyncClient;
} // namespace device_sync
namespace multidevice_setup {
class MultiDeviceSetupClient;
} // namespace multidevice_setup
namespace phonehub {
class FeatureStatusProvider;
// Implements the core logic of the Phone Hub feature and exposes interfaces via
// its public API. Implemented as a KeyedService which is keyed by the primary
// Profile; since there is only one primary Profile, the class is intended to be
// a singleton.
class PhoneHubManager : public KeyedService {
public:
// Returns a pointer to the singleton once it has been instantiated. Returns
// null if the primary profile has not yet been initialized or has already
// shut down, if the kPhoneHub flag is disabled, or if the feature is
// prohibited by policy.
static PhoneHubManager* Get();
PhoneHubManager(
device_sync::DeviceSyncClient* device_sync_client,
multidevice_setup::MultiDeviceSetupClient* multidevice_setup_client);
PhoneHubManager(const PhoneHubManager&) = delete;
PhoneHubManager& operator=(const PhoneHubManager&) = delete;
~PhoneHubManager() override;
FeatureStatusProvider* feature_status_provider() {
return feature_status_provider_.get();
}
private:
// KeyedService:
void Shutdown() override;
std::unique_ptr<FeatureStatusProvider> feature_status_provider_;
};
} // namespace phonehub
} // namespace chromeos
#endif // CHROMEOS_COMPONENTS_PHONEHUB_PHONE_HUB_MANAGER_H_
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