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_NOTIFICATION_H_
6 #define UI_MESSAGE_CENTER_NOTIFICATION_H_
11 #include "base/strings/string16.h"
12 #include "base/time/time.h"
13 #include "base/values.h"
14 #include "ui/gfx/image/image.h"
15 #include "ui/message_center/message_center_export.h"
16 #include "ui/message_center/notification_delegate.h"
17 #include "ui/message_center/notification_types.h"
18 #include "ui/message_center/notifier_settings.h"
21 namespace message_center
{
23 struct MESSAGE_CENTER_EXPORT NotificationItem
{
25 base::string16 message
;
27 NotificationItem(const base::string16
& title
, const base::string16
& message
);
30 struct MESSAGE_CENTER_EXPORT ButtonInfo
{
34 ButtonInfo(const base::string16
& title
);
37 class MESSAGE_CENTER_EXPORT RichNotificationData
{
39 RichNotificationData();
40 RichNotificationData(const RichNotificationData
& other
);
41 ~RichNotificationData();
46 base::string16 context_message
;
48 gfx::Image small_image
;
49 std::vector
<NotificationItem
> items
;
51 std::vector
<ButtonInfo
> buttons
;
52 bool should_make_spoken_feedback_for_popup_updates
;
54 std::vector
<int> vibration_pattern
;
58 class MESSAGE_CENTER_EXPORT Notification
{
60 Notification(NotificationType type
,
61 const std::string
& id
,
62 const base::string16
& title
,
63 const base::string16
& message
,
64 const gfx::Image
& icon
,
65 const base::string16
& display_source
,
66 const GURL
& origin_url
,
67 const NotifierId
& notifier_id
,
68 const RichNotificationData
& optional_fields
,
69 NotificationDelegate
* delegate
);
71 Notification(const std::string
& id
, const Notification
& other
);
73 Notification(const Notification
& other
);
75 virtual ~Notification();
77 // Copies the internal on-memory state from |base|, i.e. shown_as_popup,
78 // is_read, and never_timeout.
79 void CopyState(Notification
* base
);
81 NotificationType
type() const { return type_
; }
82 void set_type(NotificationType type
) { type_
= type
; }
84 // Uniquely identifies a notification in the message center. For
85 // notification front ends that support multiple profiles, this id should
86 // identify a unique profile + frontend_notification_id combination. You can
87 // Use this id against the MessageCenter interface but not the
88 // NotificationUIManager interface.
89 const std::string
& id() const { return id_
; }
91 const base::string16
& title() const { return title_
; }
92 void set_title(const base::string16
& title
) { title_
= title
; }
94 const base::string16
& message() const { return message_
; }
95 void set_message(const base::string16
& message
) { message_
= message
; }
97 // The origin URL of the script which requested the notification.
98 // Can be empty if the notification is requested by an extension or
100 const GURL
& origin_url() const { return origin_url_
; }
102 // A display string for the source of the notification.
103 const base::string16
& display_source() const { return display_source_
; }
105 const NotifierId
& notifier_id() const { return notifier_id_
; }
107 void set_profile_id(const std::string
& profile_id
) {
108 notifier_id_
.profile_id
= profile_id
;
111 // Begin unpacked values from optional_fields.
112 int priority() const { return optional_fields_
.priority
; }
113 void set_priority(int priority
) { optional_fields_
.priority
= priority
; }
115 // This vibration_pattern property currently has no effect on
116 // non-Android platforms.
117 const std::vector
<int>& vibration_pattern() const {
118 return optional_fields_
.vibration_pattern
;
120 void set_vibration_pattern(const std::vector
<int>& vibration_pattern
) {
121 optional_fields_
.vibration_pattern
= vibration_pattern
;
124 // This property currently has no effect on non-Android platforms.
125 bool silent() const { return optional_fields_
.silent
; }
126 void set_silent(bool silent
) { optional_fields_
.silent
= silent
; }
128 base::Time
timestamp() const { return optional_fields_
.timestamp
; }
129 void set_timestamp(const base::Time
& timestamp
) {
130 optional_fields_
.timestamp
= timestamp
;
133 const base::string16
context_message() const {
134 return optional_fields_
.context_message
;
137 void set_context_message(const base::string16
& context_message
) {
138 optional_fields_
.context_message
= context_message
;
141 // Decides if the notification origin should be used as a context message
142 bool UseOriginAsContextMessage() const;
144 const std::vector
<NotificationItem
>& items() const {
145 return optional_fields_
.items
;
147 void set_items(const std::vector
<NotificationItem
>& items
) {
148 optional_fields_
.items
= items
;
151 int progress() const { return optional_fields_
.progress
; }
152 void set_progress(int progress
) { optional_fields_
.progress
= progress
; }
153 // End unpacked values.
155 // Images fetched asynchronously.
156 const gfx::Image
& icon() const { return icon_
; }
157 void set_icon(const gfx::Image
& icon
) { icon_
= icon
; }
159 // Gets and sets whether to adjust the icon before displaying. The adjustment
160 // is designed to accomodate legacy HTML icons but isn't necessary for
161 // Chrome's hardcoded notifications. NB: this is currently ignored outside of
163 bool adjust_icon() const { return adjust_icon_
; }
164 void set_adjust_icon(bool adjust
) { adjust_icon_
= adjust
; }
166 const gfx::Image
& image() const { return optional_fields_
.image
; }
167 void set_image(const gfx::Image
& image
) { optional_fields_
.image
= image
; }
169 const gfx::Image
& small_image() const { return optional_fields_
.small_image
; }
170 void set_small_image(const gfx::Image
& image
) {
171 optional_fields_
.small_image
= image
;
174 // Buttons, with icons fetched asynchronously.
175 const std::vector
<ButtonInfo
>& buttons() const {
176 return optional_fields_
.buttons
;
178 void set_buttons(const std::vector
<ButtonInfo
>& buttons
) {
179 optional_fields_
.buttons
= buttons
;
181 void SetButtonIcon(size_t index
, const gfx::Image
& icon
);
183 bool shown_as_popup() const { return shown_as_popup_
; }
184 void set_shown_as_popup(bool shown_as_popup
) {
185 shown_as_popup_
= shown_as_popup
;
188 // Read status in the message center.
190 void set_is_read(bool read
) { is_read_
= read
; }
192 // Used to keep the order of notifications with the same timestamp.
193 // The notification with lesser serial_number is considered 'older'.
194 unsigned serial_number() { return serial_number_
; }
196 // Marks this explicitly to prevent the timeout dismiss of notification.
197 // This is used by webkit notifications to keep the existing behavior.
198 void set_never_timeout(bool never_timeout
) {
199 optional_fields_
.never_timeout
= never_timeout
;
202 bool never_timeout() const { return optional_fields_
.never_timeout
; }
204 bool clickable() const { return optional_fields_
.clickable
; }
205 void set_clickable(bool clickable
) {
206 optional_fields_
.clickable
= clickable
;
209 NotificationDelegate
* delegate() const { return delegate_
.get(); }
211 const RichNotificationData
& rich_notification_data() const {
212 return optional_fields_
;
215 // Set the priority to SYSTEM. The system priority user needs to call this
216 // method explicitly, to avoid setting it accidentally.
217 void SetSystemPriority();
220 void Display() const { delegate()->Display(); }
221 bool HasClickedListener() const { return delegate()->HasClickedListener(); }
222 void Click() const { delegate()->Click(); }
223 void ButtonClick(int index
) const { delegate()->ButtonClick(index
); }
224 void Close(bool by_user
) const { delegate()->Close(by_user
); }
226 // Helper method to create a simple system notification. |click_callback|
227 // will be invoked when the notification is clicked.
228 static scoped_ptr
<Notification
> CreateSystemNotification(
229 const std::string
& notification_id
,
230 const base::string16
& title
,
231 const base::string16
& message
,
232 const gfx::Image
& icon
,
233 const std::string
& system_component_id
,
234 const base::Closure
& click_callback
);
237 Notification
& operator=(const Notification
& other
);
239 // The type of notification we'd like displayed.
240 NotificationType type_
;
243 base::string16 title_
;
244 base::string16 message_
;
246 // Image data for the associated icon, used by Ash when available.
249 // True by default; controls whether to apply adjustments such as BG color and
250 // size scaling to |icon_|.
253 // The display string for the source of the notification. Could be
254 // the same as origin_url_, or the name of an extension.
255 base::string16 display_source_
;
258 // The origin URL of the script which requested the notification.
259 // Can be empty if requested through a chrome app or extension or if
260 // it's a system notification.
262 NotifierId notifier_id_
;
263 unsigned serial_number_
;
264 RichNotificationData optional_fields_
;
265 bool shown_as_popup_
; // True if this has been shown as a popup.
266 bool is_read_
; // True if this has been seen in the message center.
268 // A proxy object that allows access back to the JavaScript object that
269 // represents the notification, for firing events.
270 scoped_refptr
<NotificationDelegate
> delegate_
;
273 } // namespace message_center
275 #endif // UI_MESSAGE_CENTER_NOTIFICATION_H_