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.
5 #ifndef UI_MESSAGE_CENTER_MESSAGE_CENTER_IMPL_H_
6 #define UI_MESSAGE_CENTER_MESSAGE_CENTER_IMPL_H_
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"
22 namespace message_center
{
23 class NotificationDelegate
;
24 class MessageCenterImpl
;
28 class PopupTimersController
;
30 // A class that manages timeout behavior for notification popups. One instance
31 // is created per notification popup.
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
);
42 // Starts running the timer. Barring a Pause or Reset call, the timer will
43 // call back to |controller| after |timeout| seconds.
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.
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.
54 base::TimeDelta
get_timeout() const { return timeout_
; }
57 // Notification ID for which this timer applies.
58 const std::string id_
;
60 // Total time that should pass while active before calling TimerFinished.
61 base::TimeDelta timeout_
;
63 // If paused, the amount of time that passed before pause.
64 base::TimeDelta passed_
;
66 // The time that the timer was last started.
67 base::Time start_time_
;
69 // Callback recipient.
70 base::WeakPtr
<PopupTimersController
> timer_controller_
;
73 scoped_ptr
<base::OneShotTimer
<PopupTimersController
> > timer_
;
75 DISALLOW_COPY_AND_ASSIGN(PopupTimer
);
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
81 class MESSAGE_CENTER_EXPORT PopupTimersController
82 : public base::SupportsWeakPtr
<PopupTimersController
>,
83 public MessageCenterObserver
{
85 explicit PopupTimersController(MessageCenter
* message_center
);
86 virtual ~PopupTimersController();
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
)
96 // Callback for each timer when its time is up.
97 virtual void TimerFinished(const std::string
& id
);
99 // Pauses all running timers.
102 // Continues all managed timers.
105 // Removes all managed timers.
108 // Starts a timer (by creating a PopupTimer) for |id|.
109 void StartTimer(const std::string
& id
,
110 const base::TimeDelta
& timeout_in_seconds
);
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
);
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
);
120 // Removes and cancels a single popup timer, if it exists.
121 void CancelTimer(const std::string
& id
);
124 // Weak, this class is owned by MessageCenterImpl.
125 MessageCenter
* message_center_
;
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_
;
134 DISALLOW_COPY_AND_ASSIGN(PopupTimersController
);
137 } // namespace internal
139 // The default implementation of MessageCenter.
140 class MessageCenterImpl
: public MessageCenter
,
141 public NotificationBlocker::Observer
,
142 public message_center::NotifierSettingsObserver
{
145 virtual ~MessageCenterImpl();
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 HasNotification(const std::string
& id
) OVERRIDE
;
158 virtual bool IsQuietMode() const OVERRIDE
;
159 virtual bool HasClickedListener(const std::string
& id
) OVERRIDE
;
160 virtual const NotificationList::Notifications
& GetVisibleNotifications()
162 virtual NotificationList::PopupNotifications
GetPopupNotifications() OVERRIDE
;
163 virtual void AddNotification(scoped_ptr
<Notification
> notification
) OVERRIDE
;
164 virtual void UpdateNotification(const std::string
& old_id
,
165 scoped_ptr
<Notification
> new_notification
)
167 virtual void RemoveNotification(const std::string
& id
, bool by_user
) OVERRIDE
;
168 virtual void RemoveAllNotifications(bool by_user
) OVERRIDE
;
169 virtual void RemoveAllVisibleNotifications(bool by_user
) OVERRIDE
;
170 virtual void SetNotificationIcon(const std::string
& notification_id
,
171 const gfx::Image
& image
) OVERRIDE
;
172 virtual void SetNotificationImage(const std::string
& notification_id
,
173 const gfx::Image
& image
) OVERRIDE
;
174 virtual void SetNotificationButtonIcon(const std::string
& notification_id
,
176 const gfx::Image
& image
) OVERRIDE
;
177 virtual void DisableNotificationsByNotifier(
178 const NotifierId
& notifier_id
) OVERRIDE
;
179 virtual void ClickOnNotification(const std::string
& id
) OVERRIDE
;
180 virtual void ClickOnNotificationButton(const std::string
& id
,
181 int button_index
) OVERRIDE
;
182 virtual void MarkSinglePopupAsShown(const std::string
& id
,
183 bool mark_notification_as_read
) OVERRIDE
;
184 virtual void DisplayedNotification(
185 const std::string
& id
,
186 const DisplaySource source
) OVERRIDE
;
187 virtual void SetNotifierSettingsProvider(
188 NotifierSettingsProvider
* provider
) OVERRIDE
;
189 virtual NotifierSettingsProvider
* GetNotifierSettingsProvider() OVERRIDE
;
190 virtual void SetQuietMode(bool in_quiet_mode
) OVERRIDE
;
191 virtual void EnterQuietModeWithExpire(
192 const base::TimeDelta
& expires_in
) OVERRIDE
;
193 virtual void RestartPopupTimers() OVERRIDE
;
194 virtual void PausePopupTimers() OVERRIDE
;
196 // NotificationBlocker::Observer overrides:
197 virtual void OnBlockingStateChanged(NotificationBlocker
* blocker
) OVERRIDE
;
199 // message_center::NotifierSettingsObserver overrides:
200 virtual void UpdateIconImage(const NotifierId
& notifier_id
,
201 const gfx::Image
& icon
) OVERRIDE
;
202 virtual void NotifierGroupChanged() OVERRIDE
;
203 virtual void NotifierEnabledChanged(const NotifierId
& notifier_id
,
204 bool enabled
) OVERRIDE
;
207 virtual void DisableTimersForTest() OVERRIDE
;
210 struct NotificationCache
{
212 ~NotificationCache();
213 void Rebuild(const NotificationList::Notifications
& notifications
);
214 void RecountUnread();
216 NotificationList::Notifications visible_notifications
;
220 void RemoveNotifications(bool by_user
, const NotificationBlockers
& blockers
);
221 void RemoveNotificationsForNotifierId(const NotifierId
& notifier_id
);
223 scoped_ptr
<NotificationList
> notification_list_
;
224 NotificationCache notification_cache_
;
225 ObserverList
<MessageCenterObserver
> observer_list_
;
226 scoped_ptr
<internal::PopupTimersController
> popup_timers_controller_
;
227 scoped_ptr
<base::OneShotTimer
<MessageCenterImpl
> > quiet_mode_timer_
;
228 NotifierSettingsProvider
* settings_provider_
;
229 std::vector
<NotificationBlocker
*> blockers_
;
231 // Queue for the notifications to delay the addition/updates when the message
232 // center is visible.
233 scoped_ptr
<internal::ChangeQueue
> notification_queue_
;
235 DISALLOW_COPY_AND_ASSIGN(MessageCenterImpl
);
238 } // namespace message_center
240 #endif // UI_MESSAGE_CENTER_MESSAGE_CENTER_H_