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