OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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 "chrome/browser/notifications/desktop_notification_service.h" | |
6 | |
7 #include "app/l10n_util.h" | |
8 #include "app/resource_bundle.h" | |
9 #include "base/string_piece.h" | |
10 #include "base/string_util.h" | |
11 #include "base/values.h" | |
12 #include "chrome/browser/browser_list.h" | |
13 #include "chrome/browser/chrome_thread.h" | |
14 #include "chrome/browser/notifications/notification_object_proxy.h" | |
15 #include "chrome/browser/notifications/notification_ui_manager.h" | |
16 #include "chrome/browser/renderer_host/render_process_host.h" | |
17 #include "chrome/browser/renderer_host/site_instance.h" | |
18 #include "chrome/common/child_process_host.h" | |
19 #include "grit/browser_resources.h" | |
20 #include "grit/generated_resources.h" | |
21 | |
22 // Creates a data:xxxx URL which contains the full HTML for a notification | |
23 // using supplied icon, title, and text, run through a template which contains | |
24 // the standard formatting for notifications. | |
25 static string16 CreateDataUrl(const GURL& icon_url, const string16& title, | |
26 const string16& body) { | |
27 const base::StringPiece template_html( | |
28 ResourceBundle::GetSharedInstance().GetRawDataResource( | |
29 IDR_NOTIFICATION_HTML)); | |
30 | |
31 if (template_html.empty()) { | |
32 NOTREACHED() << "unable to load template. ID: " << IDR_NOTIFICATION_HTML; | |
33 return EmptyString16(); | |
34 } | |
35 | |
36 std::vector<string16> subst; | |
37 if (icon_url.is_valid()) | |
38 subst.push_back(UTF8ToUTF16(icon_url.spec())); | |
39 else | |
40 subst.push_back(EmptyString16()); | |
41 | |
42 subst.push_back(title); | |
43 subst.push_back(body); | |
44 | |
45 if (icon_url.is_valid()) { | |
46 subst.push_back(ASCIIToUTF16("block")); | |
47 subst.push_back(ASCIIToUTF16("60")); | |
48 } else { | |
49 subst.push_back(ASCIIToUTF16("none")); | |
50 subst.push_back(ASCIIToUTF16("5")); | |
51 } | |
52 | |
53 string16 format_string = ASCIIToUTF16("data:text/html;charset=utf-8," | |
54 + template_html.as_string()); | |
55 return ReplaceStringPlaceholders(format_string, subst, NULL); | |
56 } | |
57 | |
58 // This will call the notification manager on the UI thread to actually | |
59 // put the notification with the requested parameters on the desktop. | |
60 void DesktopNotificationService::ShowNotification( | |
61 const Notification& notification) { | |
62 ui_manager_->Add(notification, profile_); | |
63 } | |
64 | |
65 // Shows a notification bubble which contains the contents of url. | |
66 bool DesktopNotificationService::ShowDesktopNotification( | |
67 const GURL& origin, const GURL& url, int process_id, int route_id, | |
68 NotificationSource source, int notification_id) { | |
69 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | |
70 NotificationObjectProxy* proxy = | |
71 new NotificationObjectProxy(process_id, route_id, | |
72 notification_id, | |
73 source == WorkerNotification); | |
74 Notification notif(origin, url, proxy); | |
75 ShowNotification(notif); | |
76 return true; | |
77 } | |
78 | |
79 // Shows a notification constructed from an icon, title, and text. | |
80 bool DesktopNotificationService::ShowDesktopNotificationText( | |
81 const GURL& origin, const GURL& icon, const string16& title, | |
82 const string16& text, int process_id, int route_id, | |
83 NotificationSource source, int notification_id) { | |
84 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | |
85 NotificationObjectProxy* proxy = | |
86 new NotificationObjectProxy(process_id, route_id, | |
87 notification_id, | |
88 source == WorkerNotification); | |
89 // "upconvert" the string parameters to a data: URL. | |
90 string16 data_url = CreateDataUrl(icon, title, text); | |
91 Notification notif(origin, GURL(data_url), proxy); | |
92 ShowNotification(notif); | |
93 return true; | |
94 } | |
OLD | NEW |