session_state_notification_blocker.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 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/session_state_notification_blocker.h"
  5. #include "ash/session/session_controller_impl.h"
  6. #include "ash/shell.h"
  7. #include "ash/system/message_center/ash_message_center_lock_screen_controller.h"
  8. #include "ash/system/power/battery_notification.h"
  9. #include "base/containers/contains.h"
  10. #include "ui/message_center/message_center.h"
  11. #include "ui/message_center/public/cpp/notification.h"
  12. #include "ui/message_center/public/cpp/notification_types.h"
  13. #include "ui/message_center/public/cpp/notifier_id.h"
  14. using session_manager::SessionState;
  15. namespace ash {
  16. namespace {
  17. constexpr base::TimeDelta kLoginNotificationDelay = base::Seconds(6);
  18. // Set to false for tests so notifications can be generated without a delay.
  19. bool g_use_login_delay_for_test = true;
  20. bool CalculateShouldShowNotification() {
  21. SessionControllerImpl* const session_controller =
  22. Shell::Get()->session_controller();
  23. SessionState state = session_controller->GetSessionState();
  24. static const SessionState kNotificationBlockedStates[] = {
  25. SessionState::OOBE, SessionState::LOGIN_PRIMARY,
  26. SessionState::LOGIN_SECONDARY, SessionState::LOGGED_IN_NOT_ACTIVE};
  27. // Do not show notifications in kiosk mode or before session starts.
  28. if (session_controller->IsRunningInAppMode() ||
  29. base::Contains(kNotificationBlockedStates, state)) {
  30. return false;
  31. }
  32. return true;
  33. }
  34. bool CalculateShouldShowPopup() {
  35. SessionControllerImpl* const session_controller =
  36. Shell::Get()->session_controller();
  37. if (session_controller->IsRunningInAppMode() ||
  38. session_controller->GetSessionState() != SessionState::ACTIVE) {
  39. return false;
  40. }
  41. const UserSession* active_user_session =
  42. session_controller->GetUserSession(0);
  43. return active_user_session && session_controller->GetUserPrefServiceForUser(
  44. active_user_session->user_info.account_id);
  45. }
  46. } // namespace
  47. SessionStateNotificationBlocker::SessionStateNotificationBlocker(
  48. message_center::MessageCenter* message_center)
  49. : NotificationBlocker(message_center),
  50. should_show_notification_(CalculateShouldShowNotification()),
  51. should_show_popup_(CalculateShouldShowPopup()) {
  52. Shell::Get()->session_controller()->AddObserver(this);
  53. }
  54. SessionStateNotificationBlocker::~SessionStateNotificationBlocker() {
  55. Shell::Get()->session_controller()->RemoveObserver(this);
  56. }
  57. // static
  58. void SessionStateNotificationBlocker::SetUseLoginNotificationDelayForTest(
  59. bool use_delay) {
  60. g_use_login_delay_for_test = use_delay;
  61. }
  62. void SessionStateNotificationBlocker::OnLoginTimerEnded() {
  63. NotifyBlockingStateChanged();
  64. }
  65. bool SessionStateNotificationBlocker::ShouldShowNotification(
  66. const message_center::Notification& notification) const {
  67. // Do not show non system notifications for `kLoginNotificationsDelay`
  68. // duration.
  69. if (notification.notifier_id().type !=
  70. message_center::NotifierType::SYSTEM_COMPONENT &&
  71. login_delay_timer_.IsRunning()) {
  72. return false;
  73. }
  74. // Never show notifications in kiosk mode.
  75. if (Shell::Get()->session_controller()->IsRunningInAppMode())
  76. return false;
  77. if (notification.id() == BatteryNotification::kNotificationId)
  78. return true;
  79. return should_show_notification_;
  80. }
  81. bool SessionStateNotificationBlocker::ShouldShowNotificationAsPopup(
  82. const message_center::Notification& notification) const {
  83. SessionControllerImpl* const session_controller =
  84. Shell::Get()->session_controller();
  85. // Never show notifications in kiosk mode.
  86. if (session_controller->IsRunningInAppMode())
  87. return false;
  88. // Do not show non system notifications for `kLoginNotificationsDelay`
  89. // duration.
  90. if (notification.notifier_id().type !=
  91. message_center::NotifierType::SYSTEM_COMPONENT &&
  92. login_delay_timer_.IsRunning()) {
  93. return false;
  94. }
  95. if (notification.id() == BatteryNotification::kNotificationId)
  96. return true;
  97. return should_show_popup_;
  98. }
  99. void SessionStateNotificationBlocker::OnFirstSessionStarted() {
  100. if (!g_use_login_delay_for_test)
  101. return;
  102. login_delay_timer_.Start(FROM_HERE, kLoginNotificationDelay, this,
  103. &SessionStateNotificationBlocker::OnLoginTimerEnded);
  104. }
  105. void SessionStateNotificationBlocker::OnSessionStateChanged(
  106. SessionState state) {
  107. CheckStateAndNotifyIfChanged();
  108. }
  109. void SessionStateNotificationBlocker::OnActiveUserPrefServiceChanged(
  110. PrefService* pref_service) {
  111. CheckStateAndNotifyIfChanged();
  112. }
  113. void SessionStateNotificationBlocker::CheckStateAndNotifyIfChanged() {
  114. const bool new_should_show_notification = CalculateShouldShowNotification();
  115. const bool new_should_show_popup = CalculateShouldShowPopup();
  116. if (new_should_show_notification == should_show_notification_ &&
  117. new_should_show_popup == should_show_popup_) {
  118. return;
  119. }
  120. should_show_notification_ = new_should_show_notification;
  121. should_show_popup_ = new_should_show_popup;
  122. NotifyBlockingStateChanged();
  123. }
  124. } // namespace ash