diff --git a/sharing/certificates/nearby_share_certificate_manager_impl.cc b/sharing/certificates/nearby_share_certificate_manager_impl.cc index 42a23976..00154aad 100644 --- a/sharing/certificates/nearby_share_certificate_manager_impl.cc +++ b/sharing/certificates/nearby_share_certificate_manager_impl.cc @@ -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( nearby_identity_client_.get(), std::move(device_id), - [this, &download_succeeded]( + [this, &download_succeeded, ¬ification]( absl::StatusOr> 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()) { diff --git a/sharing/contacts/nearby_share_contact_manager_impl.cc b/sharing/contacts/nearby_share_contact_manager_impl.cc index c179f300..8ad1dc7f 100644 --- a/sharing/contacts/nearby_share_contact_manager_impl.cc +++ b/sharing/contacts/nearby_share_contact_manager_impl.cc @@ -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( - nearby_share_client_.get(), std::move(callback)); + nearby_share_client_.get(), + [¬ification, callback = std::move(callback)]( + absl::StatusOr> + 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(); }); }