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" |
| 8 #include "ui/message_center/notification_types.h" |
| 9 |
| 10 namespace { |
| 11 unsigned g_next_serial_number_ = 0; |
| 12 } |
| 13 |
7 namespace message_center { | 14 namespace message_center { |
8 | 15 |
9 NotificationItem::NotificationItem(string16 title, string16 message) | 16 NotificationItem::NotificationItem(const string16& title, |
| 17 const string16& message) |
10 : title(title), | 18 : title(title), |
11 message(message) { | 19 message(message) { |
12 } | 20 } |
13 | 21 |
14 Notification::Notification() | 22 ButtonInfo::ButtonInfo(const string16& title) |
15 : is_read(false), | 23 : title(title) { |
16 shown_as_popup(false) { | 24 } |
| 25 |
| 26 Notification::Notification(NotificationType type, |
| 27 const std::string& id, |
| 28 const string16& title, |
| 29 const string16& message, |
| 30 const string16& display_source, |
| 31 const std::string& extension_id, |
| 32 const DictionaryValue* optional_fields) |
| 33 : type_(type), |
| 34 id_(id), |
| 35 title_(title), |
| 36 message_(message), |
| 37 display_source_(display_source), |
| 38 extension_id_(extension_id), |
| 39 priority_(DEFAULT_PRIORITY), |
| 40 timestamp_(base::Time::Now()), |
| 41 serial_number_(g_next_serial_number_++), |
| 42 is_read_(false), |
| 43 shown_as_popup_(false) { |
| 44 // This can override some data members initialized to deafule values above. |
| 45 ApplyOptionalFields(optional_fields); |
17 } | 46 } |
18 | 47 |
19 Notification::~Notification() { | 48 Notification::~Notification() { |
20 } | 49 } |
21 | 50 |
| 51 bool Notification::SetButtonIcon(size_t index, const gfx::ImageSkia& icon) { |
| 52 if (index >= buttons_.size()) |
| 53 return false; |
| 54 buttons_[index].icon = icon; |
| 55 return true; |
| 56 } |
| 57 |
| 58 void Notification::ApplyOptionalFields(const DictionaryValue* fields) { |
| 59 if (!fields) |
| 60 return; |
| 61 |
| 62 fields->GetInteger(kPriorityKey, &priority_); |
| 63 if (fields->HasKey(kTimestampKey)) { |
| 64 std::string time_string; |
| 65 fields->GetString(kTimestampKey, &time_string); |
| 66 base::Time::FromString(time_string.c_str(), ×tamp_); |
| 67 } |
| 68 if (fields->HasKey(kButtonOneTitleKey) || |
| 69 fields->HasKey(kButtonOneIconUrlKey)) { |
| 70 string16 title; |
| 71 string16 icon; |
| 72 if (fields->GetString(kButtonOneTitleKey, &title) || |
| 73 fields->GetString(kButtonOneIconUrlKey, &icon)) { |
| 74 buttons_.push_back(ButtonInfo(title)); |
| 75 if (fields->GetString(kButtonTwoTitleKey, &title) || |
| 76 fields->GetString(kButtonTwoIconUrlKey, &icon)) { |
| 77 buttons_.push_back(ButtonInfo(title)); |
| 78 } |
| 79 } |
| 80 } |
| 81 fields->GetString(kExpandedMessageKey, &expanded_message_); |
| 82 if (fields->HasKey(kItemsKey)) { |
| 83 const ListValue* items; |
| 84 CHECK(fields->GetList(kItemsKey, &items)); |
| 85 for (size_t i = 0; i < items->GetSize(); ++i) { |
| 86 string16 title; |
| 87 string16 message; |
| 88 const base::DictionaryValue* item; |
| 89 items->GetDictionary(i, &item); |
| 90 item->GetString(kItemTitleKey, &title); |
| 91 item->GetString(kItemMessageKey, &message); |
| 92 items_.push_back(NotificationItem(title, message)); |
| 93 } |
| 94 } |
| 95 } |
| 96 |
22 } // namespace message_center | 97 } // namespace message_center |
OLD | NEW |