Skip to content

Commit 0803db8

Browse files
suetfeicopybara-github
authored andcommitted
migrate QueryShareCredentials.
PiperOrigin-RevId: 699329477
1 parent 526288e commit 0803db8

File tree

6 files changed

+206
-13
lines changed

6 files changed

+206
-13
lines changed

sharing/certificates/BUILD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,15 @@ cc_library(
4444
"//internal/flags:nearby_flags",
4545
"//internal/platform:types",
4646
"//internal/platform/implementation:account_manager",
47+
"//proto/identity/v1:resources_cc_proto",
48+
"//proto/identity/v1:rpcs_cc_proto",
4749
"//sharing/common",
4850
"//sharing/common:enum",
4951
"//sharing/contacts",
5052
"//sharing/flags/generated:generated_flags",
5153
"//sharing/internal/api:platform",
5254
"//sharing/internal/base",
55+
"//sharing/internal/impl/common:nearby_identity_grpc_client",
5356
"//sharing/internal/public:logging",
5457
"//sharing/internal/public:types",
5558
"//sharing/local_device_data",
@@ -119,6 +122,8 @@ cc_test(
119122
"//internal/platform/implementation:account_manager",
120123
"//internal/platform/implementation/g3", # fixdeps: keep
121124
"//internal/test",
125+
"//proto/identity/v1:resources_cc_proto",
126+
"//proto/identity/v1:rpcs_cc_proto",
122127
"//sharing/common",
123128
"//sharing/common:enum",
124129
"//sharing/contacts:test_support",

sharing/certificates/nearby_share_certificate_manager_impl.cc

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@
3232
#include "absl/functional/bind_front.h"
3333
#include "absl/memory/memory.h"
3434
#include "absl/status/statusor.h"
35+
#include "absl/strings/escaping.h"
3536
#include "absl/strings/str_cat.h"
3637
#include "absl/strings/string_view.h"
38+
#include "absl/strings/strip.h"
3739
#include "absl/synchronization/notification.h"
3840
#include "absl/time/time.h"
3941
#include "absl/types/span.h"
4042
#include "internal/flags/nearby_flags.h"
4143
#include "internal/platform/implementation/account_manager.h"
44+
#include "proto/identity/v1/resources.pb.h"
45+
#include "proto/identity/v1/rpcs.pb.h"
4246
#include "sharing/certificates/common.h"
4347
#include "sharing/certificates/constants.h"
4448
#include "sharing/certificates/nearby_share_certificate_manager.h"
@@ -71,6 +75,8 @@ namespace nearby {
7175
namespace sharing {
7276
namespace {
7377

78+
using ::google::nearby::identity::v1::QuerySharedCredentialsRequest;
79+
using ::google::nearby::identity::v1::QuerySharedCredentialsResponse;
7480
using ::nearby::sharing::api::PreferenceManager;
7581
using ::nearby::sharing::api::PublicCertificateDatabase;
7682
using ::nearby::sharing::api::SharingPlatform;
@@ -227,6 +233,7 @@ NearbyShareCertificateManagerImpl::NearbyShareCertificateManagerImpl(
227233
local_device_data_manager_(local_device_data_manager),
228234
contact_manager_(contact_manager),
229235
nearby_client_(client_factory->CreateInstance()),
236+
nearby_identity_client_(client_factory->CreateIdentityInstance()),
230237
certificate_storage_(NearbyShareCertificateStorageImpl::Factory::Create(
231238
preference_manager, std::move(public_certificate_database))),
232239
private_certificate_expiration_scheduler_(
@@ -295,9 +302,8 @@ void NearbyShareCertificateManagerImpl::CertificateDownloadContext::
295302
request.set_page_token(*next_page_token_);
296303
}
297304
nearby_share_client_->ListPublicCertificates(
298-
request, [this](
299-
const absl::StatusOr<ListPublicCertificatesResponse>&
300-
response) mutable {
305+
request, [this](const absl::StatusOr<ListPublicCertificatesResponse>&
306+
response) mutable {
301307
if (!response.ok()) {
302308
NL_LOG(WARNING) << __func__ << ": Failed to download certificates: "
303309
<< response.status();
@@ -321,6 +327,65 @@ void NearbyShareCertificateManagerImpl::CertificateDownloadContext::
321327
});
322328
}
323329

330+
void NearbyShareCertificateManagerImpl::CertificateDownloadContext::
331+
QuerySharedCredentialsFetchNextPage() {
332+
page_number_++;
333+
LOG(INFO) << __func__
334+
<< ": [Call Identity API] Downloading page=" << page_number_;
335+
QuerySharedCredentialsRequest request;
336+
request.set_name(
337+
absl::StrCat("devices/", absl::StripPrefix(device_id_, kDeviceIdPrefix)));
338+
if (next_page_token_.has_value()) {
339+
request.set_page_token(*next_page_token_);
340+
}
341+
nearby_identity_client_->QuerySharedCredentials(
342+
request, [this](const absl::StatusOr<QuerySharedCredentialsResponse>&
343+
response) mutable {
344+
if (!response.ok()) {
345+
LOG(WARNING)
346+
<< __func__
347+
<< ": [Call Identity API] Failed to download certificates: "
348+
<< response.status();
349+
std::move(download_failure_callback_)();
350+
return;
351+
}
352+
for (const auto& credential : response->shared_credentials()) {
353+
if (credential.data_type() !=
354+
google::nearby::identity::v1::SharedCredential::
355+
DATA_TYPE_PUBLIC_CERTIFICATE) {
356+
LOG(WARNING) << __func__
357+
<< ": [Call Identity API] skipping non "
358+
"DATA_TYPE_PUBLIC_CERTIFICATE, credential.id: "
359+
<< credential.id();
360+
continue;
361+
}
362+
PublicCertificate certificate;
363+
if (!certificate.ParseFromString(credential.data())) {
364+
LOG(ERROR) << __func__
365+
<< ": [Call Identity API] Failed parsing to "
366+
"PublicCertificate, credential.id: "
367+
<< credential.id() << " data: "
368+
<< absl::BytesToHexString(credential.data());
369+
continue;
370+
}
371+
VLOG(1) << __func__
372+
<< ": [Call Identity API] Successfully parsed credential: "
373+
<< credential.id();
374+
certificates_.push_back(certificate);
375+
}
376+
377+
if (response->next_page_token().empty()) {
378+
LOG(INFO) << __func__
379+
<< ": [Call Identity API] Completed to download "
380+
<< certificates_.size() << " certificates";
381+
std::move(download_success_callback_)(certificates_);
382+
return;
383+
}
384+
next_page_token_ = response->next_page_token();
385+
QuerySharedCredentialsFetchNextPage();
386+
});
387+
}
388+
324389
void NearbyShareCertificateManagerImpl::OnPublicCertificatesDownloadSuccess(
325390
const std::vector<PublicCertificate>& certificates) {
326391
// Save certificates to store.
@@ -373,15 +438,21 @@ void NearbyShareCertificateManagerImpl::DownloadPublicCertificates() {
373438
// Currently certificates download is synchronous. It completes after
374439
// FetchNextPage() returns.
375440
auto context = std::make_unique<CertificateDownloadContext>(
376-
nearby_client_.get(),
441+
nearby_client_.get(), nearby_identity_client_.get(),
377442
kDeviceIdPrefix + local_device_data_manager_->GetId(),
378443
absl::bind_front(&NearbyShareCertificateManagerImpl::
379444
OnPublicCertificatesDownloadFailure,
380445
this),
381446
absl::bind_front(&NearbyShareCertificateManagerImpl::
382447
OnPublicCertificatesDownloadSuccess,
383448
this));
384-
context->FetchNextPage();
449+
if (NearbyFlags::GetInstance().GetBoolFlag(
450+
config_package_nearby::nearby_sharing_feature::
451+
kCallNearbyIdentityApi)) {
452+
context->QuerySharedCredentialsFetchNextPage();
453+
} else {
454+
context->FetchNextPage();
455+
}
385456
});
386457
}
387458

sharing/certificates/nearby_share_certificate_manager_impl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "sharing/internal/api/public_certificate_database.h"
3939
#include "sharing/internal/api/sharing_platform.h"
4040
#include "sharing/internal/api/sharing_rpc_client.h"
41+
#include "sharing/internal/impl/common/nearby_identity_grpc_client.h"
4142
#include "sharing/internal/public/context.h"
4243
#include "sharing/local_device_data/nearby_share_local_device_data_manager.h"
4344
#include "sharing/proto/enums.pb.h"
@@ -98,13 +99,15 @@ class NearbyShareCertificateManagerImpl
9899
public:
99100
CertificateDownloadContext(
100101
nearby::sharing::api::SharingRpcClient* nearby_share_client,
102+
nearby::sharing::api::IdentityRpcClient* nearby_identity_client,
101103
std::string device_id,
102104
absl::AnyInvocable<void() &&> download_failure_callback,
103105
absl::AnyInvocable<
104106
void(const std::vector<nearby::sharing::proto::PublicCertificate>&
105107
certificates) &&>
106108
download_success_callback)
107109
: nearby_share_client_(nearby_share_client),
110+
nearby_identity_client_(nearby_identity_client),
108111
device_id_(std::move(device_id)),
109112
download_failure_callback_(std::move(download_failure_callback)),
110113
download_success_callback_(std::move(download_success_callback)) {}
@@ -115,8 +118,13 @@ class NearbyShareCertificateManagerImpl
115118
// |download_success_callback_| is invoked with all downloaded certificates.
116119
void FetchNextPage();
117120

121+
// Fetches the next page of certificates by calling Identity API
122+
// QuerySharedCredentials.
123+
void QuerySharedCredentialsFetchNextPage();
124+
118125
private:
119126
nearby::sharing::api::SharingRpcClient* const nearby_share_client_;
127+
nearby::sharing::api::IdentityRpcClient* const nearby_identity_client_;
120128
std::string device_id_;
121129
std::optional<std::string> next_page_token_;
122130
int page_number_ = 1;
@@ -202,6 +210,8 @@ class NearbyShareCertificateManagerImpl
202210
NearbyShareContactManager* const contact_manager_;
203211
int32_t vendor_id_ = 0; // Defaults to GOOGLE.
204212
std::unique_ptr< nearby::sharing::api::SharingRpcClient> nearby_client_;
213+
std::unique_ptr<nearby::sharing::api::IdentityRpcClient>
214+
nearby_identity_client_;
205215

206216
std::shared_ptr<NearbyShareCertificateStorage> certificate_storage_;
207217
std::unique_ptr<NearbyShareScheduler>

sharing/certificates/nearby_share_certificate_manager_impl_test.cc

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
#include "internal/flags/nearby_flags.h"
3939
#include "internal/platform/implementation/account_manager.h"
4040
#include "internal/test/fake_account_manager.h"
41+
#include "proto/identity/v1/resources.pb.h"
42+
#include "proto/identity/v1/rpcs.pb.h"
4143
#include "sharing/certificates/constants.h"
4244
#include "sharing/certificates/fake_nearby_share_certificate_storage.h"
4345
#include "sharing/certificates/nearby_share_certificate_manager.h"
@@ -67,6 +69,8 @@
6769
namespace nearby {
6870
namespace sharing {
6971
namespace {
72+
using ::google::nearby::identity::v1::QuerySharedCredentialsRequest;
73+
using ::google::nearby::identity::v1::QuerySharedCredentialsResponse;
7074
using ::nearby::sharing::proto::DeviceVisibility;
7175
using ::nearby::sharing::proto::PublicCertificate;
7276
using ::testing::ReturnRef;
@@ -395,6 +399,55 @@ class NearbyShareCertificateManagerImplTest
395399
initial_num_public_cert_exp_reschedules + (success ? 1u : 0u));
396400
}
397401

402+
void QuerySharedCredentialsFlow(size_t num_pages,
403+
DownloadPublicCertificatesResult result) {
404+
size_t prev_num_results = download_scheduler_->handled_results().size();
405+
cert_store_->SetPublicCertificateIds(kPublicCertificateIds);
406+
407+
size_t initial_num_notifications =
408+
num_public_certs_downloaded_notifications_;
409+
size_t initial_num_public_cert_exp_reschedules =
410+
public_cert_exp_scheduler_->num_reschedule_calls();
411+
412+
std::vector<absl::StatusOr<QuerySharedCredentialsResponse>> responses;
413+
std::string page_token;
414+
for (size_t page_number = 0; page_number < num_pages; ++page_number) {
415+
bool last_page = page_number == num_pages - 1;
416+
if (last_page && result == DownloadPublicCertificatesResult::kHttpError) {
417+
responses.push_back(absl::InternalError(""));
418+
break;
419+
}
420+
page_token = last_page ? std::string()
421+
: absl::StrCat(kPageTokenPrefix, page_number);
422+
responses.push_back(
423+
BuildQuerySharedCredentialsResponse(page_number, page_token));
424+
}
425+
426+
client_factory_.identity_instances()
427+
.back()
428+
->SetQuerySharedCredentialsResponses(responses);
429+
cert_store_->SetAddPublicCertificatesResult(
430+
result != DownloadPublicCertificatesResult::kStorageError);
431+
download_scheduler_->InvokeRequestCallback();
432+
Sync();
433+
434+
std::vector<QuerySharedCredentialsRequest> requests =
435+
client_factory_.identity_instances()
436+
.back()
437+
->query_shared_credentials_requests();
438+
EXPECT_EQ(requests.size(), num_pages);
439+
EXPECT_EQ(requests.back().name(), absl::StrCat("devices/", kDeviceId));
440+
ASSERT_EQ(download_scheduler_->handled_results().size(),
441+
prev_num_results + 1);
442+
443+
bool success = result == DownloadPublicCertificatesResult::kSuccess;
444+
EXPECT_EQ(download_scheduler_->handled_results().back(), success);
445+
EXPECT_EQ(num_public_certs_downloaded_notifications_,
446+
initial_num_notifications + (success ? 1u : 0u));
447+
EXPECT_EQ(public_cert_exp_scheduler_->num_reschedule_calls(),
448+
initial_num_public_cert_exp_reschedules + (success ? 1u : 0u));
449+
}
450+
398451
void CheckRpcRequest(int num_pages) {
399452
std::vector<proto::ListPublicCertificatesRequest> requests =
400453
client_factory_.instances().back()->list_public_certificates_requests();
@@ -414,6 +467,30 @@ class NearbyShareCertificateManagerImplTest
414467
return response;
415468
}
416469

470+
QuerySharedCredentialsResponse BuildQuerySharedCredentialsResponse(
471+
size_t page_number, absl::string_view page_token) {
472+
QuerySharedCredentialsResponse response;
473+
int i = 0;
474+
for (auto public_certificate : public_certificates_) {
475+
auto* shared_credential = response.add_shared_credentials();
476+
shared_credential->set_id(page_number * 100 + i);
477+
if (i % 2 == 0) {
478+
shared_credential->set_data_type(
479+
google::nearby::identity::v1::SharedCredential::
480+
DATA_TYPE_PUBLIC_CERTIFICATE);
481+
} else {
482+
shared_credential->set_data_type(
483+
google::nearby::identity::v1::SharedCredential::
484+
DATA_TYPE_SHARED_CREDENTIAL);
485+
}
486+
*shared_credential->mutable_data() =
487+
public_certificate.SerializeAsString();
488+
i++;
489+
}
490+
response.set_next_page_token(page_token);
491+
return response;
492+
}
493+
417494
void CheckStorageAddCertificates(
418495
const FakeNearbyShareCertificateStorage::AddPublicCertificatesCall&
419496
add_cert_call) {
@@ -697,6 +774,23 @@ TEST_F(NearbyShareCertificateManagerImplTest,
697774
/*num_pages=*/2, DownloadPublicCertificatesResult::kHttpError));
698775
}
699776

777+
TEST_F(NearbyShareCertificateManagerImplTest, QuerySharedCredentialsSuccess) {
778+
NearbyFlags::GetInstance().OverrideBoolFlagValue(
779+
config_package_nearby::nearby_sharing_feature::kCallNearbyIdentityApi,
780+
true);
781+
ASSERT_NO_FATAL_FAILURE(QuerySharedCredentialsFlow(
782+
/*num_pages=*/2, DownloadPublicCertificatesResult::kSuccess));
783+
}
784+
785+
TEST_F(NearbyShareCertificateManagerImplTest,
786+
QuerySharedCredentialsRPCFailure) {
787+
NearbyFlags::GetInstance().OverrideBoolFlagValue(
788+
config_package_nearby::nearby_sharing_feature::kCallNearbyIdentityApi,
789+
true);
790+
ASSERT_NO_FATAL_FAILURE(QuerySharedCredentialsFlow(
791+
/*num_pages=*/2, DownloadPublicCertificatesResult::kHttpError));
792+
}
793+
700794
TEST_F(NearbyShareCertificateManagerImplTest, ClearPublicCertificates) {
701795
cert_manager_->ClearPublicCertificates([&](bool result) {});
702796
EXPECT_THAT(cert_store_->clear_public_certificates_callbacks(),

sharing/internal/api/fake_nearby_share_client.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,14 @@ void FakeNearbyIdentityClient::QuerySharedCredentials(
7777
response) &&>
7878
callback) {
7979
query_shared_credentials_requests_.emplace_back(request);
80-
std::move(callback)(query_shared_credentials_response_);
80+
if (query_shared_credentials_responses_.empty()) {
81+
std::move(callback)(absl::NotFoundError(""));
82+
return;
83+
}
84+
auto response = query_shared_credentials_responses_[0];
85+
query_shared_credentials_responses_.erase(
86+
query_shared_credentials_responses_.begin());
87+
std::move(callback)(response);
8188
}
8289

8390
void FakeNearbyIdentityClient::PublishDevice(

sharing/internal/api/fake_nearby_share_client.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ class FakeNearbyIdentityClient
115115
return publish_device_requests_;
116116
}
117117

118+
std::vector<google::nearby::identity::v1::QuerySharedCredentialsRequest>&
119+
query_shared_credentials_requests() {
120+
return query_shared_credentials_requests_;
121+
}
122+
118123
void PublishDevice(
119124
const google::nearby::identity::v1::PublishDeviceRequest& request,
120125
absl::AnyInvocable<
@@ -137,11 +142,11 @@ class FakeNearbyIdentityClient
137142
response) &&>
138143
callback) override;
139144

140-
void SetQuerySharedCredentialsResponse(
141-
absl::StatusOr<
142-
google::nearby::identity::v1::QuerySharedCredentialsResponse>
143-
response) {
144-
query_shared_credentials_response_ = response;
145+
void SetQuerySharedCredentialsResponses(
146+
std::vector<absl::StatusOr<
147+
google::nearby::identity::v1::QuerySharedCredentialsResponse>>
148+
responses) {
149+
query_shared_credentials_responses_ = responses;
145150
}
146151

147152
std::vector<google::nearby::identity::v1::PublishDeviceRequest>
@@ -151,8 +156,9 @@ class FakeNearbyIdentityClient
151156

152157
std::vector<google::nearby::identity::v1::QuerySharedCredentialsRequest>
153158
query_shared_credentials_requests_;
154-
absl::StatusOr<google::nearby::identity::v1::QuerySharedCredentialsResponse>
155-
query_shared_credentials_response_;
159+
std::vector<absl::StatusOr<
160+
google::nearby::identity::v1::QuerySharedCredentialsResponse>>
161+
query_shared_credentials_responses_;
156162
};
157163

158164
class FakeNearbyShareClientFactory

0 commit comments

Comments
 (0)