summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjianli@chromium.org <jianli@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-10-22 22:59:12 +0000
committerjianli@chromium.org <jianli@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-10-22 22:59:12 +0000
commitd746bbe69a40934d99c9ea5822c1203cd4a56150 (patch)
tree990ceedd1609c99ae949261d32f086ec1335086e
parent35a70a431982741d1451b9ac612cd3bc716df232 (diff)
downloadchromium_src-d746bbe69a40934d99c9ea5822c1203cd4a56150.zip
chromium_src-d746bbe69a40934d99c9ea5822c1203cd4a56150.tar.gz
chromium_src-d746bbe69a40934d99c9ea5822c1203cd4a56150.tar.bz2
Add getPermissionLevel and onPermissionLevelChanged to notifications API
BUG=305911 TEST=new tests Review URL: https://codereview.chromium.org/33173002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@230241 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/extensions/api/notifications/notifications_api.cc34
-rw-r--r--chrome/browser/extensions/api/notifications/notifications_api.h33
-rw-r--r--chrome/browser/extensions/api/notifications/notifications_apitest.cc103
-rw-r--r--chrome/browser/notifications/desktop_notification_service.cc19
-rw-r--r--chrome/browser/notifications/desktop_notification_service.h4
-rw-r--r--chrome/common/extensions/api/notifications.idl18
-rw-r--r--chrome/test/data/extensions/api_test/notifications/api/permission/background.js34
-rw-r--r--chrome/test/data/extensions/api_test/notifications/api/permission/manifest.json13
8 files changed, 251 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,
diff --git a/chrome/common/extensions/api/notifications.idl b/chrome/common/extensions/api/notifications.idl
index 85ae00a..1eb4349 100644
--- a/chrome/common/extensions/api/notifications.idl
+++ b/chrome/common/extensions/api/notifications.idl
@@ -19,6 +19,15 @@ namespace notifications {
progress
};
+ enum PermissionLevel {
+ // User has elected to show notifications from the app or extension.
+ // This is the default at install time.
+ granted,
+
+ // User has elected not to show notifications from the app or extension.
+ denied
+ };
+
dictionary NotificationItem {
// Title of one item of a list notification.
DOMString title;
@@ -93,6 +102,8 @@ namespace notifications {
callback GetAllCallback = void (object notifications);
+ callback PermissionLevelCallback = void (PermissionLevel level);
+
interface Functions {
// Creates and displays a notification having the contents in |options|,
// identified by the id |notificationId|. If |notificationId| is empty,
@@ -119,6 +130,10 @@ namespace notifications {
// |callback| is executed with the set of notification_ids currently in
// the system.
static void getAll(GetAllCallback callback);
+
+ // Retrieves whether the user has enabled notifications from this app
+ // or extension.
+ static void getPermissionLevel(PermissionLevelCallback callback);
};
interface Events {
@@ -130,6 +145,9 @@ namespace notifications {
// The user pressed a button in the notification.
static void onButtonClicked(DOMString notificationId, long buttonIndex);
+
+ // The user changes the permission level.
+ static void onPermissionLevelChanged(PermissionLevel level);
};
};
diff --git a/chrome/test/data/extensions/api_test/notifications/api/permission/background.js b/chrome/test/data/extensions/api_test/notifications/api/permission/background.js
new file mode 100644
index 0000000..a5bddec
--- /dev/null
+++ b/chrome/test/data/extensions/api_test/notifications/api/permission/background.js
@@ -0,0 +1,34 @@
+// Copyright 2013 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.
+
+var currentPermissionLevel = "";
+
+function onGetPermissionLevel(permissionLevel) {
+ currentPermissionLevel = permissionLevel;
+}
+
+function getPermissionLevel() {
+ chrome.notifications.getPermissionLevel(onGetPermissionLevel);
+}
+
+function onPermissionLevelChangedListener(permissionLevel) {
+ if (permissionLevel != "denied" && permissionLevel != "granted") {
+ chrome.test.notifyFail(
+ "Unexpected permission level " + permissionLevel + " received");
+ return;
+ }
+
+ if (permissionLevel == currentPermissionLevel) {
+ chrome.test.notifyFail(
+ "Same permission level " + permissionLevel + " received");
+ return;
+ }
+
+ currentPermissionLevel = permissionLevel;
+ chrome.test.notifyPass();
+}
+
+getPermissionLevel();
+chrome.notifications.onPermissionLevelChanged.addListener(
+ onPermissionLevelChangedListener);
diff --git a/chrome/test/data/extensions/api_test/notifications/api/permission/manifest.json b/chrome/test/data/extensions/api_test/notifications/api/permission/manifest.json
new file mode 100644
index 0000000..47067bb
--- /dev/null
+++ b/chrome/test/data/extensions/api_test/notifications/api/permission/manifest.json
@@ -0,0 +1,13 @@
+{
+ "name": "chrome.notifications",
+ "version": "0.1",
+ "description": "chrome.notifications API permissionLevelChanged",
+ "app": {
+ "background": {
+ "scripts": ["background.js"]
+ }
+ },
+ "permissions": [
+ "notifications"
+ ]
+}