Commit b020dc10 authored by asargent@chromium.org's avatar asargent@chromium.org

Add menu option for disabling app notifications

This should only show up for apps which have the appNotifications permission
in their manifest.

BUG=106268
TEST=Install an app with the appNotifications permission - the icon on the
NTP should get a 'Disable Notifications' context menu entry which if checked
will keep the notification bubbles from appearing over the app's icon.

Review URL: http://codereview.chromium.org/8785016

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@113111 0039d316-1c4b-4281-b951-d872f2087c98
parent c1c758e1
...@@ -1442,6 +1442,11 @@ void ExtensionService::ProcessExtensionSyncData( ...@@ -1442,6 +1442,11 @@ void ExtensionService::ProcessExtensionSyncData(
bundle.pending_sync_data[extension_sync_data.id()] = extension_sync_data; bundle.pending_sync_data[extension_sync_data.id()] = extension_sync_data;
CheckForUpdatesSoon(); CheckForUpdatesSoon();
} }
if (extension_sync_data.notifications_disabled() !=
extension_prefs_->IsAppNotificationDisabled(id)) {
extension_prefs_->SetAppNotificationDisabled(
id, extension_sync_data.notifications_disabled());
}
} else { } else {
// TODO(akalin): Replace silent update with a list of enabled // TODO(akalin): Replace silent update with a list of enabled
// permissions. // permissions.
......
...@@ -63,9 +63,13 @@ cr.define('ntp4', function() { ...@@ -63,9 +63,13 @@ cr.define('ntp4', function() {
menu.appendChild(cr.ui.MenuItem.createSeparator()); menu.appendChild(cr.ui.MenuItem.createSeparator());
this.options_ = this.appendMenuItem_('appoptions'); this.options_ = this.appendMenuItem_('appoptions');
this.disableNotifications_ =
this.appendMenuItem_('appdisablenotifications');
this.uninstall_ = this.appendMenuItem_('appuninstall'); this.uninstall_ = this.appendMenuItem_('appuninstall');
this.options_.addEventListener('activate', this.options_.addEventListener('activate',
this.onShowOptions_.bind(this)); this.onShowOptions_.bind(this));
this.disableNotifications_.addEventListener(
'activate', this.onDisableNotifications_.bind(this));
this.uninstall_.addEventListener('activate', this.uninstall_.addEventListener('activate',
this.onUninstall_.bind(this)); this.onUninstall_.bind(this));
...@@ -115,7 +119,7 @@ cr.define('ntp4', function() { ...@@ -115,7 +119,7 @@ cr.define('ntp4', function() {
}, },
/** /**
* Does all the necessary setup to show the menu for the give app. * Does all the necessary setup to show the menu for the given app.
* @param {App} app The App object that will be showing a context menu. * @param {App} app The App object that will be showing a context menu.
*/ */
setupForApp: function(app) { setupForApp: function(app) {
...@@ -130,6 +134,13 @@ cr.define('ntp4', function() { ...@@ -130,6 +134,13 @@ cr.define('ntp4', function() {
this.options_.disabled = !app.appData.options_url || !app.appData.enabled; this.options_.disabled = !app.appData.options_url || !app.appData.enabled;
this.uninstall_.disabled = !app.appData.can_uninstall; this.uninstall_.disabled = !app.appData.can_uninstall;
this.disableNotifications_.hidden = true;
var notificationsDisabled = app.appData.notifications_disabled;
if (typeof notificationsDisabled != 'undefined') {
this.disableNotifications_.hidden = false;
this.disableNotifications_.checked = notificationsDisabled;
}
}, },
/** /**
...@@ -155,6 +166,14 @@ cr.define('ntp4', function() { ...@@ -155,6 +166,14 @@ cr.define('ntp4', function() {
onShowOptions_: function(e) { onShowOptions_: function(e) {
window.location = this.app_.appData.options_url; window.location = this.app_.appData.options_url;
}, },
onDisableNotifications_: function(e) {
var app = this.app_;
app.removeBubble();
// Toggle the current disable setting.
var newSetting = !this.disableNotifications_.checked;
app.appData.notifications_disabled = newSetting;
chrome.send('setNotificationsDisabled', [app.appData.id, newSetting]);
},
onUninstall_: function(e) { onUninstall_: function(e) {
chrome.send('uninstallApp', [this.app_.appData.id]); chrome.send('uninstallApp', [this.app_.appData.id]);
}, },
...@@ -222,7 +241,8 @@ cr.define('ntp4', function() { ...@@ -222,7 +241,8 @@ cr.define('ntp4', function() {
var notification = this.appData_.notification; var notification = this.appData_.notification;
var hasNotification = typeof notification != 'undefined' && var hasNotification = typeof notification != 'undefined' &&
typeof notification['title'] != 'undefined' && typeof notification['title'] != 'undefined' &&
typeof notification['body'] != 'undefined'; typeof notification['body'] != 'undefined' &&
!this.appData_.notifications_disabled;
if (hasNotification) if (hasNotification)
this.setupNotification_(notification); this.setupNotification_(notification);
...@@ -355,6 +375,16 @@ cr.define('ntp4', function() { ...@@ -355,6 +375,16 @@ cr.define('ntp4', function() {
} }
}, },
/**
* Removes the info bubble if there is one.
*/
removeBubble: function() {
if (this.currentBubbleShowing_) {
this.currentBubbleShowing_.hide();
this.currentBubbleShowing_ = null;
}
},
/** /**
* Creates the apps-promo section of the app (should only be called for the * Creates the apps-promo section of the app (should only be called for the
* webstore app). * webstore app).
...@@ -784,7 +814,8 @@ cr.define('ntp4', function() { ...@@ -784,7 +814,8 @@ cr.define('ntp4', function() {
function appNotificationChanged(id, notification) { function appNotificationChanged(id, notification) {
var app = $(id); var app = $(id);
if (app) // The app might have been uninstalled. // The app might have been uninstalled, or notifications might be disabled.
if (app && !app.appData.notifications_disabled)
app.setupNotification_(notification); app.setupNotification_(notification);
}; };
......
...@@ -149,6 +149,12 @@ void AppLauncherHandler::CreateAppInfo(const Extension* extension, ...@@ -149,6 +149,12 @@ void AppLauncherHandler::CreateAppInfo(const Extension* extension,
value->SetBoolean("is_webstore", value->SetBoolean("is_webstore",
extension->id() == extension_misc::kWebStoreAppId); extension->id() == extension_misc::kWebStoreAppId);
if (extension->HasAPIPermission(ExtensionAPIPermission::kAppNotifications)) {
ExtensionPrefs* prefs = service->extension_prefs();
value->SetBoolean("notifications_disabled",
prefs->IsAppNotificationDisabled(extension->id()));
}
if (notification) if (notification)
value->Set("notification", SerializeNotification(*notification)); value->Set("notification", SerializeNotification(*notification));
...@@ -221,6 +227,9 @@ void AppLauncherHandler::RegisterMessages() { ...@@ -221,6 +227,9 @@ void AppLauncherHandler::RegisterMessages() {
web_ui_->RegisterMessageCallback("closeNotification", web_ui_->RegisterMessageCallback("closeNotification",
base::Bind(&AppLauncherHandler::HandleNotificationClose, base::Bind(&AppLauncherHandler::HandleNotificationClose,
base::Unretained(this))); base::Unretained(this)));
web_ui_->RegisterMessageCallback("setNotificationsDisabled",
base::Bind(&AppLauncherHandler::HandleSetNotificationsDisabled,
base::Unretained(this)));
} }
void AppLauncherHandler::Observe(int type, void AppLauncherHandler::Observe(int type,
...@@ -238,10 +247,6 @@ void AppLauncherHandler::Observe(int type, ...@@ -238,10 +247,6 @@ void AppLauncherHandler::Observe(int type,
switch (type) { switch (type) {
case chrome::NOTIFICATION_APP_NOTIFICATION_STATE_CHANGED: { case chrome::NOTIFICATION_APP_NOTIFICATION_STATE_CHANGED: {
Profile* profile = content::Source<Profile>(source).ptr();
if (!Profile::FromWebUI(web_ui_)->IsSameProfile(profile))
return;
const std::string& id = const std::string& id =
*content::Details<const std::string>(details).ptr(); *content::Details<const std::string>(details).ptr();
const AppNotification* notification = const AppNotification* notification =
...@@ -468,7 +473,7 @@ void AppLauncherHandler::HandleGetApps(const ListValue* args) { ...@@ -468,7 +473,7 @@ void AppLauncherHandler::HandleGetApps(const ListValue* args) {
pref_change_registrar_.Add(prefs::kNTPAppPageNames, this); pref_change_registrar_.Add(prefs::kNTPAppPageNames, this);
registrar_.Add(this, chrome::NOTIFICATION_APP_NOTIFICATION_STATE_CHANGED, registrar_.Add(this, chrome::NOTIFICATION_APP_NOTIFICATION_STATE_CHANGED,
content::NotificationService::AllSources()); content::Source<Profile>(profile));
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
content::Source<Profile>(profile)); content::Source<Profile>(profile));
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
...@@ -741,6 +746,20 @@ void AppLauncherHandler::HandleNotificationClose(const ListValue* args) { ...@@ -741,6 +746,20 @@ void AppLauncherHandler::HandleNotificationClose(const ListValue* args) {
notification_manager->ClearAll(extension_id); notification_manager->ClearAll(extension_id);
} }
void AppLauncherHandler::HandleSetNotificationsDisabled(
const ListValue* args) {
std::string extension_id;
bool disabled = false;
CHECK(args->GetString(0, &extension_id));
CHECK(args->GetBoolean(1, &disabled));
const Extension* extension = extension_service_->GetExtensionById(
extension_id, true);
if (!extension)
return;
extension_service_->SetAppNotificationDisabled(extension_id, disabled);
}
void AppLauncherHandler::OnFaviconForApp(FaviconService::Handle handle, void AppLauncherHandler::OnFaviconForApp(FaviconService::Handle handle,
history::FaviconData data) { history::FaviconData data) {
scoped_ptr<AppInstallInfo> install_info( scoped_ptr<AppInstallInfo> install_info(
......
...@@ -106,6 +106,9 @@ class AppLauncherHandler : public WebUIMessageHandler, ...@@ -106,6 +106,9 @@ class AppLauncherHandler : public WebUIMessageHandler,
// Callback for "closeNotification" message. // Callback for "closeNotification" message.
void HandleNotificationClose(const base::ListValue* args); void HandleNotificationClose(const base::ListValue* args);
// Callback for "setNotificationsDisabled" message.
void HandleSetNotificationsDisabled(const base::ListValue* args);
// Register app launcher preferences. // Register app launcher preferences.
static void RegisterUserPrefs(PrefService* pref_service); static void RegisterUserPrefs(PrefService* pref_service);
......
...@@ -316,6 +316,8 @@ void NTPResourceCache::CreateNewTabHTML() { ...@@ -316,6 +316,8 @@ void NTPResourceCache::CreateNewTabHTML() {
l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME))); l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME)));
localized_strings.SetString("appoptions", localized_strings.SetString("appoptions",
l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_OPTIONS)); l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_OPTIONS));
localized_strings.SetString("appdisablenotifications",
l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_DISABLE_NOTIFICATIONS));
localized_strings.SetString("appcreateshortcut", localized_strings.SetString("appcreateshortcut",
l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_CREATE_SHORTCUT)); l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_CREATE_SHORTCUT));
localized_strings.SetString("appDefaultPageName", localized_strings.SetString("appDefaultPageName",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment