Commit 597f92a5 authored by Kyle Horimoto's avatar Kyle Horimoto Committed by Commit Bot

[CrOS PhoneHub] Implement NearbyConnectionManagerImpl

This CL also adds a NearbyConnection class, which currently does nothing
when Connect() is called. A future CL will call the Nearby Connections
API within that class.

Bug: 1106937
Change-Id: I397c7c8ff62092559b158f8cb09b8e0948544188
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2425211Reviewed-by: default avatarJames Vecore <vecore@google.com>
Commit-Queue: Kyle Horimoto <khorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#810370}
parent 58453d82
...@@ -104,6 +104,8 @@ static_library("secure_channel") { ...@@ -104,6 +104,8 @@ static_library("secure_channel") {
"multiplexed_channel.h", "multiplexed_channel.h",
"multiplexed_channel_impl.cc", "multiplexed_channel_impl.cc",
"multiplexed_channel_impl.h", "multiplexed_channel_impl.h",
"nearby_connection.cc",
"nearby_connection.h",
"nearby_connection_manager.cc", "nearby_connection_manager.cc",
"nearby_connection_manager.h", "nearby_connection_manager.h",
"nearby_connection_manager_impl.cc", "nearby_connection_manager_impl.cc",
......
// 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/services/secure_channel/nearby_connection.h"
#include "base/memory/ptr_util.h"
namespace chromeos {
namespace secure_channel {
// static
NearbyConnection::Factory* NearbyConnection::Factory::factory_instance_ =
nullptr;
// static
std::unique_ptr<Connection> NearbyConnection::Factory::Create(
multidevice::RemoteDeviceRef remote_device) {
if (factory_instance_)
return factory_instance_->CreateInstance(remote_device);
return base::WrapUnique(new NearbyConnection(remote_device));
}
// static
void NearbyConnection::Factory::SetFactoryForTesting(Factory* factory) {
factory_instance_ = factory;
}
NearbyConnection::NearbyConnection(multidevice::RemoteDeviceRef remote_device)
: Connection(remote_device) {}
NearbyConnection::~NearbyConnection() {
// TODO(https://crbug.com/1106937): Clean up potentially-lingering connection.
}
void NearbyConnection::Connect() {
// TODO(https://crbug.com/1106937): Implement.
}
void NearbyConnection::Disconnect() {
// TODO(https://crbug.com/1106937): Implement.
}
std::string NearbyConnection::GetDeviceAddress() {
return remote_device().bluetooth_public_address();
}
void NearbyConnection::SendMessageImpl(std::unique_ptr<WireMessage> message) {
// TODO(https://crbug.com/1106937): Implement.
}
} // namespace secure_channel
} // 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_SERVICES_SECURE_CHANNEL_NEARBY_CONNECTION_H_
#define CHROMEOS_SERVICES_SECURE_CHANNEL_NEARBY_CONNECTION_H_
#include "chromeos/services/secure_channel/connection.h"
namespace chromeos {
namespace secure_channel {
// Connection implementation which creates a connection to a remote device via
// the Nearby Connections library.
// TODO(https://crbug.com/1106937): Add real implementation.
class NearbyConnection : public Connection {
public:
class Factory {
public:
static std::unique_ptr<Connection> Create(
multidevice::RemoteDeviceRef remote_device);
static void SetFactoryForTesting(Factory* factory);
virtual ~Factory() = default;
protected:
virtual std::unique_ptr<Connection> CreateInstance(
multidevice::RemoteDeviceRef remote_device) = 0;
private:
static Factory* factory_instance_;
};
~NearbyConnection() override;
private:
NearbyConnection(multidevice::RemoteDeviceRef remote_device);
// Connection:
void Connect() override;
void Disconnect() override;
std::string GetDeviceAddress() override;
void SendMessageImpl(std::unique_ptr<WireMessage> message) override;
};
} // namespace secure_channel
} // namespace chromeos
#endif // CHROMEOS_SERVICES_SECURE_CHANNEL_NEARBY_CONNECTION_H_
...@@ -5,23 +5,36 @@ ...@@ -5,23 +5,36 @@
#ifndef CHROMEOS_SERVICES_SECURE_CHANNEL_NEARBY_CONNECTION_MANAGER_IMPL_H_ #ifndef CHROMEOS_SERVICES_SECURE_CHANNEL_NEARBY_CONNECTION_MANAGER_IMPL_H_
#define CHROMEOS_SERVICES_SECURE_CHANNEL_NEARBY_CONNECTION_MANAGER_IMPL_H_ #define CHROMEOS_SERVICES_SECURE_CHANNEL_NEARBY_CONNECTION_MANAGER_IMPL_H_
#include "chromeos/services/secure_channel/ble_scanner.h"
#include "chromeos/services/secure_channel/nearby_connection_manager.h" #include "chromeos/services/secure_channel/nearby_connection_manager.h"
#include "chromeos/services/secure_channel/secure_channel.h"
namespace chromeos { namespace chromeos {
namespace secure_channel { namespace secure_channel {
// TODO(https://crbug.com/1106937): Add real implementation. class SecureChannelDisconnector;
class NearbyConnectionManagerImpl : public NearbyConnectionManager {
// NearbyConnectionManager implementation which uses BleScanner to determine
// whether the desired device is in proximity. If BleScanner discovers the
// device is nearby, it creates a new NearbyConnection to that device, then
// completes the authentication flow before returning it to the caller.
class NearbyConnectionManagerImpl : public NearbyConnectionManager,
public BleScanner::Observer,
public SecureChannel::Observer {
public: public:
class Factory { class Factory {
public: public:
static std::unique_ptr<NearbyConnectionManager> Create(); static std::unique_ptr<NearbyConnectionManager> Create(
BleScanner* ble_scanner,
SecureChannelDisconnector* secure_channel_disconnector);
static void SetFactoryForTesting(Factory* test_factory); static void SetFactoryForTesting(Factory* test_factory);
protected: protected:
virtual ~Factory(); virtual ~Factory();
virtual std::unique_ptr<NearbyConnectionManager> CreateInstance() = 0; virtual std::unique_ptr<NearbyConnectionManager> CreateInstance(
BleScanner* ble_scanner,
SecureChannelDisconnector* secure_channel_disconnector) = 0;
private: private:
static Factory* test_factory_; static Factory* test_factory_;
...@@ -33,13 +46,72 @@ class NearbyConnectionManagerImpl : public NearbyConnectionManager { ...@@ -33,13 +46,72 @@ class NearbyConnectionManagerImpl : public NearbyConnectionManager {
~NearbyConnectionManagerImpl() override; ~NearbyConnectionManagerImpl() override;
private: private:
NearbyConnectionManagerImpl(); NearbyConnectionManagerImpl(
BleScanner* ble_scanner,
SecureChannelDisconnector* secure_channel_disconnector);
// NearbyConnectionManager: // NearbyConnectionManager:
void PerformAttemptNearbyInitiatorConnection( void PerformAttemptNearbyInitiatorConnection(
const DeviceIdPair& device_id_pair) override; const DeviceIdPair& device_id_pair) override;
void PerformCancelNearbyInitiatorConnectionAttempt( void PerformCancelNearbyInitiatorConnectionAttempt(
const DeviceIdPair& device_id_pair) override; const DeviceIdPair& device_id_pair) override;
// BleScanner::Observer:
void OnReceivedAdvertisement(multidevice::RemoteDeviceRef remote_device,
device::BluetoothDevice* bluetooth_device,
ConnectionMedium connection_medium,
ConnectionRole connection_role) override;
// SecureChannel::Observer:
void OnSecureChannelStatusChanged(
SecureChannel* secure_channel,
const SecureChannel::Status& old_status,
const SecureChannel::Status& new_status) override;
// Returns whether a channel exists connecting to |remote_device_id|,
// regardless of the local device ID used to create the connection.
bool DoesAuthenticatingChannelExist(const std::string& remote_device_id);
// Adds |secure_channel| to |remote_device_id_to_secure_channel_map_| and
// pauses any ongoing attempts to |remote_device_id|, since a connection has
// already been established to that device.
void SetAuthenticatingChannel(const std::string& remote_device_id,
std::unique_ptr<SecureChannel> secure_channel);
// Pauses pending connection attempts (i.e., BLE scanning) for
// |remote_device_id|.
void PauseConnectionAttemptsToDevice(const std::string& remote_device_id);
// Restarts connections which were paused as part of
// PauseConnectionAttemptsToDevice();
void RestartPausedAttemptsToDevice(const std::string& remote_device_id);
// Checks to see if there is a leftover channel authenticating with
// |remote_device_id| even though there are no pending requests for a
// connection to that device. This situation arises when an active request is
// canceled after a connection has been established but before that connection
// has been fully authenticated. This function disconnects the channel in the
// case that it finds one.
void ProcessPotentialLingeringChannel(const std::string& remote_device_id);
std::string GetRemoteDeviceIdForSecureChannel(SecureChannel* secure_channel);
void HandleSecureChannelDisconnection(const std::string& remote_device_id,
bool was_authenticating);
void HandleChannelAuthenticated(const std::string& remote_device_id);
// Chooses the connection attempt which will receive the success callback.
// It is possible that there is more than one possible recipient in the case
// that two attempts are made with the same remote device ID but different
// local device IDs. In the case of multiple possible recipients, we
// arbitrarily choose the one which was registered first.
DeviceIdPair ChooseChannelRecipient(const std::string& remote_device_id);
BleScanner* ble_scanner_;
SecureChannelDisconnector* secure_channel_disconnector_;
base::flat_map<std::string, std::unique_ptr<SecureChannel>>
remote_device_id_to_secure_channel_map_;
base::Optional<std::string> notifying_remote_device_id_;
}; };
} // namespace secure_channel } // namespace secure_channel
......
...@@ -85,8 +85,9 @@ SecureChannelImpl::SecureChannelImpl( ...@@ -85,8 +85,9 @@ SecureChannelImpl::SecureChannelImpl(
ble_scanner_.get(), ble_scanner_.get(),
secure_channel_disconnector_.get(), secure_channel_disconnector_.get(),
timer_factory_.get())), timer_factory_.get())),
nearby_connection_manager_( nearby_connection_manager_(NearbyConnectionManagerImpl::Factory::Create(
NearbyConnectionManagerImpl::Factory::Create()), ble_scanner_.get(),
secure_channel_disconnector_.get())),
pending_connection_manager_(PendingConnectionManagerImpl::Factory::Create( pending_connection_manager_(PendingConnectionManagerImpl::Factory::Create(
this /* delegate */, this /* delegate */,
ble_connection_manager_.get(), ble_connection_manager_.get(),
......
...@@ -273,20 +273,37 @@ class FakeBleConnectionManagerFactory ...@@ -273,20 +273,37 @@ class FakeBleConnectionManagerFactory
class FakeNearbyConnectionManagerFactory class FakeNearbyConnectionManagerFactory
: public NearbyConnectionManagerImpl::Factory { : public NearbyConnectionManagerImpl::Factory {
public: public:
FakeNearbyConnectionManagerFactory() = default; FakeNearbyConnectionManagerFactory(
FakeBleScannerFactory* fake_ble_scanner_factory,
FakeSecureChannelDisconnectorFactory*
fake_secure_channel_disconnector_factory)
: fake_ble_scanner_factory_(fake_ble_scanner_factory),
fake_secure_channel_disconnector_factory_(
fake_secure_channel_disconnector_factory) {}
~FakeNearbyConnectionManagerFactory() override = default; ~FakeNearbyConnectionManagerFactory() override = default;
FakeNearbyConnectionManager* instance() { return instance_; } FakeNearbyConnectionManager* instance() { return instance_; }
private: private:
// NearbyConnectionManagerImpl::Factory: // NearbyConnectionManagerImpl::Factory:
std::unique_ptr<NearbyConnectionManager> CreateInstance() override { std::unique_ptr<NearbyConnectionManager> CreateInstance(
BleScanner* ble_scanner,
SecureChannelDisconnector* secure_channel_disconnector) override {
EXPECT_EQ(fake_ble_scanner_factory_->instance(), ble_scanner);
EXPECT_EQ(fake_secure_channel_disconnector_factory_->instance(),
secure_channel_disconnector);
EXPECT_FALSE(instance_); EXPECT_FALSE(instance_);
auto instance = std::make_unique<FakeNearbyConnectionManager>(); auto instance = std::make_unique<FakeNearbyConnectionManager>();
instance_ = instance.get(); instance_ = instance.get();
return instance; return instance;
} }
FakeBleScannerFactory* fake_ble_scanner_factory_;
FakeSecureChannelDisconnectorFactory*
fake_secure_channel_disconnector_factory_;
FakeNearbyConnectionManager* instance_ = nullptr; FakeNearbyConnectionManager* instance_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(FakeNearbyConnectionManagerFactory); DISALLOW_COPY_AND_ASSIGN(FakeNearbyConnectionManagerFactory);
...@@ -519,7 +536,9 @@ class SecureChannelServiceTest : public testing::Test { ...@@ -519,7 +536,9 @@ class SecureChannelServiceTest : public testing::Test {
fake_ble_connection_manager_factory_.get()); fake_ble_connection_manager_factory_.get());
fake_nearby_connection_manager_factory_ = fake_nearby_connection_manager_factory_ =
std::make_unique<FakeNearbyConnectionManagerFactory>(); std::make_unique<FakeNearbyConnectionManagerFactory>(
fake_ble_scanner_factory_.get(),
fake_secure_channel_disconnector_factory_.get());
NearbyConnectionManagerImpl::Factory::SetFactoryForTesting( NearbyConnectionManagerImpl::Factory::SetFactoryForTesting(
fake_nearby_connection_manager_factory_.get()); fake_nearby_connection_manager_factory_.get());
......
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