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.
5 #ifndef UI_MESSAGE_CENTER_MESSAGE_CENTER_H_
6 #define UI_MESSAGE_CENTER_MESSAGE_CENTER_H_
10 #include "base/memory/scoped_ptr.h"
11 #include "base/observer_list.h"
12 #include "ui/gfx/native_widget_types.h"
13 #include "ui/message_center/message_center_export.h"
14 #include "ui/message_center/notification_change_observer.h"
15 #include "ui/message_center/notification_list.h"
16 #include "ui/message_center/notification_types.h"
19 class DictionaryValue
;
22 // Class for managing the NotificationList. The client (e.g. Chrome) calls
23 // [Add|Remove|Update]Notification to create and update notifications in the
24 // list. It can also implement Delegate to receive callbacks when a
25 // notification is removed (closed), or clicked on.
26 // If an Observer is provided, it will be informed when the notification list
27 // changes, and is expected to handle creating, showing, and hiding of any
30 namespace message_center
{
32 class MESSAGE_CENTER_EXPORT MessageCenter
: public NotificationChangeObserver
,
33 public NotificationList::Delegate
{
35 // Creates the global message center object.
36 static void Initialize();
38 // Returns the global message center object. Initialize must be called first.
39 static MessageCenter
* Get();
41 // Destroys the global message_center object.
42 static void Shutdown();
44 // Class that hosts the message center.
45 class MESSAGE_CENTER_EXPORT Observer
{
47 // Called when the notification list has changed. |new_notification| will
48 // be true if a notification was added or updated.
49 virtual void OnMessageCenterChanged(bool new_notification
) = 0;
51 virtual ~Observer() {}
54 class MESSAGE_CENTER_EXPORT Delegate
{
58 // Called when the notification associated with |notification_id| is
59 // removed (i.e. closed by the user).
60 virtual void NotificationRemoved(const std::string
& notification_id
,
63 // Request to disable the extension associated with |notification_id|.
64 virtual void DisableExtension(const std::string
& notification_id
) = 0;
66 // Request to disable notifications from the source of |notification_id|.
67 virtual void DisableNotificationsFromSource(
68 const std::string
& notification_id
) = 0;
70 // Request to show the notification settings (|notification_id| is used
71 // to identify the requesting browser context).
72 virtual void ShowSettings(const std::string
& notification_id
) = 0;
74 // Request to show the notification settings dialog. |context| is necessary
75 // to create a new window.
76 virtual void ShowSettingsDialog(gfx::NativeView context
) = 0;
78 // Called when the notification body is clicked on.
79 virtual void OnClicked(const std::string
& notification_id
) = 0;
81 // Called when a button in a notification is clicked. |button_index|
82 // indicates which button was clicked, zero-indexed (button one is 0,
85 // TODO(miket): consider providing default implementations for the pure
86 // virtuals above, to avoid changing so many files in disparate parts of
87 // the codebase each time we enhance this interface.
88 virtual void OnButtonClicked(const std::string
& id
, int button_index
) = 0;
91 // Called to set the delegate. Generally called only once, except in tests.
92 // Changing the delegate does not affect notifications in its
94 void SetDelegate(Delegate
* delegate
);
96 // Management of the observer list.
97 void AddObserver(Observer
* observer
);
98 void RemoveObserver(Observer
* observer
);
100 // Informs the notification list whether the message center is visible.
101 // This affects whether or not a message has been "read".
102 void SetMessageCenterVisible(bool visible
);
104 // Accessors to notification_list_
105 size_t NotificationCount() const;
106 size_t UnreadNotificationCount() const;
107 bool HasPopupNotifications() const;
109 // Adds a new notification. |id| is a unique identifier, used to update or
110 // remove notifications. |title| and |meesage| describe the notification text.
111 // Use SetNotificationIcon, SetNotificationImage, or SetNotificationButtonIcon
112 // to set images. If |extension_id| is provided then 'Disable extension' will
113 // appear in a dropdown menu and the id will be used to disable notifications
114 // from the extension. Otherwise if |display_source| is provided, a menu item
115 // showing the source and allowing notifications from that source to be
116 // disabled will be shown. All actual disabling is handled by the Delegate.
117 void AddNotification(NotificationType type
,
118 const std::string
& id
,
119 const string16
& title
,
120 const string16
& message
,
121 const string16
& display_source
,
122 const std::string
& extension_id
,
123 const base::DictionaryValue
* optional_fields
);
125 // Updates an existing notification with id = old_id and set its id to new_id.
126 // |optional_fields| can be NULL in case of no updates on those fields.
127 void UpdateNotification(const std::string
& old_id
,
128 const std::string
& new_id
,
129 const string16
& title
,
130 const string16
& message
,
131 const base::DictionaryValue
* optional_fields
);
133 // Removes an existing notification.
134 void RemoveNotification(const std::string
& id
);
136 void SetNotificationIcon(const std::string
& notification_id
,
137 const gfx::Image
& image
);
139 void SetNotificationImage(const std::string
& notification_id
,
140 const gfx::Image
& image
);
142 void SetNotificationButtonIcon(const std::string
& notification_id
,
144 const gfx::Image
& image
);
146 NotificationList
* notification_list() { return notification_list_
.get(); }
147 bool quiet_mode() const { return notification_list_
->quiet_mode(); }
149 // Overridden from NotificationChangeObserver:
150 virtual void OnRemoveNotification(const std::string
& id
, bool by_user
)
152 virtual void OnRemoveAllNotifications(bool by_user
) OVERRIDE
;
153 virtual void OnDisableNotificationsByExtension(const std::string
& id
)
155 virtual void OnDisableNotificationsByUrl(const std::string
& id
) OVERRIDE
;
156 virtual void OnShowNotificationSettings(const std::string
& id
) OVERRIDE
;
157 virtual void OnShowNotificationSettingsDialog(gfx::NativeView context
)
159 virtual void OnExpanded(const std::string
& id
) OVERRIDE
;
160 virtual void OnClicked(const std::string
& id
) OVERRIDE
;
161 virtual void OnButtonClicked(const std::string
& id
, int button_index
)
164 // Overridden from NotificationList::Delegate:
165 virtual void SendRemoveNotification(const std::string
& id
,
166 bool by_user
) OVERRIDE
;
167 virtual void OnQuietModeChanged(bool quiet_mode
) OVERRIDE
;
171 virtual ~MessageCenter();
174 // Calls OnMessageCenterChanged on each observer.
175 void NotifyMessageCenterChanged(bool new_notification
);
177 scoped_ptr
<NotificationList
> notification_list_
;
178 ObserverList
<Observer
> observer_list_
;
181 DISALLOW_COPY_AND_ASSIGN(MessageCenter
);
184 } // namespace message_center
186 #endif // UI_MESSAGE_CENTER_MESSAGE_CENTER_H_