Commit b4f91407 authored by Claude van der Merwe's avatar Claude van der Merwe Committed by Commit Bot

Add WifiSyncFeatureManager to multidevice setup

WifiFeatureManager adds global enable/disable functionality for
Wifi Sync V2.

WifiFeatureManager asynchronously enables/disables WIFI_SYNC_HOST
for a synced device on the back-end, and provides retry logic if the
network request fails by storing the last pending request in prefs.

Bug: 1117619
Change-Id: Ide945c00e5eb060dd86b11b31e1a830aa1fde3ed
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2419332
Commit-Queue: Claude van der Merwe <cvandermerwe@google.com>
Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Reviewed-by: default avatarJon Mann <jonmann@chromium.org>
Cr-Commit-Position: refs/heads/master@{#811024}
parent dab8c1ff
...@@ -52,6 +52,9 @@ static_library("multidevice_setup") { ...@@ -52,6 +52,9 @@ static_library("multidevice_setup") {
"privileged_host_device_setter_base.h", "privileged_host_device_setter_base.h",
"privileged_host_device_setter_impl.cc", "privileged_host_device_setter_impl.cc",
"privileged_host_device_setter_impl.h", "privileged_host_device_setter_impl.h",
"wifi_sync_feature_manager.h",
"wifi_sync_feature_manager_impl.cc",
"wifi_sync_feature_manager_impl.h",
] ]
deps = [ deps = [
...@@ -139,6 +142,7 @@ source_set("unit_tests") { ...@@ -139,6 +142,7 @@ source_set("unit_tests") {
"multidevice_setup_impl_unittest.cc", "multidevice_setup_impl_unittest.cc",
"multidevice_setup_service_unittest.cc", "multidevice_setup_service_unittest.cc",
"privileged_host_device_setter_impl_unittest.cc", "privileged_host_device_setter_impl_unittest.cc",
"wifi_sync_feature_manager_impl_unittest.cc",
] ]
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.
#ifndef CHROMEOS_SERVICES_MULTIDEVICE_SETUP_WIFI_SYNC_FEATURE_MANAGER_H_
#define CHROMEOS_SERVICES_MULTIDEVICE_SETUP_WIFI_SYNC_FEATURE_MANAGER_H_
#include "base/callback_forward.h"
#include "base/macros.h"
#include "base/observer_list.h"
#include "base/optional.h"
#include "chromeos/components/multidevice/remote_device_ref.h"
#include "chromeos/services/multidevice_setup/host_status_provider.h"
namespace chromeos {
namespace multidevice_setup {
// Manager for setting and receiving the Wifi Sync Host enabled/disabled state.
// This class is considered the source of truth for the current state of Wifi
// Sync Host.
class WifiSyncFeatureManager {
public:
virtual ~WifiSyncFeatureManager() = default;
WifiSyncFeatureManager(const WifiSyncFeatureManager&) = delete;
WifiSyncFeatureManager& operator=(const WifiSyncFeatureManager&) = delete;
// Attempts to enable/disable Wifi Sync on the backend for the host
// device that is synced at the time SetIsWifiSyncEnabled is called.
virtual void SetIsWifiSyncEnabled(bool enabled) = 0;
// Returns whether Wifi Sync is enabled/disabled.
virtual bool IsWifiSyncEnabled() = 0;
protected:
WifiSyncFeatureManager() = default;
};
} // namespace multidevice_setup
} // namespace chromeos
#endif // CHROMEOS_SERVICES_MULTIDEVICE_SETUP_WIFI_SYNC_FEATURE_MANAGER_H_
\ No newline at end of file
// 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_MULTIDEVICE_SETUP_WIFI_SYNC_FEATURE_MANAGER_IMPL_H_
#define CHROMEOS_SERVICES_MULTIDEVICE_SETUP_WIFI_SYNC_FEATURE_MANAGER_IMPL_H_
#include "base/callback_forward.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/optional.h"
#include "base/timer/timer.h"
#include "chromeos/components/multidevice/remote_device_ref.h"
#include "chromeos/services/device_sync/public/cpp/device_sync_client.h"
#include "chromeos/services/multidevice_setup/wifi_sync_feature_manager.h"
class PrefRegistrySimple;
class PrefService;
namespace chromeos {
namespace multidevice_setup {
// Concrete WifiSyncFeatureManager implementation, which utilizes
// DeviceSyncClient to communicate with the back-end.
//
// This toggles WIFI_SYNC_HOST between enabled/supported on cryptauth for a
// synced phone, where supported is considered disabled by user.
//
// Toggling WIFI_SYNC_HOST is a global action, so it will be reflected on all
// synced devices.
class WifiSyncFeatureManagerImpl
: public WifiSyncFeatureManager,
public HostStatusProvider::Observer,
public device_sync::DeviceSyncClient::Observer {
public:
class Factory {
public:
static std::unique_ptr<WifiSyncFeatureManager> Create(
HostStatusProvider* host_status_provider,
PrefService* pref_service,
device_sync::DeviceSyncClient* device_sync_client,
std::unique_ptr<base::OneShotTimer> timer =
std::make_unique<base::OneShotTimer>());
static void SetFactoryForTesting(Factory* test_factory);
protected:
virtual ~Factory();
virtual std::unique_ptr<WifiSyncFeatureManager> CreateInstance(
HostStatusProvider* host_status_provider,
PrefService* pref_service,
device_sync::DeviceSyncClient* device_sync_client,
std::unique_ptr<base::OneShotTimer> timer) = 0;
private:
static Factory* test_factory_;
};
static void RegisterPrefs(PrefRegistrySimple* registry);
~WifiSyncFeatureManagerImpl() override;
WifiSyncFeatureManagerImpl(const WifiSyncFeatureManagerImpl&) = delete;
WifiSyncFeatureManagerImpl& operator=(const WifiSyncFeatureManagerImpl&) =
delete;
private:
WifiSyncFeatureManagerImpl(HostStatusProvider* host_status_provider,
PrefService* pref_service,
device_sync::DeviceSyncClient* device_sync_client,
std::unique_ptr<base::OneShotTimer> timer);
// HostStatusProvider::Observer,
void OnHostStatusChange(const HostStatusProvider::HostStatusWithDevice&
host_status_with_device) override;
// DeviceSyncClient::Observer:
void OnNewDevicesSynced() override;
// WifiSyncFeatureManager:
// Attempts to enable/disable WIFI_SYNC_HOST on the backend for the host
// device that is synced at the time SetIsWifiSyncEnabled is called.
//
// If a the request fails (e.g., the device is offline or the server is down),
// the OnBackendRequestFailed() observer function is invoked, but this object
// continues to attempt the request until one of the following happens: the
// request succeeds, SetIsWifiSyncEnabled() called is with different value, or
// the synced host device changes.
//
// If there is already a pending request and this function is called with the
// same request, a retry will be attempted immediately.
void SetIsWifiSyncEnabled(bool enabled) override;
// Returns whether WIFI_SYNC_HOST is enabled/disabled. If there is a pending
// request to enable or disable WIFI_SYNC_HOST, the state that the pending
// request is intending to set WIFI_SYNC_HOST to is returned, otherwise the
// state on the back-end is returned.
bool IsWifiSyncEnabled() override;
// Numerical values cannot be changed because they map to integers that are
// stored persistently in prefs.
enum class PendingState {
kPendingNone = 0,
kPendingEnable = 1,
kPendingDisable = 2
};
enum class CurrentState {
kNoVerifiedHost,
kNoPendingRequest,
kPendingMatchesBackend,
kValidPendingRequest
};
void ResetPendingWifiSyncHostNetworkRequest();
PendingState GetPendingState();
CurrentState GetCurrentState();
void SetPendingWifiSyncHostNetworkRequest(PendingState pending_state);
void AttemptSetWifiSyncHostStateNetworkRequest(bool is_retry);
void OnSetWifiSyncHostStateNetworkRequestFinished(
bool attempted_to_enable,
device_sync::mojom::NetworkRequestResult result_code);
HostStatusProvider* host_status_provider_;
PrefService* pref_service_;
device_sync::DeviceSyncClient* device_sync_client_;
std::unique_ptr<base::OneShotTimer> timer_;
bool network_request_in_flight_ = false;
base::WeakPtrFactory<WifiSyncFeatureManagerImpl> weak_ptr_factory_{this};
};
} // namespace multidevice_setup
} // namespace chromeos
#endif // CHROMEOS_SERVICES_MULTIDEVICE_SETUP_WIFI_SYNC_FEATURE_MANAGER_IMPL_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