From 0faf2aa8df6e27a0ae8b720440df9907c52fdf31 Mon Sep 17 00:00:00 2001 From: "johnnyg@chromium.org" Date: Tue, 24 Nov 2009 22:10:49 +0000 Subject: Make notifications permission available in the extension manifest and hook up to the desktop notification service. BUG=27249 TEST=none Review URL: http://codereview.chromium.org/432005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@32979 0039d316-1c4b-4281-b951-d872f2087c98 --- .../notifications/desktop_notification_service.cc | 34 ++++++++++++++++++++-- .../notifications/desktop_notification_service.h | 20 +++++++++---- 2 files changed, 47 insertions(+), 7 deletions(-) (limited to 'chrome/browser/notifications') diff --git a/chrome/browser/notifications/desktop_notification_service.cc b/chrome/browser/notifications/desktop_notification_service.cc index 9ece534..0c98404 100644 --- a/chrome/browser/notifications/desktop_notification_service.cc +++ b/chrome/browser/notifications/desktop_notification_service.cc @@ -191,6 +191,11 @@ DesktopNotificationService::DesktopNotificationService(Profile* profile, : profile_(profile), ui_manager_(ui_manager) { InitPrefs(); + + // Listen for new extension installations so we can cache permissions. + notification_registrar_.Add(this, + NotificationType::EXTENSION_LOADED, + Source(profile_)); } DesktopNotificationService::~DesktopNotificationService() { @@ -212,6 +217,31 @@ void DesktopNotificationService::InitPrefs() { denied_sites = prefs->GetList(prefs::kDesktopNotificationDeniedOrigins); prefs_cache_ = new NotificationsPrefsCache(allowed_sites, denied_sites); + + ExtensionsService* ext_service = profile_->GetExtensionsService(); + if (ext_service) { + const ExtensionList* extensions = ext_service->extensions(); + for (ExtensionList::const_iterator iter = extensions->begin(); + iter != extensions->end(); ++iter) { + if ((*iter)->HasApiPermission(Extension::kNotificationPermission)) { + prefs_cache_->CacheAllowedOrigin((*iter)->url()); + } + } + } +} + +void DesktopNotificationService::Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) { + if (type != NotificationType::EXTENSION_LOADED) { + NOTREACHED(); + return; + } + + Details extension = static_cast >(details); + if (extension->HasApiPermission(Extension::kNotificationPermission)) { + GrantPermission(extension->url()); + } } void DesktopNotificationService::GrantPermission(const GURL& origin) { @@ -292,7 +322,7 @@ bool DesktopNotificationService::CancelDesktopNotification( bool DesktopNotificationService::ShowDesktopNotification( const GURL& origin, const GURL& url, int process_id, int route_id, - NotificationSource source, int notification_id) { + DesktopNotificationSource source, int notification_id) { DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); NotificationObjectProxy* proxy = new NotificationObjectProxy(process_id, route_id, @@ -306,7 +336,7 @@ bool DesktopNotificationService::ShowDesktopNotification( 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) { + DesktopNotificationSource source, int notification_id) { DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); NotificationObjectProxy* proxy = new NotificationObjectProxy(process_id, route_id, diff --git a/chrome/browser/notifications/desktop_notification_service.h b/chrome/browser/notifications/desktop_notification_service.h index ad075e2..05141ff 100644 --- a/chrome/browser/notifications/desktop_notification_service.h +++ b/chrome/browser/notifications/desktop_notification_service.h @@ -9,6 +9,8 @@ #include "base/basictypes.h" #include "chrome/browser/notifications/notification.h" +#include "chrome/common/notification_registrar.h" +#include "chrome/common/notification_service.h" #include "googleurl/src/gurl.h" class NotificationUIManager; @@ -19,16 +21,16 @@ class Task; // The DesktopNotificationService is an object, owned by the Profile, // which provides the creation of desktop "toasts" to web pages and workers. -class DesktopNotificationService { +class DesktopNotificationService : public NotificationObserver { public: - enum NotificationSource { + enum DesktopNotificationSource { PageNotification, WorkerNotification }; DesktopNotificationService(Profile* profile, NotificationUIManager* ui_manager); - ~DesktopNotificationService(); + virtual ~DesktopNotificationService(); // Requests permission (using an info-bar) for a given origin. // |callback_context| contains an opaque value to pass back to the @@ -49,11 +51,11 @@ class DesktopNotificationService { // value to be passed back to the process when events occur on // this notification. bool ShowDesktopNotification(const GURL& origin, const GURL& url, - int process_id, int route_id, NotificationSource source, + int process_id, int route_id, DesktopNotificationSource source, int notification_id); bool ShowDesktopNotificationText(const GURL& origin, const GURL& icon, const string16& title, const string16& text, int process_id, - int route_id, NotificationSource source, int notification_id); + int route_id, DesktopNotificationSource source, int notification_id); // Cancels a notification. If it has already been shown, it will be // removed from the screen. If it hasn't been shown yet, it won't be @@ -68,6 +70,11 @@ class DesktopNotificationService { NotificationsPrefsCache* prefs_cache() { return prefs_cache_; } + // NotificationObserver interface. + virtual void Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details); + private: void InitPrefs(); @@ -87,6 +94,9 @@ class DesktopNotificationService { // UI for desktop toasts. NotificationUIManager* ui_manager_; + // Connection to the service providing the other kind of notifications. + NotificationRegistrar notification_registrar_; + DISALLOW_COPY_AND_ASSIGN(DesktopNotificationService); }; -- cgit v1.1