Commit ff2bc9e6 authored by Alex Chau's avatar Alex Chau Committed by Commit Bot

[Nearby] Implement outgoing payload NearbyConnections interface

chrome/services/sharing/*
- Introduced AcceptConnection, RejectConnection for accepting/rejecting
  connection with remote endpoints, AcceptConnection also pass a
  PayloadListener
- Introduced SendPayload and CancelPayload for sedning/canceling
  outgoing payloads

chrome/browser/nearby_sharing/nearby_connections_manager*
- Browser side usage of the above APIs
  NearbyConnectionsManager uses AcceptConnection to accept connection,
  and implements PadloadListener.
  For outgoing payloads, OnPayloadTransferUpdate will pass the status to
  its listeners directly
- SendPayload and CancelPayload are straight forward

Bug: 1076008
Change-Id: If32e703e1d701373df1833719214039a990fafa8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2339670Reviewed-by: default avatarAlex Gough <ajgo@chromium.org>
Reviewed-by: default avatarRichard Knoll <knollr@chromium.org>
Commit-Queue: Alex Chau <alexchau@chromium.org>
Cr-Commit-Position: refs/heads/master@{#797170}
parent cd7b7fff
......@@ -66,8 +66,7 @@ void FakeNearbyConnectionsManager::Disconnect(const std::string& endpoint_id) {
void FakeNearbyConnectionsManager::Send(const std::string& endpoint_id,
PayloadPtr payload,
PayloadStatusListener* listener,
ConnectionsCallback callback) {
PayloadStatusListener* listener) {
DCHECK(!IsShutdown());
// TODO(alexchau): Implement.
}
......@@ -86,8 +85,7 @@ FakeNearbyConnectionsManager::GetIncomingPayload(int64_t payload_id) {
return nullptr;
}
void FakeNearbyConnectionsManager::Cancel(int64_t payload_id,
ConnectionsCallback callback) {
void FakeNearbyConnectionsManager::Cancel(int64_t payload_id) {
DCHECK(!IsShutdown());
// TODO(alexchau): Implement.
}
......
......@@ -41,12 +41,11 @@ class FakeNearbyConnectionsManager
void Disconnect(const std::string& endpoint_id) override;
void Send(const std::string& endpoint_id,
PayloadPtr payload,
PayloadStatusListener* listener,
ConnectionsCallback callback) override;
PayloadStatusListener* listener) override;
void RegisterPayloadStatusListener(int64_t payload_id,
PayloadStatusListener* listener) override;
Payload* GetIncomingPayload(int64_t payload_id) override;
void Cancel(int64_t payload_id, ConnectionsCallback callback) override;
void Cancel(int64_t payload_id) override;
void ClearIncomingPayloads() override;
base::Optional<std::vector<uint8_t>> GetRawAuthenticationToken(
const std::string& endpoint_id) override;
......
......@@ -19,6 +19,8 @@ using EndpointDiscoveryListener =
location::nearby::connections::mojom::EndpointDiscoveryListener;
using ConnectionLifecycleListener =
location::nearby::connections::mojom::ConnectionLifecycleListener;
using PayloadListener = location::nearby::connections::mojom::PayloadListener;
using PayloadPtr = location::nearby::connections::mojom::PayloadPtr;
class MockNearbyConnections : public NearbyConnectionsMojom {
public:
......@@ -55,6 +57,27 @@ class MockNearbyConnections : public NearbyConnectionsMojom {
DisconnectFromEndpoint,
(const std::string& endpoint_id, DisconnectFromEndpointCallback),
(override));
MOCK_METHOD(void,
AcceptConnection,
(const std::string& endpoint_id,
mojo::PendingRemote<PayloadListener> listener,
AcceptConnectionCallback callback),
(override));
MOCK_METHOD(void,
RejectConnection,
(const std::string& endpoint_id,
RejectConnectionCallback callback),
(override));
MOCK_METHOD(void,
SendPayload,
(const std::vector<std::string>& endpoint_ids,
PayloadPtr payload,
SendPayloadCallback callback),
(override));
MOCK_METHOD(void,
CancelPayload,
(int64_t payload_id, CancelPayloadCallback callback),
(override));
};
#endif // CHROME_BROWSER_NEARBY_SHARING_MOCK_NEARBY_CONNECTIONS_H_
......@@ -43,7 +43,7 @@ void NearbyConnectionImpl::Write(std::vector<uint8_t> bytes) {
payload->content =
PayloadContent::NewBytes(BytesPayload::New(std::move(bytes)));
nearby_connections_manager_->Send(endpoint_id_, std::move(payload),
/*listener=*/nullptr, base::DoNothing());
/*listener=*/nullptr);
}
void NearbyConnectionImpl::Close() {
......
......@@ -54,12 +54,13 @@ class NearbyConnectionsManager {
// A callback for tracking the status of a payload (both incoming and
// outgoing).
class PayloadStatusListener {
using PayloadTransferUpdate =
location::nearby::connections::mojom::PayloadTransferUpdate;
public:
using PayloadTransferUpdatePtr =
location::nearby::connections::mojom::PayloadTransferUpdatePtr;
virtual ~PayloadStatusListener() = default;
virtual void OnStatusUpdate(PayloadTransferUpdate update) = 0;
virtual void OnStatusUpdate(PayloadTransferUpdatePtr update) = 0;
};
virtual ~NearbyConnectionsManager() = default;
......@@ -102,8 +103,7 @@ class NearbyConnectionsManager {
// OnStatusUpdate.
virtual void Send(const std::string& endpoint_id,
PayloadPtr payload,
PayloadStatusListener* listener,
ConnectionsCallback callback) = 0;
PayloadStatusListener* listener) = 0;
// Register a |listener| with |payload_id|. Caller is expected to ensure
// |listener| remains valid until kSuccess/kFailure/kCancelled is invoked with
......@@ -116,7 +116,7 @@ class NearbyConnectionsManager {
virtual Payload* GetIncomingPayload(int64_t payload_id) = 0;
// Cancels a Payload currently in-flight to or from remote endpoints.
virtual void Cancel(int64_t payload_id, ConnectionsCallback callback) = 0;
virtual void Cancel(int64_t payload_id) = 0;
// Clears all incoming payloads.
virtual void ClearIncomingPayloads() = 0;
......
......@@ -181,23 +181,21 @@ void NearbyConnectionsManagerImpl::Disconnect(const std::string& endpoint_id) {
void NearbyConnectionsManagerImpl::Send(const std::string& endpoint_id,
PayloadPtr payload,
PayloadStatusListener* listener,
ConnectionsCallback callback) {
if (!nearby_connections_) {
std::move(callback).Run(ConnectionsStatus::kError);
PayloadStatusListener* listener) {
if (!nearby_connections_)
return;
}
// TOOD(crbug/1076008): Implement.
if (listener)
RegisterPayloadStatusListener(payload->id, listener);
nearby_connections_->SendPayload({endpoint_id}, std::move(payload),
base::DoNothing());
}
void NearbyConnectionsManagerImpl::RegisterPayloadStatusListener(
int64_t payload_id,
PayloadStatusListener* listener) {
if (!nearby_connections_)
return;
// TOOD(crbug/1076008): Implement.
payload_status_listeners_.insert_or_assign(payload_id, listener);
}
NearbyConnectionsManagerImpl::Payload*
......@@ -206,14 +204,20 @@ NearbyConnectionsManagerImpl::GetIncomingPayload(int64_t payload_id) {
return nullptr;
}
void NearbyConnectionsManagerImpl::Cancel(int64_t payload_id,
ConnectionsCallback callback) {
if (!nearby_connections_) {
std::move(callback).Run(ConnectionsStatus::kError);
void NearbyConnectionsManagerImpl::Cancel(int64_t payload_id) {
if (!nearby_connections_)
return;
}
// TOOD(crbug/1076008): Implement.
auto it = payload_status_listeners_.find(payload_id);
if (it != payload_status_listeners_.end()) {
it->second->OnStatusUpdate(
PayloadTransferUpdate::New(payload_id, PayloadStatus::kCanceled,
/*total_bytes=*/0,
/*bytes_transferred=*/0));
payload_status_listeners_.erase(it);
}
nearby_connections_->CancelPayload(payload_id, base::DoNothing());
NS_LOG(INFO) << "Cancelling payload: " << payload_id;
}
void NearbyConnectionsManagerImpl::ClearIncomingPayloads() {
......@@ -300,9 +304,13 @@ void NearbyConnectionsManagerImpl::OnConnectionInitiated(
ConnectionInfoPtr info) {
auto result = connection_info_map_.emplace(endpoint_id, std::move(info));
DCHECK(result.second);
// TOOD(crbug/1076008): Implemnet AcceptConnection.
// nearby_connections_->AcceptConnection(
// endpoint_id, payload_listener_.BindNewPipeAndPassRemote());
mojo::PendingRemote<PayloadListener> payload_listener;
payload_listeners_.Add(this,
payload_listener.InitWithNewPipeAndPassReceiver());
nearby_connections_->AcceptConnection(
endpoint_id, std::move(payload_listener), base::DoNothing());
}
void NearbyConnectionsManagerImpl::OnConnectionAccepted(
......@@ -374,6 +382,29 @@ void NearbyConnectionsManagerImpl::OnBandwidthChanged(
// TODO(crbug/1111458): Support TransferManager.
}
void NearbyConnectionsManagerImpl::OnPayloadTransferUpdate(
const std::string& endpoint_id,
PayloadTransferUpdatePtr update) {
// If this is a payload we've registered for, then forward its status to the
// PayloadStatusListener. We don't need to do anything more with the payload.
auto it = payload_status_listeners_.find(update->payload_id);
if (it != payload_status_listeners_.end()) {
PayloadStatusListener* listener = it->second;
switch (update->status) {
case PayloadStatus::kInProgress:
break;
case PayloadStatus::kSuccess:
case PayloadStatus::kCanceled:
case PayloadStatus::kFailure:
payload_status_listeners_.erase(it);
break;
}
listener->OnStatusUpdate(std::move(update));
}
// TOOD(crbug/1076008): Handle incoming payload transfer.
}
bool NearbyConnectionsManagerImpl::BindNearbyConnections() {
if (!nearby_connections_) {
nearby_connections_ =
......
......@@ -24,7 +24,8 @@ class NearbyConnectionsManagerImpl
: public NearbyConnectionsManager,
public NearbyProcessManager::Observer,
public location::nearby::connections::mojom::EndpointDiscoveryListener,
public location::nearby::connections::mojom::ConnectionLifecycleListener {
public location::nearby::connections::mojom::ConnectionLifecycleListener,
public location::nearby::connections::mojom::PayloadListener {
public:
NearbyConnectionsManagerImpl(NearbyProcessManager* process_manager,
Profile* profile);
......@@ -52,12 +53,11 @@ class NearbyConnectionsManagerImpl
void Disconnect(const std::string& endpoint_id) override;
void Send(const std::string& endpoint_id,
PayloadPtr payload,
PayloadStatusListener* listener,
ConnectionsCallback callback) override;
PayloadStatusListener* listener) override;
void RegisterPayloadStatusListener(int64_t payload_id,
PayloadStatusListener* listener) override;
Payload* GetIncomingPayload(int64_t payload_id) override;
void Cancel(int64_t payload_id, ConnectionsCallback callback) override;
void Cancel(int64_t payload_id) override;
void ClearIncomingPayloads() override;
base::Optional<std::vector<uint8_t>> GetRawAuthenticationToken(
const std::string& endpoint_id) override;
......@@ -66,17 +66,23 @@ class NearbyConnectionsManagerImpl
private:
using AdvertisingOptions =
location::nearby::connections::mojom::AdvertisingOptions;
using MediumSelection = location::nearby::connections::mojom::MediumSelection;
using DiscoveryOptions =
location::nearby::connections::mojom::DiscoveryOptions;
using EndpointDiscoveryListener =
location::nearby::connections::mojom::EndpointDiscoveryListener;
using ConnectionInfoPtr =
location::nearby::connections::mojom::ConnectionInfoPtr;
using ConnectionLifecycleListener =
location::nearby::connections::mojom::ConnectionLifecycleListener;
using DiscoveredEndpointInfoPtr =
location::nearby::connections::mojom::DiscoveredEndpointInfoPtr;
using ConnectionInfoPtr =
location::nearby::connections::mojom::ConnectionInfoPtr;
using DiscoveryOptions =
location::nearby::connections::mojom::DiscoveryOptions;
using EndpointDiscoveryListener =
location::nearby::connections::mojom::EndpointDiscoveryListener;
using MediumSelection = location::nearby::connections::mojom::MediumSelection;
using PayloadListener = location::nearby::connections::mojom::PayloadListener;
using PayloadTransferUpdate =
location::nearby::connections::mojom::PayloadTransferUpdate;
using PayloadStatus = location::nearby::connections::mojom::PayloadStatus;
using PayloadTransferUpdatePtr =
location::nearby::connections::mojom::PayloadTransferUpdatePtr;
using Status = location::nearby::connections::mojom::Status;
FRIEND_TEST_ALL_PREFIXES(NearbyConnectionsManagerImplTest,
......@@ -87,12 +93,12 @@ class NearbyConnectionsManagerImpl
void OnNearbyProcessStarted() override;
void OnNearbyProcessStopped() override;
// mojom::EndpointDiscoveryListener:
// EndpointDiscoveryListener:
void OnEndpointFound(const std::string& endpoint_id,
DiscoveredEndpointInfoPtr info) override;
void OnEndpointLost(const std::string& endpoint_id) override;
// mojom::ConnectionLifecycleListener:
// ConnectionLifecycleListener:
void OnConnectionInitiated(const std::string& endpoint_id,
ConnectionInfoPtr info) override;
void OnConnectionAccepted(const std::string& endpoint_id) override;
......@@ -102,6 +108,10 @@ class NearbyConnectionsManagerImpl
void OnBandwidthChanged(const std::string& endpoint_id,
int32_t quality) override;
// PayloadListener:
void OnPayloadTransferUpdate(const std::string& endpoint_id,
PayloadTransferUpdatePtr update) override;
void OnConnectionRequested(const std::string& endpoint_id,
NearbyConnectionCallback callback,
ConnectionsStatus status);
......@@ -121,12 +131,15 @@ class NearbyConnectionsManagerImpl
// A map of endpoint_id to NearbyConnection.
base::flat_map<std::string, std::unique_ptr<NearbyConnectionImpl>>
connections_;
// A map of payload_id to PayloadStatusListener*.
base::flat_map<int64_t, PayloadStatusListener*> payload_status_listeners_;
ScopedObserver<NearbyProcessManager, NearbyProcessManager::Observer>
nearby_process_observer_{this};
mojo::Receiver<EndpointDiscoveryListener> endpoint_discovery_listener_{this};
mojo::ReceiverSet<ConnectionLifecycleListener>
connection_lifecycle_listeners_;
mojo::ReceiverSet<PayloadListener> payload_listeners_;
location::nearby::connections::mojom::NearbyConnections* nearby_connections_ =
nullptr;
......
......@@ -8,6 +8,8 @@
#include "chrome/browser/nearby_sharing/logging/logging.h"
#include "chrome/services/sharing/nearby/nearby_connections_conversions.h"
#include "chrome/services/sharing/public/mojom/nearby_connections_types.mojom.h"
#include "third_party/nearby/src/cpp/platform_v2/public/file.h"
#include "third_party/nearby/src/cpp/platform_v2/public/single_thread_executor.h"
namespace location {
namespace nearby {
......@@ -276,6 +278,77 @@ void NearbyConnections::DisconnectFromEndpoint(
ResultCallbackFromMojom(std::move(callback)));
}
void NearbyConnections::AcceptConnection(
const std::string& endpoint_id,
mojo::PendingRemote<mojom::PayloadListener> listener,
AcceptConnectionCallback callback) {
mojo::SharedRemote<mojom::PayloadListener> remote(std::move(listener));
PayloadListener payload_listener = {
.payload_progress_cb = [remote](const std::string& endpoint_id,
const PayloadProgressInfo& info) {
if (!remote)
return;
DCHECK_GE(info.total_bytes, 0);
DCHECK_GE(info.bytes_transferred, 0);
remote->OnPayloadTransferUpdate(
endpoint_id, mojom::PayloadTransferUpdate::New(
info.payload_id, PayloadStatusToMojom(info.status),
info.total_bytes, info.bytes_transferred));
}};
core_->AcceptConnection(endpoint_id, std::move(payload_listener),
ResultCallbackFromMojom(std::move(callback)));
}
void NearbyConnections::RejectConnection(const std::string& endpoint_id,
RejectConnectionCallback callback) {
core_->RejectConnection(endpoint_id,
ResultCallbackFromMojom(std::move(callback)));
}
void NearbyConnections::SendPayload(
const std::vector<std::string>& endpoint_ids,
mojom::PayloadPtr payload,
SendPayloadCallback callback) {
std::unique_ptr<Payload> core_payload;
switch (payload->content->which()) {
case mojom::PayloadContent::Tag::BYTES:
core_payload = std::make_unique<Payload>(
payload->id,
ByteArrayFromMojom(payload->content->get_bytes()->bytes));
break;
case mojom::PayloadContent::Tag::FILE:
int64_t file_size = payload->content->get_file()->file.GetLength();
outgoing_file_map_.insert_or_assign(
payload->id, std::move(payload->content->get_file()->file));
core_payload = std::make_unique<Payload>(
payload->id, InputFile(payload->id, file_size));
break;
}
core_->SendPayload(absl::MakeSpan(endpoint_ids), std::move(*core_payload),
ResultCallbackFromMojom(std::move(callback)));
}
void NearbyConnections::CancelPayload(int64_t payload_id,
CancelPayloadCallback callback) {
core_->CancelPayload(payload_id,
ResultCallbackFromMojom(std::move(callback)));
}
base::File NearbyConnections::ExtractFileForPayload(int64_t payload_id) {
auto file_it = outgoing_file_map_.find(payload_id);
if (file_it == outgoing_file_map_.end())
return base::File();
base::File file = std::move(file_it->second);
outgoing_file_map_.erase(file_it);
return file;
// TOOD(crbug/1076008): Handle incoming file payload.
}
} // namespace connections
} // namespace nearby
} // namespace location
......@@ -5,10 +5,15 @@
#ifndef CHROME_SERVICES_SHARING_NEARBY_NEARBY_CONNECTIONS_H_
#define CHROME_SERVICES_SHARING_NEARBY_NEARBY_CONNECTIONS_H_
#include <stdint.h>
#include <memory>
#include "base/callback_forward.h"
#include "base/containers/flat_map.h"
#include "base/files/file.h"
#include "base/memory/weak_ptr.h"
#include "base/optional.h"
#include "base/task/post_task.h"
#include "chrome/services/sharing/public/mojom/nearby_connections.mojom.h"
#include "chrome/services/sharing/public/mojom/webrtc_signaling_messenger.mojom.h"
#include "device/bluetooth/public/mojom/adapter.mojom.h"
......@@ -78,6 +83,19 @@ class NearbyConnections : public mojom::NearbyConnections {
RequestConnectionCallback callback) override;
void DisconnectFromEndpoint(const std::string& endpoint_id,
DisconnectFromEndpointCallback callback) override;
void AcceptConnection(const std::string& endpoint_id,
mojo::PendingRemote<mojom::PayloadListener> listener,
AcceptConnectionCallback callback) override;
void RejectConnection(const std::string& endpoint_id,
RejectConnectionCallback callback) override;
void SendPayload(const std::vector<std::string>& endpoint_ids,
mojom::PayloadPtr payload,
SendPayloadCallback callback) override;
void CancelPayload(int64_t payload_id,
CancelPayloadCallback callback) override;
// Return the file associated with |payload_id|.
base::File ExtractFileForPayload(int64_t payload_id);
private:
void OnDisconnect();
......@@ -95,6 +113,7 @@ class NearbyConnections : public mojom::NearbyConnections {
webrtc_signaling_messenger_;
std::unique_ptr<Core> core_;
base::flat_map<int64_t, base::File> outgoing_file_map_;
base::WeakPtrFactory<NearbyConnections> weak_ptr_factory_{this};
};
......
......@@ -81,6 +81,23 @@ std::vector<uint8_t> ByteArrayToMojom(const ByteArray& byte_array) {
byte_array.data() + byte_array.size());
}
ByteArray ByteArrayFromMojom(const std::vector<uint8_t>& byte_array) {
return ByteArray(std::string(byte_array.begin(), byte_array.end()));
}
mojom::PayloadStatus PayloadStatusToMojom(PayloadProgressInfo::Status status) {
switch (status) {
case PayloadProgressInfo::Status::kSuccess:
return mojom::PayloadStatus::kSuccess;
case PayloadProgressInfo::Status::kFailure:
return mojom::PayloadStatus::kFailure;
case PayloadProgressInfo::Status::kInProgress:
return mojom::PayloadStatus::kInProgress;
case PayloadProgressInfo::Status::kCanceled:
return mojom::PayloadStatus::kCanceled;
}
}
} // namespace connections
} // namespace nearby
} // namespace location
......@@ -28,6 +28,10 @@ ResultCallback ResultCallbackFromMojom(StatusCallback callback);
std::vector<uint8_t> ByteArrayToMojom(const ByteArray& byte_array);
ByteArray ByteArrayFromMojom(const std::vector<uint8_t>& byte_array);
mojom::PayloadStatus PayloadStatusToMojom(PayloadProgressInfo::Status status);
} // namespace connections
} // namespace nearby
} // namespace location
......
......@@ -19,6 +19,8 @@ source_set("platform_v2") {
"count_down_latch.cc",
"count_down_latch.h",
"crypto.cc",
"input_file.cc",
"input_file.h",
"log_message.cc",
"log_message.h",
"mutex.cc",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/services/sharing/nearby/platform_v2/input_file.h"
#include "base/logging.h"
namespace location {
namespace nearby {
namespace chrome {
InputFile::InputFile(base::File file) : file_(std::move(file)) {
DCHECK(file_.IsValid());
seek_succeeded_ = file_.Seek(base::File::FROM_BEGIN, 0) == 0;
}
InputFile::~InputFile() = default;
std::string InputFile::GetFilePath() const {
// File path is not supported.
return std::string();
}
std::int64_t InputFile::GetTotalSize() const {
return file_.GetLength();
}
ExceptionOr<ByteArray> InputFile::Read(std::int64_t size) {
if (!seek_succeeded_)
return Exception::kFailed;
ByteArray bytes(size);
int bytes_read = file_.ReadAtCurrentPos(bytes.data(), bytes.size());
if (bytes_read != size)
return Exception::kFailed;
return ExceptionOr<ByteArray>(std::move(bytes));
}
Exception InputFile::Close() {
file_.Close();
return {Exception::kSuccess};
}
} // namespace chrome
} // namespace nearby
} // namespace location
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_SERVICES_SHARING_NEARBY_PLATFORM_V2_INPUT_FILE_H_
#define CHROME_SERVICES_SHARING_NEARBY_PLATFORM_V2_INPUT_FILE_H_
#include "base/files/file.h"
#include "third_party/nearby/src/cpp/platform_v2/api/input_file.h"
namespace location {
namespace nearby {
namespace chrome {
// Concrete InputFile implementation.
class InputFile : public api::InputFile {
public:
explicit InputFile(base::File file);
~InputFile() override;
InputFile(const InputFile&) = delete;
InputFile& operator=(const InputFile&) = delete;
// api::InputFile:
std::string GetFilePath() const override;
std::int64_t GetTotalSize() const override;
ExceptionOr<ByteArray> Read(std::int64_t size) override;
Exception Close() override;
private:
// File::GetLength is not const but api::InputFile::GetTotalSize is const.
mutable base::File file_;
bool seek_succeeded_;
};
} // namespace chrome
} // namespace nearby
} // namespace location
#endif // CHROME_SERVICES_SHARING_NEARBY_PLATFORM_V2_INPUT_FILE_H_
......@@ -13,6 +13,7 @@
#include "chrome/services/sharing/nearby/platform_v2/bluetooth_classic_medium.h"
#include "chrome/services/sharing/nearby/platform_v2/condition_variable.h"
#include "chrome/services/sharing/nearby/platform_v2/count_down_latch.h"
#include "chrome/services/sharing/nearby/platform_v2/input_file.h"
#include "chrome/services/sharing/nearby/platform_v2/log_message.h"
#include "chrome/services/sharing/nearby/platform_v2/mutex.h"
#include "chrome/services/sharing/nearby/platform_v2/recursive_mutex.h"
......@@ -104,8 +105,12 @@ std::unique_ptr<AtomicBoolean> ImplementationPlatform::CreateAtomicBoolean(
std::unique_ptr<InputFile> ImplementationPlatform::CreateInputFile(
std::int64_t payload_id,
std::int64_t total_size) {
return std::make_unique<shared::InputFile>(GetPayloadPath(payload_id),
total_size);
auto& connections = connections::NearbyConnections::GetInstance();
auto file = connections.ExtractFileForPayload(payload_id);
if (!file.IsValid())
return nullptr;
return std::make_unique<chrome::InputFile>(std::move(file));
}
std::unique_ptr<OutputFile> ImplementationPlatform::CreateOutputFile(
......
......@@ -91,6 +91,20 @@ interface ConnectionLifecycleListener {
OnBandwidthChanged(string endpoint_id, int32 quality);
};
// Listener for payload status. Methods in this interface are called from
// utiltiiy process, and is used by the browser process to listen for payload
// status associated with remote endpoints.
interface PayloadListener {
// Called with progress information about an active Payload transfer, either
// incoming or outgoing.
//
// endpoint_id - The identifier for the remote endpoint that is sending or
// receiving this payload.
// update - The PayloadTransferUpdate structure describing the status of
// the transfer.
OnPayloadTransferUpdate(string endpoint_id, PayloadTransferUpdate update);
};
// Main interface to control the NearbyConnections library. Implemented in a
// sandboxed process. This interface is used by the browser process to connect
// to remote devices and send / receive raw data packets. Parsing of those
......@@ -179,6 +193,37 @@ interface NearbyConnections {
pending_remote<ConnectionLifecycleListener> listener)
=> (Status status);
// Accepts a connection to a remote endpoint. This method must be called
// before Payloads can be exchanged with the remote endpoint.
//
// endpoint_id - The identifier for the remote endpoint. Should match the
// value provided in a call to
// ConnectionsLifecycleListener::OnConnectionInitiated().
// listener - An interface for payloads exchanged with the remote endpoint.
// Possible return values include:
// Status::kSuccess if the connection request was accepted.
// Status::kAlreadyConnectedToEndpoint if the app already as a connection
// to the specified endpoint.
// Status::kOutOfOrderApiCall if
// ConnectionsLifecycleListener::OnConnectionInitiated() has not been
// called for the |endpoint_id|.
AcceptConnection(string endpoint_id, pending_remote<PayloadListener> listener)
=> (Status status);
// Rejects a connection to a remote endpoint.
//
// endpoint_id - The identifier for the remote endpoint. Should match the
// value provided in a call to
// ConnectionsLifecycleListener::OnConnectionInitiated().
// Possible return values include:
// Status::kSuccess if the connection request was rejected.
// Status::kAlreadyConnectedToEndpoint if the app already has a connection
// to the specified endpoint.
// Status::kOutOfOrderApiCall if
// ConnectionsLifecycleListener::OnConnectionInitiated() has not been
// called for the |endpoint_id|.
RejectConnection(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.
//
......@@ -186,6 +231,33 @@ interface NearbyConnections {
// Possible return values include:
// Status::kSuccess disconnected successfully.
DisconnectFromEndpoint(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().
//
// endpoint_ids - Array of remote endpoint identifiers for to which the
// payload should be sent.
// payload - The Payload to be sent.
// Possible return values include:
// Status::kOutOfOrderApiCall if the device has not first performed
// advertisement or discovery (to set the Strategy).
// Status::kEndpointUnknown if there's no active (or pending) connection to
// the remote endpoint.
// Status::kSuccess if none of the above errors occurred. Note that this
// indicates that Nearby Connections will attempt to send the Payload,
// but not that the send has successfully completed yet. Errors might
// 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);
// Cancels a Payload currently in-flight to or from remote endpoint(s).
//
// payload_id - The identifier for the Payload to be canceled.
// Possible return values include:
// Status::kSuccess if the payload got canceled.
CancelPayload(int64 payload_id) => (Status status);
};
// Provide all the dependencies that NearbyConnections library requires.
......
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