Commit 05a1565a authored by Kyle Horimoto's avatar Kyle Horimoto Committed by Commit Bot

[CrOS PhoneHub] Handle pending Nearby connection requests

This CL implements a TODO in PendingConnectionManagerImpl to handle
incoming requests for connections via the Nearby Connections library.
When a request is received, we create a NearbyInitiatorConnectionAttempt
(if one does not already exist), then add a new
PendingNearbyInitiatorConnectionRequest to it.

Additionally, this CL updates the initialization flow for the
SecureChannel service so that it now creates an instance of
NearbyConnectionManager.

Bug: 1106937
Change-Id: Ibe56476babfb201b748beed1fefc80efda7e304e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2419374
Commit-Queue: Kyle Horimoto <khorimoto@chromium.org>
Reviewed-by: default avatarJames Vecore <vecore@google.com>
Cr-Commit-Position: refs/heads/master@{#809534}
parent da6b36bc
......@@ -18,6 +18,7 @@
#include "chromeos/services/secure_channel/connection_attempt_delegate.h"
#include "chromeos/services/secure_channel/connection_role.h"
#include "chromeos/services/secure_channel/device_id_pair.h"
#include "chromeos/services/secure_channel/nearby_initiator_failure_type.h"
#include "chromeos/services/secure_channel/pending_connection_manager.h"
#include "chromeos/services/secure_channel/public/cpp/shared/connection_medium.h"
#include "chromeos/services/secure_channel/public/cpp/shared/connection_priority.h"
......@@ -28,6 +29,7 @@ namespace chromeos {
namespace secure_channel {
class BleConnectionManager;
class NearbyConnectionManager;
// Concrete PendingConnectionManager implementation. This class creates one
// ConnectionAttempt per ConnectionAttemptDetails requested; if more than one
......@@ -45,6 +47,7 @@ class PendingConnectionManagerImpl : public PendingConnectionManager,
static std::unique_ptr<PendingConnectionManager> Create(
Delegate* delegate,
BleConnectionManager* ble_connection_manager,
NearbyConnectionManager* nearby_connection_manager,
scoped_refptr<device::BluetoothAdapter> bluetooth_adapter);
static void SetFactoryForTesting(Factory* test_factory);
......@@ -53,6 +56,7 @@ class PendingConnectionManagerImpl : public PendingConnectionManager,
virtual std::unique_ptr<PendingConnectionManager> CreateInstance(
Delegate* delegate,
BleConnectionManager* ble_connection_manager,
NearbyConnectionManager* nearby_connection_manager,
scoped_refptr<device::BluetoothAdapter> bluetooth_adapter) = 0;
private:
......@@ -65,6 +69,7 @@ class PendingConnectionManagerImpl : public PendingConnectionManager,
PendingConnectionManagerImpl(
Delegate* delegate,
BleConnectionManager* ble_connection_manager,
NearbyConnectionManager* nearby_connection_manager,
scoped_refptr<device::BluetoothAdapter> bluetooth_adapter);
// PendingConnectionManager:
......@@ -97,9 +102,29 @@ class PendingConnectionManagerImpl : public PendingConnectionManager,
const ConnectionAttemptDetails& connection_attempt_details,
std::unique_ptr<ClientConnectionParameters> client_connection_parameters,
ConnectionPriority connection_priority);
void HandleNearbyInitiatorRequest(
const ConnectionAttemptDetails& connection_attempt_details,
std::unique_ptr<ClientConnectionParameters> client_connection_parameters,
ConnectionPriority connection_priority);
// Retrieves ClientConnectionParameters for a given connection attempt.
// Because a single connection attempt may have multiple client requests
// (e.g., when multiple clients requets a connection at the same time), this
// function returns a vector.
//
// Note that this function std::move()s results from the |id_pair_to_*_| maps
// below, so these maps will end up having "empty" values after the function
// is called. This function is expected to be used in conjunction with
// RemoveMapEntriesForFinishedConnectionAttempt(), which cleans up those empty
// values.
std::vector<std::unique_ptr<ClientConnectionParameters>>
ExtractClientConnectionParameters(
const ConnectionAttemptDetails& connection_attempt_details);
void RemoveMapEntriesForFinishedConnectionAttempt(
const ConnectionAttemptDetails& connection_attempt_details);
void RemoveIdPairToConnectionAttemptMapEntriesForFinishedConnectionAttempt(
const ConnectionAttemptDetails& connection_attempt_details);
base::flat_map<DeviceIdPair,
std::unique_ptr<ConnectionAttempt<BleInitiatorFailureType>>>
......@@ -109,10 +134,15 @@ class PendingConnectionManagerImpl : public PendingConnectionManager,
std::unique_ptr<ConnectionAttempt<BleListenerFailureType>>>
id_pair_to_ble_listener_connection_attempts_;
base::flat_map<DeviceIdPair,
std::unique_ptr<ConnectionAttempt<NearbyInitiatorFailureType>>>
id_pair_to_nearby_initiator_connection_attempts_;
base::flat_map<ConnectionDetails, base::flat_set<ConnectionAttemptDetails>>
details_to_attempt_details_map_;
BleConnectionManager* ble_connection_manager_;
NearbyConnectionManager* nearby_connection_manager_;
scoped_refptr<device::BluetoothAdapter> bluetooth_adapter_;
DISALLOW_COPY_AND_ASSIGN(PendingConnectionManagerImpl);
......
......@@ -18,6 +18,7 @@
#include "chromeos/services/secure_channel/bluetooth_helper_impl.h"
#include "chromeos/services/secure_channel/client_connection_parameters_impl.h"
#include "chromeos/services/secure_channel/device_id_pair.h"
#include "chromeos/services/secure_channel/nearby_connection_manager_impl.h"
#include "chromeos/services/secure_channel/pending_connection_manager_impl.h"
#include "chromeos/services/secure_channel/timer_factory_impl.h"
#include "device/bluetooth/bluetooth_adapter.h"
......@@ -80,9 +81,12 @@ SecureChannelImpl::SecureChannelImpl(
ble_synchronizer_.get(),
ble_scanner_.get(),
timer_factory_.get())),
nearby_connection_manager_(
NearbyConnectionManagerImpl::Factory::Create()),
pending_connection_manager_(PendingConnectionManagerImpl::Factory::Create(
this /* delegate */,
ble_connection_manager_.get(),
nearby_connection_manager_.get(),
bluetooth_adapter_)),
active_connection_manager_(
ActiveConnectionManagerImpl::Factory::Create(this /* delegate */)) {}
......
......@@ -32,6 +32,7 @@ class BleConnectionManager;
class BleScanner;
class BluetoothHelper;
class BleSynchronizerBase;
class NearbyConnectionManager;
class TimerFactory;
// Concrete SecureChannelImpl implementation, which contains three pieces:
......@@ -178,6 +179,7 @@ class SecureChannelImpl : public mojom::SecureChannel,
std::unique_ptr<BleSynchronizerBase> ble_synchronizer_;
std::unique_ptr<BleScanner> ble_scanner_;
std::unique_ptr<BleConnectionManager> ble_connection_manager_;
std::unique_ptr<NearbyConnectionManager> nearby_connection_manager_;
std::unique_ptr<PendingConnectionManager> pending_connection_manager_;
std::unique_ptr<ActiveConnectionManager> active_connection_manager_;
......
......@@ -28,8 +28,10 @@
#include "chromeos/services/secure_channel/fake_bluetooth_helper.h"
#include "chromeos/services/secure_channel/fake_client_connection_parameters.h"
#include "chromeos/services/secure_channel/fake_connection_delegate.h"
#include "chromeos/services/secure_channel/fake_nearby_connection_manager.h"
#include "chromeos/services/secure_channel/fake_pending_connection_manager.h"
#include "chromeos/services/secure_channel/fake_timer_factory.h"
#include "chromeos/services/secure_channel/nearby_connection_manager_impl.h"
#include "chromeos/services/secure_channel/pending_connection_manager_impl.h"
#include "chromeos/services/secure_channel/public/cpp/shared/connection_priority.h"
#include "chromeos/services/secure_channel/public/mojom/secure_channel.mojom.h"
......@@ -236,13 +238,39 @@ class FakeBleConnectionManagerFactory
DISALLOW_COPY_AND_ASSIGN(FakeBleConnectionManagerFactory);
};
class FakeNearbyConnectionManagerFactory
: public NearbyConnectionManagerImpl::Factory {
public:
FakeNearbyConnectionManagerFactory() = default;
~FakeNearbyConnectionManagerFactory() override = default;
FakeNearbyConnectionManager* instance() { return instance_; }
private:
// NearbyConnectionManagerImpl::Factory:
std::unique_ptr<NearbyConnectionManager> CreateInstance() override {
EXPECT_FALSE(instance_);
auto instance = std::make_unique<FakeNearbyConnectionManager>();
instance_ = instance.get();
return instance;
}
FakeNearbyConnectionManager* instance_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(FakeNearbyConnectionManagerFactory);
};
class FakePendingConnectionManagerFactory
: public PendingConnectionManagerImpl::Factory {
public:
FakePendingConnectionManagerFactory(
FakeBleConnectionManagerFactory* fake_ble_connection_manager_factory)
FakeBleConnectionManagerFactory* fake_ble_connection_manager_factory,
FakeNearbyConnectionManagerFactory*
fake_nearby_connection_manager_factory)
: fake_ble_connection_manager_factory_(
fake_ble_connection_manager_factory) {}
fake_ble_connection_manager_factory),
fake_nearby_connection_manager_factory_(
fake_nearby_connection_manager_factory) {}
~FakePendingConnectionManagerFactory() override = default;
......@@ -253,10 +281,13 @@ class FakePendingConnectionManagerFactory
std::unique_ptr<PendingConnectionManager> CreateInstance(
PendingConnectionManager::Delegate* delegate,
BleConnectionManager* ble_connection_manager,
NearbyConnectionManager* nearby_connection_manager,
scoped_refptr<device::BluetoothAdapter> bluetooth_adapter) override {
EXPECT_FALSE(instance_);
EXPECT_EQ(fake_ble_connection_manager_factory_->instance(),
ble_connection_manager);
EXPECT_EQ(fake_nearby_connection_manager_factory_->instance(),
nearby_connection_manager);
auto instance = std::make_unique<FakePendingConnectionManager>(delegate);
instance_ = instance.get();
......@@ -264,6 +295,7 @@ class FakePendingConnectionManagerFactory
}
FakeBleConnectionManagerFactory* fake_ble_connection_manager_factory_;
FakeNearbyConnectionManagerFactory* fake_nearby_connection_manager_factory_;
FakePendingConnectionManager* instance_ = nullptr;
......@@ -447,9 +479,15 @@ class SecureChannelServiceTest : public testing::Test {
BleConnectionManagerImpl::Factory::SetFactoryForTesting(
fake_ble_connection_manager_factory_.get());
fake_nearby_connection_manager_factory_ =
std::make_unique<FakeNearbyConnectionManagerFactory>();
NearbyConnectionManagerImpl::Factory::SetFactoryForTesting(
fake_nearby_connection_manager_factory_.get());
fake_pending_connection_manager_factory_ =
std::make_unique<FakePendingConnectionManagerFactory>(
fake_ble_connection_manager_factory_.get());
fake_ble_connection_manager_factory_.get(),
fake_nearby_connection_manager_factory_.get());
PendingConnectionManagerImpl::Factory::SetFactoryForTesting(
fake_pending_connection_manager_factory_.get());
......@@ -481,6 +519,7 @@ class SecureChannelServiceTest : public testing::Test {
BleSynchronizer::Factory::SetFactoryForTesting(nullptr);
BleScannerImpl::Factory::SetFactoryForTesting(nullptr);
BleConnectionManagerImpl::Factory::SetFactoryForTesting(nullptr);
NearbyConnectionManagerImpl::Factory::SetFactoryForTesting(nullptr);
PendingConnectionManagerImpl::Factory::SetFactoryForTesting(nullptr);
ActiveConnectionManagerImpl::Factory::SetFactoryForTesting(nullptr);
SecureChannelInitializer::Factory::SetFactoryForTesting(nullptr);
......@@ -957,6 +996,8 @@ class SecureChannelServiceTest : public testing::Test {
std::unique_ptr<FakeBleScannerFactory> fake_ble_scanner_factory_;
std::unique_ptr<FakeBleConnectionManagerFactory>
fake_ble_connection_manager_factory_;
std::unique_ptr<FakeNearbyConnectionManagerFactory>
fake_nearby_connection_manager_factory_;
std::unique_ptr<FakePendingConnectionManagerFactory>
fake_pending_connection_manager_factory_;
std::unique_ptr<FakeActiveConnectionManagerFactory>
......
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