multidevice_setup_initializer.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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_MULTIDEVICE_SETUP_INITIALIZER_H_
  5. #define ASH_SERVICES_MULTIDEVICE_SETUP_MULTIDEVICE_SETUP_INITIALIZER_H_
  6. #include <tuple>
  7. #include <utility>
  8. #include <vector>
  9. #include "ash/services/device_sync/public/cpp/device_sync_client.h"
  10. #include "ash/services/multidevice_setup/multidevice_setup_base.h"
  11. #include "ash/services/multidevice_setup/public/mojom/multidevice_setup.mojom.h"
  12. #include "mojo/public/cpp/bindings/pending_remote.h"
  13. class PrefService;
  14. namespace ash {
  15. namespace device_sync {
  16. class GcmDeviceInfoProvider;
  17. }
  18. namespace multidevice_setup {
  19. class AndroidSmsAppHelperDelegate;
  20. class AndroidSmsPairingStateTracker;
  21. class AuthTokenValidator;
  22. class OobeCompletionTracker;
  23. // Initializes the MultiDeviceSetup service. This class is responsible for
  24. // waiting for asynchronous initialization steps to complete before creating
  25. // the concrete implementation of the mojom::MultiDeviceSetup interface.
  26. class MultiDeviceSetupInitializer
  27. : public MultiDeviceSetupBase,
  28. public device_sync::DeviceSyncClient::Observer {
  29. public:
  30. class Factory {
  31. public:
  32. static std::unique_ptr<MultiDeviceSetupBase> Create(
  33. PrefService* pref_service,
  34. device_sync::DeviceSyncClient* device_sync_client,
  35. AuthTokenValidator* auth_token_validator,
  36. OobeCompletionTracker* oobe_completion_tracker,
  37. AndroidSmsAppHelperDelegate* android_sms_app_helper_delegate,
  38. AndroidSmsPairingStateTracker* android_sms_pairing_state_tracker,
  39. const device_sync::GcmDeviceInfoProvider* gcm_device_info_provider,
  40. bool is_secondary_user);
  41. static void SetFactoryForTesting(Factory* test_factory);
  42. protected:
  43. virtual ~Factory();
  44. virtual std::unique_ptr<MultiDeviceSetupBase> CreateInstance(
  45. PrefService* pref_service,
  46. device_sync::DeviceSyncClient* device_sync_client,
  47. AuthTokenValidator* auth_token_validator,
  48. OobeCompletionTracker* oobe_completion_tracker,
  49. AndroidSmsAppHelperDelegate* android_sms_app_helper_delegate,
  50. AndroidSmsPairingStateTracker* android_sms_pairing_state_tracker,
  51. const device_sync::GcmDeviceInfoProvider* gcm_device_info_provider,
  52. bool is_secondary_user) = 0;
  53. private:
  54. static Factory* test_factory_;
  55. };
  56. MultiDeviceSetupInitializer(const MultiDeviceSetupInitializer&) = delete;
  57. MultiDeviceSetupInitializer& operator=(const MultiDeviceSetupInitializer&) =
  58. delete;
  59. ~MultiDeviceSetupInitializer() override;
  60. private:
  61. // Used for both SetHostDevice() and SetHostDeviceWithoutAuthToken().
  62. struct SetHostDeviceArgs {
  63. // For SetHostDevice().
  64. SetHostDeviceArgs(const std::string& host_instance_id_or_legacy_device_id,
  65. const std::string& auth_token,
  66. SetHostDeviceCallback callback);
  67. // For SetHostDeviceWithoutAuthToken().
  68. SetHostDeviceArgs(
  69. const std::string& host_instance_id_or_legacy_device_id,
  70. mojom::PrivilegedHostDeviceSetter::SetHostDeviceCallback callback);
  71. ~SetHostDeviceArgs();
  72. std::string host_instance_id_or_legacy_device_id;
  73. // Null for SetHostDeviceWithoutAuthToken().
  74. absl::optional<std::string> auth_token;
  75. base::OnceCallback<void(bool)> callback;
  76. };
  77. MultiDeviceSetupInitializer(
  78. PrefService* pref_service,
  79. device_sync::DeviceSyncClient* device_sync_client,
  80. AuthTokenValidator* auth_token_validator,
  81. OobeCompletionTracker* oobe_completion_tracker,
  82. AndroidSmsAppHelperDelegate* android_sms_app_helper_delegate,
  83. AndroidSmsPairingStateTracker* android_sms_pairing_state_tracker,
  84. const device_sync::GcmDeviceInfoProvider* gcm_device_info_provider,
  85. bool is_secondary_user);
  86. // mojom::MultiDeviceSetup:
  87. void SetAccountStatusChangeDelegate(
  88. mojo::PendingRemote<mojom::AccountStatusChangeDelegate> delegate)
  89. override;
  90. void AddHostStatusObserver(
  91. mojo::PendingRemote<mojom::HostStatusObserver> observer) override;
  92. void AddFeatureStateObserver(
  93. mojo::PendingRemote<mojom::FeatureStateObserver> observer) override;
  94. void GetEligibleHostDevices(GetEligibleHostDevicesCallback callback) override;
  95. void GetEligibleActiveHostDevices(
  96. GetEligibleActiveHostDevicesCallback callback) override;
  97. void SetHostDevice(const std::string& host_instance_id_or_legacy_device_id,
  98. const std::string& auth_token,
  99. SetHostDeviceCallback callback) override;
  100. void RemoveHostDevice() override;
  101. void GetHostStatus(GetHostStatusCallback callback) override;
  102. void SetFeatureEnabledState(mojom::Feature feature,
  103. bool enabled,
  104. const absl::optional<std::string>& auth_token,
  105. SetFeatureEnabledStateCallback callback) override;
  106. void GetFeatureStates(GetFeatureStatesCallback callback) override;
  107. void RetrySetHostNow(RetrySetHostNowCallback callback) override;
  108. void TriggerEventForDebugging(
  109. mojom::EventTypeForDebugging type,
  110. TriggerEventForDebuggingCallback callback) override;
  111. // MultiDeviceSetupBase:
  112. void SetHostDeviceWithoutAuthToken(
  113. const std::string& host_instance_id_or_legacy_device_id,
  114. mojom::PrivilegedHostDeviceSetter::SetHostDeviceCallback callback)
  115. override;
  116. // device_sync::DeviceSyncClient::Observer:
  117. void OnReady() override;
  118. void InitializeImplementation();
  119. PrefService* pref_service_;
  120. device_sync::DeviceSyncClient* device_sync_client_;
  121. AuthTokenValidator* auth_token_validator_;
  122. OobeCompletionTracker* oobe_completion_tracker_;
  123. AndroidSmsAppHelperDelegate* android_sms_app_helper_delegate_;
  124. AndroidSmsPairingStateTracker* android_sms_pairing_state_tracker_;
  125. const device_sync::GcmDeviceInfoProvider* gcm_device_info_provider_;
  126. bool is_secondary_user_;
  127. std::unique_ptr<MultiDeviceSetupBase> multidevice_setup_impl_;
  128. // If API functions are called before initialization is complete, their
  129. // parameters are cached here. Once asynchronous initialization is complete,
  130. // the parameters are passed to |multidevice_setup_impl_|.
  131. mojo::PendingRemote<mojom::AccountStatusChangeDelegate> pending_delegate_;
  132. std::vector<mojo::PendingRemote<mojom::HostStatusObserver>>
  133. pending_host_status_observers_;
  134. std::vector<mojo::PendingRemote<mojom::FeatureStateObserver>>
  135. pending_feature_state_observers_;
  136. std::vector<GetEligibleHostDevicesCallback> pending_get_eligible_hosts_args_;
  137. std::vector<GetEligibleActiveHostDevicesCallback>
  138. pending_get_eligible_active_hosts_args_;
  139. std::vector<GetHostStatusCallback> pending_get_host_args_;
  140. std::vector<std::tuple<mojom::Feature,
  141. bool,
  142. absl::optional<std::string>,
  143. SetFeatureEnabledStateCallback>>
  144. pending_set_feature_enabled_args_;
  145. std::vector<GetFeatureStatesCallback> pending_get_feature_states_args_;
  146. std::vector<RetrySetHostNowCallback> pending_retry_set_host_args_;
  147. // Special case: for SetHostDevice(), SetHostDeviceWithoutAuthToken(), and
  148. // RemoveHostDevice(), only keep track of the most recent call. Since each
  149. // call to either of these functions overwrites the previous call, only one
  150. // needs to be passed.
  151. absl::optional<SetHostDeviceArgs> pending_set_host_args_;
  152. bool pending_should_remove_host_device_ = false;
  153. };
  154. } // namespace multidevice_setup
  155. } // namespace ash
  156. #endif // ASH_SERVICES_MULTIDEVICE_SETUP_MULTIDEVICE_SETUP_INITIALIZER_H_