tether_controller_impl.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright 2020 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_COMPONENTS_PHONEHUB_TETHER_CONTROLLER_IMPL_H_
  5. #define ASH_COMPONENTS_PHONEHUB_TETHER_CONTROLLER_IMPL_H_
  6. #include "ash/components/phonehub/phone_model.h"
  7. #include "ash/components/phonehub/tether_controller.h"
  8. #include "ash/services/multidevice_setup/public/cpp/multidevice_setup_client.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "chromeos/services/network_config/public/cpp/cros_network_config_observer.h"
  11. #include "mojo/public/cpp/bindings/remote.h"
  12. namespace ash {
  13. namespace phonehub {
  14. class UserActionRecorder;
  15. // TetherController implementation which utilizes MultiDeviceSetupClient and
  16. // CrosNetworkConfig in order to interact with Instant Tethering. If Instant
  17. // Tethering is user disabled, AttemptConnection() will first enable the feature
  18. // via the MultiDeviceSetupClient, then scan for an eligible phone via
  19. // CrosNetworkConfig, and finally connect to the phone via CrosNetworkConfig. If
  20. // Instant Tethering is enabled, but there is no visible Tether network,
  21. // AttemptConnection() will first scan for an eligible phone via
  22. // CrosNetworkConfig, and connect to the phone via CrosNetworkConfig. If Instant
  23. // Tethering is enabled and there is a visible Tether Network previously fetched
  24. // from observing CrosNetworkConfig, AttemptConnection() will just connect to
  25. // the phone via CrosNetworkConfig. Disconnect() disconnects the Tether network
  26. // if one exists.
  27. class TetherControllerImpl
  28. : public TetherController,
  29. public PhoneModel::Observer,
  30. public multidevice_setup::MultiDeviceSetupClient::Observer,
  31. public chromeos::network_config::CrosNetworkConfigObserver {
  32. public:
  33. TetherControllerImpl(
  34. PhoneModel* phone_model,
  35. UserActionRecorder* user_action_recorder,
  36. multidevice_setup::MultiDeviceSetupClient* multidevice_setup_client);
  37. ~TetherControllerImpl() override;
  38. // TetherController:
  39. Status GetStatus() const override;
  40. void ScanForAvailableConnection() override;
  41. void AttemptConnection() override;
  42. void Disconnect() override;
  43. private:
  44. friend class TetherControllerImplTest;
  45. // Used to track AttemptConnection() and Disconnect() calls.
  46. enum class ConnectDisconnectStatus {
  47. // No AttemptConnection or Disconnect is in progress. The class still
  48. // observes changes in the Tether network initiated externally (e.g in OS
  49. // Settings), and causes changes to the |status_|.
  50. kIdle = 0,
  51. // Used in AttemptConnection flow. Enabling the InstantTethering feature as
  52. // it was previously disabled.
  53. kTurningOnInstantTethering = 1,
  54. // Used in AttemptConnection flow. Requesting a scan has has no callback, so
  55. // this state is changed upon observing tether network changes or device
  56. // changes. If a visible Tether network is observed, the
  57. // |connect_disconnect_status_| will change to kConnectingToEligiblePhone.
  58. // If a visible Tether network is not observed by the time the Tether device
  59. // stops scanning, the |connect_disconnect_status_| will change back to
  60. // kIdle.
  61. // Note: Calling ScanForAvailableConnection() will not set the
  62. // |connect_disconnect_status_| to this value.
  63. kScanningForEligiblePhone = 2,
  64. // Used in AttemptConnection flow. In the process of connecting to a Tether
  65. // Network.
  66. kConnectingToEligiblePhone = 3,
  67. // Used in Disconnect flow. Disconnects from the tether network.
  68. kDisconnecting = 4,
  69. };
  70. // Connector that uses CrosNetworkConfig to connect, disconnect, and get the
  71. // network state list. This class is used for testing purposes.
  72. class TetherNetworkConnector {
  73. public:
  74. using StartConnectCallback = base::OnceCallback<void(
  75. chromeos::network_config::mojom::StartConnectResult result,
  76. const std::string& message)>;
  77. using StartDisconnectCallback = base::OnceCallback<void(bool)>;
  78. using GetNetworkStateListCallback = base::OnceCallback<void(
  79. std::vector<
  80. chromeos::network_config::mojom::NetworkStatePropertiesPtr>)>;
  81. TetherNetworkConnector();
  82. TetherNetworkConnector(const TetherNetworkConnector&) = delete;
  83. TetherNetworkConnector& operator=(const TetherNetworkConnector&) = delete;
  84. virtual ~TetherNetworkConnector();
  85. virtual void StartConnect(const std::string& guid,
  86. StartConnectCallback callback);
  87. virtual void StartDisconnect(const std::string& guid,
  88. StartDisconnectCallback callback);
  89. virtual void GetNetworkStateList(
  90. chromeos::network_config::mojom::NetworkFilterPtr filter,
  91. GetNetworkStateListCallback callback);
  92. private:
  93. mojo::Remote<chromeos::network_config::mojom::CrosNetworkConfig>
  94. cros_network_config_;
  95. };
  96. // Two parameter constructor made available for testing purposes. The one
  97. // parameter constructor calls this constructor.
  98. TetherControllerImpl(
  99. PhoneModel* phone_model,
  100. UserActionRecorder* user_action_recorder,
  101. multidevice_setup::MultiDeviceSetupClient* multidevice_setup_client,
  102. std::unique_ptr<TetherControllerImpl::TetherNetworkConnector> connector);
  103. // PhoneModel::Observer:
  104. void OnModelChanged() override;
  105. // multidevice_setup::MultiDeviceSetupClient::Observer:
  106. void OnFeatureStatesChanged(
  107. const multidevice_setup::MultiDeviceSetupClient::FeatureStatesMap&
  108. feature_states_map) override;
  109. // CrosNetworkConfigObserver:
  110. void OnActiveNetworksChanged(
  111. std::vector<chromeos::network_config::mojom::NetworkStatePropertiesPtr>
  112. networks) override;
  113. void OnNetworkStateListChanged() override;
  114. void OnDeviceStateListChanged() override;
  115. void AttemptTurningOnTethering();
  116. void OnSetFeatureEnabled(bool success);
  117. void PerformConnectionAttempt();
  118. void StartConnect();
  119. void OnStartConnectCompleted(
  120. chromeos::network_config::mojom::StartConnectResult result,
  121. const std::string& message);
  122. void OnDisconnectCompleted(bool success);
  123. void FetchVisibleTetherNetwork();
  124. void OnGetDeviceStateList(
  125. std::vector<chromeos::network_config::mojom::DeviceStatePropertiesPtr>
  126. devices);
  127. void OnVisibleTetherNetworkFetched(
  128. std::vector<chromeos::network_config::mojom::NetworkStatePropertiesPtr>
  129. networks);
  130. void SetConnectDisconnectStatus(
  131. ConnectDisconnectStatus connect_disconnect_status);
  132. void UpdateStatus();
  133. TetherController::Status ComputeStatus() const;
  134. PhoneModel* phone_model_;
  135. UserActionRecorder* user_action_recorder_;
  136. multidevice_setup::MultiDeviceSetupClient* multidevice_setup_client_;
  137. ConnectDisconnectStatus connect_disconnect_status_ =
  138. ConnectDisconnectStatus::kIdle;
  139. Status status_ = Status::kIneligibleForFeature;
  140. // Whether this class is attempting a tether connection.
  141. bool is_attempting_connection_ = false;
  142. chromeos::network_config::mojom::NetworkStatePropertiesPtr tether_network_;
  143. std::unique_ptr<TetherNetworkConnector> connector_;
  144. mojo::Receiver<chromeos::network_config::mojom::CrosNetworkConfigObserver>
  145. receiver_{this};
  146. mojo::Remote<chromeos::network_config::mojom::CrosNetworkConfig>
  147. cros_network_config_;
  148. base::WeakPtrFactory<TetherControllerImpl> weak_ptr_factory_{this};
  149. };
  150. } // namespace phonehub
  151. } // namespace ash
  152. #endif // ASH_COMPONENTS_PHONEHUB_TETHER_CONTROLLER_IMPL_H_