ota_activator_impl.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2019 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_CELLULAR_SETUP_OTA_ACTIVATOR_IMPL_H_
  5. #define ASH_SERVICES_CELLULAR_SETUP_OTA_ACTIVATOR_IMPL_H_
  6. #include <memory>
  7. #include <ostream>
  8. #include "ash/services/cellular_setup/ota_activator.h"
  9. #include "ash/services/cellular_setup/public/mojom/cellular_setup.mojom.h"
  10. #include "base/callback_forward.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/threading/thread_task_runner_handle.h"
  13. #include "base/time/time.h"
  14. #include "base/timer/timer.h"
  15. #include "chromeos/ash/components/network/network_state_handler_observer.h"
  16. #include "mojo/public/cpp/bindings/pending_remote.h"
  17. #include "mojo/public/cpp/bindings/remote.h"
  18. namespace ash {
  19. class NetworkState;
  20. class NetworkStateHandler;
  21. class NetworkActivationHandler;
  22. class NetworkConnectionHandler;
  23. namespace cellular_setup {
  24. // Concrete OtaActivator implementation. This class activates a SIM using the
  25. // following steps:
  26. // (1) Find a valid SIM in the device. In this context, a SIM is only valid if
  27. // it is present in the machine and has an associated carrier, MEID, IMEI,
  28. // and MDN. If a valid SIM is not present, this class reboots the modem to
  29. // see if the SIM can be detected after a restart.
  30. // (2) Ensure an eligible cellular connection is active. In this context, a
  31. // cellular network is only eligible for activation if it has associated
  32. // payment metadata which can be provided to the carrier portal. If such
  33. // a network is available, this class connects to that network.
  34. // (3) Wait for carrier payment to complete. This class impelments
  35. // CarrierPortalHandler to receive updates about the payment status.
  36. // (4) Complete activation via Shill.
  37. class OtaActivatorImpl : public OtaActivator,
  38. public NetworkStateHandlerObserver {
  39. public:
  40. class Factory {
  41. public:
  42. static std::unique_ptr<OtaActivator> Create(
  43. mojo::PendingRemote<mojom::ActivationDelegate> activation_delegate,
  44. base::OnceClosure on_finished_callback,
  45. NetworkStateHandler* network_state_handler,
  46. NetworkConnectionHandler* network_connection_handler,
  47. NetworkActivationHandler* network_activation_handler,
  48. scoped_refptr<base::TaskRunner> task_runner =
  49. base::ThreadTaskRunnerHandle::Get());
  50. static void SetFactoryForTesting(Factory* test_factory);
  51. protected:
  52. virtual ~Factory();
  53. virtual std::unique_ptr<OtaActivator> CreateInstance(
  54. mojo::PendingRemote<mojom::ActivationDelegate> activation_delegate,
  55. base::OnceClosure on_finished_callback,
  56. NetworkStateHandler* network_state_handler,
  57. NetworkConnectionHandler* network_connection_handler,
  58. NetworkActivationHandler* network_activation_handler,
  59. scoped_refptr<base::TaskRunner> task_runner) = 0;
  60. };
  61. OtaActivatorImpl(const OtaActivatorImpl&) = delete;
  62. OtaActivatorImpl& operator=(const OtaActivatorImpl&) = delete;
  63. ~OtaActivatorImpl() override;
  64. private:
  65. // Delay for first connection retry attempt. Delay doubles for every
  66. // subsequent attempt.
  67. static const base::TimeDelta kConnectRetryDelay;
  68. // Maximum number of connection retry attempts.
  69. static const size_t kMaxConnectRetryAttempt;
  70. friend class CellularSetupOtaActivatorImplTest;
  71. enum class State {
  72. kNotYetStarted,
  73. kWaitingForValidSimToBecomePresent,
  74. kWaitingForCellularConnection,
  75. kWaitingForCellularPayment,
  76. kWaitingForActivation,
  77. kFinished
  78. };
  79. friend std::ostream& operator<<(std::ostream& stream, const State& state);
  80. OtaActivatorImpl(
  81. mojo::PendingRemote<mojom::ActivationDelegate> activation_delegate,
  82. base::OnceClosure on_finished_callback,
  83. NetworkStateHandler* network_state_handler,
  84. NetworkConnectionHandler* network_connection_handler,
  85. NetworkActivationHandler* network_activation_handler,
  86. scoped_refptr<base::TaskRunner> task_runner);
  87. // mojom::CarrierPortalHandler:
  88. void OnCarrierPortalStatusChange(mojom::CarrierPortalStatus status) override;
  89. // NetworkStateHandlerObserver:
  90. void NetworkListChanged() override;
  91. void DeviceListChanged() override;
  92. void NetworkPropertiesUpdated(const NetworkState* network) override;
  93. void DevicePropertiesUpdated(const DeviceState* device) override;
  94. void OnShuttingDown() override;
  95. const DeviceState* GetCellularDeviceState() const;
  96. const NetworkState* GetCellularNetworkState() const;
  97. void StartActivation();
  98. void ChangeStateAndAttemptNextStep(State state);
  99. void AttemptNextActivationStep();
  100. void FinishActivationAttempt(mojom::ActivationResult activation_result);
  101. void AttemptToDiscoverSim();
  102. void AttemptConnectionToCellularNetwork();
  103. void AttemptToSendMetadataToDelegate();
  104. void AttemptToCompleteActivation();
  105. void OnCompleteActivationError(const std::string& error_name);
  106. void OnNetworkConnectionError(const std::string& error_name);
  107. void FlushForTesting();
  108. mojo::Remote<mojom::ActivationDelegate> activation_delegate_;
  109. NetworkStateHandler* network_state_handler_;
  110. NetworkConnectionHandler* network_connection_handler_;
  111. NetworkActivationHandler* network_activation_handler_;
  112. State state_ = State::kNotYetStarted;
  113. absl::optional<mojom::CarrierPortalStatus> last_carrier_portal_status_;
  114. std::string iccid_;
  115. bool has_sent_metadata_ = false;
  116. bool has_called_complete_activation_ = false;
  117. base::OneShotTimer connect_retry_timer_;
  118. size_t connect_retry_attempts_ = 0;
  119. base::WeakPtrFactory<OtaActivatorImpl> weak_ptr_factory_{this};
  120. };
  121. std::ostream& operator<<(std::ostream& stream,
  122. const OtaActivatorImpl::State& state);
  123. } // namespace cellular_setup
  124. } // namespace ash
  125. #endif // ASH_SERVICES_CELLULAR_SETUP_OTA_ACTIVATOR_IMPL_H_