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 "base/observer_list.h"
     12 #include "ui/gfx/native_widget_types.h"
     13 #include "ui/message_center/message_center_export.h"
     14 #include "ui/message_center/notification_list.h"
     15 #include "ui/message_center/notification_types.h"
     16 
     17 class TrayViewControllerTest;
     18 
     19 namespace base {
     20 class DictionaryValue;
     21 }
     22 
     23 // Interface to manage the NotificationList. The client (e.g. Chrome) calls
     24 // [Add|Remove|Update]Notification to create and update notifications in the
     25 // list. It also sends those changes to its observers when a notification
     26 // is shown, closed, or clicked on.
     27 // It can also implement Delegate to ask platform-dependent features like
     28 // disabling extensions or opening settings.
     29 
     30 namespace message_center {
     31 
     32 class MessageCenterObserver;
     33 class NotificationList;
     34 class NotifierSettingsDelegate;
     35 class NotifierSettingsProvider;
     36 
     37 class MESSAGE_CENTER_EXPORT MessageCenter {
     38  public:
     39   // Creates the global message center object.
     40   static void Initialize();
     41 
     42   // Returns the global message center object. Initialize must be called first.
     43   static MessageCenter* Get();
     44 
     45   // Destroys the global message_center object.
     46   static void Shutdown();
     47 
     48   class MESSAGE_CENTER_EXPORT Delegate {
     49    public:
     50     virtual ~Delegate();
     51 
     52     // Request to disable the extension associated with |notification_id|.
     53     virtual void DisableExtension(const std::string& notification_id) = 0;
     54 
     55     // Request to disable notifications from the source of |notification_id|.
     56     virtual void DisableNotificationsFromSource(
     57         const std::string& notification_id) = 0;
     58 
     59     // Request to show the notification settings (|notification_id| is used
     60     // to identify the requesting browser context).
     61     virtual void ShowSettings(const std::string& notification_id) = 0;
     62   };
     63 
     64   // Called to set the delegate.  Generally called only once, except in tests.
     65   // Changing the delegate does not affect notifications in its
     66   // NotificationList.
     67   virtual void SetDelegate(Delegate* delegate) = 0;
     68 
     69   // Management of the observer list.
     70   virtual void AddObserver(MessageCenterObserver* observer) = 0;
     71   virtual void RemoveObserver(MessageCenterObserver* observer) = 0;
     72 
     73   // Queries of current notification list status.
     74   virtual size_t NotificationCount() const = 0;
     75   virtual size_t UnreadNotificationCount() const = 0;
     76   virtual bool HasPopupNotifications() const = 0;
     77   virtual bool HasNotification(const std::string& id) = 0;
     78   virtual bool IsQuietMode() const = 0;
     79   virtual bool HasClickedListener(const std::string& id) = 0;
     80 
     81   // Getters of the current notifications.
     82   virtual const NotificationList::Notifications& GetNotifications() = 0;
     83   // Gets all notifications being shown as popups.
     84   virtual NotificationList::PopupNotifications GetPopupNotifications() = 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 
    100   // Sets the icon image. Icon appears at the top-left of the notification.
    101   virtual void SetNotificationIcon(const std::string& notification_id,
    102                                    const gfx::Image& image) = 0;
    103 
    104   // Sets the large image for the notifications of type == TYPE_IMAGE. Specified
    105   // image will appear below of the notification.
    106   virtual void SetNotificationImage(const std::string& notification_id,
    107                                     const gfx::Image& image) = 0;
    108 
    109   // Sets the image for the icon of the specific action button.
    110   virtual void SetNotificationButtonIcon(const std::string& notification_id,
    111                                          int button_index,
    112                                          const gfx::Image& image) = 0;
    113 
    114   // Operations happening especially from GUIs: click, expand, disable,
    115   // and settings.
    116   // Searches through the notifications and disables any that match the
    117   // extension id given.
    118   virtual void DisableNotificationsByExtension(const std::string& id) = 0;
    119 
    120   // Disables all notifications that match the given url by querying the
    121   // delegate and also by matching display_source.
    122   // TODO(dewittj): Is display_source matching necessary?
    123   virtual void DisableNotificationsByUrl(const std::string& url) = 0;
    124 
    125   // TODO(mukai): settings can be in another class?
    126   // Shows the settings for a web notification (profile is identified by the
    127   // given notification id).
    128   virtual void ShowNotificationSettings(const std::string& id) = 0;
    129 
    130   // Reformat a notification to show its entire text content.
    131   virtual void ExpandNotification(const std::string& id) = 0;
    132 
    133   // This should be called by UI classes when a notification is clicked to
    134   // trigger the notification's delegate callback and also update the message
    135   // center observers.
    136   virtual void ClickOnNotification(const std::string& id) = 0;
    137 
    138   // This should be called by UI classes when a notification button is clicked
    139   // to trigger the notification's delegate callback and also update the message
    140   // center observers.
    141   virtual void ClickOnNotificationButton(const std::string& id,
    142                                          int button_index) = 0;
    143 
    144   // This should be called by UI classes after a visible notification popup
    145   // closes, indicating that the notification has been shown to the user.
    146   // |mark_notification_as_read|, if false, will unset the read bit on a
    147   // notification, increasing the unread count of the center.
    148   virtual void MarkSinglePopupAsShown(const std::string& id,
    149                                       bool mark_notification_as_read) = 0;
    150 
    151   // This should be called by UI classes when a notification is first displayed
    152   // to the user, in order to decrement the unread_count for the tray, and to
    153   // notify observers that the notification is visible.
    154   virtual void DisplayedNotification(const std::string& id) = 0;
    155 
    156   // Setter/getter of notifier settings provider. This will be a weak reference.
    157   // This should be set at the initialization process. The getter may return
    158   // NULL for tests.
    159   virtual void SetNotifierSettingsProvider(
    160       NotifierSettingsProvider* provider) = 0;
    161   virtual NotifierSettingsProvider* GetNotifierSettingsProvider() = 0;
    162 
    163   // This can be called to change the quiet mode state (without a timeout).
    164   virtual void SetQuietMode(bool in_quiet_mode) = 0;
    165 
    166   // Temporarily enables quiet mode for |expires_in| time.
    167   virtual void EnterQuietModeWithExpire(const base::TimeDelta& expires_in) = 0;
    168 
    169   // Informs the notification list whether the message center is visible.
    170   // This affects whether or not a message has been "read".
    171   virtual void SetMessageCenterVisible(bool visible) = 0;
    172 
    173   // Allows querying the visibility of the center.
    174   virtual bool IsMessageCenterVisible() = 0;
    175 
    176   // UI classes should call this when there is cause to leave popups visible for
    177   // longer than the default (for example, when the mouse hovers over a popup).
    178   virtual void PausePopupTimers() = 0;
    179 
    180   // UI classes should call this when the popup timers should restart (for
    181   // example, after the mouse leaves the popup.)
    182   virtual void RestartPopupTimers() = 0;
    183 
    184  protected:
    185   friend class ::TrayViewControllerTest;
    186   virtual void DisableTimersForTest() = 0;
    187 
    188   MessageCenter();
    189   virtual ~MessageCenter();
    190 
    191  private:
    192   DISALLOW_COPY_AND_ASSIGN(MessageCenter);
    193 };
    194 
    195 }  // namespace message_center
    196 
    197 #endif  // UI_MESSAGE_CENTER_MESSAGE_CENTER_H_
    198