mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Add and plumb GetContacts() API
PiperOrigin-RevId: 644504090
This commit is contained in:
committed by
Copybara-Service
parent
02379cc0a8
commit
7db83028ab
@@ -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",
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
@@ -60,5 +60,7 @@ void FakeNearbyShareContactManager::OnStart() {}
|
||||
|
||||
void FakeNearbyShareContactManager::OnStop() {}
|
||||
|
||||
void FakeNearbyShareContactManager::GetContacts(ContactsCallback callback) {}
|
||||
|
||||
} // namespace sharing
|
||||
} // namespace nearby
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -21,6 +21,8 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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<std::vector<nearby::sharing::proto::ContactRecord>>,
|
||||
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;
|
||||
|
||||
@@ -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<ContactRecord> 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<ContactDownloadContext>(
|
||||
nearby_share_client_.get(), std::move(callback));
|
||||
context->FetchNextPage();
|
||||
});
|
||||
}
|
||||
|
||||
void NearbyShareContactManagerImpl::OnStart() {
|
||||
periodic_contact_upload_scheduler_->Start();
|
||||
contact_download_and_upload_scheduler_->Start();
|
||||
|
||||
@@ -91,12 +91,7 @@ class NearbyShareContactManagerImpl : public NearbyShareContactManager {
|
||||
public:
|
||||
ContactDownloadContext(
|
||||
nearby::sharing::api::SharingRpcClient* nearby_share_client,
|
||||
absl::AnyInvocable<
|
||||
void(absl::StatusOr<
|
||||
std::vector<nearby::sharing::proto::ContactRecord>>
|
||||
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<std::string> next_page_token_;
|
||||
int page_number_ = 1;
|
||||
std::vector<nearby::sharing::proto::ContactRecord> contacts_;
|
||||
absl::AnyInvocable<
|
||||
void(absl::StatusOr<std::vector<nearby::sharing::proto::ContactRecord>>
|
||||
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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user