Commit a3e045f8 authored by Max Li's avatar Max Li Committed by Commit Bot

Add Mojo API for fetching eligible active hosts

Bug: 923594
Change-Id: I20025d7919c07d29b466a4d36d7a2c36b47d5cb6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1896299
Commit-Queue: Max Li <themaxli@chromium.org>
Reviewed-by: default avatarJosh Nohle <nohle@chromium.org>
Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#713951}
parent 422a39e7
......@@ -2,6 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <utility>
#include <vector>
#include "chromeos/services/multidevice_setup/multidevice_setup_impl.h"
#include "base/memory/ptr_util.h"
......@@ -182,6 +185,19 @@ void MultiDeviceSetupImpl::GetEligibleHostDevices(
std::move(callback).Run(eligible_remote_devices);
}
void MultiDeviceSetupImpl::GetEligibleActiveHostDevices(
GetEligibleActiveHostDevicesCallback callback) {
std::vector<mojom::HostDevicePtr> eligible_active_hosts;
for (const auto& host_device :
eligible_host_devices_provider_->GetEligibleActiveHostDevices()) {
eligible_active_hosts.push_back(
mojom::HostDevice::New(host_device.remote_device.GetRemoteDevice(),
host_device.connectivity_status));
}
std::move(callback).Run(std::move(eligible_active_hosts));
}
void MultiDeviceSetupImpl::SetHostDevice(const std::string& host_device_id,
const std::string& auth_token,
SetHostDeviceCallback callback) {
......
......@@ -87,6 +87,8 @@ class MultiDeviceSetupImpl : public MultiDeviceSetupBase,
void AddFeatureStateObserver(
mojo::PendingRemote<mojom::FeatureStateObserver> observer) override;
void GetEligibleHostDevices(GetEligibleHostDevicesCallback callback) override;
void GetEligibleActiveHostDevices(
GetEligibleActiveHostDevicesCallback callback) override;
void SetHostDevice(const std::string& host_device_id,
const std::string& auth_token,
SetHostDeviceCallback callback) override;
......
......@@ -633,6 +633,20 @@ class MultiDeviceSetupImplTest : public testing::Test {
return eligible_devices_list;
}
std::vector<mojom::HostDevicePtr> CallGetEligibleActiveHostDevices() {
base::RunLoop run_loop;
multidevice_setup_->GetEligibleActiveHostDevices(base::BindOnce(
&MultiDeviceSetupImplTest::OnEligibleActiveHostDevicesFetched,
base::Unretained(this), run_loop.QuitClosure()));
run_loop.Run();
std::vector<mojom::HostDevicePtr> eligible_devices_list =
std::move(*last_eligible_active_devices_list_);
last_eligible_active_devices_list_.reset();
return eligible_devices_list;
}
bool CallSetHostDevice(const std::string& host_device_id,
const std::string& auth_token) {
base::RunLoop run_loop;
......@@ -810,6 +824,15 @@ class MultiDeviceSetupImplTest : public testing::Test {
std::move(quit_closure).Run();
}
void OnEligibleActiveHostDevicesFetched(
base::OnceClosure quit_closure,
std::vector<mojom::HostDevicePtr> eligible_active_devices_list) {
EXPECT_FALSE(last_eligible_active_devices_list_);
last_eligible_active_devices_list_ =
std::move(eligible_active_devices_list);
std::move(quit_closure).Run();
}
void OnSetHostDeviceResult(base::OnceClosure quit_closure, bool success) {
EXPECT_FALSE(last_set_host_success_);
last_set_host_success_ = success;
......@@ -899,6 +922,8 @@ class MultiDeviceSetupImplTest : public testing::Test {
base::Optional<bool> last_debug_event_success_;
base::Optional<multidevice::RemoteDeviceList> last_eligible_devices_list_;
base::Optional<std::vector<mojom::HostDevicePtr>>
last_eligible_active_devices_list_;
base::Optional<bool> last_set_host_success_;
base::Optional<bool> last_set_host_without_auth_success_;
base::Optional<
......@@ -1181,6 +1206,29 @@ TEST_F(MultiDeviceSetupImplTest, ComprehensiveHostTest) {
fake_host_backend_delegate()->NotifyHostChangedOnBackend(base::nullopt);
}
TEST_F(MultiDeviceSetupImplTest, TestGetEligibleActiveHosts) {
// Start with no eligible devices.
EXPECT_TRUE(CallGetEligibleActiveHostDevices().empty());
multidevice::DeviceWithConnectivityStatusList host_device_list;
for (auto remote_device_ref : test_devices()) {
host_device_list.emplace_back(multidevice::DeviceWithConnectivityStatus(
remote_device_ref, cryptauthv2::ConnectivityStatus::ONLINE));
}
// Simulate a sync occurring; now, all of the test devices are eligible hosts.
fake_eligible_host_devices_provider()->set_eligible_active_host_devices(
host_device_list);
std::vector<mojom::HostDevicePtr> result_hosts =
CallGetEligibleActiveHostDevices();
for (size_t i = 0; i < kNumTestDevices; i++) {
EXPECT_EQ(*GetMutableRemoteDevice(test_devices()[i]),
result_hosts[i]->remote_device);
EXPECT_EQ(cryptauthv2::ConnectivityStatus::ONLINE,
result_hosts[i]->connectivity_status);
}
}
TEST_F(MultiDeviceSetupImplTest, TestSetHostDevice_InvalidAuthToken) {
// Start valid eligible host devices.
fake_eligible_host_devices_provider()->set_eligible_host_devices(
......
......@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <utility>
#include "chromeos/services/multidevice_setup/multidevice_setup_initializer.h"
#include "base/logging.h"
......@@ -138,6 +140,16 @@ void MultiDeviceSetupInitializer::GetEligibleHostDevices(
pending_get_eligible_hosts_args_.push_back(std::move(callback));
}
void MultiDeviceSetupInitializer::GetEligibleActiveHostDevices(
GetEligibleActiveHostDevicesCallback callback) {
if (multidevice_setup_impl_) {
multidevice_setup_impl_->GetEligibleActiveHostDevices(std::move(callback));
return;
}
pending_get_eligible_active_hosts_args_.push_back(std::move(callback));
}
void MultiDeviceSetupInitializer::SetHostDevice(
const std::string& host_device_id,
const std::string& auth_token,
......@@ -326,6 +338,12 @@ void MultiDeviceSetupInitializer::InitializeImplementation() {
}
pending_get_eligible_hosts_args_.clear();
for (auto& get_eligible_callback : pending_get_eligible_active_hosts_args_) {
multidevice_setup_impl_->GetEligibleActiveHostDevices(
std::move(get_eligible_callback));
}
pending_get_eligible_active_hosts_args_.clear();
for (auto& get_host_callback : pending_get_host_args_)
multidevice_setup_impl_->GetHostStatus(std::move(get_host_callback));
pending_get_host_args_.clear();
......
......@@ -95,6 +95,8 @@ class MultiDeviceSetupInitializer
void AddFeatureStateObserver(
mojo::PendingRemote<mojom::FeatureStateObserver> observer) override;
void GetEligibleHostDevices(GetEligibleHostDevicesCallback callback) override;
void GetEligibleActiveHostDevices(
GetEligibleActiveHostDevicesCallback callback) override;
void SetHostDevice(const std::string& host_device_id,
const std::string& auth_token,
SetHostDeviceCallback callback) override;
......@@ -140,6 +142,8 @@ class MultiDeviceSetupInitializer
std::vector<mojo::PendingRemote<mojom::FeatureStateObserver>>
pending_feature_state_observers_;
std::vector<GetEligibleHostDevicesCallback> pending_get_eligible_hosts_args_;
std::vector<GetEligibleActiveHostDevicesCallback>
pending_get_eligible_active_hosts_args_;
std::vector<GetHostStatusCallback> pending_get_host_args_;
std::vector<std::tuple<mojom::Feature,
bool,
......
......@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <utility>
#include "chromeos/services/multidevice_setup/public/cpp/fake_multidevice_setup.h"
#include "base/containers/flat_map.h"
......@@ -22,6 +24,13 @@ FakeMultiDeviceSetup::~FakeMultiDeviceSetup() {
std::move(get_eligible_hosts_arg).Run(multidevice::RemoteDeviceList());
}
for (auto& get_eligible_active_hosts_arg : get_eligible_active_hosts_args_) {
if (get_eligible_active_hosts_arg) {
std::move(get_eligible_active_hosts_arg)
.Run(std::vector<mojom::HostDevicePtr>());
}
}
for (auto& set_host_arg : set_host_args_) {
if (std::get<2>(set_host_arg))
std::move(std::get<2>(set_host_arg)).Run(false /* success */);
......@@ -115,6 +124,11 @@ void FakeMultiDeviceSetup::GetEligibleHostDevices(
get_eligible_hosts_args_.push_back(std::move(callback));
}
void FakeMultiDeviceSetup::GetEligibleActiveHostDevices(
GetEligibleActiveHostDevicesCallback callback) {
get_eligible_active_hosts_args_.push_back(std::move(callback));
}
void FakeMultiDeviceSetup::SetHostDevice(const std::string& host_device_id,
const std::string& auth_token,
SetHostDeviceCallback callback) {
......
......@@ -94,6 +94,8 @@ class FakeMultiDeviceSetup : public MultiDeviceSetupBase {
void AddFeatureStateObserver(
mojo::PendingRemote<mojom::FeatureStateObserver> observer) override;
void GetEligibleHostDevices(GetEligibleHostDevicesCallback callback) override;
void GetEligibleActiveHostDevices(
GetEligibleActiveHostDevicesCallback callback) override;
void SetHostDevice(const std::string& host_device_id,
const std::string& auth_token,
SetHostDeviceCallback callback) override;
......@@ -120,6 +122,8 @@ class FakeMultiDeviceSetup : public MultiDeviceSetupBase {
mojo::RemoteSet<mojom::FeatureStateObserver> feature_state_observers_;
std::vector<GetEligibleHostDevicesCallback> get_eligible_hosts_args_;
std::vector<GetEligibleActiveHostDevicesCallback>
get_eligible_active_hosts_args_;
std::vector<std::tuple<std::string, std::string, SetHostDeviceCallback>>
set_host_args_;
size_t num_remove_host_calls_ = 0u;
......
......@@ -5,6 +5,7 @@
module chromeos.multidevice_setup.mojom;
import "chromeos/components/multidevice/mojom/multidevice_types.mojom";
import "chromeos/services/device_sync/public/mojom/device_sync.mojom";
// Enumeration of event types which can be dispatched. Only used for debugging
// purposes.
......@@ -91,6 +92,13 @@ enum FeatureState {
kFurtherSetupRequired = 8,
};
// Metadata describing a device that can used as the host for multidevice
// features.
struct HostDevice {
chromeos.multidevice.mojom.RemoteDevice remote_device;
chromeos.device_sync.mojom.ConnectivityStatus connectivity_status;
};
interface AccountStatusChangeDelegate {
// Callback which indicates that one or more MultiDevice host phones are
// available for setup with the MultiDevice setup flow. This function is only
......@@ -154,6 +162,12 @@ interface MultiDeviceSetup {
GetEligibleHostDevices() =>
(array<chromeos.multidevice.mojom.RemoteDevice> eligible_host_devices);
// Provides a list of all active eligible host devices (i.e., those which
// can be passed to SetHostDevice()) sorted by last usage as determined by
// the server.
GetEligibleActiveHostDevices() =>
(array<HostDevice> eligible_host_devices);
// Sets the host associated with the provided device ID as the host device
// for this account. The provided auth token must be valid in order to prove
// that the user is authenticated. If called when there is no current host or
......
......@@ -51,6 +51,21 @@ class FakeMojoService {
});
}
/** @override */
getEligibleActiveHostDevices() {
const deviceNames = ['Pixel', 'Pixel XL', 'Nexus 5', 'Nexus 6P'];
const devices = [];
for (let i = 0; i < this.deviceCount; i++) {
const deviceName = deviceNames[i % 4];
devices.push({
remoteDevice: {deviceName: deviceName, deviceId: deviceName + '--' + i}
});
}
return new Promise(function(resolve, reject) {
resolve({eligibleHostDevices: devices});
});
}
/** @override */
setHostDevice(deviceId) {
if (this.shouldSetHostSucceed) {
......
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