Do not upload to backend if not logged in.

PiperOrigin-RevId: 700057202
This commit is contained in:
Francis Tsui
2024-11-25 12:00:53 -08:00
committed by Copybara-Service
parent 0803db861f
commit b46cbc62b4
2 changed files with 79 additions and 112 deletions
@@ -117,9 +117,8 @@ std::optional<EncryptedMetadata> BuildMetadata(
Context* context) {
EncryptedMetadata metadata;
if (device_name.empty()) {
NL_LOG(WARNING) << __func__
<< ": Failed to create private certificate metadata; "
<< "missing device name.";
LOG(WARNING) << "Failed to create private certificate metadata; "
"missing device name.";
return std::nullopt;
}
@@ -147,8 +146,7 @@ void TryDecryptPublicCertificates(
NearbyShareCertificateManager::CertDecryptedCallback callback, bool success,
std::unique_ptr<std::vector<PublicCertificate>> public_certificates) {
if (!success || !public_certificates) {
NL_LOG(ERROR) << __func__
<< ": Failed to read public certificates from storage.";
LOG(ERROR) << "Failed to read public certificates from storage.";
std::move(callback)(std::nullopt);
return;
}
@@ -158,15 +156,13 @@ void TryDecryptPublicCertificates(
NearbyShareDecryptedPublicCertificate::DecryptPublicCertificate(
cert, encrypted_metadata_key);
if (decrypted) {
NL_VLOG(1) << __func__
<< ": Successfully decrypted public certificate with ID "
<< nearby::utils::HexEncode(decrypted->id());
VLOG(1) << "Successfully decrypted public certificate with ID "
<< nearby::utils::HexEncode(decrypted->id());
std::move(callback)(std::move(decrypted));
return;
}
}
NL_VLOG(1) << __func__
<< ": Metadata key could not decrypt any public certificates.";
VLOG(1) << "Metadata key could not decrypt any public certificates.";
std::move(callback)(std::nullopt);
}
@@ -197,7 +193,7 @@ NearbyShareCertificateManagerImpl::Factory::Create(
NearbyShareLocalDeviceDataManager* local_device_data_manager,
NearbyShareContactManager* contact_manager, absl::string_view profile_path,
nearby::sharing::api::SharingRpcClientFactory* client_factory) {
NL_DCHECK(context);
DCHECK(context);
if (test_factory_) {
return test_factory_->CreateInstance(context, local_device_data_manager,
@@ -244,7 +240,7 @@ NearbyShareCertificateManagerImpl::NearbyShareCertificateManagerImpl(
/*require_connectivity=*/false,
prefs::kNearbySharingSchedulerPrivateCertificateExpirationName,
[&] {
NL_LOG(INFO)
LOG(INFO)
<< ": Private certificate expiration scheduler is called.";
OnPrivateCertificateExpiration();
})),
@@ -256,7 +252,7 @@ NearbyShareCertificateManagerImpl::NearbyShareCertificateManagerImpl(
/*require_connectivity=*/false,
prefs::kNearbySharingSchedulerPublicCertificateExpirationName,
[&] {
NL_LOG(INFO)
LOG(INFO)
<< ": Public certificate expiration scheduler is called.";
OnPublicCertificateExpiration();
})),
@@ -267,8 +263,8 @@ NearbyShareCertificateManagerImpl::NearbyShareCertificateManagerImpl(
/*require_connectivity=*/true,
prefs::kNearbySharingSchedulerUploadLocalDeviceCertificatesName,
[&] {
NL_LOG(INFO) << ": Upload local device certificates scheduler "
"is called.";
LOG(INFO) << ": Upload local device certificates scheduler "
"is called.";
UploadLocalDeviceCertificates();
})),
download_public_certificates_scheduler_(
@@ -279,7 +275,7 @@ NearbyShareCertificateManagerImpl::NearbyShareCertificateManagerImpl(
/*require_connectivity=*/true,
prefs::kNearbySharingSchedulerDownloadPublicCertificatesName,
[&] {
NL_LOG(INFO)
LOG(INFO)
<< ": Download public certificates scheduler is called.";
DownloadPublicCertificates();
})),
@@ -295,7 +291,7 @@ NearbyShareCertificateManagerImpl::~NearbyShareCertificateManagerImpl() {
void NearbyShareCertificateManagerImpl::CertificateDownloadContext::
FetchNextPage() {
NL_LOG(INFO) << __func__ << ": Downloading page=" << page_number_++;
LOG(INFO) << "Downloading certificate page=" << page_number_++;
ListPublicCertificatesRequest request;
request.set_parent(device_id_);
if (next_page_token_.has_value()) {
@@ -305,20 +301,19 @@ void NearbyShareCertificateManagerImpl::CertificateDownloadContext::
request, [this](const absl::StatusOr<ListPublicCertificatesResponse>&
response) mutable {
if (!response.ok()) {
NL_LOG(WARNING) << __func__ << ": Failed to download certificates: "
<< response.status();
LOG(WARNING) << "Failed to download certificates: "
<< response.status();
std::move(download_failure_callback_)();
return;
}
certificates_.insert(certificates_.end(),
response->public_certificates().begin(),
response->public_certificates().end());
response->public_certificates().begin(),
response->public_certificates().end());
if (response->next_page_token().empty()) {
NL_LOG(INFO) << __func__ << ": Completed to download "
<< certificates_.size()
<< " certificates from backend";
LOG(INFO) << "Finished downloading " << certificates_.size()
<< " certificates from backend";
std::move(download_success_callback_)(certificates_);
return;
}
@@ -392,15 +387,14 @@ void NearbyShareCertificateManagerImpl::OnPublicCertificatesDownloadSuccess(
absl::Notification notification;
bool is_added_to_store = false;
certificate_storage_->AddPublicCertificates(
absl::MakeSpan(certificates.data(),
certificates.size()),
absl::MakeSpan(certificates.data(), certificates.size()),
[&](bool success) {
is_added_to_store = success;
notification.Notify();
});
notification.WaitForNotification();
if (!is_added_to_store) {
NL_LOG(ERROR) << __func__ << ": Failed to add certificates to store.";
LOG(ERROR) << "Failed to add certificates to store.";
OnPublicCertificatesDownloadFailure();
return;
}
@@ -419,18 +413,14 @@ void NearbyShareCertificateManagerImpl::OnPublicCertificatesDownloadFailure() {
void NearbyShareCertificateManagerImpl::DownloadPublicCertificates() {
executor_->PostTask([&]() {
NL_LOG(INFO) << __func__ << ": Start to download certificates.";
LOG(INFO) << "Start to download certificates.";
if (!is_running()) {
NL_LOG(WARNING) << __func__
<< ": Ignore to download certificates due to manager is "
"not running.";
LOG(WARNING) << "Ignore certificates download, manager is not running.";
return;
}
if (!account_manager_.GetCurrentAccount().has_value()) {
NL_LOG(WARNING)
<< __func__
<< ": Ignore to download certificates due to no login account.";
LOG(WARNING) << "Ignore certificates download, no logged in account.";
download_public_certificates_scheduler_->HandleResult(/*success=*/true);
return;
}
@@ -457,21 +447,18 @@ void NearbyShareCertificateManagerImpl::DownloadPublicCertificates() {
}
void NearbyShareCertificateManagerImpl::UploadLocalDeviceCertificates() {
executor_->PostTask([this]() {
LOG(INFO) << __func__ << ": Start to upload local device certificates.";
executor_->PostTask([&]() {
LOG(INFO) << "Start to upload local device certificates.";
if (!is_running()) {
LOG(WARNING)
<< __func__
<< ": Ignore to upload local device certificates due to manager is "
"not running.";
<< "Ignore local device certificates upload, manager is not running.";
return;
}
if (!account_manager_.GetCurrentAccount().has_value()) {
LOG(WARNING) << __func__
<< ": Ignore to upload local device certificates due to no "
"login account.";
LOG(WARNING)
<< "Ignore local device certificates upload, no logged in account.";
upload_local_device_certificates_scheduler_->HandleResult(
/*success=*/true);
return;
@@ -485,6 +472,8 @@ void NearbyShareCertificateManagerImpl::UploadLocalDeviceCertificates() {
public_certs.push_back(*private_cert.ToPublicCertificate());
}
LOG(INFO) << "Uploading " << public_certs.size()
<< " local device certificates.";
bool upload_certificates_result = false;
absl::Notification notification;
if (NearbyFlags::GetInstance().GetBoolFlag(
@@ -511,7 +500,7 @@ void NearbyShareCertificateManagerImpl::UploadLocalDeviceCertificates() {
});
}
notification.WaitForNotification();
LOG(INFO) << __func__ << ": Upload of local device certificates "
LOG(INFO) << "Upload local device certificates "
<< (upload_certificates_result ? "succeeded" : "failed.");
upload_local_device_certificates_scheduler_->HandleResult(
upload_certificates_result);
@@ -596,9 +585,8 @@ NearbyShareCertificateManagerImpl::GetValidPrivateCertificate(
}
}
NL_LOG(WARNING) << __func__
<< ": No valid private certificate found with visibility "
<< static_cast<int>(visibility);
LOG(WARNING) << "No valid private certificate found with visibility "
<< static_cast<int>(visibility);
return std::nullopt;
}
@@ -610,7 +598,7 @@ void NearbyShareCertificateManagerImpl::UpdatePrivateCertificateInStorage(
void NearbyShareCertificateManagerImpl::OnContactsDownloaded(
const std::vector<nearby::sharing::proto::ContactRecord>& contacts,
uint32_t num_unreachable_contacts_filtered_out) {
NL_LOG(INFO) << __func__ << ": Contacts downloaded.";
LOG(INFO) << "Contacts downloaded.";
}
void NearbyShareCertificateManagerImpl::OnContactsUploaded(
@@ -640,7 +628,7 @@ void NearbyShareCertificateManagerImpl::OnLocalDeviceDataChanged(
bool did_icon_change) {
executor_->PostTask([&, did_device_name_change, did_full_name_change,
did_icon_change]() {
NL_LOG(INFO) << __func__ << ": Handle to local device data changed.";
LOG(INFO) << "Handle local device data changed.";
if (!did_device_name_change && !did_full_name_change && !did_icon_change)
return;
@@ -713,29 +701,28 @@ NearbyShareCertificateManagerImpl::NextPrivateCertificateExpirationTime() {
std::optional<absl::Time> expiration_time =
certificate_storage_->NextPrivateCertificateExpirationTime();
NL_DCHECK(expiration_time);
DCHECK(expiration_time);
return *expiration_time;
}
void NearbyShareCertificateManagerImpl::OnPrivateCertificateExpiration() {
NL_VLOG(1)
<< __func__
<< ": Private certificate expiration detected; refreshing certificates.";
VLOG(1)
<< "Private certificate expiration detected; refreshing certificates.";
FinishPrivateCertificateRefresh();
}
void NearbyShareCertificateManagerImpl::FinishPrivateCertificateRefresh() {
executor_->PostTask([&]() {
NL_LOG(INFO) << __func__ << ": Refresh private certificates.";
LOG(INFO) << "Refreshed private certificates.";
absl::Time now = context_->GetClock()->Now();
certificate_storage_->RemoveExpiredPrivateCertificates(now);
std::vector<NearbySharePrivateCertificate> certs =
*certificate_storage_->GetPrivateCertificates();
if (certs.size() == NumExpectedPrivateCertificates()) {
NL_VLOG(1) << __func__ << ": All private certificates are still valid.";
VLOG(1) << "All private certificates are still valid.";
private_certificate_expiration_scheduler_->HandleResult(/*success=*/true);
return;
}
@@ -780,8 +767,7 @@ void NearbyShareCertificateManagerImpl::FinishPrivateCertificateRefresh() {
icon_url, email, vendor_id_, context_);
if (!metadata.has_value()) {
NL_LOG(WARNING)
<< __func__
LOG(WARNING)
<< "Failed to create private certificates; cannot create metadata";
private_certificate_expiration_scheduler_->HandleResult(
/*success=*/false);
@@ -790,8 +776,8 @@ void NearbyShareCertificateManagerImpl::FinishPrivateCertificateRefresh() {
// Add new certificates if necessary. Each visibility should have
// kNearbyShareNumPrivateCertificates.
NL_LOG(INFO)
<< __func__ << ": Creating "
LOG(INFO)
<< "Creating "
<< kNearbyShareNumPrivateCertificates -
num_valid_certs[DeviceVisibility::DEVICE_VISIBILITY_ALL_CONTACTS]
<< " all-contacts visibility and "
@@ -836,7 +822,7 @@ NearbyShareCertificateManagerImpl::NextPublicCertificateExpirationTime() {
void NearbyShareCertificateManagerImpl::OnPublicCertificateExpiration() {
executor_->PostTask([&]() {
NL_LOG(INFO) << __func__ << ": Removing expired public certificates.";
LOG(INFO) << "Removing expired public certificates.";
absl::Notification notification;
bool result = false;
certificate_storage_->RemoveExpiredPublicCertificates(
@@ -846,8 +832,7 @@ void NearbyShareCertificateManagerImpl::OnPublicCertificateExpiration() {
});
notification.WaitForNotification();
if (!result) {
NL_LOG(ERROR) << __func__
<< ": Failed to remove expired public certificates.";
LOG(ERROR) << "Failed to remove expired public certificates.";
}
public_certificate_expiration_scheduler_->HandleResult(result);
});
@@ -188,16 +188,15 @@ void NearbyShareContactManagerImpl::OnContactsDownloadCompleted(
OnContactsDownloadFailure();
return;
}
NL_LOG(INFO) << __func__ << ": Completed to download contacts from backend";
NL_VLOG(1) << __func__ << ": Removed "
<< num_unreachable_contacts_filtered_out
<< " unreachable contacts.";
LOG(INFO) << "Finished downloading contacts from backend";
VLOG(1) << "Removed " << num_unreachable_contacts_filtered_out
<< " unreachable contacts.";
OnContactsDownloadSuccess(std::move(*contacts),
num_unreachable_contacts_filtered_out);
}
void NearbyShareContactManagerImpl::ContactDownloadContext::FetchNextPage() {
NL_LOG(INFO) << __func__ << ": Downloading page=" << page_number_++;
LOG(INFO) << "Downloading contacts page=" << page_number_++;
ListContactPeopleRequest request;
if (next_page_token_.has_value()) {
request.set_page_token(*next_page_token_);
@@ -207,15 +206,14 @@ void NearbyShareContactManagerImpl::ContactDownloadContext::FetchNextPage() {
[this](
const absl::StatusOr<ListContactPeopleResponse>& response) mutable {
if (!response.ok()) {
NL_LOG(WARNING) << __func__ << ": Failed to download contacts: "
<< response.status();
LOG(WARNING) << "Failed to download contacts: " << response.status();
std::move(download_callback_)(
response.status(), /*num_unreachable_contacts_filtered_out=*/0);
return;
}
contacts_.insert(contacts_.end(), response->contact_records().begin(),
response->contact_records().end());
response->contact_records().end());
if (response->next_page_token().empty()) {
// We should filter here because we only care about contacts that we
@@ -240,20 +238,15 @@ void NearbyShareContactManagerImpl::DownloadContacts() {
return;
}
executor_->PostTask([this]() {
NL_LOG(INFO) << __func__ << ": Start to download contacts";
LOG(INFO) << "Started to download contacts";
if (!is_running()) {
NL_LOG(WARNING) << __func__
<< ": Ignore to download contacts due to manager is not "
"running.";
LOG(WARNING) << "Ignore contacts download, manager is not running.";
return;
}
std::vector<ContactRecord> contacts;
if (!account_manager_.GetCurrentAccount().has_value()) {
NL_LOG(WARNING)
<< __func__
<< ": Ignore to download certificates due to no login account.";
OnContactsDownloadSuccess(contacts, 0);
LOG(WARNING) << "Ignore contacts download, no logged in account.";
contact_download_and_upload_scheduler_->HandleResult(/*success=*/true);
return;
}
@@ -269,19 +262,15 @@ void NearbyShareContactManagerImpl::DownloadContacts() {
void NearbyShareContactManagerImpl::GetContacts(ContactsCallback callback) {
executor_->PostTask([this, callback = std::move(callback)]() mutable {
NL_LOG(INFO) << __func__ << ": Start to download contacts";
LOG(INFO) << "Start downloading contacts";
if (!is_running()) {
NL_LOG(WARNING) << __func__
<< ": Ignore to download contacts due to manager is not "
"running.";
LOG(WARNING) << "Ignore contacts download, manager is not running.";
return;
}
std::vector<ContactRecord> contacts;
if (!account_manager_.GetCurrentAccount().has_value()) {
NL_LOG(WARNING)
<< __func__
<< ": Ignore to download certificates due to no login account.";
LOG(WARNING) << "Ignore contacts download, no logged in account.";
std::move(callback)(contacts,
/*num_unreachable_contacts_filtered_out=*/0);
return;
@@ -318,29 +307,25 @@ void NearbyShareContactManagerImpl::OnStop() {
void NearbyShareContactManagerImpl::OnContactsDownloadSuccess(
std::vector<ContactRecord> contacts,
uint32_t num_unreachable_contacts_filtered_out) {
NL_LOG(INFO) << __func__ << ": Nearby Share download of " << contacts.size()
<< " contacts succeeded.";
LOG(INFO) << "Download of " << contacts.size() << " contacts succeeded.";
std::optional<AccountManager::Account> account =
account_manager_.GetCurrentAccount();
if (!account.has_value()) {
// Skip uploading contacts if user is not logged in.
// This also skips uploading private certificates.
LOG(WARNING)
<< "Cannot upload contacts, user logged out since download started.";
contact_download_and_upload_scheduler_->HandleResult(/*success=*/true);
return;
}
// Notify observers that the contact list was downloaded.
NotifyAllObserversContactsDownloaded(contacts,
num_unreachable_contacts_filtered_out);
std::vector<Contact> contacts_to_upload =
ContactRecordsToContacts(contacts);
// Enable cross-device self-share by adding your account to the list of
// contacts. It is also marked as a selected contact.
std::optional<AccountManager::Account> account =
account_manager_.GetCurrentAccount();
if (!account.has_value()) {
NL_LOG(WARNING) << __func__
<< ": Profile user name is not valid; could not "
<< "add self to list of contacts to upload.";
} else {
contacts_to_upload.push_back(CreateLocalContact(account->email));
}
std::vector<Contact> contacts_to_upload = ContactRecordsToContacts(contacts);
// Enable self-share by adding your account to the list of contacts.
contacts_to_upload.push_back(CreateLocalContact(account->email));
std::string last_contact_upload_hash = preference_manager_.GetString(
prefs::kNearbySharingContactUploadHashName, "");
@@ -356,9 +341,8 @@ void NearbyShareContactManagerImpl::OnContactsDownloadSuccess(
// Request a contacts upload if the contact list or allowlist has changed
// since the last successful upload or max upload interval has passed.
if (did_contacts_change_since_last_upload) {
NL_LOG(INFO) << __func__ << ": Contact list changed since last "
<< "successful upload at "
<< absl::FromUnixSeconds(last_contact_upload_time);
LOG(INFO) << "Contact list changed since last successful upload at "
<< absl::FromUnixSeconds(last_contact_upload_time);
absl::Notification notification;
bool upload_success = false;
local_device_data_manager_->UploadContacts(std::move(contacts_to_upload),
@@ -368,8 +352,7 @@ void NearbyShareContactManagerImpl::OnContactsDownloadSuccess(
});
notification.WaitForNotification();
NL_LOG(INFO) << __func__ << ": Completed to upload contacts with result:"
<< upload_success;
LOG(INFO) << "Finished contacts upload, result: " << upload_success;
OnContactsUploadFinished(did_contacts_change_since_last_upload,
contact_upload_hash, now, upload_success);
@@ -381,7 +364,7 @@ void NearbyShareContactManagerImpl::OnContactsDownloadSuccess(
}
void NearbyShareContactManagerImpl::OnContactsDownloadFailure() {
NL_LOG(WARNING) << __func__ << ": Nearby Share contacts download failed.";
LOG(WARNING) << "Contacts download failed.";
contact_download_and_upload_scheduler_->HandleResult(/*success=*/false);
}
@@ -390,9 +373,8 @@ void NearbyShareContactManagerImpl::OnContactsUploadFinished(
bool did_contacts_change_since_last_upload,
absl::string_view contact_upload_hash, absl::Time upload_time,
bool success) {
NL_LOG(INFO) << __func__ << ": Upload of contacts to Nearby Share server "
<< (success ? "succeeded." : "failed.")
<< " Contact upload hash: " << contact_upload_hash;
LOG(INFO) << "Upload of contacts " << (success ? "succeeded." : "failed.")
<< " Contact upload hash: " << contact_upload_hash;
if (success) {
std::string last_contact_upload_hash = preference_manager_.GetString(
prefs::kNearbySharingContactUploadHashName, "");