Remove assumption that RPC calls are synchronous.

PiperOrigin-RevId: 807256161
This commit is contained in:
Francis Tsui
2025-09-15 08:47:24 -07:00
committed by Copybara-Service
parent 5bdc1ab09c
commit 525e0e7b11
2 changed files with 24 additions and 6 deletions
@@ -406,11 +406,10 @@ bool NearbyShareCertificateManagerImpl::DownloadPublicCertificatesInExecutor() {
}
bool download_succeeded = false;
// Currently certificates download is synchronous. It completes after
// QuerySharedCredentialsFetchNextPage() returns.
absl::Notification notification;
auto context = std::make_unique<CertificateDownloadContext>(
nearby_identity_client_.get(), std::move(device_id),
[this, &download_succeeded](
[this, &download_succeeded, &notification](
absl::StatusOr<std::vector<PublicCertificate>> certificates_status) {
if (!certificates_status.ok()) {
download_succeeded = false;
@@ -426,8 +425,13 @@ bool NearbyShareCertificateManagerImpl::DownloadPublicCertificatesInExecutor() {
}
download_succeeded = UpdatePublicCertificates(certificates);
}
notification.Notify();
});
context->QuerySharedCredentialsFetchNextPage();
// Wait for all pages of certificates to be downloaded.
// MUST not terminate early, otherwise notification will go out of scope, and
// the callback will call Notify on a destroyed object.
notification.WaitForNotification();
LOG(INFO) << "Public certificates downloadws, success: "
<< download_succeeded;
return download_succeeded;
@@ -551,6 +555,8 @@ bool NearbyShareCertificateManagerImpl::UploadDeviceCertificatesInExecutor(
}
notification.Notify();
});
// MUST not terminate early, otherwise notification will go out of scope, and
// the callback will call Notify on a destroyed object.
notification.WaitForNotification();
// check whether the manager is still running
if (!is_running()) {
@@ -25,6 +25,7 @@
#include "absl/memory/memory.h"
#include "absl/status/statusor.h"
#include "absl/synchronization/notification.h"
#include "internal/platform/implementation/account_manager.h"
#include "sharing/contacts/nearby_share_contact_manager.h"
#include "sharing/internal/api/sharing_rpc_client.h"
@@ -135,11 +136,22 @@ void NearbyShareContactManagerImpl::GetContacts(ContactsCallback callback) {
return;
}
// Currently Contacts download is synchronous. It completes after
// FetchNextPage() returns.
absl::Notification notification;
auto context = std::make_unique<ContactDownloadContext>(
nearby_share_client_.get(), std::move(callback));
nearby_share_client_.get(),
[&notification, callback = std::move(callback)](
absl::StatusOr<std::vector<nearby::sharing::proto::ContactRecord>>
contacts,
uint32_t num_unreachable_contacts_filtered_out) mutable {
std::move(callback)(std::move(contacts),
num_unreachable_contacts_filtered_out);
notification.Notify();
});
context->FetchNextPage();
// Wait for all pages of contacts to be downloaded.
// MUST not terminate early, otherwise notification will go out of scope,
// and the callback will call Notify on a destroyed object.
notification.WaitForNotification();
});
}