Commit d5563e40 authored by Josh Nohle's avatar Josh Nohle Committed by Commit Bot

[Nearby] Add contact/cert/device-data managers

Adds the abstract manager classes for contacts, certificates, and local
device data, whose eventual implementations will direct data flows to
and from the Nearby server and local storage.

Bug: b/154865027, b/154863644, b/154863722
Change-Id: I3a6947541b3167a144b7c6524850b1ec8a7887b4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2272121
Commit-Queue: Josh Nohle <nohle@chromium.org>
Reviewed-by: default avatarJames Vecore <vecore@google.com>
Cr-Commit-Position: refs/heads/master@{#789746}
parent de7ce712
...@@ -3734,6 +3734,7 @@ static_library("browser") { ...@@ -3734,6 +3734,7 @@ static_library("browser") {
"//chrome/browser/media/kaleidoscope/mojom", "//chrome/browser/media/kaleidoscope/mojom",
"//chrome/browser/nearby_sharing/certificates", "//chrome/browser/nearby_sharing/certificates",
"//chrome/browser/nearby_sharing/client", "//chrome/browser/nearby_sharing/client",
"//chrome/browser/nearby_sharing/contacts",
"//chrome/browser/nearby_sharing/instantmessaging/proto", "//chrome/browser/nearby_sharing/instantmessaging/proto",
"//chrome/browser/nearby_sharing/local_device_data", "//chrome/browser/nearby_sharing/local_device_data",
"//chrome/browser/nearby_sharing/logging", "//chrome/browser/nearby_sharing/logging",
......
...@@ -8,6 +8,8 @@ source_set("certificates") { ...@@ -8,6 +8,8 @@ source_set("certificates") {
"common.h", "common.h",
"constants.cc", "constants.cc",
"constants.h", "constants.h",
"nearby_share_certificate_manager.cc",
"nearby_share_certificate_manager.h",
"nearby_share_decrypted_public_certificate.cc", "nearby_share_decrypted_public_certificate.cc",
"nearby_share_decrypted_public_certificate.h", "nearby_share_decrypted_public_certificate.h",
"nearby_share_encrypted_metadata_key.cc", "nearby_share_encrypted_metadata_key.cc",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/nearby_sharing/certificates/nearby_share_certificate_manager.h"
NearbyShareCertificateManager::NearbyShareCertificateManager() = default;
NearbyShareCertificateManager::~NearbyShareCertificateManager() = default;
void NearbyShareCertificateManager::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void NearbyShareCertificateManager::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
void NearbyShareCertificateManager::Start() {
DCHECK(!is_running_);
is_running_ = true;
OnStart();
}
void NearbyShareCertificateManager::Stop() {
DCHECK(is_running_);
is_running_ = false;
OnStop();
}
void NearbyShareCertificateManager::NotifyPublicCertificatesDownloaded(
bool new_certs_added) {
for (auto& observer : observers_)
observer.OnPublicCertificatesDownloaded(new_certs_added);
}
void NearbyShareCertificateManager::NotifyPrivateCertificatesChanged() {
for (auto& observer : observers_)
observer.OnPrivateCertificatesChanged();
}
// Copyright 2020 The Chromium Authors. All rights reserved.
// 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_CERTIFICATES_NEARBY_SHARE_CERTIFICATE_MANAGER_H_
#define CHROME_BROWSER_NEARBY_SHARING_CERTIFICATES_NEARBY_SHARE_CERTIFICATE_MANAGER_H_
#include "base/callback.h"
#include "base/containers/span.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "base/optional.h"
#include "chrome/browser/nearby_sharing/certificates/nearby_share_decrypted_public_certificate.h"
#include "chrome/browser/nearby_sharing/certificates/nearby_share_private_certificate.h"
#include "chrome/browser/nearby_sharing/certificates/nearby_share_visibility.h"
// The Nearby Share certificate manager maintains the local device's private
// certificates and contacts' public certificates. The manager communicates with
// the Nearby server to 1) download contacts' public certificates and 2) upload
// local device public certificates to be distributed to contacts. All crypto
// operations are performed by the private/public certificate classes. Access
// the relevant certificates here, then perform the necessary operations, such
// as signing/verifying a payload or generating an encrypted metadata key for an
// advertisement using the certificate class. Observers are notified of any
// changes to private/public certificates.
class NearbyShareCertificateManager {
public:
class Observer : public base::CheckedObserver {
public:
virtual void OnPublicCertificatesDownloaded(bool new_certs_added) = 0;
virtual void OnPrivateCertificatesChanged() = 0;
};
using CertDecryptedCallback = base::OnceCallback<void(
base::Optional<NearbyShareDecryptedPublicCertificate>)>;
NearbyShareCertificateManager();
virtual ~NearbyShareCertificateManager();
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
// Starts/Stops certificate task scheduling.
void Start();
void Stop();
bool is_running() { return is_running_; }
// Clears all certificate-related data.
virtual void ClearAllData() = 0;
// Returns the currently valid private certificate with |visibility|.
// TODO(crbug.com/1106369): Use common visibility enum.
virtual NearbySharePrivateCertificate GetValidPrivateCertificate(
NearbyShareVisibility visibility) const = 0;
// Returns in |callback| the public certificate that is able to be decrypted
// using |encrypted_metadata_key| and |salt|, and returns base::nullopt if no
// such public certificate exists.
virtual void GetDecryptedPublicCertificate(
base::span<const uint8_t> encrypted_metadata_key,
base::span<const uint8_t> salt,
CertDecryptedCallback callback) const = 0;
// Makes an RPC call to the Nearby server to retrieve all public certificates
// available to the local device. These are also downloaded periodically.
// Observers are notified when all public certificate downloads succeed via
// OnPublicCertificatesDownloaded().
virtual void DownloadPublicCertificates() = 0;
protected:
virtual void OnStart() = 0;
virtual void OnStop() = 0;
void NotifyPublicCertificatesDownloaded(bool new_certs_added);
void NotifyPrivateCertificatesChanged();
private:
bool is_running_ = false;
base::ObserverList<Observer> observers_;
};
#endif // CHROME_BROWSER_NEARBY_SHARING_CERTIFICATES_NEARBY_SHARE_CERTIFICATE_MANAGER_H_
# Copyright 2020 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
source_set("contacts") {
sources = [
"nearby_share_contact_manager.cc",
"nearby_share_contact_manager.h",
]
deps = [
"//base",
"//chrome/browser/nearby_sharing/proto",
]
}
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/nearby_sharing/contacts/nearby_share_contact_manager.h"
NearbyShareContactManager::NearbyShareContactManager() = default;
NearbyShareContactManager::~NearbyShareContactManager() = default;
void NearbyShareContactManager::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void NearbyShareContactManager::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
void NearbyShareContactManager::Start() {
DCHECK(!is_running_);
is_running_ = true;
OnStart();
}
void NearbyShareContactManager::Stop() {
DCHECK(is_running_);
is_running_ = false;
OnStop();
}
void NearbyShareContactManager::NotifyContactsUpdated(
bool contacts_list_changed,
bool contacts_added_to_allowlist,
bool contacts_removed_from_allowlist,
const std::set<std::string>& allowed_contact_ids,
const base::Optional<std::vector<nearbyshare::proto::ContactRecord>>&
contacts) {
for (auto& observer : observers_) {
observer.OnContactsUpdated(
contacts_list_changed, contacts_added_to_allowlist,
contacts_removed_from_allowlist, allowed_contact_ids, contacts);
}
}
// Copyright 2020 The Chromium Authors. All rights reserved.
// 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_H_
#define CHROME_BROWSER_NEARBY_SHARING_CONTACTS_NEARBY_SHARE_CONTACT_MANAGER_H_
#include <set>
#include <string>
#include <vector>
#include "base/callback.h"
#include "base/containers/span.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "base/optional.h"
#include "chrome/browser/nearby_sharing/proto/rpc_resources.pb.h"
// The Nearby Share contacts manager interfaces with the Nearby server to 1)
// download the user's contacts and 2) upload the user-input list of allowed
// contacts for selected-contacts visibility mode. All contact data and update
// notifications are conveyed to observers via OnContactsUpdated(); the manager
// does not return data directly from function calls.
class NearbyShareContactManager {
public:
class Observer : public base::CheckedObserver {
public:
virtual void OnContactsUpdated(
bool contacts_list_changed,
bool contacts_added_to_allowlist,
bool contacts_removed_from_allowlist,
const std::set<std::string>& allowed_contact_ids,
const base::Optional<std::vector<nearbyshare::proto::ContactRecord>>&
contacts) = 0;
};
NearbyShareContactManager();
virtual ~NearbyShareContactManager();
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
// Starts/Stops contact task scheduling.
void Start();
void Stop();
bool is_running() { return is_running_; }
// Clears all contact-related data.
virtual void ClearAllData() = 0;
// Makes RPC calls to check if the user's contact list has changed since the
// last call to the server. If it changed or if |only_download_if_changed| is
// false, the contact list is downloaded from the server. The list of allowed
// contacts is reconciled with the newly downloaded contacts. These RPC calls
// are also scheduled periodically. The results are sent to observers via
// OnContactsUpdated().
virtual void DownloadContacts(bool only_download_if_changed) = 0;
// Assigns the set of contacts that the local device allows sharing with when
// in selected-contacts visibility mode. (Note: This set is irrelevant for
// all-contacts visibility mode.) The allowed contact list determines what
// contacts receive the local device's "selected-contacts" visibility public
// certificates. Changes to the allowlist will trigger an RPC call. Observers
// are notified of any changes to the allowlist via OnContactsUpdated().
virtual void SetAllowedContacts(
const std::set<std::string>& allowed_contact_ids) = 0;
protected:
virtual void OnStart() = 0;
virtual void OnStop() = 0;
void NotifyContactsUpdated(
bool contacts_list_changed,
bool contacts_added_to_allowlist,
bool contacts_removed_from_allowlist,
const std::set<std::string>& allowed_contact_ids,
const base::Optional<std::vector<nearbyshare::proto::ContactRecord>>&
contacts);
private:
bool is_running_ = false;
base::ObserverList<Observer> observers_;
};
#endif // CHROME_BROWSER_NEARBY_SHARING_CONTACTS_NEARBY_SHARE_CONTACT_MANAGER_H_
...@@ -8,6 +8,8 @@ source_set("local_device_data") { ...@@ -8,6 +8,8 @@ source_set("local_device_data") {
"nearby_share_device_data_updater.h", "nearby_share_device_data_updater.h",
"nearby_share_device_data_updater_impl.cc", "nearby_share_device_data_updater_impl.cc",
"nearby_share_device_data_updater_impl.h", "nearby_share_device_data_updater_impl.h",
"nearby_share_local_device_data_manager.cc",
"nearby_share_local_device_data_manager.h",
] ]
deps = [ deps = [
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/nearby_sharing/local_device_data/nearby_share_local_device_data_manager.h"
NearbyShareLocalDeviceDataManager::NearbyShareLocalDeviceDataManager() =
default;
NearbyShareLocalDeviceDataManager::~NearbyShareLocalDeviceDataManager() =
default;
void NearbyShareLocalDeviceDataManager::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void NearbyShareLocalDeviceDataManager::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
void NearbyShareLocalDeviceDataManager::Start() {
DCHECK(!is_running_);
is_running_ = true;
OnStart();
}
void NearbyShareLocalDeviceDataManager::Stop() {
DCHECK(is_running_);
is_running_ = false;
OnStop();
}
void NearbyShareLocalDeviceDataManager::NotifyLocalDeviceDataChanged(
bool did_device_name_change,
bool did_full_name_change,
bool did_icon_url_change) {
for (auto& observer : observers_) {
observer.OnLocalDeviceDataChanged(
did_device_name_change, did_full_name_change, did_icon_url_change);
}
}
// Copyright 2020 The Chromium Authors. All rights reserved.
// 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_LOCAL_DEVICE_DATA_NEARBY_SHARE_LOCAL_DEVICE_DATA_MANAGER_H_
#define CHROME_BROWSER_NEARBY_SHARING_LOCAL_DEVICE_DATA_NEARBY_SHARE_LOCAL_DEVICE_DATA_MANAGER_H_
#include <string>
#include <vector>
#include "base/callback.h"
#include "base/containers/span.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "base/optional.h"
#include "chrome/browser/nearby_sharing/proto/rpc_resources.pb.h"
// Manages local device data related to the UpdateDevice RPC such as the device
// ID, name, and icon url; provides the user's full name and icon URL returned
// from the Nearby server; and handles uploading contacts and certificates to
// the Nearby server. The uploading of contacts and certificates might seem out
// of place, but this class is the entry point for all UpdateDevice RPC calls.
class NearbyShareLocalDeviceDataManager {
public:
class Observer : public base::CheckedObserver {
public:
virtual void OnLocalDeviceDataChanged(bool did_device_name_change,
bool did_full_name_change,
bool did_icon_url_change) = 0;
};
using UploadCompleteCallback = base::OnceCallback<void(bool success)>;
NearbyShareLocalDeviceDataManager();
virtual ~NearbyShareLocalDeviceDataManager();
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
// Starts/Stops local-device-data task scheduling.
void Start();
void Stop();
bool is_running() { return is_running_; }
// Clears all local-device-related data.
virtual void ClearAllData() = 0;
// Returns the immutable ID generated for the local device, used to
// differentiate a user's devices when communicating with the Nearby server.
virtual std::string GetId() = 0;
// Returns the name of the local device, typically in the format of
// "UserName's DeviceType". This can be modified by SetDeviceName(). Returns
// base::nullopt if the device name have not been set yet.
virtual base::Optional<std::string> GetDeviceName() const = 0;
// Returns the user's full name, for example, "Barack Obama". Returns
// base::nullopt if the name has not yet been set from an UpdateDevice RPC
// response.
virtual base::Optional<std::string> GetFullName() const = 0;
// Returns the URL of the user's image. Returns base::nullopt if the URL has
// not yet been set from an UpdateDevice RPC response.
virtual base::Optional<std::string> GetIconUrl() const = 0;
// Uses the UpdateDevice RPC to change the local device name in the Nearby
// Share server and in local storage. Must be UTF-8. Observers are notified
// via OnLocalDeviceDataChanged() if the device name changes.
virtual void SetDeviceName(const std::string& name) = 0;
// Makes an UpdateDevice RPC call to the Nearby Share server to retrieve all
// available device data, which includes the full name and icon URL for now.
// This action is also scheduled periodically. Observers are notified via
// OnLocalDeviceDataChanged() if any device data changes.
virtual void DownloadDeviceData() = 0;
// Uses the UpdateDevice RPC to send the local device's contact list to the
// Nearby Share server, including which contacts are allowed for
// selected-contacts visibility mode. This should only be invoked by the
// contact manager, and the contact manager should handle scheduling, failure
// retry, etc.
virtual void UploadContacts(
base::Optional<std::vector<nearbyshare::proto::Contact>> contacts,
UploadCompleteCallback callback) = 0;
// Uses the UpdateDevice RPC to send the local device's public certificates to
// the Nearby Share server. This should only be invoked by the certificate
// manager, and the certificate manager should handle scheduling, failure
// retry, etc.
virtual void UploadCertificates(
base::Optional<std::vector<nearbyshare::proto::PublicCertificate>>
certificates,
UploadCompleteCallback callback) = 0;
protected:
virtual void OnStart() = 0;
virtual void OnStop() = 0;
void NotifyLocalDeviceDataChanged(bool did_device_name_change,
bool did_full_name_change,
bool did_icon_url_change);
private:
bool is_running_ = false;
base::ObserverList<Observer> observers_;
};
#endif // CHROME_BROWSER_NEARBY_SHARING_LOCAL_DEVICE_DATA_NEARBY_SHARE_LOCAL_DEVICE_DATA_MANAGER_H_
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