notification_list.h revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef UI_MESSAGE_CENTER_NOTIFICATION_LIST_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define UI_MESSAGE_CENTER_NOTIFICATION_LIST_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <set>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/string16.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/time.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/timer.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/gfx/image/image.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/gfx/native_widget_types.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/message_center/message_center_export.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/message_center/notification.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/message_center/notification_types.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base {
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DictionaryValue;
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace message_center {
2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class NotificationDelegate;
2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
28c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace test {
29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class NotificationListTest;
30c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Comparers used to auto-sort the lists of Notifications.
33c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)struct MESSAGE_CENTER_EXPORT ComparePriorityTimestampSerial {
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool operator()(Notification* n1, Notification* n2);
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)struct CompareTimestampSerial {
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool operator()(Notification* n1, Notification* n2);
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A helper class to manage the list of notifications.
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MESSAGE_CENTER_EXPORT NotificationList {
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Auto-sorted set. Matches the order in which Notifications are shown in
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Notification Center.
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  typedef std::set<Notification*, ComparePriorityTimestampSerial> Notifications;
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Auto-sorted set used to return the Notifications to be shown as popup
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // toasts.
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  typedef std::set<Notification*, CompareTimestampSerial> PopupNotifications;
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
52c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  explicit NotificationList();
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~NotificationList();
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Affects whether or not a message has been "read". Collects the set of
56c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // ids whose state have changed and set to |udpated_ids|. NULL if updated
57c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // ids don't matter.
58c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void SetMessageCenterVisible(bool visible,
59c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                               std::set<std::string>* updated_ids);
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void AddNotification(NotificationType type,
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       const std::string& id,
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       const string16& title,
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       const string16& message,
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       const string16& display_source,
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       const std::string& extension_id,
6790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                       const base::DictionaryValue* optional_fields,
6890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                       NotificationDelegate* delegate);
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void UpdateNotificationMessage(const std::string& old_id,
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 const std::string& new_id,
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 const string16& title,
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                 const string16& message,
7490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                 const base::DictionaryValue* optional_fields,
7590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                 NotificationDelegate* delegate);
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void RemoveNotification(const std::string& id);
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RemoveAllNotifications();
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
81c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  Notifications GetNotificationsBySource(const std::string& id);
82c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  Notifications GetNotificationsByExtension(const std::string& id);
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the notification exists and was updated.
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool SetNotificationIcon(const std::string& notification_id,
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                           const gfx::Image& image);
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns true if the notification exists and was updated.
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool SetNotificationImage(const std::string& notification_id,
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                            const gfx::Image& image);
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns true if the notification and button exist and were updated.
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool SetNotificationButtonIcon(const std::string& notification_id,
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                 int button_index,
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                 const gfx::Image& image);
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HasNotification(const std::string& id);
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns false if the first notification has been shown as a popup (which
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // means that all notifications have been shown).
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HasPopupNotifications();
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns the recent notifications of the priority higher then LOW,
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // that have not been shown as a popup. kMaxVisiblePopupNotifications are
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // used to limit the number of notifications for the DEFAULT priority.
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The returned list is sorted by timestamp, newer first.
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PopupNotifications GetPopupNotifications();
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Marks the popups for the |priority| as shown.
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void MarkPopupsAsShown(int priority);
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Marks a specific popup item as shown. Set |mark_notification_as_read| to
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // true in case marking the notification as read too.
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void MarkSinglePopupAsShown(const std::string& id,
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                              bool mark_notification_as_read);
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
117c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Marks a specific popup item as displayed.
118c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void MarkSinglePopupAsDisplayed(const std::string& id);
119c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Marks the specified notification as expanded in the notification center.
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void MarkNotificationAsExpanded(const std::string& id);
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  NotificationDelegate* GetNotificationDelegate(const std::string& id);
12490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool quiet_mode() const { return quiet_mode_; }
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Sets the current quiet mode status to |quiet_mode|. The new status is not
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // expired.
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetQuietMode(bool quiet_mode);
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Sets the current quiet mode to true. The quiet mode will expire in the
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // specified time-delta from now.
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void EnterQuietModeWithExpire(const base::TimeDelta& expires_in);
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns all notifications, in a (priority-timestamp) order. Suitable for
1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // rendering notifications in a NotificationCenter.
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const Notifications& GetNotifications();
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  size_t NotificationCount() const;
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t unread_count() const { return unread_count_; }
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
142c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  friend class test::NotificationListTest;
143c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Iterates through the list and returns the first notification matching |id|.
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Notifications::iterator GetNotification(const std::string& id);
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void EraseNotification(Notifications::iterator iter);
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void PushNotification(scoped_ptr<Notification> notification);
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Sets the current quiet mode status to |quiet_mode|.
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetQuietModeInternal(bool quiet_mode);
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Notifications notifications_;
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool message_center_visible_;
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t unread_count_;
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool quiet_mode_;
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<base::OneShotTimer<NotificationList> > quiet_mode_timer_;
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(NotificationList);
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace message_center
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // UI_MESSAGE_CENTER_NOTIFICATION_LIST_H_
166