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

Add NearbyConnectionsManager and NearbyConnection interface

Bug: 1084574
Change-Id: I55c4f059f473737589d9f8c896d71bcfbbe13098
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2212127Reviewed-by: default avatarRichard Knoll <knollr@chromium.org>
Commit-Queue: Alex Chau <alexchau@chromium.org>
Cr-Commit-Position: refs/heads/master@{#771164}
parent d6520b7d
...@@ -3407,6 +3407,9 @@ static_library("browser") { ...@@ -3407,6 +3407,9 @@ static_library("browser") {
"nearby_sharing/fast_initiation_manager.h", "nearby_sharing/fast_initiation_manager.h",
"nearby_sharing/file_attachment.cc", "nearby_sharing/file_attachment.cc",
"nearby_sharing/file_attachment.h", "nearby_sharing/file_attachment.h",
"nearby_sharing/nearby_connection.h",
"nearby_sharing/nearby_connections_manager.h",
"nearby_sharing/nearby_constants.h",
"nearby_sharing/nearby_sharing_prefs.cc", "nearby_sharing/nearby_sharing_prefs.cc",
"nearby_sharing/nearby_sharing_prefs.h", "nearby_sharing/nearby_sharing_prefs.h",
"nearby_sharing/nearby_sharing_service.h", "nearby_sharing/nearby_sharing_service.h",
......
// 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_BROWSER_NEARBY_SHARING_NEARBY_CONNECTION_H_
#define CHROME_BROWSER_NEARBY_SHARING_NEARBY_CONNECTION_H_
#include <stdint.h>
#include <string>
#include <vector>
#include "base/callback.h"
#include "base/optional.h"
// A socket-like wrapper around Nearby Connections that allows for asynchronous
// reads and writes.
class NearbyConnection {
public:
using ReadCallback =
base::OnceCallback<void(base::Optional<std::vector<uint8_t>> bytes)>;
using WriteCallback = base::OnceCallback<void(bool result)>;
virtual ~NearbyConnection() = default;
// Reads a stream of bytes from the remote device. Invoke |callback| when
// there is incoming data or when the socket is closed.
virtual void Read(ReadCallback callback) = 0;
// Writes an outgoing stream of bytes to the remote device asynchronously.
// Invoke |callback| with True if successful, False if failed or socket is
// closed.
virtual void Write(std::vector<uint8_t> bytes, WriteCallback callback) = 0;
// Closes the socket and disconnects from the remote device.
virtual void Close() = 0;
// Return True if the socket is closed, False otherwise.
virtual bool IsClosed();
// Listens to the socket being closed. Invoke |callback| when the socket is
// closed.
virtual void RegisterForDisconnection(base::OnceClosure callback) = 0;
};
#endif // CHROME_BROWSER_NEARBY_SHARING_NEARBY_CONNECTION_H_
// 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_BROWSER_NEARBY_SHARING_NEARBY_CONNECTIONS_MANAGER_H_
#define CHROME_BROWSER_NEARBY_SHARING_NEARBY_CONNECTIONS_MANAGER_H_
#include <stdint.h>
#include <vector>
#include "base/callback.h"
#include "base/optional.h"
#include "chrome/browser/nearby_sharing/nearby_connection.h"
#include "chrome/browser/nearby_sharing/nearby_constants.h"
#include "chrome/services/sharing/public/mojom/nearby_connections_types.mojom.h"
// A wrapper around the Nearby Connections mojo API.
class NearbyConnectionsManager {
public:
using Payload = location::nearby::connections::mojom::Payload;
using ConnectionsStatus = location::nearby::connections::mojom::Status;
using ConnectionsCallback =
base::OnceCallback<void(ConnectionsStatus status)>;
// A callback for handling incoming connections while advertising.
class IncomingConnectionListener {
virtual ~IncomingConnectionListener() = default;
virtual void OnIncomingConnection(const std::string& endpoint_id,
const std::vector<uint8_t>& endpoint_info,
NearbyConnection connection) = 0;
};
// A callback for handling discovered devices while discovering.
class DiscoveryListener {
virtual ~DiscoveryListener() = default;
virtual void OnEndpointDiscovered(
const std::string& endpoint_id,
const std::vector<uint8_t>& endpoint_info) = 0;
virtual void OnEndpointLost(const std::string& endpoint_id) = 0;
};
// A callback for tracking the status of a payload (both incoming and
// outgoing).
class PayloadStatusListener {
using PayloadTransferUpdate =
location::nearby::connections::mojom::PayloadTransferUpdate;
virtual ~PayloadStatusListener() = default;
virtual void OnStatusUpdate(PayloadTransferUpdate update) = 0;
};
virtual ~NearbyConnectionsManager() = default;
// Disconnects from all endpoints and shut down Nearby Connections.
virtual void Shutdown() = 0;
// Starts advertising through Nearby Connections. Caller is expected to ensure
// |listener| remains valid until StopAdvertising is called.
virtual void StartAdvertising(std::vector<uint8_t> endpoint_info,
IncomingConnectionListener* listener,
PowerLevel power_level,
DataUsage data_usage,
ConnectionsCallback callback) = 0;
// Stops advertising through Nearby Connections.
virtual void StopAdvertising() = 0;
// Starts discovery through Nearby Connections. Caller is expected to ensure
// |listener| remains valid until StopDiscovery is called.
virtual void StartDiscovery(std::vector<uint8_t> endpoint_info,
DiscoveryListener* listener,
ConnectionsCallback callback) = 0;
// Stops discovery through Nearby Connections.
virtual void StopDiscovery() = 0;
// Conntects to remote |endpoint_id| through Nearby Connections.
virtual NearbyConnection Connect(
std::vector<uint8_t> endpoint_info,
const std::string& endpoint_id,
base::Optional<std::vector<uint8_t>> bluetooth_mac_address,
DataUsage data_usage,
ConnectionsCallback callback) = 0;
// Disconnects from remote |endpoint_id| through Nearby Connections.
virtual void Disconnect(const std::string& endpoint_id) = 0;
// Sends |payload| through Nearby Connections. Caller is expected to ensure
// |listener| remains valid until kSuccess/kFailure/kCancelled is invoked with
// OnStatusUpdate.
virtual void Send(const std::string& endpoint_id,
Payload payload,
PayloadStatusListener* listener,
ConnectionsCallback callback) = 0;
// Register a |listener| with |payload_id|. Caller is expected to ensure
// |listener| remains valid until kSuccess/kFailure/kCancelled is invoked with
// OnStatusUpdate.
virtual void RegisterPayloadStatusListener(
int64_t payload_id,
PayloadStatusListener* listener) = 0;
// Gets the payload associated with |payload_id| if available.
virtual base::Optional<Payload> GetIncomingPayload(int64_t payload_id);
// Cancels a Payload currently in-flight to or from remote endpoints.
virtual void Cancel(int64_t payload_id, ConnectionsCallback callback) = 0;
// Clears all incoming payloads.
virtual void ClearIncomingPayloads();
// Gets the raw authentication token for the |endpoint_id|.
virtual base::Optional<std::vector<uint8_t>> GetRawAuthenticationToken(
const std::string& endpoint_id) = 0;
};
#endif // CHROME_BROWSER_NEARBY_SHARING_NEARBY_CONNECTIONS_MANAGER_H_
// 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_BROWSER_NEARBY_SHARING_NEARBY_CONSTANTS_H_
#define CHROME_BROWSER_NEARBY_SHARING_NEARBY_CONSTANTS_H_
// Represents the advertising bluetooth power for Nearby Connections.
enum class PowerLevel {
kLowPower,
kMediumPower,
kHighPower,
kMaxValue = kHighPower
};
// Represents the data usage preference.
enum class DataUsage {
// User is never willing to use the Internet
kOffline,
// User is always willing to use the Internet
kOnline,
// User is willing to use the Internet on an unmetered connection.
kWifiOnly,
kMaxValue = kWifiOnly
};
#endif // CHROME_BROWSER_NEARBY_SHARING_NEARBY_CONSTANTS_H_
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "base/memory/singleton.h" #include "base/memory/singleton.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "chrome/browser/browser_features.h" #include "chrome/browser/browser_features.h"
#include "chrome/browser/nearby_sharing/nearby_connections_manager.h"
#include "chrome/browser/nearby_sharing/nearby_sharing_prefs.h" #include "chrome/browser/nearby_sharing/nearby_sharing_prefs.h"
#include "chrome/browser/nearby_sharing/nearby_sharing_service_factory.h" #include "chrome/browser/nearby_sharing/nearby_sharing_service_factory.h"
#include "chrome/browser/nearby_sharing/nearby_sharing_service_impl.h" #include "chrome/browser/nearby_sharing/nearby_sharing_service_impl.h"
...@@ -57,7 +58,8 @@ KeyedService* NearbySharingServiceFactory::BuildServiceInstanceFor( ...@@ -57,7 +58,8 @@ KeyedService* NearbySharingServiceFactory::BuildServiceInstanceFor(
} }
VLOG(1) << __func__ << ": creating NearbySharingService."; VLOG(1) << __func__ << ": creating NearbySharingService.";
return new NearbySharingServiceImpl(Profile::FromBrowserContext(context)); return new NearbySharingServiceImpl(Profile::FromBrowserContext(context),
nullptr /* nearby_connections_manager */);
} }
content::BrowserContext* NearbySharingServiceFactory::GetBrowserContextToUse( content::BrowserContext* NearbySharingServiceFactory::GetBrowserContextToUse(
......
...@@ -5,8 +5,14 @@ ...@@ -5,8 +5,14 @@
#include "chrome/browser/nearby_sharing/nearby_sharing_service_impl.h" #include "chrome/browser/nearby_sharing/nearby_sharing_service_impl.h"
#include "base/logging.h" #include "base/logging.h"
#include "chrome/browser/nearby_sharing/nearby_connections_manager.h"
NearbySharingServiceImpl::NearbySharingServiceImpl(Profile* profile) {} NearbySharingServiceImpl::NearbySharingServiceImpl(
Profile* profile,
std::unique_ptr<NearbyConnectionsManager> nearby_connections_manager)
: nearby_connections_manager_(std::move(nearby_connections_manager)) {}
NearbySharingServiceImpl::~NearbySharingServiceImpl() = default;
void NearbySharingServiceImpl::RegisterSendSurface( void NearbySharingServiceImpl::RegisterSendSurface(
TransferUpdateCallback* transferCallback, TransferUpdateCallback* transferCallback,
......
...@@ -5,16 +5,21 @@ ...@@ -5,16 +5,21 @@
#ifndef CHROME_BROWSER_NEARBY_SHARING_NEARBY_SHARING_SERVICE_IMPL_H_ #ifndef CHROME_BROWSER_NEARBY_SHARING_NEARBY_SHARING_SERVICE_IMPL_H_
#define CHROME_BROWSER_NEARBY_SHARING_NEARBY_SHARING_SERVICE_IMPL_H_ #define CHROME_BROWSER_NEARBY_SHARING_NEARBY_SHARING_SERVICE_IMPL_H_
#include <memory>
#include "chrome/browser/nearby_sharing/nearby_sharing_service.h" #include "chrome/browser/nearby_sharing/nearby_sharing_service.h"
#include "components/keyed_service/core/keyed_service.h" #include "components/keyed_service/core/keyed_service.h"
class NearbyConnectionsManager;
class Profile; class Profile;
class NearbySharingServiceImpl : public NearbySharingService, class NearbySharingServiceImpl : public NearbySharingService,
public KeyedService { public KeyedService {
public: public:
explicit NearbySharingServiceImpl(Profile* profile); explicit NearbySharingServiceImpl(
~NearbySharingServiceImpl() override = default; Profile* profile,
std::unique_ptr<NearbyConnectionsManager> nearby_connections_manager);
~NearbySharingServiceImpl() override;
// NearbySharingService: // NearbySharingService:
void RegisterSendSurface(TransferUpdateCallback* transferCallback, void RegisterSendSurface(TransferUpdateCallback* transferCallback,
...@@ -44,6 +49,9 @@ class NearbySharingServiceImpl : public NearbySharingService, ...@@ -44,6 +49,9 @@ class NearbySharingServiceImpl : public NearbySharingService,
StatusCodesCallback status_codes_callback) override; StatusCodesCallback status_codes_callback) override;
void Open(const ShareTarget& share_target, void Open(const ShareTarget& share_target,
StatusCodesCallback status_codes_callback) override; StatusCodesCallback status_codes_callback) override;
private:
std::unique_ptr<NearbyConnectionsManager> nearby_connections_manager_;
}; };
#endif // CHROME_BROWSER_NEARBY_SHARING_NEARBY_SHARING_SERVICE_IMPL_H_ #endif // CHROME_BROWSER_NEARBY_SHARING_NEARBY_SHARING_SERVICE_IMPL_H_
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