notification.h revision 868fa2fe829687343ffae624259930155e16dbd8
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/string16.h"
12#include "base/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
19namespace message_center {
20
21struct MESSAGE_CENTER_EXPORT NotificationItem {
22  string16 title;
23  string16 message;
24
25  NotificationItem(const string16& title, const string16& message);
26};
27
28struct MESSAGE_CENTER_EXPORT ButtonInfo {
29  string16 title;
30  gfx::Image icon;
31
32  ButtonInfo(const string16& title);
33};
34
35class MESSAGE_CENTER_EXPORT RichNotificationData {
36 public:
37  RichNotificationData();
38  RichNotificationData(const RichNotificationData& other);
39  ~RichNotificationData();
40
41  int priority;
42  bool never_timeout;
43  base::Time timestamp;
44  string16 expanded_message;
45  gfx::Image image;
46  std::vector<NotificationItem> items;
47  std::vector<ButtonInfo> buttons;
48};
49
50class MESSAGE_CENTER_EXPORT Notification {
51 public:
52  Notification(NotificationType type,
53               const std::string& id,
54               const string16& title,
55               const string16& message,
56               const gfx::Image& icon,
57               const string16& display_source,
58               const std::string& extension_id,
59               const DictionaryValue* optional_fields,  // May be NULL.
60               NotificationDelegate* delegate);         // May be NULL.
61
62  Notification(NotificationType type,
63               const std::string& id,
64               const string16& title,
65               const string16& message,
66               const gfx::Image& icon,
67               const string16& display_source,
68               const std::string& extension_id,
69               const RichNotificationData& optional_fields,
70               NotificationDelegate* delegate);
71
72  Notification(const Notification& other);
73  Notification& operator=(const Notification& other);
74  virtual ~Notification();
75
76  // Copies the internal on-memory state from |base|, i.e. shown_as_popup,
77  // is_read, is_expanded, and never_timeout.
78  void CopyState(Notification* base);
79
80  NotificationType type() const { return type_; }
81  const std::string& id() const { return id_; }
82  const string16& title() const { return title_; }
83  const string16& message() const { return message_; }
84
85  // A display string for the source of the notification.
86  const string16& display_source() const { return display_source_; }
87  const std::string& extension_id() const { return extension_id_; }
88  void set_extension_id(const std::string& extension_id) {
89    extension_id_ = extension_id;
90  }
91
92  // Begin unpacked values from optional_fields.
93  int priority() const { return optional_fields_.priority; }
94  base::Time timestamp() const { return optional_fields_.timestamp; }
95  const string16& expanded_message() const {
96    return optional_fields_.expanded_message;
97  }
98  const std::vector<NotificationItem>& items() const {
99    return optional_fields_.items;
100  }
101  // End unpacked values.
102
103  // Images fetched asynchronously.
104  const gfx::Image& icon() const { return icon_; }
105  void set_icon(const gfx::Image& icon) { icon_ = icon; }
106
107  const gfx::Image& image() const { return optional_fields_.image; }
108  void set_image(const gfx::Image& image) { optional_fields_.image = image; }
109
110  // Buttons, with icons fetched asynchronously.
111  const std::vector<ButtonInfo>& buttons() const {
112    return optional_fields_.buttons;
113  }
114  void SetButtonIcon(size_t index, const gfx::Image& icon);
115
116  bool shown_as_popup() const { return shown_as_popup_; }
117  void set_shown_as_popup(bool shown_as_popup) {
118    shown_as_popup_ = shown_as_popup;
119  }
120
121  // Read status in the message center.
122  bool is_read() const { return is_read_; }
123  void set_is_read(bool read) { is_read_ = read; }
124
125  // Expanded status in the message center (not the popups).
126  bool is_expanded() const { return is_expanded_; }
127  void set_is_expanded(bool expanded) { is_expanded_ = expanded; }
128
129  // Used to keep the order of notifications with the same timestamp.
130  // The notification with lesser serial_number is considered 'older'.
131  unsigned serial_number() { return serial_number_; }
132
133  // Marks this explicitly to prevent the timeout dismiss of notification.
134  // This is used by webkit notifications to keep the existing behavior.
135  void set_never_timeout(bool never_timeout) {
136    optional_fields_.never_timeout = never_timeout;
137  }
138
139  bool never_timeout() const { return optional_fields_.never_timeout; }
140  NotificationDelegate* delegate() const { return delegate_.get(); }
141  const RichNotificationData& rich_notification_data() const {
142    return optional_fields_;
143  }
144
145  // Delegate actions.
146  void Display() const { delegate()->Display(); }
147  void Error() const { delegate()->Error(); }
148  bool HasClickedListener() const { return delegate()->HasClickedListener(); }
149  void Click() const { delegate()->Click(); }
150  void ButtonClick(int index) const { delegate()->ButtonClick(index); }
151  void Close(bool by_user) const { delegate()->Close(by_user); }
152
153 protected:
154  // The type of notification we'd like displayed.
155  NotificationType type_;
156
157  std::string id_;
158  string16 title_;
159  string16 message_;
160
161  // Image data for the associated icon, used by Ash when available.
162  gfx::Image icon_;
163
164  // The display string for the source of the notification.  Could be
165  // the same as origin_url_, or the name of an extension.
166  string16 display_source_;
167
168 private:
169  // Unpacks the provided |optional_fields| and applies the values to override
170  // the notification's data members.
171  void ApplyOptionalFields(const DictionaryValue* optional_fields);
172
173  std::string extension_id_;
174  unsigned serial_number_;
175  RichNotificationData optional_fields_;
176  bool shown_as_popup_;  // True if this has been shown as a popup.
177  bool is_read_;  // True if this has been seen in the message center.
178  bool is_expanded_;  // True if this has been expanded in the message center.
179
180  // A proxy object that allows access back to the JavaScript object that
181  // represents the notification, for firing events.
182  scoped_refptr<NotificationDelegate> delegate_;
183};
184
185}  // namespace message_center
186
187#endif  // UI_MESSAGE_CENTER_NOTIFICATION_H_
188