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