OLD | NEW |
| (Empty) |
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 #include "ash/system/web_notification/web_notification_list.h" | |
6 | |
7 namespace message_center { | |
8 | |
9 const size_t WebNotificationList::kMaxVisibleMessageCenterNotifications = 100; | |
10 const size_t WebNotificationList::kMaxVisiblePopupNotifications = 5; | |
11 | |
12 WebNotificationList::WebNotificationList(Delegate* delegate) | |
13 : delegate_(delegate), | |
14 message_center_visible_(false), | |
15 unread_count_(0) { | |
16 } | |
17 | |
18 WebNotificationList::~WebNotificationList() { | |
19 } | |
20 | |
21 void WebNotificationList::SetMessageCenterVisible(bool visible) { | |
22 if (message_center_visible_ == visible) | |
23 return; | |
24 message_center_visible_ = visible; | |
25 if (!visible) { | |
26 // When the list is hidden, clear the unread count, and mark all | |
27 // notifications as read and shown. | |
28 unread_count_ = 0; | |
29 for (Notifications::iterator iter = notifications_.begin(); | |
30 iter != notifications_.end(); ++iter) { | |
31 iter->is_read = true; | |
32 iter->shown_as_popup = true; | |
33 } | |
34 } | |
35 } | |
36 | |
37 void WebNotificationList::AddNotification(const std::string& id, | |
38 const string16& title, | |
39 const string16& message, | |
40 const string16& display_source, | |
41 const std::string& extension_id) { | |
42 WebNotification notification; | |
43 notification.id = id; | |
44 notification.title = title; | |
45 notification.message = message; | |
46 notification.display_source = display_source; | |
47 notification.extension_id = extension_id; | |
48 PushNotification(notification); | |
49 } | |
50 | |
51 void WebNotificationList::UpdateNotificationMessage(const std::string& old_id, | |
52 const std::string& new_id, | |
53 const string16& title, | |
54 const string16& message) { | |
55 Notifications::iterator iter = GetNotification(old_id); | |
56 if (iter == notifications_.end()) | |
57 return; | |
58 // Copy and update notification, then move it to the front of the list. | |
59 WebNotification notification(*iter); | |
60 notification.id = new_id; | |
61 notification.title = title; | |
62 notification.message = message; | |
63 EraseNotification(iter); | |
64 PushNotification(notification); | |
65 } | |
66 | |
67 bool WebNotificationList::RemoveNotification(const std::string& id) { | |
68 Notifications::iterator iter = GetNotification(id); | |
69 if (iter == notifications_.end()) | |
70 return false; | |
71 EraseNotification(iter); | |
72 return true; | |
73 } | |
74 | |
75 void WebNotificationList::RemoveAllNotifications() { | |
76 notifications_.clear(); | |
77 } | |
78 | |
79 void WebNotificationList::SendRemoveNotificationsBySource( | |
80 const std::string& id) { | |
81 Notifications::iterator source_iter = GetNotification(id); | |
82 if (source_iter == notifications_.end()) | |
83 return; | |
84 string16 display_source = source_iter->display_source; | |
85 for (Notifications::iterator loopiter = notifications_.begin(); | |
86 loopiter != notifications_.end(); ) { | |
87 Notifications::iterator curiter = loopiter++; | |
88 if (curiter->display_source == display_source) | |
89 delegate_->SendRemoveNotification(curiter->id); | |
90 } | |
91 } | |
92 | |
93 void WebNotificationList::SendRemoveNotificationsByExtension( | |
94 const std::string& id) { | |
95 Notifications::iterator source_iter = GetNotification(id); | |
96 if (source_iter == notifications_.end()) | |
97 return; | |
98 std::string extension_id = source_iter->extension_id; | |
99 for (Notifications::iterator loopiter = notifications_.begin(); | |
100 loopiter != notifications_.end(); ) { | |
101 Notifications::iterator curiter = loopiter++; | |
102 if (curiter->extension_id == extension_id) | |
103 delegate_->SendRemoveNotification(curiter->id); | |
104 } | |
105 } | |
106 | |
107 bool WebNotificationList::SetNotificationImage(const std::string& id, | |
108 const gfx::ImageSkia& image) { | |
109 Notifications::iterator iter = GetNotification(id); | |
110 if (iter == notifications_.end()) | |
111 return false; | |
112 iter->image = image; | |
113 return true; | |
114 } | |
115 | |
116 bool WebNotificationList::HasNotification(const std::string& id) { | |
117 return GetNotification(id) != notifications_.end(); | |
118 } | |
119 | |
120 bool WebNotificationList::HasPopupNotifications() { | |
121 return !notifications_.empty() && !notifications_.front().shown_as_popup; | |
122 } | |
123 | |
124 void WebNotificationList::GetPopupNotifications( | |
125 WebNotificationList::Notifications* notifications) { | |
126 Notifications::iterator first, last; | |
127 GetPopupIterators(first, last); | |
128 notifications->clear(); | |
129 for (Notifications::iterator iter = first; iter != last; ++iter) | |
130 notifications->push_back(*iter); | |
131 } | |
132 | |
133 void WebNotificationList::MarkPopupsAsShown() { | |
134 Notifications::iterator first, last; | |
135 GetPopupIterators(first, last); | |
136 for (Notifications::iterator iter = first; iter != last; ++iter) | |
137 iter->shown_as_popup = true; | |
138 } | |
139 | |
140 WebNotificationList::Notifications::iterator | |
141 WebNotificationList::GetNotification( | |
142 const std::string& id) { | |
143 for (Notifications::iterator iter = notifications_.begin(); | |
144 iter != notifications_.end(); ++iter) { | |
145 if (iter->id == id) | |
146 return iter; | |
147 } | |
148 return notifications_.end(); | |
149 } | |
150 | |
151 void WebNotificationList::EraseNotification( | |
152 WebNotificationList::Notifications::iterator iter) { | |
153 if (!message_center_visible_ && !iter->is_read) | |
154 --unread_count_; | |
155 notifications_.erase(iter); | |
156 } | |
157 | |
158 void WebNotificationList::PushNotification(WebNotification& notification) { | |
159 // Ensure that notification.id is unique by erasing any existing | |
160 // notification with the same id (shouldn't normally happen). | |
161 Notifications::iterator iter = GetNotification(notification.id); | |
162 if (iter != notifications_.end()) | |
163 EraseNotification(iter); | |
164 // Add the notification to the front (top) of the list and mark it | |
165 // unread and unshown. | |
166 if (!message_center_visible_) { | |
167 ++unread_count_; | |
168 notification.is_read = false; | |
169 notification.shown_as_popup = false; | |
170 } | |
171 notifications_.push_front(notification); | |
172 } | |
173 | |
174 void WebNotificationList::GetPopupIterators(Notifications::iterator& first, | |
175 Notifications::iterator& last) { | |
176 size_t popup_count = 0; | |
177 first = notifications_.begin(); | |
178 last = first; | |
179 while (last != notifications_.end()) { | |
180 if (last->shown_as_popup) | |
181 break; | |
182 ++last; | |
183 if (popup_count < kMaxVisiblePopupNotifications) | |
184 ++popup_count; | |
185 else | |
186 ++first; | |
187 } | |
188 } | |
189 | |
190 } // namespace message_center | |
OLD | NEW |