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/containers/scoped_ptr_map.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/time/time.h"
16 #include "base/timer/timer.h"
17 #include "ui/message_center/message_center.h"
18 #include "ui/message_center/message_center_observer.h"
19 #include "ui/message_center/message_center_types.h"
20 #include "ui/message_center/notification_blocker.h"
21 #include "ui/message_center/notifier_settings.h"
23 namespace message_center
{
24 class NotificationDelegate
;
25 class MessageCenterImpl
;
29 class PopupTimersController
;
31 // A class that manages timeout behavior for notification popups. One instance
32 // is created per notification popup.
35 // Accepts a notification ID, time until callback, and a reference to the
36 // controller which will be called back. The reference is a weak pointer so
37 // that timers never cause a callback on a destructed object.
38 PopupTimer(const std::string
& id
,
39 base::TimeDelta timeout
,
40 base::WeakPtr
<PopupTimersController
> controller
);
43 // Starts running the timer. Barring a Pause or Reset call, the timer will
44 // call back to |controller| after |timeout| seconds.
47 // Stops the timer, and retains the amount of time that has passed so that on
48 // subsequent calls to Start the timer will continue where it left off.
51 // Stops the timer, and resets the amount of time that has passed so that
52 // calling Start results in a timeout equal to the initial timeout setting.
55 base::TimeDelta
get_timeout() const { return timeout_
; }
58 // Notification ID for which this timer applies.
59 const std::string id_
;
61 // Total time that should pass while active before calling TimerFinished.
62 base::TimeDelta timeout_
;
64 // If paused, the amount of time that passed before pause.
65 base::TimeDelta passed_
;
67 // The time that the timer was last started.
68 base::Time start_time_
;
70 // Callback recipient.
71 base::WeakPtr
<PopupTimersController
> timer_controller_
;
74 scoped_ptr
<base::OneShotTimer
<PopupTimersController
> > timer_
;
76 DISALLOW_COPY_AND_ASSIGN(PopupTimer
);
79 // A class that manages all the timers running for individual notification popup
80 // windows. It supports weak pointers in order to allow safe callbacks when
82 class MESSAGE_CENTER_EXPORT PopupTimersController
83 : public base::SupportsWeakPtr
<PopupTimersController
>,
84 public MessageCenterObserver
{
86 explicit PopupTimersController(MessageCenter
* message_center
);
87 ~PopupTimersController() override
;
89 // MessageCenterObserver implementation.
90 void OnNotificationDisplayed(const std::string
& id
,
91 const DisplaySource source
) override
;
92 void OnNotificationUpdated(const std::string
& id
) override
;
93 void OnNotificationRemoved(const std::string
& id
, bool by_user
) override
;
95 // Callback for each timer when its time is up.
96 virtual void TimerFinished(const std::string
& id
);
98 // Pauses all running timers.
101 // Continues all managed timers.
104 // Removes all managed timers.
107 // Starts a timer (by creating a PopupTimer) for |id|.
108 void StartTimer(const std::string
& id
,
109 const base::TimeDelta
& timeout_in_seconds
);
111 // Stops a single timer, reverts it to a new timeout, and restarts it.
112 void ResetTimer(const std::string
& id
,
113 const base::TimeDelta
& timeout_in_seconds
);
115 // Pauses a single timer, such that it will continue where it left off after a
116 // call to StartAll or StartTimer.
117 void PauseTimer(const std::string
& id
);
119 // Removes and cancels a single popup timer, if it exists.
120 void CancelTimer(const std::string
& id
);
123 // Weak, this class is owned by MessageCenterImpl.
124 MessageCenter
* message_center_
;
126 // The PopupTimerCollection contains all the managed timers by their ID.
127 typedef base::ScopedPtrMap
<std::string
, scoped_ptr
<PopupTimer
>>
128 PopupTimerCollection
;
129 PopupTimerCollection popup_timers_
;
131 DISALLOW_COPY_AND_ASSIGN(PopupTimersController
);
134 } // namespace internal
136 // The default implementation of MessageCenter.
137 class MessageCenterImpl
: public MessageCenter
,
138 public NotificationBlocker::Observer
,
139 public message_center::NotifierSettingsObserver
{
142 ~MessageCenterImpl() override
;
144 // MessageCenter overrides:
145 void AddObserver(MessageCenterObserver
* observer
) override
;
146 void RemoveObserver(MessageCenterObserver
* observer
) override
;
147 void AddNotificationBlocker(NotificationBlocker
* blocker
) override
;
148 void RemoveNotificationBlocker(NotificationBlocker
* blocker
) override
;
149 void SetVisibility(Visibility visible
) override
;
150 bool IsMessageCenterVisible() const override
;
151 size_t NotificationCount() const override
;
152 size_t UnreadNotificationCount() const override
;
153 bool HasPopupNotifications() const override
;
154 bool IsQuietMode() const override
;
155 bool HasClickedListener(const std::string
& id
) override
;
156 message_center::Notification
* FindVisibleNotificationById(
157 const std::string
& id
) override
;
158 const NotificationList::Notifications
& GetVisibleNotifications() override
;
159 NotificationList::PopupNotifications
GetPopupNotifications() override
;
160 void AddNotification(scoped_ptr
<Notification
> notification
) override
;
161 void UpdateNotification(const std::string
& old_id
,
162 scoped_ptr
<Notification
> new_notification
) override
;
163 void RemoveNotification(const std::string
& id
, bool by_user
) override
;
164 void RemoveAllNotifications(bool by_user
) override
;
165 void RemoveAllVisibleNotifications(bool by_user
) override
;
166 void SetNotificationIcon(const std::string
& notification_id
,
167 const gfx::Image
& image
) override
;
168 void SetNotificationImage(const std::string
& notification_id
,
169 const gfx::Image
& image
) override
;
170 void SetNotificationButtonIcon(const std::string
& notification_id
,
172 const gfx::Image
& image
) override
;
173 void DisableNotificationsByNotifier(const NotifierId
& notifier_id
) override
;
174 void ClickOnNotification(const std::string
& id
) override
;
175 void ClickOnNotificationButton(const std::string
& id
,
176 int button_index
) override
;
177 void MarkSinglePopupAsShown(const std::string
& id
,
178 bool mark_notification_as_read
) override
;
179 void DisplayedNotification(const std::string
& id
,
180 const DisplaySource source
) override
;
181 void SetNotifierSettingsProvider(NotifierSettingsProvider
* provider
) override
;
182 NotifierSettingsProvider
* GetNotifierSettingsProvider() override
;
183 void SetQuietMode(bool in_quiet_mode
) override
;
184 void EnterQuietModeWithExpire(const base::TimeDelta
& expires_in
) override
;
185 void RestartPopupTimers() override
;
186 void PausePopupTimers() override
;
188 // NotificationBlocker::Observer overrides:
189 void OnBlockingStateChanged(NotificationBlocker
* blocker
) override
;
191 // message_center::NotifierSettingsObserver overrides:
192 void UpdateIconImage(const NotifierId
& notifier_id
,
193 const gfx::Image
& icon
) override
;
194 void NotifierGroupChanged() override
;
195 void NotifierEnabledChanged(const NotifierId
& notifier_id
,
196 bool enabled
) override
;
199 void DisableTimersForTest() override
;
202 struct NotificationCache
{
204 ~NotificationCache();
205 void Rebuild(const NotificationList::Notifications
& notifications
);
206 void RecountUnread();
208 NotificationList::Notifications visible_notifications
;
212 void RemoveNotifications(bool by_user
, const NotificationBlockers
& blockers
);
213 void RemoveNotificationsForNotifierId(const NotifierId
& notifier_id
);
215 scoped_ptr
<NotificationList
> notification_list_
;
216 NotificationCache notification_cache_
;
217 base::ObserverList
<MessageCenterObserver
> observer_list_
;
218 scoped_ptr
<internal::PopupTimersController
> popup_timers_controller_
;
219 scoped_ptr
<base::OneShotTimer
<MessageCenterImpl
> > quiet_mode_timer_
;
220 NotifierSettingsProvider
* settings_provider_
;
221 std::vector
<NotificationBlocker
*> blockers_
;
223 // Queue for the notifications to delay the addition/updates when the message
224 // center is visible.
225 scoped_ptr
<internal::ChangeQueue
> notification_queue_
;
227 DISALLOW_COPY_AND_ASSIGN(MessageCenterImpl
);
230 } // namespace message_center
232 #endif // UI_MESSAGE_CENTER_MESSAGE_CENTER_H_