usb_peripheral_notification_controller.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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/usb_peripheral/usb_peripheral_notification_controller.h"
  5. #include "ash/constants/ash_pref_names.h"
  6. #include "ash/constants/notifier_catalogs.h"
  7. #include "ash/public/cpp/new_window_delegate.h"
  8. #include "ash/public/cpp/notification_utils.h"
  9. #include "ash/resources/vector_icons/vector_icons.h"
  10. #include "ash/session/session_controller_impl.h"
  11. #include "ash/shell.h"
  12. #include "ash/strings/grit/ash_strings.h"
  13. #include "components/prefs/pref_registry_simple.h"
  14. #include "components/prefs/pref_service.h"
  15. #include "third_party/abseil-cpp/absl/types/optional.h"
  16. #include "third_party/cros_system_api/dbus/typecd/dbus-constants.h"
  17. #include "ui/base/l10n/l10n_util.h"
  18. #include "ui/message_center/message_center.h"
  19. #include "ui/message_center/public/cpp/notification.h"
  20. #include "ui/message_center/public/cpp/notification_types.h"
  21. #include "url/gurl.h"
  22. namespace ash {
  23. namespace {
  24. const char kNotifierUsbPeripheral[] = "ash.usb_peripheral";
  25. const char kUsbPeripheralInvalidDpCableNotificationId[] =
  26. "cros_usb_peripheral_invalid_dp_cable_notification_id";
  27. const char kUsbPeripheralInvalidUSB4ValidTBTCableNotificationId[] =
  28. "cros_usb_peripheral_invalid_usb4_valid_tbt_cable_notification_id";
  29. const char kUsbPeripheralInvalidUSB4CableNotificationId[] =
  30. "cros_usb_peripheral_invalid_usb4_cable_notification_id";
  31. const char kUsbPeripheralInvalidTBTCableNotificationId[] =
  32. "cros_usb_peripheral_invalid_tbt_cable_notification_id";
  33. const char kUsbPeripheralSpeedLimitingCableNotificationId[] =
  34. "cros_usb_peripheral_speed_limiting_cable_notification_id";
  35. const char kNotificationDisplayLandingPageUrl[] =
  36. "https://support.google.com/chromebook?p=cable_notification";
  37. const char kNotificationDeviceLandingPageUrl[] =
  38. "https://support.google.com/chromebook?p=cable_notification_2";
  39. bool GetCableSpeedNotificationShownPref() {
  40. PrefService* prefs =
  41. Shell::Get()->session_controller()->GetActivePrefService();
  42. return prefs->GetBoolean(prefs::kUsbPeripheralCableSpeedNotificationShown);
  43. }
  44. void SetCableSpeedNotificationShownPref(bool pref) {
  45. PrefService* prefs =
  46. Shell::Get()->session_controller()->GetActivePrefService();
  47. prefs->SetBoolean(prefs::kUsbPeripheralCableSpeedNotificationShown, pref);
  48. }
  49. bool ShouldDisplayNotification() {
  50. return Shell::Get()->session_controller()->GetSessionState() ==
  51. session_manager::SessionState::ACTIVE &&
  52. !Shell::Get()->session_controller()->IsUserSessionBlocked();
  53. }
  54. void OnCableNotificationClicked(const std::string& notification_id,
  55. const std::string& landing_page,
  56. absl::optional<int> button_index) {
  57. if (notification_id == kUsbPeripheralSpeedLimitingCableNotificationId)
  58. SetCableSpeedNotificationShownPref(true);
  59. if (button_index) {
  60. NewWindowDelegate::GetInstance()->OpenUrl(
  61. GURL(landing_page), NewWindowDelegate::OpenUrlFrom::kUserInteraction,
  62. NewWindowDelegate::Disposition::kNewForegroundTab);
  63. }
  64. message_center::MessageCenter::Get()->RemoveNotification(notification_id,
  65. /*from_user=*/true);
  66. }
  67. } // namespace
  68. UsbPeripheralNotificationController::UsbPeripheralNotificationController(
  69. message_center::MessageCenter* message_center)
  70. : message_center_(message_center) {
  71. DCHECK(message_center_);
  72. }
  73. UsbPeripheralNotificationController::~UsbPeripheralNotificationController() {
  74. if (ash::PeripheralNotificationManager::IsInitialized())
  75. ash::PeripheralNotificationManager::Get()->RemoveObserver(this);
  76. }
  77. // static
  78. void UsbPeripheralNotificationController::RegisterProfilePrefs(
  79. PrefRegistrySimple* registry) {
  80. registry->RegisterBooleanPref(
  81. prefs::kUsbPeripheralCableSpeedNotificationShown, false);
  82. }
  83. void UsbPeripheralNotificationController::
  84. OnPeripheralNotificationManagerInitialized() {
  85. DCHECK(ash::PeripheralNotificationManager::IsInitialized());
  86. ash::PeripheralNotificationManager::Get()->AddObserver(this);
  87. }
  88. // Notify the user that the current cable may not support dp alt mode.
  89. void UsbPeripheralNotificationController::OnInvalidDpCableWarning() {
  90. if (!ShouldDisplayNotification())
  91. return;
  92. message_center::RichNotificationData optional;
  93. optional.buttons.push_back(message_center::ButtonInfo(
  94. l10n_util::GetStringUTF16(IDS_ASH_USB_NOTIFICATION_V2_LEARN_MORE)));
  95. std::unique_ptr<message_center::Notification> notification =
  96. CreateSystemNotification(
  97. message_center::NOTIFICATION_TYPE_SIMPLE,
  98. kUsbPeripheralInvalidDpCableNotificationId,
  99. l10n_util::GetStringUTF16(
  100. IDS_ASH_USB_NOTIFICATION_V2_CABLE_WARNING_DISPLAY_TITLE),
  101. l10n_util::GetStringUTF16(
  102. IDS_ASH_USB_NOTIFICATION_V2_CABLE_WARNING_DISPLAY_BODY),
  103. /*display_source=*/std::u16string(), GURL(),
  104. message_center::NotifierId(
  105. message_center::NotifierType::SYSTEM_COMPONENT,
  106. kNotifierUsbPeripheral,
  107. NotificationCatalogName::kUSBPeripheralInvalidDpCable),
  108. optional,
  109. base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
  110. base::BindRepeating(&OnCableNotificationClicked,
  111. kUsbPeripheralInvalidDpCableNotificationId,
  112. kNotificationDisplayLandingPageUrl)),
  113. kSettingsIcon,
  114. message_center::SystemNotificationWarningLevel::WARNING);
  115. message_center_->AddNotification(std::move(notification));
  116. }
  117. // Notify the user that the USB4 device will use TBT due to the cable.
  118. void UsbPeripheralNotificationController::OnInvalidUSB4ValidTBTCableWarning() {
  119. if (!ShouldDisplayNotification())
  120. return;
  121. message_center::RichNotificationData optional;
  122. optional.buttons.push_back(message_center::ButtonInfo(
  123. l10n_util::GetStringUTF16(IDS_ASH_USB_NOTIFICATION_V2_LEARN_MORE)));
  124. std::unique_ptr<message_center::Notification> notification =
  125. CreateSystemNotification(
  126. message_center::NOTIFICATION_TYPE_SIMPLE,
  127. kUsbPeripheralInvalidUSB4ValidTBTCableNotificationId,
  128. l10n_util::GetStringUTF16(
  129. IDS_ASH_USB_NOTIFICATION_V2_CABLE_WARNING_PERFORMANCE_TITLE),
  130. l10n_util::GetStringUTF16(
  131. IDS_ASH_USB_NOTIFICATION_V2_CABLE_WARNING_NO_USB4_SUPPORT_BODY),
  132. /*display_source=*/std::u16string(), GURL(),
  133. message_center::NotifierId(
  134. message_center::NotifierType::SYSTEM_COMPONENT,
  135. kNotifierUsbPeripheral,
  136. NotificationCatalogName::kUSBPeripheralInvalidUSB4ValidTBTCable),
  137. optional,
  138. base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
  139. base::BindRepeating(
  140. &OnCableNotificationClicked,
  141. kUsbPeripheralInvalidUSB4ValidTBTCableNotificationId,
  142. kNotificationDeviceLandingPageUrl)),
  143. kSettingsIcon,
  144. message_center::SystemNotificationWarningLevel::WARNING);
  145. message_center_->AddNotification(std::move(notification));
  146. }
  147. // Notify the user that the USB4 device will use DisplayPort,
  148. // USB 3.2 or USB 2.0 due to the cable.
  149. void UsbPeripheralNotificationController::OnInvalidUSB4CableWarning() {
  150. if (!ShouldDisplayNotification())
  151. return;
  152. message_center::RichNotificationData optional;
  153. optional.buttons.push_back(message_center::ButtonInfo(
  154. l10n_util::GetStringUTF16(IDS_ASH_USB_NOTIFICATION_V2_LEARN_MORE)));
  155. std::unique_ptr<message_center::Notification> notification =
  156. CreateSystemNotification(
  157. message_center::NOTIFICATION_TYPE_SIMPLE,
  158. kUsbPeripheralInvalidUSB4CableNotificationId,
  159. l10n_util::GetStringUTF16(
  160. IDS_ASH_USB_NOTIFICATION_V2_CABLE_WARNING_PERFORMANCE_TITLE),
  161. l10n_util::GetStringUTF16(
  162. IDS_ASH_USB_NOTIFICATION_V2_CABLE_WARNING_NO_USB4_SUPPORT_BODY),
  163. /*display_source=*/std::u16string(), GURL(),
  164. message_center::NotifierId(
  165. message_center::NotifierType::SYSTEM_COMPONENT,
  166. kNotifierUsbPeripheral,
  167. NotificationCatalogName::kUSBPeripheralInvalidUSB4Cable),
  168. optional,
  169. base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
  170. base::BindRepeating(&OnCableNotificationClicked,
  171. kUsbPeripheralInvalidUSB4CableNotificationId,
  172. kNotificationDeviceLandingPageUrl)),
  173. kSettingsIcon,
  174. message_center::SystemNotificationWarningLevel::WARNING);
  175. message_center_->AddNotification(std::move(notification));
  176. }
  177. // Notify the user that the TBT device will use DisplayPort,
  178. // USB 3.2 or USB 2.0 due to the cable.
  179. void UsbPeripheralNotificationController::OnInvalidTBTCableWarning() {
  180. if (!ShouldDisplayNotification())
  181. return;
  182. message_center::RichNotificationData optional;
  183. optional.buttons.push_back(message_center::ButtonInfo(
  184. l10n_util::GetStringUTF16(IDS_ASH_USB_NOTIFICATION_V2_LEARN_MORE)));
  185. std::unique_ptr<message_center::Notification> notification =
  186. CreateSystemNotification(
  187. message_center::NOTIFICATION_TYPE_SIMPLE,
  188. kUsbPeripheralInvalidTBTCableNotificationId,
  189. l10n_util::GetStringUTF16(
  190. IDS_ASH_USB_NOTIFICATION_V2_CABLE_WARNING_PERFORMANCE_TITLE),
  191. l10n_util::GetStringUTF16(
  192. IDS_ASH_USB_NOTIFICATION_V2_CABLE_WARNING_NO_TBT_SUPPORT_BODY),
  193. /*display_source=*/std::u16string(), GURL(),
  194. message_center::NotifierId(
  195. message_center::NotifierType::SYSTEM_COMPONENT,
  196. kNotifierUsbPeripheral,
  197. NotificationCatalogName::kUSBPeripheralInvalidTBTCable),
  198. optional,
  199. base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
  200. base::BindRepeating(&OnCableNotificationClicked,
  201. kUsbPeripheralInvalidTBTCableNotificationId,
  202. kNotificationDeviceLandingPageUrl)),
  203. kSettingsIcon,
  204. message_center::SystemNotificationWarningLevel::WARNING);
  205. message_center_->AddNotification(std::move(notification));
  206. }
  207. // Notify the user that the cable limits USB device performance.
  208. void UsbPeripheralNotificationController::OnSpeedLimitingCableWarning() {
  209. if (!ShouldDisplayNotification() || GetCableSpeedNotificationShownPref())
  210. return;
  211. message_center::RichNotificationData optional;
  212. optional.buttons.push_back(message_center::ButtonInfo(
  213. l10n_util::GetStringUTF16(IDS_ASH_USB_NOTIFICATION_V2_LEARN_MORE)));
  214. std::unique_ptr<message_center::Notification> notification =
  215. CreateSystemNotification(
  216. message_center::NOTIFICATION_TYPE_SIMPLE,
  217. kUsbPeripheralSpeedLimitingCableNotificationId,
  218. l10n_util::GetStringUTF16(
  219. IDS_ASH_USB_NOTIFICATION_V2_CABLE_WARNING_PERFORMANCE_TITLE),
  220. l10n_util::GetStringUTF16(
  221. IDS_ASH_USB_NOTIFICATION_V2_CABLE_WARNING_SPEED_LIMITED_BODY),
  222. /*display_source=*/std::u16string(), GURL(),
  223. message_center::NotifierId(
  224. message_center::NotifierType::SYSTEM_COMPONENT,
  225. kNotifierUsbPeripheral,
  226. NotificationCatalogName::kUSBPeripheralSpeedLimitingCable),
  227. optional,
  228. base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
  229. base::BindRepeating(
  230. &OnCableNotificationClicked,
  231. kUsbPeripheralSpeedLimitingCableNotificationId,
  232. kNotificationDeviceLandingPageUrl)),
  233. kSettingsIcon,
  234. message_center::SystemNotificationWarningLevel::WARNING);
  235. message_center_->AddNotification(std::move(notification));
  236. }
  237. } // namespace ash