Commit 2c2e96ff authored by Kyle Horimoto's avatar Kyle Horimoto Committed by Commit Bot

[CrOS Nearby] Use chrome::nearby::NearbyProcessManager in Nearby Share

This CL updates Nearby Share's NearbyProcessManager class to use the
newly-created chrome::nearby::NearbyProcessManager internally in order
to make connections to the Nearby utility process.

In the long-term, Nearby Share's version should be renamed or
potentially removed altogether, but this is not within the scope of the
bug fix, so I've added a TODO to take care of this.

Bug: 1130069
Change-Id: I0f8eb7e29e83ffbc6ce428eeaacd151a294d5ef1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2459572
Commit-Queue: Kyle Horimoto <khorimoto@chromium.org>
Reviewed-by: default avatarJames Vecore <vecore@google.com>
Cr-Commit-Position: refs/heads/master@{#817688}
parent dcf600bd
...@@ -4338,6 +4338,7 @@ static_library("browser") { ...@@ -4338,6 +4338,7 @@ static_library("browser") {
"//chromeos/services/multidevice_setup", "//chromeos/services/multidevice_setup",
"//chromeos/services/multidevice_setup/public/cpp:first_run_field_trial", "//chromeos/services/multidevice_setup/public/cpp:first_run_field_trial",
"//chromeos/services/multidevice_setup/public/mojom", "//chromeos/services/multidevice_setup/public/mojom",
"//chromeos/services/nearby/public/cpp",
"//chromeos/services/network_config", "//chromeos/services/network_config",
"//chromeos/services/network_config/public/mojom", "//chromeos/services/network_config/public/mojom",
"//chromeos/services/secure_channel/public/mojom", "//chromeos/services/secure_channel/public/mojom",
......
...@@ -15,6 +15,8 @@ namespace chromeos { ...@@ -15,6 +15,8 @@ namespace chromeos {
namespace nearby { namespace nearby {
namespace { namespace {
bool g_bypass_primary_user_check_for_testing = false;
bool IsLoggedInAsPrimaryUser(Profile* profile) { bool IsLoggedInAsPrimaryUser(Profile* profile) {
// Guest/incognito profiles cannot use Phone Hub. // Guest/incognito profiles cannot use Phone Hub.
if (profile->IsOffTheRecord()) if (profile->IsOffTheRecord())
...@@ -42,6 +44,13 @@ NearbyProcessManagerFactory* NearbyProcessManagerFactory::GetInstance() { ...@@ -42,6 +44,13 @@ NearbyProcessManagerFactory* NearbyProcessManagerFactory::GetInstance() {
return base::Singleton<NearbyProcessManagerFactory>::get(); return base::Singleton<NearbyProcessManagerFactory>::get();
} }
// static
void NearbyProcessManagerFactory::SetBypassPrimaryUserCheckForTesting(
bool bypass_primary_user_check_for_testing) {
g_bypass_primary_user_check_for_testing =
bypass_primary_user_check_for_testing;
}
NearbyProcessManagerFactory::NearbyProcessManagerFactory() NearbyProcessManagerFactory::NearbyProcessManagerFactory()
: BrowserContextKeyedServiceFactory( : BrowserContextKeyedServiceFactory(
"NearbyProcessManager", "NearbyProcessManager",
...@@ -58,11 +67,15 @@ KeyedService* NearbyProcessManagerFactory::BuildServiceInstanceFor( ...@@ -58,11 +67,15 @@ KeyedService* NearbyProcessManagerFactory::BuildServiceInstanceFor(
// The service is meant to be a singleton, since multiple simultaneous process // The service is meant to be a singleton, since multiple simultaneous process
// managers could interfere with each other. Provide access only to the // managers could interfere with each other. Provide access only to the
// primary user. // primary user.
if (!IsLoggedInAsPrimaryUser(profile)) if (IsLoggedInAsPrimaryUser(profile) ||
return nullptr; g_bypass_primary_user_check_for_testing) {
return NearbyProcessManagerImpl::Factory::Create(
return new NearbyProcessManagerImpl( NearbyConnectionsDependenciesProviderFactory::GetForProfile(
NearbyConnectionsDependenciesProviderFactory::GetForProfile(profile)); profile))
.release();
}
return nullptr;
} }
bool NearbyProcessManagerFactory::ServiceIsCreatedWithBrowserContext() const { bool NearbyProcessManagerFactory::ServiceIsCreatedWithBrowserContext() const {
......
...@@ -23,6 +23,11 @@ class NearbyProcessManagerFactory : public BrowserContextKeyedServiceFactory { ...@@ -23,6 +23,11 @@ class NearbyProcessManagerFactory : public BrowserContextKeyedServiceFactory {
static NearbyProcessManagerFactory* GetInstance(); static NearbyProcessManagerFactory* GetInstance();
// When true is passed, this factory will create a NearbyProcessManager even
// when it is not the primary profile.
static void SetBypassPrimaryUserCheckForTesting(
bool bypass_primary_user_check_for_testing);
private: private:
friend struct base::DefaultSingletonTraits<NearbyProcessManagerFactory>; friend struct base::DefaultSingletonTraits<NearbyProcessManagerFactory>;
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "chrome/browser/chromeos/nearby/nearby_process_manager_impl.h" #include "chrome/browser/chromeos/nearby/nearby_process_manager_impl.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "chrome/browser/chromeos/nearby/nearby_connections_dependencies_provider.h" #include "chrome/browser/chromeos/nearby/nearby_connections_dependencies_provider.h"
#include "chrome/browser/nearby_sharing/logging/logging.h" #include "chrome/browser/nearby_sharing/logging/logging.h"
#include "chrome/browser/sharing/webrtc/sharing_mojo_service.h" #include "chrome/browser/sharing/webrtc/sharing_mojo_service.h"
...@@ -12,6 +13,29 @@ ...@@ -12,6 +13,29 @@
namespace chromeos { namespace chromeos {
namespace nearby { namespace nearby {
namespace {
NearbyProcessManagerImpl::Factory* g_test_factory = nullptr;
} // namespace
// static
std::unique_ptr<NearbyProcessManager> NearbyProcessManagerImpl::Factory::Create(
NearbyConnectionsDependenciesProvider*
nearby_connections_dependencies_provider) {
if (g_test_factory) {
return g_test_factory->BuildInstance(
nearby_connections_dependencies_provider);
}
return base::WrapUnique(
new NearbyProcessManagerImpl(nearby_connections_dependencies_provider));
}
// static
void NearbyProcessManagerImpl::Factory::SetFactoryForTesting(Factory* factory) {
g_test_factory = factory;
}
NearbyProcessManagerImpl::NearbyReferenceImpl::NearbyReferenceImpl( NearbyProcessManagerImpl::NearbyReferenceImpl::NearbyReferenceImpl(
const mojo::SharedRemote< const mojo::SharedRemote<
......
...@@ -30,9 +30,20 @@ class NearbyConnectionsDependenciesProvider; ...@@ -30,9 +30,20 @@ class NearbyConnectionsDependenciesProvider;
// is killed as soon as the last client releases its reference. // is killed as soon as the last client releases its reference.
class NearbyProcessManagerImpl : public NearbyProcessManager { class NearbyProcessManagerImpl : public NearbyProcessManager {
public: public:
explicit NearbyProcessManagerImpl( class Factory {
NearbyConnectionsDependenciesProvider* public:
nearby_connections_dependencies_provider); static std::unique_ptr<NearbyProcessManager> Create(
NearbyConnectionsDependenciesProvider*
nearby_connections_dependencies_provider);
static void SetFactoryForTesting(Factory* factory);
virtual ~Factory() = default;
private:
virtual std::unique_ptr<NearbyProcessManager> BuildInstance(
NearbyConnectionsDependenciesProvider*
nearby_connections_dependencies_provider) = 0;
};
~NearbyProcessManagerImpl() override; ~NearbyProcessManagerImpl() override;
private: private:
...@@ -61,6 +72,10 @@ class NearbyProcessManagerImpl : public NearbyProcessManager { ...@@ -61,6 +72,10 @@ class NearbyProcessManagerImpl : public NearbyProcessManager {
base::OnceClosure destructor_callback_; base::OnceClosure destructor_callback_;
}; };
explicit NearbyProcessManagerImpl(
NearbyConnectionsDependenciesProvider*
nearby_connections_dependencies_provider);
// NearbyProcessManagerImpl: // NearbyProcessManagerImpl:
std::unique_ptr<NearbyProcessReference> GetNearbyProcessReference( std::unique_ptr<NearbyProcessReference> GetNearbyProcessReference(
base::OnceClosure on_process_stopped_callback) override; base::OnceClosure on_process_stopped_callback) override;
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "base/observer_list_types.h" #include "base/observer_list_types.h"
#include "base/scoped_observer.h" #include "base/scoped_observer.h"
#include "chrome/browser/profiles/profile_manager_observer.h" #include "chrome/browser/profiles/profile_manager_observer.h"
#include "chromeos/services/nearby/public/cpp/nearby_process_manager.h"
#include "chromeos/services/nearby/public/mojom/nearby_connections.mojom.h" #include "chromeos/services/nearby/public/mojom/nearby_connections.mojom.h"
#include "chromeos/services/nearby/public/mojom/nearby_decoder.mojom.h" #include "chromeos/services/nearby/public/mojom/nearby_decoder.mojom.h"
#include "chromeos/services/nearby/public/mojom/sharing.mojom.h" #include "chromeos/services/nearby/public/mojom/sharing.mojom.h"
...@@ -24,9 +25,10 @@ ...@@ -24,9 +25,10 @@
class Profile; class Profile;
class ProfileAttributesEntry; class ProfileAttributesEntry;
// Manages the lifetime of the Nearby process. It runs the Nearby Connections // Interfaces with the Nearby utility process.
// library and Nearby Sharing data decoding. Only one instance of the process is // TODO(https://crbug.com/1137664): This class should be renamed or deleted
// supported at a time. // altogether. It was written before chromeos::nearby::NerabyProcessManager was
// created.
class NearbyProcessManager : public ProfileManagerObserver { class NearbyProcessManager : public ProfileManagerObserver {
public: public:
using NearbyConnectionsMojom = using NearbyConnectionsMojom =
...@@ -107,10 +109,6 @@ class NearbyProcessManager : public ProfileManagerObserver { ...@@ -107,10 +109,6 @@ class NearbyProcessManager : public ProfileManagerObserver {
void OnProfileAdded(Profile* profile) override; void OnProfileAdded(Profile* profile) override;
void OnProfileMarkedForPermanentDeletion(Profile* profile) override; void OnProfileMarkedForPermanentDeletion(Profile* profile) override;
// Binds the given |sharing| remote to be used as the interface to the Sharing
// process running in a sandbox.
void BindSharingProcess(mojo::PendingRemote<sharing::mojom::Sharing> sharing);
private: private:
FRIEND_TEST_ALL_PREFIXES(NearbyProcessManagerTest, AddRemoveObserver); FRIEND_TEST_ALL_PREFIXES(NearbyProcessManagerTest, AddRemoveObserver);
FRIEND_TEST_ALL_PREFIXES(NearbySharingServiceImplTest, FRIEND_TEST_ALL_PREFIXES(NearbySharingServiceImplTest,
...@@ -124,65 +122,15 @@ class NearbyProcessManager : public ProfileManagerObserver { ...@@ -124,65 +122,15 @@ class NearbyProcessManager : public ProfileManagerObserver {
NearbyProcessManager(); NearbyProcessManager();
~NearbyProcessManager() override; ~NearbyProcessManager() override;
// Launches a new sandboxed process and stops any currently running one. This void EnsureProcessIsRunning();
// process is then used to run the Nearby Connections library. The process
// will use the current profile to initialize Nearby Connections as returned
// by UsedProfile().
void LaunchNewProcess();
// Binds a new pipe to the Nearby Connections library. May start a new process
// if there is none running yet.
void BindNearbyConnections();
// Gather dependencies for NearbyConnections:
void GetBluetoothAdapter(
location::nearby::connections::mojom::NearbyConnectionsDependencies*
dependencies,
base::ScopedClosureRunner done_closure);
void OnGetBluetoothAdapter(
location::nearby::connections::mojom::NearbyConnectionsDependencies*
dependencies,
base::ScopedClosureRunner done_closure,
scoped_refptr<device::BluetoothAdapter> adapter);
void GetWebRtcDependencies(
location::nearby::connections::mojom::NearbyConnectionsDependencies*
dependencies,
base::ScopedClosureRunner done_closure);
// Called when all dependencies are gathered.
void OnDependenciesGathered(
mojo::PendingReceiver<NearbyConnectionsMojom> receiver,
location::nearby::connections::mojom::NearbyConnectionsDependenciesPtr
dependencies);
// Called by the sandboxed process after initializing the Nearby Connections
// library.
void OnNearbyConnections(
mojo::PendingReceiver<NearbyConnectionsMojom> receiver,
mojo::PendingRemote<NearbyConnectionsMojom> remote);
// Called if any of the mojo interfaces to the sandboxed process disconnects.
// If that happens we stop the process and notify all observers via
// Observer::OnNearbyProcessStopped().
void OnNearbyProcessStopped(); void OnNearbyProcessStopped();
void EnsureNearbyProcessReferenceReleased();
// Binds a new pipe to the Nearby Sharing Decoder. May start a new process // Reference to the Nearby utility process; if null, no reference is currently
// if there is none running yet. // held.
void BindNearbySharingDecoder(); std::unique_ptr<
chromeos::nearby::NearbyProcessManager::NearbyProcessReference>
// Called by the sandboxed process after initializing the Nearby Sharing reference_;
// Decoder.
void OnNearbySharingDecoder(
mojo::PendingReceiver<NearbySharingDecoderMojom> receiver,
mojo::PendingRemote<NearbySharingDecoderMojom> remote);
// The bound remote to a sandboxed process.
mojo::Remote<sharing::mojom::Sharing> sharing_process_;
// The bound remote to the Nearby Connections library inside the sandbox.
mojo::Remote<NearbyConnectionsMojom> connections_;
// The bound remote to the Nearby Decoder interface inside the sandbox.
mojo::Remote<NearbySharingDecoderMojom> decoder_;
// All registered observers, typically one per loaded profile. // All registered observers, typically one per loaded profile.
base::ObserverList<Observer> observers_; base::ObserverList<Observer> observers_;
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/memory/singleton.h" #include "base/memory/singleton.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "chrome/browser/browser_features.h" #include "chrome/browser/browser_features.h"
#include "chrome/browser/chromeos/nearby/nearby_process_manager_factory.h"
#include "chrome/browser/nearby_sharing/common/nearby_share_prefs.h" #include "chrome/browser/nearby_sharing/common/nearby_share_prefs.h"
#include "chrome/browser/nearby_sharing/logging/logging.h" #include "chrome/browser/nearby_sharing/logging/logging.h"
#include "chrome/browser/nearby_sharing/nearby_connections_manager.h" #include "chrome/browser/nearby_sharing/nearby_connections_manager.h"
...@@ -55,6 +56,7 @@ NearbySharingServiceFactory::NearbySharingServiceFactory() ...@@ -55,6 +56,7 @@ NearbySharingServiceFactory::NearbySharingServiceFactory()
kServiceName, kServiceName,
BrowserContextDependencyManager::GetInstance()) { BrowserContextDependencyManager::GetInstance()) {
DependsOn(IdentityManagerFactory::GetInstance()); DependsOn(IdentityManagerFactory::GetInstance());
DependsOn(chromeos::nearby::NearbyProcessManagerFactory::GetInstance());
DependsOn(NotificationDisplayServiceFactory::GetInstance()); DependsOn(NotificationDisplayServiceFactory::GetInstance());
} }
......
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