grandfathered_easy_unlock_host_disabler.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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_GRANDFATHERED_EASY_UNLOCK_HOST_DISABLER_H_
  5. #define ASH_SERVICES_MULTIDEVICE_SETUP_GRANDFATHERED_EASY_UNLOCK_HOST_DISABLER_H_
  6. #include <memory>
  7. #include "ash/services/device_sync/public/mojom/device_sync.mojom.h"
  8. #include "ash/services/multidevice_setup/host_backend_delegate.h"
  9. #include "base/memory/weak_ptr.h"
  10. class PrefRegistrySimple;
  11. class PrefService;
  12. namespace base {
  13. class OneShotTimer;
  14. } // namespace base
  15. namespace ash {
  16. namespace device_sync {
  17. class DeviceSyncClient;
  18. }
  19. namespace multidevice_setup {
  20. // This class watches for BETTER_TOGETHER_HOST to be disabled on a device and
  21. // reacts by also disabling EASY_UNLOCK_HOST on that device. See
  22. // https://crbug.com/881612.
  23. //
  24. // The flow of the class is as follows:
  25. //
  26. // Constructor --------------+
  27. // |
  28. // V (no host to disable)
  29. // OnHostChangedOnBackend -->+---> DisableEasyUnlockHostIfNecessary ------> Done
  30. // ^ | ^
  31. // | | |
  32. // (retry timer) | | |
  33. // | V (success) |
  34. // +--- OnDisableEasyUnlockResult ------------------+
  35. class GrandfatheredEasyUnlockHostDisabler
  36. : public HostBackendDelegate::Observer {
  37. public:
  38. class Factory {
  39. public:
  40. static std::unique_ptr<GrandfatheredEasyUnlockHostDisabler> Create(
  41. HostBackendDelegate* host_backend_delegate,
  42. device_sync::DeviceSyncClient* device_sync_client,
  43. PrefService* pref_service,
  44. std::unique_ptr<base::OneShotTimer> timer =
  45. std::make_unique<base::OneShotTimer>());
  46. static void SetFactoryForTesting(Factory* test_factory);
  47. protected:
  48. virtual ~Factory();
  49. virtual std::unique_ptr<GrandfatheredEasyUnlockHostDisabler> CreateInstance(
  50. HostBackendDelegate* host_backend_delegate,
  51. device_sync::DeviceSyncClient* device_sync_client,
  52. PrefService* pref_service,
  53. std::unique_ptr<base::OneShotTimer> timer) = 0;
  54. private:
  55. static Factory* test_factory_;
  56. };
  57. static void RegisterPrefs(PrefRegistrySimple* registry);
  58. GrandfatheredEasyUnlockHostDisabler(
  59. const GrandfatheredEasyUnlockHostDisabler&) = delete;
  60. GrandfatheredEasyUnlockHostDisabler& operator=(
  61. const GrandfatheredEasyUnlockHostDisabler&) = delete;
  62. ~GrandfatheredEasyUnlockHostDisabler() override;
  63. private:
  64. GrandfatheredEasyUnlockHostDisabler(
  65. HostBackendDelegate* host_backend_delegate,
  66. device_sync::DeviceSyncClient* device_sync_client,
  67. PrefService* pref_service,
  68. std::unique_ptr<base::OneShotTimer> timer);
  69. // HostBackendDelegate::Observer:
  70. void OnHostChangedOnBackend() override;
  71. void DisableEasyUnlockHostIfNecessary();
  72. void OnDisableEasyUnlockHostResult(
  73. multidevice::RemoteDeviceRef device,
  74. device_sync::mojom::NetworkRequestResult result_code);
  75. void SetPotentialEasyUnlockHostToDisable(
  76. absl::optional<multidevice::RemoteDeviceRef> device);
  77. absl::optional<multidevice::RemoteDeviceRef> GetEasyUnlockHostToDisable();
  78. HostBackendDelegate* host_backend_delegate_;
  79. device_sync::DeviceSyncClient* device_sync_client_;
  80. PrefService* pref_service_;
  81. std::unique_ptr<base::OneShotTimer> timer_;
  82. absl::optional<multidevice::RemoteDeviceRef> current_better_together_host_;
  83. base::WeakPtrFactory<GrandfatheredEasyUnlockHostDisabler> weak_ptr_factory_{
  84. this};
  85. };
  86. } // namespace multidevice_setup
  87. } // namespace ash
  88. #endif // ASH_SERVICES_MULTIDEVICE_SETUP_GRANDFATHERED_EASY_UNLOCK_HOST_DISABLER_H_