Skip to content

Commit

Permalink
[Nearby Presence] Create mojo interface for RequestConnectionV3
Browse files Browse the repository at this point in the history
Context/Design: go/cros-nearby-presence-np-nc-interop

This CL creates the RequestConnectionV3 function in
nearby_connections.mojom. This function operates similarly as the
RequestConnection function but to be used by Nearby Presence. Instead
of passing in the endpoint ID and info we are instead passing a
NearbyDevice (see design). A follow-up CL will use this new
functionality in the NearbyConnectionsManager.

Low-Coverage-Reason: OTHER ConnectionListenerV3 was introduced and only
has coverage for initiated_cb. Future CLs that implement
Accept/RejectConnectionV3, DisconnectFromDeviceV3, and
BandwidthUpgradeInitiatedV3 will have additional tests that provide
coverage for the rest of the listener.

Test: unit tests added
Bug: b/287336280, b/307319934
Change-Id: I6805aafe5d4a95fd01c41e163f593020870650f8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4937670
Commit-Queue: Crisrael Lucero <crisrael@google.com>
Reviewed-by: Ryan Hansberry <hansberry@chromium.org>
Reviewed-by: Matthew Denton <mpdenton@chromium.org>
Reviewed-by: Juliet Lévesque <julietlevesque@google.com>
Cr-Commit-Position: refs/heads/main@{#1216357}
  • Loading branch information
crisrael authored and Chromium LUCI CQ committed Oct 27, 2023
1 parent 50fc44e commit bffa63b
Show file tree
Hide file tree
Showing 9 changed files with 393 additions and 21 deletions.
105 changes: 100 additions & 5 deletions chrome/services/sharing/nearby/nearby_connections.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
#include "base/time/time.h"
#include "chrome/browser/nearby_sharing/logging/logging.h"
#include "chrome/services/sharing/nearby/nearby_connections_conversions.h"
#include "chrome/services/sharing/nearby/nearby_presence_conversions.h"
#include "chrome/services/sharing/nearby/platform/input_file.h"
#include "chromeos/ash/services/nearby/public/mojom/nearby_connections_types.mojom.h"
#include "chromeos/ash/services/nearby/public/mojom/webrtc.mojom.h"
#include "services/network/public/mojom/p2p.mojom.h"
#include "third_party/nearby/src/connections/core.h"
#include "third_party/nearby/src/connections/v3/bandwidth_info.h"
#include "third_party/nearby/src/connections/v3/connection_result.h"
#include "third_party/nearby/src/connections/v3/listeners.h"

namespace nearby::connections {

Expand All @@ -35,8 +39,9 @@ ConnectionRequestInfo CreateConnectionRequestInfo(
.initiated_cb =
[remote](const std::string& endpoint_id,
const ConnectionResponseInfo& info) {
if (!remote)
if (!remote) {
return;
}

remote->OnConnectionInitiated(
endpoint_id,
Expand All @@ -48,37 +53,93 @@ ConnectionRequestInfo CreateConnectionRequestInfo(
},
.accepted_cb =
[remote](const std::string& endpoint_id) {
if (!remote)
if (!remote) {
return;
}

remote->OnConnectionAccepted(endpoint_id);
},
.rejected_cb =
[remote](const std::string& endpoint_id, Status status) {
if (!remote)
if (!remote) {
return;
}

remote->OnConnectionRejected(endpoint_id,
StatusToMojom(status.value));
},
.disconnected_cb =
[remote](const std::string& endpoint_id) {
if (!remote)
if (!remote) {
return;
}

remote->OnDisconnected(endpoint_id);
},
.bandwidth_changed_cb =
[remote](const std::string& endpoint_id, Medium medium) {
if (!remote)
if (!remote) {
return;
}

remote->OnBandwidthChanged(endpoint_id, MediumToMojom(medium));
},
},
};
}

// The callbacks are all casting NearbyDevice to PresenceDevice. Currently,
// Presence will be the main consumer of this listener, so casting is safe.
//
// TODO(b/308178927): Change out the `NOTIMPLEMENTED()` macro calls to the
// appropriate function calls when `mojom::ConnectionListenerV3` is fully
// implemented.
//
// TODO(b/307319934): Extend to be used by non-Presence clients when the
// migration to V3 APIs occurs.
v3::ConnectionListener CreateConnectionListenerV3(
mojo::PendingRemote<mojom::ConnectionListenerV3> listener) {
mojo::SharedRemote<mojom::ConnectionListenerV3> remote(std::move(listener));

return v3::ConnectionListener{
.initiated_cb =
[remote](const NearbyDevice& remote_device,
const v3::InitialConnectionInfo& info) {
if (!remote) {
return;
}

NOTIMPLEMENTED();
},
.result_cb =
[remote](const NearbyDevice& remote_device,
v3::ConnectionResult result) {
if (!remote) {
return;
}

NOTIMPLEMENTED();
},
.disconnected_cb =
[remote](const NearbyDevice& remote_device) {
if (!remote) {
return;
}

NOTIMPLEMENTED();
},
.bandwidth_changed_cb =
[remote](const NearbyDevice& remote_device,
v3::BandwidthInfo bandwidth_info) {
if (!remote) {
return;
}

NOTIMPLEMENTED();
},
};
}

} // namespace

// Should only be accessed by objects within lifetime of NearbyConnections.
Expand Down Expand Up @@ -472,6 +533,40 @@ void NearbyConnections::RegisterPayloadFile(
std::move(callback).Run(mojom::Status::kSuccess);
}

void NearbyConnections::RequestConnectionV3(
const std::string& service_id,
ash::nearby::presence::mojom::PresenceDevicePtr remote_device,
mojom::ConnectionOptionsPtr options,
mojo::PendingRemote<mojom::ConnectionListenerV3> listener,
RequestConnectionCallback callback) {
int keep_alive_interval_millis =
options->keep_alive_interval
? options->keep_alive_interval->InMilliseconds()
: 0;
int keep_alive_timeout_millis =
options->keep_alive_timeout
? options->keep_alive_timeout->InMilliseconds()
: 0;

ConnectionOptions connection_options{
.keep_alive_interval_millis = std::max(keep_alive_interval_millis, 0),
.keep_alive_timeout_millis = std::max(keep_alive_timeout_millis, 0)};
connection_options.allowed =
MediumSelectorFromMojom(options->allowed_mediums.get());

if (options->remote_bluetooth_mac_address) {
connection_options.remote_bluetooth_mac_address =
ByteArrayFromMojom(*options->remote_bluetooth_mac_address);
}

GetCore(service_id)
->RequestConnectionV3(
presence::PresenceDevice(ash::nearby::presence::MetadataFromMojom(
remote_device->metadata.get())),
connection_options, CreateConnectionListenerV3(std::move(listener)),
ResultCallbackFromMojom(std::move(callback)));
}

base::File NearbyConnections::ExtractInputFile(int64_t payload_id) {
base::AutoLock al(input_file_lock_);
auto file_it = input_file_map_.find(payload_id);
Expand Down
8 changes: 8 additions & 0 deletions chrome/services/sharing/nearby/nearby_connections.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "chrome/services/sharing/nearby/nearby_shared_remotes.h"
#include "chromeos/ash/services/nearby/public/mojom/firewall_hole.mojom.h"
#include "chromeos/ash/services/nearby/public/mojom/nearby_connections.mojom.h"
#include "chromeos/ash/services/nearby/public/mojom/nearby_presence.mojom.h"
#include "chromeos/ash/services/nearby/public/mojom/sharing.mojom.h"
#include "chromeos/ash/services/nearby/public/mojom/tcp_socket_factory.mojom.h"
#include "chromeos/ash/services/nearby/public/mojom/webrtc_signaling_messenger.mojom.h"
Expand All @@ -31,6 +32,7 @@
#include "mojo/public/cpp/bindings/receiver.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/nearby/src/connections/implementation/service_controller_router.h"
#include "third_party/nearby/src/presence/presence_device.h"

namespace nearby::connections {

Expand Down Expand Up @@ -118,6 +120,12 @@ class NearbyConnections : public mojom::NearbyConnections {
base::File input_file,
base::File output_file,
RegisterPayloadFileCallback callback) override;
void RequestConnectionV3(
const std::string& service_id,
ash::nearby::presence::mojom::PresenceDevicePtr remote_device,
mojom::ConnectionOptionsPtr connection_options,
mojo::PendingRemote<mojom::ConnectionListenerV3> listener,
RequestConnectionCallback callback) override;

// Returns the file associated with |payload_id| for InputFile.
base::File ExtractInputFile(int64_t payload_id);
Expand Down
19 changes: 15 additions & 4 deletions chrome/services/sharing/nearby/nearby_connections_conversions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
#include "chromeos/ash/services/nearby/public/mojom/nearby_connections.mojom.h"
#include "chromeos/ash/services/nearby/public/mojom/nearby_connections_types.mojom.h"

namespace nearby {
namespace connections {
namespace nearby::connections {

Strategy StrategyFromMojom(mojom::Strategy strategy) {
switch (strategy) {
Expand Down Expand Up @@ -140,5 +139,17 @@ BooleanMediumSelector MediumSelectorFromMojom(
};
}

} // namespace connections
} // namespace nearby
mojom::BandwidthQuality BandwidthQualityToMojom(v3::Quality quality) {
switch (quality) {
case v3::Quality::kUnknown:
return mojom::BandwidthQuality::kUnknown;
case v3::Quality::kLow:
return mojom::BandwidthQuality::kLow;
case v3::Quality::kMedium:
return mojom::BandwidthQuality::kMedium;
case v3::Quality::kHigh:
return mojom::BandwidthQuality::kHigh;
}
}

} // namespace nearby::connections
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
#include "chromeos/ash/services/nearby/public/mojom/nearby_connections.mojom-forward.h"
#include "chromeos/ash/services/nearby/public/mojom/nearby_connections_types.mojom-forward.h"
#include "third_party/nearby/src/connections/params.h"
#include "third_party/nearby/src/connections/v3/bandwidth_info.h"

namespace nearby {
namespace connections {
namespace nearby::connections {

using StatusCallback = base::OnceCallback<void(mojom::Status)>;

Expand All @@ -32,10 +32,11 @@ mojom::PayloadStatus PayloadStatusToMojom(PayloadProgressInfo::Status status);

mojom::Medium MediumToMojom(Medium medium);

mojom::BandwidthQuality BandwidthQualityToMojom(v3::Quality quality);

BooleanMediumSelector MediumSelectorFromMojom(
mojom::MediumSelection* allowed_mediums);

} // namespace connections
} // namespace nearby
} // namespace nearby::connections

#endif // CHROME_SERVICES_SHARING_NEARBY_NEARBY_CONNECTIONS_CONVERSIONS_H_

0 comments on commit bffa63b

Please sign in to comment.