diff --git a/sharing/BUILD b/sharing/BUILD index 19b868e9..24a00337 100644 --- a/sharing/BUILD +++ b/sharing/BUILD @@ -47,6 +47,7 @@ cc_library( visibility = [ "//location/nearby/cpp/sharing:__subpackages__", "//location/nearby/sharing/sdk/quick_share_server:__pkg__", + "//location/nearby/testing/nearby_native:__subpackages__", "//sharing:__subpackages__", ], deps = [ @@ -80,6 +81,7 @@ cc_library( visibility = [ "//location/nearby/cpp/sharing:__subpackages__", "//location/nearby/sharing/sdk/quick_share_server:__pkg__", + "//location/nearby/testing/nearby_native:__subpackages__", "//sharing:__subpackages__", ], deps = [ @@ -110,6 +112,7 @@ cc_library( visibility = [ "//location/nearby/cpp/sharing:__subpackages__", "//location/nearby/sharing/sdk/quick_share_server:__pkg__", + "//location/nearby/testing/nearby_native:__subpackages__", "//sharing:__subpackages__", ], deps = [ @@ -183,6 +186,7 @@ cc_library( visibility = [ "//location/nearby/cpp/sharing:__subpackages__", "//location/nearby/sharing/sdk/quick_share_server:__pkg__", + "//location/nearby/testing/nearby_native:__subpackages__", "//sharing:__subpackages__", ], deps = [ diff --git a/sharing/fake_nearby_sharing_service.cc b/sharing/fake_nearby_sharing_service.cc index c1423454..4da5d5cb 100644 --- a/sharing/fake_nearby_sharing_service.cc +++ b/sharing/fake_nearby_sharing_service.cc @@ -31,6 +31,7 @@ #include "sharing/share_target_discovered_callback.h" #include "sharing/transfer_metadata.h" #include "sharing/transfer_update_callback.h" +#include "sharing/wrapped_share_target_discovered_callback.h" namespace nearby { namespace sharing { @@ -61,11 +62,15 @@ void FakeNearbySharingService::RegisterSendSurface( ShareTargetDiscoveredCallback* discovery_callback, SendSurfaceState state, std::function status_codes_callback) { if (state == SendSurfaceState::kForeground) { - foreground_send_transfer_callbacks_.AddObserver(transfer_callback); - foreground_send_discovered_callbacks_.AddObserver(discovery_callback); + foreground_send_surface_map_.insert( + {transfer_callback, + WrappedShareTargetDiscoveredCallback( + discovery_callback, Advertisement::BlockedVendorId::kNone)}); } else { - background_send_transfer_callbacks_.AddObserver(transfer_callback); - background_send_discovered_callbacks_.AddObserver(discovery_callback); + background_send_surface_map_.insert( + {transfer_callback, + WrappedShareTargetDiscoveredCallback( + discovery_callback, Advertisement::BlockedVendorId::kNone)}); } status_codes_callback(StatusCodes::kOk); @@ -74,12 +79,9 @@ void FakeNearbySharingService::RegisterSendSurface( // Unregisters the current send surface. void FakeNearbySharingService::UnregisterSendSurface( TransferUpdateCallback* transfer_callback, - ShareTargetDiscoveredCallback* discovery_callback, std::function status_codes_callback) { - foreground_send_transfer_callbacks_.RemoveObserver(transfer_callback); - foreground_send_discovered_callbacks_.RemoveObserver(discovery_callback); - background_send_transfer_callbacks_.RemoveObserver(transfer_callback); - background_send_discovered_callbacks_.RemoveObserver(discovery_callback); + foreground_send_surface_map_.erase(transfer_callback); + background_send_surface_map_.erase(transfer_callback); status_codes_callback(StatusCodes::kOk); } @@ -261,16 +263,14 @@ void FakeNearbySharingService::FireSendTransferUpdate( const AttachmentContainer& attachment_container, TransferMetadata transfer_metadata) { if (state == SendSurfaceState::kForeground) { - for (auto& transfer_callback : - foreground_send_transfer_callbacks_.GetObservers()) { - transfer_callback->OnTransferUpdate(share_target, attachment_container, - transfer_metadata); + for (auto& entry : foreground_send_surface_map_) { + entry.first->OnTransferUpdate(share_target, attachment_container, + transfer_metadata); } } else { - for (auto& transfer_callback : - background_send_transfer_callbacks_.GetObservers()) { - transfer_callback->OnTransferUpdate(share_target, attachment_container, - transfer_metadata); + for (auto& entry : background_send_surface_map_) { + entry.first->OnTransferUpdate(share_target, attachment_container, + transfer_metadata); } } } @@ -298,14 +298,12 @@ void FakeNearbySharingService::FireReceiveTransferUpdate( void FakeNearbySharingService::FireShareTargetDiscovered( SendSurfaceState state, ShareTarget share_target) { if (state == SendSurfaceState::kForeground) { - for (auto& discovered_callback : - foreground_send_discovered_callbacks_.GetObservers()) { - discovered_callback->OnShareTargetDiscovered(share_target); + for (auto& entry : foreground_send_surface_map_) { + entry.second.OnShareTargetDiscovered(share_target); } } else { - for (auto& discovered_callback : - background_send_discovered_callbacks_.GetObservers()) { - discovered_callback->OnShareTargetDiscovered(share_target); + for (auto& entry : background_send_surface_map_) { + entry.second.OnShareTargetDiscovered(share_target); } } } @@ -313,14 +311,12 @@ void FakeNearbySharingService::FireShareTargetDiscovered( void FakeNearbySharingService::FireShareTargetLost(SendSurfaceState state, ShareTarget share_target) { if (state == SendSurfaceState::kForeground) { - for (auto& discovered_callback : - foreground_send_discovered_callbacks_.GetObservers()) { - discovered_callback->OnShareTargetLost(share_target); + for (auto& entry : foreground_send_surface_map_) { + entry.second.OnShareTargetLost(share_target); } } else { - for (auto& discovered_callback : - background_send_discovered_callbacks_.GetObservers()) { - discovered_callback->OnShareTargetLost(share_target); + for (auto& entry : background_send_surface_map_) { + entry.second.OnShareTargetLost(share_target); } } } diff --git a/sharing/fake_nearby_sharing_service.h b/sharing/fake_nearby_sharing_service.h index 5c70b8c3..dc80a639 100644 --- a/sharing/fake_nearby_sharing_service.h +++ b/sharing/fake_nearby_sharing_service.h @@ -31,6 +31,7 @@ #include "sharing/share_target_discovered_callback.h" #include "sharing/transfer_metadata.h" #include "sharing/transfer_update_callback.h" +#include "sharing/wrapped_share_target_discovered_callback.h" namespace nearby { namespace sharing { @@ -57,7 +58,6 @@ class FakeNearbySharingService : public NearbySharingService { // Unregisters the current send surface. void UnregisterSendSurface( TransferUpdateCallback* transfer_callback, - ShareTargetDiscoveredCallback* discovery_callback, std::function status_codes_callback) override; // Registers a receiver surface for handling payload transfer status. @@ -169,12 +169,14 @@ class FakeNearbySharingService : public NearbySharingService { private: ObserverList observers_; ObserverList settings_observers_; - ObserverList foreground_send_transfer_callbacks_; - ObserverList background_send_transfer_callbacks_; - ObserverList - foreground_send_discovered_callbacks_; - ObserverList - background_send_discovered_callbacks_; + // A mapping of foreground transfer callbacks to foreground send surface data. + absl::flat_hash_map + foreground_send_surface_map_; + // A mapping of background transfer callbacks to background send surface data. + absl::flat_hash_map + background_send_surface_map_; ObserverList foreground_receive_transfer_callbacks_; ObserverList background_receive_transfer_callbacks_; }; diff --git a/sharing/nearby_sharing_service.h b/sharing/nearby_sharing_service.h index c02d38f2..23447f34 100644 --- a/sharing/nearby_sharing_service.h +++ b/sharing/nearby_sharing_service.h @@ -153,7 +153,6 @@ class NearbySharingService { // Unregisters the current send surface. virtual void UnregisterSendSurface( TransferUpdateCallback* transfer_callback, - ShareTargetDiscoveredCallback* discovery_callback, std::function status_codes_callback) = 0; // Registers a receiver surface for handling payload transfer status, and diff --git a/sharing/nearby_sharing_service_impl.cc b/sharing/nearby_sharing_service_impl.cc index 5cbda5b3..3f706979 100644 --- a/sharing/nearby_sharing_service_impl.cc +++ b/sharing/nearby_sharing_service_impl.cc @@ -499,11 +499,10 @@ void NearbySharingServiceImpl::RegisterSendSurface( void NearbySharingServiceImpl::UnregisterSendSurface( TransferUpdateCallback* transfer_callback, - ShareTargetDiscoveredCallback* discovery_callback, std::function status_codes_callback) { RunOnNearbySharingServiceThread( "api_unregister_send_surface", - [this, transfer_callback, discovery_callback, + [this, transfer_callback, status_codes_callback = std::move(status_codes_callback)]() { StatusCodes status_codes = InternalUnregisterSendSurface(transfer_callback); diff --git a/sharing/nearby_sharing_service_impl.h b/sharing/nearby_sharing_service_impl.h index a3df97b0..7a3d4bc3 100644 --- a/sharing/nearby_sharing_service_impl.h +++ b/sharing/nearby_sharing_service_impl.h @@ -138,7 +138,6 @@ class NearbySharingServiceImpl std::function status_codes_callback) override; void UnregisterSendSurface( TransferUpdateCallback* transfer_callback, - ShareTargetDiscoveredCallback* discovery_callback, std::function status_codes_callback) override; void RegisterReceiveSurface( TransferUpdateCallback* transfer_callback, ReceiveSurfaceState state, diff --git a/sharing/nearby_sharing_service_impl_test.cc b/sharing/nearby_sharing_service_impl_test.cc index 3308af8a..83b63509 100644 --- a/sharing/nearby_sharing_service_impl_test.cc +++ b/sharing/nearby_sharing_service_impl_test.cc @@ -511,14 +511,12 @@ class NearbySharingServiceImplTest : public testing::Test { } NearbySharingService::StatusCodes UnregisterSendSurface( - TransferUpdateCallback* transfer_callback, - ShareTargetDiscoveredCallback* discovery_callback) { + TransferUpdateCallback* transfer_callback) { NearbySharingService::StatusCodes result = NearbySharingService::StatusCodes::kError; absl::Notification notification; service_->UnregisterSendSurface( - transfer_callback, discovery_callback, - [&](NearbySharingService::StatusCodes status_codes) { + transfer_callback, [&](NearbySharingService::StatusCodes status_codes) { result = status_codes; notification.Notify(); }); @@ -1463,7 +1461,7 @@ TEST_F(NearbySharingServiceImplTest, StopFastInitiationAdvertising) { SendSurfaceState::kForeground), NearbySharingService::StatusCodes::kOk); EXPECT_EQ(fast_initiation->StartAdvertisingCount(), 1); - EXPECT_EQ(UnregisterSendSurface(&transfer_callback, &discovery_callback), + EXPECT_EQ(UnregisterSendSurface(&transfer_callback), NearbySharingService::StatusCodes::kOk); EXPECT_EQ(fast_initiation->StartAdvertisingCount(), fast_initiation->StopAdvertisingCount()); @@ -1859,7 +1857,7 @@ TEST_F(NearbySharingServiceImplTest, UnregisterSendSurfaceStopsDiscovering) { NearbySharingService::StatusCodes::kOk); EXPECT_TRUE(fake_nearby_connections_manager_->IsDiscovering()); - EXPECT_EQ(UnregisterSendSurface(&transfer_callback, &discovery_callback), + EXPECT_EQ(UnregisterSendSurface(&transfer_callback), NearbySharingService::StatusCodes::kOk); EXPECT_FALSE(fake_nearby_connections_manager_->IsDiscovering()); EXPECT_FALSE(fake_nearby_connections_manager_->is_shutdown()); @@ -1877,7 +1875,7 @@ TEST_F(NearbySharingServiceImplTest, MockTransferUpdateCallback transfer_callback2; MockShareTargetDiscoveredCallback discovery_callback2; - EXPECT_EQ(UnregisterSendSurface(&transfer_callback2, &discovery_callback2), + EXPECT_EQ(UnregisterSendSurface(&transfer_callback2), NearbySharingService::StatusCodes::kError); EXPECT_TRUE(fake_nearby_connections_manager_->IsDiscovering()); } @@ -1887,7 +1885,7 @@ TEST_F(NearbySharingServiceImplTest, UnregisterSendSurfaceNeverRegistered) { MockTransferUpdateCallback transfer_callback; MockShareTargetDiscoveredCallback discovery_callback; - EXPECT_EQ(UnregisterSendSurface(&transfer_callback, &discovery_callback), + EXPECT_EQ(UnregisterSendSurface(&transfer_callback), NearbySharingService::StatusCodes::kError); EXPECT_FALSE(fake_nearby_connections_manager_->IsDiscovering()); } @@ -3441,7 +3439,7 @@ TEST_F(NearbySharingServiceImplTest, SendAttachmentsWithoutAttachments) { EXPECT_EQ(SendAttachments(target, /*attachment_container=*/nullptr), NearbySharingServiceImpl::StatusCodes::kInvalidArgument); - UnregisterSendSurface(&transfer_callback, &discovery_callback); + UnregisterSendSurface(&transfer_callback); } TEST_F(NearbySharingServiceImplTest, RegisterReceiveSurfaceWhileSending) { @@ -3466,7 +3464,7 @@ TEST_F(NearbySharingServiceImplTest, RegisterReceiveSurfaceWhileSending) { NearbySharingService::ReceiveSurfaceState::kForeground); EXPECT_EQ(result, NearbySharingService::StatusCodes::kOk); - UnregisterSendSurface(&transfer_callback, &discovery_callback); + UnregisterSendSurface(&transfer_callback); } TEST_F(NearbySharingServiceImplTest, SendTextAlreadySending) { @@ -3490,7 +3488,7 @@ TEST_F(NearbySharingServiceImplTest, SendTextAlreadySending) { EXPECT_EQ(SendAttachments(target, CreateTextAttachments({kTextPayload})), NearbySharingServiceImpl::StatusCodes::kError); - UnregisterSendSurface(&transfer_callback, &discovery_callback); + UnregisterSendSurface(&transfer_callback); } TEST_F(NearbySharingServiceImplTest, SendTextWithoutScanning) { @@ -3507,7 +3505,7 @@ TEST_F(NearbySharingServiceImplTest, SendTextUnknownTarget) { ShareTarget target; EXPECT_EQ(SendAttachments(target, CreateTextAttachments({kTextPayload})), NearbySharingServiceImpl::StatusCodes::kInvalidArgument); - UnregisterSendSurface(&transfer_callback, &discovery_callback); + UnregisterSendSurface(&transfer_callback); } TEST_F(NearbySharingServiceImplTest, SendTextFailedCreateEndpointInfo) { @@ -3522,7 +3520,7 @@ TEST_F(NearbySharingServiceImplTest, SendTextFailedCreateEndpointInfo) { EXPECT_EQ(SendAttachments(target, CreateTextAttachments({kTextPayload})), NearbySharingServiceImpl::StatusCodes::kError); - UnregisterSendSurface(&transfer_callback, &discovery_callback); + UnregisterSendSurface(&transfer_callback); } TEST_F(NearbySharingServiceImplTest, SendTextFailedToConnect) { @@ -3545,7 +3543,7 @@ TEST_F(NearbySharingServiceImplTest, SendTextFailedToConnect) { NearbySharingServiceImpl::StatusCodes::kOk); EXPECT_TRUE(notification.WaitForNotificationWithTimeout(kWaitTimeout)); - UnregisterSendSurface(&transfer_callback, &discovery_callback); + UnregisterSendSurface(&transfer_callback); } TEST_F(NearbySharingServiceImplTest, SendTextFailedKeyVerification) { @@ -3571,7 +3569,7 @@ TEST_F(NearbySharingServiceImplTest, SendTextFailedKeyVerification) { NearbySharingServiceImpl::StatusCodes::kOk); EXPECT_TRUE(notification.WaitForNotificationWithTimeout(kWaitTimeout)); - UnregisterSendSurface(&transfer_callback, &discovery_callback); + UnregisterSendSurface(&transfer_callback); } TEST_F(NearbySharingServiceImplTest, SendTextUnableToVerifyKey) { @@ -3597,7 +3595,7 @@ TEST_F(NearbySharingServiceImplTest, SendTextUnableToVerifyKey) { NearbySharingServiceImpl::StatusCodes::kOk); EXPECT_TRUE(notification.WaitForNotificationWithTimeout(kWaitTimeout)); - UnregisterSendSurface(&transfer_callback, &discovery_callback); + UnregisterSendSurface(&transfer_callback); } INSTANTIATE_TEST_SUITE_P(NearbySharingServiceImplSendFailureTest, @@ -3638,7 +3636,7 @@ TEST_P(NearbySharingServiceImplSendFailureTest, SendTextRemoteFailure) { EXPECT_TRUE(connection_.IsClosed()); - UnregisterSendSurface(&transfer_callback, &discovery_callback); + UnregisterSendSurface(&transfer_callback); } TEST_P(NearbySharingServiceImplSendFailureTest, SendFilesRemoteFailure) { @@ -3678,7 +3676,7 @@ TEST_P(NearbySharingServiceImplSendFailureTest, SendFilesRemoteFailure) { EXPECT_TRUE(connection_.IsClosed()); - UnregisterSendSurface(&transfer_callback, &discovery_callback); + UnregisterSendSurface(&transfer_callback); } TEST_F(NearbySharingServiceImplTest, SendTextSuccess) { @@ -3744,7 +3742,7 @@ TEST_F(NearbySharingServiceImplTest, SendTextSuccess) { EXPECT_FALSE( fake_nearby_connections_manager_->connection_endpoint_info(kEndpointId)); - UnregisterSendSurface(&transfer_callback, &discovery_callback); + UnregisterSendSurface(&transfer_callback); account_manager().SetAccount(std::nullopt); } @@ -3775,7 +3773,7 @@ TEST_F(NearbySharingServiceImplTest, SendTextSuccessClosedConnection) { // Make sure the scheduled disconnect callback does nothing. FastForward(kOutgoingDisconnectionDelay); - UnregisterSendSurface(&transfer_callback, &discovery_callback); + UnregisterSendSurface(&transfer_callback); } TEST_F(NearbySharingServiceImplTest, SendFilesSuccess) { @@ -3841,7 +3839,7 @@ TEST_F(NearbySharingServiceImplTest, SendFilesSuccess) { EXPECT_TRUE( payload_notification.WaitForNotificationWithTimeout(kWaitTimeout)); - UnregisterSendSurface(&transfer_callback, &discovery_callback); + UnregisterSendSurface(&transfer_callback); } TEST_F(NearbySharingServiceImplTest, SendWifiCredentialsSuccess) { @@ -3912,7 +3910,7 @@ TEST_F(NearbySharingServiceImplTest, SendWifiCredentialsSuccess) { EXPECT_TRUE( payload_notification.WaitForNotificationWithTimeout(kWaitTimeout)); - UnregisterSendSurface(&transfer_callback, &discovery_callback); + UnregisterSendSurface(&transfer_callback); } TEST_F(NearbySharingServiceImplTest, CancelSenderInitiator) { @@ -3956,7 +3954,7 @@ TEST_F(NearbySharingServiceImplTest, CancelSenderInitiator) { // After the TransferMetadata::Status::kCancelled update, we expect other // classes to unregister the send surface. - UnregisterSendSurface(&transfer_callback, &discovery_callback); + UnregisterSendSurface(&transfer_callback); // The initiator of the cancel should send a cancel frame to the other device, // then wait a few seconds before disconnecting to allow for processing on the @@ -4370,7 +4368,7 @@ TEST_F(NearbySharingServiceImplTest, EXPECT_CALL(discovery_callback, OnShareTargetLost).Times(0); ProcessLatestPublicCertificateDecryption(/*expected_num_calls=*/1, /*success=*/false); - UnregisterSendSurface(&transfer_callback, &discovery_callback); + UnregisterSendSurface(&transfer_callback); FastForward(kCertificateDownloadDuringDiscoveryPeriod); EXPECT_EQ(certificate_manager()->num_download_public_certificates_calls(), 1u); @@ -4471,7 +4469,7 @@ TEST_F(NearbySharingServiceImplTest, RetryDiscoveredEndpointsDownloadLimit) { FastForward(kCertificateDownloadDuringDiscoveryPeriod); EXPECT_EQ(certificate_manager()->num_download_public_certificates_calls(), 1u + kMaxCertificateDownloadsDuringDiscovery); - UnregisterSendSurface(&transfer_callback, &discovery_callback); + UnregisterSendSurface(&transfer_callback); RegisterSendSurface(&transfer_callback, &discovery_callback, SendSurfaceState::kForeground); // Note: Certificate downloads are also requested in RegisterSendSurface; this @@ -4959,7 +4957,7 @@ TEST_F(NearbySharingServiceImplTest, LoginAndLogoutNoStopRunningSurfaces) { logout_notification.Notify(); }); EXPECT_TRUE(logout_notification.WaitForNotificationWithTimeout(kWaitTimeout)); - UnregisterSendSurface(&transfer_callback, &discovery_callback); + UnregisterSendSurface(&transfer_callback); EXPECT_TRUE(sharing_service_task_runner_->SyncWithTimeout(kTaskWaitTimeout)); }