notification_list.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright (c) 2012 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_LIST_H_
  5. #define UI_MESSAGE_CENTER_NOTIFICATION_LIST_H_
  6. #include <stddef.h>
  7. #include <list>
  8. #include <map>
  9. #include <set>
  10. #include <string>
  11. #include "base/gtest_prod_util.h"
  12. #include "base/memory/raw_ptr.h"
  13. #include "ui/message_center/message_center_export.h"
  14. #include "ui/message_center/notification_blocker.h"
  15. #include "ui/message_center/public/cpp/notification_types.h"
  16. namespace base {
  17. class TimeDelta;
  18. }
  19. namespace gfx {
  20. class Image;
  21. }
  22. namespace message_center {
  23. namespace test {
  24. class NotificationListTest;
  25. }
  26. class Notification;
  27. class NotificationDelegate;
  28. struct NotifierId;
  29. // Comparers used to auto-sort the lists of Notifications.
  30. struct MESSAGE_CENTER_EXPORT ComparePriorityTimestampSerial {
  31. bool operator()(Notification* n1, Notification* n2) const;
  32. };
  33. struct MESSAGE_CENTER_EXPORT CompareTimestampSerial {
  34. bool operator()(Notification* n1, Notification* n2) const;
  35. };
  36. // An adapter to allow use of the comparers above with std::unique_ptr.
  37. template <typename PlainCompare>
  38. struct UniquePtrCompare {
  39. template <typename T>
  40. bool operator()(const std::unique_ptr<T>& n1,
  41. const std::unique_ptr<T>& n2) const {
  42. return PlainCompare()(n1.get(), n2.get());
  43. }
  44. };
  45. // A helper class to manage the list of notifications.
  46. class MESSAGE_CENTER_EXPORT NotificationList {
  47. public:
  48. struct NotificationState {
  49. bool operator!=(const NotificationState& other) const;
  50. bool shown_as_popup = false;
  51. bool is_read = false;
  52. };
  53. // Auto-sorted set. Matches the order in which Notifications are shown in
  54. // Notification Center.
  55. using Notifications = std::set<Notification*, ComparePriorityTimestampSerial>;
  56. using OwnedNotifications =
  57. std::map<std::unique_ptr<Notification>,
  58. NotificationState,
  59. UniquePtrCompare<ComparePriorityTimestampSerial>>;
  60. // Auto-sorted set used to return the Notifications to be shown as popup
  61. // toasts.
  62. using PopupNotifications = std::set<Notification*, CompareTimestampSerial>;
  63. explicit NotificationList(MessageCenter* message_center);
  64. NotificationList(const NotificationList&) = delete;
  65. NotificationList& operator=(const NotificationList&) = delete;
  66. virtual ~NotificationList();
  67. // Makes a message "read". Collects the set of ids whose state have changed
  68. // and set to |udpated_ids|. NULL if updated ids don't matter.
  69. void SetNotificationsShown(const NotificationBlockers& blockers,
  70. std::set<std::string>* updated_ids);
  71. void AddNotification(std::unique_ptr<Notification> notification);
  72. void UpdateNotificationMessage(
  73. const std::string& old_id,
  74. std::unique_ptr<Notification> new_notification);
  75. void RemoveNotification(const std::string& id);
  76. // Returns all notifications in this list.
  77. Notifications GetNotifications() const;
  78. // Returns all notifications that have a matching |notifier_id|.
  79. Notifications GetNotificationsByNotifierId(
  80. const NotifierId& notifier_id) const;
  81. // Returns all notifications that have a matching |app_id|.
  82. Notifications GetNotificationsByAppId(const std::string& app_id) const;
  83. // Returns all notifications that have a matching `origin_url`.
  84. Notifications GetNotificationsByOriginUrl(const GURL& origin_url) const;
  85. // Returns true if the notification exists and was updated.
  86. bool SetNotificationIcon(const std::string& notification_id,
  87. const ui::ImageModel& image);
  88. // Returns true if the notification exists and was updated.
  89. bool SetNotificationImage(const std::string& notification_id,
  90. const gfx::Image& image);
  91. // Returns true if |id| matches a notification in the list and that
  92. // notification's type matches the given type.
  93. bool HasNotificationOfType(const std::string& id,
  94. const NotificationType type) const;
  95. // Returns false if the first notification has been shown as a popup (which
  96. // means that all notifications have been shown).
  97. bool HasPopupNotifications(const NotificationBlockers& blockers) const;
  98. // Returns the recent notifications of the priority higher then LOW,
  99. // that have not been shown as a popup. kMaxVisiblePopupNotifications are
  100. // used to limit the number of notifications for the DEFAULT priority.
  101. // It also stores the list of notifications which are blocked by |blockers|
  102. // to |blocked|. |blocked| can be NULL if the caller doesn't care which
  103. // notifications are blocked.
  104. PopupNotifications GetPopupNotifications(const NotificationBlockers& blockers,
  105. std::list<std::string>* blocked);
  106. // Lists all notifications (even those that aren't shown due to shown popup
  107. // limits) that would qualify as popups with the given list of blockers.
  108. // Doesn't mark popups as shown.
  109. PopupNotifications GetPopupNotificationsWithoutBlocker(
  110. const NotificationBlockers& blockers,
  111. const NotificationBlocker& blocker) const;
  112. // Marks a specific popup item as shown. Set |mark_notification_as_read| to
  113. // true in case marking the notification as read too.
  114. void MarkSinglePopupAsShown(const std::string& id,
  115. bool mark_notification_as_read);
  116. // Marks a specific popup item as displayed.
  117. void MarkSinglePopupAsDisplayed(const std::string& id);
  118. // Resets the state for a pop up so that it can be shown again. Used to
  119. // bring up a grouped notification when a new item is added to it.
  120. void ResetSinglePopup(const std::string& id);
  121. NotificationDelegate* GetNotificationDelegate(const std::string& id);
  122. bool quiet_mode() const { return quiet_mode_; }
  123. // Sets the current quiet mode status to |quiet_mode|.
  124. void SetQuietMode(bool quiet_mode);
  125. // Sets the current quiet mode to true. The quiet mode will expire in the
  126. // specified time-delta from now.
  127. void EnterQuietModeWithExpire(const base::TimeDelta& expires_in);
  128. // Returns the notification with the corresponding id. If not found, returns
  129. // NULL. Notification instance is owned by this list.
  130. Notification* GetNotificationById(const std::string& id);
  131. void PopupBlocked(const std::string& id);
  132. // Returns all visible notifications, in a (priority-timestamp) order.
  133. // Suitable for rendering notifications in a MessageCenter.
  134. Notifications GetVisibleNotifications(
  135. const NotificationBlockers& blockers) const;
  136. size_t NotificationCount(const NotificationBlockers& blockers) const;
  137. private:
  138. friend class NotificationListTest;
  139. FRIEND_TEST_ALL_PREFIXES(NotificationListTest,
  140. TestPushingShownNotification);
  141. // Iterates through the list and returns the first notification matching |id|.
  142. OwnedNotifications::iterator GetNotification(const std::string& id);
  143. OwnedNotifications::const_iterator GetNotification(
  144. const std::string& id) const;
  145. void EraseNotification(OwnedNotifications::iterator iter);
  146. void PushNotification(std::unique_ptr<Notification> notification);
  147. raw_ptr<MessageCenter> message_center_; // owner
  148. OwnedNotifications notifications_;
  149. bool quiet_mode_;
  150. };
  151. } // namespace message_center
  152. #endif // UI_MESSAGE_CENTER_NOTIFICATION_LIST_H_