From 7db83028ab467b3b9cffebd81b42e9644c742582 Mon Sep 17 00:00:00 2001 From: Anay Wadhera Date: Tue, 18 Jun 2024 14:01:13 -0700 Subject: [PATCH] Add and plumb GetContacts() API PiperOrigin-RevId: 644504090 --- sharing/contacts/BUILD | 3 +- .../fake_nearby_share_contact_manager.cc | 2 ++ .../fake_nearby_share_contact_manager.h | 1 + .../contacts/nearby_share_contact_manager.h | 9 ++++++ .../nearby_share_contact_manager_impl.cc | 28 +++++++++++++++++++ .../nearby_share_contact_manager_impl.h | 14 ++-------- 6 files changed, 44 insertions(+), 13 deletions(-) diff --git a/sharing/contacts/BUILD b/sharing/contacts/BUILD index 15281ead..b22d00f2 100644 --- a/sharing/contacts/BUILD +++ b/sharing/contacts/BUILD @@ -32,7 +32,6 @@ cc_library( "//internal/crypto_cros", "//internal/platform:types", "//internal/platform/implementation:account_manager", - "//internal/platform/implementation:types", "//sharing/common", "//sharing/internal/api:platform", "//sharing/internal/base", @@ -44,11 +43,11 @@ cc_library( "@com_google_absl//absl/functional:any_invocable", "@com_google_absl//absl/functional:bind_front", "@com_google_absl//absl/memory", + "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings", "@com_google_absl//absl/synchronization", "@com_google_absl//absl/time", - "@com_google_protobuf//:protobuf_lite", ], ) diff --git a/sharing/contacts/fake_nearby_share_contact_manager.cc b/sharing/contacts/fake_nearby_share_contact_manager.cc index 7c3fe8ea..271a7dad 100644 --- a/sharing/contacts/fake_nearby_share_contact_manager.cc +++ b/sharing/contacts/fake_nearby_share_contact_manager.cc @@ -60,5 +60,7 @@ void FakeNearbyShareContactManager::OnStart() {} void FakeNearbyShareContactManager::OnStop() {} +void FakeNearbyShareContactManager::GetContacts(ContactsCallback callback) {} + } // namespace sharing } // namespace nearby diff --git a/sharing/contacts/fake_nearby_share_contact_manager.h b/sharing/contacts/fake_nearby_share_contact_manager.h index 3bb53770..97999b4e 100644 --- a/sharing/contacts/fake_nearby_share_contact_manager.h +++ b/sharing/contacts/fake_nearby_share_contact_manager.h @@ -98,6 +98,7 @@ class FakeNearbyShareContactManager : public NearbyShareContactManager { void DownloadContacts() override; void OnStart() override; void OnStop() override; + void GetContacts(ContactsCallback callback) override; size_t num_download_contacts_calls_ = 0; }; diff --git a/sharing/contacts/nearby_share_contact_manager.h b/sharing/contacts/nearby_share_contact_manager.h index b37ec55e..7e6d2f7e 100644 --- a/sharing/contacts/nearby_share_contact_manager.h +++ b/sharing/contacts/nearby_share_contact_manager.h @@ -21,6 +21,8 @@ #include #include +#include "absl/functional/any_invocable.h" +#include "absl/status/statusor.h" #include "internal/base/observer_list.h" #include "sharing/proto/rpc_resources.pb.h" @@ -42,6 +44,10 @@ namespace sharing { // the manager does not return data directly from function calls. class NearbyShareContactManager { public: + using ContactsCallback = absl::AnyInvocable< + void(absl::StatusOr>, + uint32_t num_unreachable_contacts_filtered_out) &&>; + class Observer { public: virtual ~Observer() = default; @@ -75,6 +81,9 @@ class NearbyShareContactManager { // OnContactsUploaded(). virtual void DownloadContacts() = 0; + // Retrieves the user's contact list from the server. + virtual void GetContacts(ContactsCallback callback) = 0; + protected: virtual void OnStart() = 0; virtual void OnStop() = 0; diff --git a/sharing/contacts/nearby_share_contact_manager_impl.cc b/sharing/contacts/nearby_share_contact_manager_impl.cc index f995be1c..cc43d697 100644 --- a/sharing/contacts/nearby_share_contact_manager_impl.cc +++ b/sharing/contacts/nearby_share_contact_manager_impl.cc @@ -263,6 +263,34 @@ void NearbyShareContactManagerImpl::DownloadContacts() { }); } +void NearbyShareContactManagerImpl::GetContacts(ContactsCallback callback) { + executor_->PostTask([this, callback = std::move(callback)]() mutable { + NL_LOG(INFO) << __func__ << ": Start to download contacts"; + if (!is_running()) { + NL_LOG(WARNING) << __func__ + << ": Ignore to download contacts due to manager is not " + "running."; + return; + } + + std::vector contacts; + if (!account_manager_.GetCurrentAccount().has_value()) { + NL_LOG(WARNING) + << __func__ + << ": Ignore to download certificates due to no login account."; + std::move(callback)(contacts, + /*num_unreachable_contacts_filtered_out=*/0); + return; + } + + // Currently Contacts download is synchronous. It completes after + // FetchNextPage() returns. + auto context = std::make_unique( + nearby_share_client_.get(), std::move(callback)); + context->FetchNextPage(); + }); +} + void NearbyShareContactManagerImpl::OnStart() { periodic_contact_upload_scheduler_->Start(); contact_download_and_upload_scheduler_->Start(); diff --git a/sharing/contacts/nearby_share_contact_manager_impl.h b/sharing/contacts/nearby_share_contact_manager_impl.h index 9ca6c753..d5f45d06 100644 --- a/sharing/contacts/nearby_share_contact_manager_impl.h +++ b/sharing/contacts/nearby_share_contact_manager_impl.h @@ -91,12 +91,7 @@ class NearbyShareContactManagerImpl : public NearbyShareContactManager { public: ContactDownloadContext( nearby::sharing::api::SharingRpcClient* nearby_share_client, - absl::AnyInvocable< - void(absl::StatusOr< - std::vector> - contacts, - uint32_t num_unreachable_contacts_filtered_out) &&> - download_callback) + ContactsCallback download_callback) : nearby_share_client_(nearby_share_client), download_callback_(std::move(download_callback)) {} @@ -111,11 +106,7 @@ class NearbyShareContactManagerImpl : public NearbyShareContactManager { std::optional next_page_token_; int page_number_ = 1; std::vector contacts_; - absl::AnyInvocable< - void(absl::StatusOr> - contacts, - uint32_t num_unreachable_contacts_filtered_out) &&> - download_callback_; + ContactsCallback download_callback_; }; NearbyShareContactManagerImpl( @@ -127,6 +118,7 @@ class NearbyShareContactManagerImpl : public NearbyShareContactManager { // NearbyShareContactsManager: void DownloadContacts() override; + void GetContacts(ContactsCallback callback) override; void OnStart() override; void OnStop() override;