OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/notifications/notification.h" | 5 #include "chrome/browser/notifications/notification.h" |
6 | 6 |
| 7 #include "base/string_util.h" |
7 #include "chrome/browser/notifications/desktop_notification_service.h" | 8 #include "chrome/browser/notifications/desktop_notification_service.h" |
| 9 #include "ui/message_center/message_center_util.h" |
| 10 #include "ui/webui/web_ui_util.h" |
8 | 11 |
9 Notification::Notification(const GURL& origin_url, | 12 Notification::Notification(const GURL& origin_url, |
10 const GURL& content_url, | 13 const GURL& content_url, |
11 const string16& display_source, | 14 const string16& display_source, |
12 const string16& replace_id, | 15 const string16& replace_id, |
13 NotificationDelegate* delegate) | 16 NotificationDelegate* delegate) |
14 : type_(message_center::NOTIFICATION_TYPE_SIMPLE), | 17 : message_center::Notification(message_center::NOTIFICATION_TYPE_SIMPLE, |
| 18 delegate->id(), |
| 19 EmptyString16(), |
| 20 EmptyString16(), |
| 21 gfx::Image(), |
| 22 display_source, |
| 23 origin_url.spec(), |
| 24 NULL, |
| 25 delegate), |
15 origin_url_(origin_url), | 26 origin_url_(origin_url), |
16 is_html_(true), | 27 is_html_(true), |
17 content_url_(content_url), | 28 content_url_(content_url), |
18 display_source_(display_source), | |
19 replace_id_(replace_id), | 29 replace_id_(replace_id), |
20 delegate_(delegate) {} | 30 delegate_(delegate) {} |
21 | 31 |
22 Notification::Notification(const GURL& origin_url, | 32 Notification::Notification(const GURL& origin_url, |
23 const GURL& icon_url, | 33 const GURL& icon_url, |
24 const string16& title, | 34 const string16& title, |
25 const string16& body, | 35 const string16& body, |
26 WebKit::WebTextDirection dir, | 36 WebKit::WebTextDirection dir, |
27 const string16& display_source, | 37 const string16& display_source, |
28 const string16& replace_id, | 38 const string16& replace_id, |
29 NotificationDelegate* delegate) | 39 NotificationDelegate* delegate) |
30 : type_(message_center::NOTIFICATION_TYPE_SIMPLE), | 40 : message_center::Notification(message_center::NOTIFICATION_TYPE_SIMPLE, |
| 41 delegate->id(), |
| 42 title, |
| 43 body, |
| 44 gfx::Image(), |
| 45 display_source, |
| 46 origin_url.spec(), |
| 47 NULL, |
| 48 delegate), |
31 origin_url_(origin_url), | 49 origin_url_(origin_url), |
32 icon_url_(icon_url), | 50 icon_url_(icon_url), |
33 is_html_(false), | 51 is_html_(false), |
34 title_(title), | |
35 body_(body), | |
36 display_source_(display_source), | |
37 replace_id_(replace_id), | 52 replace_id_(replace_id), |
38 delegate_(delegate) { | 53 delegate_(delegate) { |
39 // "Upconvert" the string parameters to a data: URL. | 54 // "Upconvert" the string parameters to a data: URL. |
40 content_url_ = GURL(DesktopNotificationService::CreateDataUrl( | 55 content_url_ = GURL(DesktopNotificationService::CreateDataUrl( |
41 icon_url, title, body, dir)); | 56 icon_url, title, body, dir)); |
42 } | 57 } |
43 | 58 |
44 Notification::Notification(message_center::NotificationType type, | 59 Notification::Notification(message_center::NotificationType type, |
45 const GURL& origin_url, | 60 const GURL& origin_url, |
46 const GURL& icon_url, | 61 const GURL& icon_url, |
47 const string16& title, | 62 const string16& title, |
48 const string16& body, | 63 const string16& body, |
49 WebKit::WebTextDirection dir, | 64 WebKit::WebTextDirection dir, |
50 const string16& display_source, | 65 const string16& display_source, |
51 const string16& replace_id, | 66 const string16& replace_id, |
52 const DictionaryValue* optional_fields, | 67 const DictionaryValue* optional_fields, |
53 NotificationDelegate* delegate) | 68 NotificationDelegate* delegate) |
54 : type_(type), | 69 : message_center::Notification(type, |
| 70 delegate->id(), |
| 71 title, |
| 72 body, |
| 73 gfx::Image(), |
| 74 display_source, |
| 75 origin_url.spec(), |
| 76 optional_fields, |
| 77 delegate), |
55 origin_url_(origin_url), | 78 origin_url_(origin_url), |
56 icon_url_(icon_url), | 79 icon_url_(icon_url), |
57 is_html_(false), | 80 is_html_(false), |
58 title_(title), | |
59 body_(body), | |
60 display_source_(display_source), | |
61 replace_id_(replace_id), | 81 replace_id_(replace_id), |
62 optional_fields_(NULL), | |
63 delegate_(delegate) { | 82 delegate_(delegate) { |
64 if (optional_fields) | 83 if (optional_fields) |
65 optional_fields_.reset(optional_fields->DeepCopy()); | 84 ApplyOptionalFields(optional_fields); |
66 // "Upconvert" the string parameters to a data: URL. Some balloon views | 85 // "Upconvert" the string parameters to a data: URL. Some balloon views |
67 // require content URL to render anything, so this serves as a backup. | 86 // require content URL to render anything, so this serves as a backup. |
68 content_url_ = GURL(DesktopNotificationService::CreateDataUrl( | 87 content_url_ = GURL(DesktopNotificationService::CreateDataUrl( |
69 icon_url, title, body, dir)); | 88 icon_url, title, body, dir)); |
70 } | 89 } |
71 | 90 |
| 91 Notification::Notification( |
| 92 message_center::NotificationType type, |
| 93 const GURL& origin_url, |
| 94 const string16& title, |
| 95 const string16& body, |
| 96 const gfx::Image& icon, |
| 97 WebKit::WebTextDirection dir, |
| 98 const string16& display_source, |
| 99 const string16& replace_id, |
| 100 const message_center::RichNotificationData& rich_notification_data, |
| 101 NotificationDelegate* delegate) |
| 102 : message_center::Notification(type, |
| 103 delegate->id(), |
| 104 title, |
| 105 body, |
| 106 icon, |
| 107 display_source, |
| 108 origin_url.spec(), |
| 109 rich_notification_data, |
| 110 delegate), |
| 111 origin_url_(origin_url), |
| 112 is_html_(false), |
| 113 replace_id_(replace_id), |
| 114 delegate_(delegate) { |
| 115 if (!message_center::IsRichNotificationEnabled()) { |
| 116 // "Upconvert" the string parameters to a data: URL. Some balloon views |
| 117 // require content URL to render anything, so this serves as a backup. |
| 118 GURL icon_url; |
| 119 if (!icon.IsEmpty()) |
| 120 icon_url_ = GURL(webui::GetBitmapDataUrl(*icon.ToSkBitmap())); |
| 121 content_url_ = GURL( |
| 122 DesktopNotificationService::CreateDataUrl(icon_url, title, body, dir)); |
| 123 } |
| 124 } |
| 125 |
72 Notification::Notification(const GURL& origin_url, | 126 Notification::Notification(const GURL& origin_url, |
73 const gfx::Image& icon, | 127 const gfx::Image& icon, |
74 const string16& title, | 128 const string16& title, |
75 const string16& body, | 129 const string16& body, |
76 WebKit::WebTextDirection dir, | 130 WebKit::WebTextDirection dir, |
77 const string16& display_source, | 131 const string16& display_source, |
78 const string16& replace_id, | 132 const string16& replace_id, |
79 NotificationDelegate* delegate) | 133 NotificationDelegate* delegate) |
80 : type_(message_center::NOTIFICATION_TYPE_SIMPLE), | 134 : message_center::Notification(message_center::NOTIFICATION_TYPE_SIMPLE, |
| 135 delegate->id(), |
| 136 title, |
| 137 body, |
| 138 icon, |
| 139 display_source, |
| 140 origin_url.spec(), |
| 141 NULL, |
| 142 delegate), |
81 origin_url_(origin_url), | 143 origin_url_(origin_url), |
82 icon_(icon), | |
83 is_html_(false), | 144 is_html_(false), |
84 title_(title), | |
85 body_(body), | |
86 display_source_(display_source), | |
87 replace_id_(replace_id), | 145 replace_id_(replace_id), |
88 delegate_(delegate) {} | 146 delegate_(delegate) {} |
89 | 147 |
90 Notification::Notification(const Notification& notification) | 148 Notification::Notification(const Notification& notification) |
91 : type_(notification.type()), | 149 : message_center::Notification(notification), |
92 origin_url_(notification.origin_url()), | 150 origin_url_(notification.origin_url()), |
93 icon_(notification.icon()), | |
94 icon_url_(notification.icon_url()), | 151 icon_url_(notification.icon_url()), |
95 is_html_(notification.is_html()), | 152 is_html_(notification.is_html()), |
96 content_url_(notification.content_url()), | 153 content_url_(notification.content_url()), |
97 title_(notification.title()), | 154 button_one_icon_url_(notification.button_one_icon_url()), |
98 body_(notification.body()), | 155 button_two_icon_url_(notification.button_two_icon_url()), |
99 display_source_(notification.display_source()), | 156 image_url_(notification.image_url()), |
100 replace_id_(notification.replace_id()), | 157 replace_id_(notification.replace_id()), |
101 delegate_(notification.delegate()) { | 158 delegate_(notification.delegate()) {} |
102 if (notification.optional_fields()) | |
103 optional_fields_.reset(notification.optional_fields()->DeepCopy()); | |
104 } | |
105 | 159 |
106 Notification::~Notification() {} | 160 Notification::~Notification() {} |
107 | 161 |
108 Notification& Notification::operator=(const Notification& notification) { | 162 Notification& Notification::operator=(const Notification& notification) { |
109 type_ = notification.type(); | 163 message_center::Notification::operator=(notification); |
110 origin_url_ = notification.origin_url(); | 164 origin_url_ = notification.origin_url(); |
111 icon_ = notification.icon_; | |
112 icon_url_ = notification.icon_url(); | 165 icon_url_ = notification.icon_url(); |
113 is_html_ = notification.is_html(); | 166 is_html_ = notification.is_html(); |
114 content_url_ = notification.content_url(); | 167 content_url_ = notification.content_url(); |
115 title_ = notification.title(); | 168 button_one_icon_url_ = notification.button_one_icon_url(); |
116 body_ = notification.body(); | 169 button_two_icon_url_ = notification.button_two_icon_url(); |
117 display_source_ = notification.display_source(); | 170 image_url_ = notification.image_url(); |
118 replace_id_ = notification.replace_id(); | 171 replace_id_ = notification.replace_id(); |
119 if (notification.optional_fields()) | |
120 optional_fields_.reset(notification.optional_fields()->DeepCopy()); | |
121 else | |
122 optional_fields_.reset(); | |
123 delegate_ = notification.delegate(); | 172 delegate_ = notification.delegate(); |
124 return *this; | 173 return *this; |
125 } | 174 } |
126 | 175 |
127 void Notification::DisableTimeout() { | 176 void Notification::ApplyOptionalFields(const DictionaryValue* optional_fields) { |
128 if (!optional_fields_.get()) | 177 if (!optional_fields) |
129 optional_fields_.reset(new base::DictionaryValue()); | 178 return; |
130 optional_fields_->SetBoolean(message_center::kPrivateNeverTimeoutKey, true); | 179 |
| 180 string16 url; |
| 181 if (optional_fields->GetString(message_center::kButtonOneIconUrlKey, &url)) { |
| 182 button_one_icon_url_ = GURL(url); |
| 183 } |
| 184 if (optional_fields->GetString(message_center::kButtonTwoIconUrlKey, &url)) { |
| 185 button_two_icon_url_ = GURL(url); |
| 186 } |
| 187 if (optional_fields->GetString(message_center::kImageUrlKey, &url)) { |
| 188 image_url_ = GURL(url); |
| 189 } |
131 } | 190 } |
OLD | NEW |