Reset endpointUD after login and logout.

PiperOrigin-RevId: 810648551
This commit is contained in:
Francis Tsui
2025-09-23 18:00:51 -07:00
committed by Copybara-Service
parent 6d9122c4eb
commit 5911d53352
16 changed files with 78 additions and 11 deletions
+2
View File
@@ -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;
+2
View File
@@ -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,
@@ -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)) {
@@ -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
+8 -6
View File
@@ -1189,12 +1189,7 @@ void ClientProxy::ScheduleClearCachedEndpointIdAlarm() {
cached_endpoint_id_alarm_ = std::make_unique<CancelableAlarm>(
"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:
@@ -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:
+1 -1
View File
@@ -53,7 +53,7 @@ void FakeNearbyConnectionsManager::Shutdown() {
void FakeNearbyConnectionsManager::StartAdvertising(
std::vector<uint8_t> 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;
{
+1 -1
View File
@@ -50,7 +50,7 @@ class FakeNearbyConnectionsManager : public NearbyConnectionsManager {
void StartAdvertising(std::vector<uint8_t> 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,
+1
View File
@@ -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.
+2 -1
View File
@@ -176,7 +176,7 @@ void NearbyConnectionsManagerImpl::Shutdown() { Reset(); }
void NearbyConnectionsManagerImpl::StartAdvertising(
std::vector<uint8_t> 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));
+1 -1
View File
@@ -61,7 +61,7 @@ class NearbyConnectionsManagerImpl : public NearbyConnectionsManager {
void StartAdvertising(std::vector<uint8_t> 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,
@@ -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));
@@ -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;
+2
View File
@@ -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.
+8
View File
@@ -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__
+2
View File
@@ -585,6 +585,8 @@ class NearbySharingServiceImpl
absl::Time share_foreground_send_surface_start_timestamp_;
std::unique_ptr<nearby::api::AppInfo> app_info_;
std::optional<uint16_t> 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