message_center.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // Copyright (c) 2012 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. #ifndef UI_MESSAGE_CENTER_MESSAGE_CENTER_H_
  5. #define UI_MESSAGE_CENTER_MESSAGE_CENTER_H_
  6. #include <stddef.h>
  7. #include <memory>
  8. #include <string>
  9. #include "ui/message_center/message_center_export.h"
  10. #include "ui/message_center/message_center_types.h"
  11. #include "ui/message_center/notification_list.h"
  12. #include "ui/message_center/public/cpp/notification.h"
  13. #include "ui/message_center/public/cpp/notifier_id.h"
  14. class DownloadNotification;
  15. class DownloadNotificationTestBase;
  16. // Interface to manage the NotificationList. The client (e.g. Chrome) calls
  17. // [Add|Remove|Update]Notification to create and update notifications in the
  18. // list. It also sends those changes to its observers when a notification
  19. // is shown, closed, or clicked on.
  20. //
  21. // MessageCenter is agnostic of profiles; it uses the string returned by
  22. // Notification::id() to uniquely identify a notification. It is
  23. // the caller's responsibility to formulate the id so that 2 different
  24. // notification should have different ids. For example, if the caller supports
  25. // multiple profiles, then caller should encode both profile characteristics and
  26. // notification front end's notification id into a new id and set it into the
  27. // notification instance before passing that in. Consequently the id passed to
  28. // observers will be this unique id, which can be used with MessageCenter
  29. // interface but probably not higher level interfaces.
  30. namespace message_center {
  31. namespace test {
  32. class MessagePopupCollectionTest;
  33. }
  34. class LockScreenController;
  35. class MessageCenterObserver;
  36. class MessageCenterImplTest;
  37. class NotificationBlocker;
  38. class MESSAGE_CENTER_EXPORT MessageCenter {
  39. public:
  40. enum class RemoveType {
  41. // Remove all notifications.
  42. ALL,
  43. // Remove non-pinned notification (don't remove invisible ones).
  44. NON_PINNED,
  45. };
  46. // Creates the global message center object with default LockScreenController.
  47. static void Initialize();
  48. // Creates the global message center object with custom LockScreenController.
  49. static void Initialize(std::unique_ptr<LockScreenController> controller);
  50. // Returns the global message center object. Returns null if Initialize is
  51. // not called.
  52. static MessageCenter* Get();
  53. // Destroys the global message_center object.
  54. static void Shutdown();
  55. MessageCenter(const MessageCenter&) = delete;
  56. MessageCenter& operator=(const MessageCenter&) = delete;
  57. // Management of the observer list.
  58. virtual void AddObserver(MessageCenterObserver* observer) = 0;
  59. virtual void RemoveObserver(MessageCenterObserver* observer) = 0;
  60. // Queries of current notification list status.
  61. virtual size_t NotificationCount() const = 0;
  62. virtual bool HasPopupNotifications() const = 0;
  63. virtual bool IsQuietMode() const = 0;
  64. // Returns true if chrome vox spoken feedback is enabled.
  65. virtual bool IsSpokenFeedbackEnabled() const = 0;
  66. // Returns the notification with the corresponding id. If not found, returns
  67. // nullptr. Notification instance is owned by this list.
  68. virtual Notification* FindNotificationById(const std::string& id) = 0;
  69. // Find the parent notification for the corresponding notification. This is
  70. // the oldest notification with the same url. Returns nullptr if not found.
  71. // The returned instance is owned by the message center.
  72. virtual Notification* FindParentNotification(Notification* notification) = 0;
  73. virtual Notification* FindPopupNotificationById(const std::string& id) = 0;
  74. // Find the notification with the corresponding id. Returns null if not
  75. // found. The returned instance is owned by the message center.
  76. virtual Notification* FindVisibleNotificationById(const std::string& id) = 0;
  77. // Find all notifications with the corresponding |app_id|. Returns an
  78. // empty set if none are found.
  79. virtual NotificationList::Notifications FindNotificationsByAppId(
  80. const std::string& app_id) = 0;
  81. // Gets all notifications the message center knows about. These might contain
  82. // currently hidden ones due to any active NotificationBlockers.
  83. virtual NotificationList::Notifications GetNotifications() = 0;
  84. // Gets all notifications to be shown to the user in the message center. Note
  85. // that queued changes due to the message center being open are not reflected
  86. // in this list.
  87. virtual const NotificationList::Notifications& GetVisibleNotifications() = 0;
  88. // Gets all notifications being shown as popups. This should not be affected
  89. // by the change queue since notifications are not held up while the state is
  90. // VISIBILITY_TRANSIENT or VISIBILITY_SETTINGS.
  91. //
  92. // Popups returned by this method are assumed to have now been shown to the
  93. // user.
  94. virtual NotificationList::PopupNotifications GetPopupNotifications() = 0;
  95. // Gets all notifications that would be popups if not for the given blocker.
  96. // Ignores limits in the number of popups (e.g. for screen space).
  97. virtual NotificationList::PopupNotifications
  98. GetPopupNotificationsWithoutBlocker(
  99. const NotificationBlocker& blocker) const = 0;
  100. // Management of NotificationBlockers.
  101. virtual void AddNotificationBlocker(NotificationBlocker* blocker) = 0;
  102. virtual void RemoveNotificationBlocker(NotificationBlocker* blocker) = 0;
  103. // Basic operations of notification: add/remove/update.
  104. // Adds a new notification.
  105. virtual void AddNotification(std::unique_ptr<Notification> notification) = 0;
  106. // Updates an existing notification with id = old_id and set its id to new_id.
  107. virtual void UpdateNotification(
  108. const std::string& old_id,
  109. std::unique_ptr<Notification> new_notification) = 0;
  110. // Removes an existing notification.
  111. virtual void RemoveNotification(const std::string& id, bool by_user) = 0;
  112. virtual void RemoveNotificationsForNotifierId(
  113. const NotifierId& notifier_id) = 0;
  114. virtual void RemoveAllNotifications(bool by_user, RemoveType type) = 0;
  115. // Sets the icon image. Icon appears at the top-left of the notification.
  116. virtual void SetNotificationIcon(const std::string& notification_id,
  117. const ui::ImageModel& image) = 0;
  118. // Sets the large image for the notifications of type == TYPE_IMAGE. Specified
  119. // image will appear below of the notification.
  120. virtual void SetNotificationImage(const std::string& notification_id,
  121. const gfx::Image& image) = 0;
  122. // This should be called by UI classes when a notification is clicked to
  123. // trigger the notification's delegate callback and also update the message
  124. // center observers.
  125. virtual void ClickOnNotification(const std::string& id) = 0;
  126. // This should be called by UI classes when a notification button is clicked
  127. // to trigger the notification's delegate callback and also update the message
  128. // center observers.
  129. virtual void ClickOnNotificationButton(const std::string& id,
  130. int button_index) = 0;
  131. // This should be called by UI classes when a notification button with an
  132. // input is clicked to trigger the notification's delegate callback and also
  133. // update the message center observers.
  134. virtual void ClickOnNotificationButtonWithReply(
  135. const std::string& id,
  136. int button_index,
  137. const std::u16string& reply) = 0;
  138. // Called by the UI classes when the settings buttons is clicked
  139. // to trigger the notification's delegate and update the message
  140. // center observers.
  141. virtual void ClickOnSettingsButton(const std::string& id) = 0;
  142. // This should be called by UI classes when a user select from notification
  143. // inline settings to disable notifications from the same origin of the
  144. // notification.
  145. virtual void DisableNotification(const std::string& id) = 0;
  146. // This should be called by UI classes after a visible notification popup
  147. // closes, indicating that the notification has been shown to the user.
  148. // |mark_notification_as_read|, if false, will unset the read bit on a
  149. // notification, increasing the unread count of the center.
  150. virtual void MarkSinglePopupAsShown(const std::string& id,
  151. bool mark_notification_as_read) = 0;
  152. // Resets the timer for the popup associated with the provided notification
  153. // id.
  154. virtual void ResetPopupTimer(const std::string& id) = 0;
  155. // Resets the state for a popup so it is shown again.
  156. virtual void ResetSinglePopup(const std::string& id) = 0;
  157. // This should be called by UI classes when a notification is first displayed
  158. // to the user, in order to decrement the unread_count for the tray, and to
  159. // notify observers that the notification is visible.
  160. virtual void DisplayedNotification(const std::string& id,
  161. const DisplaySource source) = 0;
  162. // This can be called to change the quiet mode state (without a timeout).
  163. virtual void SetQuietMode(bool in_quiet_mode) = 0;
  164. // Used to set the spoken feedback state.
  165. virtual void SetSpokenFeedbackEnabled(bool enabled) = 0;
  166. // Temporarily enables quiet mode for |expires_in| time.
  167. virtual void EnterQuietModeWithExpire(const base::TimeDelta& expires_in) = 0;
  168. // Informs the notification list whether the message center is visible.
  169. // This affects whether or not a message has been "read".
  170. virtual void SetVisibility(Visibility visible) = 0;
  171. // Allows querying the visibility of the center.
  172. virtual bool IsMessageCenterVisible() const = 0;
  173. // Informs the MessageCenter whether there's a bubble anchored to a system
  174. // tray which holds notifications. If false, only toasts are shown (e.g. on
  175. // desktop Linux and Windows). When there's no message center view, updated
  176. // notifications will be re-appear as toasts even if they've already been
  177. // shown.
  178. virtual void SetHasMessageCenterView(bool has_message_center_view) = 0;
  179. virtual bool HasMessageCenterView() const = 0;
  180. // UI classes should call this when there is cause to leave popups visible for
  181. // longer than the default (for example, when the mouse hovers over a popup).
  182. virtual void PausePopupTimers() = 0;
  183. // UI classes should call this when the popup timers should restart (for
  184. // example, after the mouse leaves the popup.)
  185. virtual void RestartPopupTimers() = 0;
  186. // The user-visible "app name" for system-generated notifications, which is
  187. // used to identify the application that generated a notification. Only used
  188. // for MD style notifications, which means that currently it's only set and
  189. // used on Chrome OS. On Chrome OS, this is "Chrome OS".
  190. virtual const std::u16string& GetSystemNotificationAppName() const = 0;
  191. virtual void SetSystemNotificationAppName(const std::u16string& name) = 0;
  192. // Called when a message view associated with `notification_id` is hovered on.
  193. virtual void OnMessageViewHovered(const std::string& notification_id) = 0;
  194. protected:
  195. friend class ::DownloadNotification;
  196. friend class ::DownloadNotificationTestBase;
  197. friend class MessageCenterImplTest;
  198. friend class MessageCenterImplTestWithChangeQueue;
  199. friend class MessageCenterImplTestWithoutChangeQueue;
  200. friend class NotificationViewControllerTest;
  201. friend class UiControllerTest;
  202. friend class TrayViewControllerTest;
  203. friend class MessagePopupCollectionTest;
  204. virtual void DisableTimersForTest() = 0;
  205. MessageCenter();
  206. virtual ~MessageCenter();
  207. };
  208. } // namespace message_center
  209. #endif // UI_MESSAGE_CENTER_MESSAGE_CENTER_H_