message_center_ui_controller.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright (c) 2013 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/message_center/message_center_ui_controller.h"
  5. #include <memory>
  6. #include "ash/shell.h"
  7. #include "ash/system/message_center/metrics_utils.h"
  8. #include "base/observer_list.h"
  9. #include "ui/message_center/message_center.h"
  10. #include "ui/message_center/message_center_types.h"
  11. #include "ui/message_center/notification_blocker.h"
  12. namespace ash {
  13. namespace {
  14. // The duration used to log the number of notifications shown
  15. // right after a user logs in.
  16. constexpr base::TimeDelta kLoginNotificationLogDuration = base::Minutes(1);
  17. } // namespace
  18. MessageCenterUiController::MessageCenterUiController(
  19. MessageCenterUiDelegate* delegate)
  20. : message_center_(message_center::MessageCenter::Get()),
  21. message_center_visible_(false),
  22. popups_visible_(false),
  23. delegate_(delegate) {
  24. message_center_->AddObserver(this);
  25. session_observer_.Observe(Shell::Get()->session_controller());
  26. }
  27. MessageCenterUiController::~MessageCenterUiController() {
  28. message_center_->RemoveObserver(this);
  29. session_observer_.Reset();
  30. }
  31. bool MessageCenterUiController::ShowMessageCenterBubble() {
  32. if (message_center_visible_)
  33. return true;
  34. HidePopupBubbleInternal();
  35. message_center_->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER);
  36. message_center_visible_ = delegate_->ShowMessageCenter();
  37. if (message_center_visible_)
  38. NotifyUiControllerChanged();
  39. return message_center_visible_;
  40. }
  41. bool MessageCenterUiController::HideMessageCenterBubble() {
  42. if (!message_center_visible_)
  43. return false;
  44. delegate_->HideMessageCenter();
  45. MarkMessageCenterHidden();
  46. return true;
  47. }
  48. void MessageCenterUiController::MarkMessageCenterHidden() {
  49. if (!message_center_visible_)
  50. return;
  51. message_center_visible_ = false;
  52. message_center_->SetVisibility(message_center::VISIBILITY_TRANSIENT);
  53. // Some notifications (like system ones) should appear as popups again
  54. // after the message center is closed.
  55. if (message_center_->HasPopupNotifications()) {
  56. ShowPopupBubble();
  57. return;
  58. }
  59. NotifyUiControllerChanged();
  60. }
  61. void MessageCenterUiController::ShowPopupBubble() {
  62. if (message_center_visible_)
  63. return;
  64. if (popups_visible_) {
  65. NotifyUiControllerChanged();
  66. return;
  67. }
  68. if (!message_center_->HasPopupNotifications())
  69. return;
  70. popups_visible_ = delegate_->ShowPopups();
  71. NotifyUiControllerChanged();
  72. }
  73. bool MessageCenterUiController::HidePopupBubble() {
  74. if (!popups_visible_)
  75. return false;
  76. HidePopupBubbleInternal();
  77. NotifyUiControllerChanged();
  78. return true;
  79. }
  80. void MessageCenterUiController::HidePopupBubbleInternal() {
  81. if (!popups_visible_)
  82. return;
  83. delegate_->HidePopups();
  84. popups_visible_ = false;
  85. }
  86. void MessageCenterUiController::OnNotificationAdded(
  87. const std::string& notification_id) {
  88. metrics_utils::LogNotificationAdded(notification_id);
  89. OnMessageCenterChanged();
  90. }
  91. void MessageCenterUiController::OnNotificationRemoved(
  92. const std::string& notification_id,
  93. bool by_user) {
  94. OnMessageCenterChanged();
  95. }
  96. void MessageCenterUiController::OnNotificationUpdated(
  97. const std::string& notification_id) {
  98. OnMessageCenterChanged();
  99. }
  100. void MessageCenterUiController::OnNotificationClicked(
  101. const std::string& notification_id,
  102. const absl::optional<int>& button_index,
  103. const absl::optional<std::u16string>& reply) {
  104. if (popups_visible_)
  105. OnMessageCenterChanged();
  106. // Note: we use |message_center_visible_| instead of |popups_visible_| here
  107. // due to timing issues when dismissing the last popup notification.
  108. bool is_popup = !message_center_visible_;
  109. if (reply.has_value())
  110. metrics_utils::LogInlineReplySent(notification_id, is_popup);
  111. else if (button_index.has_value())
  112. metrics_utils::LogClickedActionButton(notification_id, is_popup);
  113. else
  114. metrics_utils::LogClickedBody(notification_id, is_popup);
  115. }
  116. void MessageCenterUiController::OnNotificationDisplayed(
  117. const std::string& notification_id,
  118. const message_center::DisplaySource source) {
  119. if (login_notification_logging_timer_.IsRunning())
  120. notifications_displayed_in_first_minute_count_++;
  121. NotifyUiControllerChanged();
  122. }
  123. void MessageCenterUiController::OnQuietModeChanged(bool in_quiet_mode) {
  124. NotifyUiControllerChanged();
  125. }
  126. void MessageCenterUiController::OnBlockingStateChanged(
  127. message_center::NotificationBlocker* blocker) {
  128. OnMessageCenterChanged();
  129. }
  130. void MessageCenterUiController::OnNotificationPopupShown(
  131. const std::string& notification_id,
  132. bool mark_notification_as_read) {
  133. // Timed out popup notifications are not marked as read.
  134. if (!mark_notification_as_read)
  135. metrics_utils::LogPopupExpiredToTray(notification_id);
  136. }
  137. void MessageCenterUiController::OnMessageViewHovered(
  138. const std::string& notification_id) {
  139. // Note: we use |message_center_visible_| instead of |popups_visible_| here
  140. // due to timing issues when dismissing the last popup notification.
  141. bool is_popup = !message_center_visible_;
  142. metrics_utils::LogHover(notification_id, is_popup);
  143. }
  144. void MessageCenterUiController::OnFirstSessionStarted() {
  145. login_notification_logging_timer_.Start(
  146. FROM_HERE, kLoginNotificationLogDuration, this,
  147. &MessageCenterUiController::OnLoginTimerEnded);
  148. }
  149. void MessageCenterUiController::OnLoginTimerEnded() {
  150. metrics_utils::LogNotificationsShownInFirstMinute(
  151. notifications_displayed_in_first_minute_count_);
  152. }
  153. void MessageCenterUiController::OnMessageCenterChanged() {
  154. if (hide_on_last_notification_ && message_center_visible_ &&
  155. message_center_->NotificationCount() == 0) {
  156. HideMessageCenterBubble();
  157. return;
  158. }
  159. if (popups_visible_ && !message_center_->HasPopupNotifications())
  160. HidePopupBubbleInternal();
  161. else if (!popups_visible_ && message_center_->HasPopupNotifications())
  162. ShowPopupBubble();
  163. NotifyUiControllerChanged();
  164. }
  165. void MessageCenterUiController::NotifyUiControllerChanged() {
  166. delegate_->OnMessageCenterContentsChanged();
  167. }
  168. } // namespace ash