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