Commit acdddaa9 authored by Michael Hansen's avatar Michael Hansen Committed by Commit Bot

[Nearby] InvalidateFastInitiationAdvertising().

Adds InvalidateFastInitiationAdvertising() to NearbySharingServiceImpl
so that we can validate and update the Fast Init advertising state
through a single idempotent flow.

Bug: 1120082
Change-Id: I4aacc66acd1ddfe3c60caf1cf1b0c1d17da54743
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2437489Reviewed-by: default avatarRyan Hansberry <hansberry@chromium.org>
Commit-Queue: Michael Hansen <hansenmichael@google.com>
Cr-Commit-Position: refs/heads/master@{#811791}
parent 59be2021
......@@ -297,6 +297,7 @@ void NearbySharingServiceImpl::Shutdown() {
incoming_share_target_info_map_.clear();
StopAdvertising();
StopFastInitiationAdvertising();
StopScanning();
nearby_connections_manager_->Shutdown();
......@@ -310,8 +311,6 @@ void NearbySharingServiceImpl::Shutdown() {
power_client_->RemoveObserver(this);
StopFastInitiationAdvertising();
if (bluetooth_adapter_)
bluetooth_adapter_->RemoveObserver(this);
......@@ -421,8 +420,6 @@ NearbySharingService::StatusCodes NearbySharingServiceImpl::RegisterSendSurface(
<< ": A SendSurface has been registered for state: "
<< SendSurfaceStateToString(state);
InvalidateSendSurfaceState();
if (state == SendSurfaceState::kForeground)
StartFastInitiationAdvertising();
return StatusCodes::kOk;
}
......@@ -475,7 +472,6 @@ NearbySharingServiceImpl::UnregisterSendSurface(
NS_LOG(VERBOSE) << __func__ << ": A SendSurface has been unregistered: "
<< SendSurfaceStateToString(state);
InvalidateSurfaceState();
StopFastInitiationAdvertising();
return StatusCodes::kOk;
}
......@@ -932,8 +928,6 @@ void NearbySharingServiceImpl::AdapterPresentChanged(
bool present) {
NS_LOG(VERBOSE) << "Bluetooth present changed: " << present;
InvalidateSurfaceState();
if (!present)
StopFastInitiationAdvertising();
}
void NearbySharingServiceImpl::AdapterPoweredChanged(
......@@ -941,8 +935,6 @@ void NearbySharingServiceImpl::AdapterPoweredChanged(
bool powered) {
NS_LOG(VERBOSE) << "Bluetooth powered changed: " << powered;
InvalidateSurfaceState();
if (!powered)
StopFastInitiationAdvertising();
}
void NearbySharingServiceImpl::SuspendImminent() {
......@@ -1039,23 +1031,6 @@ void NearbySharingServiceImpl::OnGetBluetoothAdapter(
}
void NearbySharingServiceImpl::StartFastInitiationAdvertising() {
if (!profile_) {
NS_LOG(INFO)
<< "Failed to advertise FastInitiation. Profile is shutting down.";
return;
}
if (!IsBluetoothPowered()) {
NS_LOG(INFO) << "Failed to advertise FastInitiation. Bluetooth is not "
"powered.";
return;
}
if (fast_initiation_manager_) {
NS_LOG(INFO) << "Failed to advertise FastInitiation. Already advertising.";
return;
}
fast_initiation_manager_ =
FastInitiationManager::Factory::Create(bluetooth_adapter_);
......@@ -1085,7 +1060,8 @@ void NearbySharingServiceImpl::OnStartFastInitiationAdvertisingError() {
void NearbySharingServiceImpl::StopFastInitiationAdvertising() {
if (!fast_initiation_manager_) {
NS_LOG(INFO) << "Can't stop advertising FastInitiation. Not advertising.";
NS_LOG(VERBOSE)
<< "Can't stop advertising FastInitiation. Not advertising.";
return;
}
......@@ -1236,7 +1212,7 @@ bool NearbySharingServiceImpl::ShouldStopNearbyProcess() {
void NearbySharingServiceImpl::InvalidateSendSurfaceState() {
InvalidateScanningState();
// TODO(b/161889067) InvalidateFastInitAdvertisement();
InvalidateFastInitiationAdvertising();
}
void NearbySharingServiceImpl::InvalidateScanningState() {
......@@ -1308,6 +1284,73 @@ void NearbySharingServiceImpl::InvalidateScanningState() {
StartScanning();
}
void NearbySharingServiceImpl::InvalidateFastInitiationAdvertising() {
// Nothing to do if we're shutting down the profile.
if (!profile_)
return;
if (power_client_->IsSuspended()) {
StopFastInitiationAdvertising();
NS_LOG(VERBOSE)
<< __func__
<< ": Stopping fast init advertising because the system is suspended.";
return;
}
if (!process_manager_->IsActiveProfile(profile_)) {
StopFastInitiationAdvertising();
NS_LOG(VERBOSE)
<< __func__
<< ": Stopping fast init advertising because profile was not active: "
<< profile_->GetProfileUserName();
return;
}
// Screen is off. Do no work.
if (is_screen_locked_) {
StopFastInitiationAdvertising();
NS_LOG(VERBOSE)
<< __func__
<< ": Stopping fast init advertising because the screen is locked.";
return;
}
if (!IsBluetoothPowered()) {
StopFastInitiationAdvertising();
NS_LOG(VERBOSE) << __func__
<< ": Stopping fast init advertising because both "
"bluetooth is disabled.";
return;
}
// Nearby Sharing is disabled. Don't fast init advertise.
if (!settings_.GetEnabled()) {
StopFastInitiationAdvertising();
NS_LOG(VERBOSE) << __func__
<< ": Stopping fast init advertising because Nearby "
"Sharing is disabled.";
return;
}
if (!foreground_send_transfer_callbacks_.might_have_observers()) {
StopFastInitiationAdvertising();
NS_LOG(VERBOSE) << __func__
<< ": Stopping fast init advertising because no send "
"surface is registered.";
return;
}
if (fast_initiation_manager_) {
NS_LOG(VERBOSE)
<< "Failed to advertise FastInitiation. Already advertising.";
return;
}
NS_LOG(VERBOSE) << __func__ << ": Starting fast init advertising.";
StartFastInitiationAdvertising();
}
void NearbySharingServiceImpl::InvalidateReceiveSurfaceState() {
InvalidateAdvertisingState();
// TODO(b/161889067) InvalidateFastInitScan();
......
......@@ -178,6 +178,7 @@ class NearbySharingServiceImpl
bool ShouldStopNearbyProcess();
void InvalidateSendSurfaceState();
void InvalidateScanningState();
void InvalidateFastInitiationAdvertising();
void InvalidateReceiveSurfaceState();
void InvalidateAdvertisingState();
void StopAdvertising();
......
......@@ -1082,8 +1082,7 @@ TEST_F(NearbySharingServiceImplTest,
NearbySharingService::StatusCodes::kOk,
service_->RegisterSendSurface(&transfer_callback, &discovery_callback,
SendSurfaceState::kForeground));
adapter_observer_->AdapterPresentChanged(mock_bluetooth_adapter_.get(),
false);
SetBluetoothIsPresent(false);
EXPECT_TRUE(fast_initiation_manager_factory_
->StopAdvertisingCalledAndManagerDestroyed());
}
......@@ -1097,8 +1096,7 @@ TEST_F(NearbySharingServiceImplTest,
NearbySharingService::StatusCodes::kOk,
service_->RegisterSendSurface(&transfer_callback, &discovery_callback,
SendSurfaceState::kForeground));
adapter_observer_->AdapterPoweredChanged(mock_bluetooth_adapter_.get(),
false);
SetBluetoothIsPowered(false);
EXPECT_TRUE(fast_initiation_manager_factory_
->StopAdvertisingCalledAndManagerDestroyed());
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment