Skip to content

Commit

Permalink
Add NotifyOnShowSettings implementation of notification
Browse files Browse the repository at this point in the history
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

Cr-Commit-Position: refs/heads/master@{#289823}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@289823 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
liyanhou@chromium.org committed Aug 15, 2014
1 parent 2258782 commit 728d945
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)));
Expand All @@ -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::
Expand Down
27 changes: 22 additions & 5 deletions chrome/common/extensions/api/notification_provider.idl
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);

Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
});
});
Expand All @@ -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 ]);

0 comments on commit 728d945

Please sign in to comment.