Commit 0912baa4 authored by Josh Nohle's avatar Josh Nohle Committed by Commit Bot

[Nearby] Implement contact manager

This implementation:
- Stores the set of selected contacts to prefs, but the full contact
  list must be downloaded as needed.
- Handles the upload of contacts to the Nearby Share server.
- Schedules periodic check-ins with the server to see if contacts have
  changed since the last upload.

Bug: b/154863722, b/154861871
Change-Id: I72c03ea0f922f3386fd6ea5336fbab728f0e429c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2307477Reviewed-by: default avatarJames Vecore <vecore@google.com>
Commit-Queue: Josh Nohle <nohle@chromium.org>
Cr-Commit-Position: refs/heads/master@{#801720}
parent 39c3b613
......@@ -28,6 +28,10 @@ const char kNearbySharingFullNamePrefName[] = "nearby_sharing.full_name";
const char kNearbySharingIconUrlPrefName[] = "nearby_sharing.icon_url";
const char kNearbySharingOnboardingDismissedTimePrefName[] =
"nearby_sharing.onboarding_dismissed_time";
const char kNearbySharingSchedulerContactDownloadPrefName[] =
"nearby_sharing.scheduler.contact_download";
const char kNearbySharingSchedulerContactUploadPrefName[] =
"nearby_sharing.scheduler.contact_upload";
const char kNearbySharingSchedulerDownloadDeviceDataPrefName[] =
"nearby_sharing.scheduler.download_device_data";
const char kNearbySharingSchedulerUploadDeviceNamePrefName[] =
......@@ -63,6 +67,10 @@ void RegisterNearbySharingPrefs(PrefRegistrySimple* registry) {
/*default_value=*/std::string());
registry->RegisterStringPref(prefs::kNearbySharingIconUrlPrefName,
/*default_value=*/std::string());
registry->RegisterDictionaryPref(
prefs::kNearbySharingSchedulerContactDownloadPrefName);
registry->RegisterDictionaryPref(
prefs::kNearbySharingSchedulerContactUploadPrefName);
registry->RegisterDictionaryPref(
prefs::kNearbySharingSchedulerDownloadDeviceDataPrefName);
registry->RegisterDictionaryPref(
......
......@@ -19,6 +19,8 @@ extern const char kNearbySharingEnabledPrefName[];
extern const char kNearbySharingFullNamePrefName[];
extern const char kNearbySharingIconUrlPrefName[];
extern const char kNearbySharingOnboardingDismissedTimePrefName[];
extern const char kNearbySharingSchedulerContactDownloadPrefName[];
extern const char kNearbySharingSchedulerContactUploadPrefName[];
extern const char kNearbySharingSchedulerDownloadDeviceDataPrefName[];
extern const char kNearbySharingSchedulerUploadDeviceNamePrefName[];
extern const char kNearbySharingPublicCertificateExpirationDictPrefName[];
......
......@@ -18,8 +18,11 @@ source_set("contacts") {
"//base",
"//chrome/browser/nearby_sharing/client",
"//chrome/browser/nearby_sharing/common",
"//chrome/browser/nearby_sharing/local_device_data",
"//chrome/browser/nearby_sharing/logging",
"//chrome/browser/nearby_sharing/proto",
"//chrome/browser/nearby_sharing/scheduling",
"//components/prefs",
]
}
......@@ -43,7 +46,10 @@ source_set("test_support") {
source_set("unit_tests") {
testonly = true
sources = [ "nearby_share_contact_downloader_impl_unittest.cc" ]
sources = [
"nearby_share_contact_downloader_impl_unittest.cc",
"nearby_share_contact_manager_impl_unittest.cc",
]
deps = [
":contacts",
......@@ -53,7 +59,11 @@ source_set("unit_tests") {
"//chrome/browser/nearby_sharing/client",
"//chrome/browser/nearby_sharing/client:test_support",
"//chrome/browser/nearby_sharing/common",
"//chrome/browser/nearby_sharing/local_device_data:test_support",
"//chrome/browser/nearby_sharing/proto",
"//chrome/browser/nearby_sharing/scheduling",
"//chrome/browser/nearby_sharing/scheduling:test_support",
"//components/sync_preferences:test_support",
"//testing/gtest",
]
}
......@@ -9,7 +9,14 @@ FakeNearbyShareContactManager::Factory::Factory() = default;
FakeNearbyShareContactManager::Factory::~Factory() = default;
std::unique_ptr<NearbyShareContactManager>
FakeNearbyShareContactManager::Factory::CreateInstance() {
FakeNearbyShareContactManager::Factory::CreateInstance(
PrefService* pref_service,
NearbyShareClientFactory* http_client_factory,
NearbyShareLocalDeviceDataManager* local_device_data_manager) {
latest_pref_service_ = pref_service;
latest_http_client_factory_ = http_client_factory;
latest_local_device_data_manager_ = local_device_data_manager;
auto instance = std::make_unique<FakeNearbyShareContactManager>();
instances_.push_back(instance.get());
......
......@@ -15,6 +15,10 @@
#include "chrome/browser/nearby_sharing/contacts/nearby_share_contact_manager_impl.h"
#include "chrome/browser/nearby_sharing/proto/rpc_resources.pb.h"
class NearbyShareClientFactory;
class NearbyShareLocalDeviceDataManager;
class PrefService;
// A fake implementation of NearbyShareContactManager, along with a fake
// factory, to be used in tests. Stores parameters input into
// NearbyShareContactManager method calls. Use the notification methods from the
......@@ -37,11 +41,29 @@ class FakeNearbyShareContactManager : public NearbyShareContactManager {
return instances_;
}
PrefService* latest_pref_service() const { return latest_pref_service_; }
NearbyShareClientFactory* latest_http_client_factory() const {
return latest_http_client_factory_;
}
NearbyShareLocalDeviceDataManager* latest_local_device_data_manager()
const {
return latest_local_device_data_manager_;
}
private:
// NearbyShareContactManagerImpl::Factory:
std::unique_ptr<NearbyShareContactManager> CreateInstance() override;
std::unique_ptr<NearbyShareContactManager> CreateInstance(
PrefService* pref_service,
NearbyShareClientFactory* http_client_factory,
NearbyShareLocalDeviceDataManager* local_device_data_manager) override;
std::vector<FakeNearbyShareContactManager*> instances_;
PrefService* latest_pref_service_ = nullptr;
NearbyShareClientFactory* latest_http_client_factory_ = nullptr;
NearbyShareLocalDeviceDataManager* latest_local_device_data_manager_ =
nullptr;
};
FakeNearbyShareContactManager();
......
......@@ -9,19 +9,50 @@
#include <set>
#include <string>
#include "base/callback.h"
#include "base/optional.h"
#include "chrome/browser/nearby_sharing/contacts/nearby_share_contact_manager.h"
#include "chrome/browser/nearby_sharing/proto/rpc_resources.pb.h"
// TODO(nohle): Add description after class is fully implemented.
class NearbyShareClientFactory;
class NearbyShareContactDownloader;
class NearbyShareLocalDeviceDataManager;
class NearbyShareScheduler;
class PrefService;
// Implementation of NearbyShareContactManager that persists the set of allowed
// contact IDs--for selected-contacts visiblity mode--in prefs. All other
// contact data is downloaded from People API, via the NearbyShare server, as
// needed.
//
// The Nearby Share server must be explicitly informed of all contacts this
// device is aware of--needed for all-contacts visibility mode--as well as what
// contacts are allowed for selected-contacts visibility mode. The
// NearbyShareContactManagerImpl controls when contacts are uploaded to the
// server: 1) when the server communicates that the contact list has changed
// since the last upload, or 2) when the user locally makes changes to the list
// of selected contacts. These uploaded contact lists are used by the server to
// distribute the device's public certificates accordingly.
//
// In addition to supporting on-demand contact downloads, this implementation
// periodically checks in with the Nearby Share server to see if the user's
// contact list has changed since the last upload.
class NearbyShareContactManagerImpl : public NearbyShareContactManager {
public:
class Factory {
public:
static std::unique_ptr<NearbyShareContactManager> Create();
static std::unique_ptr<NearbyShareContactManager> Create(
PrefService* pref_service,
NearbyShareClientFactory* http_client_factory,
NearbyShareLocalDeviceDataManager* local_device_data_manager);
static void SetFactoryForTesting(Factory* test_factory);
protected:
virtual ~Factory();
virtual std::unique_ptr<NearbyShareContactManager> CreateInstance() = 0;
virtual std::unique_ptr<NearbyShareContactManager> CreateInstance(
PrefService* pref_service,
NearbyShareClientFactory* http_client_factory,
NearbyShareLocalDeviceDataManager* local_device_data_manager) = 0;
private:
static Factory* test_factory_;
......@@ -30,7 +61,12 @@ class NearbyShareContactManagerImpl : public NearbyShareContactManager {
~NearbyShareContactManagerImpl() override;
private:
NearbyShareContactManagerImpl();
enum class UploadState { kIdle, kWaitingForDownload, kInProgress };
NearbyShareContactManagerImpl(
PrefService* pref_service,
NearbyShareClientFactory* http_client_factory,
NearbyShareLocalDeviceDataManager* local_device_data_manager);
// NearbyShareContactsManager:
void DownloadContacts(bool only_download_if_changed) override;
......@@ -38,6 +74,34 @@ class NearbyShareContactManagerImpl : public NearbyShareContactManager {
const std::set<std::string>& allowed_contact_ids) override;
void OnStart() override;
void OnStop() override;
std::set<std::string> GetAllowedContacts() const;
void OnContactsDownloadRequested();
void OnContactsDownloadSuccess(
bool did_contacts_change_since_last_upload,
base::Optional<std::vector<nearbyshare::proto::ContactRecord>> contacts);
void OnContactsDownloadFailure();
void OnContactsUploadRequested();
void StartContactsUpload(
bool did_contacts_change_since_last_upload,
const std::vector<nearbyshare::proto::ContactRecord>& contacts);
void OnContactsUploadFinished(bool did_contacts_change_since_last_upload,
bool success);
bool SetAllowlist(const std::set<std::string>& new_allowlist);
// By default, only download contacts if they have changed since the last
// upload. Only set to false on explicit request from DownloadContacts(), and
// reset to true after a successful contact download.
bool only_download_if_changed_ = true;
UploadState upload_state_ = UploadState::kIdle;
PrefService* pref_service_ = nullptr;
NearbyShareClientFactory* http_client_factory_ = nullptr;
NearbyShareLocalDeviceDataManager* local_device_data_manager_ = nullptr;
std::unique_ptr<NearbyShareScheduler> contact_download_scheduler_;
std::unique_ptr<NearbyShareScheduler> contact_upload_scheduler_;
std::unique_ptr<NearbyShareContactDownloader> contact_downloader_;
base::WeakPtrFactory<NearbyShareContactManagerImpl> weak_ptr_factory_{this};
};
#endif // CHROME_BROWSER_NEARBY_SHARING_CONTACTS_NEARBY_SHARE_CONTACT_MANAGER_IMPL_H_
......@@ -242,7 +242,10 @@ NearbySharingServiceImpl::NearbySharingServiceImpl(
NearbyShareLocalDeviceDataManagerImpl::Factory::Create(
prefs,
http_client_factory_.get())),
contact_manager_(NearbyShareContactManagerImpl::Factory::Create()),
contact_manager_(NearbyShareContactManagerImpl::Factory::Create(
prefs,
http_client_factory_.get(),
local_device_data_manager_.get())),
certificate_manager_(NearbyShareCertificateManagerImpl::Factory::Create(
local_device_data_manager_.get(),
prefs,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment