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 #include "ui/message_center/notification.h"
      6 
      7 #include "base/logging.h"
      8 #include "ui/message_center/notification_delegate.h"
      9 #include "ui/message_center/notification_types.h"
     10 
     11 namespace {
     12 unsigned g_next_serial_number_ = 0;
     13 }
     14 
     15 namespace message_center {
     16 
     17 NotificationItem::NotificationItem(const string16& title,
     18                                    const string16& message)
     19  : title(title),
     20    message(message) {
     21 }
     22 
     23 ButtonInfo::ButtonInfo(const string16& title)
     24  : title(title) {
     25 }
     26 
     27 RichNotificationData::RichNotificationData()
     28     : priority(DEFAULT_PRIORITY),
     29       never_timeout(false),
     30       timestamp(base::Time::Now()),
     31       progress(0),
     32       should_make_spoken_feedback_for_popup_updates(true),
     33       clickable(true) {}
     34 
     35 RichNotificationData::RichNotificationData(const RichNotificationData& other)
     36     : priority(other.priority),
     37       never_timeout(other.never_timeout),
     38       timestamp(other.timestamp),
     39       expanded_message(other.expanded_message),
     40       context_message(other.context_message),
     41       image(other.image),
     42       items(other.items),
     43       progress(other.progress),
     44       buttons(other.buttons),
     45       should_make_spoken_feedback_for_popup_updates(
     46           other.should_make_spoken_feedback_for_popup_updates),
     47       clickable(other.clickable) {}
     48 
     49 RichNotificationData::~RichNotificationData() {}
     50 
     51 Notification::Notification(NotificationType type,
     52                            const std::string& id,
     53                            const string16& title,
     54                            const string16& message,
     55                            const gfx::Image& icon,
     56                            const string16& display_source,
     57                            const NotifierId& notifier_id,
     58                            const RichNotificationData& optional_fields,
     59                            NotificationDelegate* delegate)
     60     : type_(type),
     61       id_(id),
     62       title_(title),
     63       message_(message),
     64       icon_(icon),
     65       display_source_(display_source),
     66       notifier_id_(notifier_id),
     67       serial_number_(g_next_serial_number_++),
     68       optional_fields_(optional_fields),
     69       shown_as_popup_(false),
     70       is_read_(false),
     71       is_expanded_(false),
     72       delegate_(delegate) {}
     73 
     74 Notification::Notification(const Notification& other)
     75     : type_(other.type_),
     76       id_(other.id_),
     77       title_(other.title_),
     78       message_(other.message_),
     79       icon_(other.icon_),
     80       display_source_(other.display_source_),
     81       notifier_id_(other.notifier_id_),
     82       serial_number_(other.serial_number_),
     83       optional_fields_(other.optional_fields_),
     84       shown_as_popup_(other.shown_as_popup_),
     85       is_read_(other.is_read_),
     86       is_expanded_(other.is_expanded_),
     87       delegate_(other.delegate_) {}
     88 
     89 Notification& Notification::operator=(const Notification& other) {
     90   type_ = other.type_;
     91   id_ = other.id_;
     92   title_ = other.title_;
     93   message_ = other.message_;
     94   icon_ = other.icon_;
     95   display_source_ = other.display_source_;
     96   notifier_id_ = other.notifier_id_;
     97   serial_number_ = other.serial_number_;
     98   optional_fields_ = other.optional_fields_;
     99   shown_as_popup_ = other.shown_as_popup_;
    100   is_read_ = other.is_read_;
    101   is_expanded_ = other.is_expanded_;
    102   delegate_ = other.delegate_;
    103 
    104   return *this;
    105 }
    106 
    107 Notification::~Notification() {}
    108 
    109 bool Notification::IsRead() const {
    110   return is_read_ || optional_fields_.priority == MIN_PRIORITY;
    111 }
    112 
    113 void Notification::CopyState(Notification* base) {
    114   shown_as_popup_ = base->shown_as_popup();
    115   is_read_ = base->is_read_;
    116   is_expanded_ = base->is_expanded();
    117   if (!delegate_.get())
    118     delegate_ = base->delegate();
    119   optional_fields_.never_timeout = base->never_timeout();
    120 }
    121 
    122 void Notification::SetButtonIcon(size_t index, const gfx::Image& icon) {
    123   if (index >= optional_fields_.buttons.size())
    124     return;
    125   optional_fields_.buttons[index].icon = icon;
    126 }
    127 
    128 void Notification::SetSystemPriority() {
    129   optional_fields_.priority = SYSTEM_PRIORITY;
    130   optional_fields_.never_timeout = true;
    131 }
    132 
    133 // static
    134 scoped_ptr<Notification> Notification::CreateSystemNotification(
    135     const std::string& notification_id,
    136     const base::string16& title,
    137     const base::string16& message,
    138     const gfx::Image& icon,
    139     const std::string& system_component_id,
    140     const base::Closure& click_callback) {
    141   scoped_ptr<Notification> notification(
    142       new Notification(
    143           NOTIFICATION_TYPE_SIMPLE,
    144           notification_id,
    145           title,
    146           message,
    147           icon,
    148           base::string16()  /* display_source */,
    149           NotifierId(NotifierId::SYSTEM_COMPONENT, system_component_id),
    150           RichNotificationData(),
    151           new HandleNotificationClickedDelegate(click_callback)));
    152   notification->SetSystemPriority();
    153   return notification.Pass();
    154 }
    155 
    156 }  // namespace message_center
    157