Do not start CertificateManager scheduled tasks if user not logged in.

PiperOrigin-RevId: 808831574
This commit is contained in:
Francis Tsui
2025-09-18 19:17:07 -07:00
committed by Copybara-Service
parent e3e3822610
commit 358557342c
8 changed files with 28 additions and 30 deletions
@@ -98,10 +98,6 @@ void FakeNearbyShareCertificateManager::ClearPublicCertificates(
callback(true);
}
void FakeNearbyShareCertificateManager::OnStart() {}
void FakeNearbyShareCertificateManager::OnStop() {}
std::optional<NearbySharePrivateCertificate>
FakeNearbyShareCertificateManager::GetValidPrivateCertificate(
DeviceVisibility visibility) const {
@@ -123,8 +123,8 @@ class FakeNearbyShareCertificateManager : public NearbyShareCertificateManager {
private:
// NearbyShareCertificateManager:
void OnStart() override;
void OnStop() override;
void OnStartScheduledTasks() override {}
void OnStopScheduledTasks() override {}
std::optional<NearbySharePrivateCertificate> GetValidPrivateCertificate(
proto::DeviceVisibility visibility) const override;
void UpdatePrivateCertificateInStorage(
@@ -41,18 +41,18 @@ void NearbyShareCertificateManager::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
void NearbyShareCertificateManager::Start() {
void NearbyShareCertificateManager::StartScheduledTasks() {
if (is_running_) return;
is_running_ = true;
OnStart();
OnStartScheduledTasks();
}
void NearbyShareCertificateManager::Stop() {
void NearbyShareCertificateManager::StopScheduledTasks() {
if (!is_running_) return;
is_running_ = false;
OnStop();
OnStopScheduledTasks();
}
std::optional<NearbyShareEncryptedMetadataKey>
@@ -69,8 +69,8 @@ class NearbyShareCertificateManager {
void RemoveObserver(Observer* observer);
// Starts/Stops certificate task scheduling.
void Start();
void Stop();
void StartScheduledTasks();
void StopScheduledTasks();
bool is_running() { return is_running_; }
// Encrypts the metadata encryption key of the currently valid private
@@ -123,8 +123,8 @@ class NearbyShareCertificateManager {
virtual std::string Dump() const = 0;
protected:
virtual void OnStart() = 0;
virtual void OnStop() = 0;
virtual void OnStartScheduledTasks() = 0;
virtual void OnStopScheduledTasks() = 0;
// Returns the currently valid private certificate with |visibility|, or
// returns std::nullopt if one does not exist.
@@ -594,14 +594,14 @@ void NearbyShareCertificateManagerImpl::ClearPublicCertificates(
certificate_storage_->ClearPublicCertificates(std::move(callback));
}
void NearbyShareCertificateManagerImpl::OnStart() {
void NearbyShareCertificateManagerImpl::OnStartScheduledTasks() {
private_certificate_expiration_scheduler_->Start();
public_certificate_expiration_scheduler_->Start();
force_contacts_update_scheduler_->Start();
download_public_certificates_scheduler_->Start();
}
void NearbyShareCertificateManagerImpl::OnStop() {
void NearbyShareCertificateManagerImpl::OnStopScheduledTasks() {
private_certificate_expiration_scheduler_->Stop();
public_certificate_expiration_scheduler_->Stop();
force_contacts_update_scheduler_->Stop();
@@ -139,8 +139,8 @@ class NearbyShareCertificateManagerImpl
nearby::sharing::api::SharingRpcClientFactory* client_factory);
// NearbyShareCertificateManager:
void OnStart() override;
void OnStop() override;
void OnStartScheduledTasks() override;
void OnStopScheduledTasks() override;
std::optional<NearbySharePrivateCertificate> GetValidPrivateCertificate(
proto::DeviceVisibility visibility) const override;
void UpdatePrivateCertificateInStorage(
@@ -175,7 +175,7 @@ class NearbyShareCertificateManagerImplTest
PopulatePrivateCertificates();
PopulatePublicCertificates();
cert_manager_->Start();
cert_manager_->StartScheduledTasks();
}
// NearbyShareCertificateManager::Observer:
@@ -762,7 +762,7 @@ TEST_F(NearbyShareCertificateManagerImplTest,
TEST_F(NearbyShareCertificateManagerImplTest,
RefreshPrivateCertificates_OnLocalDeviceMetadataChanged) {
Initialize();
cert_manager_->Start();
cert_manager_->StartScheduledTasks();
// Destroy and recreate private certificates if any metadata fields change.
for (bool did_device_name_change : {true, false}) {
@@ -800,7 +800,7 @@ TEST_F(NearbyShareCertificateManagerImplTest,
RefreshPrivateCertificates_PublishDevice_OnVendorIdChanged) {
Initialize();
cert_store_->ReplacePrivateCertificates(private_certificates_);
cert_manager_->Start();
cert_manager_->StartScheduledTasks();
cert_manager_->SetVendorId(12345);
@@ -825,7 +825,7 @@ TEST_F(NearbyShareCertificateManagerImplTest,
SetVendorId_WhenNoPrivateCertificates) {
Initialize();
cert_store_->ReplacePrivateCertificates({});
cert_manager_->Start();
cert_manager_->StartScheduledTasks();
cert_manager_->SetVendorId(12345);
@@ -853,7 +853,7 @@ TEST_F(NearbyShareCertificateManagerImplTest,
FastForward(kNearbyShareCertificateValidityPeriod * 1.5);
cert_store_->ReplacePrivateCertificates(private_certificates_);
cert_manager_->Start();
cert_manager_->StartScheduledTasks();
InvokePrivateCertificateRefresh(/*expected_success=*/true);
VerifyPrivateCertificates(/*expected_metadata=*/GetNearbyShareTestMetadata());
@@ -866,7 +866,7 @@ TEST_F(NearbyShareCertificateManagerImplTest,
SetBluetoothAdapterIsPresent(false);
cert_manager_->Start();
cert_manager_->StartScheduledTasks();
// Bluetooth MAC address is optional, so the refresh should still succeed.
InvokePrivateCertificateRefresh(/*expected_success=*/true);
+8 -6
View File
@@ -273,7 +273,9 @@ NearbySharingServiceImpl::NearbySharingServiceImpl(
LOG(INFO) << __func__ << ": Set custom save path: " << custom_save_path;
nearby_connections_manager_->SetCustomSavePath(custom_save_path);
certificate_manager_->Start();
if (account_manager_.GetCurrentAccount().has_value()) {
certificate_manager_->StartScheduledTasks();
}
update_file_paths_in_progress_ = false;
SetupBluetoothAdapter();
@@ -313,7 +315,7 @@ void NearbySharingServiceImpl::Shutdown(
settings_->RemoveSettingsObserver(this);
certificate_manager_->Stop();
certificate_manager_->StopScheduledTasks();
is_shutting_down_ = nullptr;
std::move(status_codes_callback)(StatusCodes::kOk);
@@ -3438,7 +3440,7 @@ void NearbySharingServiceImpl::ResetAllSettings(bool logout) {
StopAdvertising();
StopScanning();
nearby_connections_manager_->Shutdown();
certificate_manager_->Stop();
certificate_manager_->StopScheduledTasks();
// Reset preferences for logout.
if (logout) {
@@ -3479,10 +3481,10 @@ void NearbySharingServiceImpl::ResetAllSettings(bool logout) {
// Set contacts visibility when logging in so the user is ready to share
// immediately. Notify observers as well.
settings_->SetVisibility(DeviceVisibility::DEVICE_VISIBILITY_ALL_CONTACTS);
}
// Start services again.
certificate_manager_->Start();
// Start services again on login.
certificate_manager_->StartScheduledTasks();
}
InvalidateSurfaceState();
}