notification_list.h revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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)
858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include <list>
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <set>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/strings/string16.h"
13eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h"
14eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/timer/timer.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/gfx/image/image.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/gfx/native_widget_types.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/message_center/message_center_export.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/message_center/notification.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/message_center/notification_types.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base {
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DictionaryValue;
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace message_center {
2690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)class NotificationBlocker;
2890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class NotificationDelegate;
2990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
30c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace test {
31c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class NotificationListTest;
32c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Comparers used to auto-sort the lists of Notifications.
35c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)struct MESSAGE_CENTER_EXPORT ComparePriorityTimestampSerial {
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool operator()(Notification* n1, Notification* n2);
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)struct CompareTimestampSerial {
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool operator()(Notification* n1, Notification* n2);
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A helper class to manage the list of notifications.
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MESSAGE_CENTER_EXPORT NotificationList {
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Auto-sorted set. Matches the order in which Notifications are shown in
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Notification Center.
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  typedef std::set<Notification*, ComparePriorityTimestampSerial> Notifications;
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Auto-sorted set used to return the Notifications to be shown as popup
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // toasts.
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  typedef std::set<Notification*, CompareTimestampSerial> PopupNotifications;
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
54c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  explicit NotificationList();
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~NotificationList();
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
57c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Affects whether or not a message has been "read". Collects the set of
58c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // ids whose state have changed and set to |udpated_ids|. NULL if updated
59c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // ids don't matter.
60c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void SetMessageCenterVisible(bool visible,
61c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                               std::set<std::string>* updated_ids);
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void AddNotification(scoped_ptr<Notification> notification);
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void UpdateNotificationMessage(const std::string& old_id,
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                 scoped_ptr<Notification> new_notification);
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void RemoveNotification(const std::string& id);
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RemoveAllNotifications();
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
72424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  Notifications GetNotificationsByNotifierId(const NotifierId& notifier_id);
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the notification exists and was updated.
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool SetNotificationIcon(const std::string& notification_id,
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                           const gfx::Image& image);
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns true if the notification exists and was updated.
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool SetNotificationImage(const std::string& notification_id,
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                            const gfx::Image& image);
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns true if the notification and button exist and were updated.
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool SetNotificationButtonIcon(const std::string& notification_id,
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                 int button_index,
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                 const gfx::Image& image);
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
87d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Returns true if |id| matches a notification in the list.
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HasNotification(const std::string& id);
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
90d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Returns true if |id| matches a notification in the list and that
91d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // notification's type matches the given type.
92d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  bool HasNotificationOfType(const std::string& id,
93d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                             const NotificationType type);
94d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns false if the first notification has been shown as a popup (which
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // means that all notifications have been shown).
9758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool HasPopupNotifications(const std::vector<NotificationBlocker*>& blockers);
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns the recent notifications of the priority higher then LOW,
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // that have not been shown as a popup. kMaxVisiblePopupNotifications are
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // used to limit the number of notifications for the DEFAULT priority.
10258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // It also stores the list of notification ids which is blocked by |blockers|
10358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // to |blocked_ids|. |blocked_ids| can be NULL if the caller doesn't care
10458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // which notifications are blocked.
10558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  PopupNotifications GetPopupNotifications(
10658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      const std::vector<NotificationBlocker*>& blockers,
10758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      std::list<std::string>* blocked_ids);
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Marks a specific popup item as shown. Set |mark_notification_as_read| to
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // true in case marking the notification as read too.
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void MarkSinglePopupAsShown(const std::string& id,
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                              bool mark_notification_as_read);
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
114c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Marks a specific popup item as displayed.
115c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void MarkSinglePopupAsDisplayed(const std::string& id);
116c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Marks the specified notification as expanded in the notification center.
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void MarkNotificationAsExpanded(const std::string& id);
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  NotificationDelegate* GetNotificationDelegate(const std::string& id);
12190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool quiet_mode() const { return quiet_mode_; }
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
124424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Sets the current quiet mode status to |quiet_mode|.
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetQuietMode(bool quiet_mode);
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Sets the current quiet mode to true. The quiet mode will expire in the
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // specified time-delta from now.
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void EnterQuietModeWithExpire(const base::TimeDelta& expires_in);
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns all notifications, in a (priority-timestamp) order. Suitable for
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // rendering notifications in a NotificationCenter.
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const Notifications& GetNotifications();
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  size_t NotificationCount() const;
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t unread_count() const { return unread_count_; }
136868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  bool is_message_center_visible() const { return message_center_visible_; }
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  friend class NotificationListTest;
1404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(NotificationListTest,
1414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                           TestPushingShownNotification);
142c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Iterates through the list and returns the first notification matching |id|.
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Notifications::iterator GetNotification(const std::string& id);
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void EraseNotification(Notifications::iterator iter);
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void PushNotification(scoped_ptr<Notification> notification);
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Notifications notifications_;
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool message_center_visible_;
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t unread_count_;
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool quiet_mode_;
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(NotificationList);
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace message_center
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // UI_MESSAGE_CENTER_NOTIFICATION_LIST_H_
161