Bug 1419771 - Introduce DOMPrefs, a thread-safe access to preferences for DOM - part 6 - Notification API enabled, r=asuth

This commit is contained in:
Andrea Marchesini 2018-01-08 14:05:04 +01:00
Родитель 61778fdf43
Коммит 8fada45fa4
7 изменённых файлов: 26 добавлений и 36 удалений

Просмотреть файл

@ -38,6 +38,8 @@ PREF(ImageBitmapExtensionsEnabled, "canvas.imagebitmap_extensions.enabled")
PREF(DOMCachesEnabled, "dom.caches.enabled")
PREF(DOMCachesTestingEnabled, "dom.caches.testing.enabled")
PREF(PerformanceLoggingEnabled, "dom.performance.enable_user_timing_logging")
PREF(NotificationEnabled, "dom.webnotifications.enabled")
PREF(NotificationEnabledInServiceWorkers, "dom.webnotifications.serviceworker.enabled")
#undef PREF
@ -50,6 +52,7 @@ PREF(PerformanceLoggingEnabled, "dom.performance.enable_user_timing_logging")
PREF_WEBIDL(ImageBitmapExtensionsEnabled)
PREF_WEBIDL(DOMCachesEnabled)
PREF_WEBIDL(NotificationEnabledInServiceWorkers)
#undef PREF_WEBIDL

Просмотреть файл

@ -29,6 +29,16 @@ public:
// Returns true if the dom.performance.enable_user_timing_logging pref is set.
static bool PerformanceLoggingEnabled();
// Returns true if the dom.webnotifications.enabled pref is set.
// Note that you should use NotificationEnabledInServiceWorkers if you need to
// enable Notification API for ServiceWorkers
static bool NotificationEnabled();
// Returns true if the dom.webnotifications.serviceworker.enabled pref is set.
static bool NotificationEnabledInServiceWorkers();
static bool NotificationEnabledInServiceWorkers(JSContext* aCx,
JSObject* aObj);
};
} // dom namespace

Просмотреть файл

@ -19,6 +19,7 @@
#include "mozilla/dom/AppNotificationServiceOptionsBinding.h"
#include "mozilla/dom/BindingUtils.h"
#include "mozilla/dom/ContentChild.h"
#include "mozilla/dom/DOMPrefs.h"
#include "mozilla/dom/NotificationEvent.h"
#include "mozilla/dom/PermissionMessageUtils.h"
#include "mozilla/dom/Promise.h"
@ -912,20 +913,18 @@ Notification::RequireInteractionEnabled(JSContext* aCx, JSObject* aOjb)
bool
Notification::PrefEnabled(JSContext* aCx, JSObject* aObj)
{
if (NS_IsMainThread()) {
return Preferences::GetBool("dom.webnotifications.enabled", false);
if (!NS_IsMainThread()) {
WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx);
if (!workerPrivate) {
return false;
}
if (workerPrivate->IsServiceWorker()) {
return DOMPrefs::NotificationEnabledInServiceWorkers();
}
}
WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx);
if (!workerPrivate) {
return false;
}
if (workerPrivate->IsServiceWorker()) {
return workerPrivate->DOMServiceWorkerNotificationEnabled();
}
return workerPrivate->DOMWorkerNotificationEnabled();
return DOMPrefs::NotificationEnabled();
}
// static

Просмотреть файл

@ -44,8 +44,8 @@ partial interface ServiceWorkerRegistration {
// https://notifications.spec.whatwg.org/
partial interface ServiceWorkerRegistration {
[Throws, Func="mozilla::dom::ServiceWorkerRegistration::NotificationAPIVisible"]
[Throws, Func="mozilla::dom::DOMPrefs::NotificationEnabledInServiceWorkers"]
Promise<void> showNotification(DOMString title, optional NotificationOptions options);
[Throws, Func="mozilla::dom::ServiceWorkerRegistration::NotificationAPIVisible"]
[Throws, Func="mozilla::dom::DOMPrefs::NotificationEnabledInServiceWorkers"]
Promise<sequence<Notification>> getNotifications(optional GetNotificationOptions filter);
};

Просмотреть файл

@ -54,22 +54,6 @@ ServiceWorkerRegistration::Visible(JSContext* aCx, JSObject* aObj)
return workerPrivate->ServiceWorkersEnabled();
}
/* static */ bool
ServiceWorkerRegistration::NotificationAPIVisible(JSContext* aCx, JSObject* aObj)
{
if (NS_IsMainThread()) {
return Preferences::GetBool("dom.webnotifications.serviceworker.enabled", false);
}
// Otherwise check the pref via the work private helper
WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx);
if (!workerPrivate) {
return false;
}
return workerPrivate->DOMServiceWorkerNotificationEnabled();
}
////////////////////////////////////////////////////
// Main Thread implementation

Просмотреть файл

@ -64,10 +64,6 @@ public:
static bool
Visible(JSContext* aCx, JSObject* aObj);
static bool
NotificationAPIVisible(JSContext* aCx, JSObject* aObj);
static already_AddRefed<ServiceWorkerRegistration>
CreateForMainThread(nsPIDOMWindowInner* aWindow,
const nsAString& aScope);

Просмотреть файл

@ -21,8 +21,6 @@
// * First argument is the name of the pref.
// * The name of the function that updates the new value of a pref.
WORKER_SIMPLE_PREF("dom.webnotifications.enabled", DOMWorkerNotificationEnabled, DOM_WORKERNOTIFICATION)
WORKER_SIMPLE_PREF("dom.webnotifications.serviceworker.enabled", DOMServiceWorkerNotificationEnabled, DOM_SERVICEWORKERNOTIFICATION)
WORKER_SIMPLE_PREF("dom.webnotifications.requireinteraction.enabled", DOMWorkerNotificationRIEnabled, DOM_WORKERNOTIFICATIONRI)
WORKER_SIMPLE_PREF("dom.serviceWorkers.enabled", ServiceWorkersEnabled, SERVICEWORKERS_ENABLED)
WORKER_SIMPLE_PREF("dom.serviceWorkers.testing.enabled", ServiceWorkersTestingEnabled, SERVICEWORKERS_TESTING_ENABLED)