diff options
Diffstat (limited to 'chrome/browser')
5 files changed, 186 insertions, 7 deletions
diff --git a/chrome/browser/extensions/api/notifications/notifications_api.cc b/chrome/browser/extensions/api/notifications/notifications_api.cc index 1625432..3418c43 100644 --- a/chrome/browser/extensions/api/notifications/notifications_api.cc +++ b/chrome/browser/extensions/api/notifications/notifications_api.cc @@ -447,11 +447,19 @@ bool NotificationsApiFunction::UpdateNotification( return true; } -bool NotificationsApiFunction::IsNotificationsApiEnabled() { +bool NotificationsApiFunction::AreExtensionNotificationsAllowed() const { DesktopNotificationService* service = DesktopNotificationServiceFactory::GetForProfile(profile()); return service->IsNotifierEnabled(message_center::NotifierId( - message_center::NotifierId::APPLICATION, extension_->id())); + message_center::NotifierId::APPLICATION, extension_->id())); +} + +bool NotificationsApiFunction::IsNotificationsApiEnabled() const { + return CanRunWhileDisabled() || AreExtensionNotificationsAllowed(); +} + +bool NotificationsApiFunction::CanRunWhileDisabled() const { + return false; } bool NotificationsApiFunction::RunImpl() { @@ -601,4 +609,26 @@ bool NotificationsGetAllFunction::RunNotificationsApi() { return true; } +NotificationsGetPermissionLevelFunction:: +NotificationsGetPermissionLevelFunction() {} + +NotificationsGetPermissionLevelFunction:: +~NotificationsGetPermissionLevelFunction() {} + +bool NotificationsGetPermissionLevelFunction::CanRunWhileDisabled() const { + return true; +} + +bool NotificationsGetPermissionLevelFunction::RunNotificationsApi() { + api::notifications::PermissionLevel result = + AreExtensionNotificationsAllowed() + ? api::notifications::PERMISSION_LEVEL_GRANTED + : api::notifications::PERMISSION_LEVEL_DENIED; + + SetResult(new base::StringValue(api::notifications::ToString(result))); + SendResponse(true); + + return true; +} + } // namespace extensions diff --git a/chrome/browser/extensions/api/notifications/notifications_api.h b/chrome/browser/extensions/api/notifications/notifications_api.h index a465604..1f5eb4c 100644 --- a/chrome/browser/extensions/api/notifications/notifications_api.h +++ b/chrome/browser/extensions/api/notifications/notifications_api.h @@ -33,7 +33,13 @@ class NotificationsApiFunction : public ApiFunction { api::notifications::NotificationOptions* options, Notification* notification); - bool IsNotificationsApiEnabled(); + bool IsNotificationsApiEnabled() const; + + bool AreExtensionNotificationsAllowed() const; + + // Returns true if the API function is still allowed to run even when the + // notifications for a notifier have been disabled. + virtual bool CanRunWhileDisabled() const; // Called inside of RunImpl. virtual bool RunNotificationsApi() = 0; @@ -49,7 +55,7 @@ class NotificationsCreateFunction : public NotificationsApiFunction { public: NotificationsCreateFunction(); - // UIThreadExtensionFunction: + // NotificationsApiFunction: virtual bool RunNotificationsApi() OVERRIDE; protected: @@ -65,7 +71,7 @@ class NotificationsUpdateFunction : public NotificationsApiFunction { public: NotificationsUpdateFunction(); - // UIThreadExtensionFunction: + // NotificationsApiFunction: virtual bool RunNotificationsApi() OVERRIDE; protected: @@ -81,7 +87,7 @@ class NotificationsClearFunction : public NotificationsApiFunction { public: NotificationsClearFunction(); - // UIThreadExtensionFunction: + // NotificationsApiFunction: virtual bool RunNotificationsApi() OVERRIDE; protected: @@ -97,7 +103,7 @@ class NotificationsGetAllFunction : public NotificationsApiFunction { public: NotificationsGetAllFunction(); - // UIThreadExtensionFunction: + // NotificationsApiFunction: virtual bool RunNotificationsApi() OVERRIDE; protected: @@ -107,6 +113,23 @@ class NotificationsGetAllFunction : public NotificationsApiFunction { DECLARE_EXTENSION_FUNCTION("notifications.getAll", NOTIFICATIONS_GET_ALL) }; +class NotificationsGetPermissionLevelFunction : + public NotificationsApiFunction { + public: + NotificationsGetPermissionLevelFunction(); + + // NotificationsApiFunction: + virtual bool CanRunWhileDisabled() const OVERRIDE; + virtual bool RunNotificationsApi() OVERRIDE; + + protected: + virtual ~NotificationsGetPermissionLevelFunction(); + + private: + DECLARE_EXTENSION_FUNCTION("notifications.getPermissionLevel", + NOTIFICATIONS_GET_ALL) +}; + } // namespace extensions #endif // CHROME_BROWSER_EXTENSIONS_API_NOTIFICATIONS_NOTIFICATIONS_API_H_ diff --git a/chrome/browser/extensions/api/notifications/notifications_apitest.cc b/chrome/browser/extensions/api/notifications/notifications_apitest.cc index bd85021..a41b656 100644 --- a/chrome/browser/extensions/api/notifications/notifications_apitest.cc +++ b/chrome/browser/extensions/api/notifications/notifications_apitest.cc @@ -14,6 +14,7 @@ #include "ui/message_center/message_center.h" #include "ui/message_center/message_center_switches.h" #include "ui/message_center/message_center_util.h" +#include "ui/message_center/notifier_settings.h" using extensions::Extension; @@ -700,3 +701,105 @@ IN_PROC_BROWSER_TEST_F(NotificationsApiTest, TestPartialUpdate) { ASSERT_TRUE(copy_bool_value); } } + +// MessaceCenter-specific test. +#if defined(RUN_MESSAGE_CENTER_TESTS) +#define MAYBE_TestGetPermissionLevel TestGetPermissionLevel +#else +#define MAYBE_TestGetPermissionLevel DISABLED_TestGetPermissionLevel +#endif + +IN_PROC_BROWSER_TEST_F(NotificationsApiTest, MAYBE_TestGetPermissionLevel) { + scoped_refptr<Extension> empty_extension(utils::CreateEmptyExtension()); + + // Get permission level for the extension whose notifications are enabled. + { + scoped_refptr<extensions::NotificationsGetPermissionLevelFunction> + notification_function( + new extensions::NotificationsGetPermissionLevelFunction()); + + notification_function->set_extension(empty_extension.get()); + notification_function->set_has_callback(true); + + scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( + notification_function.get(), + "[]", + browser(), + utils::NONE)); + + EXPECT_EQ(base::Value::TYPE_STRING, result->GetType()); + std::string permission_level; + EXPECT_TRUE(result->GetAsString(&permission_level)); + EXPECT_EQ("granted", permission_level); + } + + // Get permission level for the extension whose notifications are disabled. + { + scoped_refptr<extensions::NotificationsGetPermissionLevelFunction> + notification_function( + new extensions::NotificationsGetPermissionLevelFunction()); + + notification_function->set_extension(empty_extension.get()); + notification_function->set_has_callback(true); + + message_center::NotifierId notifier_id( + message_center::NotifierId::APPLICATION, + empty_extension->id()); + message_center::Notifier notifier(notifier_id, string16(), true); + g_browser_process->message_center()->GetNotifierSettingsProvider()-> + SetNotifierEnabled(notifier, false); + + scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( + notification_function.get(), + "[]", + browser(), + utils::NONE)); + + EXPECT_EQ(base::Value::TYPE_STRING, result->GetType()); + std::string permission_level; + EXPECT_TRUE(result->GetAsString(&permission_level)); + EXPECT_EQ("denied", permission_level); + } +} + +// MessaceCenter-specific test. +#if defined(RUN_MESSAGE_CENTER_TESTS) +#define MAYBE_TestOnPermissionLevelChanged TestOnPermissionLevelChanged +#else +#define MAYBE_TestOnPermissionLevelChanged DISABLED_TestOnPermissionLevelChanged +#endif + +IN_PROC_BROWSER_TEST_F(NotificationsApiTest, + MAYBE_TestOnPermissionLevelChanged) { + const extensions::Extension* extension = + LoadExtensionAndWait("notifications/api/permission"); + ASSERT_TRUE(extension) << message_; + + // Test permission level changing from granted to denied. + { + ResultCatcher catcher; + + message_center::NotifierId notifier_id( + message_center::NotifierId::APPLICATION, + extension->id()); + message_center::Notifier notifier(notifier_id, string16(), true); + g_browser_process->message_center()->GetNotifierSettingsProvider()-> + SetNotifierEnabled(notifier, false); + + EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); + } + + // Test permission level changing from denied to granted. + { + ResultCatcher catcher; + + message_center::NotifierId notifier_id( + message_center::NotifierId::APPLICATION, + extension->id()); + message_center::Notifier notifier(notifier_id, string16(), false); + g_browser_process->message_center()->GetNotifierSettingsProvider()-> + SetNotifierEnabled(notifier, true); + + EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); + } +} diff --git a/chrome/browser/notifications/desktop_notification_service.cc b/chrome/browser/notifications/desktop_notification_service.cc index a910ecb..5b2e7f3 100644 --- a/chrome/browser/notifications/desktop_notification_service.cc +++ b/chrome/browser/notifications/desktop_notification_service.cc @@ -12,6 +12,8 @@ #include "chrome/browser/content_settings/content_settings_details.h" #include "chrome/browser/content_settings/content_settings_provider.h" #include "chrome/browser/content_settings/host_content_settings_map.h" +#include "chrome/browser/extensions/api/notifications/notifications_api.h" +#include "chrome/browser/extensions/event_router.h" #include "chrome/browser/extensions/extension_info_map.h" #include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/extensions/extension_system.h" @@ -587,6 +589,7 @@ void DesktopNotificationService::SetNotifierEnabled( pref_name = prefs::kMessageCenterDisabledExtensionIds; add_new_item = !enabled; id.reset(new base::StringValue(notifier_id.id)); + FirePermissionLevelChangedEvent(notifier_id, enabled); break; case NotifierId::SYSTEM_COMPONENT: #if defined(OS_CHROMEOS) @@ -682,3 +685,19 @@ void DesktopNotificationService::Observe( SetNotifierEnabled(notifier_id, true); } + +void DesktopNotificationService::FirePermissionLevelChangedEvent( + const NotifierId& notifier_id, bool enabled) { + DCHECK_EQ(NotifierId::APPLICATION, notifier_id.type); + extensions::api::notifications::PermissionLevel permission = + enabled ? extensions::api::notifications::PERMISSION_LEVEL_GRANTED + : extensions::api::notifications::PERMISSION_LEVEL_DENIED; + scoped_ptr<base::ListValue> args(new base::ListValue()); + args->Append(new base::StringValue( + extensions::api::notifications::ToString(permission))); + scoped_ptr<extensions::Event> event(new extensions::Event( + extensions::api::notifications::OnPermissionLevelChanged::kEventName, + args.Pass())); + extensions::ExtensionSystem::Get(profile_)->event_router()-> + DispatchEventToExtension(notifier_id.id, event.Pass()); +} diff --git a/chrome/browser/notifications/desktop_notification_service.h b/chrome/browser/notifications/desktop_notification_service.h index 46defd4..cd9dbd0 100644 --- a/chrome/browser/notifications/desktop_notification_service.h +++ b/chrome/browser/notifications/desktop_notification_service.h @@ -195,6 +195,10 @@ class DesktopNotificationService : public BrowserContextKeyedService, // Called when the enabled_sync_notifier_id pref has been changed. void OnEnabledSyncNotifierIdsChanged(); + void FirePermissionLevelChangedEvent( + const message_center::NotifierId& notifier_id, + bool enabled); + // content::NotificationObserver: virtual void Observe(int type, const content::NotificationSource& source, |