Commit 6a5e94d3 authored by Kyle Horimoto's avatar Kyle Horimoto Committed by Commit Bot

[CrOS MultiDevice] DeviceSync: Add GetLocalDeviceMetadata() API fn.

This function returns device metadata associated with the current device
(i.e., the one on which the code is running).

Bug: 824568, 752273
Change-Id: I08e53e390cf2737bdaff154a9dff2cead9884db0
Reviewed-on: https://chromium-review.googlesource.com/1020069
Commit-Queue: Kyle Horimoto <khorimoto@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarRyan Hansberry <hansberry@chromium.org>
Reviewed-by: default avatarJeremy Klein <jlklein@chromium.org>
Cr-Commit-Position: refs/heads/master@{#553398}
parent 3619737e
......@@ -6,6 +6,7 @@
#include "base/memory/ptr_util.h"
#include "base/no_destructor.h"
#include "base/optional.h"
#include "base/time/default_clock.h"
#include "chromeos/components/proximity_auth/logging/logging.h"
#include "chromeos/services/device_sync/cryptauth_client_factory_impl.h"
......@@ -162,6 +163,21 @@ void DeviceSyncImpl::ForceSyncNow(ForceSyncNowCallback callback) {
std::move(callback).Run(true /* success */);
}
void DeviceSyncImpl::GetLocalDeviceMetadata(
GetLocalDeviceMetadataCallback callback) {
if (status_ != Status::READY) {
PA_LOG(WARNING) << "DeviceSyncImpl::GetLocalDeviceMetadata() invoked "
<< "before initialization was complete. Cannot return "
<< "local device metadata.";
std::move(callback).Run(base::nullopt);
return;
}
std::string public_key = cryptauth_enrollment_manager_->GetUserPublicKey();
DCHECK(!public_key.empty());
std::move(callback).Run(GetSyncedDeviceWithPublicKey(public_key));
}
void DeviceSyncImpl::GetSyncedDevices(GetSyncedDevicesCallback callback) {
if (status_ != Status::READY) {
PA_LOG(WARNING) << "DeviceSyncImpl::GetSyncedDevices() invoked before "
......
......@@ -96,6 +96,7 @@ class DeviceSyncImpl : public mojom::DeviceSync,
AddObserverCallback callback) override;
void ForceEnrollmentNow(ForceEnrollmentNowCallback callback) override;
void ForceSyncNow(ForceSyncNowCallback callback) override;
void GetLocalDeviceMetadata(GetLocalDeviceMetadataCallback callback) override;
void GetSyncedDevices(GetSyncedDevicesCallback callback) override;
void SetSoftwareFeatureState(
const std::string& device_public_key,
......
......@@ -6,6 +6,7 @@
#include "base/memory/scoped_refptr.h"
#include "base/no_destructor.h"
#include "base/optional.h"
#include "base/run_loop.h"
#include "base/test/null_task_runner.h"
#include "base/test/scoped_task_environment.h"
......@@ -45,6 +46,7 @@ namespace {
const char kTestEmail[] = "example@gmail.com";
const char kTestGcmDeviceInfoLongDeviceId[] = "longDeviceId";
const char kTestCryptAuthGCMRegistrationId[] = "cryptAuthRegistrationId";
const char kLocalDevicePublicKey[] = "localDevicePublicKey";
const size_t kNumTestDevices = 5u;
const cryptauth::GcmDeviceInfo& GetTestGcmDeviceInfo() {
......@@ -61,6 +63,11 @@ cryptauth::RemoteDeviceList GenerateTestRemoteDevices() {
cryptauth::RemoteDeviceList devices =
cryptauth::GenerateTestRemoteDevices(kNumTestDevices);
// One of the synced devices refers to the current (i.e., local) device.
// Arbitrarily choose the 0th device as the local one and set its public key
// accordingly.
devices[0].public_key = kLocalDevicePublicKey;
// Load an empty set of BeaconSeeds for each device.
// TODO(khorimoto): Adjust device_sync_mojom_traits.h/cc to allow passing
// devices without BeaconSeeds to be sent across Mojo.
......@@ -236,6 +243,7 @@ class FakeCryptAuthEnrollmentManagerFactory
auto instance =
std::make_unique<cryptauth::FakeCryptAuthEnrollmentManager>();
instance->set_user_public_key(kLocalDevicePublicKey);
instance->set_is_enrollment_valid(device_already_enrolled_in_cryptauth_);
instance_ = instance.get();
......@@ -684,6 +692,10 @@ class DeviceSyncServiceTest : public testing::Test {
// GetSyncedDevices() returns a null list before initialization.
EXPECT_FALSE(CallGetSyncedDevices());
// GetLocalDeviceMetadata() returns a null RemoteDevice before
// initialization.
EXPECT_FALSE(CallGetLocalDeviceMetadata());
// SetSoftwareFeatureState() should return a struct with the special
// kErrorNotInitialized error code.
CallSetSoftwareFeatureState(
......@@ -745,6 +757,15 @@ class DeviceSyncServiceTest : public testing::Test {
return last_force_sync_now_result_;
}
const base::Optional<cryptauth::RemoteDevice>& CallGetLocalDeviceMetadata() {
base::RunLoop run_loop;
device_sync_->GetLocalDeviceMetadata(base::BindOnce(
&DeviceSyncServiceTest::OnGetLocalDeviceMetadataCompleted,
base::Unretained(this), run_loop.QuitClosure()));
run_loop.Run();
return last_local_device_metadata_result_;
}
const base::Optional<cryptauth::RemoteDeviceList>& CallGetSyncedDevices() {
base::RunLoop run_loop;
device_sync_->GetSyncedDevices(
......@@ -844,6 +865,13 @@ class DeviceSyncServiceTest : public testing::Test {
std::move(quit_closure).Run();
}
void OnGetLocalDeviceMetadataCompleted(
base::OnceClosure quit_closure,
const base::Optional<cryptauth::RemoteDevice>& local_device_metadata) {
last_local_device_metadata_result_ = local_device_metadata;
std::move(quit_closure).Run();
}
void OnGetSyncedDevicesCompleted(
base::OnceClosure quit_closure,
const base::Optional<cryptauth::RemoteDeviceList>& synced_devices) {
......@@ -926,6 +954,7 @@ class DeviceSyncServiceTest : public testing::Test {
bool last_force_enrollment_now_result_;
bool last_force_sync_now_result_;
base::Optional<cryptauth::RemoteDeviceList> last_synced_devices_result_;
base::Optional<cryptauth::RemoteDevice> last_local_device_metadata_result_;
std::unique_ptr<base::Optional<std::string>>
last_set_software_feature_state_response_;
std::unique_ptr<std::pair<base::Optional<std::string>,
......@@ -1039,6 +1068,17 @@ TEST_F(DeviceSyncServiceTest, EnrollAgainAfterInitialization) {
EXPECT_EQ(1u, fake_device_sync_observer()->num_enrollment_events());
}
TEST_F(DeviceSyncServiceTest, GetLocalDeviceMetadata) {
InitializeServiceSuccessfully();
const auto& result = CallGetLocalDeviceMetadata();
EXPECT_TRUE(result);
EXPECT_EQ(kLocalDevicePublicKey, result->public_key);
// Note: In GenerateTestRemoteDevices(), the 0th test device is arbitrarily
// chosen as the local device.
EXPECT_EQ(test_devices()[0], *result);
}
TEST_F(DeviceSyncServiceTest, SyncedDeviceUpdates) {
InitializeServiceSuccessfully();
EXPECT_EQ(1u, fake_device_sync_observer()->num_sync_events());
......
......@@ -144,6 +144,10 @@ interface DeviceSync {
// device has not yet registered with the back-end, no list is provided.
GetSyncedDevices() => (array<RemoteDevice>? devices);
// Returns the RemoteDevice object associated with this device. If this device
// has not yet registered with the back-end, no device is provided.
GetLocalDeviceMetadata() => (RemoteDevice? local_device);
// Enables or disables the given software feature for the device with the
// given public key. If |enabled| and |is_exclusive| are both true, this
// function will enable the feature for the given device and disable the
......
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