cros_state_sender.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #include "ash/components/phonehub/cros_state_sender.h"
  5. #include "ash/components/multidevice/logging/logging.h"
  6. #include "ash/components/phonehub/message_sender.h"
  7. #include "ash/components/phonehub/phone_model.h"
  8. #include "ash/services/multidevice_setup/public/mojom/multidevice_setup.mojom.h"
  9. namespace ash {
  10. namespace phonehub {
  11. namespace {
  12. using multidevice_setup::mojom::Feature;
  13. using multidevice_setup::mojom::FeatureState;
  14. // The minimum time to wait before checking whether the phone has responded to
  15. // status messages sent by CrosStateSender, and re-sending the status messages
  16. // if there was no response (no phone status model exists).
  17. constexpr base::TimeDelta kMinimumRetryDelay = base::Seconds(15u);
  18. // The amount the previous delay is multiplied by to determine the new amount
  19. // of time to wait before determining whether CrosStateSender should resend the
  20. // CrOS State. Follows a doubling sequence, e.g 2 sec, 4 sec, 8 sec... etc.
  21. constexpr int kRetryDelayMultiplier = 2;
  22. } // namespace
  23. CrosStateSender::CrosStateSender(
  24. MessageSender* message_sender,
  25. secure_channel::ConnectionManager* connection_manager,
  26. multidevice_setup::MultiDeviceSetupClient* multidevice_setup_client,
  27. PhoneModel* phone_model)
  28. : CrosStateSender(message_sender,
  29. connection_manager,
  30. multidevice_setup_client,
  31. phone_model,
  32. std::make_unique<base::OneShotTimer>()) {}
  33. CrosStateSender::CrosStateSender(
  34. MessageSender* message_sender,
  35. secure_channel::ConnectionManager* connection_manager,
  36. multidevice_setup::MultiDeviceSetupClient* multidevice_setup_client,
  37. PhoneModel* phone_model,
  38. std::unique_ptr<base::OneShotTimer> timer)
  39. : message_sender_(message_sender),
  40. connection_manager_(connection_manager),
  41. multidevice_setup_client_(multidevice_setup_client),
  42. phone_model_(phone_model),
  43. retry_timer_(std::move(timer)),
  44. retry_delay_(kMinimumRetryDelay) {
  45. DCHECK(message_sender_);
  46. DCHECK(connection_manager_);
  47. DCHECK(multidevice_setup_client_);
  48. DCHECK(phone_model_);
  49. DCHECK(retry_timer_);
  50. connection_manager_->AddObserver(this);
  51. multidevice_setup_client_->AddObserver(this);
  52. }
  53. CrosStateSender::~CrosStateSender() {
  54. connection_manager_->RemoveObserver(this);
  55. multidevice_setup_client_->RemoveObserver(this);
  56. }
  57. void CrosStateSender::AttemptUpdateCrosState() {
  58. // Stop and cancel old timer if it is running, and reset the |retry_delay_| to
  59. // |kMinimumRetryDelay|.
  60. retry_timer_->Stop();
  61. retry_delay_ = kMinimumRetryDelay;
  62. // Wait for connection to be established.
  63. if (connection_manager_->GetStatus() !=
  64. secure_channel::ConnectionManager::Status::kConnected) {
  65. PA_LOG(VERBOSE) << "Could not start AttemptUpdateCrosState() because "
  66. << "connection manager status is: "
  67. << connection_manager_->GetStatus();
  68. return;
  69. }
  70. PerformUpdateCrosState();
  71. }
  72. void CrosStateSender::PerformUpdateCrosState() {
  73. bool are_notifications_enabled =
  74. multidevice_setup_client_->GetFeatureState(
  75. Feature::kPhoneHubNotifications) == FeatureState::kEnabledByUser;
  76. bool is_camera_roll_enabled =
  77. multidevice_setup_client_->GetFeatureState(
  78. Feature::kPhoneHubCameraRoll) == FeatureState::kEnabledByUser;
  79. PA_LOG(INFO) << "Attempting to send cros state with notifications enabled "
  80. << "state as: " << are_notifications_enabled
  81. << " and camera roll enabled state as: "
  82. << is_camera_roll_enabled;
  83. message_sender_->SendCrosState(are_notifications_enabled,
  84. is_camera_roll_enabled);
  85. retry_timer_->Start(FROM_HERE, retry_delay_,
  86. base::BindOnce(&CrosStateSender::OnRetryTimerFired,
  87. base::Unretained(this)));
  88. }
  89. void CrosStateSender::OnRetryTimerFired() {
  90. // If the phone status model is non-null, implying that the phone has
  91. // responded to the previous PerformUpdateCrosState(), or if the
  92. // connection status is no longer in the connected state, do not
  93. // retry sending the cros state.
  94. if (phone_model_->phone_status_model().has_value() ||
  95. connection_manager_->GetStatus() !=
  96. secure_channel::ConnectionManager::Status::kConnected) {
  97. return;
  98. }
  99. retry_delay_ *= kRetryDelayMultiplier;
  100. PerformUpdateCrosState();
  101. }
  102. void CrosStateSender::OnConnectionStatusChanged() {
  103. AttemptUpdateCrosState();
  104. }
  105. void CrosStateSender::OnFeatureStatesChanged(
  106. const multidevice_setup::MultiDeviceSetupClient::FeatureStatesMap&
  107. feature_states_map) {
  108. AttemptUpdateCrosState();
  109. }
  110. } // namespace phonehub
  111. } // namespace ash