From 614563c3fe055278db4eb5950804944fc1fbfd7c Mon Sep 17 00:00:00 2001 From: Francis Tsui Date: Mon, 16 Mar 2026 22:31:00 -0700 Subject: [PATCH] Refactor OutgoingShareSession to handle both transfers and pairing. PiperOrigin-RevId: 884809727 --- sharing/nearby_sharing_service_impl.cc | 74 ++++++++++++++++---------- sharing/nearby_sharing_service_impl.h | 6 ++- sharing/outgoing_share_session.cc | 2 +- sharing/outgoing_share_session.h | 7 ++- 4 files changed, 56 insertions(+), 33 deletions(-) diff --git a/sharing/nearby_sharing_service_impl.cc b/sharing/nearby_sharing_service_impl.cc index caaa3c05..0d08d02e 100644 --- a/sharing/nearby_sharing_service_impl.cc +++ b/sharing/nearby_sharing_service_impl.cc @@ -737,16 +737,6 @@ void NearbySharingServiceImpl::SendAttachments( return; } } - // Outgoing connections always announces with contacts visibility. - std::optional> endpoint_info = - CreateEndpointInfo(DeviceVisibility::DEVICE_VISIBILITY_ALL_CONTACTS, - local_device_data_manager_->GetDeviceName()); - if (!endpoint_info) { - LOG(WARNING) << "Could not create local endpoint info."; - std::move(status_codes_callback)(StatusCodes::kError); - return; - } - OutgoingShareSession* session = outgoing_targets_manager_.GetOutgoingShareSession(share_target_id); if (!session) { @@ -754,29 +744,39 @@ void NearbySharingServiceImpl::SendAttachments( std::move(status_codes_callback)(StatusCodes::kInvalidArgument); return; } - - app_info_->SetActiveFlag(); - + StatusCodes status_code = StatusCodes::kOk; if (session->InitiateSendAttachments( - std::move(attachment_container))) { - OutgoingSessionConnect(*session, std::move(*endpoint_info)); + std::move(attachment_container ))) { + status_code = ConnectOutgoingSessionOnServiceThread(*session); } - std::move(status_codes_callback)(StatusCodes::kOk); + std::move(status_codes_callback)(status_code); }); } -void NearbySharingServiceImpl::OutgoingSessionConnect( - OutgoingShareSession& session, std::vector endpoint_info) { +NearbySharingService::StatusCodes +NearbySharingServiceImpl::ConnectOutgoingSessionOnServiceThread( + OutgoingShareSession& session) { + // Outgoing connections always announces with contacts visibility. + std::optional> endpoint_info = + CreateEndpointInfo(DeviceVisibility::DEVICE_VISIBILITY_ALL_CONTACTS, + local_device_data_manager_->GetDeviceName()); + if (!endpoint_info) { + LOG(WARNING) << "Could not create local endpoint info."; + return StatusCodes::kError; + } + app_info_->SetActiveFlag(); + OnTransferStarted(/*is_incoming=*/false); is_connecting_ = true; InvalidateSendSurfaceState(); int64_t share_target_id = session.share_target().id; session.Connect( - std::move(endpoint_info), settings_->GetDataUsage(), + std::move(*endpoint_info), settings_->GetDataUsage(), GetDisableWifiHotspotState(), absl::bind_front(&NearbySharingServiceImpl::OnOutgoingConnection, this, share_target_id)); + return StatusCodes::kOk; } bool NearbySharingServiceImpl::OutgoingSessionAccept( @@ -1366,7 +1366,7 @@ void NearbySharingServiceImpl::AdapterPresentChanged( void NearbySharingServiceImpl::AdapterPoweredChanged( sharing::api::BluetoothAdapter* adapter, bool powered) { - // When adpater is powered on, it takes some time for the RFCOMM service to + // When adapter is powered on, it takes some time for the RFCOMM service to // be ready. If we don't wait the RfCommServiceProvider::CreateAsync() call // fails with a "device is not ready for use" error. // Waiting 500ms seems to be enough to allow it to reliably work. @@ -2521,10 +2521,19 @@ void NearbySharingServiceImpl::OnOutgoingConnectionKeyVerificationDone( session->Abort(TransferMetadata::Status::kDeviceAuthenticationFailed); return; } + if (session->is_transfer_session()) { + BeginOutgoingTransfer(*session); + } else { + BeginOutgoingPairing(*session); + } +} +void NearbySharingServiceImpl::BeginOutgoingTransfer( + OutgoingShareSession& session) { VLOG(1) << __func__ << ": Preparing to send introduction to " - << share_target_id; - if (!session->SendIntroduction([this, share_target_id]() { + << session.share_target().id; + if (!session.SendIntroduction([this, share_target_id = + session.share_target().id]() { VLOG(1) << "Outgoing mutual acceptance timed out, closing connection for " << share_target_id; @@ -2537,27 +2546,34 @@ void NearbySharingServiceImpl::OnOutgoingConnectionKeyVerificationDone( })) { LOG(WARNING) << __func__ << ": No payloads tied to transfer, disconnecting."; - session->Abort(TransferMetadata::Status::kMediaUnavailable); + session.Abort(TransferMetadata::Status::kMediaUnavailable); return; } // Auto Accept if key verification is successful or skip sender confirmation. bool protection_enabled = preference_manager_.GetBoolean(PrefNames::kAdvancedProtectionEnabled, /*default_value=*/false); - session->SetAdvancedProtectionStatus(protection_enabled, - /*advanced_protection_mismatch=*/false); - if (session->token().empty() || !protection_enabled) { + session.SetAdvancedProtectionStatus(protection_enabled, + /*advanced_protection_mismatch=*/false); + if (session.token().empty() || !protection_enabled) { // Auto accept if no token or if advanced protection is disabled. - OutgoingSessionAccept(*session); + OutgoingSessionAccept(session); } else { - session->UpdateTransferMetadata( + session.UpdateTransferMetadata( TransferMetadataBuilder() .set_status(TransferMetadata::Status::kAwaitingLocalConfirmation) - .set_token(session->token()) + .set_token(session.token()) .build()); } } +void NearbySharingServiceImpl::BeginOutgoingPairing( + OutgoingShareSession& session) { + VLOG(1) << __func__ << ": Preparing to initiate pairing with " + << session.share_target().id; + // TODO(ftsui): Implement this. +} + void NearbySharingServiceImpl::OnReceivedIntroduction( IncomingShareSession& session, const IntroductionFrame& frame) { LOG(INFO) << __func__ << ": Successfully read the introduction frame."; diff --git a/sharing/nearby_sharing_service_impl.h b/sharing/nearby_sharing_service_impl.h index 1e082a7b..b3528bc3 100644 --- a/sharing/nearby_sharing_service_impl.h +++ b/sharing/nearby_sharing_service_impl.h @@ -293,8 +293,8 @@ class NearbySharingServiceImpl absl::string_view endpoint_id, NearbyConnection* connection, Status status); - void OutgoingSessionConnect(OutgoingShareSession& session, - std::vector endpoint_info); + StatusCodes ConnectOutgoingSessionOnServiceThread( + OutgoingShareSession& session); void Fail(IncomingShareSession& session, TransferMetadata::Status status); void OnIncomingAdvertisementDecoded( @@ -317,6 +317,8 @@ class NearbySharingServiceImpl int64_t share_target_id, PairedKeyVerificationRunner::PairedKeyVerificationResult result, ::location::nearby::proto::sharing::OSType share_target_os_type); + void BeginOutgoingTransfer(OutgoingShareSession& session); + void BeginOutgoingPairing(OutgoingShareSession& session); void OnIncomingSessionFrameRead( int64_t share_target_id, bool is_timeout, diff --git a/sharing/outgoing_share_session.cc b/sharing/outgoing_share_session.cc index 2522d02a..0073f259 100644 --- a/sharing/outgoing_share_session.cc +++ b/sharing/outgoing_share_session.cc @@ -40,7 +40,6 @@ #include "sharing/nearby_connections_manager.h" #include "sharing/nearby_connections_types.h" #include "sharing/nearby_sharing_util.h" -#include "sharing/paired_key_verification_runner.h" #include "sharing/payload_tracker.h" #include "sharing/share_session.h" #include "sharing/share_target.h" @@ -151,6 +150,7 @@ void OutgoingShareSession::InvokeTransferUpdateCallback( bool OutgoingShareSession::InitiateSendAttachments( std::unique_ptr attachment_container) { SetAttachmentContainer(std::move(*attachment_container)); + is_transfer_session_ = true; is_connecting_ = true; // Set session ID. diff --git a/sharing/outgoing_share_session.h b/sharing/outgoing_share_session.h index 21b8b8e6..d7a37430 100644 --- a/sharing/outgoing_share_session.h +++ b/sharing/outgoing_share_session.h @@ -34,7 +34,6 @@ #include "sharing/nearby_connection.h" #include "sharing/nearby_connections_manager.h" #include "sharing/nearby_connections_types.h" -#include "sharing/paired_key_verification_runner.h" #include "sharing/proto/enums.pb.h" #include "sharing/share_session.h" #include "sharing/share_target.h" @@ -159,6 +158,10 @@ class OutgoingShareSession : public ShareSession { const std::vector& file_payloads() const { return file_payloads_; } + // Returns true if the session is a transfer session. + // Otherwise, it is a pairing session. + bool is_transfer_session() const { return is_transfer_session_; } + protected: void InvokeTransferUpdateCallback(const TransferMetadata& metadata) override; void OnConnectionDisconnected() override; @@ -196,6 +199,8 @@ class OutgoingShareSession : public ShareSession { bool advanced_protection_enabled_ = false; bool advanced_protection_mismatch_ = false; bool is_connecting_ = false; + // Session can be for transfer or pairing. + bool is_transfer_session_ = false; }; } // namespace nearby::sharing