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