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_MESSAGE_CENTER_H_
      6 #define UI_MESSAGE_CENTER_MESSAGE_CENTER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/memory/scoped_ptr.h"
     11 #include "ui/message_center/message_center_export.h"
     12 #include "ui/message_center/message_center_types.h"
     13 #include "ui/message_center/notification_list.h"
     14 
     15 namespace base {
     16 class DictionaryValue;
     17 }
     18 
     19 // Interface to manage the NotificationList. The client (e.g. Chrome) calls
     20 // [Add|Remove|Update]Notification to create and update notifications in the
     21 // list. It also sends those changes to its observers when a notification
     22 // is shown, closed, or clicked on.
     23 //
     24 // MessageCenter is agnostic of profiles; it uses the string returned by
     25 // message_center::Notification::id() to uniquely identify a notification. It is
     26 // the caller's responsibility to formulate the id so that 2 different
     27 // notification should have different ids. For example, if the caller supports
     28 // multiple profiles, then caller should encode both profile characteristics and
     29 // notification front end's notification id into a new id and set it into the
     30 // notification instance before passing that in. Consequently the id passed to
     31 // observers will be this unique id, which can be used with MessageCenter
     32 // interface but probably not higher level interfaces.
     33 
     34 namespace message_center {
     35 
     36 namespace test {
     37 class MessagePopupCollectionTest;
     38 }
     39 
     40 class MessageCenterObserver;
     41 class NotificationBlocker;
     42 class NotifierSettingsProvider;
     43 
     44 class MESSAGE_CENTER_EXPORT MessageCenter {
     45  public:
     46   // Creates the global message center object.
     47   static void Initialize();
     48 
     49   // Returns the global message center object. Returns NULL if Initialize is not
     50   // called.
     51   static MessageCenter* Get();
     52 
     53   // Destroys the global message_center object.
     54   static void Shutdown();
     55 
     56   // Management of the observer list.
     57   virtual void AddObserver(MessageCenterObserver* observer) = 0;
     58   virtual void RemoveObserver(MessageCenterObserver* observer) = 0;
     59 
     60   // Queries of current notification list status.
     61   virtual size_t NotificationCount() const = 0;
     62   virtual size_t UnreadNotificationCount() const = 0;
     63   virtual bool HasPopupNotifications() const = 0;
     64   virtual bool IsQuietMode() const = 0;
     65   virtual bool HasClickedListener(const std::string& id) = 0;
     66 
     67   // Find the notification with the corresponding id. Returns NULL if not found.
     68   // The returned instance is owned by the message center.
     69   virtual message_center::Notification* FindVisibleNotificationById(
     70       const std::string& id) = 0;
     71 
     72   // Gets all notifications to be shown to the user in the message center.  Note
     73   // that queued changes due to the message center being open are not reflected
     74   // in this list.
     75   virtual const NotificationList::Notifications& GetVisibleNotifications() = 0;
     76 
     77   // Gets all notifications being shown as popups.  This should not be affected
     78   // by the change queue since notifications are not held up while the state is
     79   // VISIBILITY_TRANSIENT or VISIBILITY_SETTINGS.
     80   virtual NotificationList::PopupNotifications GetPopupNotifications() = 0;
     81 
     82   // Management of NotificationBlockers.
     83   virtual void AddNotificationBlocker(NotificationBlocker* blocker) = 0;
     84   virtual void RemoveNotificationBlocker(NotificationBlocker* blocker) = 0;
     85 
     86   // Basic operations of notification: add/remove/update.
     87 
     88   // Adds a new notification.
     89   virtual void AddNotification(scoped_ptr<Notification> notification) = 0;
     90 
     91   // Updates an existing notification with id = old_id and set its id to new_id.
     92   virtual void UpdateNotification(
     93       const std::string& old_id,
     94       scoped_ptr<Notification> new_notification) = 0;
     95 
     96   // Removes an existing notification.
     97   virtual void RemoveNotification(const std::string& id, bool by_user) = 0;
     98   virtual void RemoveAllNotifications(bool by_user) = 0;
     99   virtual void RemoveAllVisibleNotifications(bool by_user) = 0;
    100 
    101   // Sets the icon image. Icon appears at the top-left of the notification.
    102   virtual void SetNotificationIcon(const std::string& notification_id,
    103                                    const gfx::Image& image) = 0;
    104 
    105   // Sets the large image for the notifications of type == TYPE_IMAGE. Specified
    106   // image will appear below of the notification.
    107   virtual void SetNotificationImage(const std::string& notification_id,
    108                                     const gfx::Image& image) = 0;
    109 
    110   // Sets the image for the icon of the specific action button.
    111   virtual void SetNotificationButtonIcon(const std::string& notification_id,
    112                                          int button_index,
    113                                          const gfx::Image& image) = 0;
    114 
    115   // Operations happening especially from GUIs: click, disable, and settings.
    116   // Searches through the notifications and disables any that match the
    117   // extension id given.
    118   virtual void DisableNotificationsByNotifier(
    119       const NotifierId& notifier_id) = 0;
    120 
    121   // This should be called by UI classes when a notification is clicked to
    122   // trigger the notification's delegate callback and also update the message
    123   // center observers.
    124   virtual void ClickOnNotification(const std::string& id) = 0;
    125 
    126   // This should be called by UI classes when a notification button is clicked
    127   // to trigger the notification's delegate callback and also update the message
    128   // center observers.
    129   virtual void ClickOnNotificationButton(const std::string& id,
    130                                          int button_index) = 0;
    131 
    132   // This should be called by UI classes after a visible notification popup
    133   // closes, indicating that the notification has been shown to the user.
    134   // |mark_notification_as_read|, if false, will unset the read bit on a
    135   // notification, increasing the unread count of the center.
    136   virtual void MarkSinglePopupAsShown(const std::string& id,
    137                                       bool mark_notification_as_read) = 0;
    138 
    139   // This should be called by UI classes when a notification is first displayed
    140   // to the user, in order to decrement the unread_count for the tray, and to
    141   // notify observers that the notification is visible.
    142   virtual void DisplayedNotification(
    143       const std::string& id,
    144       const DisplaySource source) = 0;
    145 
    146   // Setter/getter of notifier settings provider. This will be a weak reference.
    147   // This should be set at the initialization process. The getter may return
    148   // NULL for tests.
    149   virtual void SetNotifierSettingsProvider(
    150       NotifierSettingsProvider* provider) = 0;
    151   virtual NotifierSettingsProvider* GetNotifierSettingsProvider() = 0;
    152 
    153   // This can be called to change the quiet mode state (without a timeout).
    154   virtual void SetQuietMode(bool in_quiet_mode) = 0;
    155 
    156   // Temporarily enables quiet mode for |expires_in| time.
    157   virtual void EnterQuietModeWithExpire(const base::TimeDelta& expires_in) = 0;
    158 
    159   // Informs the notification list whether the message center is visible.
    160   // This affects whether or not a message has been "read".
    161   virtual void SetVisibility(Visibility visible) = 0;
    162 
    163   // Allows querying the visibility of the center.
    164   virtual bool IsMessageCenterVisible() const = 0;
    165 
    166   // UI classes should call this when there is cause to leave popups visible for
    167   // longer than the default (for example, when the mouse hovers over a popup).
    168   virtual void PausePopupTimers() = 0;
    169 
    170   // UI classes should call this when the popup timers should restart (for
    171   // example, after the mouse leaves the popup.)
    172   virtual void RestartPopupTimers() = 0;
    173 
    174  protected:
    175   friend class TrayViewControllerTest;
    176   friend class test::MessagePopupCollectionTest;
    177   virtual void DisableTimersForTest() = 0;
    178 
    179   MessageCenter();
    180   virtual ~MessageCenter();
    181 
    182  private:
    183   DISALLOW_COPY_AND_ASSIGN(MessageCenter);
    184 };
    185 
    186 }  // namespace message_center
    187 
    188 #endif  // UI_MESSAGE_CENTER_MESSAGE_CENTER_H_
    189