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 ~PopupTimersController() override
;
88 // MessageCenterObserver implementation.
89 void OnNotificationDisplayed(const std::string
& id
,
90 const DisplaySource source
) override
;
91 void OnNotificationUpdated(const std::string
& id
) override
;
92 void OnNotificationRemoved(const std::string
& id
, bool by_user
) override
;
94 // Callback for each timer when its time is up.
95 virtual void TimerFinished(const std::string
& id
);
97 // Pauses all running timers.
100 // Continues all managed timers.
103 // Removes all managed timers.
106 // Starts a timer (by creating a PopupTimer) for |id|.
107 void StartTimer(const std::string
& id
,
108 const base::TimeDelta
& timeout_in_seconds
);
110 // Stops a single timer, reverts it to a new timeout, and restarts it.
111 void ResetTimer(const std::string
& id
,
112 const base::TimeDelta
& timeout_in_seconds
);
114 // Pauses a single timer, such that it will continue where it left off after a
115 // call to StartAll or StartTimer.
116 void PauseTimer(const std::string
& id
);
118 // Removes and cancels a single popup timer, if it exists.
119 void CancelTimer(const std::string
& id
);
122 // Weak, this class is owned by MessageCenterImpl.
123 MessageCenter
* message_center_
;
125 // The PopupTimerCollection contains all the managed timers by their ID. They
126 // are owned by this class, and deleted by |popup_deleter_| on destructon or
127 // when explicitly cancelled.
128 typedef std::map
<std::string
, PopupTimer
*> PopupTimerCollection
;
129 PopupTimerCollection popup_timers_
;
130 STLValueDeleter
<PopupTimerCollection
> popup_deleter_
;
132 DISALLOW_COPY_AND_ASSIGN(PopupTimersController
);
135 } // namespace internal
137 // The default implementation of MessageCenter.
138 class MessageCenterImpl
: public MessageCenter
,
139 public NotificationBlocker::Observer
,
140 public message_center::NotifierSettingsObserver
{
143 ~MessageCenterImpl() override
;
145 // MessageCenter overrides:
146 void AddObserver(MessageCenterObserver
* observer
) override
;
147 void RemoveObserver(MessageCenterObserver
* observer
) override
;
148 void AddNotificationBlocker(NotificationBlocker
* blocker
) override
;
149 void RemoveNotificationBlocker(NotificationBlocker
* blocker
) override
;
150 void SetVisibility(Visibility visible
) override
;
151 bool IsMessageCenterVisible() const override
;
152 size_t NotificationCount() const override
;
153 size_t UnreadNotificationCount() const override
;
154 bool HasPopupNotifications() const override
;
155 bool IsQuietMode() const override
;
156 bool HasClickedListener(const std::string
& id
) override
;
157 message_center::Notification
* FindVisibleNotificationById(
158 const std::string
& id
) override
;
159 const NotificationList::Notifications
& GetVisibleNotifications() override
;
160 NotificationList::PopupNotifications
GetPopupNotifications() override
;
161 void AddNotification(scoped_ptr
<Notification
> notification
) override
;
162 void UpdateNotification(const std::string
& old_id
,
163 scoped_ptr
<Notification
> new_notification
) override
;
164 void RemoveNotification(const std::string
& id
, bool by_user
) override
;
165 void RemoveAllNotifications(bool by_user
) override
;
166 void RemoveAllVisibleNotifications(bool by_user
) override
;
167 void SetNotificationIcon(const std::string
& notification_id
,
168 const gfx::Image
& image
) override
;
169 void SetNotificationImage(const std::string
& notification_id
,
170 const gfx::Image
& image
) override
;
171 void SetNotificationButtonIcon(const std::string
& notification_id
,
173 const gfx::Image
& image
) override
;
174 void DisableNotificationsByNotifier(const NotifierId
& notifier_id
) override
;
175 void ClickOnNotification(const std::string
& id
) override
;
176 void ClickOnNotificationButton(const std::string
& id
,
177 int button_index
) override
;
178 void MarkSinglePopupAsShown(const std::string
& id
,
179 bool mark_notification_as_read
) override
;
180 void DisplayedNotification(const std::string
& id
,
181 const DisplaySource source
) override
;
182 void SetNotifierSettingsProvider(NotifierSettingsProvider
* provider
) override
;
183 NotifierSettingsProvider
* GetNotifierSettingsProvider() override
;
184 void SetQuietMode(bool in_quiet_mode
) override
;
185 void EnterQuietModeWithExpire(const base::TimeDelta
& expires_in
) override
;
186 void RestartPopupTimers() override
;
187 void PausePopupTimers() override
;
189 // NotificationBlocker::Observer overrides:
190 void OnBlockingStateChanged(NotificationBlocker
* blocker
) override
;
192 // message_center::NotifierSettingsObserver overrides:
193 void UpdateIconImage(const NotifierId
& notifier_id
,
194 const gfx::Image
& icon
) override
;
195 void NotifierGroupChanged() override
;
196 void NotifierEnabledChanged(const NotifierId
& notifier_id
,
197 bool enabled
) override
;
200 void DisableTimersForTest() override
;
203 struct NotificationCache
{
205 ~NotificationCache();
206 void Rebuild(const NotificationList::Notifications
& notifications
);
207 void RecountUnread();
209 NotificationList::Notifications visible_notifications
;
213 void RemoveNotifications(bool by_user
, const NotificationBlockers
& blockers
);
214 void RemoveNotificationsForNotifierId(const NotifierId
& notifier_id
);
216 scoped_ptr
<NotificationList
> notification_list_
;
217 NotificationCache notification_cache_
;
218 ObserverList
<MessageCenterObserver
> observer_list_
;
219 scoped_ptr
<internal::PopupTimersController
> popup_timers_controller_
;
220 scoped_ptr
<base::OneShotTimer
<MessageCenterImpl
> > quiet_mode_timer_
;
221 NotifierSettingsProvider
* settings_provider_
;
222 std::vector
<NotificationBlocker
*> blockers_
;
224 // Queue for the notifications to delay the addition/updates when the message
225 // center is visible.
226 scoped_ptr
<internal::ChangeQueue
> notification_queue_
;
228 DISALLOW_COPY_AND_ASSIGN(MessageCenterImpl
);
231 } // namespace message_center
233 #endif // UI_MESSAGE_CENTER_MESSAGE_CENTER_H_