From e8ef8769f62b13fab35bd41323cafc70fb74f230 Mon Sep 17 00:00:00 2001 From: "liyanhou@chromium.org" Date: Sat, 16 Aug 2014 06:10:14 +0000 Subject: [PATCH] Add NotifyOnPermissionLevelChanged implementation of notification provider API When an extension/app with notificationProvider permission shows a user information about sources of the notifications, the notifiers. When the user wants to allow or disallow one notifier, NotifyOnPermissionLevelChanged can be used to inform the notifier that its permission level was changed. BUG=397197 Review URL: https://codereview.chromium.org/468813002 Cr-Commit-Position: refs/heads/master@{#290105} git-svn-id: svn://svn.chromium.org/chrome/trunk/src@290105 0039d316-1c4b-4281-b951-d872f2087c98 --- .../notification_provider_api.cc | 26 ++++++++++++++++++- .../basic_usage/background.js | 23 +++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/chrome/browser/extensions/api/notification_provider/notification_provider_api.cc b/chrome/browser/extensions/api/notification_provider/notification_provider_api.cc index 78f773789340..784b538322ef 100644 --- a/chrome/browser/extensions/api/notification_provider/notification_provider_api.cc +++ b/chrome/browser/extensions/api/notification_provider/notification_provider_api.cc @@ -10,6 +10,8 @@ #include "base/strings/string_number_conversions.h" #include "base/strings/utf_string_conversions.h" #include "chrome/browser/browser_process.h" +#include "chrome/browser/notifications/desktop_notification_service.h" +#include "chrome/browser/notifications/desktop_notification_service_factory.h" #include "chrome/browser/notifications/notification.h" #include "chrome/browser/notifications/notification_ui_manager.h" #include "chrome/common/chrome_version_info.h" @@ -194,9 +196,31 @@ NotificationProviderNotifyOnPermissionLevelChangedFunction::Run() { params = api::notification_provider::NotifyOnPermissionLevelChanged:: Params::Create(*args_); EXTENSION_FUNCTION_VALIDATE(params.get()); + + // Third party apps/extensions with notification provider API will not be able + // to change permission levels of web notifiers, because the list of allowed + // websites should only be set in Chrome Settings manually by users. But they + // are able to change permission levels of application type notifiers. + bool is_application_type = + (params->notifier_type == + api::notification_provider::NotifierType::NOTIFIER_TYPE_APPLICATION); + if (is_application_type) { + bool enabled = + (params->level == api::notification_provider::NotifierPermissionLevel:: + NOTIFIER_PERMISSION_LEVEL_GRANTED); + + DesktopNotificationService* desktop_notification_service = + DesktopNotificationServiceFactory::GetForProfile(GetProfile()); + message_center::NotifierId notifier_id( + message_center::NotifierId::NotifierType::APPLICATION, + params->notifier_id); + + desktop_notification_service->SetNotifierEnabled(notifier_id, enabled); + } + return RespondNow( ArgumentList(api::notification_provider::NotifyOnPermissionLevelChanged:: - Results::Create(true))); + Results::Create(is_application_type))); } NotificationProviderNotifyOnShowSettingsFunction:: diff --git a/chrome/test/data/extensions/api_test/notification_provider/basic_usage/background.js b/chrome/test/data/extensions/api_test/notification_provider/basic_usage/background.js index f62df7753cf5..39e50d551bdb 100644 --- a/chrome/test/data/extensions/api_test/notification_provider/basic_usage/background.js +++ b/chrome/test/data/extensions/api_test/notification_provider/basic_usage/background.js @@ -160,6 +160,26 @@ function testNotifyOnClosed() { .catch(function() { failTest("NotifyOnCleared"); }); } +function testNotifyOnPermissionLevelChanged() { + chrome.notifications.onPermissionLevelChanged.addListener(function(level) { + chrome.test.succeed(); + }); + + // Create a notification, so there will be one existing notification. + createNotification(id1, content) + .catch(function() { failTest("notifications.create"); }) + // Try to notify a web type notifier its permissional level is changed. + .then(function() { return notifyOnPermissionLevelChanged("SomeURL", + "web", + "granted"); }) + .then(function() { failTest("NotifyOnPermissionLevelChanged"); }) + // Notify that the permission level of current notifier is changed. + .catch(function () { return notifyOnPermissionLevelChanged(myId, + "application", + "granted"); }) + .catch(function() { failTest("NotifyOnPermissionLevelChanged"); }); +} + function testNotifyOnShowSettings() { chrome.notifications.onShowSettings.addListener(function() { chrome.test.succeed(); @@ -175,10 +195,11 @@ function testNotifyOnShowSettings() { .then(function() { failTest("NotifyOnShowSettings"); }) // Notify current notifier that a user checked its settings. .catch(function () { return notifyOnShowSettings(myId, "application"); }) - .catch(function() { failTest("NotifyOnShowSettings"); }) + .catch(function() { failTest("NotifyOnShowSettings"); }); } chrome.test.runTests([ testNotifyOnClicked, testNotifyOnButtonClicked, testNotifyOnClosed, + testNotifyOnPermissionLevelChanged, testNotifyOnShowSettings ]); \ No newline at end of file