OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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/desktop_notification_service.h" | 5 #include "chrome/browser/notifications/desktop_notification_service.h" |
6 | 6 |
7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
| 9 #include "base/string_piece.h" |
| 10 #include "base/string_util.h" |
9 #include "base/thread.h" | 11 #include "base/thread.h" |
10 #include "chrome/browser/browser_list.h" | 12 #include "chrome/browser/browser_list.h" |
11 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
12 #include "chrome/browser/chrome_thread.h" | 14 #include "chrome/browser/chrome_thread.h" |
13 #include "chrome/browser/notifications/notification.h" | 15 #include "chrome/browser/notifications/notification.h" |
14 #include "chrome/browser/notifications/notification_object_proxy.h" | 16 #include "chrome/browser/notifications/notification_object_proxy.h" |
| 17 #include "chrome/browser/notifications/notification_ui_manager.h" |
15 #include "chrome/browser/notifications/notifications_prefs_cache.h" | 18 #include "chrome/browser/notifications/notifications_prefs_cache.h" |
16 #include "chrome/browser/profile.h" | 19 #include "chrome/browser/profile.h" |
17 #include "chrome/browser/renderer_host/render_process_host.h" | 20 #include "chrome/browser/renderer_host/render_process_host.h" |
18 #include "chrome/browser/renderer_host/render_view_host.h" | 21 #include "chrome/browser/renderer_host/render_view_host.h" |
19 #include "chrome/browser/renderer_host/site_instance.h" | 22 #include "chrome/browser/renderer_host/site_instance.h" |
20 #include "chrome/browser/tab_contents/infobar_delegate.h" | 23 #include "chrome/browser/tab_contents/infobar_delegate.h" |
21 #include "chrome/browser/tab_contents/tab_contents.h" | 24 #include "chrome/browser/tab_contents/tab_contents.h" |
22 #include "chrome/browser/worker_host/worker_process_host.h" | 25 #include "chrome/browser/worker_host/worker_process_host.h" |
23 #include "chrome/common/child_process_host.h" | 26 #include "chrome/common/child_process_host.h" |
24 #include "chrome/common/pref_names.h" | 27 #include "chrome/common/pref_names.h" |
25 #include "chrome/common/pref_service.h" | 28 #include "chrome/common/pref_service.h" |
26 #include "chrome/common/render_messages.h" | 29 #include "chrome/common/render_messages.h" |
27 #include "webkit/api/public/WebNotificationPresenter.h" | 30 #include "grit/browser_resources.h" |
28 #include "grit/chromium_strings.h" | 31 #include "grit/chromium_strings.h" |
29 #include "grit/generated_resources.h" | 32 #include "grit/generated_resources.h" |
30 #include "grit/theme_resources.h" | 33 #include "grit/theme_resources.h" |
| 34 #include "webkit/api/public/WebNotificationPresenter.h" |
31 | 35 |
32 using WebKit::WebNotificationPresenter; | 36 using WebKit::WebNotificationPresenter; |
33 | 37 |
| 38 // Creates a data:xxxx URL which contains the full HTML for a notification |
| 39 // using supplied icon, title, and text, run through a template which contains |
| 40 // the standard formatting for notifications. |
| 41 static string16 CreateDataUrl(const GURL& icon_url, const string16& title, |
| 42 const string16& body) { |
| 43 const base::StringPiece template_html( |
| 44 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 45 IDR_NOTIFICATION_HTML)); |
| 46 |
| 47 if (template_html.empty()) { |
| 48 NOTREACHED() << "unable to load template. ID: " << IDR_NOTIFICATION_HTML; |
| 49 return EmptyString16(); |
| 50 } |
| 51 |
| 52 std::vector<string16> subst; |
| 53 if (icon_url.is_valid()) |
| 54 subst.push_back(UTF8ToUTF16(icon_url.spec())); |
| 55 else |
| 56 subst.push_back(EmptyString16()); |
| 57 |
| 58 subst.push_back(title); |
| 59 subst.push_back(body); |
| 60 |
| 61 if (icon_url.is_valid()) { |
| 62 subst.push_back(ASCIIToUTF16("block")); |
| 63 subst.push_back(ASCIIToUTF16("60")); |
| 64 } else { |
| 65 subst.push_back(ASCIIToUTF16("none")); |
| 66 subst.push_back(ASCIIToUTF16("5")); |
| 67 } |
| 68 |
| 69 string16 format_string = ASCIIToUTF16("data:text/html;charset=utf-8," |
| 70 + template_html.as_string()); |
| 71 return ReplaceStringPlaceholders(format_string, subst, NULL); |
| 72 } |
| 73 |
34 // A task object which calls the renderer to inform the web page that the | 74 // A task object which calls the renderer to inform the web page that the |
35 // permission request has completed. | 75 // permission request has completed. |
36 class NotificationPermissionCallbackTask : public Task { | 76 class NotificationPermissionCallbackTask : public Task { |
37 public: | 77 public: |
38 NotificationPermissionCallbackTask(int process_id, int route_id, | 78 NotificationPermissionCallbackTask(int process_id, int route_id, |
39 int request_id) | 79 int request_id) |
40 : process_id_(process_id), | 80 : process_id_(process_id), |
41 route_id_(route_id), | 81 route_id_(route_id), |
42 request_id_(request_id) { | 82 request_id_(request_id) { |
43 } | 83 } |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 if (!browser) { | 261 if (!browser) { |
222 // Reached during ui tests. | 262 // Reached during ui tests. |
223 return; | 263 return; |
224 } | 264 } |
225 TabContents* tab = browser->GetSelectedTabContents(); | 265 TabContents* tab = browser->GetSelectedTabContents(); |
226 if (!tab) | 266 if (!tab) |
227 return; | 267 return; |
228 tab->AddInfoBar(new NotificationPermissionInfoBarDelegate(tab, origin, | 268 tab->AddInfoBar(new NotificationPermissionInfoBarDelegate(tab, origin, |
229 callback_context)); | 269 callback_context)); |
230 } | 270 } |
| 271 |
| 272 void DesktopNotificationService::ShowNotification( |
| 273 const Notification& notification) { |
| 274 ui_manager_->Add(notification, profile_); |
| 275 } |
| 276 |
| 277 bool DesktopNotificationService::ShowDesktopNotification( |
| 278 const GURL& origin, const GURL& url, int process_id, int route_id, |
| 279 NotificationSource source, int notification_id) { |
| 280 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 281 NotificationObjectProxy* proxy = |
| 282 new NotificationObjectProxy(process_id, route_id, |
| 283 notification_id, |
| 284 source == WorkerNotification); |
| 285 Notification notif(origin, url, proxy); |
| 286 ShowNotification(notif); |
| 287 return true; |
| 288 } |
| 289 |
| 290 bool DesktopNotificationService::ShowDesktopNotificationText( |
| 291 const GURL& origin, const GURL& icon, const string16& title, |
| 292 const string16& text, int process_id, int route_id, |
| 293 NotificationSource source, int notification_id) { |
| 294 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 295 NotificationObjectProxy* proxy = |
| 296 new NotificationObjectProxy(process_id, route_id, |
| 297 notification_id, |
| 298 source == WorkerNotification); |
| 299 // "upconvert" the string parameters to a data: URL. |
| 300 string16 data_url = CreateDataUrl(icon, title, text); |
| 301 Notification notif(origin, GURL(data_url), proxy); |
| 302 ShowNotification(notif); |
| 303 return true; |
| 304 } |
OLD | NEW |