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(
incoming_connection_listener_ = listener;
nearby_connections_->StartAdvertising(
endpoint_info, kServiceId,
kServiceId, endpoint_info,
AdvertisingOptions::New(
kStrategy, std::move(allowed_mediums),
/*auto_upgrade_bandwidth=*/is_high_power,
......@@ -112,7 +112,7 @@ void NearbyConnectionsManagerImpl::StartAdvertising(
void NearbyConnectionsManagerImpl::StopAdvertising() {
if (nearby_connections_) {
nearby_connections_->StopAdvertising(
base::BindOnce([](ConnectionsStatus status) {
kServiceId, base::BindOnce([](ConnectionsStatus status) {
NS_LOG(VERBOSE) << __func__
<< ": Stop advertising attempted over Nearby "
"Connections with result: "
......@@ -155,7 +155,7 @@ void NearbyConnectionsManagerImpl::StartDiscovery(
void NearbyConnectionsManagerImpl::StopDiscovery() {
if (nearby_connections_) {
nearby_connections_->StopDiscovery(
base::BindOnce([](ConnectionsStatus status) {
kServiceId, base::BindOnce([](ConnectionsStatus status) {
NS_LOG(VERBOSE) << __func__
<< ": Stop discovery attempted over Nearby "
"Connections with result: "
......@@ -203,7 +203,7 @@ void NearbyConnectionsManagerImpl::Connect(
connect_timeout_timers_.emplace(endpoint_id, std::move(timeout_timer));
nearby_connections_->RequestConnection(
endpoint_info, endpoint_id,
kServiceId, endpoint_info, endpoint_id,
ConnectionOptions::New(std::move(allowed_mediums),
std::move(bluetooth_mac_address)),
std::move(lifecycle_listener),
......@@ -239,7 +239,7 @@ void NearbyConnectionsManagerImpl::Disconnect(const std::string& endpoint_id) {
return;
nearby_connections_->DisconnectFromEndpoint(
endpoint_id,
kServiceId, endpoint_id,
base::BindOnce(
[](const std::string& endpoint_id, ConnectionsStatus status) {
NS_LOG(VERBOSE)
......@@ -263,7 +263,7 @@ void NearbyConnectionsManagerImpl::Send(const std::string& endpoint_id,
RegisterPayloadStatusListener(payload->id, listener);
nearby_connections_->SendPayload(
{endpoint_id}, std::move(payload),
kServiceId, {endpoint_id}, std::move(payload),
base::BindOnce(
[](const std::string& endpoint_id, ConnectionsStatus status) {
NS_LOG(VERBOSE)
......@@ -300,8 +300,8 @@ void NearbyConnectionsManagerImpl::OnFileCreated(
ConnectionsCallback callback,
NearbyFileHandler::CreateFileResult result) {
nearby_connections_->RegisterPayloadFile(
payload_id, std::move(result.input_file), std::move(result.output_file),
std::move(callback));
kServiceId, payload_id, std::move(result.input_file),
std::move(result.output_file), std::move(callback));
}
NearbyConnectionsManagerImpl::Payload*
......@@ -326,7 +326,7 @@ void NearbyConnectionsManagerImpl::Cancel(int64_t payload_id) {
payload_status_listeners_.erase(it);
}
nearby_connections_->CancelPayload(
payload_id,
kServiceId, payload_id,
base::BindOnce(
[](int64_t payload_id, ConnectionsStatus status) {
NS_LOG(VERBOSE)
......@@ -367,7 +367,7 @@ void NearbyConnectionsManagerImpl::UpgradeBandwidth(
return;
nearby_connections_->InitiateBandwidthUpgrade(
endpoint_id,
kServiceId, endpoint_id,
base::BindOnce(
[](const std::string& endpoint_id, ConnectionsStatus status) {
NS_LOG(VERBOSE)
......@@ -452,7 +452,7 @@ void NearbyConnectionsManagerImpl::OnConnectionInitiated(
payload_listener.InitWithNewPipeAndPassReceiver());
nearby_connections_->AcceptConnection(
endpoint_id, std::move(payload_listener),
kServiceId, endpoint_id, std::move(payload_listener),
base::BindOnce(
[](const std::string& endpoint_id, ConnectionsStatus status) {
NS_LOG(VERBOSE)
......@@ -572,7 +572,8 @@ void NearbyConnectionsManagerImpl::OnPayloadTransferUpdate(
if (!payload_it->second->content->is_bytes()) {
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;
}
......@@ -599,7 +600,7 @@ bool NearbyConnectionsManagerImpl::BindNearbyConnections() {
void NearbyConnectionsManagerImpl::Reset() {
if (nearby_connections_) {
nearby_connections_->StopAllEndpoints(
base::BindOnce([](ConnectionsStatus status) {
kServiceId, base::BindOnce([](ConnectionsStatus status) {
NS_LOG(VERBOSE) << __func__
<< ": Stop all endpoints attempted over Nearby "
"Connections with result: "
......
......@@ -180,12 +180,13 @@ class NearbyConnectionsManagerImplTest : public testing::Test {
std::end(kEndpointInfo));
EXPECT_CALL(nearby_connections_, StartAdvertising)
.WillOnce(
[&](const std::vector<uint8_t>& endpoint_info,
const std::string& service_id, AdvertisingOptionsPtr options,
[&](const std::string& service_id,
const std::vector<uint8_t>& endpoint_info,
AdvertisingOptionsPtr options,
mojo::PendingRemote<ConnectionLifecycleListener> listener,
NearbyConnectionsMojom::StartAdvertisingCallback callback) {
EXPECT_EQ(local_endpoint_info, endpoint_info);
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(local_endpoint_info, endpoint_info);
EXPECT_EQ(kStrategy, options->strategy);
EXPECT_TRUE(options->enforce_topology_constraints);
......@@ -214,10 +215,12 @@ class NearbyConnectionsManagerImplTest : public testing::Test {
EXPECT_CALL(nearby_connections_, RequestConnection)
.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,
mojo::PendingRemote<ConnectionLifecycleListener> listener,
NearbyConnectionsMojom::RequestConnectionCallback callback) {
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(local_endpoint_info, endpoint_info);
EXPECT_EQ(kRemoteEndpointId, endpoint_id);
......@@ -238,9 +241,10 @@ class NearbyConnectionsManagerImplTest : public testing::Test {
base::RunLoop accept_run_loop;
EXPECT_CALL(nearby_connections_, AcceptConnection)
.WillOnce(
[&](const std::string& endpoint_id,
[&](const std::string& service_id, const std::string& endpoint_id,
mojo::PendingRemote<PayloadListener> listener,
NearbyConnectionsMojom::AcceptConnectionCallback callback) {
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(kRemoteEndpointId, endpoint_id);
payload_listener_remote.Bind(std::move(listener));
......@@ -280,9 +284,10 @@ class NearbyConnectionsManagerImplTest : public testing::Test {
base::RunLoop accept_run_loop;
EXPECT_CALL(nearby_connections_, AcceptConnection)
.WillOnce(
[&](const std::string& endpoint_id,
[&](const std::string& service_id, const std::string& endpoint_id,
mojo::PendingRemote<PayloadListener> listener,
NearbyConnectionsMojom::AcceptConnectionCallback callback) {
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(kRemoteEndpointId, endpoint_id);
payload_listener_remote.Bind(std::move(listener));
......@@ -332,10 +337,12 @@ class NearbyConnectionsManagerImplTest : public testing::Test {
base::RunLoop run_loop;
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,
NearbyConnectionsMojom::SendPayloadCallback callback) {
ASSERT_EQ(1u, endpoint_ids.size());
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(kRemoteEndpointId, endpoint_ids.front());
ASSERT_TRUE(payload);
ASSERT_EQ(PayloadContent::Tag::FILE, payload->content->which());
......@@ -507,10 +514,12 @@ TEST_P(NearbyConnectionsManagerImplTestConnectionMediums,
std::end(kEndpointInfo));
EXPECT_CALL(nearby_connections_, RequestConnection)
.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,
mojo::PendingRemote<ConnectionLifecycleListener> listener,
NearbyConnectionsMojom::RequestConnectionCallback callback) {
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(local_endpoint_info, endpoint_info);
EXPECT_EQ(kRemoteEndpointId, endpoint_id);
EXPECT_EQ(expected_mediums, options->allowed_mediums);
......@@ -570,10 +579,12 @@ TEST_P(NearbyConnectionsManagerImplTestConnectionBluetoothMacAddress,
std::end(kEndpointInfo));
EXPECT_CALL(nearby_connections_, RequestConnection)
.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,
mojo::PendingRemote<ConnectionLifecycleListener> listener,
NearbyConnectionsMojom::RequestConnectionCallback callback) {
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(local_endpoint_info, endpoint_info);
EXPECT_EQ(kRemoteEndpointId, endpoint_id);
EXPECT_EQ(GetParam().expected_bluetooth_mac_address,
......@@ -757,9 +768,11 @@ TEST_F(NearbyConnectionsManagerImplTest, ConnectWrite) {
base::RunLoop run_loop;
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,
NearbyConnectionsMojom::SendPayloadCallback callback) {
EXPECT_EQ(kServiceId, service_id);
ASSERT_EQ(1u, endpoint_ids.size());
EXPECT_EQ(kRemoteEndpointId, endpoint_ids.front());
ASSERT_TRUE(payload);
......@@ -801,8 +814,9 @@ TEST_F(NearbyConnectionsManagerImplTest, ConnectClosed) {
EXPECT_CALL(nearby_connections_, DisconnectFromEndpoint)
.WillOnce(
[&](const std::string& endpoint_id,
[&](const std::string& service_id, const std::string& endpoint_id,
NearbyConnectionsMojom::DisconnectFromEndpointCallback callback) {
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(kRemoteEndpointId, endpoint_id);
std::move(callback).Run(Status::kSuccess);
});
......@@ -874,8 +888,9 @@ TEST_F(NearbyConnectionsManagerImplTest, ConnectClosedByClient) {
EXPECT_CALL(nearby_connections_, DisconnectFromEndpoint)
.WillOnce(
[&](const std::string& endpoint_id,
[&](const std::string& service_id, const std::string& endpoint_id,
NearbyConnectionsMojom::DisconnectFromEndpointCallback callback) {
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(kRemoteEndpointId, endpoint_id);
std::move(callback).Run(Status::kSuccess);
});
......@@ -934,8 +949,9 @@ TEST_F(NearbyConnectionsManagerImplTest, ConnectCancelPayload) {
base::RunLoop cancel_run_loop;
EXPECT_CALL(nearby_connections_, CancelPayload)
.WillOnce([&](int64_t payload_id,
.WillOnce([&](const std::string& service_id, int64_t payload_id,
NearbyConnectionsMojom::CancelPayloadCallback callback) {
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(kPayloadId, payload_id);
std::move(callback).Run(Status::kSuccess);
......@@ -971,10 +987,12 @@ TEST_F(NearbyConnectionsManagerImplTest, ConnectTimeout) {
NearbyConnectionsMojom::RequestConnectionCallback connect_callback;
EXPECT_CALL(nearby_connections_, RequestConnection)
.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,
mojo::PendingRemote<ConnectionLifecycleListener> listener,
NearbyConnectionsMojom::RequestConnectionCallback callback) {
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(local_endpoint_info, endpoint_info);
EXPECT_EQ(kRemoteEndpointId, endpoint_id);
......@@ -986,8 +1004,9 @@ TEST_F(NearbyConnectionsManagerImplTest, ConnectTimeout) {
// Timing out should call disconnect.
EXPECT_CALL(nearby_connections_, DisconnectFromEndpoint)
.WillOnce(
[&](const std::string& endpoint_id,
[&](const std::string& service_id, const std::string& endpoint_id,
NearbyConnectionsMojom::DisconnectFromEndpointCallback callback) {
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(kRemoteEndpointId, endpoint_id);
std::move(callback).Run(Status::kSuccess);
});
......@@ -1095,8 +1114,10 @@ TEST_F(NearbyConnectionsManagerImplTest, IncomingRegisterPayloadPath) {
base::RunLoop register_payload_run_loop;
EXPECT_CALL(nearby_connections_, RegisterPayloadFile)
.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) {
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(kPayloadId, payload_id);
ASSERT_TRUE(input_file.IsValid());
ASSERT_TRUE(output_file.IsValid());
......@@ -1294,8 +1315,8 @@ TEST_P(NearbyConnectionsManagerImplTestMediums, StartAdvertising_Options) {
incoming_connection_listener;
EXPECT_CALL(nearby_connections_, StartAdvertising)
.WillOnce([&](const std::vector<uint8_t>& endpoint_info,
const std::string& service_id,
.WillOnce([&](const std::string& service_id,
const std::vector<uint8_t>& endpoint_info,
AdvertisingOptionsPtr options,
mojo::PendingRemote<ConnectionLifecycleListener> listener,
NearbyConnectionsMojom::StartAdvertisingCallback callback) {
......@@ -1392,9 +1413,11 @@ TEST_F(NearbyConnectionsManagerImplTest,
// Upgrading bandwidth will succeed.
EXPECT_CALL(nearby_connections_, InitiateBandwidthUpgrade)
.WillOnce([&](const std::string& endpoint_id,
.WillOnce([&](const std::string& service_id,
const std::string& endpoint_id,
NearbyConnectionsMojom::InitiateBandwidthUpgradeCallback
callback) {
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(kRemoteEndpointId, endpoint_id);
std::move(callback).Run(Status::kSuccess);
});
......@@ -1418,9 +1441,11 @@ TEST_F(NearbyConnectionsManagerImplTest,
// Upgrading bandwidth will succeed.
EXPECT_CALL(nearby_connections_, InitiateBandwidthUpgrade)
.WillOnce([&](const std::string& endpoint_id,
.WillOnce([&](const std::string& service_id,
const std::string& endpoint_id,
NearbyConnectionsMojom::InitiateBandwidthUpgradeCallback
callback) {
EXPECT_EQ(kServiceId, service_id);
EXPECT_EQ(kRemoteEndpointId, endpoint_id);
std::move(callback).Run(Status::kSuccess);
});
......
......@@ -12,6 +12,8 @@
#include "chrome/services/sharing/nearby/nearby_connections_conversions.h"
#include "chrome/services/sharing/nearby/platform/input_file.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 nearby {
......@@ -19,6 +21,94 @@ namespace connections {
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(
const std::vector<uint8_t>& endpoint_info,
mojo::PendingRemote<mojom::ConnectionLifecycleListener> listener) {
......@@ -90,11 +180,24 @@ NearbyConnections::NearbyConnections(
mojom::NearbyConnectionsDependenciesPtr dependencies,
scoped_refptr<base::SequencedTaskRunner> io_task_runner,
base::OnceClosure on_disconnect,
std::unique_ptr<Core> core)
std::unique_ptr<ServiceController> service_controller)
: nearby_connections_(this, std::move(nearby_connections)),
on_disconnect_(std::move(on_disconnect)),
core_(std::move(core)),
service_controller_(std::move(service_controller)),
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(
&NearbyConnections::OnDisconnect, weak_ptr_factory_.GetWeakPtr()));
......@@ -144,10 +247,17 @@ 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;
}
void NearbyConnections::InitializeOfflineServiceController() {
DCHECK(!service_controller_);
service_controller_ = std::make_unique<OfflineServiceController>();
}
void NearbyConnections::OnDisconnect() {
if (on_disconnect_)
std::move(on_disconnect_).Run();
......@@ -155,8 +265,8 @@ void NearbyConnections::OnDisconnect() {
}
void NearbyConnections::StartAdvertising(
const std::vector<uint8_t>& endpoint_info,
const std::string& service_id,
const std::vector<uint8_t>& endpoint_info,
mojom::AdvertisingOptionsPtr options,
mojo::PendingRemote<mojom::ConnectionLifecycleListener> listener,
StartAdvertisingCallback callback) {
......@@ -169,14 +279,17 @@ void NearbyConnections::StartAdvertising(
.fast_advertisement_service_uuid =
options->fast_advertisement_service_uuid.canonical_value()};
core_->StartAdvertising(
GetCore(service_id)
->StartAdvertising(
service_id, std::move(connection_options),
CreateConnectionRequestInfo(endpoint_info, std::move(listener)),
ResultCallbackFromMojom(std::move(callback)));
}
void NearbyConnections::StopAdvertising(StopAdvertisingCallback callback) {
core_->StopAdvertising(ResultCallbackFromMojom(std::move(callback)));
void NearbyConnections::StopAdvertising(const std::string& service_id,
StopAdvertisingCallback callback) {
GetCore(service_id)
->StopAdvertising(ResultCallbackFromMojom(std::move(callback)));
}
void NearbyConnections::StartDiscovery(
......@@ -214,16 +327,20 @@ void NearbyConnections::StartDiscovery(
};
ResultCallback result_callback = ResultCallbackFromMojom(std::move(callback));
core_->StartDiscovery(service_id, std::move(connection_options),
GetCore(service_id)
->StartDiscovery(service_id, std::move(connection_options),
std::move(discovery_listener),
std::move(result_callback));
}
void NearbyConnections::StopDiscovery(StopDiscoveryCallback callback) {
core_->StopDiscovery(ResultCallbackFromMojom(std::move(callback)));
void NearbyConnections::StopDiscovery(const std::string& service_id,
StopDiscoveryCallback callback) {
GetCore(service_id)
->StopDiscovery(ResultCallbackFromMojom(std::move(callback)));
}
void NearbyConnections::RequestConnection(
const std::string& service_id,
const std::vector<uint8_t>& endpoint_info,
const std::string& endpoint_id,
mojom::ConnectionOptionsPtr options,
......@@ -235,7 +352,8 @@ void NearbyConnections::RequestConnection(
connection_options.remote_bluetooth_mac_address =
ByteArrayFromMojom(*options->remote_bluetooth_mac_address);
}
core_->RequestConnection(
GetCore(service_id)
->RequestConnection(
endpoint_id,
CreateConnectionRequestInfo(endpoint_info, std::move(listener)),
std::move(connection_options),
......@@ -243,13 +361,16 @@ void NearbyConnections::RequestConnection(
}
void NearbyConnections::DisconnectFromEndpoint(
const std::string& service_id,
const std::string& endpoint_id,
DisconnectFromEndpointCallback callback) {
core_->DisconnectFromEndpoint(endpoint_id,
GetCore(service_id)
->DisconnectFromEndpoint(endpoint_id,
ResultCallbackFromMojom(std::move(callback)));
}
void NearbyConnections::AcceptConnection(
const std::string& service_id,
const std::string& endpoint_id,
mojo::PendingRemote<mojom::PayloadListener> listener,
AcceptConnectionCallback callback) {
......@@ -257,7 +378,7 @@ void NearbyConnections::AcceptConnection(
// Capturing Core* is safe as Core owns PayloadListener.
PayloadListener payload_listener = {
.payload_cb =
[remote, core = core_.get()](const std::string& endpoint_id,
[remote, core = GetCore(service_id)](const std::string& endpoint_id,
Payload payload) {
if (!remote)
return;
......@@ -315,17 +436,21 @@ void NearbyConnections::AcceptConnection(
info.total_bytes, info.bytes_transferred));
}};
core_->AcceptConnection(endpoint_id, std::move(payload_listener),
GetCore(service_id)
->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) {
core_->RejectConnection(endpoint_id,
GetCore(service_id)
->RejectConnection(endpoint_id,
ResultCallbackFromMojom(std::move(callback)));
}
void NearbyConnections::SendPayload(
const std::string& service_id,
const std::vector<std::string>& endpoint_ids,
mojom::PayloadPtr payload,
SendPayloadCallback callback) {
......@@ -347,28 +472,35 @@ void NearbyConnections::SendPayload(
break;
}
core_->SendPayload(absl::MakeSpan(endpoint_ids), std::move(core_payload),
GetCore(service_id)
->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) {
core_->CancelPayload(payload_id,
ResultCallbackFromMojom(std::move(callback)));
GetCore(service_id)
->CancelPayload(payload_id, ResultCallbackFromMojom(std::move(callback)));
}
void NearbyConnections::StopAllEndpoints(StopAllEndpointsCallback callback) {
core_->StopAllEndpoints(ResultCallbackFromMojom(std::move(callback)));
void NearbyConnections::StopAllEndpoints(const std::string& service_id,
StopAllEndpointsCallback callback) {
GetCore(service_id)
->StopAllEndpoints(ResultCallbackFromMojom(std::move(callback)));
}
void NearbyConnections::InitiateBandwidthUpgrade(
const std::string& service_id,
const std::string& endpoint_id,
InitiateBandwidthUpgradeCallback callback) {
core_->InitiateBandwidthUpgrade(endpoint_id,
GetCore(service_id)
->InitiateBandwidthUpgrade(endpoint_id,
ResultCallbackFromMojom(std::move(callback)));
}
void NearbyConnections::RegisterPayloadFile(
const std::string& service_id,
int64_t payload_id,
base::File input_file,
base::File output_file,
......@@ -418,6 +550,21 @@ NearbyConnections::GetThreadTaskRunner() {
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 nearby
} // namespace location
......@@ -26,12 +26,14 @@
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.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 nearby {
namespace connections {
class Core;
// Implementation of the NearbyConnections mojo interface.
// 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
......@@ -50,7 +52,7 @@ class NearbyConnections : public mojom::NearbyConnections {
mojom::NearbyConnectionsDependenciesPtr dependencies,
scoped_refptr<base::SequencedTaskRunner> io_task_runner,
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& operator=(const NearbyConnections&) = delete;
......@@ -85,41 +87,52 @@ class NearbyConnections : public mojom::NearbyConnections {
// mojom::NearbyConnections:
void StartAdvertising(
const std::vector<uint8_t>& endpoint_info,
const std::string& service_id,
const std::vector<uint8_t>& endpoint_info,
mojom::AdvertisingOptionsPtr options,
mojo::PendingRemote<mojom::ConnectionLifecycleListener> listener,
StartAdvertisingCallback callback) override;
void StopAdvertising(StopAdvertisingCallback callback) override;
void StopAdvertising(const std::string& service_id,
StopAdvertisingCallback callback) override;
void StartDiscovery(
const std::string& service_id,
mojom::DiscoveryOptionsPtr options,
mojo::PendingRemote<mojom::EndpointDiscoveryListener> listener,
StartDiscoveryCallback callback) override;
void StopDiscovery(StopDiscoveryCallback callback) override;
void StopDiscovery(const std::string& service_id,
StopDiscoveryCallback callback) override;
void RequestConnection(
const std::string& service_id,
const std::vector<uint8_t>& endpoint_info,
const std::string& endpoint_id,
mojom::ConnectionOptionsPtr options,
mojo::PendingRemote<mojom::ConnectionLifecycleListener> listener,
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;
void AcceptConnection(const std::string& endpoint_id,
void AcceptConnection(const std::string& service_id,
const std::string& endpoint_id,
mojo::PendingRemote<mojom::PayloadListener> listener,
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;
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,
SendPayloadCallback callback) override;
void CancelPayload(int64_t payload_id,
void CancelPayload(const std::string& service_id,
int64_t payload_id,
CancelPayloadCallback callback) override;
void StopAllEndpoints(StopAllEndpointsCallback callback) override;
void StopAllEndpoints(const std::string& service_id,
StopAllEndpointsCallback callback) override;
void InitiateBandwidthUpgrade(
const std::string& service_id,
const std::string& endpoint_id,
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 output_file,
RegisterPayloadFileCallback callback) override;
......@@ -134,6 +147,9 @@ class NearbyConnections : public mojom::NearbyConnections {
scoped_refptr<base::SingleThreadTaskRunner> GetThreadTaskRunner();
private:
Core* GetCore(const std::string& service_id);
void InitializeOfflineServiceController();
void OnDisconnect();
mojo::Receiver<mojom::NearbyConnections> nearby_connections_;
......@@ -148,9 +164,12 @@ class NearbyConnections : public mojom::NearbyConnections {
mojo::SharedRemote<sharing::mojom::WebRtcSignalingMessenger>
webrtc_signaling_messenger_;
// Core is thread-safe as its operations are always dispatched to a
// single-thread executor.
std::unique_ptr<Core> core_;
std::unique_ptr<ServiceController> service_controller_;
// 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.
base::Lock input_file_lock_;
......
......@@ -186,16 +186,15 @@ class NearbyConnectionsTest : public testing::Test {
auto dependencies = mojom::NearbyConnectionsDependencies::New(
bluetooth_adapter_.adapter_.BindNewPipeAndPassRemote(),
std::move(webrtc_dependencies));
service_controller_ =
auto service_controller =
std::make_unique<testing::NiceMock<MockServiceController>>();
service_controller_ptr_ = service_controller_.get();
service_controller_ptr_ = service_controller.get();
nearby_connections_ = std::make_unique<NearbyConnections>(
remote_.BindNewPipeAndPassReceiver(), std::move(dependencies),
/*io_task_runner=*/nullptr,
base::BindOnce(&NearbyConnectionsTest::OnDisconnect,
base::Unretained(this)),
std::make_unique<Core>(
[&]() { return service_controller_.release(); }));
std::move(service_controller));
}
void OnDisconnect() { disconnect_run_loop_.Quit(); }
......@@ -278,7 +277,7 @@ class NearbyConnectionsTest : public testing::Test {
base::RunLoop start_advertising_run_loop;
nearby_connections_->StartAdvertising(
endpoint_info, kServiceId, CreateAdvertisingOptions(),
kServiceId, endpoint_info, CreateAdvertisingOptions(),
fake_connection_life_cycle_listener.receiver.BindNewPipeAndPassRemote(),
base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kSuccess, status);
......@@ -328,7 +327,7 @@ class NearbyConnectionsTest : public testing::Test {
base::RunLoop request_connection_run_loop;
nearby_connections_->RequestConnection(
endpoint_info, endpoint_data.remote_endpoint_id,
kServiceId, endpoint_info, endpoint_data.remote_endpoint_id,
CreateConnectionOptions(bluetooth_mac_address),
fake_connection_life_cycle_listener.receiver.BindNewPipeAndPassRemote(),
base::BindLambdaForTesting([&](mojom::Status status) {
......@@ -356,7 +355,7 @@ class NearbyConnectionsTest : public testing::Test {
base::RunLoop accept_connection_run_loop;
nearby_connections_->AcceptConnection(
remote_endpoint_id,
kServiceId, remote_endpoint_id,
fake_payload_listener.receiver.BindNewPipeAndPassRemote(),
base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kSuccess, status);
......@@ -375,9 +374,6 @@ class NearbyConnectionsTest : public testing::Test {
std::unique_ptr<NearbyConnections> nearby_connections_;
testing::NiceMock<MockServiceController>* service_controller_ptr_;
base::RunLoop disconnect_run_loop_;
private:
std::unique_ptr<testing::NiceMock<MockServiceController>> service_controller_;
};
TEST_F(NearbyConnectionsTest, RemoteDisconnect) {
......@@ -449,7 +445,7 @@ TEST_F(NearbyConnectionsTest, StopDiscovery) {
base::RunLoop stop_discovery_run_loop;
nearby_connections_->StopDiscovery(
base::BindLambdaForTesting([&](mojom::Status status) {
kServiceId, base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kSuccess, status);
stop_discovery_run_loop.Quit();
}));
......@@ -588,7 +584,7 @@ TEST_F(NearbyConnectionsTest, RequestConnectionOnBandwidthUpgrade) {
});
base::RunLoop bandwidth_upgrade_run_loop;
nearby_connections_->InitiateBandwidthUpgrade(
endpoint_data.remote_endpoint_id,
kServiceId, endpoint_data.remote_endpoint_id,
base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kSuccess, status);
bandwidth_upgrade_run_loop.Quit();
......@@ -657,7 +653,7 @@ TEST_F(NearbyConnectionsTest, RequestConnectionDisconnect) {
base::RunLoop disconnect_from_endpoint_run_loop;
nearby_connections_->DisconnectFromEndpoint(
endpoint_data.remote_endpoint_id,
kServiceId, endpoint_data.remote_endpoint_id,
base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kSuccess, status);
disconnect_from_endpoint_run_loop.Quit();
......@@ -725,7 +721,7 @@ TEST_F(NearbyConnectionsTest, SendBytesPayload) {
base::RunLoop send_payload_run_loop;
nearby_connections_->SendPayload(
{endpoint_data.remote_endpoint_id},
kServiceId, {endpoint_data.remote_endpoint_id},
mojom::Payload::New(kPayloadId,
mojom::PayloadContent::NewBytes(
mojom::BytesPayload::New(expected_payload))),
......@@ -769,7 +765,7 @@ TEST_F(NearbyConnectionsTest, SendBytesPayloadCancelled) {
base::RunLoop send_payload_run_loop;
nearby_connections_->SendPayload(
{endpoint_data.remote_endpoint_id},
kServiceId, {endpoint_data.remote_endpoint_id},
mojom::Payload::New(kPayloadId,
mojom::PayloadContent::NewBytes(
mojom::BytesPayload::New(expected_payload))),
......@@ -785,7 +781,8 @@ TEST_F(NearbyConnectionsTest, SendBytesPayloadCancelled) {
base::RunLoop cancel_payload_run_loop;
nearby_connections_->CancelPayload(
kPayloadId, base::BindLambdaForTesting([&](mojom::Status status) {
kServiceId, kPayloadId,
base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kSuccess, status);
cancel_payload_run_loop.Quit();
}));
......@@ -840,7 +837,7 @@ TEST_F(NearbyConnectionsTest, SendFilePayload) {
base::RunLoop send_payload_run_loop;
nearby_connections_->SendPayload(
{endpoint_data.remote_endpoint_id},
kServiceId, {endpoint_data.remote_endpoint_id},
mojom::Payload::New(kPayloadId,
mojom::PayloadContent::NewFile(
mojom::FilePayload::New(std::move(input_file)))),
......@@ -928,7 +925,7 @@ TEST_F(NearbyConnectionsTest, StopAdvertising) {
base::RunLoop stop_advertising_run_loop;
nearby_connections_->StopAdvertising(
base::BindLambdaForTesting([&](mojom::Status status) {
kServiceId, base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kSuccess, status);
stop_advertising_run_loop.Quit();
}));
......@@ -983,7 +980,7 @@ TEST_F(NearbyConnectionsTest, DisconnectAllEndpoints) {
base::RunLoop stop_endpoints_run_loop;
nearby_connections_->StopAllEndpoints(
base::BindLambdaForTesting([&](mojom::Status status) {
kServiceId, base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kSuccess, status);
stop_endpoints_run_loop.Quit();
}));
......@@ -1000,7 +997,7 @@ TEST_F(NearbyConnectionsTest, InitiateBandwidthUpgradeFails) {
EndpointData endpoint_data = CreateEndpointData(1);
base::RunLoop bandwidth_upgrade_run_loop;
nearby_connections_->InitiateBandwidthUpgrade(
endpoint_data.remote_endpoint_id,
kServiceId, endpoint_data.remote_endpoint_id,
base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kOutOfOrderApiCall, status);
bandwidth_upgrade_run_loop.Quit();
......@@ -1020,7 +1017,7 @@ TEST_F(NearbyConnectionsTest, InitiateBandwidthUpgradeAfterDiscoveringFails) {
// Requesting a bandwidth upgrade should fail.
base::RunLoop bandwidth_upgrade_run_loop;
nearby_connections_->InitiateBandwidthUpgrade(
endpoint_data.remote_endpoint_id,
kServiceId, endpoint_data.remote_endpoint_id,
base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kOutOfOrderApiCall, status);
bandwidth_upgrade_run_loop.Quit();
......@@ -1037,7 +1034,7 @@ TEST_F(NearbyConnectionsTest, InitiateBandwidthUpgradeAfterAdvertisingFails) {
// Requesting a bandwidth upgrade should fail.
base::RunLoop bandwidth_upgrade_run_loop;
nearby_connections_->InitiateBandwidthUpgrade(
endpoint_data.remote_endpoint_id,
kServiceId, endpoint_data.remote_endpoint_id,
base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kOutOfOrderApiCall, status);
bandwidth_upgrade_run_loop.Quit();
......@@ -1066,7 +1063,7 @@ TEST_F(NearbyConnectionsTest, InitiateBandwidthUpgradeAfterConnectionSucceeds) {
// Requesting a bandwidth upgrade should succeed.
base::RunLoop bandwidth_upgrade_run_loop;
nearby_connections_->InitiateBandwidthUpgrade(
endpoint_data.remote_endpoint_id,
kServiceId, endpoint_data.remote_endpoint_id,
base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kSuccess, status);
bandwidth_upgrade_run_loop.Quit();
......@@ -1139,7 +1136,7 @@ TEST_F(NearbyConnectionsTest, ReceiveFilePayload) {
base::RunLoop register_payload_run_loop;
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) {
EXPECT_EQ(mojom::Status::kSuccess, status);
register_payload_run_loop.Quit();
......@@ -1217,7 +1214,7 @@ TEST_F(NearbyConnectionsTest, ReceiveFilePayloadNotRegistered) {
TEST_F(NearbyConnectionsTest, RegisterPayloadFileInvalid) {
base::RunLoop register_payload_run_loop;
nearby_connections_->RegisterPayloadFile(
kPayloadId, base::File(), base::File(),
kServiceId, kPayloadId, base::File(), base::File(),
base::BindLambdaForTesting([&](mojom::Status status) {
EXPECT_EQ(mojom::Status::kError, status);
register_payload_run_loop.Quit();
......
......@@ -44,13 +44,16 @@ class MockNearbyConnections : public NearbyConnectionsMojom {
MOCK_METHOD(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,
AdvertisingOptionsPtr,
mojo::PendingRemote<ConnectionLifecycleListener>,
StartDiscoveryCallback),
(override));
MOCK_METHOD(void, StopAdvertising, (StopAdvertisingCallback), (override));
MOCK_METHOD(void,
StopAdvertising,
(const std::string& service_id, StopAdvertisingCallback),
(override));
MOCK_METHOD(void,
StartDiscovery,
(const std::string& service_id,
......@@ -58,10 +61,14 @@ class MockNearbyConnections : public NearbyConnectionsMojom {
mojo::PendingRemote<EndpointDiscoveryListener>,
StartDiscoveryCallback),
(override));
MOCK_METHOD(void, StopDiscovery, (StopDiscoveryCallback), (override));
MOCK_METHOD(void,
StopDiscovery,
(const std::string& service_id, StopDiscoveryCallback),
(override));
MOCK_METHOD(void,
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,
ConnectionOptionsPtr options,
mojo::PendingRemote<ConnectionLifecycleListener>,
......@@ -69,41 +76,51 @@ class MockNearbyConnections : public NearbyConnectionsMojom {
(override));
MOCK_METHOD(void,
DisconnectFromEndpoint,
(const std::string& endpoint_id, DisconnectFromEndpointCallback),
(const std::string& service_id,
const std::string& endpoint_id,
DisconnectFromEndpointCallback),
(override));
MOCK_METHOD(void,
AcceptConnection,
(const std::string& endpoint_id,
(const std::string& service_id,
const std::string& endpoint_id,
mojo::PendingRemote<PayloadListener> listener,
AcceptConnectionCallback callback),
(override));
MOCK_METHOD(void,
RejectConnection,
(const std::string& endpoint_id,
(const std::string& service_id,
const std::string& endpoint_id,
RejectConnectionCallback callback),
(override));
MOCK_METHOD(void,
SendPayload,
(const std::vector<std::string>& endpoint_ids,
(const std::string& service_id,
const std::vector<std::string>& endpoint_ids,
PayloadPtr payload,
SendPayloadCallback callback),
(override));
MOCK_METHOD(void,
CancelPayload,
(int64_t payload_id, CancelPayloadCallback callback),
(const std::string& service_id,
int64_t payload_id,
CancelPayloadCallback callback),
(override));
MOCK_METHOD(void,
StopAllEndpoints,
(DisconnectFromEndpointCallback callback),
(const std::string& service_id,
DisconnectFromEndpointCallback callback),
(override));
MOCK_METHOD(void,
InitiateBandwidthUpgrade,
(const std::string& endpoint_id,
(const std::string& service_id,
const std::string& endpoint_id,
InitiateBandwidthUpgradeCallback callback),
(override));
MOCK_METHOD(void,
RegisterPayloadFile,
(int64_t payload_id,
(const std::string& service_id,
int64_t payload_id,
base::File input_file,
base::File output_file,
RegisterPayloadFileCallback callback),
......
......@@ -127,13 +127,13 @@ interface NearbyConnections {
// advertisement can be active at a time and calling StartAdvertising() while
// 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.
// This can be an arbitrary string, so long as it uniquely
// identifies your service. A good default is to use your
// 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.
// listener - An interface notified when remote endpoints request a
// connection to this endpoint.
......@@ -142,7 +142,7 @@ interface NearbyConnections {
// Status::kAlreadyAdvertising if the app is already advertising.
// Status::kOutOfOrderApiCall if the app is currently connected to remote
// endpoints; call StopAllEndpoints first.
StartAdvertising(array<uint8> endpoint_info, string service_id,
StartAdvertising(string service_id, array<uint8> endpoint_info,
AdvertisingOptions options,
pending_remote<ConnectionLifecycleListener> listener)
=> (Status status);
......@@ -152,9 +152,11 @@ interface NearbyConnections {
// itself or goes inactive. Payloads can still be sent to connected
// endpoints after advertising ends.
//
// service_id - Identifier used in the preceding StartAdvertising() call.
//
// Possible return values include:
// 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.
//
......@@ -177,12 +179,15 @@ interface NearbyConnections {
// goes inactive. Payloads can still be sent to connected endpoints after
// discovery ends.
//
// service_id - Identifier used in the preceding StartDiscovery() call.
//
// Possible return values include:
// 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.
//
// service_id - Service ID used to discover the endpoint.
// endpoint_info - The local info including a human readable name to appear on
// the remote endpoint.
// endpoint_id - The identifier for the remote endpoint to which a
......@@ -201,7 +206,9 @@ interface NearbyConnections {
// Status::kWifiLanError if we failed to connect because of an
// issue with WiFi.
// Status::kError if we failed to connect for any other reason.
RequestConnection(array<uint8> endpoint_info, string endpoint_id,
RequestConnection(string service_id,
array<uint8> endpoint_info,
string endpoint_id,
ConnectionOptions options,
pending_remote<ConnectionLifecycleListener> listener)
=> (Status status);
......@@ -209,6 +216,7 @@ interface NearbyConnections {
// Accepts a connection to a remote endpoint. This method must be called
// 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
// value provided in a call to
// ConnectionsLifecycleListener::OnConnectionInitiated().
......@@ -220,11 +228,14 @@ interface NearbyConnections {
// Status::kOutOfOrderApiCall if
// ConnectionsLifecycleListener::OnConnectionInitiated() has not been
// 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);
// 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
// value provided in a call to
// ConnectionsLifecycleListener::OnConnectionInitiated().
......@@ -235,20 +246,23 @@ interface NearbyConnections {
// Status::kOutOfOrderApiCall if
// ConnectionsLifecycleListener::OnConnectionInitiated() has not been
// 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
// 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.
// Possible return values include:
// 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
// remote endpoints once a notice of connection acceptance has been delivered
// via ConnectionsLifecycleListener::OnConnectionResult().
//
// service_id - Service ID used to bootstrap the connection.
// endpoint_ids - Array of remote endpoint identifiers for to which the
// payload should be sent.
// payload - The Payload to be sent.
......@@ -263,14 +277,18 @@ interface NearbyConnections {
// still occur during transmission (and at different times for different
// endpoints), and will be delivered via
// 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).
//
// service_id - Service ID used to bootstrap the connection.
//
// payload_id - The identifier for the Payload to be cancelled.
// Possible return values include:
// 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
// discovered endpoints. As a side effect of this call, both
......@@ -278,28 +296,37 @@ interface NearbyConnections {
// StopAllEndpoints, no further operations with remote endpoints will
// be possible until a new call to one of StartAdvertising or
// StartDiscovery.
//
// service_id - Service ID used to discover endpoints.
//
// Possible return values include:
// Status::kSuccess disconnected from all endpoints successfully.
StopAllEndpoints() => (Status status);
StopAllEndpoints(string service_id) => (Status status);
// 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
// switching to a higher connection data rate and possibly
// different wireless protocol. On success, calls
// ConnectionLifecycleListener::OnBandwidthChanged.
// Possible return values include:
// 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
// payload associated with |payload_id_|, which is determined by feature
// specific design. The |input_file| should be opened for read access, and
// |output_file| should be opened for write access.
//
// service_id - Service ID used to discover the endpoint.
//
// Possible return values include:
// Status::kSuccess if file is registered successfully.
// 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.File output_file)
=> (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