OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef UI_MESSAGE_CENTER_NOTIFICATION_LIST_H_ | 5 #ifndef UI_MESSAGE_CENTER_NOTIFICATION_LIST_H_ |
6 #define UI_MESSAGE_CENTER_NOTIFICATION_LIST_H_ | 6 #define UI_MESSAGE_CENTER_NOTIFICATION_LIST_H_ |
7 | 7 |
8 #include <list> | 8 #include <set> |
9 #include <map> | |
10 #include <string> | 9 #include <string> |
11 | 10 |
12 #include "base/string16.h" | 11 #include "base/string16.h" |
13 #include "base/time.h" | 12 #include "base/time.h" |
14 #include "base/timer.h" | 13 #include "base/timer.h" |
15 #include "ui/gfx/image/image_skia.h" | 14 #include "ui/gfx/image/image_skia.h" |
16 #include "ui/gfx/native_widget_types.h" | 15 #include "ui/gfx/native_widget_types.h" |
17 #include "ui/message_center/message_center_export.h" | 16 #include "ui/message_center/message_center_export.h" |
18 #include "ui/message_center/notification.h" | 17 #include "ui/message_center/notification.h" |
19 #include "ui/notifications/notification_types.h" | 18 #include "ui/message_center/notification_types.h" |
20 | 19 |
21 namespace base { | 20 namespace base { |
22 class DictionaryValue; | 21 class DictionaryValue; |
23 } | 22 } |
24 | 23 |
25 namespace message_center { | 24 namespace message_center { |
26 | 25 |
| 26 // Comparers used to auto-sort the lists of Notifications. |
| 27 struct ComparePriorityTimestampSerial { |
| 28 bool operator()(Notification* n1, Notification* n2); |
| 29 }; |
| 30 |
| 31 struct CompareTimestampSerial { |
| 32 bool operator()(Notification* n1, Notification* n2); |
| 33 }; |
| 34 |
27 // A helper class to manage the list of notifications. | 35 // A helper class to manage the list of notifications. |
28 class MESSAGE_CENTER_EXPORT NotificationList { | 36 class MESSAGE_CENTER_EXPORT NotificationList { |
29 public: | 37 public: |
30 typedef std::list<Notification> Notifications; | 38 // Auto-sorted set. Matches the order in which Notifications are shown in |
| 39 // Notification Center. |
| 40 typedef std::set<Notification*, ComparePriorityTimestampSerial> Notifications; |
| 41 |
| 42 // Auto-sorted set used to return the Notifications to be shown as popup |
| 43 // toasts. |
| 44 typedef std::set<Notification*, CompareTimestampSerial> PopupNotifications; |
31 | 45 |
32 class MESSAGE_CENTER_EXPORT Delegate { | 46 class MESSAGE_CENTER_EXPORT Delegate { |
33 public: | 47 public: |
34 Delegate() {} | 48 Delegate() {} |
35 virtual ~Delegate() {} | 49 virtual ~Delegate() {} |
36 | 50 |
37 // Removes notifications | 51 // Removes notifications |
38 virtual void SendRemoveNotification(const std::string& id) = 0; | 52 virtual void SendRemoveNotification(const std::string& id) = 0; |
39 virtual void SendRemoveAllNotifications() = 0; | 53 virtual void SendRemoveAllNotifications() = 0; |
40 | 54 |
(...skipping 21 matching lines...) Expand all Loading... |
62 // Returns the list of notifications to display. | 76 // Returns the list of notifications to display. |
63 virtual NotificationList* GetNotificationList() = 0; | 77 virtual NotificationList* GetNotificationList() = 0; |
64 }; | 78 }; |
65 | 79 |
66 explicit NotificationList(Delegate* delegate); | 80 explicit NotificationList(Delegate* delegate); |
67 virtual ~NotificationList(); | 81 virtual ~NotificationList(); |
68 | 82 |
69 // Affects whether or not a message has been "read". | 83 // Affects whether or not a message has been "read". |
70 void SetMessageCenterVisible(bool visible); | 84 void SetMessageCenterVisible(bool visible); |
71 | 85 |
72 void AddNotification(ui::notifications::NotificationType type, | 86 void AddNotification(NotificationType type, |
73 const std::string& id, | 87 const std::string& id, |
74 const string16& title, | 88 const string16& title, |
75 const string16& message, | 89 const string16& message, |
76 const string16& display_source, | 90 const string16& display_source, |
77 const std::string& extension_id, | 91 const std::string& extension_id, |
78 const base::DictionaryValue* optional_fields); | 92 const base::DictionaryValue* optional_fields); |
79 | 93 |
80 void UpdateNotificationMessage(const std::string& old_id, | 94 void UpdateNotificationMessage(const std::string& old_id, |
81 const std::string& new_id, | 95 const std::string& new_id, |
82 const string16& title, | 96 const string16& title, |
83 const string16& message, | 97 const string16& message, |
84 const base::DictionaryValue* optional_fields); | 98 const base::DictionaryValue* optional_fields); |
85 | 99 |
86 // Returns true if the notification was removed. | 100 void RemoveNotification(const std::string& id); |
87 bool RemoveNotification(const std::string& id); | |
88 | 101 |
89 void RemoveAllNotifications(); | 102 void RemoveAllNotifications(); |
90 | 103 |
91 void SendRemoveNotificationsBySource(const std::string& id); | 104 void SendRemoveNotificationsBySource(const std::string& id); |
92 | 105 |
93 void SendRemoveNotificationsByExtension(const std::string& id); | 106 void SendRemoveNotificationsByExtension(const std::string& id); |
94 | 107 |
95 // Returns true if the notification exists and was updated. | 108 // Returns true if the notification exists and was updated. |
96 bool SetNotificationIcon(const std::string& notification_id, | 109 bool SetNotificationIcon(const std::string& notification_id, |
97 const gfx::ImageSkia& image); | 110 const gfx::ImageSkia& image); |
98 | 111 |
99 // Returns true if the notification exists and was updated. | 112 // Returns true if the notification exists and was updated. |
100 bool SetNotificationImage(const std::string& notification_id, | 113 bool SetNotificationImage(const std::string& notification_id, |
101 const gfx::ImageSkia& image); | 114 const gfx::ImageSkia& image); |
102 | 115 |
103 // Returns true if the notification and button exist and were updated. | 116 // Returns true if the notification and button exist and were updated. |
104 bool SetNotificationButtonIcon(const std::string& notification_id, | 117 bool SetNotificationButtonIcon(const std::string& notification_id, |
105 int button_index, | 118 int button_index, |
106 const gfx::ImageSkia& image); | 119 const gfx::ImageSkia& image); |
107 | 120 |
108 bool HasNotification(const std::string& id); | 121 bool HasNotification(const std::string& id); |
109 | 122 |
110 // Returns false if the first notification has been shown as a popup (which | 123 // Returns false if the first notification has been shown as a popup (which |
111 // means that all notifications have been shown). | 124 // means that all notifications have been shown). |
112 bool HasPopupNotifications(); | 125 bool HasPopupNotifications(); |
113 | 126 |
114 // Modifies |notifications| to contain the |kMaxVisiblePopupNotifications| | 127 // Returns the recent notifications of the priority higher then LOW, |
115 // least recent notifications that have not been shown as a popup. | 128 // that have not been shown as a popup. kMaxVisiblePopupNotifications are |
116 void GetPopupNotifications(Notifications* notifications); | 129 // used to limit the number of notifications for the DEFAULT priority. |
| 130 // The returned list is sorted by timestamp, newer first. |
| 131 PopupNotifications GetPopupNotifications(); |
117 | 132 |
118 // Marks the popups for the |priority| as shown. | 133 // Marks the popups for the |priority| as shown. |
119 void MarkPopupsAsShown(int priority); | 134 void MarkPopupsAsShown(int priority); |
120 | 135 |
121 // Marks a specific popup item as shown. Set |mark_notification_as_read| to | 136 // Marks a specific popup item as shown. Set |mark_notification_as_read| to |
122 // true in case marking the notification as read too. | 137 // true in case marking the notification as read too. |
123 void MarkSinglePopupAsShown(const std::string& id, | 138 void MarkSinglePopupAsShown(const std::string& id, |
124 bool mark_notification_as_read); | 139 bool mark_notification_as_read); |
125 | 140 |
126 bool quiet_mode() const { return quiet_mode_; } | 141 bool quiet_mode() const { return quiet_mode_; } |
127 | 142 |
128 // Sets the current quiet mode status to |quiet_mode|. The new status is not | 143 // Sets the current quiet mode status to |quiet_mode|. The new status is not |
129 // expired. | 144 // expired. |
130 void SetQuietMode(bool quiet_mode); | 145 void SetQuietMode(bool quiet_mode); |
131 | 146 |
132 // Sets the current quiet mode to true. The quiet mode will expire in the | 147 // Sets the current quiet mode to true. The quiet mode will expire in the |
133 // specified time-delta from now. | 148 // specified time-delta from now. |
134 void EnterQuietModeWithExpire(const base::TimeDelta& expires_in); | 149 void EnterQuietModeWithExpire(const base::TimeDelta& expires_in); |
135 | 150 |
136 void GetNotifications(Notifications* notifications) const; | 151 // Returns all notifications, in a (priority-timestamp) order. Suitable for |
| 152 // rendering notifications in a NotificationCenter. |
| 153 const Notifications& GetNotifications(); |
137 size_t NotificationCount() const; | 154 size_t NotificationCount() const; |
138 size_t unread_count() const { return unread_count_; } | 155 size_t unread_count() const { return unread_count_; } |
139 | 156 |
140 static const size_t kMaxVisiblePopupNotifications; | 157 static const size_t kMaxVisiblePopupNotifications; |
141 static const size_t kMaxVisibleMessageCenterNotifications; | 158 static const size_t kMaxVisibleMessageCenterNotifications; |
142 | 159 |
143 private: | 160 private: |
144 typedef std::map<int, Notifications> NotificationMap; | 161 // Iterates through the list and returns the first notification matching |id|. |
145 | 162 Notifications::iterator GetNotification(const std::string& id); |
146 // Iterates through the list and stores the first notification matching |id| | |
147 // (should always be unique) to |iter|. Returns true if it's found. | |
148 bool GetNotification(const std::string& id, Notifications::iterator* iter); | |
149 | 163 |
150 void EraseNotification(Notifications::iterator iter); | 164 void EraseNotification(Notifications::iterator iter); |
151 | 165 |
152 void PushNotification(Notification& notification); | 166 void PushNotification(scoped_ptr<Notification> notification); |
153 | |
154 // Returns the recent notifications of the |priority| that have not been shown | |
155 // as a popup. kMaxVisiblePopupNotifications are used to limit the number of | |
156 // notifications for the default priority. | |
157 void GetPopupIterators(int priority, | |
158 Notifications::iterator* first, | |
159 Notifications::iterator* last); | |
160 | |
161 // Given a dictionary of optional notification fields (or NULL), unpacks all | |
162 // recognized values into the given Notification struct. We assume prior | |
163 // proper initialization of |notification| fields that correspond to | |
164 // |optional_fields|. | |
165 void UnpackOptionalFields(const base::DictionaryValue* optional_fields, | |
166 Notification* notification); | |
167 | 167 |
168 // Sets the current quiet mode status to |quiet_mode|. | 168 // Sets the current quiet mode status to |quiet_mode|. |
169 void SetQuietModeInternal(bool quiet_mode); | 169 void SetQuietModeInternal(bool quiet_mode); |
170 | 170 |
171 Delegate* delegate_; | 171 Delegate* delegate_; |
172 NotificationMap notifications_; | 172 Notifications notifications_; |
173 bool message_center_visible_; | 173 bool message_center_visible_; |
174 size_t unread_count_; | 174 size_t unread_count_; |
175 bool quiet_mode_; | 175 bool quiet_mode_; |
176 scoped_ptr<base::OneShotTimer<NotificationList> > quiet_mode_timer_; | 176 scoped_ptr<base::OneShotTimer<NotificationList> > quiet_mode_timer_; |
177 | 177 |
178 DISALLOW_COPY_AND_ASSIGN(NotificationList); | 178 DISALLOW_COPY_AND_ASSIGN(NotificationList); |
179 }; | 179 }; |
180 | 180 |
181 } // namespace message_center | 181 } // namespace message_center |
182 | 182 |
183 #endif // UI_MESSAGE_CENTER_NOTIFICATION_LIST_H_ | 183 #endif // UI_MESSAGE_CENTER_NOTIFICATION_LIST_H_ |
OLD | NEW |