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. 4 5 #ifndef UI_MESSAGE_CENTER_MESSAGE_CENTER_IMPL_H_ 6 #define UI_MESSAGE_CENTER_MESSAGE_CENTER_IMPL_H_ 7 8 #include <string> 9 10 #include "base/memory/weak_ptr.h" 11 #include "base/stl_util.h" 12 #include "base/time/time.h" 13 #include "base/timer/timer.h" 14 #include "ui/message_center/message_center.h" 15 #include "ui/message_center/message_center_observer.h" 16 17 namespace message_center { 18 class NotificationDelegate; 19 class MessageCenterImpl; 20 21 namespace internal { 22 class PopupTimersController; 23 24 // A class that manages timeout behavior for notification popups. One instance 25 // is created per notification popup. 26 class PopupTimer { 27 public: 28 // Accepts a notification ID, time until callback, and a reference to the 29 // controller which will be called back. The reference is a weak pointer so 30 // that timers never cause a callback on a destructed object. 31 PopupTimer(const std::string& id, 32 base::TimeDelta timeout, 33 base::WeakPtr<PopupTimersController> controller); 34 ~PopupTimer(); 35 36 // Starts running the timer. Barring a Pause or Reset call, the timer will 37 // call back to |controller| after |timeout| seconds. 38 void Start(); 39 40 // Stops the timer, and retains the amount of time that has passed so that on 41 // subsequent calls to Start the timer will continue where it left off. 42 void Pause(); 43 44 // Stops the timer, and resets the amount of time that has passed so that 45 // calling Start results in a timeout equal to the initial timeout setting. 46 void Reset(); 47 48 base::TimeDelta get_timeout() const { return timeout_; } 49 50 private: 51 // Notification ID for which this timer applies. 52 const std::string id_; 53 54 // Total time that should pass while active before calling TimerFinished. 55 base::TimeDelta timeout_; 56 57 // If paused, the amount of time that passed before pause. 58 base::TimeDelta passed_; 59 60 // The time that the timer was last started. 61 base::Time start_time_; 62 63 // Callback recipient. 64 base::WeakPtr<PopupTimersController> timer_controller_; 65 66 // The actual timer. 67 scoped_ptr<base::OneShotTimer<PopupTimersController> > timer_; 68 69 DISALLOW_COPY_AND_ASSIGN(PopupTimer); 70 }; 71 72 // A class that manages all the timers running for individual notification popup 73 // windows. It supports weak pointers in order to allow safe callbacks when 74 // timers expire. 75 class MESSAGE_CENTER_EXPORT PopupTimersController 76 : public base::SupportsWeakPtr<PopupTimersController>, 77 public MessageCenterObserver { 78 public: 79 explicit PopupTimersController(MessageCenter* message_center); 80 virtual ~PopupTimersController(); 81 82 // MessageCenterObserver implementation. 83 virtual void OnNotificationDisplayed(const std::string& id) OVERRIDE; 84 virtual void OnNotificationUpdated(const std::string& id) OVERRIDE; 85 virtual void OnNotificationRemoved(const std::string& id, bool by_user) 86 OVERRIDE; 87 88 // Callback for each timer when its time is up. 89 virtual void TimerFinished(const std::string& id); 90 91 // Pauses all running timers. 92 void PauseAll(); 93 94 // Continues all managed timers. 95 void StartAll(); 96 97 // Removes all managed timers. 98 void CancelAll(); 99 100 // Starts a timer (by creating a PopupTimer) for |id|. 101 void StartTimer(const std::string& id, 102 const base::TimeDelta& timeout_in_seconds); 103 104 // Stops a single timer, reverts it to a new timeout, and restarts it. 105 void ResetTimer(const std::string& id, 106 const base::TimeDelta& timeout_in_seconds); 107 108 // Pauses a single timer, such that it will continue where it left off after a 109 // call to StartAll or StartTimer. 110 void PauseTimer(const std::string& id); 111 112 // Removes and cancels a single popup timer, if it exists. 113 void CancelTimer(const std::string& id); 114 115 private: 116 // Weak, this class is owned by MessageCenterImpl. 117 MessageCenter* message_center_; 118 119 // The PopupTimerCollection contains all the managed timers by their ID. They 120 // are owned by this class, and deleted by |popup_deleter_| on destructon or 121 // when explicitly cancelled. 122 typedef std::map<std::string, PopupTimer*> PopupTimerCollection; 123 PopupTimerCollection popup_timers_; 124 STLValueDeleter<PopupTimerCollection> popup_deleter_; 125 126 DISALLOW_COPY_AND_ASSIGN(PopupTimersController); 127 }; 128 129 } // namespace internal 130 131 // The default implementation of MessageCenter. 132 class MessageCenterImpl : public MessageCenter { 133 public: 134 MessageCenterImpl(); 135 virtual ~MessageCenterImpl(); 136 137 // MessageCenter overrides: 138 virtual void SetDelegate(Delegate* delegate) OVERRIDE; 139 virtual void AddObserver(MessageCenterObserver* observer) OVERRIDE; 140 virtual void RemoveObserver(MessageCenterObserver* observer) OVERRIDE; 141 virtual void SetMessageCenterVisible(bool visible) OVERRIDE; 142 virtual bool IsMessageCenterVisible() OVERRIDE; 143 virtual size_t NotificationCount() const OVERRIDE; 144 virtual size_t UnreadNotificationCount() const OVERRIDE; 145 virtual bool HasPopupNotifications() const OVERRIDE; 146 virtual bool HasNotification(const std::string& id) OVERRIDE; 147 virtual bool IsQuietMode() const OVERRIDE; 148 virtual bool HasClickedListener(const std::string& id) OVERRIDE; 149 virtual const NotificationList::Notifications& GetNotifications() OVERRIDE; 150 virtual NotificationList::PopupNotifications GetPopupNotifications() OVERRIDE; 151 virtual void AddNotification(scoped_ptr<Notification> notification) OVERRIDE; 152 virtual void UpdateNotification(const std::string& old_id, 153 scoped_ptr<Notification> new_notification) 154 OVERRIDE; 155 virtual void RemoveNotification(const std::string& id, bool by_user) OVERRIDE; 156 virtual void RemoveAllNotifications(bool by_user) OVERRIDE; 157 virtual void SetNotificationIcon(const std::string& notification_id, 158 const gfx::Image& image) OVERRIDE; 159 virtual void SetNotificationImage(const std::string& notification_id, 160 const gfx::Image& image) OVERRIDE; 161 virtual void SetNotificationButtonIcon(const std::string& notification_id, 162 int button_index, 163 const gfx::Image& image) OVERRIDE; 164 virtual void DisableNotificationsByExtension(const std::string& id) OVERRIDE; 165 virtual void DisableNotificationsByUrl(const std::string& id) OVERRIDE; 166 virtual void ShowNotificationSettings(const std::string& id) OVERRIDE; 167 virtual void ExpandNotification(const std::string& id) OVERRIDE; 168 virtual void ClickOnNotification(const std::string& id) OVERRIDE; 169 virtual void ClickOnNotificationButton(const std::string& id, 170 int button_index) OVERRIDE; 171 virtual void MarkSinglePopupAsShown(const std::string& id, 172 bool mark_notification_as_read) OVERRIDE; 173 virtual void DisplayedNotification(const std::string& id) OVERRIDE; 174 virtual void SetNotifierSettingsProvider( 175 NotifierSettingsProvider* provider) OVERRIDE; 176 virtual NotifierSettingsProvider* GetNotifierSettingsProvider() OVERRIDE; 177 virtual void SetQuietMode(bool in_quiet_mode) OVERRIDE; 178 virtual void EnterQuietModeWithExpire( 179 const base::TimeDelta& expires_in) OVERRIDE; 180 virtual void RestartPopupTimers() OVERRIDE; 181 virtual void PausePopupTimers() OVERRIDE; 182 183 protected: 184 virtual void DisableTimersForTest() OVERRIDE; 185 186 private: 187 scoped_ptr<NotificationList> notification_list_; 188 ObserverList<MessageCenterObserver> observer_list_; 189 scoped_ptr<internal::PopupTimersController> popup_timers_controller_; 190 Delegate* delegate_; 191 NotifierSettingsProvider* settings_provider_; 192 193 DISALLOW_COPY_AND_ASSIGN(MessageCenterImpl); 194 }; 195 196 } // namespace message_center 197 198 #endif // UI_MESSAGE_CENTER_MESSAGE_CENTER_H_ 199