From 3706d9d4979fa5ec07d75dabb2306aeaf14a9256 Mon Sep 17 00:00:00 2001 From: Francis Tsui Date: Tue, 4 Feb 2025 15:06:57 -0800 Subject: [PATCH] Cleanup PiperOrigin-RevId: 723240490 --- sharing/nearby_sharing_service_impl.cc | 81 ++++++++++++-------------- sharing/nearby_sharing_service_impl.h | 8 ++- 2 files changed, 41 insertions(+), 48 deletions(-) diff --git a/sharing/nearby_sharing_service_impl.cc b/sharing/nearby_sharing_service_impl.cc index e1a8770112..5dfbc502a0 100644 --- a/sharing/nearby_sharing_service_impl.cc +++ b/sharing/nearby_sharing_service_impl.cc @@ -915,9 +915,6 @@ void NearbySharingServiceImpl::DoCancel( return; } - // For metrics. - all_cancelled_share_target_ids_.insert(share_target_id); - // Cancel all ongoing payload transfers before invoking the transfer update // callback. Invoking the transfer update callback first could result in // payload cleanup before we have a chance to cancel the payload via Nearby @@ -1738,6 +1735,36 @@ void NearbySharingServiceImpl::FinishEndpointDiscoveryEvent() { } } +void NearbySharingServiceImpl::OnShareTargetDiscovered( + const ShareTarget& share_target) { + for (auto& entry : foreground_send_surface_map_) { + entry.second.OnShareTargetDiscovered(share_target); + } + for (auto& entry : background_send_surface_map_) { + entry.second.OnShareTargetDiscovered(share_target); + } +} + +void NearbySharingServiceImpl::OnShareTargetUpdated( + const ShareTarget& share_target) { + for (auto& entry : foreground_send_surface_map_) { + entry.second.OnShareTargetUpdated(share_target); + } + for (auto& entry : background_send_surface_map_) { + entry.second.OnShareTargetUpdated(share_target); + } +} + +void NearbySharingServiceImpl::OnShareTargetLost( + const ShareTarget& share_target) { + for (auto& entry : foreground_send_surface_map_) { + entry.second.OnShareTargetLost(share_target); + } + for (auto& entry : background_send_surface_map_) { + entry.second.OnShareTargetLost(share_target); + } +} + void NearbySharingServiceImpl::OnOutgoingDecryptedCertificate( absl::string_view endpoint_id, absl::Span endpoint_info, const Advertisement& advertisement, @@ -1811,12 +1838,7 @@ void NearbySharingServiceImpl::OnOutgoingDecryptedCertificate( background_send_surface_map_.size()) << " discovery callbacks be called."; - for (auto& entry : foreground_send_surface_map_) { - entry.second.OnShareTargetDiscovered(*share_target); - } - for (auto& entry : background_send_surface_map_) { - entry.second.OnShareTargetDiscovered(*share_target); - } + OnShareTargetDiscovered(*share_target); VLOG(1) << __func__ << ": Reported OnShareTargetDiscovered: share_target: " << share_target->ToString() << " endpoint_id=" << endpoint_id @@ -2355,12 +2377,7 @@ void NearbySharingServiceImpl::RemoveOutgoingShareTargetAndReportLost( if (!share_target_opt.has_value()) { return; } - for (auto& entry : foreground_send_surface_map_) { - entry.second.OnShareTargetLost(share_target_opt.value()); - } - for (auto& entry : background_send_surface_map_) { - entry.second.OnShareTargetLost(share_target_opt.value()); - } + OnShareTargetLost(*share_target_opt); VLOG(1) << __func__ << ": Reported OnShareTargetLost for EndpointId: " << endpoint_id @@ -2478,9 +2495,6 @@ void NearbySharingServiceImpl::OnCreatePayloads( std::optional> bluetooth_mac_address = GetBluetoothMacAddressForShareTarget(session); - // For metrics. - all_cancelled_share_target_ids_.clear(); - int64_t share_target_id = session.share_target().id; session.Connect( @@ -3162,12 +3176,7 @@ void NearbySharingServiceImpl::DeduplicateInOutgoingShareTarget( session_it->second.UpdateSessionForDedup(share_target, std::move(certificate), endpoint_id); - for (auto& entry : foreground_send_surface_map_) { - entry.second.OnShareTargetUpdated(share_target); - } - for (auto& entry : background_send_surface_map_) { - entry.second.OnShareTargetUpdated(share_target); - } + OnShareTargetUpdated(share_target); LOG(INFO) << __func__ << ": [Dedupped] Reported OnShareTargetUpdated to all surfaces " @@ -3179,12 +3188,7 @@ void NearbySharingServiceImpl::DeDuplicateInDiscoveryCache( const ShareTarget& share_target, absl::string_view endpoint_id, std::optional certificate) { CreateOutgoingShareSession(share_target, endpoint_id, std::move(certificate)); - for (auto& entry : foreground_send_surface_map_) { - entry.second.OnShareTargetUpdated(share_target); - } - for (auto& entry : background_send_surface_map_) { - entry.second.OnShareTargetUpdated(share_target); - } + OnShareTargetUpdated(share_target); LOG(INFO) << __func__ << ": [Dedupped] Reported OnShareTargetUpdated to all surfaces " @@ -3322,12 +3326,7 @@ void NearbySharingServiceImpl::MoveToDiscoveryCache(std::string endpoint_id, << ", share_target.id=" << share_target.id << ") from discovery_cache after " << expiry_ms << "ms"; - for (auto& entry : foreground_send_surface_map_) { - entry.second.OnShareTargetLost(share_target); - } - for (auto& entry : background_send_surface_map_) { - entry.second.OnShareTargetLost(share_target); - } + OnShareTargetLost(share_target); VLOG(1) << "discovery_cache entry: " << endpoint_id << " timeout after " << expiry_ms << "ms" @@ -3336,12 +3335,7 @@ void NearbySharingServiceImpl::MoveToDiscoveryCache(std::string endpoint_id, << share_target.ToString(); }); // Send ShareTarget update to set receive disabled to true. - for (auto& entry : foreground_send_surface_map_) { - entry.second.OnShareTargetUpdated(cache_entry.share_target); - } - for (auto& entry : background_send_surface_map_) { - entry.second.OnShareTargetUpdated(cache_entry.share_target); - } + OnShareTargetUpdated(cache_entry.share_target); auto [it, inserted] = discovery_cache_.insert_or_assign(endpoint_id, std::move(cache_entry)); LOG(INFO) << "[Dedupped] added to discovery_cache: " << endpoint_id << " by " @@ -3428,9 +3422,6 @@ void NearbySharingServiceImpl::DisableAllOutgoingShareTargets() { void NearbySharingServiceImpl::UnregisterShareTarget(int64_t share_target_id) { LOG(INFO) << __func__ << ": Unregister share target " << share_target_id; - // For metrics. - all_cancelled_share_target_ids_.erase(share_target_id); - // If share target ID is found in incoming_share_session_map_, then it's an // incoming share target. // Do not destroy the session until it has been removed from the map. diff --git a/sharing/nearby_sharing_service_impl.h b/sharing/nearby_sharing_service_impl.h index c2240e657a..285effe5a1 100644 --- a/sharing/nearby_sharing_service_impl.h +++ b/sharing/nearby_sharing_service_impl.h @@ -458,6 +458,11 @@ class NearbySharingServiceImpl void OnIncomingFilesMetadataUpdated(int64_t share_target_id, TransferMetadata metadata, bool success); + // Notify all registered send surfaces of share target state changes. + void OnShareTargetDiscovered(const ShareTarget& share_target); + void OnShareTargetUpdated(const ShareTarget& share_target); + void OnShareTargetLost(const ShareTarget& share_target); + // Used to run nearby sharing service APIs. std::unique_ptr service_thread_; Context* const context_; @@ -526,9 +531,6 @@ class NearbySharingServiceImpl // A map of Endpoint id to DiscoveryCacheEntry. // All ShareTargets in discovery cache have received_disabled set to true. absl::flat_hash_map discovery_cache_; - // For metrics. The IDs of ShareTargets that are cancelled while trying to - // establish an outgoing connection. - absl::flat_hash_set all_cancelled_share_target_ids_; // The IDs of ShareTargets that we cancelled the transfer to. absl::flat_hash_set locally_cancelled_share_target_ids_; // A map from endpoint ID to endpoint info from discovered, contact-based