Use new QuerySharedCredentials rpc when file sync is enabled.

PiperOrigin-RevId: 886435458
This commit is contained in:
Francis Tsui
2026-03-19 16:32:19 -07:00
committed by Copybara-Service
parent 9c3523cf6c
commit 593e46c858
4 changed files with 164 additions and 2 deletions
+4
View File
@@ -48,10 +48,12 @@ cc_library(
"//internal/base",
"//internal/base:file_path",
"//internal/crypto_cros",
"//internal/flags:nearby_flags",
"//internal/platform:mac_address",
"//internal/platform:types",
"//location/nearby/sharing/lib/account:account_manager",
"//location/nearby/sharing/lib/rpc:sharing_rpc_client",
"//sharing/flags/generated:generated_flags",
"//sharing/internal/api:platform",
"//sharing/internal/base",
"//sharing/internal/public:logging",
@@ -126,12 +128,14 @@ cc_test(
":test_support",
"//google/nearby/identity/v1:resources_cc_proto",
"//google/nearby/identity/v1:rpcs_cc_proto",
"//internal/flags:nearby_flags",
"//internal/platform:mac_address",
"//internal/platform/implementation:platform_impl",
"//location/nearby/sharing/lib/account:account_manager",
"//location/nearby/sharing/lib/account:fake_account_manager",
"//location/nearby/sharing/lib/rpc:fake_nearby_share_client",
"//sharing/common:enum",
"//sharing/flags/generated:generated_flags",
"//sharing/internal/api:mock_sharing_platform",
"//sharing/internal/api:platform",
"//sharing/internal/public:pref_names",
@@ -45,6 +45,7 @@
#include "absl/time/time.h"
#include "absl/types/span.h"
#include "internal/base/file_path.h"
#include "internal/flags/nearby_flags.h"
#include "internal/platform/mac_address.h"
#include "sharing/certificates/common.h"
#include "sharing/certificates/constants.h"
@@ -54,6 +55,7 @@
#include "sharing/certificates/nearby_share_decrypted_public_certificate.h"
#include "sharing/certificates/nearby_share_encrypted_metadata_key.h"
#include "sharing/certificates/nearby_share_private_certificate.h"
#include "sharing/flags/generated/nearby_sharing_feature_flags.h"
#include "sharing/internal/api/bluetooth_adapter.h"
#include "sharing/internal/api/preference_manager.h"
#include "sharing/internal/api/public_certificate_database.h"
@@ -82,6 +84,10 @@ using ::google::nearby::identity::v1::PublishDeviceRequest;
using ::google::nearby::identity::v1::PublishDeviceResponse;
using ::google::nearby::identity::v1::QuerySharedCredentialsRequest;
using ::google::nearby::identity::v1::QuerySharedCredentialsResponse;
using ::google::nearby::identity::v1::
QuerySharedCredentialsWithBindingIdsRequest;
using ::google::nearby::identity::v1::
QuerySharedCredentialsWithBindingIdsResponse;
using ::google::nearby::identity::v1::SharedCredential;
using ::nearby::sharing::api::PreferenceManager;
using ::nearby::sharing::api::PublicCertificateDatabase;
@@ -372,6 +378,55 @@ void NearbyShareCertificateManagerImpl::CertificateDownloadContext::
});
}
void NearbyShareCertificateManagerImpl::CertificateDownloadContext::
QuerySharedCredentialsWithBindingIdsFetchNextPage() {
LOG(INFO) << __func__
<< ": Downloading public certificates with binding ids page="
<< page_number_;
page_number_++;
QuerySharedCredentialsWithBindingIdsRequest request;
request.set_name(absl::StrCat("devices/", device_id_));
if (next_page_token_.has_value()) {
request.set_page_token(*next_page_token_);
}
nearby_identity_client_->QuerySharedCredentialsWithBindingIds(
std::move(request), api::IdentityRpcClient::kTimeout,
[this](const absl::StatusOr<QuerySharedCredentialsWithBindingIdsResponse>&
response) mutable {
if (!response.ok()) {
LOG(WARNING) << "Failed to download public certificates: "
<< response.status();
std::move(download_callback_)(response.status());
return;
}
for (const auto& credential : response->shared_credentials()) {
if (credential.data_type() !=
SharedCredential::DATA_TYPE_PUBLIC_CERTIFICATE) {
continue;
}
PublicCertificate certificate;
if (!certificate.ParseFromString(credential.data())) {
LOG(ERROR) << "Failed parsing to PublicCertificate, credential.id: "
<< credential.id() << " data: "
<< absl::BytesToHexString(credential.data());
continue;
}
VLOG(1) << "Successfully parsed credential: " << credential.id();
certificates_.push_back(certificate);
}
if (response->next_page_token().empty()) {
LOG(INFO) << "Completed download of " << certificates_.size()
<< " certificates";
std::move(download_callback_)(std::move(certificates_));
return;
}
next_page_token_ = response->next_page_token();
QuerySharedCredentialsWithBindingIdsFetchNextPage();
});
}
bool NearbyShareCertificateManagerImpl::UpdatePublicCertificates(
const std::vector<PublicCertificate>& certificates) {
// Save certificates to store.
@@ -437,7 +492,12 @@ bool NearbyShareCertificateManagerImpl::DownloadPublicCertificatesInExecutor() {
}
notification.Notify();
});
context->QuerySharedCredentialsFetchNextPage();
if (NearbyFlags::GetInstance().GetBoolFlag(
config_package_nearby::nearby_sharing_feature::kEnableFileSync)) {
context->QuerySharedCredentialsWithBindingIdsFetchNextPage();
} else {
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.
@@ -118,6 +118,7 @@ class NearbyShareCertificateManagerImpl
// On successful download, if page token in the response is empty, the
// |download_success_callback_| is invoked with all downloaded certificates.
void QuerySharedCredentialsFetchNextPage();
void QuerySharedCredentialsWithBindingIdsFetchNextPage();
private:
nearby::sharing::api::IdentityRpcClient* absl_nonnull const
@@ -39,6 +39,7 @@
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "absl/types/span.h"
#include "internal/flags/nearby_flags.h"
#include "internal/platform/mac_address.h"
#include "sharing/certificates/constants.h"
#include "sharing/certificates/fake_nearby_share_certificate_storage.h"
@@ -48,6 +49,7 @@
#include "sharing/certificates/nearby_share_encrypted_metadata_key.h"
#include "sharing/certificates/nearby_share_private_certificate.h"
#include "sharing/certificates/test_util.h"
#include "sharing/flags/generated/nearby_sharing_feature_flags.h"
#include "sharing/internal/api/mock_sharing_platform.h"
#include "sharing/internal/public/pref_names.h"
#include "sharing/internal/test/fake_bluetooth_adapter.h"
@@ -70,6 +72,10 @@ using ::google::nearby::identity::v1::PublishDeviceRequest;
using ::google::nearby::identity::v1::PublishDeviceResponse;
using ::google::nearby::identity::v1::QuerySharedCredentialsRequest;
using ::google::nearby::identity::v1::QuerySharedCredentialsResponse;
using ::google::nearby::identity::v1::
QuerySharedCredentialsWithBindingIdsRequest;
using ::google::nearby::identity::v1::
QuerySharedCredentialsWithBindingIdsResponse;
using ::nearby::sharing::proto::DeviceVisibility;
using ::nearby::sharing::proto::PublicCertificate;
using ::testing::Not;
@@ -100,6 +106,7 @@ class NearbyShareCertificateManagerImplTest
~NearbyShareCertificateManagerImplTest() override = default;
void SetUp() override {
NearbyFlags::GetInstance().ResetOverridedValues();
ON_CALL(mock_sharing_platform_, GetPreferenceManager)
.WillByDefault(ReturnRef(preference_manager_));
ON_CALL(mock_sharing_platform_, GetAccountManager)
@@ -304,7 +311,7 @@ class NearbyShareCertificateManagerImplTest
std::max(max_not_after_self_share, cert.not_after());
break;
default:
DCHECK(false);
FAIL() << "Unexpected visibility: " << cert.visibility();
break;
}
@@ -423,6 +430,78 @@ class NearbyShareCertificateManagerImplTest
return response;
}
void QuerySharedCredentialsWithBindingIdsFlow(
size_t num_pages, DownloadPublicCertificatesResult result) {
size_t prev_num_results = download_scheduler_->handled_results().size();
cert_store_->SetPublicCertificateIds(kPublicCertificateIds);
size_t initial_num_notifications =
num_public_certs_downloaded_notifications_;
size_t initial_num_public_cert_exp_reschedules =
public_cert_exp_scheduler_->num_reschedule_calls();
std::vector<absl::StatusOr<QuerySharedCredentialsWithBindingIdsResponse>>
responses;
std::string page_token;
for (size_t page_number = 0; page_number < num_pages; ++page_number) {
bool last_page = page_number == num_pages - 1;
if (last_page && result == DownloadPublicCertificatesResult::kHttpError) {
responses.push_back(absl::InternalError(""));
break;
}
page_token = last_page ? std::string()
: absl::StrCat(kPageTokenPrefix, page_number);
responses.push_back(BuildQuerySharedCredentialsWithBindingIdsResponse(
page_number, page_token));
}
identity_client_.SetQuerySharedCredentialsWithBindingIdsResponses(
responses);
cert_store_->SetAddPublicCertificatesResult(
result != DownloadPublicCertificatesResult::kStorageError);
download_scheduler_->InvokeRequestCallback();
Sync();
std::vector<QuerySharedCredentialsWithBindingIdsRequest> requests =
identity_client_.query_shared_credentials_with_binding_ids_requests();
EXPECT_EQ(requests.size(), num_pages);
EXPECT_EQ(requests.back().name(), absl::StrCat("devices/", kDeviceId));
ASSERT_EQ(download_scheduler_->handled_results().size(),
prev_num_results + 1);
bool success = result == DownloadPublicCertificatesResult::kSuccess;
EXPECT_EQ(download_scheduler_->handled_results().back(), success);
EXPECT_EQ(num_public_certs_downloaded_notifications_,
initial_num_notifications + (success ? 1u : 0u));
EXPECT_EQ(public_cert_exp_scheduler_->num_reschedule_calls(),
initial_num_public_cert_exp_reschedules + (success ? 1u : 0u));
}
QuerySharedCredentialsWithBindingIdsResponse
BuildQuerySharedCredentialsWithBindingIdsResponse(
size_t page_number, absl::string_view page_token) {
QuerySharedCredentialsWithBindingIdsResponse response;
int i = 0;
for (auto public_certificate : public_certificates_) {
auto* shared_credential = response.add_shared_credentials();
shared_credential->set_id(page_number * 100 + i);
if (i % 2 == 0) {
shared_credential->set_data_type(
google::nearby::identity::v1::SharedCredential::
DATA_TYPE_PUBLIC_CERTIFICATE);
} else {
shared_credential->set_data_type(
google::nearby::identity::v1::SharedCredential::
DATA_TYPE_SHARED_CREDENTIAL);
}
*shared_credential->mutable_data() =
public_certificate.SerializeAsString();
i++;
}
response.set_next_page_token(page_token);
return response;
}
void CheckStorageAddCertificates(
const FakeNearbyShareCertificateStorage::AddPublicCertificatesCall&
add_cert_call) {
@@ -700,6 +779,24 @@ TEST_F(NearbyShareCertificateManagerImplTest,
/*num_pages=*/2, DownloadPublicCertificatesResult::kHttpError));
}
TEST_F(NearbyShareCertificateManagerImplTest,
QuerySharedCredentialsWithBindingIdsSuccess) {
NearbyFlags::GetInstance().OverrideBoolFlagValue(
config_package_nearby::nearby_sharing_feature::kEnableFileSync, true);
Initialize();
ASSERT_NO_FATAL_FAILURE(QuerySharedCredentialsWithBindingIdsFlow(
/*num_pages=*/2, DownloadPublicCertificatesResult::kSuccess));
}
TEST_F(NearbyShareCertificateManagerImplTest,
QuerySharedCredentialsWithBindingIdsRPCFailure) {
NearbyFlags::GetInstance().OverrideBoolFlagValue(
config_package_nearby::nearby_sharing_feature::kEnableFileSync, true);
Initialize();
ASSERT_NO_FATAL_FAILURE(QuerySharedCredentialsWithBindingIdsFlow(
/*num_pages=*/2, DownloadPublicCertificatesResult::kHttpError));
}
TEST_F(NearbyShareCertificateManagerImplTest, ClearPublicCertificates) {
Initialize();
cert_manager_->ClearPublicCertificates([&](bool result) {});