notification_blocker.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #ifndef UI_MESSAGE_CENTER_NOTIFICATION_BLOCKER_H_
  5. #define UI_MESSAGE_CENTER_NOTIFICATION_BLOCKER_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/observer_list.h"
  8. #include "base/observer_list_types.h"
  9. #include "ui/message_center/message_center_export.h"
  10. #include "ui/message_center/public/cpp/notification.h"
  11. namespace message_center {
  12. class MessageCenter;
  13. // NotificationBlocker manages the availability of notifications based on the
  14. // current system status. Each NotificationBlocker implementation covers a
  15. // single state such as screen lock or fullscreen.
  16. class MESSAGE_CENTER_EXPORT NotificationBlocker {
  17. public:
  18. class Observer : public base::CheckedObserver {
  19. public:
  20. virtual void OnBlockingStateChanged(NotificationBlocker* blocker) = 0;
  21. };
  22. explicit NotificationBlocker(MessageCenter* message_center);
  23. virtual ~NotificationBlocker();
  24. void AddObserver(Observer* observer);
  25. void RemoveObserver(Observer* observer);
  26. // Checks the current state and updates the availability.
  27. virtual void CheckState() {}
  28. // Returns true should be shown in the message center. Default returns true
  29. // always.
  30. virtual bool ShouldShowNotification(
  31. const Notification& notification) const;
  32. // Returns true if this notification should be shown as popups on screen.
  33. // If it's false, those notifications should be queued.
  34. // When a blocker starts returning false for a notification which is already
  35. // shown as a popup, the notification should be closed as a popup immediately.
  36. virtual bool ShouldShowNotificationAsPopup(
  37. const Notification& notification) const = 0;
  38. protected:
  39. MessageCenter* message_center() { return message_center_; }
  40. void NotifyBlockingStateChanged();
  41. private:
  42. base::ObserverList<Observer> observers_;
  43. raw_ptr<MessageCenter> message_center_; // weak
  44. };
  45. typedef std::vector<NotificationBlocker*> NotificationBlockers;
  46. } // namespace message_center
  47. #endif // UI_MESSAGE_CENTER_NOTIFICATION_BLOCKER_H_