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