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/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
8 #include "base/strings/utf_string_conversions.h" | |
8 #include "chrome/browser/notifications/desktop_notification_service.h" | 9 #include "chrome/browser/notifications/desktop_notification_service.h" |
10 #include "grit/browser_resources.h" | |
11 #include "net/base/escape.h" | |
12 #include "ui/base/resource/resource_bundle.h" | |
9 #include "ui/base/webui/web_ui_util.h" | 13 #include "ui/base/webui/web_ui_util.h" |
10 | 14 |
15 namespace { | |
16 | |
17 // Creates a data:xxxx URL which contains the full HTML for a notification | |
18 // using supplied icon, title, and text, run through a template which contains | |
19 // the standard formatting for notifications. | |
20 base::string16 CreateDataUrlForNotificationData(const GURL& icon_url, | |
21 const base::string16& title, | |
22 const base::string16& body, | |
23 blink::WebTextDirection dir) { | |
24 int resource; | |
25 std::vector<std::string> subst; | |
26 if (icon_url.is_valid()) { | |
27 resource = IDR_NOTIFICATION_ICON_HTML; | |
28 subst.push_back(icon_url.spec()); | |
29 subst.push_back(net::EscapeForHTML(base::UTF16ToUTF8(title))); | |
30 subst.push_back(net::EscapeForHTML(base::UTF16ToUTF8(body))); | |
31 // icon float position | |
32 subst.push_back(dir == blink::WebTextDirectionRightToLeft ? | |
33 "right" : "left"); | |
34 } else if (title.empty() || body.empty()) { | |
35 resource = IDR_NOTIFICATION_1LINE_HTML; | |
36 base::string16 line = title.empty() ? body : title; | |
37 // Strings are div names in the template file. | |
38 base::string16 line_name = | |
39 title.empty() ? base::ASCIIToUTF16("description") | |
40 : base::ASCIIToUTF16("title"); | |
41 subst.push_back(net::EscapeForHTML(base::UTF16ToUTF8(line_name))); | |
42 subst.push_back(net::EscapeForHTML(base::UTF16ToUTF8(line))); | |
43 } else { | |
44 resource = IDR_NOTIFICATION_2LINE_HTML; | |
45 subst.push_back(net::EscapeForHTML(base::UTF16ToUTF8(title))); | |
46 subst.push_back(net::EscapeForHTML(base::UTF16ToUTF8(body))); | |
47 } | |
48 // body text direction | |
49 subst.push_back(dir == blink::WebTextDirectionRightToLeft ? | |
50 "rtl" : "ltr"); | |
51 | |
52 const base::StringPiece template_html( | |
53 ResourceBundle::GetSharedInstance().GetRawDataResource( | |
54 resource)); | |
55 | |
56 if (template_html.empty()) { | |
57 NOTREACHED() << "unable to load template. ID: " << resource; | |
58 return base::string16(); | |
59 } | |
60 | |
61 std::string data = ReplaceStringPlaceholders(template_html, subst, NULL); | |
62 return base::UTF8ToUTF16("data:text/html;charset=utf-8," + | |
63 net::EscapeQueryParamValue(data, false)); | |
64 } | |
65 | |
66 } // namespace | |
67 | |
11 Notification::Notification(const GURL& origin_url, | 68 Notification::Notification(const GURL& origin_url, |
12 const GURL& icon_url, | 69 const GURL& icon_url, |
13 const base::string16& title, | 70 const base::string16& title, |
14 const base::string16& body, | 71 const base::string16& body, |
15 blink::WebTextDirection dir, | 72 blink::WebTextDirection dir, |
16 const base::string16& display_source, | 73 const base::string16& display_source, |
17 const base::string16& replace_id, | 74 const base::string16& replace_id, |
18 NotificationDelegate* delegate) | 75 NotificationDelegate* delegate) |
19 : message_center::Notification(message_center::NOTIFICATION_TYPE_SIMPLE, | 76 : message_center::Notification(message_center::NOTIFICATION_TYPE_SIMPLE, |
20 delegate->id(), | 77 delegate->id(), |
21 title, | 78 title, |
22 body, | 79 body, |
23 gfx::Image(), | 80 gfx::Image(), |
24 display_source, | 81 display_source, |
25 message_center::NotifierId(origin_url), | 82 message_center::NotifierId(origin_url), |
26 message_center::RichNotificationData(), | 83 message_center::RichNotificationData(), |
27 delegate), | 84 delegate), |
28 origin_url_(origin_url), | 85 origin_url_(origin_url), |
29 icon_url_(icon_url), | 86 icon_url_(icon_url), |
30 replace_id_(replace_id), | 87 replace_id_(replace_id), |
31 delegate_(delegate) { | 88 delegate_(delegate) { |
32 // "Upconvert" the string parameters to a data: URL. | 89 // "Upconvert" the string parameters to a data: URL. |
33 content_url_ = GURL(DesktopNotificationService::CreateDataUrl( | 90 content_url_ = GURL(CreateDataUrlForNotificationData( |
Jun Mukai
2014/09/10 18:18:59
actually, content_url_ itself is a deprecated thin
Peter Beverloo
2014/09/11 16:02:26
Yay! Done.
| |
34 icon_url, title, body, dir)); | 91 icon_url, title, body, dir)); |
35 } | 92 } |
36 | 93 |
37 Notification::Notification( | 94 Notification::Notification( |
38 message_center::NotificationType type, | 95 message_center::NotificationType type, |
39 const GURL& origin_url, | 96 const GURL& origin_url, |
40 const base::string16& title, | 97 const base::string16& title, |
41 const base::string16& body, | 98 const base::string16& body, |
42 const gfx::Image& icon, | 99 const gfx::Image& icon, |
43 blink::WebTextDirection dir, | 100 blink::WebTextDirection dir, |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
101 origin_url_ = notification.origin_url(); | 158 origin_url_ = notification.origin_url(); |
102 icon_url_ = notification.icon_url(); | 159 icon_url_ = notification.icon_url(); |
103 content_url_ = notification.content_url(); | 160 content_url_ = notification.content_url(); |
104 button_one_icon_url_ = notification.button_one_icon_url(); | 161 button_one_icon_url_ = notification.button_one_icon_url(); |
105 button_two_icon_url_ = notification.button_two_icon_url(); | 162 button_two_icon_url_ = notification.button_two_icon_url(); |
106 image_url_ = notification.image_url(); | 163 image_url_ = notification.image_url(); |
107 replace_id_ = notification.replace_id(); | 164 replace_id_ = notification.replace_id(); |
108 delegate_ = notification.delegate(); | 165 delegate_ = notification.delegate(); |
109 return *this; | 166 return *this; |
110 } | 167 } |
OLD | NEW |