Home | History | Annotate | Download | only in message_center
      1 // Copyright (c) 2013 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_IMPL_H_
      6 #define UI_MESSAGE_CENTER_MESSAGE_CENTER_IMPL_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/memory/scoped_vector.h"
     12 #include "base/memory/weak_ptr.h"
     13 #include "base/stl_util.h"
     14 #include "base/time/time.h"
     15 #include "base/timer/timer.h"
     16 #include "ui/message_center/message_center.h"
     17 #include "ui/message_center/message_center_observer.h"
     18 #include "ui/message_center/message_center_types.h"
     19 #include "ui/message_center/notification_blocker.h"
     20 
     21 namespace message_center {
     22 class NotificationDelegate;
     23 class MessageCenterImpl;
     24 
     25 namespace internal {
     26 class ChangeQueue;
     27 class PopupTimersController;
     28 
     29 // A class that manages timeout behavior for notification popups.  One instance
     30 // is created per notification popup.
     31 class PopupTimer {
     32  public:
     33   // Accepts a notification ID, time until callback, and a reference to the
     34   // controller which will be called back.  The reference is a weak pointer so
     35   // that timers never cause a callback on a destructed object.
     36   PopupTimer(const std::string& id,
     37              base::TimeDelta timeout,
     38              base::WeakPtr<PopupTimersController> controller);
     39   ~PopupTimer();
     40 
     41   // Starts running the timer.  Barring a Pause or Reset call, the timer will
     42   // call back to |controller| after |timeout| seconds.
     43   void Start();
     44 
     45   // Stops the timer, and retains the amount of time that has passed so that on
     46   // subsequent calls to Start the timer will continue where it left off.
     47   void Pause();
     48 
     49   // Stops the timer, and resets the amount of time that has passed so that
     50   // calling Start results in a timeout equal to the initial timeout setting.
     51   void Reset();
     52 
     53   base::TimeDelta get_timeout() const { return timeout_; }
     54 
     55  private:
     56   // Notification ID for which this timer applies.
     57   const std::string id_;
     58 
     59   // Total time that should pass while active before calling TimerFinished.
     60   base::TimeDelta timeout_;
     61 
     62   // If paused, the amount of time that passed before pause.
     63   base::TimeDelta passed_;
     64 
     65   // The time that the timer was last started.
     66   base::Time start_time_;
     67 
     68   // Callback recipient.
     69   base::WeakPtr<PopupTimersController> timer_controller_;
     70 
     71   // The actual timer.
     72   scoped_ptr<base::OneShotTimer<PopupTimersController> > timer_;
     73 
     74   DISALLOW_COPY_AND_ASSIGN(PopupTimer);
     75 };
     76 
     77 // A class that manages all the timers running for individual notification popup
     78 // windows.  It supports weak pointers in order to allow safe callbacks when
     79 // timers expire.
     80 class MESSAGE_CENTER_EXPORT PopupTimersController
     81     : public base::SupportsWeakPtr<PopupTimersController>,
     82       public MessageCenterObserver {
     83  public:
     84   explicit PopupTimersController(MessageCenter* message_center);
     85   virtual ~PopupTimersController();
     86 
     87   // MessageCenterObserver implementation.
     88   virtual void OnNotificationDisplayed(const std::string& id) OVERRIDE;
     89   virtual void OnNotificationUpdated(const std::string& id) OVERRIDE;
     90   virtual void OnNotificationRemoved(const std::string& id, bool by_user)
     91       OVERRIDE;
     92 
     93   // Callback for each timer when its time is up.
     94   virtual void TimerFinished(const std::string& id);
     95 
     96   // Pauses all running timers.
     97   void PauseAll();
     98 
     99   // Continues all managed timers.
    100   void StartAll();
    101 
    102   // Removes all managed timers.
    103   void CancelAll();
    104 
    105   // Starts a timer (by creating a PopupTimer) for |id|.
    106   void StartTimer(const std::string& id,
    107                   const base::TimeDelta& timeout_in_seconds);
    108 
    109   // Stops a single timer, reverts it to a new timeout, and restarts it.
    110   void ResetTimer(const std::string& id,
    111                   const base::TimeDelta& timeout_in_seconds);
    112 
    113   // Pauses a single timer, such that it will continue where it left off after a
    114   // call to StartAll or StartTimer.
    115   void PauseTimer(const std::string& id);
    116 
    117   // Removes and cancels a single popup timer, if it exists.
    118   void CancelTimer(const std::string& id);
    119 
    120  private:
    121   // Weak, this class is owned by MessageCenterImpl.
    122   MessageCenter* message_center_;
    123 
    124   // The PopupTimerCollection contains all the managed timers by their ID.  They
    125   // are owned by this class, and deleted by |popup_deleter_| on destructon or
    126   // when explicitly cancelled.
    127   typedef std::map<std::string, PopupTimer*> PopupTimerCollection;
    128   PopupTimerCollection popup_timers_;
    129   STLValueDeleter<PopupTimerCollection> popup_deleter_;
    130 
    131   DISALLOW_COPY_AND_ASSIGN(PopupTimersController);
    132 };
    133 
    134 }  // namespace internal
    135 
    136 // The default implementation of MessageCenter.
    137 class MessageCenterImpl : public MessageCenter,
    138                           public NotificationBlocker::Observer {
    139  public:
    140   MessageCenterImpl();
    141   virtual ~MessageCenterImpl();
    142 
    143   // MessageCenter overrides:
    144   virtual void AddObserver(MessageCenterObserver* observer) OVERRIDE;
    145   virtual void RemoveObserver(MessageCenterObserver* observer) OVERRIDE;
    146   virtual void AddNotificationBlocker(NotificationBlocker* blocker) OVERRIDE;
    147   virtual void RemoveNotificationBlocker(NotificationBlocker* blocker) OVERRIDE;
    148   virtual void SetVisibility(Visibility visible) OVERRIDE;
    149   virtual bool IsMessageCenterVisible() const OVERRIDE;
    150   virtual size_t NotificationCount() const OVERRIDE;
    151   virtual size_t UnreadNotificationCount() const OVERRIDE;
    152   virtual bool HasPopupNotifications() const OVERRIDE;
    153   virtual bool HasNotification(const std::string& id) OVERRIDE;
    154   virtual bool IsQuietMode() const OVERRIDE;
    155   virtual bool HasClickedListener(const std::string& id) OVERRIDE;
    156   virtual const NotificationList::Notifications& GetVisibleNotifications()
    157       OVERRIDE;
    158   virtual NotificationList::PopupNotifications GetPopupNotifications() OVERRIDE;
    159   virtual void AddNotification(scoped_ptr<Notification> notification) OVERRIDE;
    160   virtual void UpdateNotification(const std::string& old_id,
    161                                   scoped_ptr<Notification> new_notification)
    162       OVERRIDE;
    163   virtual void RemoveNotification(const std::string& id, bool by_user) OVERRIDE;
    164   virtual void RemoveAllNotifications(bool by_user) OVERRIDE;
    165   virtual void RemoveAllVisibleNotifications(bool by_user) OVERRIDE;
    166   virtual void SetNotificationIcon(const std::string& notification_id,
    167                                    const gfx::Image& image) OVERRIDE;
    168   virtual void SetNotificationImage(const std::string& notification_id,
    169                                     const gfx::Image& image) OVERRIDE;
    170   virtual void SetNotificationButtonIcon(const std::string& notification_id,
    171                                          int button_index,
    172                                          const gfx::Image& image) OVERRIDE;
    173   virtual void DisableNotificationsByNotifier(
    174       const NotifierId& notifier_id) OVERRIDE;
    175   virtual void ExpandNotification(const std::string& id) OVERRIDE;
    176   virtual void ClickOnNotification(const std::string& id) OVERRIDE;
    177   virtual void ClickOnNotificationButton(const std::string& id,
    178                                          int button_index) OVERRIDE;
    179   virtual void MarkSinglePopupAsShown(const std::string& id,
    180                                       bool mark_notification_as_read) OVERRIDE;
    181   virtual void DisplayedNotification(const std::string& id) OVERRIDE;
    182   virtual void SetNotifierSettingsProvider(
    183       NotifierSettingsProvider* provider) OVERRIDE;
    184   virtual NotifierSettingsProvider* GetNotifierSettingsProvider() OVERRIDE;
    185   virtual void SetQuietMode(bool in_quiet_mode) OVERRIDE;
    186   virtual void EnterQuietModeWithExpire(
    187       const base::TimeDelta& expires_in) OVERRIDE;
    188   virtual void RestartPopupTimers() OVERRIDE;
    189   virtual void PausePopupTimers() OVERRIDE;
    190 
    191   // NotificationBlocker::Observer overrides:
    192   virtual void OnBlockingStateChanged(NotificationBlocker* blocker) OVERRIDE;
    193 
    194  protected:
    195   virtual void DisableTimersForTest() OVERRIDE;
    196 
    197  private:
    198   struct NotificationCache {
    199     NotificationCache();
    200     ~NotificationCache();
    201     void Rebuild(const NotificationList::Notifications& notificaitons);
    202     void RecountUnread();
    203 
    204     NotificationList::Notifications visible_notifications;
    205     size_t unread_count;
    206   };
    207 
    208   void RemoveNotifications(bool by_user, const NotificationBlockers& blockers);
    209 
    210   scoped_ptr<NotificationList> notification_list_;
    211   NotificationCache notification_cache_;
    212   ObserverList<MessageCenterObserver> observer_list_;
    213   scoped_ptr<internal::PopupTimersController> popup_timers_controller_;
    214   scoped_ptr<base::OneShotTimer<MessageCenterImpl> > quiet_mode_timer_;
    215   NotifierSettingsProvider* settings_provider_;
    216   std::vector<NotificationBlocker*> blockers_;
    217 
    218   // Queue for the notifications to delay the addition/updates when the message
    219   // center is visible.
    220   scoped_ptr<internal::ChangeQueue> notification_queue_;
    221 
    222   DISALLOW_COPY_AND_ASSIGN(MessageCenterImpl);
    223 };
    224 
    225 }  // namespace message_center
    226 
    227 #endif  // UI_MESSAGE_CENTER_MESSAGE_CENTER_H_
    228