managed_sim_lock_notifier.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Copyright 2022 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/managed_sim_lock_notifier.h"
  5. #include "ash/public/cpp/network_config_service.h"
  6. #include "ash/public/cpp/notification_utils.h"
  7. #include "ash/public/cpp/system_tray_client.h"
  8. #include "ash/session/session_controller_impl.h"
  9. #include "ash/shell.h"
  10. #include "ash/strings/grit/ash_strings.h"
  11. #include "ash/system/model/system_tray_model.h"
  12. #include "ash/system/tray/tray_popup_utils.h"
  13. #include "base/bind.h"
  14. #include "chromeos/ash/components/network/cellular_metrics_logger.h"
  15. #include "components/onc/onc_constants.h"
  16. #include "components/session_manager/session_manager_types.h"
  17. #include "ui/base/l10n/l10n_util.h"
  18. #include "ui/message_center/message_center.h"
  19. #include "ui/message_center/public/cpp/message_center_constants.h"
  20. namespace ash {
  21. namespace {
  22. const char kNotifierManagedSimLock[] = "ash.managed-simlock";
  23. chromeos::network_config::mojom::DeviceStatePropertiesPtr
  24. GetCellularDeviceIfExists(
  25. std::vector<chromeos::network_config::mojom::DeviceStatePropertiesPtr>&
  26. devices) {
  27. for (auto& device : devices) {
  28. if (device->type == chromeos::network_config::mojom::NetworkType::kCellular)
  29. return std::move(device);
  30. }
  31. return nullptr;
  32. }
  33. } // namespace
  34. // static
  35. const char ManagedSimLockNotifier::kManagedSimLockNotificationId[] =
  36. "cros_managed_sim_lock_notifier_ids.pin_unlock_device";
  37. ManagedSimLockNotifier::ManagedSimLockNotifier() {
  38. GetNetworkConfigService(
  39. remote_cros_network_config_.BindNewPipeAndPassReceiver());
  40. remote_cros_network_config_->AddObserver(
  41. cros_network_config_observer_receiver_.BindNewPipeAndPassRemote());
  42. Shell::Get()->session_controller()->AddObserver(this);
  43. }
  44. ManagedSimLockNotifier::~ManagedSimLockNotifier() {
  45. Shell::Get()->session_controller()->RemoveObserver(this);
  46. }
  47. void ManagedSimLockNotifier::OnSessionStateChanged(
  48. session_manager::SessionState state) {
  49. if (Shell::Get()->session_controller()->GetSessionState() ==
  50. session_manager::SessionState::ACTIVE) {
  51. CheckGlobalNetworkConfiguration();
  52. }
  53. }
  54. void ManagedSimLockNotifier::OnDeviceStateListChanged() {
  55. remote_cros_network_config_->GetDeviceStateList(
  56. base::BindOnce(&ManagedSimLockNotifier::OnGetDeviceStateList,
  57. weak_ptr_factory_.GetWeakPtr()));
  58. }
  59. void ManagedSimLockNotifier::OnGetDeviceStateList(
  60. std::vector<chromeos::network_config::mojom::DeviceStatePropertiesPtr>
  61. devices) {
  62. chromeos::network_config::mojom::DeviceStatePropertiesPtr cellular_device =
  63. GetCellularDeviceIfExists(devices);
  64. // Remove Notification and reset |primary_iccid_| if no cellular device or
  65. // the cellular device is currently not enabled.
  66. if (!cellular_device ||
  67. cellular_device->device_state !=
  68. chromeos::network_config::mojom::DeviceStateType::kEnabled) {
  69. primary_iccid_.clear();
  70. RemoveNotification();
  71. return;
  72. }
  73. // If the SIM Lock setting is disabled, remove notification.
  74. if (!cellular_device->sim_lock_status->lock_enabled) {
  75. RemoveNotification();
  76. return;
  77. }
  78. // If the primary SIM changes, check if the restrict SIM Lock Global Network
  79. // Configuration is enabled. If it is, identify the primary cellular network,
  80. // and surface the notification if the SIM lock setting is enabled.
  81. for (const auto& sim_info : *cellular_device->sim_infos) {
  82. if (!sim_info->is_primary)
  83. continue;
  84. std::string old_primary_iccid = primary_iccid_;
  85. primary_iccid_ = sim_info->iccid;
  86. if (primary_iccid_ != old_primary_iccid)
  87. CheckGlobalNetworkConfiguration();
  88. return;
  89. }
  90. }
  91. void ManagedSimLockNotifier::OnPoliciesApplied(const std::string& userhash) {
  92. CheckGlobalNetworkConfiguration();
  93. }
  94. void ManagedSimLockNotifier::CheckGlobalNetworkConfiguration() {
  95. remote_cros_network_config_->GetGlobalPolicy(
  96. base::BindOnce(&ManagedSimLockNotifier::OnGetGlobalPolicy,
  97. weak_ptr_factory_.GetWeakPtr()));
  98. }
  99. void ManagedSimLockNotifier::OnGetGlobalPolicy(
  100. chromeos::network_config::mojom::GlobalPolicyPtr global_policy) {
  101. if (!global_policy->allow_cellular_sim_lock) {
  102. MaybeShowNotification();
  103. return;
  104. }
  105. RemoveNotification();
  106. }
  107. void ManagedSimLockNotifier::MaybeShowNotification() {
  108. remote_cros_network_config_->GetNetworkStateList(
  109. chromeos::network_config::mojom::NetworkFilter::New(
  110. chromeos::network_config::mojom::FilterType::kAll,
  111. chromeos::network_config::mojom::NetworkType::kCellular,
  112. chromeos::network_config::mojom::kNoLimit),
  113. base::BindOnce(&ManagedSimLockNotifier::OnCellularNetworksList,
  114. weak_ptr_factory_.GetWeakPtr()));
  115. }
  116. void ManagedSimLockNotifier::OnCellularNetworksList(
  117. std::vector<chromeos::network_config::mojom::NetworkStatePropertiesPtr>
  118. networks) {
  119. // Check if there are any PIN locked pSIM or eSIM networks.
  120. for (auto& network : networks) {
  121. if (network->type_state->get_cellular()->sim_lock_enabled) {
  122. ShowNotification();
  123. if (network->type_state->get_cellular()->sim_locked) {
  124. CellularMetricsLogger::RecordSimLockNotificationLockType(
  125. network->type_state->get_cellular()->sim_lock_type);
  126. }
  127. return;
  128. }
  129. }
  130. RemoveNotification();
  131. }
  132. void ManagedSimLockNotifier::Close(bool by_user) {
  133. if (by_user) {
  134. CellularMetricsLogger::RecordSimLockNotificationEvent(
  135. CellularMetricsLogger::SimLockNotificationEvent::kDismissed);
  136. }
  137. }
  138. void ManagedSimLockNotifier::Click(
  139. const absl::optional<int>& button_index,
  140. const absl::optional<std::u16string>& reply) {
  141. CellularMetricsLogger::RecordSimLockNotificationEvent(
  142. CellularMetricsLogger::SimLockNotificationEvent::kClicked);
  143. // When clicked, open the SIM Unlock dialog in Cellular settings if
  144. // we can open WebUI settings, otherwise do nothing.
  145. if (TrayPopupUtils::CanOpenWebUISettings()) {
  146. Shell::Get()->system_tray_model()->client()->ShowSettingsSimUnlock();
  147. } else {
  148. LOG(WARNING) << "Cannot open Cellular settings since it's not "
  149. "possible to open OS Settings";
  150. }
  151. }
  152. void ManagedSimLockNotifier::ShowNotification() {
  153. std::unique_ptr<message_center::Notification> notification =
  154. ash::CreateSystemNotification(
  155. message_center::NOTIFICATION_TYPE_SIMPLE,
  156. kManagedSimLockNotificationId,
  157. l10n_util::GetStringUTF16(
  158. IDS_ASH_NETWORK_MANAGED_SIM_LOCK_NOTIFICATION_TITLE),
  159. l10n_util::GetStringUTF16(
  160. IDS_ASH_NETWORK_MANAGED_SIM_LOCK_NOTIFICATION_MESSAGE),
  161. /*display_source=*/std::u16string(), GURL(),
  162. message_center::NotifierId(
  163. message_center::NotifierType::SYSTEM_COMPONENT,
  164. kNotifierManagedSimLock,
  165. NotificationCatalogName::kManagedSimLock),
  166. message_center::RichNotificationData(),
  167. base::MakeRefCounted<message_center::ThunkNotificationDelegate>(
  168. weak_ptr_factory_.GetWeakPtr()),
  169. /*small_image=*/gfx::VectorIcon(),
  170. message_center::SystemNotificationWarningLevel::WARNING);
  171. message_center::MessageCenter* message_center =
  172. message_center::MessageCenter::Get();
  173. message_center->AddNotification(std::move(notification));
  174. CellularMetricsLogger::RecordSimLockNotificationEvent(
  175. CellularMetricsLogger::SimLockNotificationEvent::kShown);
  176. }
  177. void ManagedSimLockNotifier::RemoveNotification() {
  178. message_center::MessageCenter* message_center =
  179. message_center::MessageCenter::Get();
  180. message_center->RemoveNotification(kManagedSimLockNotificationId, false);
  181. }
  182. } // namespace ash