Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal changes. #3113

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 21 additions & 25 deletions sharing/nearby_sharing_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ void NearbySharingServiceImpl::Cleanup() {

endpoint_discovery_events_ = {};

ClearOutgoingShareSessionMap();
DisableAllOutgoingShareTargets();
discovery_cache_.clear();
for (auto& it : incoming_share_session_map_) {
it.second.OnDisconnect();
Expand Down Expand Up @@ -1710,7 +1710,7 @@ void NearbySharingServiceImpl::HandleEndpointLost(

discovered_advertisements_to_retry_map_.erase(endpoint_id);
discovered_advertisements_retried_set_.erase(endpoint_id);
MoveToDiscoveryCache(endpoint_id,
MoveToDiscoveryCache(std::string(endpoint_id),
NearbyFlags::GetInstance().GetInt64Flag(
config_package_nearby::nearby_sharing_feature::
kDiscoveryCacheLostExpiryMs));
Expand Down Expand Up @@ -2144,7 +2144,7 @@ void NearbySharingServiceImpl::StartScanning() {
is_scanning_ = true;
InvalidateReceiveSurfaceState();

ClearOutgoingShareSessionMap();
DisableAllOutgoingShareTargets();
discovered_advertisements_to_retry_map_.clear();
discovered_advertisements_retried_set_.clear();

Expand Down Expand Up @@ -3322,20 +3322,12 @@ NearbySharingServiceImpl::RemoveOutgoingShareTargetWithEndpointId(
return share_target;
}

void NearbySharingServiceImpl::TriggerDiscoveryCacheExpiryTimers() {
for (auto it = discovery_cache_.begin(); it != discovery_cache_.end(); ++it) {
for (auto& entry : foreground_send_surface_map_) {
entry.second.OnShareTargetLost(it->second.share_target);
}
for (auto& entry : background_send_surface_map_) {
entry.second.OnShareTargetLost(it->second.share_target);
}
}
discovery_cache_.clear();
}

void NearbySharingServiceImpl::MoveToDiscoveryCache(
absl::string_view endpoint_id, uint64_t expiry_ms) {
// Pass endpoint_id by value here since we remove entries from the
// outgoing_share_target_map_ in this function, and some callers like
// DisableAllOutgoingShareTargets pass the map item key as the endpoint_id.
// This prevents the endpoint_id from being invalidated in this function.
void NearbySharingServiceImpl::MoveToDiscoveryCache(std::string endpoint_id,
uint64_t expiry_ms) {
std::optional<ShareTarget> share_target_opt =
RemoveOutgoingShareTargetWithEndpointId(endpoint_id);
if (!share_target_opt.has_value()) {
Expand All @@ -3346,7 +3338,7 @@ void NearbySharingServiceImpl::MoveToDiscoveryCache(
// Entries in Discovery Cache are all receive disabled.
cache_entry.share_target.receive_disabled = true;
cache_entry.expiry_timer = std::make_unique<ThreadTimer>(
*service_thread_, absl::StrCat("discovery_cache_timeout_", expiry_ms),
*service_thread_, absl::StrCat("discovery_cache_timeout_", endpoint_id),
absl::Milliseconds(expiry_ms),
[this, expiry_ms, endpoint_id = std::string(endpoint_id)]() {
auto it = discovery_cache_.find(endpoint_id);
Expand Down Expand Up @@ -3381,7 +3373,10 @@ void NearbySharingServiceImpl::MoveToDiscoveryCache(
for (auto& entry : background_send_surface_map_) {
entry.second.OnShareTargetUpdated(cache_entry.share_target);
}
discovery_cache_.insert_or_assign(endpoint_id, std::move(cache_entry));
auto [it, inserted] =
discovery_cache_.insert_or_assign(endpoint_id, std::move(cache_entry));
LOG(INFO) << "[Dedupped] added to discovery_cache: " << endpoint_id << " by "
<< (inserted ? "insert" : "assign");
}

void NearbySharingServiceImpl::CreateOutgoingShareSession(
Expand Down Expand Up @@ -3448,11 +3443,13 @@ NearbySharingServiceImpl::GetBluetoothMacAddressForShareTarget(
return GetBluetoothMacAddressFromCertificate(*certificate);
}

void NearbySharingServiceImpl::ClearOutgoingShareSessionMap() {
VLOG(1) << __func__ << ": Clearing outgoing share target map.";
void NearbySharingServiceImpl::DisableAllOutgoingShareTargets() {
VLOG(1) << "Move all outgoing share targets to discovery cache.";
while (!outgoing_share_target_map_.empty()) {
RemoveOutgoingShareTargetAndReportLost(
/*endpoint_id=*/outgoing_share_target_map_.begin()->first);
MoveToDiscoveryCache(outgoing_share_target_map_.begin()->first,
NearbyFlags::GetInstance().GetInt64Flag(
config_package_nearby::nearby_sharing_feature::
kUnregisterTargetDiscoveryCacheLostExpiryMs));
}
DCHECK(outgoing_share_target_map_.empty());
DCHECK(outgoing_share_session_map_.empty());
Expand Down Expand Up @@ -3502,9 +3499,8 @@ void NearbySharingServiceImpl::UnregisterShareTarget(int64_t share_target_id) {
if (!is_scanning_ && !is_transferring_) {
LOG(INFO) << "Cannot find session for target " << share_target_id
<< " clearing all outgoing sessions.";
ClearOutgoingShareSessionMap();
DisableAllOutgoingShareTargets();
}
TriggerDiscoveryCacheExpiryTimers();
}

VLOG(1) << __func__
Expand Down
9 changes: 4 additions & 5 deletions sharing/nearby_sharing_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,7 @@ class NearbySharingServiceImpl
std::optional<NearbyShareDecryptedPublicCertificate> certificate);

// Move the endpoint to the discovery cache with the given expiry time.
void MoveToDiscoveryCache(absl::string_view endpoint_id, uint64_t expiry_ms);
// Immediately expire all timers in the discovery cache. (i.e. report
// ShareTargetLost)
void TriggerDiscoveryCacheExpiryTimers();
void MoveToDiscoveryCache(std::string endpoint_id, uint64_t expiry_ms);

// Update the entry in outgoing_share_session_map_ with the new share target
// and OnShareTargetUpdated is called.
Expand Down Expand Up @@ -410,7 +407,9 @@ class NearbySharingServiceImpl
std::optional<std::vector<uint8_t>> GetBluetoothMacAddressForShareTarget(
OutgoingShareSession& session);

void ClearOutgoingShareSessionMap();
// Move all outgoing share targets to the discovery cache so that they will be
// reported as receive_disabled.
void DisableAllOutgoingShareTargets();
void UnregisterShareTarget(int64_t share_target_id);

void OnStartAdvertisingResult(bool used_device_name, Status status);
Expand Down
11 changes: 7 additions & 4 deletions sharing/nearby_sharing_service_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4050,7 +4050,6 @@ TEST_F(NearbySharingServiceImplTest,
EXPECT_EQ(certificate_manager()->num_download_public_certificates_calls(),
1u);

EXPECT_CALL(discovery_callback, OnShareTargetLost);
Shutdown();
service_.reset();
}
Expand Down Expand Up @@ -4096,6 +4095,9 @@ TEST_F(NearbySharingServiceImplTest, DedupSameEndpointId) {
static_cast<uint8_t>(Advertisement::BlockedVendorId::kSamsung));
EXPECT_TRUE(notification.WaitForNotificationWithTimeout(kWaitTimeout));
}
// On shutdown update the share target to receive_disabled.
EXPECT_CALL(discovery_callback, OnShareTargetUpdated(_));

Shutdown();
service_.reset();
}
Expand Down Expand Up @@ -4152,7 +4154,8 @@ TEST_F(NearbySharingServiceImplTest,
static_cast<uint8_t>(Advertisement::BlockedVendorId::kSamsung));
EXPECT_TRUE(notification.WaitForNotificationWithTimeout(kWaitTimeout));
}
EXPECT_CALL(discovery_callback, OnShareTargetLost).Times(1);
// On shutdown update the share target to receive_disabled.
EXPECT_CALL(discovery_callback, OnShareTargetUpdated(_));
Shutdown();
service_.reset();
}
Expand Down Expand Up @@ -4214,7 +4217,6 @@ TEST_F(NearbySharingServiceImplTest, OnLostDedupSameEndpointIdAfterExpiry) {
static_cast<uint8_t>(Advertisement::BlockedVendorId::kSamsung));
EXPECT_TRUE(notification.WaitForNotificationWithTimeout(kWaitTimeout));
}
EXPECT_CALL(discovery_callback, OnShareTargetLost).Times(1);
Shutdown();
service_.reset();
}
Expand Down Expand Up @@ -4334,7 +4336,6 @@ TEST_F(NearbySharingServiceImplTest, EndpointDedupBasedOnDeviceId) {
/*success=*/true);
EXPECT_TRUE(notification.WaitForNotificationWithTimeout(kWaitTimeout));
}
EXPECT_CALL(discovery_callback, OnShareTargetLost).Times(1);

FlushTesting();

Expand Down Expand Up @@ -4364,6 +4365,8 @@ TEST_F(NearbySharingServiceImplTest, EndpointDedupBasedOnDeviceId) {
->connection_endpoint_info(/*endpoint_id=*/"2")
.has_value());

// On shutdown update the share target to receive_disabled.
EXPECT_CALL(discovery_callback, OnShareTargetUpdated(_));
Shutdown();
service_.reset();
}
Expand Down
Loading