From 91a06b6a7f9549fcbd114948a172b573fc614118 Mon Sep 17 00:00:00 2001 From: Francis Tsui Date: Mon, 16 Feb 2026 08:49:06 -0800 Subject: [PATCH] Split frame handler for incoming and outgoing sessions. PiperOrigin-RevId: 870909627 --- sharing/incoming_frames_reader.cc | 10 +- sharing/incoming_share_session.cc | 24 ----- sharing/incoming_share_session.h | 11 -- sharing/incoming_share_session_test.cc | 133 ------------------------ sharing/nearby_sharing_service_impl.cc | 135 +++++++++++++++---------- sharing/nearby_sharing_service_impl.h | 10 +- sharing/outgoing_share_session.cc | 6 -- sharing/outgoing_share_session.h | 4 - sharing/outgoing_share_session_test.cc | 32 ------ sharing/share_session.cc | 78 +++++++------- sharing/share_session.h | 14 ++- sharing/share_session_test.cc | 31 +++--- 12 files changed, 153 insertions(+), 335 deletions(-) diff --git a/sharing/incoming_frames_reader.cc b/sharing/incoming_frames_reader.cc index 73d6d49e..72be3530 100644 --- a/sharing/incoming_frames_reader.cc +++ b/sharing/incoming_frames_reader.cc @@ -159,6 +159,7 @@ void IncomingFramesReader::OnDataReadFromConnection( { absl::MutexLock lock(mutex_); if (read_frame_info_queue_.empty()) { + // Drop the frame if no one is waiting. return; } const ReadFrameInfo& frame_info = read_frame_info_queue_.front(); @@ -210,12 +211,9 @@ void IncomingFramesReader::Done(std::unique_ptr frame) { read_frame_info_queue_.pop(); } - if (read_frame_info.timeout != absl::ZeroDuration()) { - ReadFrame(*read_frame_info.frame_type, std::move(read_frame_info.callback), - read_frame_info.timeout); - } else { - ReadFrame(std::move(read_frame_info.callback), read_frame_info.timeout); - } + ProcessReadRequest(read_frame_info.frame_type, + std::move(read_frame_info.callback), + read_frame_info.timeout); } std::unique_ptr IncomingFramesReader::PopCachedFrame( diff --git a/sharing/incoming_share_session.cc b/sharing/incoming_share_session.cc index 9d74403d..2cb7fd91 100644 --- a/sharing/incoming_share_session.cc +++ b/sharing/incoming_share_session.cc @@ -192,30 +192,6 @@ IncomingShareSession::ProcessIntroduction( return std::nullopt; } -bool IncomingShareSession::ProcessKeyVerificationResult( - PairedKeyVerificationRunner::PairedKeyVerificationResult result, - OSType share_target_os_type, - std::function)> - introduction_callback) { - if (!HandleKeyVerificationResult(result, share_target_os_type)) { - return false; - } - LOG(INFO) << ":Waiting for introduction from " << share_target().id; - - frames_reader()->ReadFrame( - V1Frame::INTRODUCTION, - [callback = std::move(introduction_callback)]( - bool is_timeout, std::optional frame) { - if (!frame.has_value()) { - callback(std::nullopt); - } else { - callback(frame->introduction()); - } - }, - kReadFramesTimeout); - return true; -} - bool IncomingShareSession::ReadyForTransfer( std::function accept_timeout_callback, std::function frame)> diff --git a/sharing/incoming_share_session.h b/sharing/incoming_share_session.h index 3728d4cf..1ec0e519 100644 --- a/sharing/incoming_share_session.h +++ b/sharing/incoming_share_session.h @@ -61,17 +61,6 @@ class IncomingShareSession : public ShareSession { const nearby::sharing::service::proto::IntroductionFrame& introduction_frame); - // Processes the PairedKeyVerificationResult. - // Returns true if verification was successful and the session is now waiting - // for the introduction frame. Calls |introduction_callback| when it is - // received. - bool ProcessKeyVerificationResult( - PairedKeyVerificationRunner::PairedKeyVerificationResult result, - location::nearby::proto::sharing::OSType share_target_os_type, - std::function)> - introduction_callback); - // Returns true if the transfer can begin and AcceptTransfer should be called // immediately. // Returns false if user needs to accept the transfer. diff --git a/sharing/incoming_share_session_test.cc b/sharing/incoming_share_session_test.cc index 70fd119d..b926f65b 100644 --- a/sharing/incoming_share_session_test.cc +++ b/sharing/incoming_share_session_test.cc @@ -1235,139 +1235,6 @@ TEST_F(IncomingShareSessionTest, AcceptTransferSuccess) { ConnectionResponseFrame::ACCEPT); } -TEST_F(IncomingShareSessionTest, ProcessKeyVerificationResultSuccess) { - session_.OnConnected(&connection_); - session_.SetTokenForTests("1234"); - - bool introduction_received = false; - EXPECT_THAT( - session_.ProcessKeyVerificationResult( - PairedKeyVerificationRunner::PairedKeyVerificationResult::kSuccess, - OSType::WINDOWS, - [&introduction_received](std::optional) { - introduction_received = true; - }), - IsTrue()); - - EXPECT_THAT(session_.self_share(), IsFalse()); - EXPECT_THAT(session_.token(), IsEmpty()); - EXPECT_THAT(session_.os_type(), Eq(OSType::WINDOWS)); - EXPECT_THAT(introduction_received, IsFalse()); - - // Send Introduction frame - nearby::sharing::service::proto::Frame frame = - nearby::sharing::service::proto::Frame(); - frame.set_version(nearby::sharing::service::proto::Frame::V1); - V1Frame* v1frame = frame.mutable_v1(); - v1frame->set_type(service::proto::V1Frame::INTRODUCTION); - v1frame->mutable_introduction(); - std::vector data; - data.resize(frame.ByteSizeLong()); - EXPECT_THAT(frame.SerializeToArray(data.data(), data.size()), IsTrue()); - connection_.WriteMessage(std::move(data)); - - EXPECT_THAT(introduction_received, IsTrue()); -} - -TEST_F(IncomingShareSessionTest, ProcessKeyVerificationResultFail) { - session_.OnConnected(&connection_); - session_.SetTokenForTests("1234"); - - bool introduction_received = false; - EXPECT_THAT( - session_.ProcessKeyVerificationResult( - PairedKeyVerificationRunner::PairedKeyVerificationResult::kFail, - OSType::WINDOWS, - [&introduction_received](std::optional) { - introduction_received = true; - }), - IsFalse()); - - EXPECT_THAT(session_.token(), Eq("1234")); - EXPECT_THAT(session_.os_type(), Eq(OSType::WINDOWS)); - EXPECT_THAT(introduction_received, IsFalse()); - - // Send Introduction frame - nearby::sharing::service::proto::Frame frame = - nearby::sharing::service::proto::Frame(); - frame.set_version(nearby::sharing::service::proto::Frame::V1); - V1Frame* v1frame = frame.mutable_v1(); - v1frame->set_type(service::proto::V1Frame::INTRODUCTION); - v1frame->mutable_introduction(); - std::vector data; - data.resize(frame.ByteSizeLong()); - EXPECT_THAT(frame.SerializeToArray(data.data(), data.size()), IsTrue()); - connection_.WriteMessage(std::move(data)); - - EXPECT_THAT(introduction_received, IsFalse()); -} - -TEST_F(IncomingShareSessionTest, ProcessKeyVerificationResultUnable) { - session_.OnConnected(&connection_); - session_.SetTokenForTests("1234"); - - bool introduction_received = false; - EXPECT_THAT( - session_.ProcessKeyVerificationResult( - PairedKeyVerificationRunner::PairedKeyVerificationResult::kUnable, - OSType::WINDOWS, - [&introduction_received](std::optional) { - introduction_received = true; - }), - IsTrue()); - - EXPECT_THAT(session_.token(), Eq("1234")); - EXPECT_THAT(session_.os_type(), Eq(OSType::WINDOWS)); - EXPECT_THAT(introduction_received, IsFalse()); - - // Send Introduction frame - nearby::sharing::service::proto::Frame frame = - nearby::sharing::service::proto::Frame(); - frame.set_version(nearby::sharing::service::proto::Frame::V1); - V1Frame* v1frame = frame.mutable_v1(); - v1frame->set_type(service::proto::V1Frame::INTRODUCTION); - v1frame->mutable_introduction(); - std::vector data; - data.resize(frame.ByteSizeLong()); - EXPECT_THAT(frame.SerializeToArray(data.data(), data.size()), IsTrue()); - connection_.WriteMessage(std::move(data)); - - EXPECT_THAT(introduction_received, IsTrue()); -} - -TEST_F(IncomingShareSessionTest, ProcessKeyVerificationResultUnknown) { - session_.OnConnected(&connection_); - session_.SetTokenForTests("1234"); - - bool introduction_received = false; - EXPECT_THAT( - session_.ProcessKeyVerificationResult( - PairedKeyVerificationRunner::PairedKeyVerificationResult::kUnknown, - OSType::WINDOWS, - [&introduction_received](std::optional) { - introduction_received = true; - }), - IsFalse()); - - EXPECT_THAT(session_.token(), Eq("1234")); - EXPECT_THAT(session_.os_type(), Eq(OSType::WINDOWS)); - EXPECT_THAT(introduction_received, IsFalse()); - - // Send Introduction frame - nearby::sharing::service::proto::Frame frame = - nearby::sharing::service::proto::Frame(); - frame.set_version(nearby::sharing::service::proto::Frame::V1); - V1Frame* v1frame = frame.mutable_v1(); - v1frame->set_type(service::proto::V1Frame::INTRODUCTION); - v1frame->mutable_introduction(); - std::vector data; - data.resize(frame.ByteSizeLong()); - EXPECT_THAT(frame.SerializeToArray(data.data(), data.size()), IsTrue()); - connection_.WriteMessage(std::move(data)); - - EXPECT_THAT(introduction_received, IsFalse()); -} - TEST_F(IncomingShareSessionTest, TryUpgradeBandwidthNotNeeded) { session_.OnConnected(&connection_); diff --git a/sharing/nearby_sharing_service_impl.cc b/sharing/nearby_sharing_service_impl.cc index 7f6b69c8..ecdc59b3 100644 --- a/sharing/nearby_sharing_service_impl.cc +++ b/sharing/nearby_sharing_service_impl.cc @@ -2428,6 +2428,57 @@ void NearbySharingServiceImpl::OnIncomingDecryptedCertificate( this, share_target_id)); } +void NearbySharingServiceImpl::OnIncomingSessionFrameRead( + int64_t share_target_id, + bool is_timeout, + std::optional frame) { + IncomingShareSession* session = GetIncomingShareSession(share_target_id); + if (session == nullptr || !session->IsConnected()) { + LOG(WARNING) << __func__ + << ": Session not connected, stop reading frames from target: " + << share_target_id; + return; + } + if (is_timeout) { + LOG(WARNING) << __func__ << ": Timed out reading frame from target: " + << share_target_id; + session->Abort(TransferMetadata::Status::kFailed); + return; + } + if (!frame.has_value()) { + // This is the case when the connection has been closed since we wait + // indefinitely for incoming frames. + return; + } + + VLOG(1) << "Received incoming frame type: " + << static_cast(frame->type()) << " from " << share_target_id; + switch (frame->type()) { + case service::proto::V1Frame::CANCEL: + RunOnNearbySharingServiceThread("cancel_transfer", [this, + share_target_id]() { + LOG(INFO) << __func__ << ": Read the cancel frame, closing connection"; + DoCancel( + share_target_id, [](StatusCodes status_codes) {}, + /*is_initiator_of_cancellation=*/false); + }); + break; + case service::proto::V1Frame::INTRODUCTION: + OnReceivedIntroduction(*session, frame->introduction()); + // OnReceivedIntroduction will schedule the next ReadFrame. + return; + default: + LOG(ERROR) << __func__ << ": Discarding unknown frame of type: " + << static_cast(frame->type()); + break; + } + + session->frames_reader()->ReadFrame( + absl::bind_front(&NearbySharingServiceImpl::OnIncomingSessionFrameRead, + this, share_target_id), + absl::ZeroDuration()); +} + void NearbySharingServiceImpl::OnIncomingConnectionKeyVerificationDone( int64_t share_target_id, PairedKeyVerificationRunner::PairedKeyVerificationResult result, @@ -2438,11 +2489,15 @@ void NearbySharingServiceImpl::OnIncomingConnectionKeyVerificationDone( return; } if (!session->ProcessKeyVerificationResult( - result, share_target_os_type, - absl::bind_front(&NearbySharingServiceImpl::OnReceivedIntroduction, - this, share_target_id))) { + result, share_target_os_type)) { session->Abort(TransferMetadata::Status::kDeviceAuthenticationFailed); + return; } + LOG(INFO) << "Waiting for introduction from " << share_target_id; + session->frames_reader()->ReadFrame( + absl::bind_front(&NearbySharingServiceImpl::OnIncomingSessionFrameRead, + this, share_target_id), + kReadFramesTimeout); } void NearbySharingServiceImpl::OnOutgoingConnectionKeyVerificationDone( @@ -2497,51 +2552,37 @@ void NearbySharingServiceImpl::OnOutgoingConnectionKeyVerificationDone( } void NearbySharingServiceImpl::OnReceivedIntroduction( - int64_t share_target_id, std::optional frame) { - IncomingShareSession* session = GetIncomingShareSession(share_target_id); - if (!session || !session->IsConnected()) { - LOG(WARNING) - << __func__ - << ": Ignore received introduction, due to no connection established."; - return; - } - - if (!frame.has_value()) { - session->Abort(TransferMetadata::Status::kFailed); - LOG(WARNING) << __func__ << ": Invalid introduction frame"; - return; - } - + IncomingShareSession& session, const IntroductionFrame& frame) { LOG(INFO) << __func__ << ": Successfully read the introduction frame."; std::optional status = - session->ProcessIntroduction(*frame); + session.ProcessIntroduction(frame); if (status.has_value()) { - Fail(*session, *status); + Fail(session, *status); return; } FilePath save_path{settings_->GetCustomSavePath()}; // Override save path for this connection. // This must be called before the transfer is accepted and payloads are being // received. - nearby_connections_manager_->OverrideSavePath(session->endpoint_id(), + nearby_connections_manager_->OverrideSavePath(session.endpoint_id(), save_path); // Log analytics event of receiving introduction. analytics_recorder_.NewReceiveIntroduction( - session->session_id(), session->share_target(), - /*referrer_package=*/std::nullopt, session->os_type()); + session.session_id(), session.share_target(), + /*referrer_package=*/std::nullopt, session.os_type()); if (IsOutOfStorage(device_info_, save_path, - session->attachment_container().GetStorageSize())) { - Fail(*session, TransferMetadata::Status::kNotEnoughSpace); + session.attachment_container().GetStorageSize())) { + Fail(session, TransferMetadata::Status::kNotEnoughSpace); LOG(WARNING) << __func__ << ": Not enough space on the receiver. We have informed " - << share_target_id; + << session.share_target().id; return; } - OnStorageCheckCompleted(*session); + OnStorageCheckCompleted(session); } void NearbySharingServiceImpl::OnReceiveConnectionResponse( @@ -2562,11 +2603,8 @@ void NearbySharingServiceImpl::OnReceiveConnectionResponse( return; } session->SendPayloads( - [this, share_target_id]( - bool is_timeout, - std::optional frame) { - OnFrameRead(share_target_id, is_timeout, std::move(frame)); - }, + absl::bind_front(&NearbySharingServiceImpl::OnOutgoingSessionFrameRead, + this, share_target_id), absl::bind_front( &NearbySharingServiceImpl::OnOutgoingPayloadTransferUpdates, this, share_target_id)); @@ -2585,8 +2623,9 @@ void NearbySharingServiceImpl::OnStorageCheckCompleted( Fail(*session, TransferMetadata::Status::kTimedOut); } }, - absl::bind_front(&NearbySharingServiceImpl::OnFrameRead, this, - session.share_target().id))) { + absl::bind_front( + &NearbySharingServiceImpl::OnIncomingSessionFrameRead, this, + session.share_target().id))) { return; } // Don't need to wait for user to accept for Self share. @@ -2597,7 +2636,7 @@ void NearbySharingServiceImpl::OnStorageCheckCompleted( OnTransferStarted(/*is_incoming=*/true); } -void NearbySharingServiceImpl::OnFrameRead( +void NearbySharingServiceImpl::OnOutgoingSessionFrameRead( int64_t share_target_id, bool is_timeout, std::optional frame) { if (!frame.has_value()) { @@ -2616,35 +2655,25 @@ void NearbySharingServiceImpl::OnFrameRead( /*is_initiator_of_cancellation=*/false); }); break; - - case nearby::sharing::service::proto::V1Frame::CERTIFICATE_INFO: - // No-op, no longer used. - break; - - case nearby::sharing::service::proto::V1Frame::PROGRESS_UPDATE: - // No-op, no longer used. - break; - default: LOG(ERROR) << __func__ << ": Discarding unknown frame of type: " << static_cast(frame->type()); break; } - ShareSession* session = GetShareSession(share_target_id); - if (!session || !session->frames_reader()) { + OutgoingShareSession* session = + outgoing_targets_manager_.GetOutgoingShareSession(share_target_id); + if (!session || !session->IsConnected()) { LOG(WARNING) << __func__ - << ": Stopped reading further frames, due to no connection " - "established."; + << ": Session not connected, stop reading frames from target: " + << share_target_id; return; } session->frames_reader()->ReadFrame( - [this, share_target_id]( - bool is_timeout, - std::optional frame) { - OnFrameRead(share_target_id, is_timeout, std::move(frame)); - }, absl::ZeroDuration()); + absl::bind_front(&NearbySharingServiceImpl::OnOutgoingSessionFrameRead, + this, share_target_id), + absl::ZeroDuration()); } void NearbySharingServiceImpl::OnConnectionDisconnected( diff --git a/sharing/nearby_sharing_service_impl.h b/sharing/nearby_sharing_service_impl.h index f1e7b211..ec045249 100644 --- a/sharing/nearby_sharing_service_impl.h +++ b/sharing/nearby_sharing_service_impl.h @@ -311,15 +311,19 @@ class NearbySharingServiceImpl int64_t share_target_id, PairedKeyVerificationRunner::PairedKeyVerificationResult result, ::location::nearby::proto::sharing::OSType share_target_os_type); - void OnReceivedIntroduction( + void OnIncomingSessionFrameRead( int64_t share_target_id, - std::optional frame); + bool is_timeout, + std::optional frame); + void OnReceivedIntroduction( + IncomingShareSession& session, + const nearby::sharing::service::proto::IntroductionFrame& frame); void OnReceiveConnectionResponse( int64_t share_target_id, std::optional frame); void OnStorageCheckCompleted(IncomingShareSession& session); - void OnFrameRead( + void OnOutgoingSessionFrameRead( int64_t share_target_id, bool is_timeout, std::optional frame); diff --git a/sharing/outgoing_share_session.cc b/sharing/outgoing_share_session.cc index 6d89c6f2..2522d02a 100644 --- a/sharing/outgoing_share_session.cc +++ b/sharing/outgoing_share_session.cc @@ -188,12 +188,6 @@ bool OutgoingShareSession::InitiateSendAttachments( return success; } -bool OutgoingShareSession::ProcessKeyVerificationResult( - PairedKeyVerificationRunner::PairedKeyVerificationResult result, - location::nearby::proto::sharing::OSType share_target_os_type) { - return HandleKeyVerificationResult(result, share_target_os_type); -} - void OutgoingShareSession::OnConnectionDisconnected() { disconnection_timeout_ = nullptr; if (pending_complete_metadata_.has_value()) { diff --git a/sharing/outgoing_share_session.h b/sharing/outgoing_share_session.h index d4742c6c..21b8b8e6 100644 --- a/sharing/outgoing_share_session.h +++ b/sharing/outgoing_share_session.h @@ -72,10 +72,6 @@ class OutgoingShareSession : public ShareSession { bool InitiateSendAttachments( std::unique_ptr attachment_container); - bool ProcessKeyVerificationResult( - PairedKeyVerificationRunner::PairedKeyVerificationResult result, - location::nearby::proto::sharing::OSType share_target_os_type); - // Returns true if the introduction frame is written successfully. // `timeout_callback` is called if accept is not received from both sender and // receiver within the timeout. diff --git a/sharing/outgoing_share_session_test.cc b/sharing/outgoing_share_session_test.cc index b8f902d6..983fc8fb 100644 --- a/sharing/outgoing_share_session_test.cc +++ b/sharing/outgoing_share_session_test.cc @@ -784,38 +784,6 @@ TEST_F(OutgoingShareSessionTest, SendNextPayload) { session_.SendNextPayload(); } -TEST_F(OutgoingShareSessionTest, ProcessKeyVerificationResultFail) { - NearbyConnectionImpl connection(device_info_); - session_.set_session_id(1234); - ConnectionSuccess(&connection); - session_.SetTokenForTests("1234"); - - EXPECT_THAT( - session_.ProcessKeyVerificationResult( - PairedKeyVerificationRunner::PairedKeyVerificationResult::kFail, - OSType::WINDOWS), - IsFalse()); - - EXPECT_THAT(session_.token(), Eq("1234")); - EXPECT_THAT(session_.os_type(), Eq(OSType::WINDOWS)); -} - -TEST_F(OutgoingShareSessionTest, ProcessKeyVerificationResultSuccess) { - NearbyConnectionImpl connection(device_info_); - session_.set_session_id(1234); - ConnectionSuccess(&connection); - session_.SetTokenForTests("1234"); - - EXPECT_THAT( - session_.ProcessKeyVerificationResult( - PairedKeyVerificationRunner::PairedKeyVerificationResult::kSuccess, - OSType::WINDOWS), - IsTrue()); - - EXPECT_THAT(session_.token(), IsEmpty()); - EXPECT_THAT(session_.os_type(), Eq(OSType::WINDOWS)); -} - TEST_F(OutgoingShareSessionTest, DelayCompleteReceiverDisconnect) { NearbyConnectionImpl connection(device_info_); session_.set_session_id(1234); diff --git a/sharing/share_session.cc b/sharing/share_session.cc index 0c14e225..885e8396 100644 --- a/sharing/share_session.cc +++ b/sharing/share_session.cc @@ -203,6 +203,45 @@ void ShareSession::RunPairedKeyVerification( key_verification_runner_->Run(std::move(callback)); } +bool ShareSession::ProcessKeyVerificationResult( + PairedKeyVerificationRunner::PairedKeyVerificationResult result, + OSType share_target_os_type) { + os_type_ = share_target_os_type; + + switch (result) { + case PairedKeyVerificationRunner::PairedKeyVerificationResult::kFail: + LOG(WARNING) << __func__ << ": Paired key handshake failed for target " + << share_target().id << ". Disconnecting."; + return false; + + case PairedKeyVerificationRunner::PairedKeyVerificationResult::kSuccess: + VLOG(1) << __func__ << ": Paired key handshake succeeded for target - " + << share_target().id; + // If verification succeeds, this either means that the target is a + // self-share or a mutual contact. In either case, we should clear the + // token. + token_.resize(0); + break; + + case PairedKeyVerificationRunner::PairedKeyVerificationResult::kUnable: + VLOG(1) << __func__ + << ": Unable to verify paired key encryption when " + "receiving connection from target - " + << share_target().id; + // If we are unable to verify the paired key, we should clear the self + // share flag. + self_share_ = false; + break; + + case PairedKeyVerificationRunner::PairedKeyVerificationResult::kUnknown: + LOG(WARNING) << __func__ + << ": Unknown PairedKeyVerificationResult for target " + << share_target().id << ". Disconnecting."; + return false; + } + return true; +} + void ShareSession::OnDisconnect() { OnConnectionDisconnected(); if (disconnect_status_ != TransferMetadata::Status::kUnknown) { @@ -267,45 +306,6 @@ void ShareSession::WriteCancelFrame() { WriteFrame(frame); } -bool ShareSession::HandleKeyVerificationResult( - PairedKeyVerificationRunner::PairedKeyVerificationResult result, - location::nearby::proto::sharing::OSType share_target_os_type) { - os_type_ = share_target_os_type; - - switch (result) { - case PairedKeyVerificationRunner::PairedKeyVerificationResult::kFail: - LOG(WARNING) << __func__ << ": Paired key handshake failed for target " - << share_target().id << ". Disconnecting."; - return false; - - case PairedKeyVerificationRunner::PairedKeyVerificationResult::kSuccess: - VLOG(1) << __func__ << ": Paired key handshake succeeded for target - " - << share_target().id; - // If verification succeeds, this either means that the target is a - // self-share or a mutual contact. In either case, we should clear the - // token. - token_.resize(0); - break; - - case PairedKeyVerificationRunner::PairedKeyVerificationResult::kUnable: - VLOG(1) << __func__ - << ": Unable to verify paired key encryption when " - "receiving connection from target - " - << share_target().id; - // If we are unable to verify the paired key, we should clear the self - // share flag. - self_share_ = false; - break; - - case PairedKeyVerificationRunner::PairedKeyVerificationResult::kUnknown: - LOG(WARNING) << __func__ - << ": Unknown PairedKeyVerificationResult for target " - << share_target().id << ". Disconnecting."; - return false; - } - return true; -} - void ShareSession::InitializePayloadTracker( absl::AnyInvocable payload_transfer_updates_callback) { auto payload_updates_queue = diff --git a/sharing/share_session.h b/sharing/share_session.h index 9a4f5816..d0dc0f03 100644 --- a/sharing/share_session.h +++ b/sharing/share_session.h @@ -23,6 +23,7 @@ #include #include "absl/container/flat_hash_map.h" +#include "absl/functional/any_invocable.h" #include "absl/strings/string_view.h" #include "internal/platform/clock.h" #include "internal/platform/task_runner.h" @@ -71,6 +72,9 @@ class ShareSession { void clear_certificate() { certificate_ = std::nullopt; } NearbyConnection* connection() const { return connection_; } + // Returns true if the session has a valid connection. + // When `IsConnected()` is true, `connection()` is non-null, as is + // `frames_reader()`. bool IsConnected() const { return connection_ != nullptr; } void UpdateTransferMetadata(const TransferMetadata& transfer_metadata); @@ -113,6 +117,11 @@ class ShareSession { void(PairedKeyVerificationRunner::PairedKeyVerificationResult, location::nearby::proto::sharing::OSType)> callback); + // Processes the PairedKeyVerificationResult. + // Returns true if verification was successful. + bool ProcessKeyVerificationResult( + PairedKeyVerificationRunner::PairedKeyVerificationResult result, + location::nearby::proto::sharing::OSType share_target_os_type); void OnDisconnect(); const AttachmentContainer& attachment_container() const { @@ -165,11 +174,6 @@ class ShareSession { return attachment_container_; } void WriteFrame(const nearby::sharing::service::proto::Frame& frame); - // Processes the PairedKeyVerificationResult. - // Returns true if verification was successful. - bool HandleKeyVerificationResult( - PairedKeyVerificationRunner::PairedKeyVerificationResult result, - location::nearby::proto::sharing::OSType share_target_os_type); NearbyConnectionsManager& connections_manager() { return connections_manager_; diff --git a/sharing/share_session_test.cc b/sharing/share_session_test.cc index 53469e41..fc6d9211 100644 --- a/sharing/share_session_test.cc +++ b/sharing/share_session_test.cc @@ -69,13 +69,6 @@ class TestShareSession : public ShareSession { ShareSession::SetAttachmentPayloadId(attachment_id, payload_id); } - bool HandleKeyVerificationResult( - PairedKeyVerificationRunner::PairedKeyVerificationResult result, - OSType share_target_os_type) { - return ShareSession::HandleKeyVerificationResult(result, - share_target_os_type); - } - FakeNearbyConnectionsManager& connections_manager() { return connections_manager_; } @@ -319,21 +312,21 @@ TEST(ShareSessionTest, WriteCancelFrame) { EXPECT_EQ(frame.v1().type(), V1Frame::CANCEL); } -TEST(ShareSessionTest, HandleKeyVerificationResultFail) { +TEST(ShareSessionTest, ProcessKeyVerificationResultFail) { ShareTarget share_target; TestShareSession session(std::string(kEndpointId), share_target); NearbyConnectionImpl connection(session.device_info()); session.SetNearbyConnection(&connection); session.SetTokenForTests("9876"); - EXPECT_FALSE(session.HandleKeyVerificationResult( + EXPECT_FALSE(session.ProcessKeyVerificationResult( PairedKeyVerificationRunner::PairedKeyVerificationResult::kFail, OSType::WINDOWS)); EXPECT_EQ(session.os_type(), OSType::WINDOWS); EXPECT_FALSE(session.token().empty()); } -TEST(ShareSessionTest, HandleKeyVerificationResultSelfShareSuccess) { +TEST(ShareSessionTest, ProcessKeyVerificationResultSelfShareSuccess) { ShareTarget share_target; share_target.for_self_share = true; TestShareSession session(std::string(kEndpointId), share_target); @@ -341,7 +334,7 @@ TEST(ShareSessionTest, HandleKeyVerificationResultSelfShareSuccess) { session.SetNearbyConnection(&connection); session.SetTokenForTests("9876"); - EXPECT_TRUE(session.HandleKeyVerificationResult( + EXPECT_TRUE(session.ProcessKeyVerificationResult( PairedKeyVerificationRunner::PairedKeyVerificationResult::kSuccess, OSType::WINDOWS)); EXPECT_EQ(session.os_type(), OSType::WINDOWS); @@ -349,14 +342,14 @@ TEST(ShareSessionTest, HandleKeyVerificationResultSelfShareSuccess) { EXPECT_TRUE(session.token().empty()); } -TEST(ShareSessionTest, HandleKeyVerificationResultNotSelfShareSuccess) { +TEST(ShareSessionTest, ProcessKeyVerificationResultNotSelfShareSuccess) { ShareTarget share_target; TestShareSession session(std::string(kEndpointId), share_target); NearbyConnectionImpl connection(session.device_info()); session.SetNearbyConnection(&connection); session.SetTokenForTests("9876"); - EXPECT_TRUE(session.HandleKeyVerificationResult( + EXPECT_TRUE(session.ProcessKeyVerificationResult( PairedKeyVerificationRunner::PairedKeyVerificationResult::kSuccess, OSType::WINDOWS)); EXPECT_EQ(session.os_type(), OSType::WINDOWS); @@ -365,7 +358,7 @@ TEST(ShareSessionTest, HandleKeyVerificationResultNotSelfShareSuccess) { EXPECT_TRUE(session.token().empty()); } -TEST(ShareSessionTest, HandleKeyVerificationResultSelfShareUnable) { +TEST(ShareSessionTest, ProcessKeyVerificationResultSelfShareUnable) { ShareTarget share_target; share_target.for_self_share = true; TestShareSession session(std::string(kEndpointId), share_target); @@ -373,7 +366,7 @@ TEST(ShareSessionTest, HandleKeyVerificationResultSelfShareUnable) { session.SetNearbyConnection(&connection); session.SetTokenForTests("9876"); - EXPECT_TRUE(session.HandleKeyVerificationResult( + EXPECT_TRUE(session.ProcessKeyVerificationResult( PairedKeyVerificationRunner::PairedKeyVerificationResult::kUnable, OSType::WINDOWS)); EXPECT_EQ(session.os_type(), OSType::WINDOWS); @@ -381,14 +374,14 @@ TEST(ShareSessionTest, HandleKeyVerificationResultSelfShareUnable) { EXPECT_FALSE(session.token().empty()); } -TEST(ShareSessionTest, HandleKeyVerificationResultNotSelfShareUnable) { +TEST(ShareSessionTest, ProcessKeyVerificationResultNotSelfShareUnable) { ShareTarget share_target; TestShareSession session(std::string(kEndpointId), share_target); NearbyConnectionImpl connection(session.device_info()); session.SetNearbyConnection(&connection); session.SetTokenForTests("9876"); - EXPECT_TRUE(session.HandleKeyVerificationResult( + EXPECT_TRUE(session.ProcessKeyVerificationResult( PairedKeyVerificationRunner::PairedKeyVerificationResult::kUnable, OSType::WINDOWS)); EXPECT_EQ(session.os_type(), OSType::WINDOWS); @@ -396,14 +389,14 @@ TEST(ShareSessionTest, HandleKeyVerificationResultNotSelfShareUnable) { EXPECT_FALSE(session.token().empty()); } -TEST(ShareSessionTest, HandleKeyVerificationResultUnknown) { +TEST(ShareSessionTest, ProcessKeyVerificationResultUnknown) { ShareTarget share_target; TestShareSession session(std::string(kEndpointId), share_target); NearbyConnectionImpl connection(session.device_info()); session.SetNearbyConnection(&connection); session.SetTokenForTests("9876"); - EXPECT_FALSE(session.HandleKeyVerificationResult( + EXPECT_FALSE(session.ProcessKeyVerificationResult( PairedKeyVerificationRunner::PairedKeyVerificationResult::kUnknown, OSType::WINDOWS)); EXPECT_EQ(session.os_type(), OSType::WINDOWS);