From 3a99802be0067b190240a5689a3e10cc35d8f80e Mon Sep 17 00:00:00 2001 From: "johnnyg@chromium.org" Date: Fri, 30 Oct 2009 23:53:08 +0000 Subject: Now that the UI layer is accessible cross-platform, coalesce the DesktopNotificationService layer into one common module. BUG=none TEST=none Review URL: http://codereview.chromium.org/343066 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@30652 0039d316-1c4b-4281-b951-d872f2087c98 --- .../notifications/desktop_notification_service.cc | 76 ++++++++++++++++- .../desktop_notification_service_linux.cc | 30 ------- .../desktop_notification_service_mac.mm | 30 ------- .../desktop_notification_service_win.cc | 94 ---------------------- 4 files changed, 75 insertions(+), 155 deletions(-) delete mode 100644 chrome/browser/notifications/desktop_notification_service_linux.cc delete mode 100644 chrome/browser/notifications/desktop_notification_service_mac.mm delete mode 100644 chrome/browser/notifications/desktop_notification_service_win.cc (limited to 'chrome/browser/notifications') diff --git a/chrome/browser/notifications/desktop_notification_service.cc b/chrome/browser/notifications/desktop_notification_service.cc index 76a7bd9..08e0d2b 100644 --- a/chrome/browser/notifications/desktop_notification_service.cc +++ b/chrome/browser/notifications/desktop_notification_service.cc @@ -6,12 +6,15 @@ #include "app/l10n_util.h" #include "app/resource_bundle.h" +#include "base/string_piece.h" +#include "base/string_util.h" #include "base/thread.h" #include "chrome/browser/browser_list.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/chrome_thread.h" #include "chrome/browser/notifications/notification.h" #include "chrome/browser/notifications/notification_object_proxy.h" +#include "chrome/browser/notifications/notification_ui_manager.h" #include "chrome/browser/notifications/notifications_prefs_cache.h" #include "chrome/browser/profile.h" #include "chrome/browser/renderer_host/render_process_host.h" @@ -24,13 +27,50 @@ #include "chrome/common/pref_names.h" #include "chrome/common/pref_service.h" #include "chrome/common/render_messages.h" -#include "webkit/api/public/WebNotificationPresenter.h" +#include "grit/browser_resources.h" #include "grit/chromium_strings.h" #include "grit/generated_resources.h" #include "grit/theme_resources.h" +#include "webkit/api/public/WebNotificationPresenter.h" using WebKit::WebNotificationPresenter; +// Creates a data:xxxx URL which contains the full HTML for a notification +// using supplied icon, title, and text, run through a template which contains +// the standard formatting for notifications. +static string16 CreateDataUrl(const GURL& icon_url, const string16& title, + const string16& body) { + const base::StringPiece template_html( + ResourceBundle::GetSharedInstance().GetRawDataResource( + IDR_NOTIFICATION_HTML)); + + if (template_html.empty()) { + NOTREACHED() << "unable to load template. ID: " << IDR_NOTIFICATION_HTML; + return EmptyString16(); + } + + std::vector subst; + if (icon_url.is_valid()) + subst.push_back(UTF8ToUTF16(icon_url.spec())); + else + subst.push_back(EmptyString16()); + + subst.push_back(title); + subst.push_back(body); + + if (icon_url.is_valid()) { + subst.push_back(ASCIIToUTF16("block")); + subst.push_back(ASCIIToUTF16("60")); + } else { + subst.push_back(ASCIIToUTF16("none")); + subst.push_back(ASCIIToUTF16("5")); + } + + string16 format_string = ASCIIToUTF16("data:text/html;charset=utf-8," + + template_html.as_string()); + return ReplaceStringPlaceholders(format_string, subst, NULL); +} + // A task object which calls the renderer to inform the web page that the // permission request has completed. class NotificationPermissionCallbackTask : public Task { @@ -228,3 +268,37 @@ void DesktopNotificationService::RequestPermission( tab->AddInfoBar(new NotificationPermissionInfoBarDelegate(tab, origin, callback_context)); } + +void DesktopNotificationService::ShowNotification( + const Notification& notification) { + ui_manager_->Add(notification, profile_); +} + +bool DesktopNotificationService::ShowDesktopNotification( + const GURL& origin, const GURL& url, int process_id, int route_id, + NotificationSource source, int notification_id) { + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); + NotificationObjectProxy* proxy = + new NotificationObjectProxy(process_id, route_id, + notification_id, + source == WorkerNotification); + Notification notif(origin, url, proxy); + ShowNotification(notif); + return true; +} + +bool DesktopNotificationService::ShowDesktopNotificationText( + const GURL& origin, const GURL& icon, const string16& title, + const string16& text, int process_id, int route_id, + NotificationSource source, int notification_id) { + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); + NotificationObjectProxy* proxy = + new NotificationObjectProxy(process_id, route_id, + notification_id, + source == WorkerNotification); + // "upconvert" the string parameters to a data: URL. + string16 data_url = CreateDataUrl(icon, title, text); + Notification notif(origin, GURL(data_url), proxy); + ShowNotification(notif); + return true; +} diff --git a/chrome/browser/notifications/desktop_notification_service_linux.cc b/chrome/browser/notifications/desktop_notification_service_linux.cc deleted file mode 100644 index 15148ce..0000000 --- a/chrome/browser/notifications/desktop_notification_service_linux.cc +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/notifications/desktop_notification_service.h" - -bool DesktopNotificationService::ShowDesktopNotification( - const GURL& url, - const GURL&, - int process_id, - int route_id, - NotificationSource source, - int notification_id) { - // TODO(johnnyg): http://crbug.com/23954 Linux support coming soon. - return false; -} - -bool DesktopNotificationService::ShowDesktopNotificationText( - const GURL& origin, - const GURL& url, - const string16& title, - const string16& text, - int process_id, - int route_id, - NotificationSource source, - int notification_id) { - // TODO(johnnyg): http://crbug.com/23066 Linux support coming soon. - // Coming soon. - return false; -} diff --git a/chrome/browser/notifications/desktop_notification_service_mac.mm b/chrome/browser/notifications/desktop_notification_service_mac.mm deleted file mode 100644 index 42473c4..0000000 --- a/chrome/browser/notifications/desktop_notification_service_mac.mm +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/notifications/desktop_notification_service.h" - -bool DesktopNotificationService::ShowDesktopNotification( - const GURL& url, - const GURL&, - int process_id, - int route_id, - NotificationSource source, - int notification_id) { - // TODO(johnnyg): http://crbug.com/23066 Mac support coming soon. - return false; -} - -bool DesktopNotificationService::ShowDesktopNotificationText( - const GURL& origin, - const GURL& url, - const string16& title, - const string16& text, - int process_id, - int route_id, - NotificationSource source, - int notification_id) { - // TODO(johnnyg): http://crbug.com/23066 Integration with Growl will go here - // Coming soon. - return false; -} diff --git a/chrome/browser/notifications/desktop_notification_service_win.cc b/chrome/browser/notifications/desktop_notification_service_win.cc deleted file mode 100644 index f6494cd..0000000 --- a/chrome/browser/notifications/desktop_notification_service_win.cc +++ /dev/null @@ -1,94 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/notifications/desktop_notification_service.h" - -#include "app/l10n_util.h" -#include "app/resource_bundle.h" -#include "base/string_piece.h" -#include "base/string_util.h" -#include "base/values.h" -#include "chrome/browser/browser_list.h" -#include "chrome/browser/chrome_thread.h" -#include "chrome/browser/notifications/notification_object_proxy.h" -#include "chrome/browser/notifications/notification_ui_manager.h" -#include "chrome/browser/renderer_host/render_process_host.h" -#include "chrome/browser/renderer_host/site_instance.h" -#include "chrome/common/child_process_host.h" -#include "grit/browser_resources.h" -#include "grit/generated_resources.h" - -// Creates a data:xxxx URL which contains the full HTML for a notification -// using supplied icon, title, and text, run through a template which contains -// the standard formatting for notifications. -static string16 CreateDataUrl(const GURL& icon_url, const string16& title, - const string16& body) { - const base::StringPiece template_html( - ResourceBundle::GetSharedInstance().GetRawDataResource( - IDR_NOTIFICATION_HTML)); - - if (template_html.empty()) { - NOTREACHED() << "unable to load template. ID: " << IDR_NOTIFICATION_HTML; - return EmptyString16(); - } - - std::vector subst; - if (icon_url.is_valid()) - subst.push_back(UTF8ToUTF16(icon_url.spec())); - else - subst.push_back(EmptyString16()); - - subst.push_back(title); - subst.push_back(body); - - if (icon_url.is_valid()) { - subst.push_back(ASCIIToUTF16("block")); - subst.push_back(ASCIIToUTF16("60")); - } else { - subst.push_back(ASCIIToUTF16("none")); - subst.push_back(ASCIIToUTF16("5")); - } - - string16 format_string = ASCIIToUTF16("data:text/html;charset=utf-8," - + template_html.as_string()); - return ReplaceStringPlaceholders(format_string, subst, NULL); -} - -// This will call the notification manager on the UI thread to actually -// put the notification with the requested parameters on the desktop. -void DesktopNotificationService::ShowNotification( - const Notification& notification) { - ui_manager_->Add(notification, profile_); -} - -// Shows a notification bubble which contains the contents of url. -bool DesktopNotificationService::ShowDesktopNotification( - const GURL& origin, const GURL& url, int process_id, int route_id, - NotificationSource source, int notification_id) { - DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); - NotificationObjectProxy* proxy = - new NotificationObjectProxy(process_id, route_id, - notification_id, - source == WorkerNotification); - Notification notif(origin, url, proxy); - ShowNotification(notif); - return true; -} - -// Shows a notification constructed from an icon, title, and text. -bool DesktopNotificationService::ShowDesktopNotificationText( - const GURL& origin, const GURL& icon, const string16& title, - const string16& text, int process_id, int route_id, - NotificationSource source, int notification_id) { - DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); - NotificationObjectProxy* proxy = - new NotificationObjectProxy(process_id, route_id, - notification_id, - source == WorkerNotification); - // "upconvert" the string parameters to a data: URL. - string16 data_url = CreateDataUrl(icon, title, text); - Notification notif(origin, GURL(data_url), proxy); - ShowNotification(notif); - return true; -} -- cgit v1.1