cast_notification_controller.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2018 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/cast/cast_notification_controller.h"
  5. #include "ash/constants/notifier_catalogs.h"
  6. #include "ash/public/cpp/notification_utils.h"
  7. #include "ash/resources/vector_icons/vector_icons.h"
  8. #include "ash/shell.h"
  9. #include "ash/strings/grit/ash_strings.h"
  10. #include "base/bind.h"
  11. #include "base/metrics/user_metrics.h"
  12. #include "base/strings/utf_string_conversions.h"
  13. #include "ui/base/l10n/l10n_util.h"
  14. #include "ui/message_center/message_center.h"
  15. #include "ui/message_center/public/cpp/notification.h"
  16. using message_center::MessageCenter;
  17. using message_center::Notification;
  18. namespace ash {
  19. namespace {
  20. bool ShouldShowNotification() {
  21. auto* cast_config = CastConfigController::Get();
  22. return cast_config && cast_config->HasSinksAndRoutes() &&
  23. cast_config->HasActiveRoute();
  24. }
  25. std::u16string GetNotificationTitle(const CastSink& sink,
  26. const CastRoute& route) {
  27. switch (route.content_source) {
  28. case ContentSource::kUnknown:
  29. return l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_CAST_CAST_UNKNOWN);
  30. case ContentSource::kTab:
  31. case ContentSource::kDesktop:
  32. return l10n_util::GetStringFUTF16(
  33. IDS_ASH_STATUS_TRAY_CAST_NOTIFICATION_TITLE,
  34. base::UTF8ToUTF16(sink.name));
  35. }
  36. }
  37. std::u16string GetNotificationMessage(const CastRoute& route) {
  38. switch (route.content_source) {
  39. case ContentSource::kUnknown:
  40. return std::u16string();
  41. case ContentSource::kTab:
  42. return base::UTF8ToUTF16(route.title);
  43. case ContentSource::kDesktop:
  44. return l10n_util::GetStringUTF16(
  45. IDS_ASH_STATUS_TRAY_CAST_CAST_DESKTOP_NOTIFICATION_MESSAGE);
  46. }
  47. }
  48. const char kNotificationId[] = "chrome://cast";
  49. const char kNotifierId[] = "ash.cast";
  50. } // namespace
  51. CastNotificationController::CastNotificationController() {
  52. if (CastConfigController::Get()) {
  53. CastConfigController::Get()->AddObserver(this);
  54. CastConfigController::Get()->RequestDeviceRefresh();
  55. }
  56. }
  57. CastNotificationController::~CastNotificationController() {
  58. if (CastConfigController::Get())
  59. CastConfigController::Get()->RemoveObserver(this);
  60. }
  61. void CastNotificationController::OnDevicesUpdated(
  62. const std::vector<SinkAndRoute>& devices) {
  63. if (!ShouldShowNotification()) {
  64. message_center::MessageCenter::Get()->RemoveNotification(
  65. kNotificationId, false /* by_user */);
  66. return;
  67. }
  68. for (const auto& device : devices) {
  69. const CastSink& sink = device.sink;
  70. const CastRoute& route = device.route;
  71. // We only want to display casts that came from this machine, since on a
  72. // busy network many other people could be casting.
  73. if (route.id.empty() || !route.is_local_source)
  74. continue;
  75. displayed_route_id_ = route.id;
  76. message_center::RichNotificationData data;
  77. data.buttons.push_back(message_center::ButtonInfo(
  78. l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_CAST_STOP)));
  79. std::unique_ptr<Notification> notification = CreateSystemNotification(
  80. message_center::NOTIFICATION_TYPE_SIMPLE, kNotificationId,
  81. GetNotificationTitle(sink, route), GetNotificationMessage(route),
  82. std::u16string() /* display_source */, GURL(),
  83. message_center::NotifierId(
  84. message_center::NotifierType::SYSTEM_COMPONENT, kNotifierId,
  85. NotificationCatalogName::kCast),
  86. data,
  87. base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
  88. base::BindRepeating(&CastNotificationController::StopCasting,
  89. weak_ptr_factory_.GetWeakPtr())),
  90. kSystemMenuCastIcon,
  91. message_center::SystemNotificationWarningLevel::NORMAL);
  92. notification->set_pinned(true);
  93. MessageCenter::Get()->AddNotification(std::move(notification));
  94. break;
  95. }
  96. }
  97. void CastNotificationController::StopCasting(absl::optional<int> button_index) {
  98. CastConfigController::Get()->StopCasting(displayed_route_id_);
  99. base::RecordAction(base::UserMetricsAction("StatusArea_Cast_StopCast"));
  100. }
  101. } // namespace ash