Simplify callback interface on CertificateDownloadContext.

PiperOrigin-RevId: 804519883
This commit is contained in:
Francis Tsui
2025-09-08 11:36:56 -07:00
committed by Copybara-Service
parent 579a01e26f
commit 1b99ec036b
3 changed files with 29 additions and 26 deletions
+1 -1
View File
@@ -122,7 +122,7 @@ cc_test(
":test_support",
"//internal/platform:mac_address",
"//internal/platform/implementation:account_manager",
"//internal/platform/implementation/g3", # fixdeps: keep
"//internal/platform/implementation:platform_impl", # fixdeps: keep
"//internal/test",
"//proto/identity/v1:resources_cc_proto",
"//proto/identity/v1:rpcs_cc_proto",
@@ -309,9 +309,9 @@ std::string NearbyShareCertificateManagerImpl::GetId() {
void NearbyShareCertificateManagerImpl::CertificateDownloadContext::
QuerySharedCredentialsFetchNextPage() {
page_number_++;
LOG(INFO) << __func__
<< ": Downloading public certificates page=" << page_number_;
page_number_++;
QuerySharedCredentialsRequest request;
request.set_name(absl::StrCat("devices/", device_id_));
if (next_page_token_.has_value()) {
@@ -324,7 +324,7 @@ void NearbyShareCertificateManagerImpl::CertificateDownloadContext::
LOG(WARNING) << __func__
<< ": Failed to download public certificates: "
<< response.status();
std::move(download_failure_callback_)();
std::move(download_callback_)(response.status());
return;
}
for (const auto& credential : response->shared_credentials()) {
@@ -353,7 +353,7 @@ void NearbyShareCertificateManagerImpl::CertificateDownloadContext::
if (response->next_page_token().empty()) {
LOG(INFO) << __func__ << ": Completed download of "
<< certificates_.size() << " certificates";
std::move(download_success_callback_)(certificates_);
std::move(download_callback_)(std::move(certificates_));
return;
}
next_page_token_ = response->next_page_token();
@@ -409,16 +409,22 @@ bool NearbyShareCertificateManagerImpl::DownloadPublicCertificatesInExecutor() {
// QuerySharedCredentialsFetchNextPage() returns.
auto context = std::make_unique<CertificateDownloadContext>(
nearby_identity_client_.get(), std::move(device_id),
[&download_succeeded]() { download_succeeded = false; },
[this, &download_succeeded](
const std::vector<PublicCertificate>& certificates) {
if (VLOG_IS_ON(1)) {
for (const auto& certificate : certificates) {
VLOG(1) << "Downloaded certificate id: "
<< absl::BytesToHexString(certificate.secret_id());
absl::StatusOr<std::vector<PublicCertificate>> certificates_status) {
if (!certificates_status.ok()) {
download_succeeded = false;
LOG(WARNING) << "Failed to download public certificates: "
<< certificates_status.status();
} else {
auto certificates = certificates_status.value();
if (VLOG_IS_ON(1)) {
for (const auto& certificate : certificates) {
VLOG(1) << "Downloaded certificate id: "
<< absl::BytesToHexString(certificate.secret_id());
}
}
download_succeeded = UpdatePublicCertificates(certificates);
}
download_succeeded = UpdatePublicCertificates(certificates);
});
context->QuerySharedCredentialsFetchNextPage();
LOG(INFO) << "Public certificates downloadws, success: "
@@ -24,6 +24,7 @@
#include <vector>
#include "absl/functional/any_invocable.h"
#include "absl/status/statusor.h"
#include "absl/time/time.h"
#include "internal/base/file_path.h"
#include "internal/platform/implementation/account_manager.h"
@@ -101,15 +102,13 @@ class NearbyShareCertificateManagerImpl
CertificateDownloadContext(
nearby::sharing::api::IdentityRpcClient* nearby_identity_client,
std::string device_id,
absl::AnyInvocable<void() &&> download_failure_callback,
absl::AnyInvocable<
void(const std::vector<nearby::sharing::proto::PublicCertificate>&
certificates) &&>
download_success_callback)
absl::AnyInvocable<void(absl::StatusOr<std::vector<
nearby::sharing::proto::PublicCertificate>>
certificates_status) &&>
download_callback)
: nearby_identity_client_(nearby_identity_client),
device_id_(std::move(device_id)),
download_failure_callback_(std::move(download_failure_callback)),
download_success_callback_(std::move(download_success_callback)) {}
download_callback_(std::move(download_callback)) {}
// Fetches the next page of certificates by calling Identity API
// QuerySharedCredentials.
@@ -124,11 +123,10 @@ class NearbyShareCertificateManagerImpl
std::optional<std::string> next_page_token_;
int page_number_ = 1;
std::vector<nearby::sharing::proto::PublicCertificate> certificates_;
absl::AnyInvocable<void() &&> download_failure_callback_;
absl::AnyInvocable<
void(const std::vector<nearby::sharing::proto::PublicCertificate>&
certificates) &&>
download_success_callback_;
absl::AnyInvocable<void(
absl::StatusOr<std::vector<nearby::sharing::proto::PublicCertificate>>
certificates_status) &&>
download_callback_;
};
NearbyShareCertificateManagerImpl(
@@ -138,7 +136,7 @@ class NearbyShareCertificateManagerImpl
std::unique_ptr<nearby::sharing::api::PublicCertificateDatabase>
public_certificate_database,
NearbyShareLocalDeviceDataManager* local_device_data_manager,
nearby::sharing::api::SharingRpcClientFactory* client_factory);
nearby::sharing::api::SharingRpcClientFactory* client_factory);
// NearbyShareCertificateManager:
void OnStart() override;
@@ -190,13 +188,12 @@ class NearbyShareCertificateManagerImpl
// Returns the device id use to identify the local device in BE.
std::string GetId();
Context* const context_;
AccountManager& account_manager_;
NearbyShareLocalDeviceDataManager* const local_device_data_manager_;
nearby::sharing::api::PreferenceManager& preference_manager_;
int32_t vendor_id_ = 0; // Defaults to GOOGLE.
std::unique_ptr< nearby::sharing::api::SharingRpcClient> nearby_client_;
std::unique_ptr<nearby::sharing::api::SharingRpcClient> nearby_client_;
std::unique_ptr<nearby::sharing::api::IdentityRpcClient>
nearby_identity_client_;