cellular_setup_notifier.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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/system/network/cellular_setup_notifier.h"
  5. #include "ash/constants/ash_pref_names.h"
  6. #include "ash/constants/notifier_catalogs.h"
  7. #include "ash/public/cpp/network_config_service.h"
  8. #include "ash/public/cpp/notification_utils.h"
  9. #include "ash/public/cpp/system_tray_client.h"
  10. #include "ash/resources/vector_icons/vector_icons.h"
  11. #include "ash/session/session_controller_impl.h"
  12. #include "ash/shell.h"
  13. #include "ash/strings/grit/ash_strings.h"
  14. #include "ash/system/model/system_tray_model.h"
  15. #include "base/bind.h"
  16. #include "base/timer/timer.h"
  17. #include "components/onc/onc_constants.h"
  18. #include "components/prefs/pref_registry_simple.h"
  19. #include "components/vector_icons/vector_icons.h"
  20. #include "ui/base/l10n/l10n_util.h"
  21. #include "ui/message_center/message_center.h"
  22. #include "ui/message_center/public/cpp/message_center_constants.h"
  23. namespace ash {
  24. namespace {
  25. const char kNotifierCellularSetup[] = "ash.cellular-setup";
  26. // Delay after OOBE until notification should be shown.
  27. constexpr base::TimeDelta kNotificationDelay = base::Minutes(15);
  28. bool DoesCellularDeviceExist(
  29. const std::vector<
  30. chromeos::network_config::mojom::DeviceStatePropertiesPtr>& devices) {
  31. for (const auto& device : devices) {
  32. if (device->type ==
  33. chromeos::network_config::mojom::NetworkType::kCellular) {
  34. return true;
  35. }
  36. }
  37. return false;
  38. }
  39. void OnCellularSetupNotificationClicked() {
  40. Shell::Get()->system_tray_model()->client()->ShowNetworkCreate(
  41. ::onc::network_type::kCellular);
  42. }
  43. // Returns the value of kCanCellularSetupNotificationBeShown for the last active
  44. // user. If the last active user's PrefService is null, returns false.
  45. bool GetCanCellularSetupNotificationBeShown() {
  46. PrefService* prefs =
  47. Shell::Get()->session_controller()->GetLastActiveUserPrefService();
  48. if (!prefs) {
  49. // Return false because we don't want to show the notification if we're
  50. // unsure if it can be shown or not.
  51. return false;
  52. }
  53. return prefs->GetBoolean(prefs::kCanCellularSetupNotificationBeShown);
  54. }
  55. // Sets kCanCellularSetupNotificationBeShown to false for the last active user.
  56. // Returns true if the flag was successfully set, and false if not.
  57. bool SetCellularSetupNotificationCannotBeShown() {
  58. PrefService* prefs =
  59. Shell::Get()->session_controller()->GetLastActiveUserPrefService();
  60. if (!prefs) {
  61. return false;
  62. }
  63. prefs->SetBoolean(prefs::kCanCellularSetupNotificationBeShown, false);
  64. return true;
  65. }
  66. } // namespace
  67. // static
  68. void CellularSetupNotifier::RegisterProfilePrefs(PrefRegistrySimple* registry) {
  69. // Default value is true as we usually want to show the notification except
  70. // for specific conditions (cellular-incapable device, already shown).
  71. registry->RegisterBooleanPref(prefs::kCanCellularSetupNotificationBeShown,
  72. true);
  73. }
  74. // static
  75. const char CellularSetupNotifier::kCellularSetupNotificationId[] =
  76. "cros_cellular_setup_notifier_ids.setup_network";
  77. CellularSetupNotifier::CellularSetupNotifier()
  78. : timer_(std::make_unique<base::OneShotTimer>()) {
  79. GetNetworkConfigService(
  80. remote_cros_network_config_.BindNewPipeAndPassReceiver());
  81. remote_cros_network_config_->AddObserver(
  82. cros_network_config_observer_receiver_.BindNewPipeAndPassRemote());
  83. Shell::Get()->session_controller()->AddObserver(this);
  84. }
  85. CellularSetupNotifier::~CellularSetupNotifier() {
  86. Shell::Get()->session_controller()->RemoveObserver(this);
  87. }
  88. void CellularSetupNotifier::OnSessionStateChanged(
  89. session_manager::SessionState state) {
  90. if (Shell::Get()->session_controller()->GetSessionState() !=
  91. session_manager::SessionState::ACTIVE) {
  92. timer_->Stop();
  93. return;
  94. }
  95. if (!GetCanCellularSetupNotificationBeShown()) {
  96. // The notification has already been shown or there is some condition that
  97. // dictates that the notification shouldn't be shown.
  98. return;
  99. }
  100. // Wait |kNotificationDelay| after the user logs in before attempting to show
  101. // a notification. This allows the user time to set up a cellular network if
  102. // they desire, and it also ensures we don't spam the user with an extra
  103. // notification just after they log into their device for the first time.
  104. timer_->Start(FROM_HERE, kNotificationDelay,
  105. base::BindOnce(&CellularSetupNotifier::OnTimerFired,
  106. base::Unretained(this)));
  107. }
  108. void CellularSetupNotifier::OnTimerFired() {
  109. timer_fired_ = true;
  110. MaybeShowCellularSetupNotification();
  111. }
  112. void CellularSetupNotifier::OnNetworkStateListChanged() {
  113. MaybeShowCellularSetupNotification();
  114. }
  115. void CellularSetupNotifier::OnNetworkStateChanged(
  116. chromeos::network_config::mojom::NetworkStatePropertiesPtr network) {
  117. if (network->type !=
  118. chromeos::network_config::mojom::NetworkType::kCellular ||
  119. network->type_state->get_cellular()->activation_state !=
  120. chromeos::network_config::mojom::ActivationStateType::kActivated) {
  121. return;
  122. }
  123. SetCellularSetupNotificationCannotBeShown();
  124. message_center::MessageCenter* message_center =
  125. message_center::MessageCenter::Get();
  126. message_center->RemoveNotification(kCellularSetupNotificationId, false);
  127. }
  128. void CellularSetupNotifier::MaybeShowCellularSetupNotification() {
  129. remote_cros_network_config_->GetDeviceStateList(base::BindOnce(
  130. &CellularSetupNotifier::OnGetDeviceStateList, base::Unretained(this)));
  131. }
  132. void CellularSetupNotifier::OnGetDeviceStateList(
  133. std::vector<chromeos::network_config::mojom::DeviceStatePropertiesPtr>
  134. devices) {
  135. if (!DoesCellularDeviceExist(devices)) {
  136. // Save to prefs not to show this notification again so we don't keep
  137. // starting the timer for a cellular-incapable device.
  138. SetCellularSetupNotificationCannotBeShown();
  139. return;
  140. }
  141. remote_cros_network_config_->GetNetworkStateList(
  142. chromeos::network_config::mojom::NetworkFilter::New(
  143. chromeos::network_config::mojom::FilterType::kAll,
  144. chromeos::network_config::mojom::NetworkType::kCellular,
  145. chromeos::network_config::mojom::kNoLimit),
  146. base::BindOnce(&CellularSetupNotifier::OnCellularNetworksList,
  147. base::Unretained(this)));
  148. }
  149. void CellularSetupNotifier::OnCellularNetworksList(
  150. std::vector<chromeos::network_config::mojom::NetworkStatePropertiesPtr>
  151. networks) {
  152. // Check if there are any activated pSIM or eSIM networks. The activation
  153. // state property is set to activated for all eSIM services.
  154. for (auto& network : networks) {
  155. if (network->type_state->get_cellular()->activation_state ==
  156. chromeos::network_config::mojom::ActivationStateType::kActivated) {
  157. // Save to prefs not to try to show this notification again so we don't
  158. // keep starting the timer for a user who already has an activated
  159. // cellular network.
  160. SetCellularSetupNotificationCannotBeShown();
  161. message_center::MessageCenter* message_center =
  162. message_center::MessageCenter::Get();
  163. message_center->RemoveNotification(kCellularSetupNotificationId, false);
  164. return;
  165. }
  166. }
  167. // Do not show notification if it has already been shown, or the timer
  168. // has not yet been fired.
  169. if (!GetCanCellularSetupNotificationBeShown() || !timer_fired_) {
  170. return;
  171. }
  172. ShowCellularSetupNotification();
  173. }
  174. // Shows the Cellular Setup notification except in cases where it is unable to
  175. // save that it will have shown the notification.
  176. void CellularSetupNotifier::ShowCellularSetupNotification() {
  177. if (!SetCellularSetupNotificationCannotBeShown()) {
  178. // If we didn't successfully set the flag, don't show the notification or
  179. // else we may show the notification multiple times.
  180. return;
  181. }
  182. std::unique_ptr<message_center::Notification> notification =
  183. ash::CreateSystemNotification(
  184. message_center::NOTIFICATION_TYPE_SIMPLE,
  185. kCellularSetupNotificationId,
  186. l10n_util::GetStringUTF16(
  187. IDS_ASH_NETWORK_CELLULAR_SETUP_NOTIFICATION_TITLE),
  188. l10n_util::GetStringUTF16(
  189. IDS_ASH_NETWORK_CELLULAR_SETUP_NOTIFICATION_MESSAGE),
  190. /*display_source=*/std::u16string(), GURL(),
  191. message_center::NotifierId(
  192. message_center::NotifierType::SYSTEM_COMPONENT,
  193. kNotifierCellularSetup, NotificationCatalogName::kCellularSetup),
  194. message_center::RichNotificationData(),
  195. base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
  196. base::BindRepeating(&OnCellularSetupNotificationClicked)),
  197. kAddCellularNetworkIcon,
  198. message_center::SystemNotificationWarningLevel::NORMAL);
  199. message_center::MessageCenter* message_center =
  200. message_center::MessageCenter::Get();
  201. if (message_center->FindVisibleNotificationById(kCellularSetupNotificationId))
  202. message_center->RemoveNotification(kCellularSetupNotificationId, false);
  203. message_center->AddNotification(std::move(notification));
  204. }
  205. } // namespace ash