| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/message_center/notification.h" | 5 #include "ui/message_center/notification.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "ui/message_center/notification_types.h" | 8 #include "ui/message_center/notification_types.h" |
| 9 | 9 |
| 10 namespace { | 10 namespace { |
| 11 unsigned g_next_serial_number_ = 0; | 11 unsigned g_next_serial_number_ = 0; |
| 12 } | 12 } |
| 13 | 13 |
| 14 namespace message_center { | 14 namespace message_center { |
| 15 | 15 |
| 16 NotificationItem::NotificationItem(const string16& title, | 16 NotificationItem::NotificationItem(const string16& title, |
| 17 const string16& message) | 17 const string16& message) |
| 18 : title(title), | 18 : title(title), |
| 19 message(message) { | 19 message(message) { |
| 20 } | 20 } |
| 21 | 21 |
| 22 ButtonInfo::ButtonInfo(const string16& title) | 22 ButtonInfo::ButtonInfo(const string16& title) |
| 23 : title(title) { | 23 : title(title) { |
| 24 } | 24 } |
| 25 | 25 |
| 26 RichNotificationData::RichNotificationData() |
| 27 : priority(DEFAULT_PRIORITY), |
| 28 never_timeout(false), |
| 29 timestamp(base::Time::Now()) {} |
| 30 |
| 31 RichNotificationData::RichNotificationData(const RichNotificationData& other) |
| 32 : priority(other.priority), |
| 33 never_timeout(other.never_timeout), |
| 34 timestamp(other.timestamp), |
| 35 expanded_message(other.expanded_message), |
| 36 icon(other.icon), |
| 37 image(other.image), |
| 38 items(other.items), |
| 39 buttons(other.buttons) {} |
| 40 |
| 41 RichNotificationData::~RichNotificationData() {} |
| 42 |
| 26 Notification::Notification(NotificationType type, | 43 Notification::Notification(NotificationType type, |
| 27 const std::string& id, | 44 const std::string& id, |
| 28 const string16& title, | 45 const string16& title, |
| 29 const string16& message, | 46 const string16& message, |
| 47 const gfx::Image& icon, |
| 30 const string16& display_source, | 48 const string16& display_source, |
| 31 const std::string& extension_id, | 49 const std::string& extension_id, |
| 32 const DictionaryValue* optional_fields, | 50 const DictionaryValue* optional_fields, |
| 33 NotificationDelegate* delegate) | 51 NotificationDelegate* delegate) |
| 34 : type_(type), | 52 : type_(type), |
| 35 id_(id), | 53 id_(id), |
| 36 title_(title), | 54 title_(title), |
| 37 message_(message), | 55 message_(message), |
| 38 display_source_(display_source), | 56 display_source_(display_source), |
| 39 extension_id_(extension_id), | 57 extension_id_(extension_id), |
| 40 priority_(DEFAULT_PRIORITY), | |
| 41 timestamp_(base::Time::Now()), | |
| 42 serial_number_(g_next_serial_number_++), | 58 serial_number_(g_next_serial_number_++), |
| 43 shown_as_popup_(false), | 59 shown_as_popup_(false), |
| 44 is_read_(false), | 60 is_read_(false), |
| 45 is_expanded_(false), | 61 is_expanded_(false), |
| 46 never_timeout_(false), | |
| 47 delegate_(delegate) { | 62 delegate_(delegate) { |
| 48 // This can override some data members initialized to deafule values above. | 63 // This can override some data members initialized to default values above. |
| 64 optional_fields_.icon = icon; |
| 49 ApplyOptionalFields(optional_fields); | 65 ApplyOptionalFields(optional_fields); |
| 50 } | 66 } |
| 51 | 67 |
| 52 Notification::~Notification() { | 68 Notification::Notification(NotificationType type, |
| 53 } | 69 const std::string& id, |
| 70 const string16& title, |
| 71 const string16& message, |
| 72 const string16& display_source, |
| 73 const std::string& extension_id, |
| 74 const RichNotificationData& optional_fields, |
| 75 NotificationDelegate* delegate) |
| 76 : type_(type), |
| 77 id_(id), |
| 78 title_(title), |
| 79 message_(message), |
| 80 display_source_(display_source), |
| 81 extension_id_(extension_id), |
| 82 optional_fields_(optional_fields), |
| 83 delegate_(delegate) {} |
| 84 |
| 85 Notification::~Notification() {} |
| 54 | 86 |
| 55 void Notification::CopyState(Notification* base) { | 87 void Notification::CopyState(Notification* base) { |
| 56 shown_as_popup_ = base->shown_as_popup(); | 88 shown_as_popup_ = base->shown_as_popup(); |
| 57 is_read_ = base->is_read(); | 89 is_read_ = base->is_read(); |
| 58 is_expanded_ = base->is_expanded(); | 90 is_expanded_ = base->is_expanded(); |
| 59 never_timeout_ = base->never_timeout(); | |
| 60 if (!delegate_.get()) | 91 if (!delegate_.get()) |
| 61 delegate_ = base->delegate(); | 92 delegate_ = base->delegate(); |
| 93 optional_fields_.never_timeout = base->never_timeout(); |
| 62 } | 94 } |
| 63 | 95 |
| 64 bool Notification::SetButtonIcon(size_t index, const gfx::Image& icon) { | 96 void Notification::SetButtonIcon(size_t index, const gfx::Image& icon) { |
| 65 if (index >= buttons_.size()) | 97 if (index >= optional_fields_.buttons.size()) |
| 66 return false; | 98 return; |
| 67 buttons_[index].icon = icon; | 99 optional_fields_.buttons[index].icon = icon; |
| 68 return true; | |
| 69 } | 100 } |
| 70 | 101 |
| 71 void Notification::ApplyOptionalFields(const DictionaryValue* fields) { | 102 void Notification::ApplyOptionalFields(const DictionaryValue* fields) { |
| 72 if (!fields) | 103 if (!fields) |
| 73 return; | 104 return; |
| 74 | 105 |
| 75 fields->GetInteger(kPriorityKey, &priority_); | 106 fields->GetInteger(kPriorityKey, &optional_fields_.priority); |
| 76 if (fields->HasKey(kTimestampKey)) { | 107 if (fields->HasKey(kTimestampKey)) { |
| 77 std::string time_string; | 108 std::string time_string; |
| 78 fields->GetString(kTimestampKey, &time_string); | 109 fields->GetString(kTimestampKey, &time_string); |
| 79 base::Time::FromString(time_string.c_str(), ×tamp_); | 110 base::Time::FromString(time_string.c_str(), &optional_fields_.timestamp); |
| 80 } | 111 } |
| 81 if (fields->HasKey(kButtonOneTitleKey) || | 112 if (fields->HasKey(kButtonOneTitleKey) || |
| 82 fields->HasKey(kButtonOneIconUrlKey)) { | 113 fields->HasKey(kButtonOneIconUrlKey)) { |
| 83 string16 title; | 114 string16 title; |
| 84 string16 icon; | 115 string16 icon; |
| 85 if (fields->GetString(kButtonOneTitleKey, &title) || | 116 if (fields->GetString(kButtonOneTitleKey, &title) || |
| 86 fields->GetString(kButtonOneIconUrlKey, &icon)) { | 117 fields->GetString(kButtonOneIconUrlKey, &icon)) { |
| 87 buttons_.push_back(ButtonInfo(title)); | 118 optional_fields_.buttons.push_back(ButtonInfo(title)); |
| 88 if (fields->GetString(kButtonTwoTitleKey, &title) || | 119 if (fields->GetString(kButtonTwoTitleKey, &title) || |
| 89 fields->GetString(kButtonTwoIconUrlKey, &icon)) { | 120 fields->GetString(kButtonTwoIconUrlKey, &icon)) { |
| 90 buttons_.push_back(ButtonInfo(title)); | 121 optional_fields_.buttons.push_back(ButtonInfo(title)); |
| 91 } | 122 } |
| 92 } | 123 } |
| 93 } | 124 } |
| 94 fields->GetString(kExpandedMessageKey, &expanded_message_); | 125 fields->GetString(kExpandedMessageKey, &optional_fields_.expanded_message); |
| 95 if (fields->HasKey(kItemsKey)) { | 126 if (fields->HasKey(kItemsKey)) { |
| 96 const ListValue* items; | 127 const ListValue* items; |
| 97 CHECK(fields->GetList(kItemsKey, &items)); | 128 CHECK(fields->GetList(kItemsKey, &items)); |
| 98 for (size_t i = 0; i < items->GetSize(); ++i) { | 129 for (size_t i = 0; i < items->GetSize(); ++i) { |
| 99 string16 title; | 130 string16 title; |
| 100 string16 message; | 131 string16 message; |
| 101 const base::DictionaryValue* item; | 132 const base::DictionaryValue* item; |
| 102 items->GetDictionary(i, &item); | 133 items->GetDictionary(i, &item); |
| 103 item->GetString(kItemTitleKey, &title); | 134 item->GetString(kItemTitleKey, &title); |
| 104 item->GetString(kItemMessageKey, &message); | 135 item->GetString(kItemMessageKey, &message); |
| 105 items_.push_back(NotificationItem(title, message)); | 136 optional_fields_.items.push_back(NotificationItem(title, message)); |
| 106 } | 137 } |
| 107 } | 138 } |
| 108 | 139 |
| 109 fields->GetBoolean(kPrivateNeverTimeoutKey, &never_timeout_); | 140 fields->GetBoolean(kPrivateNeverTimeoutKey, &optional_fields_.never_timeout); |
| 110 } | 141 } |
| 111 | 142 |
| 112 } // namespace message_center | 143 } // namespace message_center |
| OLD | NEW |