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