Commit 10864101 authored by Daniel Classon's avatar Daniel Classon Committed by Commit Bot

[MultideviceStubs] Add stubbed Device Sync

Adds a stubbed implementation of the device sync for use in the Linux
CrOS build.

Bug: 1085459
Change-Id: Iaf124a7575022d1a6e6da48143e68e9db55e44c2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2406543
Commit-Queue: Daniel Classon <dclasson@google.com>
Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#809077}
parent 0282afd7
......@@ -191,6 +191,7 @@ source_set("chromeos") {
"//chromeos/services/cros_healthd/public/cpp",
"//chromeos/services/cros_healthd/public/mojom",
"//chromeos/services/device_sync",
"//chromeos/services/device_sync:stub_device_sync",
"//chromeos/services/device_sync/public/cpp",
"//chromeos/services/ime:constants",
"//chromeos/services/ime/public/cpp:buildflags",
......
......@@ -173,8 +173,8 @@ static_library("device_sync") {
visibility = [
":*",
"//chrome/browser/chromeos",
"//chromeos/services/device_sync/public/cpp:prefs",
"//chrome/test:test_support_ui",
"//chromeos/services/device_sync/public/cpp:prefs",
"//chromeos/services/device_sync/public/cpp:unit_tests",
]
}
......@@ -248,6 +248,19 @@ static_library("test_support") {
]
}
source_set("stub_device_sync") {
sources = [
"stub_device_sync.cc",
"stub_device_sync.h",
]
deps = [
":device_sync",
"//base",
"//chromeos/components/multidevice:stub_multidevice_util",
]
}
source_set("unit_tests") {
testonly = true
......
// 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 "chromeos/services/device_sync/stub_device_sync.h"
#include <utility>
#include "base/memory/ptr_util.h"
#include "base/optional.h"
#include "base/time/time.h"
#include "chromeos/components/multidevice/stub_multidevice_util.h"
#include "chromeos/services/device_sync/public/mojom/device_sync.mojom.h"
namespace chromeos {
namespace device_sync {
namespace {
// Helper function for SetSoftwareFeatureState and SetFeatureStatus, sets the
// |software_feature| to the correct |enabled| value for the |device| and
// handles edge cases.
void SetDeviceSoftwareFeatureState(
multidevice::RemoteDevice& device,
multidevice::SoftwareFeature software_feature,
bool enabled) {
multidevice::SoftwareFeatureState new_state =
enabled ? multidevice::SoftwareFeatureState::kEnabled
: multidevice::SoftwareFeatureState::kSupported;
device.software_features[software_feature] = new_state;
if (software_feature != multidevice::SoftwareFeature::kBetterTogetherHost) {
return;
}
static const multidevice::SoftwareFeature
kFeatureUpdatedByPhoneWhenSuiteStateChanged[] = {
multidevice::SoftwareFeature::kSmartLockHost,
multidevice::SoftwareFeature::kInstantTetheringHost,
multidevice::SoftwareFeature::kMessagesForWebHost,
multidevice::SoftwareFeature::kPhoneHubHost,
multidevice::SoftwareFeature::kWifiSyncHost,
};
// Special case: when the Chrome OS device changes the value of the phone's
// kBetterTogetherHost field, the phone updates all other host feature
// values to match the new value. Simulate that interaction.
for (const auto& feature : kFeatureUpdatedByPhoneWhenSuiteStateChanged)
device.software_features[feature] = new_state;
}
} // namespace
StubDeviceSync::StubDeviceSync()
: DeviceSyncBase(),
synced_devices_{multidevice::CreateStubHostPhone(),
multidevice::CreateStubClientComputer()},
local_device_metadata_(multidevice::CreateStubClientComputer()) {}
StubDeviceSync::~StubDeviceSync() = default;
multidevice::RemoteDevice* StubDeviceSync::GetRemoteDevice(
const base::Optional<std::string>& device_public_key,
const base::Optional<std::string>& device_instance_id) {
auto it = std::find_if(synced_devices_.begin(), synced_devices_.end(),
[&](const auto& device) {
if (device_public_key != base::nullopt)
return device.public_key == device_public_key;
if (device_instance_id != base::nullopt)
return device.instance_id == device_instance_id;
return false;
});
if (it == synced_devices_.end()) {
return nullptr;
}
return &(*it);
}
void StubDeviceSync::ForceEnrollmentNow(ForceEnrollmentNowCallback callback) {
std::move(callback).Run(/*success=*/true);
}
void StubDeviceSync::ForceSyncNow(ForceSyncNowCallback callback) {
std::move(callback).Run(/*success=*/true);
}
void StubDeviceSync::GetLocalDeviceMetadata(
GetLocalDeviceMetadataCallback callback) {
std::move(callback).Run(local_device_metadata_);
}
void StubDeviceSync::GetSyncedDevices(GetSyncedDevicesCallback callback) {
std::move(callback).Run(synced_devices_);
}
void StubDeviceSync::SetSoftwareFeatureState(
const std::string& device_public_key,
multidevice::SoftwareFeature software_feature,
bool enabled,
bool is_exclusive,
SetSoftwareFeatureStateCallback callback) {
multidevice::RemoteDevice* device =
GetRemoteDevice(device_public_key, /*device_instance_id=*/base::nullopt);
if (device == nullptr) {
std::move(callback).Run(
/*result=*/mojom::NetworkRequestResult::kBadRequest);
return;
}
// Once software feature set for the appropriate device, return success.
SetDeviceSoftwareFeatureState(*device, software_feature, enabled);
NotifyOnNewDevicesSynced();
std::move(callback).Run(/*result=*/mojom::NetworkRequestResult::kSuccess);
}
void StubDeviceSync::SetFeatureStatus(const std::string& device_instance_id,
multidevice::SoftwareFeature feature,
FeatureStatusChange status_change,
SetFeatureStatusCallback callback) {
multidevice::RemoteDevice* device =
GetRemoteDevice(/*device_public_key=*/base::nullopt, device_instance_id);
if (device == nullptr) {
std::move(callback).Run(
/*result=*/mojom::NetworkRequestResult::kBadRequest);
return;
}
SetDeviceSoftwareFeatureState(*device, feature,
status_change != FeatureStatusChange::kDisable);
NotifyOnNewDevicesSynced();
std::move(callback).Run(/*result=*/mojom::NetworkRequestResult::kSuccess);
}
void StubDeviceSync::FindEligibleDevices(
multidevice::SoftwareFeature software_feature,
FindEligibleDevicesCallback callback) {
multidevice::RemoteDeviceList eligible_devices;
multidevice::RemoteDeviceList ineligible_devices;
std::move(callback).Run(/*result=*/mojom::NetworkRequestResult::kSuccess,
/*response=*/mojom::FindEligibleDevicesResponse::New(
eligible_devices, ineligible_devices));
}
void StubDeviceSync::NotifyDevices(
const std::vector<std::string>& device_instance_ids,
cryptauthv2::TargetService target_service,
multidevice::SoftwareFeature feature,
NotifyDevicesCallback callback) {
std::move(callback).Run(/*result=*/mojom::NetworkRequestResult::kSuccess);
}
void StubDeviceSync::GetDebugInfo(GetDebugInfoCallback callback) {
// Arbitrary values.
std::move(callback).Run(mojom::DebugInfo::New(
/*last_enrollment_time=*/base::Time::Now(),
/*time_to_next_enrollment_attempt=*/base::TimeDelta::FromMilliseconds(10),
/*is_recovering_from_enrollment_failure=*/false,
/*is_enrollment_in_progress=*/false,
/*last_sync_time=*/base::Time::Now(),
/*time_to_next_sync_attempt=*/base::TimeDelta::FromMilliseconds(10),
/*is_recovering_from_sync_failure=*/false,
/*is_sync_in_progress=*/false));
}
void StubDeviceSync::GetDevicesActivityStatus(
GetDevicesActivityStatusCallback callback) {
std::move(callback).Run(/*result=*/mojom::NetworkRequestResult::kSuccess,
/*response=*/base::nullopt);
}
} // namespace device_sync
} // namespace chromeos
// 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 CHROMEOS_SERVICES_DEVICE_SYNC_STUB_DEVICE_SYNC_H_
#define CHROMEOS_SERVICES_DEVICE_SYNC_STUB_DEVICE_SYNC_H_
#include <vector>
#include "chromeos/components/multidevice/remote_device.h"
#include "chromeos/services/device_sync/device_sync_base.h"
namespace chromeos {
namespace device_sync {
// Stub Device Sync implementation for Linux CrOS build. Creates two
// fake devices, a fake phone and a fake computer.
class StubDeviceSync : public DeviceSyncBase {
public:
StubDeviceSync();
~StubDeviceSync() override;
protected:
// mojom::DeviceSync:
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,
multidevice::SoftwareFeature software_feature,
bool enabled,
bool is_exclusive,
SetSoftwareFeatureStateCallback callback) override;
void SetFeatureStatus(const std::string& device_instance_id,
multidevice::SoftwareFeature feature,
FeatureStatusChange status_change,
SetFeatureStatusCallback callback) override;
void FindEligibleDevices(multidevice::SoftwareFeature software_feature,
FindEligibleDevicesCallback callback) override;
void NotifyDevices(const std::vector<std::string>& device_instance_ids,
cryptauthv2::TargetService target_service,
multidevice::SoftwareFeature feature,
NotifyDevicesCallback callback) override;
void GetDebugInfo(GetDebugInfoCallback callback) override;
void GetDevicesActivityStatus(
GetDevicesActivityStatusCallback callback) override;
private:
// Returns the synced device that has a matching |device_public_key| or a
// matching |device_instance_id|, otherwise returns nullptr.
multidevice::RemoteDevice* GetRemoteDevice(
const base::Optional<std::string>& device_public_key,
const base::Optional<std::string>& device_instance_id);
std::vector<multidevice::RemoteDevice> synced_devices_;
base::Optional<multidevice::RemoteDevice> local_device_metadata_;
};
} // namespace device_sync
} // namespace chromeos
#endif // CHROMEOS_SERVICES_DEVICE_SYNC_STUB_DEVICE_SYNC_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