From 1677aaeb9ac3a13de8a70579b701b3c2fc218f2c Mon Sep 17 00:00:00 2001 From: William Chen Date: Mon, 16 Nov 2015 13:20:15 -0800 Subject: [PATCH] Bug 1210211 - Part 2: Notify Push service of visible notifications. r=baku --HG-- extra : commitid : 7lTQ80hfQU6 extra : rebase_source : f31c7dd4c3dc9f9d4bb492791fd59c413268d22d --- .../push/nsIPushNotificationService.idl | 18 +++++++++++++++ dom/notification/Notification.cpp | 22 ++++++++++++++++++ dom/push/PushNotificationService.js | 23 ++++++++++++++++++- 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/dom/interfaces/push/nsIPushNotificationService.idl b/dom/interfaces/push/nsIPushNotificationService.idl index da947aaf1b7f97..0ce0ecea4e4072 100644 --- a/dom/interfaces/push/nsIPushNotificationService.idl +++ b/dom/interfaces/push/nsIPushNotificationService.idl @@ -57,3 +57,21 @@ interface nsIPushNotificationService : nsISupports */ jsval clearForDomain(in string domain); }; + +[scriptable, uuid(a2555e70-46f8-4b52-bf02-d978b979d143)] +interface nsIPushQuotaManager : nsISupports +{ + /** + * Informs the quota manager that a notification + * for the given origin has been shown. Used to + * determine if push quota should be relaxed. + */ + void notificationForOriginShown(in string origin); + + /** + * Informs the quota manager that a notification + * for the given origin has been closed. Used to + * determine if push quota should be relaxed. + */ + void notificationForOriginClosed(in string origin); +}; diff --git a/dom/notification/Notification.cpp b/dom/notification/Notification.cpp index 8d8b35e1da20d5..5cbc6d9e45ff6e 100644 --- a/dom/notification/Notification.cpp +++ b/dom/notification/Notification.cpp @@ -32,6 +32,7 @@ #include "nsILoadContext.h" #include "nsINotificationStorage.h" #include "nsIPermissionManager.h" +#include "nsIPushNotificationService.h" #include "nsIScriptSecurityManager.h" #include "nsIServiceWorkerManager.h" #include "nsIUUIDGenerator.h" @@ -1173,6 +1174,27 @@ NotificationObserver::Observe(nsISupports* aSubject, const char* aTopic, ContentChild::GetSingleton()->SendOpenNotificationSettings( IPC::Principal(mPrincipal)); return NS_OK; + } else if (!strcmp("alertshow", aTopic) || + !strcmp("alertfinished", aTopic)) { + nsCOMPtr pushQuotaManager = + do_GetService("@mozilla.org/push/NotificationService;1"); + if (pushQuotaManager) { + nsAutoCString origin; + nsresult rv = mPrincipal->GetOrigin(origin); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + + if (!strcmp("alertshow", aTopic)) { + rv = pushQuotaManager->NotificationForOriginShown(origin.get()); + } else { + rv = pushQuotaManager->NotificationForOriginClosed(origin.get()); + } + + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + } } return mObserver->Observe(aSubject, aTopic, aData); diff --git a/dom/push/PushNotificationService.js b/dom/push/PushNotificationService.js index 3ab03c88c4ec03..ae946a625bc52b 100644 --- a/dom/push/PushNotificationService.js +++ b/dom/push/PushNotificationService.js @@ -36,7 +36,8 @@ PushNotificationService.prototype = { _xpcom_factory: XPCOMUtils.generateSingletonFactory(PushNotificationService), QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference, - Ci.nsIPushNotificationService]), + Ci.nsIPushNotificationService, + Ci.nsIPushQuotaManager,]), register: function register(scope, originAttributes) { return PushService.register({ @@ -74,6 +75,26 @@ PushNotificationService.prototype = { } break; } + }, + + // nsIPushQuotaManager methods + + notificationForOriginShown: function(origin) { + if (!isParent) { + Services.cpmm.sendAsyncMessage("Push:NotificationForOriginShown", origin); + return; + } + + PushService._notificationForOriginShown(origin); + }, + + notificationForOriginClosed: function(origin) { + if (!isParent) { + Services.cpmm.sendAsyncMessage("Push:NotificationForOriginClosed", origin); + return; + } + + PushService._notificationForOriginClosed(origin); } };