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)
125c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "base/gtest_prod_util.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/message_center/message_center_export.h"
14a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "ui/message_center/notification_blocker.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/message_center/notification_types.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base {
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DictionaryValue;
195c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuclass TimeDelta;
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liunamespace gfx {
235c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuclass Image;
245c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}
2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
265c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liunamespace message_center {
2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
28c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace test {
29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class NotificationListTest;
30c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuclass Notification;
335c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuclass NotificationDelegate;
345c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liustruct NotifierId;
355c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Comparers used to auto-sort the lists of Notifications.
37c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)struct MESSAGE_CENTER_EXPORT ComparePriorityTimestampSerial {
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool operator()(Notification* n1, Notification* n2);
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)struct CompareTimestampSerial {
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool operator()(Notification* n1, Notification* n2);
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A helper class to manage the list of notifications.
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MESSAGE_CENTER_EXPORT NotificationList {
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Auto-sorted set. Matches the order in which Notifications are shown in
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Notification Center.
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  typedef std::set<Notification*, ComparePriorityTimestampSerial> Notifications;
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Auto-sorted set used to return the Notifications to be shown as popup
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // toasts.
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  typedef std::set<Notification*, CompareTimestampSerial> PopupNotifications;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
56c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  explicit NotificationList();
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~NotificationList();
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
59c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Affects whether or not a message has been "read". Collects the set of
60c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // ids whose state have changed and set to |udpated_ids|. NULL if updated
61c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // ids don't matter.
62c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void SetMessageCenterVisible(bool visible,
63c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                               std::set<std::string>* updated_ids);
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void AddNotification(scoped_ptr<Notification> notification);
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void UpdateNotificationMessage(const std::string& old_id,
68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                 scoped_ptr<Notification> new_notification);
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void RemoveNotification(const std::string& id);
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 and that
88d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // notification's type matches the given type.
89d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  bool HasNotificationOfType(const std::string& id,
90d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                             const NotificationType type);
91d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns false if the first notification has been shown as a popup (which
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // means that all notifications have been shown).
94a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  bool HasPopupNotifications(const NotificationBlockers& blockers);
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns the recent notifications of the priority higher then LOW,
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // that have not been shown as a popup. kMaxVisiblePopupNotifications are
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // used to limit the number of notifications for the DEFAULT priority.
9958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // It also stores the list of notification ids which is blocked by |blockers|
10058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // to |blocked_ids|. |blocked_ids| can be NULL if the caller doesn't care
10158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // which notifications are blocked.
10258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  PopupNotifications GetPopupNotifications(
103a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      const NotificationBlockers& blockers,
10458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      std::list<std::string>* blocked_ids);
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Marks a specific popup item as shown. Set |mark_notification_as_read| to
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // true in case marking the notification as read too.
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void MarkSinglePopupAsShown(const std::string& id,
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                              bool mark_notification_as_read);
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
111c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Marks a specific popup item as displayed.
112c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void MarkSinglePopupAsDisplayed(const std::string& id);
113c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
11490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  NotificationDelegate* GetNotificationDelegate(const std::string& id);
11590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool quiet_mode() const { return quiet_mode_; }
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
118424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Sets the current quiet mode status to |quiet_mode|.
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetQuietMode(bool quiet_mode);
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Sets the current quiet mode to true. The quiet mode will expire in the
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // specified time-delta from now.
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void EnterQuietModeWithExpire(const base::TimeDelta& expires_in);
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
125f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Returns the notification with the corresponding id. If not found, returns
126f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // NULL. Notification instance is owned by this list.
127f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  Notification* GetNotificationById(const std::string& id);
128f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
129a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Returns all visible notifications, in a (priority-timestamp) order.
130a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Suitable for rendering notifications in a MessageCenter.
131a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  Notifications GetVisibleNotifications(
132a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      const NotificationBlockers& blockers) const;
133a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  size_t NotificationCount(const NotificationBlockers& blockers) const;
134a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  size_t UnreadCount(const NotificationBlockers& blockers) const;
135a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
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_;
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool quiet_mode_;
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(NotificationList);
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace message_center
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // UI_MESSAGE_CENTER_NOTIFICATION_LIST_H_
160