Roll src/third_party/skia d32087a:1052f51
[chromium-blink-merge.git] / ui / message_center / message_center_impl.h
blob883d371903fb86cc578fe9f71a4445818ba5618e
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_
8 #include <string>
9 #include <vector>
11 #include "base/containers/scoped_ptr_map.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.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;
26 namespace internal {
27 class ChangeQueue;
28 class PopupTimersController;
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();
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();
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();
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();
54 base::TimeDelta get_timeout() const { return timeout_; }
56 private:
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_;
72 // The actual timer.
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
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 ~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.
98 void PauseAll();
100 // Continues all managed timers.
101 void StartAll();
103 // Removes all managed timers.
104 void CancelAll();
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);
121 private:
122 // Weak, this class is owned by MessageCenterImpl.
123 MessageCenter* message_center_;
125 // The PopupTimerCollection contains all the managed timers by their ID.
126 typedef base::ScopedPtrMap<std::string, scoped_ptr<PopupTimer>>
127 PopupTimerCollection;
128 PopupTimerCollection popup_timers_;
130 DISALLOW_COPY_AND_ASSIGN(PopupTimersController);
133 } // namespace internal
135 // The default implementation of MessageCenter.
136 class MessageCenterImpl : public MessageCenter,
137 public NotificationBlocker::Observer,
138 public message_center::NotifierSettingsObserver {
139 public:
140 MessageCenterImpl();
141 ~MessageCenterImpl() override;
143 // MessageCenter overrides:
144 void AddObserver(MessageCenterObserver* observer) override;
145 void RemoveObserver(MessageCenterObserver* observer) override;
146 void AddNotificationBlocker(NotificationBlocker* blocker) override;
147 void RemoveNotificationBlocker(NotificationBlocker* blocker) override;
148 void SetVisibility(Visibility visible) override;
149 bool IsMessageCenterVisible() const override;
150 size_t NotificationCount() const override;
151 size_t UnreadNotificationCount() const override;
152 bool HasPopupNotifications() const override;
153 bool IsQuietMode() const override;
154 bool HasClickedListener(const std::string& id) override;
155 message_center::Notification* FindVisibleNotificationById(
156 const std::string& id) override;
157 const NotificationList::Notifications& GetVisibleNotifications() override;
158 NotificationList::PopupNotifications GetPopupNotifications() override;
159 void AddNotification(scoped_ptr<Notification> notification) override;
160 void UpdateNotification(const std::string& old_id,
161 scoped_ptr<Notification> new_notification) override;
162 void RemoveNotification(const std::string& id, bool by_user) override;
163 void RemoveAllNotifications(bool by_user) override;
164 void RemoveAllVisibleNotifications(bool by_user) override;
165 void SetNotificationIcon(const std::string& notification_id,
166 const gfx::Image& image) override;
167 void SetNotificationImage(const std::string& notification_id,
168 const gfx::Image& image) override;
169 void SetNotificationButtonIcon(const std::string& notification_id,
170 int button_index,
171 const gfx::Image& image) override;
172 void DisableNotificationsByNotifier(const NotifierId& notifier_id) override;
173 void ClickOnNotification(const std::string& id) override;
174 void ClickOnNotificationButton(const std::string& id,
175 int button_index) override;
176 void MarkSinglePopupAsShown(const std::string& id,
177 bool mark_notification_as_read) override;
178 void DisplayedNotification(const std::string& id,
179 const DisplaySource source) override;
180 void SetNotifierSettingsProvider(NotifierSettingsProvider* provider) override;
181 NotifierSettingsProvider* GetNotifierSettingsProvider() override;
182 void SetQuietMode(bool in_quiet_mode) override;
183 void EnterQuietModeWithExpire(const base::TimeDelta& expires_in) override;
184 void RestartPopupTimers() override;
185 void PausePopupTimers() override;
186 void ForceNotificationFlush(const std::string& id) 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;
198 // Unexposed methods:
199 void AddNotificationImmediately(scoped_ptr<Notification> notification);
200 void UpdateNotificationImmediately(
201 const std::string& old_id,
202 scoped_ptr<Notification> new_notification);
203 void RemoveNotificationImmediately(const std::string& id, bool by_user);
205 protected:
206 void DisableTimersForTest() override;
208 private:
209 struct NotificationCache {
210 NotificationCache();
211 ~NotificationCache();
212 void Rebuild(const NotificationList::Notifications& notifications);
213 void RecountUnread();
215 NotificationList::Notifications visible_notifications;
216 size_t unread_count;
219 void RemoveNotifications(bool by_user, const NotificationBlockers& blockers);
220 void RemoveNotificationsForNotifierId(const NotifierId& notifier_id);
222 scoped_ptr<NotificationList> notification_list_;
223 NotificationCache notification_cache_;
224 base::ObserverList<MessageCenterObserver> observer_list_;
225 scoped_ptr<internal::PopupTimersController> popup_timers_controller_;
226 scoped_ptr<base::OneShotTimer<MessageCenterImpl> > quiet_mode_timer_;
227 NotifierSettingsProvider* settings_provider_;
228 std::vector<NotificationBlocker*> blockers_;
230 // Queue for the notifications to delay the addition/updates when the message
231 // center is visible.
232 scoped_ptr<internal::ChangeQueue> notification_queue_;
234 DISALLOW_COPY_AND_ASSIGN(MessageCenterImpl);
237 } // namespace message_center
239 #endif // UI_MESSAGE_CENTER_MESSAGE_CENTER_H_