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_NOTIFICATION_H_
6#define UI_MESSAGE_CENTER_NOTIFICATION_H_
7
8#include <string>
9#include <vector>
10
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"
19
20namespace message_center {
21
22struct MESSAGE_CENTER_EXPORT NotificationItem {
23  string16 title;
24  string16 message;
25
26  NotificationItem(const string16& title, const string16& message);
27};
28
29struct MESSAGE_CENTER_EXPORT ButtonInfo {
30  string16 title;
31  gfx::Image icon;
32
33  ButtonInfo(const string16& title);
34};
35
36class MESSAGE_CENTER_EXPORT RichNotificationData {
37 public:
38  RichNotificationData();
39  RichNotificationData(const RichNotificationData& other);
40  ~RichNotificationData();
41
42  int priority;
43  bool never_timeout;
44  base::Time timestamp;
45  string16 expanded_message;
46  string16 context_message;
47  gfx::Image image;
48  std::vector<NotificationItem> items;
49  int progress;
50  std::vector<ButtonInfo> buttons;
51  bool should_make_spoken_feedback_for_popup_updates;
52  bool clickable;
53};
54
55class MESSAGE_CENTER_EXPORT Notification {
56 public:
57  Notification(NotificationType type,
58               const std::string& id,
59               const string16& title,
60               const string16& message,
61               const gfx::Image& icon,
62               const string16& display_source,
63               const NotifierId& notifier_id,
64               const RichNotificationData& optional_fields,
65               NotificationDelegate* delegate);
66
67  Notification(const Notification& other);
68  Notification& operator=(const Notification& other);
69  virtual ~Notification();
70
71  // Copies the internal on-memory state from |base|, i.e. shown_as_popup,
72  // is_read, is_expanded, and never_timeout.
73  void CopyState(Notification* base);
74
75  NotificationType type() const { return type_; }
76  void set_type(NotificationType type) { type_ = type; }
77
78  const std::string& id() const { return id_; }
79
80  const string16& title() const { return title_; }
81  void set_title(const string16& title) { title_ = title; }
82
83  const string16& message() const { return message_; }
84  void set_message(const string16& message) { message_ = message; }
85
86  // A display string for the source of the notification.
87  const string16& display_source() const { return display_source_; }
88
89  const NotifierId& notifier_id() const { return notifier_id_; }
90
91  void set_profile_id(const std::string& profile_id) {
92    notifier_id_.profile_id = profile_id;
93  }
94
95  // Begin unpacked values from optional_fields.
96  int priority() const { return optional_fields_.priority; }
97  void set_priority(int priority) { optional_fields_.priority = priority; }
98
99  base::Time timestamp() const { return optional_fields_.timestamp; }
100  void set_timestamp(const base::Time& timestamp) {
101    optional_fields_.timestamp = timestamp;
102  }
103
104  const string16& expanded_message() const {
105    return optional_fields_.expanded_message;
106  }
107  void set_expanded_message(const string16& expanded_message) {
108    optional_fields_.expanded_message = expanded_message;
109  }
110
111  const string16& context_message() const {
112    return optional_fields_.context_message;
113  }
114  void set_context_message(const string16& context_message) {
115    optional_fields_.context_message = context_message;
116  }
117
118  const std::vector<NotificationItem>& items() const {
119    return optional_fields_.items;
120  }
121  void set_items(const std::vector<NotificationItem>& items) {
122    optional_fields_.items = items;
123  }
124
125  int progress() const { return optional_fields_.progress; }
126  void set_progress(int progress) { optional_fields_.progress = progress; }
127  // End unpacked values.
128
129  // Images fetched asynchronously.
130  const gfx::Image& icon() const { return icon_; }
131  void set_icon(const gfx::Image& icon) { icon_ = icon; }
132
133  const gfx::Image& image() const { return optional_fields_.image; }
134  void set_image(const gfx::Image& image) { optional_fields_.image = image; }
135
136  // Buttons, with icons fetched asynchronously.
137  const std::vector<ButtonInfo>& buttons() const {
138    return optional_fields_.buttons;
139  }
140  void set_buttons(const std::vector<ButtonInfo>& buttons) {
141    optional_fields_.buttons = buttons;
142  }
143  void SetButtonIcon(size_t index, const gfx::Image& icon);
144
145  bool shown_as_popup() const { return shown_as_popup_; }
146  void set_shown_as_popup(bool shown_as_popup) {
147    shown_as_popup_ = shown_as_popup;
148  }
149
150  // Read status in the message center.
151  bool IsRead() const;
152  void set_is_read(bool read) { is_read_ = read; }
153
154  // Expanded status in the message center (not the popups).
155  bool is_expanded() const { return is_expanded_; }
156  void set_is_expanded(bool expanded) { is_expanded_ = expanded; }
157
158  // Used to keep the order of notifications with the same timestamp.
159  // The notification with lesser serial_number is considered 'older'.
160  unsigned serial_number() { return serial_number_; }
161
162  // Marks this explicitly to prevent the timeout dismiss of notification.
163  // This is used by webkit notifications to keep the existing behavior.
164  void set_never_timeout(bool never_timeout) {
165    optional_fields_.never_timeout = never_timeout;
166  }
167
168  bool never_timeout() const { return optional_fields_.never_timeout; }
169
170  bool clickable() const { return optional_fields_.clickable; }
171  void set_clickable(bool clickable) {
172    optional_fields_.clickable = clickable;
173  }
174
175  NotificationDelegate* delegate() const { return delegate_.get(); }
176
177  const RichNotificationData& rich_notification_data() const {
178    return optional_fields_;
179  }
180
181  // Set the priority to SYSTEM. The system priority user needs to call this
182  // method explicitly, to avoid setting it accidentally.
183  void SetSystemPriority();
184
185  // Delegate actions.
186  void Display() const { delegate()->Display(); }
187  void Error() const { delegate()->Error(); }
188  bool HasClickedListener() const { return delegate()->HasClickedListener(); }
189  void Click() const { delegate()->Click(); }
190  void ButtonClick(int index) const { delegate()->ButtonClick(index); }
191  void Close(bool by_user) const { delegate()->Close(by_user); }
192
193  // Helper method to create a simple system notification. |click_callback|
194  // will be invoked when the notification is clicked.
195  static scoped_ptr<Notification> CreateSystemNotification(
196      const std::string& notification_id,
197      const base::string16& title,
198      const base::string16& message,
199      const gfx::Image& icon,
200      const std::string& system_component_id,
201      const base::Closure& click_callback);
202
203 protected:
204  // The type of notification we'd like displayed.
205  NotificationType type_;
206
207  std::string id_;
208  string16 title_;
209  string16 message_;
210
211  // Image data for the associated icon, used by Ash when available.
212  gfx::Image icon_;
213
214  // The display string for the source of the notification.  Could be
215  // the same as origin_url_, or the name of an extension.
216  string16 display_source_;
217
218 private:
219  NotifierId notifier_id_;
220  unsigned serial_number_;
221  RichNotificationData optional_fields_;
222  bool shown_as_popup_;  // True if this has been shown as a popup.
223  bool is_read_;  // True if this has been seen in the message center.
224  bool is_expanded_;  // True if this has been expanded in the message center.
225
226  // A proxy object that allows access back to the JavaScript object that
227  // represents the notification, for firing events.
228  scoped_refptr<NotificationDelegate> delegate_;
229};
230
231}  // namespace message_center
232
233#endif  // UI_MESSAGE_CENTER_NOTIFICATION_H_
234