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"
21 namespace message_center
{
22 class NotificationDelegate
;
23 class MessageCenterImpl
;
27 class PopupTimersController
;
29 // A class that manages timeout behavior for notification popups. One instance
30 // is created per notification popup.
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
);
41 // Starts running the timer. Barring a Pause or Reset call, the timer will
42 // call back to |controller| after |timeout| seconds.
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.
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.
53 base::TimeDelta
get_timeout() const { return timeout_
; }
56 // Notification ID for which this timer applies.
57 const std::string id_
;
59 // Total time that should pass while active before calling TimerFinished.
60 base::TimeDelta timeout_
;
62 // If paused, the amount of time that passed before pause.
63 base::TimeDelta passed_
;
65 // The time that the timer was last started.
66 base::Time start_time_
;
68 // Callback recipient.
69 base::WeakPtr
<PopupTimersController
> timer_controller_
;
72 scoped_ptr
<base::OneShotTimer
<PopupTimersController
> > timer_
;
74 DISALLOW_COPY_AND_ASSIGN(PopupTimer
);
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
80 class MESSAGE_CENTER_EXPORT PopupTimersController
81 : public base::SupportsWeakPtr
<PopupTimersController
>,
82 public MessageCenterObserver
{
84 explicit PopupTimersController(MessageCenter
* message_center
);
85 virtual ~PopupTimersController();
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
)
93 // Callback for each timer when its time is up.
94 virtual void TimerFinished(const std::string
& id
);
96 // Pauses all running timers.
99 // Continues all managed timers.
102 // Removes all managed timers.
105 // Starts a timer (by creating a PopupTimer) for |id|.
106 void StartTimer(const std::string
& id
,
107 const base::TimeDelta
& timeout_in_seconds
);
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
);
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
);
117 // Removes and cancels a single popup timer, if it exists.
118 void CancelTimer(const std::string
& id
);
121 // Weak, this class is owned by MessageCenterImpl.
122 MessageCenter
* message_center_
;
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_
;
131 DISALLOW_COPY_AND_ASSIGN(PopupTimersController
);
134 } // namespace internal
136 // The default implementation of MessageCenter.
137 class MessageCenterImpl
: public MessageCenter
,
138 public NotificationBlocker::Observer
{
141 virtual ~MessageCenterImpl();
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()
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
)
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
,
172 const gfx::Image
& image
) OVERRIDE
;
173 virtual void DisableNotificationsByNotifier(
174 const NotifierId
& notifier_id
) OVERRIDE
;
175 virtual void ClickOnNotification(const std::string
& id
) OVERRIDE
;
176 virtual void ClickOnNotificationButton(const std::string
& id
,
177 int button_index
) OVERRIDE
;
178 virtual void MarkSinglePopupAsShown(const std::string
& id
,
179 bool mark_notification_as_read
) OVERRIDE
;
180 virtual void DisplayedNotification(const std::string
& id
) OVERRIDE
;
181 virtual void SetNotifierSettingsProvider(
182 NotifierSettingsProvider
* provider
) OVERRIDE
;
183 virtual NotifierSettingsProvider
* GetNotifierSettingsProvider() OVERRIDE
;
184 virtual void SetQuietMode(bool in_quiet_mode
) OVERRIDE
;
185 virtual void EnterQuietModeWithExpire(
186 const base::TimeDelta
& expires_in
) OVERRIDE
;
187 virtual void RestartPopupTimers() OVERRIDE
;
188 virtual void PausePopupTimers() OVERRIDE
;
190 // NotificationBlocker::Observer overrides:
191 virtual void OnBlockingStateChanged(NotificationBlocker
* blocker
) OVERRIDE
;
194 virtual void DisableTimersForTest() OVERRIDE
;
197 struct NotificationCache
{
199 ~NotificationCache();
200 void Rebuild(const NotificationList::Notifications
& notifications
);
201 void RecountUnread();
203 NotificationList::Notifications visible_notifications
;
207 void RemoveNotifications(bool by_user
, const NotificationBlockers
& blockers
);
209 scoped_ptr
<NotificationList
> notification_list_
;
210 NotificationCache notification_cache_
;
211 ObserverList
<MessageCenterObserver
> observer_list_
;
212 scoped_ptr
<internal::PopupTimersController
> popup_timers_controller_
;
213 scoped_ptr
<base::OneShotTimer
<MessageCenterImpl
> > quiet_mode_timer_
;
214 NotifierSettingsProvider
* settings_provider_
;
215 std::vector
<NotificationBlocker
*> blockers_
;
217 // Queue for the notifications to delay the addition/updates when the message
218 // center is visible.
219 scoped_ptr
<internal::ChangeQueue
> notification_queue_
;
221 DISALLOW_COPY_AND_ASSIGN(MessageCenterImpl
);
224 } // namespace message_center
226 #endif // UI_MESSAGE_CENTER_MESSAGE_CENTER_H_