diff --git a/connections/advertising_options.h b/connections/advertising_options.h index d7408d16..7dd467c7 100644 --- a/connections/advertising_options.h +++ b/connections/advertising_options.h @@ -35,6 +35,8 @@ struct AdvertisingOptions : public OptionsBase { bool enable_webrtc_listening; // Indicates whether the endpoint id should be stable. bool use_stable_endpoint_id = false; + // If true, a new endpoint id will be generated. + bool force_new_endpoint_id = false; // Whether this is intended to be used in conjunction with InjectEndpoint(). bool is_out_of_band_connection = false; diff --git a/connections/core.cc b/connections/core.cc index eb685c5d..61b77422 100644 --- a/connections/core.cc +++ b/connections/core.cc @@ -286,6 +286,7 @@ void Core::StartAdvertisingV3(absl::string_view service_id, /*enable_webrtc_listening=*/ advertising_options.advertising_mediums.web_rtc, /*use_stable_endpoint_id=*/advertising_options.use_stable_endpoint_id, + /*force_new_endpoint_id=*/false, /*is_out_of_band_connection=*/false, /*fast_advertisement_service_uuid=*/ advertising_options.fast_advertisement_service_uuid, @@ -522,6 +523,7 @@ void Core::UpdateAdvertisingOptionsV3( /*enable_webrtc_listening=*/ advertising_options.advertising_mediums.web_rtc, /*use_stable_endpoint_id=*/advertising_options.use_stable_endpoint_id, + /*force_new_endpoint_id=*/false, /*is_out_of_band_connection=*/false, /*fast_advertisement_service_uuid=*/ advertising_options.fast_advertisement_service_uuid, diff --git a/connections/implementation/base_pcp_handler.cc b/connections/implementation/base_pcp_handler.cc index bb9cadff..3de89fab 100644 --- a/connections/implementation/base_pcp_handler.cc +++ b/connections/implementation/base_pcp_handler.cc @@ -233,6 +233,9 @@ Status BasePcpHandler::StartAdvertising( "start-advertising", [this, client, &service_id, &info, &compatible_advertising_options, &response]() RUN_ON_PCP_HANDLER_THREAD() { + if (compatible_advertising_options.force_new_endpoint_id) { + client->ClearCachedLocalEndpointId(); + } if (NearbyFlags::GetInstance().GetBoolFlag( connections::config_package_nearby::nearby_connections_feature:: kUseStableEndpointId)) { diff --git a/connections/implementation/base_pcp_handler_test.cc b/connections/implementation/base_pcp_handler_test.cc index e6e056ab..bc9385e5 100644 --- a/connections/implementation/base_pcp_handler_test.cc +++ b/connections/implementation/base_pcp_handler_test.cc @@ -2875,6 +2875,44 @@ TEST_F(BasePcpHandlerTest, TestUpdateDiscoveryOptionsFailsWithBadStatus) { env_.Stop(); } +TEST_F(BasePcpHandlerTest, TestForceUpdateEndpointIdAdvertisingOption) { + env_.Start(); + AdvertisingOptions use_old_endpoint_id_options{ + .auto_upgrade_bandwidth = true, + .enforce_topology_constraints = true, + .low_power = true, + .enable_bluetooth_listening = false, + .force_new_endpoint_id = false, + }; + AdvertisingOptions use_new_endpoint_id_options{ + .auto_upgrade_bandwidth = true, + .enforce_topology_constraints = true, + .low_power = true, + .enable_bluetooth_listening = false, + .force_new_endpoint_id = true, + }; + ClientProxy client; + Mediums m; + EndpointChannelManager ecm; + EndpointManager em(&ecm); + BwuManager bwu(m, em, ecm, {}, {}); + std::string old_endpoint_id = client.GetLocalEndpointId(); + MockPcpHandler pcp_handler(&m, &em, &ecm, &bwu); + StartAdvertisingWithOptions(&client, &pcp_handler, + use_old_endpoint_id_options); + EXPECT_TRUE(client.IsAdvertising()); + EXPECT_EQ(client.GetLocalEndpointId(), old_endpoint_id); + + pcp_handler.StopAdvertising(&client); + EXPECT_FALSE(client.IsAdvertising()); + + StartAdvertisingWithOptions(&client, &pcp_handler, + use_new_endpoint_id_options); + EXPECT_TRUE(client.IsAdvertising()); + EXPECT_NE(client.GetLocalEndpointId(), old_endpoint_id); + env_.Stop(); +} + } // namespace } // namespace connections } // namespace nearby diff --git a/connections/implementation/client_proxy.cc b/connections/implementation/client_proxy.cc index 0190b9f7..78d37928 100644 --- a/connections/implementation/client_proxy.cc +++ b/connections/implementation/client_proxy.cc @@ -1189,12 +1189,7 @@ void ClientProxy::ScheduleClearCachedEndpointIdAlarm() { cached_endpoint_id_alarm_ = std::make_unique( "clear_high_power_endpoint_id_cache", [this]() { - MutexLock lock(&mutex_); - LOG(INFO) << "ClientProxy [Cleared cached local high power advertising " - "endpoint Id.]: client=" - << GetClientId() - << "; cached_endpoint_id_=" << cached_endpoint_id_; - cached_endpoint_id_.clear(); + ClearCachedLocalEndpointId(); }, kHighPowerAdvertisementEndpointIdCacheTimeout, &single_thread_executor_); } @@ -1206,6 +1201,13 @@ void ClientProxy::CancelClearCachedEndpointIdAlarm() { } } +void ClientProxy::ClearCachedLocalEndpointId() { + MutexLock lock(&mutex_); + LOG(INFO) << "ClientProxy [Cleared cached local endpoint Id.]: client=" + << GetClientId() << "; cached_endpoint_id_=" << cached_endpoint_id_; + cached_endpoint_id_.clear(); +} + OsInfo::OsType ClientProxy::OSNameToOsInfoType(api::OSName osName) { switch (osName) { case api::OSName::kLinux: diff --git a/connections/implementation/client_proxy.h b/connections/implementation/client_proxy.h index 7801c546..30183ec8 100644 --- a/connections/implementation/client_proxy.h +++ b/connections/implementation/client_proxy.h @@ -368,6 +368,9 @@ class ClientProxy final { kWifiLanMultiplexEnabled = 1 << 3, }; + // Forces client to regenerate a new local endpoint id. + void ClearCachedLocalEndpointId(); + private: struct Connection { // Status: may be either: diff --git a/sharing/fake_nearby_connections_manager.cc b/sharing/fake_nearby_connections_manager.cc index 28b3fbe7..b152fabb 100644 --- a/sharing/fake_nearby_connections_manager.cc +++ b/sharing/fake_nearby_connections_manager.cc @@ -53,7 +53,7 @@ void FakeNearbyConnectionsManager::Shutdown() { void FakeNearbyConnectionsManager::StartAdvertising( std::vector endpoint_info, IncomingConnectionListener* listener, PowerLevel power_level, DataUsage data_usage, bool use_stable_endpoint_id, - ConnectionsCallback callback) { + bool force_new_endpoint_id, ConnectionsCallback callback) { DCHECK(!IsAdvertising()); is_shutdown_ = false; { diff --git a/sharing/fake_nearby_connections_manager.h b/sharing/fake_nearby_connections_manager.h index 2cf787a2..9131b7c9 100644 --- a/sharing/fake_nearby_connections_manager.h +++ b/sharing/fake_nearby_connections_manager.h @@ -50,7 +50,7 @@ class FakeNearbyConnectionsManager : public NearbyConnectionsManager { void StartAdvertising(std::vector endpoint_info, IncomingConnectionListener* listener, PowerLevel power_level, proto::DataUsage data_usage, - bool use_stable_endpoint_id, + bool use_stable_endpoint_id, bool force_new_endpoint_id, ConnectionsCallback callback) override; void StopAdvertising(ConnectionsCallback callback) override; void StartDiscovery(DiscoveryListener* listener, proto::DataUsage data_usage, diff --git a/sharing/nearby_connections_manager.h b/sharing/nearby_connections_manager.h index 896ca7b3..179c7718 100644 --- a/sharing/nearby_connections_manager.h +++ b/sharing/nearby_connections_manager.h @@ -103,6 +103,7 @@ class NearbyConnectionsManager { PowerLevel power_level, proto::DataUsage data_usage, bool use_stable_endpoint_id, + bool force_new_endpoint_id, ConnectionsCallback callback) = 0; // Stops advertising through Nearby Connections. diff --git a/sharing/nearby_connections_manager_impl.cc b/sharing/nearby_connections_manager_impl.cc index d5b5d937..2153bcab 100644 --- a/sharing/nearby_connections_manager_impl.cc +++ b/sharing/nearby_connections_manager_impl.cc @@ -176,7 +176,7 @@ void NearbyConnectionsManagerImpl::Shutdown() { Reset(); } void NearbyConnectionsManagerImpl::StartAdvertising( std::vector endpoint_info, IncomingConnectionListener* listener, PowerLevel power_level, DataUsage data_usage, bool use_stable_endpoint_id, - ConnectionsCallback callback) { + bool force_new_endpoint_id, ConnectionsCallback callback) { DCHECK(listener); DCHECK(!incoming_connection_listener_); @@ -260,6 +260,7 @@ void NearbyConnectionsManagerImpl::StartAdvertising( .enable_webrtc_listening = ShouldEnableWebRtc( connectivity_manager_, data_usage, power_level), .use_stable_endpoint_id = use_stable_endpoint_id, + .force_new_endpoint_id = force_new_endpoint_id, .fast_advertisement_service_uuid = fast_advertisement_service_uuid, }, std::move(connection_listener), std::move(callback)); diff --git a/sharing/nearby_connections_manager_impl.h b/sharing/nearby_connections_manager_impl.h index 82041689..bac82acf 100644 --- a/sharing/nearby_connections_manager_impl.h +++ b/sharing/nearby_connections_manager_impl.h @@ -61,7 +61,7 @@ class NearbyConnectionsManagerImpl : public NearbyConnectionsManager { void StartAdvertising(std::vector endpoint_info, IncomingConnectionListener* listener, PowerLevel power_level, proto::DataUsage data_usage, - bool use_stable_endpoint_id, + bool use_stable_endpoint_id, bool force_new_endpoint_id, ConnectionsCallback callback) override; void StopAdvertising(ConnectionsCallback callback) override; void StartDiscovery(DiscoveryListener* listener, proto::DataUsage data_usage, diff --git a/sharing/nearby_connections_manager_impl_test.cc b/sharing/nearby_connections_manager_impl_test.cc index 937a78ea..a04af8fc 100644 --- a/sharing/nearby_connections_manager_impl_test.cc +++ b/sharing/nearby_connections_manager_impl_test.cc @@ -241,6 +241,7 @@ class NearbyConnectionsManagerImplTest : public testing::Test { nearby_connections_manager_->StartAdvertising( local_endpoint_info, &incoming_connection_listener, PowerLevel::kHighPower, DataUsage::ONLINE_DATA_USAGE, false, + /*force_new_endpoint_id=*/false, std::move(callback)); EXPECT_TRUE( notification.WaitForNotificationWithTimeout(kSynchronizationTimeOut)); @@ -1690,7 +1691,8 @@ TEST_P(NearbyConnectionsManagerImplTestMediums, StartAdvertising_Options) { nearby_connections_manager_->StartAdvertising( local_endpoint_info, &incoming_connection_listener, power_level, - data_usage, false, std::move(callback)); + data_usage, false, + /*force_new_endpoint_id=*/false, std::move(callback)); EXPECT_TRUE( notification.WaitForNotificationWithTimeout(kSynchronizationTimeOut)); diff --git a/sharing/nearby_connections_service_impl.cc b/sharing/nearby_connections_service_impl.cc index 240d4fa2..58045704 100644 --- a/sharing/nearby_connections_service_impl.cc +++ b/sharing/nearby_connections_service_impl.cc @@ -84,6 +84,7 @@ void NearbyConnectionsServiceImpl::StartAdvertising( advertising_options.enable_bluetooth_listening; options.enable_webrtc_listening = advertising_options.enable_webrtc_listening; options.use_stable_endpoint_id = advertising_options.use_stable_endpoint_id; + options.force_new_endpoint_id = advertising_options.force_new_endpoint_id; options.fast_advertisement_service_uuid = advertising_options.fast_advertisement_service_uuid.uuid; diff --git a/sharing/nearby_connections_types.h b/sharing/nearby_connections_types.h index 71145f5c..a30cba63 100644 --- a/sharing/nearby_connections_types.h +++ b/sharing/nearby_connections_types.h @@ -225,6 +225,8 @@ struct AdvertisingOptions { // Indicates whether the endpoint id should be stable. When visibility is // everyone mode, we should set this to true to avoid duplicated endpoint ids. bool use_stable_endpoint_id = false; + // If true, a new endpoint id will be generated. + bool force_new_endpoint_id = false; // Optional. If set, BLE advertisements will be in their "fast advertisement" // form, use this UUID, and non-connectable; if empty, BLE advertisements // will otherwise be normal and connectable. diff --git a/sharing/nearby_sharing_service_impl.cc b/sharing/nearby_sharing_service_impl.cc index 4fbab3ae..2fa117c7 100644 --- a/sharing/nearby_sharing_service_impl.cc +++ b/sharing/nearby_sharing_service_impl.cc @@ -1274,6 +1274,9 @@ void NearbySharingServiceImpl::OnLoginSucceeded(absl::string_view account_id) { RunOnNearbySharingServiceThread("on_login_succeeded", [this]() { LOG(INFO) << "Account login."; + // Reset endpoint id after login. Needs to happen before ResetAllSettings + // which starts advertising. + force_new_endpoint_id_ = true; ResetAllSettings(/*logout=*/false); }); } @@ -1284,6 +1287,9 @@ void NearbySharingServiceImpl::OnLogoutSucceeded(absl::string_view account_id, "on_logout_succeeded", [this, credential_error]() { LOG(INFO) << "Account logout."; + // Reset endpoint id after logout. Needs to happen before + // ResetAllSettings which starts advertising. + force_new_endpoint_id_ = true; // Reset all settings. ResetAllSettings(/*logout=*/true); if (credential_error) { @@ -1998,6 +2004,7 @@ void NearbySharingServiceImpl::InvalidateAdvertisingState() { *endpoint_info, /*listener=*/this, power_level, data_usage, visibility == DeviceVisibility::DEVICE_VISIBILITY_EVERYONE, + force_new_endpoint_id_, [this, visibility, data_usage](Status status) { // Log analytics event of advertising start. analytics_recorder_.NewAdvertiseDevicePresenceStart( @@ -2009,6 +2016,7 @@ void NearbySharingServiceImpl::InvalidateAdvertisingState() { OnStartAdvertisingResult( visibility == DeviceVisibility::DEVICE_VISIBILITY_EVERYONE, status); }); + force_new_endpoint_id_ = false; advertising_power_level_ = power_level; VLOG(1) << __func__ diff --git a/sharing/nearby_sharing_service_impl.h b/sharing/nearby_sharing_service_impl.h index 07adce1c..4db24dd8 100644 --- a/sharing/nearby_sharing_service_impl.h +++ b/sharing/nearby_sharing_service_impl.h @@ -585,6 +585,8 @@ class NearbySharingServiceImpl absl::Time share_foreground_send_surface_start_timestamp_; std::unique_ptr app_info_; std::optional alternate_service_uuid_; + // If true, a new endpoint id will be generated at the next advertisement. + bool force_new_endpoint_id_ = false; }; } // namespace nearby::sharing