wifi_sync_notification_controller.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright 2021 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/services/multidevice_setup/wifi_sync_notification_controller.h"
  5. #include <memory>
  6. #include "ash/components/multidevice/logging/logging.h"
  7. #include "ash/components/multidevice/remote_device_ref.h"
  8. #include "ash/components/multidevice/software_feature.h"
  9. #include "ash/components/multidevice/software_feature_state.h"
  10. #include "ash/services/device_sync/public/cpp/device_sync_client.h"
  11. #include "ash/services/multidevice_setup/account_status_change_delegate_notifier.h"
  12. #include "ash/services/multidevice_setup/global_state_feature_manager.h"
  13. #include "ash/services/multidevice_setup/host_status_provider.h"
  14. #include "ash/services/multidevice_setup/public/cpp/prefs.h"
  15. #include "ash/services/multidevice_setup/public/mojom/multidevice_setup.mojom.h"
  16. #include "base/memory/ptr_util.h"
  17. #include "base/power_monitor/power_monitor.h"
  18. #include "components/prefs/pref_registry_simple.h"
  19. #include "components/prefs/pref_service.h"
  20. #include "components/session_manager/core/session_manager.h"
  21. #include "third_party/abseil-cpp/absl/types/optional.h"
  22. namespace ash {
  23. namespace multidevice_setup {
  24. const char kCanShowWifiSyncAnnouncementPrefName[] =
  25. "multidevice_setup.can_show_wifi_sync_announcement";
  26. // static
  27. WifiSyncNotificationController::Factory*
  28. WifiSyncNotificationController::Factory::test_factory_ = nullptr;
  29. // static
  30. std::unique_ptr<WifiSyncNotificationController>
  31. WifiSyncNotificationController::Factory::Create(
  32. GlobalStateFeatureManager* wifi_sync_feature_manager,
  33. HostStatusProvider* host_status_provider,
  34. PrefService* pref_service,
  35. device_sync::DeviceSyncClient* device_sync_client,
  36. AccountStatusChangeDelegateNotifier* delegate_notifier) {
  37. if (test_factory_) {
  38. return test_factory_->CreateInstance(wifi_sync_feature_manager,
  39. host_status_provider, pref_service,
  40. device_sync_client, delegate_notifier);
  41. }
  42. return base::WrapUnique(new WifiSyncNotificationController(
  43. wifi_sync_feature_manager, host_status_provider, pref_service,
  44. device_sync_client, delegate_notifier));
  45. }
  46. // static
  47. void WifiSyncNotificationController::Factory::SetFactoryForTesting(
  48. Factory* test_factory) {
  49. test_factory_ = test_factory;
  50. }
  51. WifiSyncNotificationController::Factory::~Factory() = default;
  52. void WifiSyncNotificationController::RegisterPrefs(
  53. PrefRegistrySimple* registry) {
  54. registry->RegisterBooleanPref(kCanShowWifiSyncAnnouncementPrefName, true);
  55. }
  56. WifiSyncNotificationController::WifiSyncNotificationController(
  57. GlobalStateFeatureManager* wifi_sync_feature_manager,
  58. HostStatusProvider* host_status_provider,
  59. PrefService* pref_service,
  60. device_sync::DeviceSyncClient* device_sync_client,
  61. AccountStatusChangeDelegateNotifier* delegate_notifier)
  62. : wifi_sync_feature_manager_(wifi_sync_feature_manager),
  63. host_status_provider_(host_status_provider),
  64. pref_service_(pref_service),
  65. device_sync_client_(device_sync_client),
  66. delegate_notifier_(delegate_notifier) {
  67. if (pref_service_->GetBoolean(kCanShowWifiSyncAnnouncementPrefName)) {
  68. session_manager::SessionManager::Get()->AddObserver(this);
  69. base::PowerMonitor::AddPowerSuspendObserver(this);
  70. did_register_session_observers_ = true;
  71. }
  72. }
  73. WifiSyncNotificationController::~WifiSyncNotificationController() {
  74. if (did_register_session_observers_) {
  75. session_manager::SessionManager::Get()->RemoveObserver(this);
  76. base::PowerMonitor::RemovePowerSuspendObserver(this);
  77. }
  78. }
  79. void WifiSyncNotificationController::OnSessionStateChanged() {
  80. ShowAnnouncementNotificationIfEligible();
  81. }
  82. void WifiSyncNotificationController::OnResume() {
  83. ShowAnnouncementNotificationIfEligible();
  84. }
  85. void WifiSyncNotificationController::ShowAnnouncementNotificationIfEligible() {
  86. // Show the announcement notification when the device is unlocked and
  87. // eligible for wi-fi sync. This is done on unlock/resume to avoid showing
  88. // it on the first sign-in when it would distract from showoff and other
  89. // announcements.
  90. if (session_manager::SessionManager::Get()->IsUserSessionBlocked()) {
  91. return;
  92. }
  93. if (!IsFeatureAllowed(mojom::Feature::kWifiSync, pref_service_)) {
  94. return;
  95. }
  96. if (!pref_service_->GetBoolean(kCanShowWifiSyncAnnouncementPrefName)) {
  97. return;
  98. }
  99. if (!IsWifiSyncSupported()) {
  100. return;
  101. }
  102. if (host_status_provider_->GetHostWithStatus().host_status() !=
  103. mojom::HostStatus::kHostVerified ||
  104. wifi_sync_feature_manager_->IsFeatureEnabled()) {
  105. pref_service_->SetBoolean(kCanShowWifiSyncAnnouncementPrefName, false);
  106. return;
  107. }
  108. if (!delegate_notifier_->delegate()) {
  109. return;
  110. }
  111. delegate_notifier_->delegate()->OnBecameEligibleForWifiSync();
  112. pref_service_->SetBoolean(kCanShowWifiSyncAnnouncementPrefName, false);
  113. }
  114. bool WifiSyncNotificationController::IsWifiSyncSupported() {
  115. HostStatusProvider::HostStatusWithDevice host_with_status =
  116. host_status_provider_->GetHostWithStatus();
  117. if (host_with_status.host_status() != mojom::HostStatus::kHostVerified) {
  118. return false;
  119. }
  120. absl::optional<multidevice::RemoteDeviceRef> host_device =
  121. host_with_status.host_device();
  122. if (!host_device) {
  123. PA_LOG(ERROR) << "WifiSyncNotificationController::" << __func__
  124. << ": Host device unexpectedly null.";
  125. return false;
  126. }
  127. if (host_device->GetSoftwareFeatureState(
  128. multidevice::SoftwareFeature::kWifiSyncHost) ==
  129. multidevice::SoftwareFeatureState::kNotSupported) {
  130. return false;
  131. }
  132. absl::optional<multidevice::RemoteDeviceRef> local_device =
  133. device_sync_client_->GetLocalDeviceMetadata();
  134. if (!local_device) {
  135. PA_LOG(ERROR) << "WifiSyncNotificationController::" << __func__
  136. << ": Local device unexpectedly null.";
  137. return false;
  138. }
  139. if (local_device->GetSoftwareFeatureState(
  140. multidevice::SoftwareFeature::kWifiSyncClient) ==
  141. multidevice::SoftwareFeatureState::kNotSupported) {
  142. return false;
  143. }
  144. return true;
  145. }
  146. } // namespace multidevice_setup
  147. } // namespace ash