inactive_user_notification_blocker.cc 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2017 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/inactive_user_notification_blocker.h"
  5. #include "ash/session/session_controller_impl.h"
  6. #include "ash/shell.h"
  7. #include "components/account_id/account_id.h"
  8. #include "ui/message_center/message_center.h"
  9. namespace ash {
  10. InactiveUserNotificationBlocker::InactiveUserNotificationBlocker(
  11. message_center::MessageCenter* message_center)
  12. : NotificationBlocker(message_center), scoped_observer_(this) {}
  13. InactiveUserNotificationBlocker::~InactiveUserNotificationBlocker() = default;
  14. bool InactiveUserNotificationBlocker::ShouldShowNotification(
  15. const message_center::Notification& notification) const {
  16. // Allow notifications before login or in a single user session.
  17. if (Shell::Get()->session_controller()->NumberOfLoggedInUsers() < 2)
  18. return true;
  19. // All non-system notifications should be tied to a user profile.
  20. if (notification.notifier_id().profile_id.empty())
  21. return true;
  22. return AccountId::FromUserEmail(notification.notifier_id().profile_id) ==
  23. active_account_id_;
  24. }
  25. bool InactiveUserNotificationBlocker::ShouldShowNotificationAsPopup(
  26. const message_center::Notification& notification) const {
  27. return ShouldShowNotification(notification);
  28. }
  29. void InactiveUserNotificationBlocker::OnActiveUserSessionChanged(
  30. const AccountId& account_id) {
  31. if (active_account_id_ == account_id)
  32. return;
  33. quiet_modes_[active_account_id_] = message_center()->IsQuietMode();
  34. active_account_id_ = account_id;
  35. std::map<AccountId, bool>::const_iterator iter =
  36. quiet_modes_.find(active_account_id_);
  37. if (iter != quiet_modes_.end() &&
  38. iter->second != message_center()->IsQuietMode()) {
  39. message_center()->SetQuietMode(iter->second);
  40. }
  41. NotifyBlockingStateChanged();
  42. }
  43. } // namespace ash