1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_NEARBY_SHARING_CONTACTS_NEARBY_SHARE_CONTACT_MANAGER_IMPL_H_
#define CHROME_BROWSER_NEARBY_SHARING_CONTACTS_NEARBY_SHARE_CONTACT_MANAGER_IMPL_H_
#include <memory>
#include <string>
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/nearby_sharing/contacts/nearby_share_contact_manager.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/bindings/remote_set.h"
#include "third_party/nearby/sharing/proto/rpc_resources.pb.h"
class NearbyShareClientFactory;
class NearbyShareContactDownloader;
class NearbyShareLocalDeviceDataManager;
class PrefService;
namespace ash::nearby {
class NearbyScheduler;
} // namespace ash::nearby
// 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. These uploaded
// contact lists are used by the server to distribute the device's public
// certificates accordingly. This implementation persists a hash of the last
// uploaded contact data, and after every contacts download, a subsequent upload
// request is made if we detect that the contact list or allowlist has changed
// since the last successful upload. We also schedule periodic contact uploads
// just in case the server removed the record.
//
// 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(
std::string user_email,
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(
std::string user_email,
PrefService* pref_service,
NearbyShareClientFactory* http_client_factory,
NearbyShareLocalDeviceDataManager* local_device_data_manager) = 0;
private:
static Factory* test_factory_;
};
~NearbyShareContactManagerImpl() override;
private:
NearbyShareContactManagerImpl(
std::string user_email,
PrefService* pref_service,
NearbyShareClientFactory* http_client_factory,
NearbyShareLocalDeviceDataManager* local_device_data_manager);
// NearbyShareContactsManager:
void DownloadContacts() override;
void SetAllowedContacts(
const std::set<std::string>& allowed_contact_ids) override;
std::set<std::string> GetAllowedContacts() const override;
void OnStart() override;
void OnStop() override;
void Bind(mojo::PendingReceiver<nearby_share::mojom::ContactManager> receiver)
override;
// nearby_share::mojom::ContactsManager:
void AddDownloadContactsObserver(
::mojo::PendingRemote<nearby_share::mojom::DownloadContactsObserver>
observer) override;
void OnPeriodicContactsUploadRequested();
void OnContactsDownloadRequested();
void OnContactsDownloadSuccess(
std::vector<nearby::sharing::proto::ContactRecord> contacts,
uint32_t num_unreachable_contacts_filtered_out);
void OnContactsDownloadFailure();
void OnContactsUploadFinished(bool did_contacts_change_since_last_upload,
const std::string& contact_upload_hash,
bool success);
bool SetAllowlist(const std::set<std::string>& new_allowlist);
// Notify the base-class and mojo observers that contacts were downloaded.
void NotifyAllObserversContactsDownloaded(
const std::set<std::string>& allowed_contact_ids,
const std::vector<nearby::sharing::proto::ContactRecord>& contacts,
uint32_t num_unreachable_contacts_filtered_out);
std::string user_email_;
raw_ptr<PrefService> pref_service_ = nullptr;
raw_ptr<NearbyShareClientFactory> http_client_factory_ = nullptr;
raw_ptr<NearbyShareLocalDeviceDataManager> local_device_data_manager_ =
nullptr;
std::unique_ptr<ash::nearby::NearbyScheduler>
periodic_contact_upload_scheduler_;
std::unique_ptr<ash::nearby::NearbyScheduler>
contact_download_and_upload_scheduler_;
std::unique_ptr<NearbyShareContactDownloader> contact_downloader_;
mojo::RemoteSet<nearby_share::mojom::DownloadContactsObserver> observers_set_;
mojo::ReceiverSet<nearby_share::mojom::ContactManager> receiver_set_;
base::WeakPtrFactory<NearbyShareContactManagerImpl> weak_ptr_factory_{this};
};
#endif // CHROME_BROWSER_NEARBY_SHARING_CONTACTS_NEARBY_SHARE_CONTACT_MANAGER_IMPL_H_
|