diff options
author | liyanhou@chromium.org <liyanhou@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-08-15 09:12:13 +0000 |
---|---|---|
committer | liyanhou@chromium.org <liyanhou@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-08-15 09:12:13 +0000 |
commit | 357a9b32bb330f81713fd078d1e5df29070cfe0b (patch) | |
tree | 7a1dcc4430e825f05d4afdef2bd6923cab74d9bd | |
parent | 6b575366c524f05e8de8f93fcc9814fc25c91b9f (diff) | |
download | chromium_src-357a9b32bb330f81713fd078d1e5df29070cfe0b.zip chromium_src-357a9b32bb330f81713fd078d1e5df29070cfe0b.tar.gz chromium_src-357a9b32bb330f81713fd078d1e5df29070cfe0b.tar.bz2 |
Add NotifyOnShowSettings implementation of notification
provider API and test.
When an extension/app with notificationProvider permission
shows a user that certain notifiers have advanced settings.
When the user clicks and chooses to see advanced settings of
one notifier, NotifyOnShowSettings can be used to inform the
notifier that a user requested advanced settings, so the
notifier can do something else to display its advanced
settings.
BUG=397197
Review URL: https://codereview.chromium.org/456223002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@289823 0039d316-1c4b-4281-b951-d872f2087c98
3 files changed, 140 insertions, 58 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 34535fb..78f7737 100644 --- a/chrome/browser/extensions/api/notification_provider/notification_provider_api.cc +++ b/chrome/browser/extensions/api/notification_provider/notification_provider_api.cc @@ -17,6 +17,8 @@ #include "extensions/common/extension.h" #include "extensions/common/features/feature.h" #include "ui/base/layout.h" +#include "ui/message_center/message_center.h" +#include "ui/message_center/notifier_settings.h" #include "url/gurl.h" namespace extensions { @@ -191,9 +193,7 @@ NotificationProviderNotifyOnPermissionLevelChangedFunction::Run() { scoped_ptr<api::notification_provider::NotifyOnPermissionLevelChanged::Params> params = api::notification_provider::NotifyOnPermissionLevelChanged:: Params::Create(*args_); - EXTENSION_FUNCTION_VALIDATE(params.get()); - return RespondNow( ArgumentList(api::notification_provider::NotifyOnPermissionLevelChanged:: Results::Create(true))); @@ -213,8 +213,30 @@ NotificationProviderNotifyOnShowSettingsFunction::Run() { api::notification_provider::NotifyOnShowSettings::Params::Create(*args_); EXTENSION_FUNCTION_VALIDATE(params.get()); + bool has_advanced_settings; + // Only application type notifiers have advanced settings. + if (params->notifier_type == + api::notification_provider::NotifierType::NOTIFIER_TYPE_APPLICATION) { + // TODO(dewittj): Refactor NotificationUIManage API to have a getter of + // NotifierSettingsProvider, since it holds the settings provider. + message_center::NotifierSettingsProvider* settings_provider = + message_center::MessageCenter::Get()->GetNotifierSettingsProvider(); + + message_center::NotifierId notifier_id( + message_center::NotifierId::NotifierType::APPLICATION, + params->notifier_id); + + has_advanced_settings = + settings_provider->NotifierHasAdvancedSettings(notifier_id); + if (has_advanced_settings) + settings_provider->OnNotifierAdvancedSettingsRequested(notifier_id, NULL); + } else { + has_advanced_settings = false; + } + return RespondNow(ArgumentList( - api::notification_provider::NotifyOnShowSettings::Results::Create(true))); + api::notification_provider::NotifyOnShowSettings::Results::Create( + has_advanced_settings))); } NotificationProviderGetNotifierFunction:: diff --git a/chrome/common/extensions/api/notification_provider.idl b/chrome/common/extensions/api/notification_provider.idl index 4220f40..435615b 100644 --- a/chrome/common/extensions/api/notification_provider.idl +++ b/chrome/common/extensions/api/notification_provider.idl @@ -21,7 +21,21 @@ namespace notificationProvider { denied }; + enum NotifierType { + // Notifiers that are extensions or applications. + application, + + // Notifiers that are webistes. + web + }; + dictionary Notifier { + // Id of the notifier. + DOMString notifierId; + + // Type of the notifier. + NotifierType type; + // Name of the notifier. DOMString name; @@ -41,10 +55,9 @@ namespace notificationProvider { callback NotifyOnButtonClickedCallback = void (boolean matchExists); - callback NotifyOnPermissionLevelChangedCallback = - void (boolean notifierExists); + callback NotifyOnPermissionLevelChangedCallback = void (boolean wasChanged); - callback NotifyOnShowSettingsCallback = void (boolean notifierExists); + callback NotifyOnShowSettingsCallback = void (boolean hasSettings); callback GetNotifierCallback = void (Notifier notifier); @@ -84,18 +97,22 @@ namespace notificationProvider { // Inform the notifier that the user changed the permission level of that // notifier. // |notifierId|: The id of the notifier that sent the notification. + // |notifierType|: The type of the notifier that sent the notification. // |level|: The perission level of the notifier - // |callback|: Called to indicate whether the notifier existed. + // |callback|: Called to indicate whether the permission level was changed. static void notifyOnPermissionLevelChanged( DOMString notifierId, + NotifierType notifierType, NotifierPermissionLevel level, NotifyOnPermissionLevelChangedCallback callback); // Inform the notifier that the user chose to see advanced settings of that // notifier. // |notifierId|: The id of the notifier that sent the notification. - // |callback|: Called to indicate whether a matching notifier existed. + // |notifierType|: The type of the notifier that sent the notification. + // |callback|: Called to indicate whether the notifier has extra settings. static void notifyOnShowSettings(DOMString notifierId, + NotifierType notifierType, NotifyOnShowSettingsCallback callback); // To get a notifier from it's notifier ID. 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 58e5242..f62df77 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 @@ -4,6 +4,16 @@ const notificationProvider = chrome.notificationProvider; +var myId = chrome.runtime.id; +var id1 = "id1"; +var returnId = myId + "-" + id1; +var content = { + type: "basic", + iconUrl: "icon.png", + title: "Title", + message: "This is the message." +}; + function createNotification(notificationId, options) { return new Promise(function (resolve, reject) { chrome.notifications.create(notificationId, options, function (id) { @@ -63,31 +73,35 @@ function notifyOnButtonClicked(senderId, notificationId, buttonIndex) { }); }; -function notifyOnPermissionLevelChanged(senderId, permissionLevel) { +function notifyOnPermissionLevelChanged(senderId, + notifierType, + permissionLevel) { return new Promise(function (resolve, reject) { notificationProvider.notifyOnPermissionLevelChanged( - senderId, - permissionLevel, - function (notifierExists) { - if (chrome.runtime.lastError || !notifierExists) { + senderId, + notifierType, + permissionLevel, + function (wasChanged) { + if (chrome.runtime.lastError || !wasChanged) { reject(new Error("Unable to send onPermissionLevelChanged message")); return; } - resolve(notifierExists); + resolve(wasChanged); return; }); }); }; -function notifyOnShowSettings(senderId) { +function notifyOnShowSettings(senderId, notifierType) { return new Promise(function (resolve, reject) { notificationProvider.notifyOnShowSettings(senderId, - function (notifierExists) { - if (chrome.runtime.lastError || !notifierExists) { + notifierType, + function (hasSettings) { + if (chrome.runtime.lastError || !hasSettings) { reject(new Error("Unable to send onShowSettings message")); return; } - resolve(notifierExists); + resolve(hasSettings); return; }); }); @@ -97,45 +111,74 @@ function failTest(testName) { chrome.test.fail(testName); }; -function testFunctions() { - var myId = chrome.runtime.id; - var id1 = "id1"; - var returnId = myId + "-" + id1; - var content = { - type: "basic", - iconUrl: "icon.png", - title: "Title", - message: "This is the message." - }; - - // Create a notification, so there will be one existing notification +function testNotifyOnClicked(){ + chrome.notifications.onClicked.addListener(function(id) { + chrome.test.succeed(); + }); + + // Create a notification, so there will be one existing notification. createNotification(id1, content) - .catch(function() { failTest("notifications.create"); }) - // Notify the sender that a notificaion was clicked. - .then(function() { return notifyOnClicked(myId, returnId); }) - .catch(function() { failTest("NotifyOnClicked1"); }) - // Try to notify that an non-existent notification was clicked. - .then(function() { return notifyOnClicked(myId, "doesNotExist"); }) - // Fail if it returns true. - .then(function() { failTest("NotifyOnClicked2"); }) - // Notify the sender that a notificaion button was clicked. - .catch(function() { return notifyOnButtonClicked(myId, returnId, 0); }) - .catch(function() { failTest("NotifyOnButtonClicked"); }) - // Try to notify that a non-existent notification button was clicked. - .then(function() { return notifyOnButtonClicked(myId, "doesNotExist", 0)}) - .then(function() { failTest("NotifyOnButtonClicked"); }) - // Try to notify that an non-existent notification was cleared. - .catch(function () { return notifyOnCleared(myId, "doesNotExist"); }) - .then(function() { failTest("NotifyOnCleared"); }) - // Notify that the original notification was cleared. - .catch(function() { return notifyOnCleared(myId, returnId); }) - .catch(function() { failTest("NotifyOnCleared"); }) - .then(function () { return notifyOnPermissionLevelChanged(myId, - "granted"); }) - .catch(function() { failTest("NotifyOnPermissionLevelChanged"); }) - .then(function () { return notifyOnShowSettings(myId); }) - .catch(function() { failTest("NotifyOnShowSettings"); }) - .then(function() { chrome.test.succeed(); }); -}; + .catch(function() { failTest("notifications.create"); }) + // Try to notify that an non-existent notification was clicked. + .then(function() { return notifyOnClicked(myId, "doesNotExist"); }) + // Fail if it returns true. + .then(function() { failTest("NotifyOnClicked"); }) + // Notify the sender that a notificaion was clicked. + .catch(function() { return notifyOnClicked(myId, returnId); }) + .catch(function() { failTest("NotifyOnClicked"); }); +} + +function testNotifyOnButtonClicked() { + chrome.notifications.onButtonClicked.addListener(function(id, buttonIndex) { + chrome.test.succeed(); + }); + + // Create a notification, so there will be one existing notification. + createNotification(id1, content) + .catch(function() { failTest("notifications.create"); }) + // Try to notify that a non-existent notification button was clicked. + .then(function() { return notifyOnButtonClicked(myId, "doesNotExist", 0)}) + .then(function() { failTest("NotifyOnButtonClicked"); }) + // Notify the sender that a notificaion button was clicked. + .catch(function() { return notifyOnButtonClicked(myId, returnId, 0); }) + .catch(function() { failTest("NotifyOnButtonClicked"); }); +} + +function testNotifyOnClosed() { + chrome.notifications.onClosed.addListener(function(id, byUser) { + chrome.test.succeed(); + }); + + // Create a notification, so there will be one existing notification. + createNotification(id1, content) + .catch(function() { failTest("notifications.create"); }) + // Try to notify that an non-existent notification was cleared. + .then(function () { return notifyOnCleared(myId, "doesNotExist"); }) + .then(function() { failTest("NotifyOnCleared"); }) + // Notify that the original notification was cleared. + .catch(function() { return notifyOnCleared(myId, returnId); }) + .catch(function() { failTest("NotifyOnCleared"); }); +} + +function testNotifyOnShowSettings() { + chrome.notifications.onShowSettings.addListener(function() { + 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 non existent sender that a user checked its settings. + .then(function () { return notifyOnShowSettings("DoesNotExist", + "application"); }) + // Fail if it returns true. + .then(function() { failTest("NotifyOnShowSettings"); }) + // Notify current notifier that a user checked its settings. + .catch(function () { return notifyOnShowSettings(myId, "application"); }) + .catch(function() { failTest("NotifyOnShowSettings"); }) +} -chrome.test.runTests([ testFunctions ]); +chrome.test.runTests([ testNotifyOnClicked, + testNotifyOnButtonClicked, + testNotifyOnClosed, + testNotifyOnShowSettings ]);
\ No newline at end of file |