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 5 #ifndef UI_MESSAGE_CENTER_NOTIFICATION_LIST_H_ 6 #define UI_MESSAGE_CENTER_NOTIFICATION_LIST_H_ 7 8 #include <set> 9 #include <string> 10 11 #include "base/strings/string16.h" 12 #include "base/time/time.h" 13 #include "base/timer/timer.h" 14 #include "ui/gfx/image/image.h" 15 #include "ui/gfx/native_widget_types.h" 16 #include "ui/message_center/message_center_export.h" 17 #include "ui/message_center/notification.h" 18 #include "ui/message_center/notification_types.h" 19 20 namespace base { 21 class DictionaryValue; 22 } 23 24 namespace message_center { 25 26 class NotificationDelegate; 27 28 namespace test { 29 class NotificationListTest; 30 } 31 32 // Comparers used to auto-sort the lists of Notifications. 33 struct MESSAGE_CENTER_EXPORT ComparePriorityTimestampSerial { 34 bool operator()(Notification* n1, Notification* n2); 35 }; 36 37 struct CompareTimestampSerial { 38 bool operator()(Notification* n1, Notification* n2); 39 }; 40 41 // A helper class to manage the list of notifications. 42 class MESSAGE_CENTER_EXPORT NotificationList { 43 public: 44 // Auto-sorted set. Matches the order in which Notifications are shown in 45 // Notification Center. 46 typedef std::set<Notification*, ComparePriorityTimestampSerial> Notifications; 47 48 // Auto-sorted set used to return the Notifications to be shown as popup 49 // toasts. 50 typedef std::set<Notification*, CompareTimestampSerial> PopupNotifications; 51 52 explicit NotificationList(); 53 virtual ~NotificationList(); 54 55 // Affects whether or not a message has been "read". Collects the set of 56 // ids whose state have changed and set to |udpated_ids|. NULL if updated 57 // ids don't matter. 58 void SetMessageCenterVisible(bool visible, 59 std::set<std::string>* updated_ids); 60 61 void AddNotification(scoped_ptr<Notification> notification); 62 63 void UpdateNotificationMessage(const std::string& old_id, 64 scoped_ptr<Notification> new_notification); 65 66 void RemoveNotification(const std::string& id); 67 68 void RemoveAllNotifications(); 69 70 Notifications GetNotificationsBySource(const std::string& id); 71 Notifications GetNotificationsByExtension(const std::string& id); 72 73 // Returns true if the notification exists and was updated. 74 bool SetNotificationIcon(const std::string& notification_id, 75 const gfx::Image& image); 76 77 // Returns true if the notification exists and was updated. 78 bool SetNotificationImage(const std::string& notification_id, 79 const gfx::Image& image); 80 81 // Returns true if the notification and button exist and were updated. 82 bool SetNotificationButtonIcon(const std::string& notification_id, 83 int button_index, 84 const gfx::Image& image); 85 86 bool HasNotification(const std::string& id); 87 88 // Returns false if the first notification has been shown as a popup (which 89 // means that all notifications have been shown). 90 bool HasPopupNotifications(); 91 92 // Returns the recent notifications of the priority higher then LOW, 93 // that have not been shown as a popup. kMaxVisiblePopupNotifications are 94 // used to limit the number of notifications for the DEFAULT priority. 95 // The returned list is sorted by timestamp, newer first. 96 PopupNotifications GetPopupNotifications(); 97 Notification* GetPopup(const std::string& id); 98 99 // Marks the popups for the |priority| as shown. 100 void MarkPopupsAsShown(int priority); 101 102 // Marks a specific popup item as shown. Set |mark_notification_as_read| to 103 // true in case marking the notification as read too. 104 void MarkSinglePopupAsShown(const std::string& id, 105 bool mark_notification_as_read); 106 107 // Marks a specific popup item as displayed. 108 void MarkSinglePopupAsDisplayed(const std::string& id); 109 110 // Marks the specified notification as expanded in the notification center. 111 void MarkNotificationAsExpanded(const std::string& id); 112 113 NotificationDelegate* GetNotificationDelegate(const std::string& id); 114 115 bool quiet_mode() const { return quiet_mode_; } 116 117 // Sets the current quiet mode status to |quiet_mode|. The new status is not 118 // expired. 119 void SetQuietMode(bool quiet_mode); 120 121 // Sets the current quiet mode to true. The quiet mode will expire in the 122 // specified time-delta from now. 123 void EnterQuietModeWithExpire(const base::TimeDelta& expires_in); 124 125 // Returns all notifications, in a (priority-timestamp) order. Suitable for 126 // rendering notifications in a NotificationCenter. 127 const Notifications& GetNotifications(); 128 size_t NotificationCount() const; 129 size_t unread_count() const { return unread_count_; } 130 bool is_message_center_visible() const { return message_center_visible_; } 131 132 private: 133 friend class test::NotificationListTest; 134 135 // Iterates through the list and returns the first notification matching |id|. 136 Notifications::iterator GetNotification(const std::string& id); 137 138 void EraseNotification(Notifications::iterator iter); 139 140 void PushNotification(scoped_ptr<Notification> notification); 141 142 // Sets the current quiet mode status to |quiet_mode|. 143 void SetQuietModeInternal(bool quiet_mode); 144 145 Notifications notifications_; 146 bool message_center_visible_; 147 size_t unread_count_; 148 bool quiet_mode_; 149 scoped_ptr<base::OneShotTimer<NotificationList> > quiet_mode_timer_; 150 151 DISALLOW_COPY_AND_ASSIGN(NotificationList); 152 }; 153 154 } // namespace message_center 155 156 #endif // UI_MESSAGE_CENTER_NOTIFICATION_LIST_H_ 157