host_backend_delegate_impl.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ASH_SERVICES_MULTIDEVICE_SETUP_HOST_BACKEND_DELEGATE_IMPL_H_
  5. #define ASH_SERVICES_MULTIDEVICE_SETUP_HOST_BACKEND_DELEGATE_IMPL_H_
  6. #include "ash/components/multidevice/remote_device_ref.h"
  7. #include "ash/services/device_sync/public/cpp/device_sync_client.h"
  8. #include "ash/services/multidevice_setup/host_backend_delegate.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/timer/timer.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. class PrefRegistrySimple;
  13. class PrefService;
  14. namespace ash {
  15. namespace multidevice_setup {
  16. class EligibleHostDevicesProvider;
  17. // Concrete HostBackendDelegate implementation, which utilizes
  18. // DeviceSyncClient to communicate with the back-end.
  19. class HostBackendDelegateImpl : public HostBackendDelegate,
  20. public device_sync::DeviceSyncClient::Observer {
  21. public:
  22. class Factory {
  23. public:
  24. static std::unique_ptr<HostBackendDelegate> Create(
  25. EligibleHostDevicesProvider* eligible_host_devices_provider,
  26. PrefService* pref_service,
  27. device_sync::DeviceSyncClient* device_sync_client,
  28. std::unique_ptr<base::OneShotTimer> timer =
  29. std::make_unique<base::OneShotTimer>());
  30. static void SetFactoryForTesting(Factory* test_factory);
  31. protected:
  32. virtual ~Factory();
  33. virtual std::unique_ptr<HostBackendDelegate> CreateInstance(
  34. EligibleHostDevicesProvider* eligible_host_devices_provider,
  35. PrefService* pref_service,
  36. device_sync::DeviceSyncClient* device_sync_client,
  37. std::unique_ptr<base::OneShotTimer> timer) = 0;
  38. private:
  39. static Factory* test_factory_;
  40. };
  41. static void RegisterPrefs(PrefRegistrySimple* registry);
  42. HostBackendDelegateImpl(const HostBackendDelegateImpl&) = delete;
  43. HostBackendDelegateImpl& operator=(const HostBackendDelegateImpl&) = delete;
  44. ~HostBackendDelegateImpl() override;
  45. private:
  46. HostBackendDelegateImpl(
  47. EligibleHostDevicesProvider* eligible_host_devices_provider,
  48. PrefService* pref_service,
  49. device_sync::DeviceSyncClient* device_sync_client,
  50. std::unique_ptr<base::OneShotTimer> timer);
  51. // HostBackendDelegate:
  52. void AttemptToSetMultiDeviceHostOnBackend(
  53. const absl::optional<multidevice::RemoteDeviceRef>& host_device) override;
  54. bool HasPendingHostRequest() override;
  55. absl::optional<multidevice::RemoteDeviceRef> GetPendingHostRequest()
  56. const override;
  57. absl::optional<multidevice::RemoteDeviceRef> GetMultiDeviceHostFromBackend()
  58. const override;
  59. // DeviceSyncClient::Observer:
  60. void OnNewDevicesSynced() override;
  61. bool IsHostEligible(const multidevice::RemoteDeviceRef& provided_host);
  62. // Sets the pending host request. To signal that the request is to remove the
  63. // current host, pass kPendingRemovalOfCurrentHost. To signal that there is no
  64. // pending request, pass kNoPendingRequest.
  65. void SetPendingHostRequest(const std::string& pending_host_id);
  66. // Returns the device with either an Instance ID or encoded public key of |id|
  67. // in the list of synced devices. If no such device exists, returns null.
  68. // TODO(https://crbug.com/1019206): When v1 DeviceSync is disabled, only look
  69. // up by Instance ID since all devices are guaranteed to have one.
  70. absl::optional<multidevice::RemoteDeviceRef> FindDeviceById(
  71. const std::string& id) const;
  72. void AttemptNetworkRequest(bool is_retry);
  73. absl::optional<multidevice::RemoteDeviceRef> GetHostFromDeviceSync();
  74. void OnSetHostNetworkRequestFinished(
  75. multidevice::RemoteDeviceRef device_for_request,
  76. bool attempted_to_enable,
  77. device_sync::mojom::NetworkRequestResult result_code);
  78. EligibleHostDevicesProvider* eligible_host_devices_provider_;
  79. PrefService* pref_service_;
  80. device_sync::DeviceSyncClient* device_sync_client_;
  81. std::unique_ptr<base::OneShotTimer> timer_;
  82. // The most-recent snapshot of the host on the back-end.
  83. absl::optional<multidevice::RemoteDeviceRef> host_from_last_sync_;
  84. base::WeakPtrFactory<HostBackendDelegateImpl> weak_ptr_factory_{this};
  85. };
  86. } // namespace multidevice_setup
  87. } // namespace ash
  88. #endif // ASH_SERVICES_MULTIDEVICE_SETUP_HOST_BACKEND_DELEGATE_IMPL_H_