summaryrefslogtreecommitdiffstats
path: root/chrome/browser/notifications/desktop_notification_service.cc
diff options
context:
space:
mode:
authorjohnnyg@chromium.org <johnnyg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-30 23:53:08 +0000
committerjohnnyg@chromium.org <johnnyg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-30 23:53:08 +0000
commit3a99802be0067b190240a5689a3e10cc35d8f80e (patch)
tree685344e6199423087c31031f87b554cf3c0f2d53 /chrome/browser/notifications/desktop_notification_service.cc
parent9b48e1b12ac878603851830e7a57df3962c5848b (diff)
downloadchromium_src-3a99802be0067b190240a5689a3e10cc35d8f80e.zip
chromium_src-3a99802be0067b190240a5689a3e10cc35d8f80e.tar.gz
chromium_src-3a99802be0067b190240a5689a3e10cc35d8f80e.tar.bz2
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
Diffstat (limited to 'chrome/browser/notifications/desktop_notification_service.cc')
-rw-r--r--chrome/browser/notifications/desktop_notification_service.cc76
1 files changed, 75 insertions, 1 deletions
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<string16> 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;
+}