Bug 1205399 - Backend for disabling of notifications for a site from the UI. r=nsm

This commit is contained in:
Matthew Noorenberghe 2015-09-25 13:18:28 -07:00
parent eeda2d29b5
commit 5987eb9e33
2 changed files with 61 additions and 12 deletions

View File

@ -653,17 +653,22 @@ NotificationPermissionRequest::GetTypes(nsIArray** aTypes)
aTypes);
}
class NotificationObserver : public nsIObserver
// Observer that the alert service calls to do common tasks and/or dispatch to the
// specific observer for the context e.g. main thread, worker, or service worker.
class NotificationObserver final : public nsIObserver
{
public:
UniquePtr<NotificationRef> mNotificationRef;
nsCOMPtr<nsIObserver> mObserver;
nsCOMPtr<nsIPrincipal> mPrincipal;
NS_DECL_ISUPPORTS
NS_DECL_NSIOBSERVER
explicit NotificationObserver(UniquePtr<NotificationRef> aRef)
: mNotificationRef(Move(aRef))
NotificationObserver(nsIObserver* aObserver, nsIPrincipal* aPrincipal)
: mObserver(aObserver), mPrincipal(aPrincipal)
{
AssertIsOnMainThread();
MOZ_ASSERT(mObserver);
MOZ_ASSERT(mPrincipal);
}
protected:
@ -675,6 +680,28 @@ protected:
NS_IMPL_ISUPPORTS(NotificationObserver, nsIObserver)
class MainThreadNotificationObserver : public nsIObserver
{
public:
UniquePtr<NotificationRef> mNotificationRef;
NS_DECL_ISUPPORTS
NS_DECL_NSIOBSERVER
explicit MainThreadNotificationObserver(UniquePtr<NotificationRef> aRef)
: mNotificationRef(Move(aRef))
{
AssertIsOnMainThread();
}
protected:
virtual ~MainThreadNotificationObserver()
{
AssertIsOnMainThread();
}
};
NS_IMPL_ISUPPORTS(MainThreadNotificationObserver, nsIObserver)
NS_IMETHODIMP
NotificationTask::Run()
{
@ -984,14 +1011,14 @@ Notification::GetPrincipal()
}
}
class WorkerNotificationObserver final : public NotificationObserver
class WorkerNotificationObserver final : public MainThreadNotificationObserver
{
public:
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_NSIOBSERVER
explicit WorkerNotificationObserver(UniquePtr<NotificationRef> aRef)
: NotificationObserver(Move(aRef))
: MainThreadNotificationObserver(Move(aRef))
{
AssertIsOnMainThread();
MOZ_ASSERT(mNotificationRef->GetNotification()->mWorkerPrivate);
@ -1017,7 +1044,7 @@ protected:
}
};
NS_IMPL_ISUPPORTS_INHERITED0(WorkerNotificationObserver, NotificationObserver)
NS_IMPL_ISUPPORTS_INHERITED0(WorkerNotificationObserver, MainThreadNotificationObserver)
class ServiceWorkerNotificationObserver final : public nsIObserver
{
@ -1125,6 +1152,25 @@ NotificationObserver::Observe(nsISupports* aSubject, const char* aTopic,
const char16_t* aData)
{
AssertIsOnMainThread();
if (!strcmp("alertdisablecallback", aTopic)) {
nsCOMPtr<nsIPermissionManager> permissionManager =
mozilla::services::GetPermissionManager();
if (!permissionManager) {
return NS_ERROR_FAILURE;
}
permissionManager->RemoveFromPrincipal(mPrincipal, "desktop-notification");
return NS_OK;
}
return mObserver->Observe(aSubject, aTopic, aData);
}
NS_IMETHODIMP
MainThreadNotificationObserver::Observe(nsISupports* aSubject, const char* aTopic,
const char16_t* aData)
{
AssertIsOnMainThread();
MOZ_ASSERT(mNotificationRef);
Notification* notification = mNotificationRef->GetNotification();
MOZ_ASSERT(notification);
@ -1384,16 +1430,19 @@ Notification::ShowInternal()
mObserver = new WorkerNotificationObserver(Move(ownership));
observer = mObserver;
} else {
observer = new NotificationObserver(Move(ownership));
observer = new MainThreadNotificationObserver(Move(ownership));
}
} else {
// This observer does not care about the Notification. It will be released
// at the end of this function.
//
// The observer is wholly owned by the alerts service.
// The observer is wholly owned by the NotificationObserver passed to the alert service.
observer = new ServiceWorkerNotificationObserver(mScope, GetPrincipal(), mID);
}
MOZ_ASSERT(observer);
nsCOMPtr<nsIObserver> alertObserver = new NotificationObserver(observer,
GetPrincipal());
#ifdef MOZ_B2G
nsCOMPtr<nsIAppNotificationService> appNotifier =
@ -1432,7 +1481,7 @@ Notification::ShowInternal()
}
appNotifier->ShowAppNotification(iconUrl, mTitle, mBody,
observer, val);
alertObserver, val);
return;
}
}
@ -1463,7 +1512,7 @@ Notification::ShowInternal()
nsAutoString alertName;
GetAlertName(alertName);
alertService->ShowAlertNotification(iconUrl, mTitle, mBody, true,
uniqueCookie, observer, alertName,
uniqueCookie, alertObserver, alertName,
DirectionToString(mDir), mLang,
mDataAsBase64, GetPrincipal(),
inPrivateBrowsing);

View File

@ -102,7 +102,7 @@ class Notification : public DOMEventTargetHelper
friend class CloseNotificationRunnable;
friend class NotificationTask;
friend class NotificationPermissionRequest;
friend class NotificationObserver;
friend class MainThreadNotificationObserver;
friend class NotificationStorageCallback;
friend class ServiceWorkerNotificationObserver;
friend class WorkerGetRunnable;