Commit 1f13d955 authored by Kyle Horimoto's avatar Kyle Horimoto Committed by Commit Bot

[CrOS PhoneHub] Refactor NearbyConnections to support multiple Cores

Each Core object is meant to be used by a single client, so we will use
one for Nearby Share and one for Phone Hub. Each Core instance uses the
same underlying connectivity framework, so we instantiate a single
OfflineServiceController and share it with all Cores.

This CL also adds a service_id parameter to each
mojom::NearbyConnections function so that we can decide which Core
object to use to process each call.

Bug: 1106937
Change-Id: Ibbf8378822862579b79321689dd00a61d204fb1c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2495878Reviewed-by: default avatarRyan Hansberry <hansberry@chromium.org>
Reviewed-by: default avatarTom Sepez <tsepez@chromium.org>
Reviewed-by: default avatarJames Vecore <vecore@google.com>
Commit-Queue: Kyle Horimoto <khorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#824962}
parent 875685e6
...@@ -98,7 +98,7 @@ void NearbyConnectionsManagerImpl::StartAdvertising( ...@@ -98,7 +98,7 @@ void NearbyConnectionsManagerImpl::StartAdvertising(
incoming_connection_listener_ = listener; incoming_connection_listener_ = listener;
nearby_connections_->StartAdvertising( nearby_connections_->StartAdvertising(
endpoint_info, kServiceId, kServiceId, endpoint_info,
AdvertisingOptions::New( AdvertisingOptions::New(
kStrategy, std::move(allowed_mediums), kStrategy, std::move(allowed_mediums),
/*auto_upgrade_bandwidth=*/is_high_power, /*auto_upgrade_bandwidth=*/is_high_power,
...@@ -112,7 +112,7 @@ void NearbyConnectionsManagerImpl::StartAdvertising( ...@@ -112,7 +112,7 @@ void NearbyConnectionsManagerImpl::StartAdvertising(
void NearbyConnectionsManagerImpl::StopAdvertising() { void NearbyConnectionsManagerImpl::StopAdvertising() {
if (nearby_connections_) { if (nearby_connections_) {
nearby_connections_->StopAdvertising( nearby_connections_->StopAdvertising(
base::BindOnce([](ConnectionsStatus status) { kServiceId, base::BindOnce([](ConnectionsStatus status) {
NS_LOG(VERBOSE) << __func__ NS_LOG(VERBOSE) << __func__
<< ": Stop advertising attempted over Nearby " << ": Stop advertising attempted over Nearby "
"Connections with result: " "Connections with result: "
...@@ -155,7 +155,7 @@ void NearbyConnectionsManagerImpl::StartDiscovery( ...@@ -155,7 +155,7 @@ void NearbyConnectionsManagerImpl::StartDiscovery(
void NearbyConnectionsManagerImpl::StopDiscovery() { void NearbyConnectionsManagerImpl::StopDiscovery() {
if (nearby_connections_) { if (nearby_connections_) {
nearby_connections_->StopDiscovery( nearby_connections_->StopDiscovery(
base::BindOnce([](ConnectionsStatus status) { kServiceId, base::BindOnce([](ConnectionsStatus status) {
NS_LOG(VERBOSE) << __func__ NS_LOG(VERBOSE) << __func__
<< ": Stop discovery attempted over Nearby " << ": Stop discovery attempted over Nearby "
"Connections with result: " "Connections with result: "
...@@ -203,7 +203,7 @@ void NearbyConnectionsManagerImpl::Connect( ...@@ -203,7 +203,7 @@ void NearbyConnectionsManagerImpl::Connect(
connect_timeout_timers_.emplace(endpoint_id, std::move(timeout_timer)); connect_timeout_timers_.emplace(endpoint_id, std::move(timeout_timer));
nearby_connections_->RequestConnection( nearby_connections_->RequestConnection(
endpoint_info, endpoint_id, kServiceId, endpoint_info, endpoint_id,
ConnectionOptions::New(std::move(allowed_mediums), ConnectionOptions::New(std::move(allowed_mediums),
std::move(bluetooth_mac_address)), std::move(bluetooth_mac_address)),
std::move(lifecycle_listener), std::move(lifecycle_listener),
...@@ -239,7 +239,7 @@ void NearbyConnectionsManagerImpl::Disconnect(const std::string& endpoint_id) { ...@@ -239,7 +239,7 @@ void NearbyConnectionsManagerImpl::Disconnect(const std::string& endpoint_id) {
return; return;
nearby_connections_->DisconnectFromEndpoint( nearby_connections_->DisconnectFromEndpoint(
endpoint_id, kServiceId, endpoint_id,
base::BindOnce( base::BindOnce(
[](const std::string& endpoint_id, ConnectionsStatus status) { [](const std::string& endpoint_id, ConnectionsStatus status) {
NS_LOG(VERBOSE) NS_LOG(VERBOSE)
...@@ -263,7 +263,7 @@ void NearbyConnectionsManagerImpl::Send(const std::string& endpoint_id, ...@@ -263,7 +263,7 @@ void NearbyConnectionsManagerImpl::Send(const std::string& endpoint_id,
RegisterPayloadStatusListener(payload->id, listener); RegisterPayloadStatusListener(payload->id, listener);
nearby_connections_->SendPayload( nearby_connections_->SendPayload(
{endpoint_id}, std::move(payload), kServiceId, {endpoint_id}, std::move(payload),
base::BindOnce( base::BindOnce(
[](const std::string& endpoint_id, ConnectionsStatus status) { [](const std::string& endpoint_id, ConnectionsStatus status) {
NS_LOG(VERBOSE) NS_LOG(VERBOSE)
...@@ -300,8 +300,8 @@ void NearbyConnectionsManagerImpl::OnFileCreated( ...@@ -300,8 +300,8 @@ void NearbyConnectionsManagerImpl::OnFileCreated(
ConnectionsCallback callback, ConnectionsCallback callback,
NearbyFileHandler::CreateFileResult result) { NearbyFileHandler::CreateFileResult result) {
nearby_connections_->RegisterPayloadFile( nearby_connections_->RegisterPayloadFile(
payload_id, std::move(result.input_file), std::move(result.output_file), kServiceId, payload_id, std::move(result.input_file),
std::move(callback)); std::move(result.output_file), std::move(callback));
} }
NearbyConnectionsManagerImpl::Payload* NearbyConnectionsManagerImpl::Payload*
...@@ -326,7 +326,7 @@ void NearbyConnectionsManagerImpl::Cancel(int64_t payload_id) { ...@@ -326,7 +326,7 @@ void NearbyConnectionsManagerImpl::Cancel(int64_t payload_id) {
payload_status_listeners_.erase(it); payload_status_listeners_.erase(it);
} }
nearby_connections_->CancelPayload( nearby_connections_->CancelPayload(
payload_id, kServiceId, payload_id,
base::BindOnce( base::BindOnce(
[](int64_t payload_id, ConnectionsStatus status) { [](int64_t payload_id, ConnectionsStatus status) {
NS_LOG(VERBOSE) NS_LOG(VERBOSE)
...@@ -367,7 +367,7 @@ void NearbyConnectionsManagerImpl::UpgradeBandwidth( ...@@ -367,7 +367,7 @@ void NearbyConnectionsManagerImpl::UpgradeBandwidth(
return; return;
nearby_connections_->InitiateBandwidthUpgrade( nearby_connections_->InitiateBandwidthUpgrade(
endpoint_id, kServiceId, endpoint_id,
base::BindOnce( base::BindOnce(
[](const std::string& endpoint_id, ConnectionsStatus status) { [](const std::string& endpoint_id, ConnectionsStatus status) {
NS_LOG(VERBOSE) NS_LOG(VERBOSE)
...@@ -452,7 +452,7 @@ void NearbyConnectionsManagerImpl::OnConnectionInitiated( ...@@ -452,7 +452,7 @@ void NearbyConnectionsManagerImpl::OnConnectionInitiated(
payload_listener.InitWithNewPipeAndPassReceiver()); payload_listener.InitWithNewPipeAndPassReceiver());
nearby_connections_->AcceptConnection( nearby_connections_->AcceptConnection(
endpoint_id, std::move(payload_listener), kServiceId, endpoint_id, std::move(payload_listener),
base::BindOnce( base::BindOnce(
[](const std::string& endpoint_id, ConnectionsStatus status) { [](const std::string& endpoint_id, ConnectionsStatus status) {
NS_LOG(VERBOSE) NS_LOG(VERBOSE)
...@@ -572,7 +572,8 @@ void NearbyConnectionsManagerImpl::OnPayloadTransferUpdate( ...@@ -572,7 +572,8 @@ void NearbyConnectionsManagerImpl::OnPayloadTransferUpdate(
if (!payload_it->second->content->is_bytes()) { if (!payload_it->second->content->is_bytes()) {
NS_LOG(WARNING) << "Received unknown payload of file type. Cancelling."; NS_LOG(WARNING) << "Received unknown payload of file type. Cancelling.";
nearby_connections_->CancelPayload(payload_it->first, base::DoNothing()); nearby_connections_->CancelPayload(kServiceId, payload_it->first,
base::DoNothing());
return; return;
} }
...@@ -599,7 +600,7 @@ bool NearbyConnectionsManagerImpl::BindNearbyConnections() { ...@@ -599,7 +600,7 @@ bool NearbyConnectionsManagerImpl::BindNearbyConnections() {
void NearbyConnectionsManagerImpl::Reset() { void NearbyConnectionsManagerImpl::Reset() {
if (nearby_connections_) { if (nearby_connections_) {
nearby_connections_->StopAllEndpoints( nearby_connections_->StopAllEndpoints(
base::BindOnce([](ConnectionsStatus status) { kServiceId, base::BindOnce([](ConnectionsStatus status) {
NS_LOG(VERBOSE) << __func__ NS_LOG(VERBOSE) << __func__
<< ": Stop all endpoints attempted over Nearby " << ": Stop all endpoints attempted over Nearby "
"Connections with result: " "Connections with result: "
......
...@@ -180,12 +180,13 @@ class NearbyConnectionsManagerImplTest : public testing::Test { ...@@ -180,12 +180,13 @@ class NearbyConnectionsManagerImplTest : public testing::Test {
std::end(kEndpointInfo)); std::end(kEndpointInfo));
EXPECT_CALL(nearby_connections_, StartAdvertising) EXPECT_CALL(nearby_connections_, StartAdvertising)
.WillOnce( .WillOnce(
[&](const std::vector<uint8_t>& endpoint_info, [&](const std::string& service_id,
const std::string& service_id, AdvertisingOptionsPtr options, const std::vector<uint8_t>& endpoint_info,
AdvertisingOptionsPtr options,
mojo::PendingRemote<ConnectionLifecycleListener> listener, mojo::PendingRemote<ConnectionLifecycleListener> listener,
NearbyConnectionsMojom::StartAdvertisingCallback callback) { NearbyConnectionsMojom::StartAdvertisingCallback callback) {
EXPECT_EQ(local_endpoint_info, endpoint_info);
EXPECT_EQ(kServiceId, service_id); EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(local_endpoint_info, endpoint_info);
EXPECT_EQ(kStrategy, options->strategy); EXPECT_EQ(kStrategy, options->strategy);
EXPECT_TRUE(options->enforce_topology_constraints); EXPECT_TRUE(options->enforce_topology_constraints);
...@@ -214,10 +215,12 @@ class NearbyConnectionsManagerImplTest : public testing::Test { ...@@ -214,10 +215,12 @@ class NearbyConnectionsManagerImplTest : public testing::Test {
EXPECT_CALL(nearby_connections_, RequestConnection) EXPECT_CALL(nearby_connections_, RequestConnection)
.WillOnce( .WillOnce(
[&](const std::vector<uint8_t>& endpoint_info, [&](const std::string& service_id,
const std::vector<uint8_t>& endpoint_info,
const std::string& endpoint_id, ConnectionOptionsPtr options, const std::string& endpoint_id, ConnectionOptionsPtr options,
mojo::PendingRemote<ConnectionLifecycleListener> listener, mojo::PendingRemote<ConnectionLifecycleListener> listener,
NearbyConnectionsMojom::RequestConnectionCallback callback) { NearbyConnectionsMojom::RequestConnectionCallback callback) {
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(local_endpoint_info, endpoint_info); EXPECT_EQ(local_endpoint_info, endpoint_info);
EXPECT_EQ(kRemoteEndpointId, endpoint_id); EXPECT_EQ(kRemoteEndpointId, endpoint_id);
...@@ -238,9 +241,10 @@ class NearbyConnectionsManagerImplTest : public testing::Test { ...@@ -238,9 +241,10 @@ class NearbyConnectionsManagerImplTest : public testing::Test {
base::RunLoop accept_run_loop; base::RunLoop accept_run_loop;
EXPECT_CALL(nearby_connections_, AcceptConnection) EXPECT_CALL(nearby_connections_, AcceptConnection)
.WillOnce( .WillOnce(
[&](const std::string& endpoint_id, [&](const std::string& service_id, const std::string& endpoint_id,
mojo::PendingRemote<PayloadListener> listener, mojo::PendingRemote<PayloadListener> listener,
NearbyConnectionsMojom::AcceptConnectionCallback callback) { NearbyConnectionsMojom::AcceptConnectionCallback callback) {
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(kRemoteEndpointId, endpoint_id); EXPECT_EQ(kRemoteEndpointId, endpoint_id);
payload_listener_remote.Bind(std::move(listener)); payload_listener_remote.Bind(std::move(listener));
...@@ -280,9 +284,10 @@ class NearbyConnectionsManagerImplTest : public testing::Test { ...@@ -280,9 +284,10 @@ class NearbyConnectionsManagerImplTest : public testing::Test {
base::RunLoop accept_run_loop; base::RunLoop accept_run_loop;
EXPECT_CALL(nearby_connections_, AcceptConnection) EXPECT_CALL(nearby_connections_, AcceptConnection)
.WillOnce( .WillOnce(
[&](const std::string& endpoint_id, [&](const std::string& service_id, const std::string& endpoint_id,
mojo::PendingRemote<PayloadListener> listener, mojo::PendingRemote<PayloadListener> listener,
NearbyConnectionsMojom::AcceptConnectionCallback callback) { NearbyConnectionsMojom::AcceptConnectionCallback callback) {
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(kRemoteEndpointId, endpoint_id); EXPECT_EQ(kRemoteEndpointId, endpoint_id);
payload_listener_remote.Bind(std::move(listener)); payload_listener_remote.Bind(std::move(listener));
...@@ -332,10 +337,12 @@ class NearbyConnectionsManagerImplTest : public testing::Test { ...@@ -332,10 +337,12 @@ class NearbyConnectionsManagerImplTest : public testing::Test {
base::RunLoop run_loop; base::RunLoop run_loop;
EXPECT_CALL(nearby_connections_, SendPayload) EXPECT_CALL(nearby_connections_, SendPayload)
.WillOnce([&](const std::vector<std::string>& endpoint_ids, .WillOnce([&](const std::string& service_id,
const std::vector<std::string>& endpoint_ids,
PayloadPtr payload, PayloadPtr payload,
NearbyConnectionsMojom::SendPayloadCallback callback) { NearbyConnectionsMojom::SendPayloadCallback callback) {
ASSERT_EQ(1u, endpoint_ids.size()); ASSERT_EQ(1u, endpoint_ids.size());
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(kRemoteEndpointId, endpoint_ids.front()); EXPECT_EQ(kRemoteEndpointId, endpoint_ids.front());
ASSERT_TRUE(payload); ASSERT_TRUE(payload);
ASSERT_EQ(PayloadContent::Tag::FILE, payload->content->which()); ASSERT_EQ(PayloadContent::Tag::FILE, payload->content->which());
...@@ -507,10 +514,12 @@ TEST_P(NearbyConnectionsManagerImplTestConnectionMediums, ...@@ -507,10 +514,12 @@ TEST_P(NearbyConnectionsManagerImplTestConnectionMediums,
std::end(kEndpointInfo)); std::end(kEndpointInfo));
EXPECT_CALL(nearby_connections_, RequestConnection) EXPECT_CALL(nearby_connections_, RequestConnection)
.WillOnce( .WillOnce(
[&](const std::vector<uint8_t>& endpoint_info, [&](const std::string& service_id,
const std::vector<uint8_t>& endpoint_info,
const std::string& endpoint_id, ConnectionOptionsPtr options, const std::string& endpoint_id, ConnectionOptionsPtr options,
mojo::PendingRemote<ConnectionLifecycleListener> listener, mojo::PendingRemote<ConnectionLifecycleListener> listener,
NearbyConnectionsMojom::RequestConnectionCallback callback) { NearbyConnectionsMojom::RequestConnectionCallback callback) {
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(local_endpoint_info, endpoint_info); EXPECT_EQ(local_endpoint_info, endpoint_info);
EXPECT_EQ(kRemoteEndpointId, endpoint_id); EXPECT_EQ(kRemoteEndpointId, endpoint_id);
EXPECT_EQ(expected_mediums, options->allowed_mediums); EXPECT_EQ(expected_mediums, options->allowed_mediums);
...@@ -570,10 +579,12 @@ TEST_P(NearbyConnectionsManagerImplTestConnectionBluetoothMacAddress, ...@@ -570,10 +579,12 @@ TEST_P(NearbyConnectionsManagerImplTestConnectionBluetoothMacAddress,
std::end(kEndpointInfo)); std::end(kEndpointInfo));
EXPECT_CALL(nearby_connections_, RequestConnection) EXPECT_CALL(nearby_connections_, RequestConnection)
.WillOnce( .WillOnce(
[&](const std::vector<uint8_t>& endpoint_info, [&](const std::string& service_id,
const std::vector<uint8_t>& endpoint_info,
const std::string& endpoint_id, ConnectionOptionsPtr options, const std::string& endpoint_id, ConnectionOptionsPtr options,
mojo::PendingRemote<ConnectionLifecycleListener> listener, mojo::PendingRemote<ConnectionLifecycleListener> listener,
NearbyConnectionsMojom::RequestConnectionCallback callback) { NearbyConnectionsMojom::RequestConnectionCallback callback) {
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(local_endpoint_info, endpoint_info); EXPECT_EQ(local_endpoint_info, endpoint_info);
EXPECT_EQ(kRemoteEndpointId, endpoint_id); EXPECT_EQ(kRemoteEndpointId, endpoint_id);
EXPECT_EQ(GetParam().expected_bluetooth_mac_address, EXPECT_EQ(GetParam().expected_bluetooth_mac_address,
...@@ -757,9 +768,11 @@ TEST_F(NearbyConnectionsManagerImplTest, ConnectWrite) { ...@@ -757,9 +768,11 @@ TEST_F(NearbyConnectionsManagerImplTest, ConnectWrite) {
base::RunLoop run_loop; base::RunLoop run_loop;
EXPECT_CALL(nearby_connections_, SendPayload) EXPECT_CALL(nearby_connections_, SendPayload)
.WillOnce([&](const std::vector<std::string>& endpoint_ids, .WillOnce([&](const std::string& service_id,
const std::vector<std::string>& endpoint_ids,
PayloadPtr payload, PayloadPtr payload,
NearbyConnectionsMojom::SendPayloadCallback callback) { NearbyConnectionsMojom::SendPayloadCallback callback) {
EXPECT_EQ(kServiceId, service_id);
ASSERT_EQ(1u, endpoint_ids.size()); ASSERT_EQ(1u, endpoint_ids.size());
EXPECT_EQ(kRemoteEndpointId, endpoint_ids.front()); EXPECT_EQ(kRemoteEndpointId, endpoint_ids.front());
ASSERT_TRUE(payload); ASSERT_TRUE(payload);
...@@ -801,8 +814,9 @@ TEST_F(NearbyConnectionsManagerImplTest, ConnectClosed) { ...@@ -801,8 +814,9 @@ TEST_F(NearbyConnectionsManagerImplTest, ConnectClosed) {
EXPECT_CALL(nearby_connections_, DisconnectFromEndpoint) EXPECT_CALL(nearby_connections_, DisconnectFromEndpoint)
.WillOnce( .WillOnce(
[&](const std::string& endpoint_id, [&](const std::string& service_id, const std::string& endpoint_id,
NearbyConnectionsMojom::DisconnectFromEndpointCallback callback) { NearbyConnectionsMojom::DisconnectFromEndpointCallback callback) {
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(kRemoteEndpointId, endpoint_id); EXPECT_EQ(kRemoteEndpointId, endpoint_id);
std::move(callback).Run(Status::kSuccess); std::move(callback).Run(Status::kSuccess);
}); });
...@@ -874,8 +888,9 @@ TEST_F(NearbyConnectionsManagerImplTest, ConnectClosedByClient) { ...@@ -874,8 +888,9 @@ TEST_F(NearbyConnectionsManagerImplTest, ConnectClosedByClient) {
EXPECT_CALL(nearby_connections_, DisconnectFromEndpoint) EXPECT_CALL(nearby_connections_, DisconnectFromEndpoint)
.WillOnce( .WillOnce(
[&](const std::string& endpoint_id, [&](const std::string& service_id, const std::string& endpoint_id,
NearbyConnectionsMojom::DisconnectFromEndpointCallback callback) { NearbyConnectionsMojom::DisconnectFromEndpointCallback callback) {
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(kRemoteEndpointId, endpoint_id); EXPECT_EQ(kRemoteEndpointId, endpoint_id);
std::move(callback).Run(Status::kSuccess); std::move(callback).Run(Status::kSuccess);
}); });
...@@ -934,8 +949,9 @@ TEST_F(NearbyConnectionsManagerImplTest, ConnectCancelPayload) { ...@@ -934,8 +949,9 @@ TEST_F(NearbyConnectionsManagerImplTest, ConnectCancelPayload) {
base::RunLoop cancel_run_loop; base::RunLoop cancel_run_loop;
EXPECT_CALL(nearby_connections_, CancelPayload) EXPECT_CALL(nearby_connections_, CancelPayload)
.WillOnce([&](int64_t payload_id, .WillOnce([&](const std::string& service_id, int64_t payload_id,
NearbyConnectionsMojom::CancelPayloadCallback callback) { NearbyConnectionsMojom::CancelPayloadCallback callback) {
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(kPayloadId, payload_id); EXPECT_EQ(kPayloadId, payload_id);
std::move(callback).Run(Status::kSuccess); std::move(callback).Run(Status::kSuccess);
...@@ -971,10 +987,12 @@ TEST_F(NearbyConnectionsManagerImplTest, ConnectTimeout) { ...@@ -971,10 +987,12 @@ TEST_F(NearbyConnectionsManagerImplTest, ConnectTimeout) {
NearbyConnectionsMojom::RequestConnectionCallback connect_callback; NearbyConnectionsMojom::RequestConnectionCallback connect_callback;
EXPECT_CALL(nearby_connections_, RequestConnection) EXPECT_CALL(nearby_connections_, RequestConnection)
.WillOnce( .WillOnce(
[&](const std::vector<uint8_t>& endpoint_info, [&](const std::string& service_id,
const std::vector<uint8_t>& endpoint_info,
const std::string& endpoint_id, ConnectionOptionsPtr options, const std::string& endpoint_id, ConnectionOptionsPtr options,
mojo::PendingRemote<ConnectionLifecycleListener> listener, mojo::PendingRemote<ConnectionLifecycleListener> listener,
NearbyConnectionsMojom::RequestConnectionCallback callback) { NearbyConnectionsMojom::RequestConnectionCallback callback) {
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(local_endpoint_info, endpoint_info); EXPECT_EQ(local_endpoint_info, endpoint_info);
EXPECT_EQ(kRemoteEndpointId, endpoint_id); EXPECT_EQ(kRemoteEndpointId, endpoint_id);
...@@ -986,8 +1004,9 @@ TEST_F(NearbyConnectionsManagerImplTest, ConnectTimeout) { ...@@ -986,8 +1004,9 @@ TEST_F(NearbyConnectionsManagerImplTest, ConnectTimeout) {
// Timing out should call disconnect. // Timing out should call disconnect.
EXPECT_CALL(nearby_connections_, DisconnectFromEndpoint) EXPECT_CALL(nearby_connections_, DisconnectFromEndpoint)
.WillOnce( .WillOnce(
[&](const std::string& endpoint_id, [&](const std::string& service_id, const std::string& endpoint_id,
NearbyConnectionsMojom::DisconnectFromEndpointCallback callback) { NearbyConnectionsMojom::DisconnectFromEndpointCallback callback) {
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(kRemoteEndpointId, endpoint_id); EXPECT_EQ(kRemoteEndpointId, endpoint_id);
std::move(callback).Run(Status::kSuccess); std::move(callback).Run(Status::kSuccess);
}); });
...@@ -1095,8 +1114,10 @@ TEST_F(NearbyConnectionsManagerImplTest, IncomingRegisterPayloadPath) { ...@@ -1095,8 +1114,10 @@ TEST_F(NearbyConnectionsManagerImplTest, IncomingRegisterPayloadPath) {
base::RunLoop register_payload_run_loop; base::RunLoop register_payload_run_loop;
EXPECT_CALL(nearby_connections_, RegisterPayloadFile) EXPECT_CALL(nearby_connections_, RegisterPayloadFile)
.WillOnce( .WillOnce(
[&](int64_t payload_id, base::File input_file, base::File output_file, [&](const std::string& service_id, int64_t payload_id,
base::File input_file, base::File output_file,
NearbyConnectionsMojom::RegisterPayloadFileCallback callback) { NearbyConnectionsMojom::RegisterPayloadFileCallback callback) {
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(kPayloadId, payload_id); EXPECT_EQ(kPayloadId, payload_id);
ASSERT_TRUE(input_file.IsValid()); ASSERT_TRUE(input_file.IsValid());
ASSERT_TRUE(output_file.IsValid()); ASSERT_TRUE(output_file.IsValid());
...@@ -1294,8 +1315,8 @@ TEST_P(NearbyConnectionsManagerImplTestMediums, StartAdvertising_Options) { ...@@ -1294,8 +1315,8 @@ TEST_P(NearbyConnectionsManagerImplTestMediums, StartAdvertising_Options) {
incoming_connection_listener; incoming_connection_listener;
EXPECT_CALL(nearby_connections_, StartAdvertising) EXPECT_CALL(nearby_connections_, StartAdvertising)
.WillOnce([&](const std::vector<uint8_t>& endpoint_info, .WillOnce([&](const std::string& service_id,
const std::string& service_id, const std::vector<uint8_t>& endpoint_info,
AdvertisingOptionsPtr options, AdvertisingOptionsPtr options,
mojo::PendingRemote<ConnectionLifecycleListener> listener, mojo::PendingRemote<ConnectionLifecycleListener> listener,
NearbyConnectionsMojom::StartAdvertisingCallback callback) { NearbyConnectionsMojom::StartAdvertisingCallback callback) {
...@@ -1392,9 +1413,11 @@ TEST_F(NearbyConnectionsManagerImplTest, ...@@ -1392,9 +1413,11 @@ TEST_F(NearbyConnectionsManagerImplTest,
// Upgrading bandwidth will succeed. // Upgrading bandwidth will succeed.
EXPECT_CALL(nearby_connections_, InitiateBandwidthUpgrade) EXPECT_CALL(nearby_connections_, InitiateBandwidthUpgrade)
.WillOnce([&](const std::string& endpoint_id, .WillOnce([&](const std::string& service_id,
const std::string& endpoint_id,
NearbyConnectionsMojom::InitiateBandwidthUpgradeCallback NearbyConnectionsMojom::InitiateBandwidthUpgradeCallback
callback) { callback) {
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(kRemoteEndpointId, endpoint_id); EXPECT_EQ(kRemoteEndpointId, endpoint_id);
std::move(callback).Run(Status::kSuccess); std::move(callback).Run(Status::kSuccess);
}); });
...@@ -1418,9 +1441,11 @@ TEST_F(NearbyConnectionsManagerImplTest, ...@@ -1418,9 +1441,11 @@ TEST_F(NearbyConnectionsManagerImplTest,
// Upgrading bandwidth will succeed. // Upgrading bandwidth will succeed.
EXPECT_CALL(nearby_connections_, InitiateBandwidthUpgrade) EXPECT_CALL(nearby_connections_, InitiateBandwidthUpgrade)
.WillOnce([&](const std::string& endpoint_id, .WillOnce([&](const std::string& service_id,
const std::string& endpoint_id,
NearbyConnectionsMojom::InitiateBandwidthUpgradeCallback NearbyConnectionsMojom::InitiateBandwidthUpgradeCallback
callback) { callback) {
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(kRemoteEndpointId, endpoint_id); EXPECT_EQ(kRemoteEndpointId, endpoint_id);
std::move(callback).Run(Status::kSuccess); std::move(callback).Run(Status::kSuccess);
}); });
......
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
#include "chrome/services/sharing/nearby/nearby_connections_conversions.h" #include "chrome/services/sharing/nearby/nearby_connections_conversions.h"
#include "chrome/services/sharing/nearby/platform/input_file.h" #include "chrome/services/sharing/nearby/platform/input_file.h"
#include "chromeos/services/nearby/public/mojom/nearby_connections_types.mojom.h" #include "chromeos/services/nearby/public/mojom/nearby_connections_types.mojom.h"
#include "third_party/nearby/src/cpp/core/core.h"
#include "third_party/nearby/src/cpp/core/internal/offline_service_controller.h"
namespace location { namespace location {
namespace nearby { namespace nearby {
...@@ -19,6 +21,94 @@ namespace connections { ...@@ -19,6 +21,94 @@ namespace connections {
namespace { namespace {
// Delegates all ServiceController calls to the ServiceController instance
// passed to its constructor. This proxy class is required because although we
// share one ServiceController among multiple Cores, each Core takes ownership
// of the pointer that it is provided. Using this proxy allows each Core to
// delete the pointer it is provided without deleting the shared instance.
class ServiceControllerProxy : public ServiceController {
public:
explicit ServiceControllerProxy(ServiceController* inner_service_controller)
: inner_service_controller_(inner_service_controller) {}
~ServiceControllerProxy() override = default;
// ServiceController:
Status StartAdvertising(ClientProxy* client,
const std::string& service_id,
const ConnectionOptions& options,
const ConnectionRequestInfo& info) override {
return inner_service_controller_->StartAdvertising(client, service_id,
options, info);
}
void StopAdvertising(ClientProxy* client) override {
inner_service_controller_->StopAdvertising(client);
}
Status StartDiscovery(ClientProxy* client,
const std::string& service_id,
const ConnectionOptions& options,
const DiscoveryListener& listener) override {
return inner_service_controller_->StartDiscovery(client, service_id,
options, listener);
}
void StopDiscovery(ClientProxy* client) override {
inner_service_controller_->StopDiscovery(client);
}
void InjectEndpoint(ClientProxy* client,
const std::string& service_id,
const OutOfBandConnectionMetadata& metadata) override {
inner_service_controller_->InjectEndpoint(client, service_id, metadata);
}
Status RequestConnection(ClientProxy* client,
const std::string& endpoint_id,
const ConnectionRequestInfo& info,
const ConnectionOptions& options) override {
return inner_service_controller_->RequestConnection(client, endpoint_id,
info, options);
}
Status AcceptConnection(ClientProxy* client,
const std::string& endpoint_id,
const PayloadListener& listener) override {
return inner_service_controller_->AcceptConnection(client, endpoint_id,
listener);
}
Status RejectConnection(ClientProxy* client,
const std::string& endpoint_id) override {
return inner_service_controller_->RejectConnection(client, endpoint_id);
}
void InitiateBandwidthUpgrade(ClientProxy* client,
const std::string& endpoint_id) override {
inner_service_controller_->InitiateBandwidthUpgrade(client, endpoint_id);
}
void SendPayload(ClientProxy* client,
const std::vector<std::string>& endpoint_ids,
Payload payload) override {
inner_service_controller_->SendPayload(client, endpoint_ids,
std::move(payload));
}
Status CancelPayload(ClientProxy* client, Payload::Id payload_id) override {
return inner_service_controller_->CancelPayload(client,
std::move(payload_id));
}
void DisconnectFromEndpoint(ClientProxy* client,
const std::string& endpoint_id) override {
inner_service_controller_->DisconnectFromEndpoint(client, endpoint_id);
}
private:
ServiceController* inner_service_controller_ = nullptr;
};
ConnectionRequestInfo CreateConnectionRequestInfo( ConnectionRequestInfo CreateConnectionRequestInfo(
const std::vector<uint8_t>& endpoint_info, const std::vector<uint8_t>& endpoint_info,
mojo::PendingRemote<mojom::ConnectionLifecycleListener> listener) { mojo::PendingRemote<mojom::ConnectionLifecycleListener> listener) {
...@@ -90,11 +180,24 @@ NearbyConnections::NearbyConnections( ...@@ -90,11 +180,24 @@ NearbyConnections::NearbyConnections(
mojom::NearbyConnectionsDependenciesPtr dependencies, mojom::NearbyConnectionsDependenciesPtr dependencies,
scoped_refptr<base::SequencedTaskRunner> io_task_runner, scoped_refptr<base::SequencedTaskRunner> io_task_runner,
base::OnceClosure on_disconnect, base::OnceClosure on_disconnect,
std::unique_ptr<Core> core) std::unique_ptr<ServiceController> service_controller)
: nearby_connections_(this, std::move(nearby_connections)), : nearby_connections_(this, std::move(nearby_connections)),
on_disconnect_(std::move(on_disconnect)), on_disconnect_(std::move(on_disconnect)),
core_(std::move(core)), service_controller_(std::move(service_controller)),
thread_task_runner_(base::ThreadTaskRunnerHandle::Get()) { thread_task_runner_(base::ThreadTaskRunnerHandle::Get()) {
// Note: Some tests pass a value for |service_controller_|, but this value is
// expected to be null during normal operation.
if (!service_controller_) {
// Post a task which initializes |service_controller_|. This must be posted
// as a task instead of being completed synchrnously here because
// OfflineServiceController invokes NearbyConnections::GetInstance(), which
// requires that the instance be initialized by completing this constructor.
thread_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&NearbyConnections::InitializeOfflineServiceController,
weak_ptr_factory_.GetWeakPtr()));
}
nearby_connections_.set_disconnect_handler(base::BindOnce( nearby_connections_.set_disconnect_handler(base::BindOnce(
&NearbyConnections::OnDisconnect, weak_ptr_factory_.GetWeakPtr())); &NearbyConnections::OnDisconnect, weak_ptr_factory_.GetWeakPtr()));
...@@ -144,10 +247,17 @@ NearbyConnections::NearbyConnections( ...@@ -144,10 +247,17 @@ NearbyConnections::NearbyConnections(
} }
NearbyConnections::~NearbyConnections() { NearbyConnections::~NearbyConnections() {
core_.reset(); // Note that deleting active Core objects invokes their shutdown flows. This
// is required to ensure that Nearby cleans itself up.
service_id_to_core_map_.clear();
g_instance = nullptr; g_instance = nullptr;
} }
void NearbyConnections::InitializeOfflineServiceController() {
DCHECK(!service_controller_);
service_controller_ = std::make_unique<OfflineServiceController>();
}
void NearbyConnections::OnDisconnect() { void NearbyConnections::OnDisconnect() {
if (on_disconnect_) if (on_disconnect_)
std::move(on_disconnect_).Run(); std::move(on_disconnect_).Run();
...@@ -155,8 +265,8 @@ void NearbyConnections::OnDisconnect() { ...@@ -155,8 +265,8 @@ void NearbyConnections::OnDisconnect() {
} }
void NearbyConnections::StartAdvertising( void NearbyConnections::StartAdvertising(
const std::vector<uint8_t>& endpoint_info,
const std::string& service_id, const std::string& service_id,
const std::vector<uint8_t>& endpoint_info,
mojom::AdvertisingOptionsPtr options, mojom::AdvertisingOptionsPtr options,
mojo::PendingRemote<mojom::ConnectionLifecycleListener> listener, mojo::PendingRemote<mojom::ConnectionLifecycleListener> listener,
StartAdvertisingCallback callback) { StartAdvertisingCallback callback) {
...@@ -169,14 +279,17 @@ void NearbyConnections::StartAdvertising( ...@@ -169,14 +279,17 @@ void NearbyConnections::StartAdvertising(
.fast_advertisement_service_uuid = .fast_advertisement_service_uuid =
options->fast_advertisement_service_uuid.canonical_value()}; options->fast_advertisement_service_uuid.canonical_value()};
core_->StartAdvertising( GetCore(service_id)
service_id, std::move(connection_options), ->StartAdvertising(
CreateConnectionRequestInfo(endpoint_info, std::move(listener)), service_id, std::move(connection_options),
ResultCallbackFromMojom(std::move(callback))); CreateConnectionRequestInfo(endpoint_info, std::move(listener)),
ResultCallbackFromMojom(std::move(callback)));
} }
void NearbyConnections::StopAdvertising(StopAdvertisingCallback callback) { void NearbyConnections::StopAdvertising(const std::string& service_id,
core_->StopAdvertising(ResultCallbackFromMojom(std::move(callback))); StopAdvertisingCallback callback) {
GetCore(service_id)
->StopAdvertising(ResultCallbackFromMojom(std::move(callback)));
} }
void NearbyConnections::StartDiscovery( void NearbyConnections::StartDiscovery(
...@@ -214,16 +327,20 @@ void NearbyConnections::StartDiscovery( ...@@ -214,16 +327,20 @@ void NearbyConnections::StartDiscovery(
}; };
ResultCallback result_callback = ResultCallbackFromMojom(std::move(callback)); ResultCallback result_callback = ResultCallbackFromMojom(std::move(callback));
core_->StartDiscovery(service_id, std::move(connection_options), GetCore(service_id)
std::move(discovery_listener), ->StartDiscovery(service_id, std::move(connection_options),
std::move(result_callback)); std::move(discovery_listener),
std::move(result_callback));
} }
void NearbyConnections::StopDiscovery(StopDiscoveryCallback callback) { void NearbyConnections::StopDiscovery(const std::string& service_id,
core_->StopDiscovery(ResultCallbackFromMojom(std::move(callback))); StopDiscoveryCallback callback) {
GetCore(service_id)
->StopDiscovery(ResultCallbackFromMojom(std::move(callback)));
} }
void NearbyConnections::RequestConnection( void NearbyConnections::RequestConnection(
const std::string& service_id,
const std::vector<uint8_t>& endpoint_info, const std::vector<uint8_t>& endpoint_info,
const std::string& endpoint_id, const std::string& endpoint_id,
mojom::ConnectionOptionsPtr options, mojom::ConnectionOptionsPtr options,
...@@ -235,21 +352,25 @@ void NearbyConnections::RequestConnection( ...@@ -235,21 +352,25 @@ void NearbyConnections::RequestConnection(
connection_options.remote_bluetooth_mac_address = connection_options.remote_bluetooth_mac_address =
ByteArrayFromMojom(*options->remote_bluetooth_mac_address); ByteArrayFromMojom(*options->remote_bluetooth_mac_address);
} }
core_->RequestConnection( GetCore(service_id)
endpoint_id, ->RequestConnection(
CreateConnectionRequestInfo(endpoint_info, std::move(listener)), endpoint_id,
std::move(connection_options), CreateConnectionRequestInfo(endpoint_info, std::move(listener)),
ResultCallbackFromMojom(std::move(callback))); std::move(connection_options),
ResultCallbackFromMojom(std::move(callback)));
} }
void NearbyConnections::DisconnectFromEndpoint( void NearbyConnections::DisconnectFromEndpoint(
const std::string& service_id,
const std::string& endpoint_id, const std::string& endpoint_id,
DisconnectFromEndpointCallback callback) { DisconnectFromEndpointCallback callback) {
core_->DisconnectFromEndpoint(endpoint_id, GetCore(service_id)
ResultCallbackFromMojom(std::move(callback))); ->DisconnectFromEndpoint(endpoint_id,
ResultCallbackFromMojom(std::move(callback)));
} }
void NearbyConnections::AcceptConnection( void NearbyConnections::AcceptConnection(
const std::string& service_id,
const std::string& endpoint_id, const std::string& endpoint_id,
mojo::PendingRemote<mojom::PayloadListener> listener, mojo::PendingRemote<mojom::PayloadListener> listener,
AcceptConnectionCallback callback) { AcceptConnectionCallback callback) {
...@@ -257,8 +378,8 @@ void NearbyConnections::AcceptConnection( ...@@ -257,8 +378,8 @@ void NearbyConnections::AcceptConnection(
// Capturing Core* is safe as Core owns PayloadListener. // Capturing Core* is safe as Core owns PayloadListener.
PayloadListener payload_listener = { PayloadListener payload_listener = {
.payload_cb = .payload_cb =
[remote, core = core_.get()](const std::string& endpoint_id, [remote, core = GetCore(service_id)](const std::string& endpoint_id,
Payload payload) { Payload payload) {
if (!remote) if (!remote)
return; return;
...@@ -315,17 +436,21 @@ void NearbyConnections::AcceptConnection( ...@@ -315,17 +436,21 @@ void NearbyConnections::AcceptConnection(
info.total_bytes, info.bytes_transferred)); info.total_bytes, info.bytes_transferred));
}}; }};
core_->AcceptConnection(endpoint_id, std::move(payload_listener), GetCore(service_id)
ResultCallbackFromMojom(std::move(callback))); ->AcceptConnection(endpoint_id, std::move(payload_listener),
ResultCallbackFromMojom(std::move(callback)));
} }
void NearbyConnections::RejectConnection(const std::string& endpoint_id, void NearbyConnections::RejectConnection(const std::string& service_id,
const std::string& endpoint_id,
RejectConnectionCallback callback) { RejectConnectionCallback callback) {
core_->RejectConnection(endpoint_id, GetCore(service_id)
ResultCallbackFromMojom(std::move(callback))); ->RejectConnection(endpoint_id,
ResultCallbackFromMojom(std::move(callback)));
} }
void NearbyConnections::SendPayload( void NearbyConnections::SendPayload(
const std::string& service_id,
const std::vector<std::string>& endpoint_ids, const std::vector<std::string>& endpoint_ids,
mojom::PayloadPtr payload, mojom::PayloadPtr payload,
SendPayloadCallback callback) { SendPayloadCallback callback) {
...@@ -347,28 +472,35 @@ void NearbyConnections::SendPayload( ...@@ -347,28 +472,35 @@ void NearbyConnections::SendPayload(
break; break;
} }
core_->SendPayload(absl::MakeSpan(endpoint_ids), std::move(core_payload), GetCore(service_id)
ResultCallbackFromMojom(std::move(callback))); ->SendPayload(absl::MakeSpan(endpoint_ids), std::move(core_payload),
ResultCallbackFromMojom(std::move(callback)));
} }
void NearbyConnections::CancelPayload(int64_t payload_id, void NearbyConnections::CancelPayload(const std::string& service_id,
int64_t payload_id,
CancelPayloadCallback callback) { CancelPayloadCallback callback) {
core_->CancelPayload(payload_id, GetCore(service_id)
ResultCallbackFromMojom(std::move(callback))); ->CancelPayload(payload_id, ResultCallbackFromMojom(std::move(callback)));
} }
void NearbyConnections::StopAllEndpoints(StopAllEndpointsCallback callback) { void NearbyConnections::StopAllEndpoints(const std::string& service_id,
core_->StopAllEndpoints(ResultCallbackFromMojom(std::move(callback))); StopAllEndpointsCallback callback) {
GetCore(service_id)
->StopAllEndpoints(ResultCallbackFromMojom(std::move(callback)));
} }
void NearbyConnections::InitiateBandwidthUpgrade( void NearbyConnections::InitiateBandwidthUpgrade(
const std::string& service_id,
const std::string& endpoint_id, const std::string& endpoint_id,
InitiateBandwidthUpgradeCallback callback) { InitiateBandwidthUpgradeCallback callback) {
core_->InitiateBandwidthUpgrade(endpoint_id, GetCore(service_id)
ResultCallbackFromMojom(std::move(callback))); ->InitiateBandwidthUpgrade(endpoint_id,
ResultCallbackFromMojom(std::move(callback)));
} }
void NearbyConnections::RegisterPayloadFile( void NearbyConnections::RegisterPayloadFile(
const std::string& service_id,
int64_t payload_id, int64_t payload_id,
base::File input_file, base::File input_file,
base::File output_file, base::File output_file,
...@@ -418,6 +550,21 @@ NearbyConnections::GetThreadTaskRunner() { ...@@ -418,6 +550,21 @@ NearbyConnections::GetThreadTaskRunner() {
return thread_task_runner_; return thread_task_runner_;
} }
Core* NearbyConnections::GetCore(const std::string& service_id) {
std::unique_ptr<Core>& core = service_id_to_core_map_[service_id];
if (!core) {
core = std::make_unique<Core>([&]() {
// Core expects to take ownership of the pointer provided, but since we
// share a single ServiceController among all Core objects created, we
// provide a delegate which calls into our shared instance.
return new ServiceControllerProxy(service_controller_.get());
});
}
return core.get();
}
} // namespace connections } // namespace connections
} // namespace nearby } // namespace nearby
} // namespace location } // namespace location
...@@ -26,12 +26,14 @@ ...@@ -26,12 +26,14 @@
#include "mojo/public/cpp/bindings/pending_remote.h" #include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h" #include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/shared_remote.h" #include "mojo/public/cpp/bindings/shared_remote.h"
#include "third_party/nearby/src/cpp/core/core.h" #include "third_party/nearby/src/cpp/core/internal/service_controller.h"
namespace location { namespace location {
namespace nearby { namespace nearby {
namespace connections { namespace connections {
class Core;
// Implementation of the NearbyConnections mojo interface. // Implementation of the NearbyConnections mojo interface.
// This class acts as a bridge to the NearbyConnections library which is pulled // This class acts as a bridge to the NearbyConnections library which is pulled
// in as a third_party dependency. It handles the translation from mojo calls to // in as a third_party dependency. It handles the translation from mojo calls to
...@@ -50,7 +52,7 @@ class NearbyConnections : public mojom::NearbyConnections { ...@@ -50,7 +52,7 @@ class NearbyConnections : public mojom::NearbyConnections {
mojom::NearbyConnectionsDependenciesPtr dependencies, mojom::NearbyConnectionsDependenciesPtr dependencies,
scoped_refptr<base::SequencedTaskRunner> io_task_runner, scoped_refptr<base::SequencedTaskRunner> io_task_runner,
base::OnceClosure on_disconnect, base::OnceClosure on_disconnect,
std::unique_ptr<Core> core = std::make_unique<Core>()); std::unique_ptr<ServiceController> service_controller = nullptr);
NearbyConnections(const NearbyConnections&) = delete; NearbyConnections(const NearbyConnections&) = delete;
NearbyConnections& operator=(const NearbyConnections&) = delete; NearbyConnections& operator=(const NearbyConnections&) = delete;
...@@ -85,41 +87,52 @@ class NearbyConnections : public mojom::NearbyConnections { ...@@ -85,41 +87,52 @@ class NearbyConnections : public mojom::NearbyConnections {
// mojom::NearbyConnections: // mojom::NearbyConnections:
void StartAdvertising( void StartAdvertising(
const std::vector<uint8_t>& endpoint_info,
const std::string& service_id, const std::string& service_id,
const std::vector<uint8_t>& endpoint_info,
mojom::AdvertisingOptionsPtr options, mojom::AdvertisingOptionsPtr options,
mojo::PendingRemote<mojom::ConnectionLifecycleListener> listener, mojo::PendingRemote<mojom::ConnectionLifecycleListener> listener,
StartAdvertisingCallback callback) override; StartAdvertisingCallback callback) override;
void StopAdvertising(StopAdvertisingCallback callback) override; void StopAdvertising(const std::string& service_id,
StopAdvertisingCallback callback) override;
void StartDiscovery( void StartDiscovery(
const std::string& service_id, const std::string& service_id,
mojom::DiscoveryOptionsPtr options, mojom::DiscoveryOptionsPtr options,
mojo::PendingRemote<mojom::EndpointDiscoveryListener> listener, mojo::PendingRemote<mojom::EndpointDiscoveryListener> listener,
StartDiscoveryCallback callback) override; StartDiscoveryCallback callback) override;
void StopDiscovery(StopDiscoveryCallback callback) override; void StopDiscovery(const std::string& service_id,
StopDiscoveryCallback callback) override;
void RequestConnection( void RequestConnection(
const std::string& service_id,
const std::vector<uint8_t>& endpoint_info, const std::vector<uint8_t>& endpoint_info,
const std::string& endpoint_id, const std::string& endpoint_id,
mojom::ConnectionOptionsPtr options, mojom::ConnectionOptionsPtr options,
mojo::PendingRemote<mojom::ConnectionLifecycleListener> listener, mojo::PendingRemote<mojom::ConnectionLifecycleListener> listener,
RequestConnectionCallback callback) override; RequestConnectionCallback callback) override;
void DisconnectFromEndpoint(const std::string& endpoint_id, void DisconnectFromEndpoint(const std::string& service_id,
const std::string& endpoint_id,
DisconnectFromEndpointCallback callback) override; DisconnectFromEndpointCallback callback) override;
void AcceptConnection(const std::string& endpoint_id, void AcceptConnection(const std::string& service_id,
const std::string& endpoint_id,
mojo::PendingRemote<mojom::PayloadListener> listener, mojo::PendingRemote<mojom::PayloadListener> listener,
AcceptConnectionCallback callback) override; AcceptConnectionCallback callback) override;
void RejectConnection(const std::string& endpoint_id, void RejectConnection(const std::string& service_id,
const std::string& endpoint_id,
RejectConnectionCallback callback) override; RejectConnectionCallback callback) override;
void SendPayload(const std::vector<std::string>& endpoint_ids, void SendPayload(const std::string& service_id,
const std::vector<std::string>& endpoint_ids,
mojom::PayloadPtr payload, mojom::PayloadPtr payload,
SendPayloadCallback callback) override; SendPayloadCallback callback) override;
void CancelPayload(int64_t payload_id, void CancelPayload(const std::string& service_id,
int64_t payload_id,
CancelPayloadCallback callback) override; CancelPayloadCallback callback) override;
void StopAllEndpoints(StopAllEndpointsCallback callback) override; void StopAllEndpoints(const std::string& service_id,
StopAllEndpointsCallback callback) override;
void InitiateBandwidthUpgrade( void InitiateBandwidthUpgrade(
const std::string& service_id,
const std::string& endpoint_id, const std::string& endpoint_id,
InitiateBandwidthUpgradeCallback callback) override; InitiateBandwidthUpgradeCallback callback) override;
void RegisterPayloadFile(int64_t payload_id, void RegisterPayloadFile(const std::string& service_id,
int64_t payload_id,
base::File input_file, base::File input_file,
base::File output_file, base::File output_file,
RegisterPayloadFileCallback callback) override; RegisterPayloadFileCallback callback) override;
...@@ -134,6 +147,9 @@ class NearbyConnections : public mojom::NearbyConnections { ...@@ -134,6 +147,9 @@ class NearbyConnections : public mojom::NearbyConnections {
scoped_refptr<base::SingleThreadTaskRunner> GetThreadTaskRunner(); scoped_refptr<base::SingleThreadTaskRunner> GetThreadTaskRunner();
private: private:
Core* GetCore(const std::string& service_id);
void InitializeOfflineServiceController();
void OnDisconnect(); void OnDisconnect();
mojo::Receiver<mojom::NearbyConnections> nearby_connections_; mojo::Receiver<mojom::NearbyConnections> nearby_connections_;
...@@ -148,9 +164,12 @@ class NearbyConnections : public mojom::NearbyConnections { ...@@ -148,9 +164,12 @@ class NearbyConnections : public mojom::NearbyConnections {
mojo::SharedRemote<sharing::mojom::WebRtcSignalingMessenger> mojo::SharedRemote<sharing::mojom::WebRtcSignalingMessenger>
webrtc_signaling_messenger_; webrtc_signaling_messenger_;
// Core is thread-safe as its operations are always dispatched to a std::unique_ptr<ServiceController> service_controller_;
// single-thread executor.
std::unique_ptr<Core> core_; // Map from service ID to the Core object to be used for that service. Each
// service uses its own Core object, but all Core objects share the underlying
// ServiceController instance.
base::flat_map<std::string, std::unique_ptr<Core>> service_id_to_core_map_;
// input_file_map_ is accessed from background threads. // input_file_map_ is accessed from background threads.
base::Lock input_file_lock_; base::Lock input_file_lock_;
......
...@@ -186,16 +186,15 @@ class NearbyConnectionsTest : public testing::Test { ...@@ -186,16 +186,15 @@ class NearbyConnectionsTest : public testing::Test {
auto dependencies = mojom::NearbyConnectionsDependencies::New( auto dependencies = mojom::NearbyConnectionsDependencies::New(
bluetooth_adapter_.adapter_.BindNewPipeAndPassRemote(), bluetooth_adapter_.adapter_.BindNewPipeAndPassRemote(),
std::move(webrtc_dependencies)); std::move(webrtc_dependencies));
service_controller_ = auto service_controller =
std::make_unique<testing::NiceMock<MockServiceController>>(); std::make_unique<testing::NiceMock<MockServiceController>>();
service_controller_ptr_ = service_controller_.get(); service_controller_ptr_ = service_controller.get();
nearby_connections_ = std::make_unique<NearbyConnections>( nearby_connections_ = std::make_unique<NearbyConnections>(
remote_.BindNewPipeAndPassReceiver(), std::move(dependencies), remote_.BindNewPipeAndPassReceiver(), std::move(dependencies),
/*io_task_runner=*/nullptr, /*io_task_runner=*/nullptr,
base::BindOnce(&NearbyConnectionsTest::OnDisconnect, base::BindOnce(&NearbyConnectionsTest::OnDisconnect,
base::Unretained(this)), base::Unretained(this)),
std::make_unique<Core>( std::move(service_controller));
[&]() { return service_controller_.release(); }));
} }
void OnDisconnect() { disconnect_run_loop_.Quit(); } void OnDisconnect() { disconnect_run_loop_.Quit(); }
...@@ -278,7 +277,7 @@ class NearbyConnectionsTest : public testing::Test { ...@@ -278,7 +277,7 @@ class NearbyConnectionsTest : public testing::Test {
base::RunLoop start_advertising_run_loop; base::RunLoop start_advertising_run_loop;
nearby_connections_->StartAdvertising( nearby_connections_->StartAdvertising(
endpoint_info, kServiceId, CreateAdvertisingOptions(), kServiceId, endpoint_info, CreateAdvertisingOptions(),
fake_connection_life_cycle_listener.receiver.BindNewPipeAndPassRemote(), fake_connection_life_cycle_listener.receiver.BindNewPipeAndPassRemote(),
base::BindLambdaForTesting([&](mojom::Status status) { base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kSuccess, status); EXPECT_EQ(mojom::Status::kSuccess, status);
...@@ -328,7 +327,7 @@ class NearbyConnectionsTest : public testing::Test { ...@@ -328,7 +327,7 @@ class NearbyConnectionsTest : public testing::Test {
base::RunLoop request_connection_run_loop; base::RunLoop request_connection_run_loop;
nearby_connections_->RequestConnection( nearby_connections_->RequestConnection(
endpoint_info, endpoint_data.remote_endpoint_id, kServiceId, endpoint_info, endpoint_data.remote_endpoint_id,
CreateConnectionOptions(bluetooth_mac_address), CreateConnectionOptions(bluetooth_mac_address),
fake_connection_life_cycle_listener.receiver.BindNewPipeAndPassRemote(), fake_connection_life_cycle_listener.receiver.BindNewPipeAndPassRemote(),
base::BindLambdaForTesting([&](mojom::Status status) { base::BindLambdaForTesting([&](mojom::Status status) {
...@@ -356,7 +355,7 @@ class NearbyConnectionsTest : public testing::Test { ...@@ -356,7 +355,7 @@ class NearbyConnectionsTest : public testing::Test {
base::RunLoop accept_connection_run_loop; base::RunLoop accept_connection_run_loop;
nearby_connections_->AcceptConnection( nearby_connections_->AcceptConnection(
remote_endpoint_id, kServiceId, remote_endpoint_id,
fake_payload_listener.receiver.BindNewPipeAndPassRemote(), fake_payload_listener.receiver.BindNewPipeAndPassRemote(),
base::BindLambdaForTesting([&](mojom::Status status) { base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kSuccess, status); EXPECT_EQ(mojom::Status::kSuccess, status);
...@@ -375,9 +374,6 @@ class NearbyConnectionsTest : public testing::Test { ...@@ -375,9 +374,6 @@ class NearbyConnectionsTest : public testing::Test {
std::unique_ptr<NearbyConnections> nearby_connections_; std::unique_ptr<NearbyConnections> nearby_connections_;
testing::NiceMock<MockServiceController>* service_controller_ptr_; testing::NiceMock<MockServiceController>* service_controller_ptr_;
base::RunLoop disconnect_run_loop_; base::RunLoop disconnect_run_loop_;
private:
std::unique_ptr<testing::NiceMock<MockServiceController>> service_controller_;
}; };
TEST_F(NearbyConnectionsTest, RemoteDisconnect) { TEST_F(NearbyConnectionsTest, RemoteDisconnect) {
...@@ -449,7 +445,7 @@ TEST_F(NearbyConnectionsTest, StopDiscovery) { ...@@ -449,7 +445,7 @@ TEST_F(NearbyConnectionsTest, StopDiscovery) {
base::RunLoop stop_discovery_run_loop; base::RunLoop stop_discovery_run_loop;
nearby_connections_->StopDiscovery( nearby_connections_->StopDiscovery(
base::BindLambdaForTesting([&](mojom::Status status) { kServiceId, base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kSuccess, status); EXPECT_EQ(mojom::Status::kSuccess, status);
stop_discovery_run_loop.Quit(); stop_discovery_run_loop.Quit();
})); }));
...@@ -588,7 +584,7 @@ TEST_F(NearbyConnectionsTest, RequestConnectionOnBandwidthUpgrade) { ...@@ -588,7 +584,7 @@ TEST_F(NearbyConnectionsTest, RequestConnectionOnBandwidthUpgrade) {
}); });
base::RunLoop bandwidth_upgrade_run_loop; base::RunLoop bandwidth_upgrade_run_loop;
nearby_connections_->InitiateBandwidthUpgrade( nearby_connections_->InitiateBandwidthUpgrade(
endpoint_data.remote_endpoint_id, kServiceId, endpoint_data.remote_endpoint_id,
base::BindLambdaForTesting([&](mojom::Status status) { base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kSuccess, status); EXPECT_EQ(mojom::Status::kSuccess, status);
bandwidth_upgrade_run_loop.Quit(); bandwidth_upgrade_run_loop.Quit();
...@@ -657,7 +653,7 @@ TEST_F(NearbyConnectionsTest, RequestConnectionDisconnect) { ...@@ -657,7 +653,7 @@ TEST_F(NearbyConnectionsTest, RequestConnectionDisconnect) {
base::RunLoop disconnect_from_endpoint_run_loop; base::RunLoop disconnect_from_endpoint_run_loop;
nearby_connections_->DisconnectFromEndpoint( nearby_connections_->DisconnectFromEndpoint(
endpoint_data.remote_endpoint_id, kServiceId, endpoint_data.remote_endpoint_id,
base::BindLambdaForTesting([&](mojom::Status status) { base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kSuccess, status); EXPECT_EQ(mojom::Status::kSuccess, status);
disconnect_from_endpoint_run_loop.Quit(); disconnect_from_endpoint_run_loop.Quit();
...@@ -725,7 +721,7 @@ TEST_F(NearbyConnectionsTest, SendBytesPayload) { ...@@ -725,7 +721,7 @@ TEST_F(NearbyConnectionsTest, SendBytesPayload) {
base::RunLoop send_payload_run_loop; base::RunLoop send_payload_run_loop;
nearby_connections_->SendPayload( nearby_connections_->SendPayload(
{endpoint_data.remote_endpoint_id}, kServiceId, {endpoint_data.remote_endpoint_id},
mojom::Payload::New(kPayloadId, mojom::Payload::New(kPayloadId,
mojom::PayloadContent::NewBytes( mojom::PayloadContent::NewBytes(
mojom::BytesPayload::New(expected_payload))), mojom::BytesPayload::New(expected_payload))),
...@@ -769,7 +765,7 @@ TEST_F(NearbyConnectionsTest, SendBytesPayloadCancelled) { ...@@ -769,7 +765,7 @@ TEST_F(NearbyConnectionsTest, SendBytesPayloadCancelled) {
base::RunLoop send_payload_run_loop; base::RunLoop send_payload_run_loop;
nearby_connections_->SendPayload( nearby_connections_->SendPayload(
{endpoint_data.remote_endpoint_id}, kServiceId, {endpoint_data.remote_endpoint_id},
mojom::Payload::New(kPayloadId, mojom::Payload::New(kPayloadId,
mojom::PayloadContent::NewBytes( mojom::PayloadContent::NewBytes(
mojom::BytesPayload::New(expected_payload))), mojom::BytesPayload::New(expected_payload))),
...@@ -785,7 +781,8 @@ TEST_F(NearbyConnectionsTest, SendBytesPayloadCancelled) { ...@@ -785,7 +781,8 @@ TEST_F(NearbyConnectionsTest, SendBytesPayloadCancelled) {
base::RunLoop cancel_payload_run_loop; base::RunLoop cancel_payload_run_loop;
nearby_connections_->CancelPayload( nearby_connections_->CancelPayload(
kPayloadId, base::BindLambdaForTesting([&](mojom::Status status) { kServiceId, kPayloadId,
base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kSuccess, status); EXPECT_EQ(mojom::Status::kSuccess, status);
cancel_payload_run_loop.Quit(); cancel_payload_run_loop.Quit();
})); }));
...@@ -840,7 +837,7 @@ TEST_F(NearbyConnectionsTest, SendFilePayload) { ...@@ -840,7 +837,7 @@ TEST_F(NearbyConnectionsTest, SendFilePayload) {
base::RunLoop send_payload_run_loop; base::RunLoop send_payload_run_loop;
nearby_connections_->SendPayload( nearby_connections_->SendPayload(
{endpoint_data.remote_endpoint_id}, kServiceId, {endpoint_data.remote_endpoint_id},
mojom::Payload::New(kPayloadId, mojom::Payload::New(kPayloadId,
mojom::PayloadContent::NewFile( mojom::PayloadContent::NewFile(
mojom::FilePayload::New(std::move(input_file)))), mojom::FilePayload::New(std::move(input_file)))),
...@@ -928,7 +925,7 @@ TEST_F(NearbyConnectionsTest, StopAdvertising) { ...@@ -928,7 +925,7 @@ TEST_F(NearbyConnectionsTest, StopAdvertising) {
base::RunLoop stop_advertising_run_loop; base::RunLoop stop_advertising_run_loop;
nearby_connections_->StopAdvertising( nearby_connections_->StopAdvertising(
base::BindLambdaForTesting([&](mojom::Status status) { kServiceId, base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kSuccess, status); EXPECT_EQ(mojom::Status::kSuccess, status);
stop_advertising_run_loop.Quit(); stop_advertising_run_loop.Quit();
})); }));
...@@ -983,7 +980,7 @@ TEST_F(NearbyConnectionsTest, DisconnectAllEndpoints) { ...@@ -983,7 +980,7 @@ TEST_F(NearbyConnectionsTest, DisconnectAllEndpoints) {
base::RunLoop stop_endpoints_run_loop; base::RunLoop stop_endpoints_run_loop;
nearby_connections_->StopAllEndpoints( nearby_connections_->StopAllEndpoints(
base::BindLambdaForTesting([&](mojom::Status status) { kServiceId, base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kSuccess, status); EXPECT_EQ(mojom::Status::kSuccess, status);
stop_endpoints_run_loop.Quit(); stop_endpoints_run_loop.Quit();
})); }));
...@@ -1000,7 +997,7 @@ TEST_F(NearbyConnectionsTest, InitiateBandwidthUpgradeFails) { ...@@ -1000,7 +997,7 @@ TEST_F(NearbyConnectionsTest, InitiateBandwidthUpgradeFails) {
EndpointData endpoint_data = CreateEndpointData(1); EndpointData endpoint_data = CreateEndpointData(1);
base::RunLoop bandwidth_upgrade_run_loop; base::RunLoop bandwidth_upgrade_run_loop;
nearby_connections_->InitiateBandwidthUpgrade( nearby_connections_->InitiateBandwidthUpgrade(
endpoint_data.remote_endpoint_id, kServiceId, endpoint_data.remote_endpoint_id,
base::BindLambdaForTesting([&](mojom::Status status) { base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kOutOfOrderApiCall, status); EXPECT_EQ(mojom::Status::kOutOfOrderApiCall, status);
bandwidth_upgrade_run_loop.Quit(); bandwidth_upgrade_run_loop.Quit();
...@@ -1020,7 +1017,7 @@ TEST_F(NearbyConnectionsTest, InitiateBandwidthUpgradeAfterDiscoveringFails) { ...@@ -1020,7 +1017,7 @@ TEST_F(NearbyConnectionsTest, InitiateBandwidthUpgradeAfterDiscoveringFails) {
// Requesting a bandwidth upgrade should fail. // Requesting a bandwidth upgrade should fail.
base::RunLoop bandwidth_upgrade_run_loop; base::RunLoop bandwidth_upgrade_run_loop;
nearby_connections_->InitiateBandwidthUpgrade( nearby_connections_->InitiateBandwidthUpgrade(
endpoint_data.remote_endpoint_id, kServiceId, endpoint_data.remote_endpoint_id,
base::BindLambdaForTesting([&](mojom::Status status) { base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kOutOfOrderApiCall, status); EXPECT_EQ(mojom::Status::kOutOfOrderApiCall, status);
bandwidth_upgrade_run_loop.Quit(); bandwidth_upgrade_run_loop.Quit();
...@@ -1037,7 +1034,7 @@ TEST_F(NearbyConnectionsTest, InitiateBandwidthUpgradeAfterAdvertisingFails) { ...@@ -1037,7 +1034,7 @@ TEST_F(NearbyConnectionsTest, InitiateBandwidthUpgradeAfterAdvertisingFails) {
// Requesting a bandwidth upgrade should fail. // Requesting a bandwidth upgrade should fail.
base::RunLoop bandwidth_upgrade_run_loop; base::RunLoop bandwidth_upgrade_run_loop;
nearby_connections_->InitiateBandwidthUpgrade( nearby_connections_->InitiateBandwidthUpgrade(
endpoint_data.remote_endpoint_id, kServiceId, endpoint_data.remote_endpoint_id,
base::BindLambdaForTesting([&](mojom::Status status) { base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kOutOfOrderApiCall, status); EXPECT_EQ(mojom::Status::kOutOfOrderApiCall, status);
bandwidth_upgrade_run_loop.Quit(); bandwidth_upgrade_run_loop.Quit();
...@@ -1066,7 +1063,7 @@ TEST_F(NearbyConnectionsTest, InitiateBandwidthUpgradeAfterConnectionSucceeds) { ...@@ -1066,7 +1063,7 @@ TEST_F(NearbyConnectionsTest, InitiateBandwidthUpgradeAfterConnectionSucceeds) {
// Requesting a bandwidth upgrade should succeed. // Requesting a bandwidth upgrade should succeed.
base::RunLoop bandwidth_upgrade_run_loop; base::RunLoop bandwidth_upgrade_run_loop;
nearby_connections_->InitiateBandwidthUpgrade( nearby_connections_->InitiateBandwidthUpgrade(
endpoint_data.remote_endpoint_id, kServiceId, endpoint_data.remote_endpoint_id,
base::BindLambdaForTesting([&](mojom::Status status) { base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kSuccess, status); EXPECT_EQ(mojom::Status::kSuccess, status);
bandwidth_upgrade_run_loop.Quit(); bandwidth_upgrade_run_loop.Quit();
...@@ -1139,7 +1136,7 @@ TEST_F(NearbyConnectionsTest, ReceiveFilePayload) { ...@@ -1139,7 +1136,7 @@ TEST_F(NearbyConnectionsTest, ReceiveFilePayload) {
base::RunLoop register_payload_run_loop; base::RunLoop register_payload_run_loop;
nearby_connections_->RegisterPayloadFile( nearby_connections_->RegisterPayloadFile(
kPayloadId, std::move(input_file), std::move(output_file), kServiceId, kPayloadId, std::move(input_file), std::move(output_file),
base::BindLambdaForTesting([&](mojom::Status status) { base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kSuccess, status); EXPECT_EQ(mojom::Status::kSuccess, status);
register_payload_run_loop.Quit(); register_payload_run_loop.Quit();
...@@ -1217,7 +1214,7 @@ TEST_F(NearbyConnectionsTest, ReceiveFilePayloadNotRegistered) { ...@@ -1217,7 +1214,7 @@ TEST_F(NearbyConnectionsTest, ReceiveFilePayloadNotRegistered) {
TEST_F(NearbyConnectionsTest, RegisterPayloadFileInvalid) { TEST_F(NearbyConnectionsTest, RegisterPayloadFileInvalid) {
base::RunLoop register_payload_run_loop; base::RunLoop register_payload_run_loop;
nearby_connections_->RegisterPayloadFile( nearby_connections_->RegisterPayloadFile(
kPayloadId, base::File(), base::File(), kServiceId, kPayloadId, base::File(), base::File(),
base::BindLambdaForTesting([&](mojom::Status status) { base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kError, status); EXPECT_EQ(mojom::Status::kError, status);
register_payload_run_loop.Quit(); register_payload_run_loop.Quit();
......
...@@ -44,13 +44,16 @@ class MockNearbyConnections : public NearbyConnectionsMojom { ...@@ -44,13 +44,16 @@ class MockNearbyConnections : public NearbyConnectionsMojom {
MOCK_METHOD(void, MOCK_METHOD(void,
StartAdvertising, StartAdvertising,
(const std::vector<uint8_t>& endpoint_info, (const std::string& service_id,
const std::string& service_id, const std::vector<uint8_t>& endpoint_info,
AdvertisingOptionsPtr, AdvertisingOptionsPtr,
mojo::PendingRemote<ConnectionLifecycleListener>, mojo::PendingRemote<ConnectionLifecycleListener>,
StartDiscoveryCallback), StartDiscoveryCallback),
(override)); (override));
MOCK_METHOD(void, StopAdvertising, (StopAdvertisingCallback), (override)); MOCK_METHOD(void,
StopAdvertising,
(const std::string& service_id, StopAdvertisingCallback),
(override));
MOCK_METHOD(void, MOCK_METHOD(void,
StartDiscovery, StartDiscovery,
(const std::string& service_id, (const std::string& service_id,
...@@ -58,10 +61,14 @@ class MockNearbyConnections : public NearbyConnectionsMojom { ...@@ -58,10 +61,14 @@ class MockNearbyConnections : public NearbyConnectionsMojom {
mojo::PendingRemote<EndpointDiscoveryListener>, mojo::PendingRemote<EndpointDiscoveryListener>,
StartDiscoveryCallback), StartDiscoveryCallback),
(override)); (override));
MOCK_METHOD(void, StopDiscovery, (StopDiscoveryCallback), (override)); MOCK_METHOD(void,
StopDiscovery,
(const std::string& service_id, StopDiscoveryCallback),
(override));
MOCK_METHOD(void, MOCK_METHOD(void,
RequestConnection, RequestConnection,
(const std::vector<uint8_t>& endpoint_info, (const std::string& service_id,
const std::vector<uint8_t>& endpoint_info,
const std::string& endpoint_id, const std::string& endpoint_id,
ConnectionOptionsPtr options, ConnectionOptionsPtr options,
mojo::PendingRemote<ConnectionLifecycleListener>, mojo::PendingRemote<ConnectionLifecycleListener>,
...@@ -69,41 +76,51 @@ class MockNearbyConnections : public NearbyConnectionsMojom { ...@@ -69,41 +76,51 @@ class MockNearbyConnections : public NearbyConnectionsMojom {
(override)); (override));
MOCK_METHOD(void, MOCK_METHOD(void,
DisconnectFromEndpoint, DisconnectFromEndpoint,
(const std::string& endpoint_id, DisconnectFromEndpointCallback), (const std::string& service_id,
const std::string& endpoint_id,
DisconnectFromEndpointCallback),
(override)); (override));
MOCK_METHOD(void, MOCK_METHOD(void,
AcceptConnection, AcceptConnection,
(const std::string& endpoint_id, (const std::string& service_id,
const std::string& endpoint_id,
mojo::PendingRemote<PayloadListener> listener, mojo::PendingRemote<PayloadListener> listener,
AcceptConnectionCallback callback), AcceptConnectionCallback callback),
(override)); (override));
MOCK_METHOD(void, MOCK_METHOD(void,
RejectConnection, RejectConnection,
(const std::string& endpoint_id, (const std::string& service_id,
const std::string& endpoint_id,
RejectConnectionCallback callback), RejectConnectionCallback callback),
(override)); (override));
MOCK_METHOD(void, MOCK_METHOD(void,
SendPayload, SendPayload,
(const std::vector<std::string>& endpoint_ids, (const std::string& service_id,
const std::vector<std::string>& endpoint_ids,
PayloadPtr payload, PayloadPtr payload,
SendPayloadCallback callback), SendPayloadCallback callback),
(override)); (override));
MOCK_METHOD(void, MOCK_METHOD(void,
CancelPayload, CancelPayload,
(int64_t payload_id, CancelPayloadCallback callback), (const std::string& service_id,
int64_t payload_id,
CancelPayloadCallback callback),
(override)); (override));
MOCK_METHOD(void, MOCK_METHOD(void,
StopAllEndpoints, StopAllEndpoints,
(DisconnectFromEndpointCallback callback), (const std::string& service_id,
DisconnectFromEndpointCallback callback),
(override)); (override));
MOCK_METHOD(void, MOCK_METHOD(void,
InitiateBandwidthUpgrade, InitiateBandwidthUpgrade,
(const std::string& endpoint_id, (const std::string& service_id,
const std::string& endpoint_id,
InitiateBandwidthUpgradeCallback callback), InitiateBandwidthUpgradeCallback callback),
(override)); (override));
MOCK_METHOD(void, MOCK_METHOD(void,
RegisterPayloadFile, RegisterPayloadFile,
(int64_t payload_id, (const std::string& service_id,
int64_t payload_id,
base::File input_file, base::File input_file,
base::File output_file, base::File output_file,
RegisterPayloadFileCallback callback), RegisterPayloadFileCallback callback),
......
...@@ -127,13 +127,13 @@ interface NearbyConnections { ...@@ -127,13 +127,13 @@ interface NearbyConnections {
// advertisement can be active at a time and calling StartAdvertising() while // advertisement can be active at a time and calling StartAdvertising() while
// advertising will fail and return Status::kAlreadyAdvertising. // advertising will fail and return Status::kAlreadyAdvertising.
// //
// endpoint_info - The local info to be broadcasted. May contain a human
// readable name to appear on other devices if broadcasting
// in high visibility mode.
// service_id - An identifier to advertise your app to other endpoints. // service_id - An identifier to advertise your app to other endpoints.
// This can be an arbitrary string, so long as it uniquely // This can be an arbitrary string, so long as it uniquely
// identifies your service. A good default is to use your // identifies your service. A good default is to use your
// app's package name. // app's package name.
// endpoint_info - The local info to be broadcasted. May contain a human
// readable name to appear on other devices if broadcasting
// in high visibility mode.
// options - The options for advertising. // options - The options for advertising.
// listener - An interface notified when remote endpoints request a // listener - An interface notified when remote endpoints request a
// connection to this endpoint. // connection to this endpoint.
...@@ -142,7 +142,7 @@ interface NearbyConnections { ...@@ -142,7 +142,7 @@ interface NearbyConnections {
// Status::kAlreadyAdvertising if the app is already advertising. // Status::kAlreadyAdvertising if the app is already advertising.
// Status::kOutOfOrderApiCall if the app is currently connected to remote // Status::kOutOfOrderApiCall if the app is currently connected to remote
// endpoints; call StopAllEndpoints first. // endpoints; call StopAllEndpoints first.
StartAdvertising(array<uint8> endpoint_info, string service_id, StartAdvertising(string service_id, array<uint8> endpoint_info,
AdvertisingOptions options, AdvertisingOptions options,
pending_remote<ConnectionLifecycleListener> listener) pending_remote<ConnectionLifecycleListener> listener)
=> (Status status); => (Status status);
...@@ -152,9 +152,11 @@ interface NearbyConnections { ...@@ -152,9 +152,11 @@ interface NearbyConnections {
// itself or goes inactive. Payloads can still be sent to connected // itself or goes inactive. Payloads can still be sent to connected
// endpoints after advertising ends. // endpoints after advertising ends.
// //
// service_id - Identifier used in the preceding StartAdvertising() call.
//
// Possible return values include: // Possible return values include:
// Status::kSuccess returned after advertising got stopped. // Status::kSuccess returned after advertising got stopped.
StopAdvertising() => (Status status); StopAdvertising(string service_id) => (Status status);
// Starts discovery for remote endpoints with the specified service ID. // Starts discovery for remote endpoints with the specified service ID.
// //
...@@ -177,12 +179,15 @@ interface NearbyConnections { ...@@ -177,12 +179,15 @@ interface NearbyConnections {
// goes inactive. Payloads can still be sent to connected endpoints after // goes inactive. Payloads can still be sent to connected endpoints after
// discovery ends. // discovery ends.
// //
// service_id - Identifier used in the preceding StartDiscovery() call.
//
// Possible return values include: // Possible return values include:
// Status::kSuccess returned after discovery got stopped. // Status::kSuccess returned after discovery got stopped.
StopDiscovery() => (Status status); StopDiscovery(string service_id) => (Status status);
// Sends a request to connect to a remote endpoint. // Sends a request to connect to a remote endpoint.
// //
// service_id - Service ID used to discover the endpoint.
// endpoint_info - The local info including a human readable name to appear on // endpoint_info - The local info including a human readable name to appear on
// the remote endpoint. // the remote endpoint.
// endpoint_id - The identifier for the remote endpoint to which a // endpoint_id - The identifier for the remote endpoint to which a
...@@ -201,14 +206,17 @@ interface NearbyConnections { ...@@ -201,14 +206,17 @@ interface NearbyConnections {
// Status::kWifiLanError if we failed to connect because of an // Status::kWifiLanError if we failed to connect because of an
// issue with WiFi. // issue with WiFi.
// Status::kError if we failed to connect for any other reason. // Status::kError if we failed to connect for any other reason.
RequestConnection(array<uint8> endpoint_info, string endpoint_id, RequestConnection(string service_id,
ConnectionOptions options, array<uint8> endpoint_info,
pending_remote<ConnectionLifecycleListener> listener) string endpoint_id,
ConnectionOptions options,
pending_remote<ConnectionLifecycleListener> listener)
=> (Status status); => (Status status);
// Accepts a connection to a remote endpoint. This method must be called // Accepts a connection to a remote endpoint. This method must be called
// before Payloads can be exchanged with the remote endpoint. // before Payloads can be exchanged with the remote endpoint.
// //
// service_id - Service ID used to bootstrap the connection.
// endpoint_id - The identifier for the remote endpoint. Should match the // endpoint_id - The identifier for the remote endpoint. Should match the
// value provided in a call to // value provided in a call to
// ConnectionsLifecycleListener::OnConnectionInitiated(). // ConnectionsLifecycleListener::OnConnectionInitiated().
...@@ -220,11 +228,14 @@ interface NearbyConnections { ...@@ -220,11 +228,14 @@ interface NearbyConnections {
// Status::kOutOfOrderApiCall if // Status::kOutOfOrderApiCall if
// ConnectionsLifecycleListener::OnConnectionInitiated() has not been // ConnectionsLifecycleListener::OnConnectionInitiated() has not been
// called for the |endpoint_id|. // called for the |endpoint_id|.
AcceptConnection(string endpoint_id, pending_remote<PayloadListener> listener) AcceptConnection(string service_id,
string endpoint_id,
pending_remote<PayloadListener> listener)
=> (Status status); => (Status status);
// Rejects a connection to a remote endpoint. // Rejects a connection to a remote endpoint.
// //
// service_id - Service ID used to bootstrap the connection.
// endpoint_id - The identifier for the remote endpoint. Should match the // endpoint_id - The identifier for the remote endpoint. Should match the
// value provided in a call to // value provided in a call to
// ConnectionsLifecycleListener::OnConnectionInitiated(). // ConnectionsLifecycleListener::OnConnectionInitiated().
...@@ -235,20 +246,23 @@ interface NearbyConnections { ...@@ -235,20 +246,23 @@ interface NearbyConnections {
// Status::kOutOfOrderApiCall if // Status::kOutOfOrderApiCall if
// ConnectionsLifecycleListener::OnConnectionInitiated() has not been // ConnectionsLifecycleListener::OnConnectionInitiated() has not been
// called for the |endpoint_id|. // called for the |endpoint_id|.
RejectConnection(string endpoint_id) => (Status status); RejectConnection(string service_id, string endpoint_id) => (Status status);
// Disconnects from a remote endpoint. Payloads can no longer be sent // Disconnects from a remote endpoint. Payloads can no longer be sent
// to or received from the endpoint after this method is called. // to or received from the endpoint after this method is called.
// //
// service_id - Service ID used to bootstrap the connection.
// endpoint_id - The identifier for the remote endpoint to disconnect from. // endpoint_id - The identifier for the remote endpoint to disconnect from.
// Possible return values include: // Possible return values include:
// Status::kSuccess disconnected successfully. // Status::kSuccess disconnected successfully.
DisconnectFromEndpoint(string endpoint_id) => (Status status); DisconnectFromEndpoint(string service_id,
string endpoint_id) => (Status status);
// Sends a Payload to a list of remote endpoints. Payloads can only be sent to // Sends a Payload to a list of remote endpoints. Payloads can only be sent to
// remote endpoints once a notice of connection acceptance has been delivered // remote endpoints once a notice of connection acceptance has been delivered
// via ConnectionsLifecycleListener::OnConnectionResult(). // via ConnectionsLifecycleListener::OnConnectionResult().
// //
// service_id - Service ID used to bootstrap the connection.
// endpoint_ids - Array of remote endpoint identifiers for to which the // endpoint_ids - Array of remote endpoint identifiers for to which the
// payload should be sent. // payload should be sent.
// payload - The Payload to be sent. // payload - The Payload to be sent.
...@@ -263,43 +277,56 @@ interface NearbyConnections { ...@@ -263,43 +277,56 @@ interface NearbyConnections {
// still occur during transmission (and at different times for different // still occur during transmission (and at different times for different
// endpoints), and will be delivered via // endpoints), and will be delivered via
// PayloadListener::OnPayloadTransferUpdate(). // PayloadListener::OnPayloadTransferUpdate().
SendPayload(array<string> endpoint_ids, Payload payload) => (Status status); SendPayload(string service_id,
array<string> endpoint_ids,
Payload payload) => (Status status);
// Cancels a Payload currently in-flight to or from remote endpoint(s). // Cancels a Payload currently in-flight to or from remote endpoint(s).
// //
// service_id - Service ID used to bootstrap the connection.
//
// payload_id - The identifier for the Payload to be cancelled. // payload_id - The identifier for the Payload to be cancelled.
// Possible return values include: // Possible return values include:
// Status::kSuccess if the payload got cancelled. // Status::kSuccess if the payload got cancelled.
CancelPayload(int64 payload_id) => (Status status); CancelPayload(string service_id, int64 payload_id) => (Status status);
// Disconnects from, and removes all traces of, all connected and/or // Disconnects from, and removes all traces of, all connected and/or
// discovered endpoints. As a side effect of this call, both // discovered endpoints. As a side effect of this call, both
// StopAdvertising and StopDiscovery are invoked. After calling // StopAdvertising and StopDiscovery are invoked. After calling
// StopAllEndpoints, no further operations with remote endpoints will // StopAllEndpoints, no further operations with remote endpoints will
// be possible until a new call to one of StartAdvertising or // be possible until a new call to one of StartAdvertising or
// StartDiscovery. // StartDiscovery.
// Possible return values include: //
// Status::kSuccess disconnected from all endpoints successfully. // service_id - Service ID used to discover endpoints.
StopAllEndpoints() => (Status status); //
// Possible return values include:
// Status::kSuccess disconnected from all endpoints successfully.
StopAllEndpoints(string service_id) => (Status status);
// Sends a request to initiate connection bandwidth upgrade. // Sends a request to initiate connection bandwidth upgrade.
// //
// service_id - Service ID used to discover the endpoint.
// endpoint_id - The identifier for the remote endpoint which will be // endpoint_id - The identifier for the remote endpoint which will be
// switching to a higher connection data rate and possibly // switching to a higher connection data rate and possibly
// different wireless protocol. On success, calls // different wireless protocol. On success, calls
// ConnectionLifecycleListener::OnBandwidthChanged. // ConnectionLifecycleListener::OnBandwidthChanged.
// Possible return values include: // Possible return values include:
// Status::kSuccess if upgraded successfully. // Status::kSuccess if upgraded successfully.
InitiateBandwidthUpgrade(string endpoint_id) => (Status status); InitiateBandwidthUpgrade(string service_id,
string endpoint_id) => (Status status);
// Register a pair of input and output file for handling incoming file // Register a pair of input and output file for handling incoming file
// payload associated with |payload_id_|, which is determined by feature // payload associated with |payload_id_|, which is determined by feature
// specific design. The |input_file| should be opened for read access, and // specific design. The |input_file| should be opened for read access, and
// |output_file| should be opened for write access. // |output_file| should be opened for write access.
//
// service_id - Service ID used to discover the endpoint.
//
// Possible return values include: // Possible return values include:
// Status::kSuccess if file is registered successfully. // Status::kSuccess if file is registered successfully.
// Status::kError if file is not opened correctly. // Status::kError if file is not opened correctly.
RegisterPayloadFile(int64 payload_id, RegisterPayloadFile(string service_id,
int64 payload_id,
mojo_base.mojom.ReadOnlyFile input_file, mojo_base.mojom.ReadOnlyFile input_file,
mojo_base.mojom.File output_file) mojo_base.mojom.File output_file)
=> (Status status); => (Status status);
......
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