32
32
#include " absl/functional/bind_front.h"
33
33
#include " absl/memory/memory.h"
34
34
#include " absl/status/statusor.h"
35
+ #include " absl/strings/escaping.h"
35
36
#include " absl/strings/str_cat.h"
36
37
#include " absl/strings/string_view.h"
38
+ #include " absl/strings/strip.h"
37
39
#include " absl/synchronization/notification.h"
38
40
#include " absl/time/time.h"
39
41
#include " absl/types/span.h"
40
42
#include " internal/flags/nearby_flags.h"
41
43
#include " internal/platform/implementation/account_manager.h"
44
+ #include " proto/identity/v1/resources.pb.h"
45
+ #include " proto/identity/v1/rpcs.pb.h"
42
46
#include " sharing/certificates/common.h"
43
47
#include " sharing/certificates/constants.h"
44
48
#include " sharing/certificates/nearby_share_certificate_manager.h"
@@ -71,6 +75,8 @@ namespace nearby {
71
75
namespace sharing {
72
76
namespace {
73
77
78
+ using ::google::nearby::identity::v1::QuerySharedCredentialsRequest;
79
+ using ::google::nearby::identity::v1::QuerySharedCredentialsResponse;
74
80
using ::nearby::sharing::api::PreferenceManager;
75
81
using ::nearby::sharing::api::PublicCertificateDatabase;
76
82
using ::nearby::sharing::api::SharingPlatform;
@@ -227,6 +233,7 @@ NearbyShareCertificateManagerImpl::NearbyShareCertificateManagerImpl(
227
233
local_device_data_manager_(local_device_data_manager),
228
234
contact_manager_(contact_manager),
229
235
nearby_client_(client_factory->CreateInstance ()),
236
+ nearby_identity_client_(client_factory->CreateIdentityInstance ()),
230
237
certificate_storage_(NearbyShareCertificateStorageImpl::Factory::Create(
231
238
preference_manager, std::move(public_certificate_database))),
232
239
private_certificate_expiration_scheduler_(
@@ -295,9 +302,8 @@ void NearbyShareCertificateManagerImpl::CertificateDownloadContext::
295
302
request.set_page_token (*next_page_token_);
296
303
}
297
304
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 {
301
307
if (!response.ok ()) {
302
308
NL_LOG (WARNING) << __func__ << " : Failed to download certificates: "
303
309
<< response.status ();
@@ -321,6 +327,65 @@ void NearbyShareCertificateManagerImpl::CertificateDownloadContext::
321
327
});
322
328
}
323
329
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
+
324
389
void NearbyShareCertificateManagerImpl::OnPublicCertificatesDownloadSuccess (
325
390
const std::vector<PublicCertificate>& certificates) {
326
391
// Save certificates to store.
@@ -373,15 +438,21 @@ void NearbyShareCertificateManagerImpl::DownloadPublicCertificates() {
373
438
// Currently certificates download is synchronous. It completes after
374
439
// FetchNextPage() returns.
375
440
auto context = std::make_unique<CertificateDownloadContext>(
376
- nearby_client_.get (),
441
+ nearby_client_.get (), nearby_identity_client_. get (),
377
442
kDeviceIdPrefix + local_device_data_manager_->GetId (),
378
443
absl::bind_front (&NearbyShareCertificateManagerImpl::
379
444
OnPublicCertificatesDownloadFailure,
380
445
this ),
381
446
absl::bind_front (&NearbyShareCertificateManagerImpl::
382
447
OnPublicCertificatesDownloadSuccess,
383
448
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
+ }
385
456
});
386
457
}
387
458
0 commit comments