notification_list.h revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
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)
117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/strings/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)
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void AddNotification(scoped_ptr<Notification> notification);
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void UpdateNotificationMessage(const std::string& old_id,
64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                 scoped_ptr<Notification> new_notification);
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void RemoveNotification(const std::string& id);
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RemoveAllNotifications();
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
70c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  Notifications GetNotificationsBySource(const std::string& id);
71c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  Notifications GetNotificationsByExtension(const std::string& id);
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the notification exists and was updated.
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool SetNotificationIcon(const std::string& notification_id,
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                           const gfx::Image& image);
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns true if the notification exists and was updated.
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool SetNotificationImage(const std::string& notification_id,
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                            const gfx::Image& image);
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns true if the notification and button exist and were updated.
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool SetNotificationButtonIcon(const std::string& notification_id,
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                 int button_index,
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                 const gfx::Image& image);
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HasNotification(const std::string& id);
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns false if the first notification has been shown as a popup (which
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // means that all notifications have been shown).
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HasPopupNotifications();
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns the recent notifications of the priority higher then LOW,
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // that have not been shown as a popup. kMaxVisiblePopupNotifications are
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // used to limit the number of notifications for the DEFAULT priority.
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The returned list is sorted by timestamp, newer first.
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PopupNotifications GetPopupNotifications();
97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  Notification* GetPopup(const std::string& id);
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Marks the popups for the |priority| as shown.
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void MarkPopupsAsShown(int priority);
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Marks a specific popup item as shown. Set |mark_notification_as_read| to
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // true in case marking the notification as read too.
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void MarkSinglePopupAsShown(const std::string& id,
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                              bool mark_notification_as_read);
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
107c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Marks a specific popup item as displayed.
108c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void MarkSinglePopupAsDisplayed(const std::string& id);
109c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Marks the specified notification as expanded in the notification center.
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void MarkNotificationAsExpanded(const std::string& id);
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  NotificationDelegate* GetNotificationDelegate(const std::string& id);
11490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool quiet_mode() const { return quiet_mode_; }
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Sets the current quiet mode status to |quiet_mode|. The new status is not
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // expired.
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)
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns all notifications, in a (priority-timestamp) order. Suitable for
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // rendering notifications in a NotificationCenter.
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const Notifications& GetNotifications();
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  size_t NotificationCount() const;
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t unread_count() const { return unread_count_; }
130868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  bool is_message_center_visible() const { return message_center_visible_; }
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
133c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  friend class test::NotificationListTest;
134c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Iterates through the list and returns the first notification matching |id|.
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Notifications::iterator GetNotification(const std::string& id);
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void EraseNotification(Notifications::iterator iter);
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void PushNotification(scoped_ptr<Notification> notification);
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Sets the current quiet mode status to |quiet_mode|.
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetQuietModeInternal(bool quiet_mode);
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Notifications notifications_;
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool message_center_visible_;
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t unread_count_;
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool quiet_mode_;
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<base::OneShotTimer<NotificationList> > quiet_mode_timer_;
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(NotificationList);
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace message_center
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // UI_MESSAGE_CENTER_NOTIFICATION_LIST_H_
157