From 6e916540f5b9ed5afd743ffd99026e220947accf Mon Sep 17 00:00:00 2001 From: Ehsan Akhgari Date: Mon, 27 Jul 2015 15:35:28 -0400 Subject: [PATCH] Bug 1188091 - Fix the exposure of Push interfaces; r=dougt,bzbarsky Currently we don't check the dom.push.enabled pref in some cases for some of these interfaces. This patch unifies how all of these interfaces are exposed to Window, Worker, and ServiceWorker. --- dom/base/nsContentUtils.cpp | 20 +++++++++++++++++++ dom/base/nsContentUtils.h | 2 ++ dom/push/PushManager.cpp | 17 ---------------- dom/push/PushManager.h | 3 --- dom/webidl/PushEvent.webidl | 6 ++++-- dom/webidl/PushManager.webidl | 2 +- dom/webidl/PushMessageData.webidl | 3 ++- dom/webidl/PushSubscription.webidl | 2 +- dom/webidl/ServiceWorkerRegistration.webidl | 2 +- dom/workers/RuntimeService.cpp | 10 ++++++++++ dom/workers/ServiceWorkerEvents.h | 1 + dom/workers/WorkerPrivate.h | 7 +++++++ dom/workers/Workers.h | 1 + .../test_serviceworker_interfaces.js | 10 ++++++---- dom/workers/test/test_worker_interfaces.js | 4 ++++ 15 files changed, 60 insertions(+), 30 deletions(-) diff --git a/dom/base/nsContentUtils.cpp b/dom/base/nsContentUtils.cpp index 04f5c7babd3a..cf87ca4e4be8 100644 --- a/dom/base/nsContentUtils.cpp +++ b/dom/base/nsContentUtils.cpp @@ -51,6 +51,7 @@ #include "mozilla/dom/TextDecoder.h" #include "mozilla/dom/TouchEvent.h" #include "mozilla/dom/ShadowRoot.h" +#include "mozilla/dom/WorkerPrivate.h" #include "mozilla/EventDispatcher.h" #include "mozilla/EventListenerManager.h" #include "mozilla/EventStateManager.h" @@ -7973,3 +7974,22 @@ nsContentUtils::SetFetchReferrerURIWithPolicy(nsIPrincipal* aPrincipal, net::ReferrerPolicy referrerPolicy = aDoc->GetReferrerPolicy(); return aChannel->SetReferrerWithPolicy(referrerURI, referrerPolicy); } + +// static +bool +nsContentUtils::PushEnabled(JSContext* aCx, JSObject* aObj) +{ + if (NS_IsMainThread()) { + return Preferences::GetBool("dom.push.enabled", false); + } + + using namespace workers; + + // Otherwise, check the pref via the WorkerPrivate + WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx); + if (!workerPrivate) { + return false; + } + + return workerPrivate->PushEnabled(); +} diff --git a/dom/base/nsContentUtils.h b/dom/base/nsContentUtils.h index 3215dcefe92f..a17fb7bfb7c0 100644 --- a/dom/base/nsContentUtils.h +++ b/dom/base/nsContentUtils.h @@ -2439,6 +2439,8 @@ public: nsIDocument* aDoc, nsIHttpChannel* aChannel); + static bool PushEnabled(JSContext* aCx, JSObject* aObj); + private: static bool InitializeEventTable(); diff --git a/dom/push/PushManager.cpp b/dom/push/PushManager.cpp index 33d96b24765d..df767b07641e 100644 --- a/dom/push/PushManager.cpp +++ b/dom/push/PushManager.cpp @@ -129,23 +129,6 @@ NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PushSubscription) NS_INTERFACE_MAP_ENTRY(nsISupports) NS_INTERFACE_MAP_END -// PushManager -// static -bool -PushManager::Enabled(JSContext* aCx, JSObject* aObj) -{ - if (NS_IsMainThread()) { - return Preferences::GetBool("dom.push.enabled", false); - } - - // XXXnsm: As of this patch it seems like Push will be enabled before or with - // ServiceWorkers, so this seems OK for now. - ServiceWorkerGlobalScope* scope = nullptr; - nsresult rv = UnwrapObject(aObj, scope); - return NS_SUCCEEDED(rv); -} - PushManager::PushManager(nsIGlobalObject* aGlobal, const nsAString& aScope) : mGlobal(aGlobal), mScope(aScope) { diff --git a/dom/push/PushManager.h b/dom/push/PushManager.h index 5e92314aef94..9e569704758e 100644 --- a/dom/push/PushManager.h +++ b/dom/push/PushManager.h @@ -106,9 +106,6 @@ public: NS_DECL_CYCLE_COLLECTING_ISUPPORTS NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(PushManager) - static bool - Enabled(JSContext* aCx, JSObject* aObj); - explicit PushManager(nsIGlobalObject* aGlobal, const nsAString& aScope); nsIGlobalObject* diff --git a/dom/webidl/PushEvent.webidl b/dom/webidl/PushEvent.webidl index 806b786a59df..a14af42a7c6d 100644 --- a/dom/webidl/PushEvent.webidl +++ b/dom/webidl/PushEvent.webidl @@ -7,7 +7,9 @@ * https://w3c.github.io/push-api/ */ -[Constructor(DOMString type, optional PushEventInit eventInitDict), Exposed=ServiceWorker] +[Constructor(DOMString type, optional PushEventInit eventInitDict), + Func="nsContentUtils::PushEnabled", + Exposed=ServiceWorker] interface PushEvent : ExtendableEvent { readonly attribute PushMessageData data; }; @@ -16,4 +18,4 @@ typedef USVString PushMessageDataInit; dictionary PushEventInit : ExtendableEventInit { PushMessageDataInit data; -}; \ No newline at end of file +}; diff --git a/dom/webidl/PushManager.webidl b/dom/webidl/PushManager.webidl index a1ae9d4fdf36..a6658e726604 100644 --- a/dom/webidl/PushManager.webidl +++ b/dom/webidl/PushManager.webidl @@ -22,7 +22,7 @@ interface PushManagerImpl { [Func="ServiceWorkerRegistration::WebPushMethodHider"] void setScope(DOMString scope); }; -[Exposed=(Window,Worker), Func="mozilla::dom::PushManager::Enabled"] +[Exposed=(Window,Worker), Func="nsContentUtils::PushEnabled"] interface PushManager { [ChromeOnly, Throws, Exposed=Window] void setPushManagerImpl(PushManagerImpl store); diff --git a/dom/webidl/PushMessageData.webidl b/dom/webidl/PushMessageData.webidl index c2fd12aaa313..f2604a1a0e87 100644 --- a/dom/webidl/PushMessageData.webidl +++ b/dom/webidl/PushMessageData.webidl @@ -7,7 +7,8 @@ * https://w3c.github.io/push-api/ */ -[Exposed=ServiceWorker] +[Func="nsContentUtils::PushEnabled", + Exposed=ServiceWorker] interface PushMessageData { // FIXME(nsm): Bug 1149195. diff --git a/dom/webidl/PushSubscription.webidl b/dom/webidl/PushSubscription.webidl index 37b7776234e3..e8efdaa90ccc 100644 --- a/dom/webidl/PushSubscription.webidl +++ b/dom/webidl/PushSubscription.webidl @@ -9,7 +9,7 @@ interface Principal; -[Exposed=(Window,Worker), Func="mozilla::dom::PushManager::Enabled", +[Exposed=(Window,Worker), Func="nsContentUtils::PushEnabled", ChromeConstructor(DOMString pushEndpoint, DOMString scope)] interface PushSubscription { diff --git a/dom/webidl/ServiceWorkerRegistration.webidl b/dom/webidl/ServiceWorkerRegistration.webidl index 2f220a8eaaab..c82f217147bf 100644 --- a/dom/webidl/ServiceWorkerRegistration.webidl +++ b/dom/webidl/ServiceWorkerRegistration.webidl @@ -28,7 +28,7 @@ interface ServiceWorkerRegistration : EventTarget { partial interface ServiceWorkerRegistration { #ifndef MOZ_SIMPLEPUSH - [Throws, Exposed=(Window,Worker), Func="mozilla::dom::PushManager::Enabled"] + [Throws, Exposed=(Window,Worker), Func="nsContentUtils::PushEnabled"] readonly attribute PushManager pushManager; #endif }; diff --git a/dom/workers/RuntimeService.cpp b/dom/workers/RuntimeService.cpp index 1ef6a701edff..de79b37ed2f3 100644 --- a/dom/workers/RuntimeService.cpp +++ b/dom/workers/RuntimeService.cpp @@ -165,6 +165,7 @@ static_assert(MAX_WORKERS_PER_DOMAIN >= 1, #define PREF_SERVICEWORKERS_TESTING_ENABLED "dom.serviceWorkers.testing.enabled" #define PREF_INTERCEPTION_ENABLED "dom.serviceWorkers.interception.enabled" #define PREF_INTERCEPTION_OPAQUE_ENABLED "dom.serviceWorkers.interception.opaque.enabled" +#define PREF_PUSH_ENABLED "dom.push.enabled" namespace { @@ -1944,6 +1945,10 @@ RuntimeService::Init() WorkerPrefChanged, PREF_SERVICEWORKERS_TESTING_ENABLED, reinterpret_cast(WORKERPREF_SERVICEWORKERS_TESTING))) || + NS_FAILED(Preferences::RegisterCallbackAndCall( + WorkerPrefChanged, + PREF_PUSH_ENABLED, + reinterpret_cast(WORKERPREF_PUSH))) || NS_FAILED(Preferences::RegisterCallback(LoadRuntimeOptions, PREF_JS_OPTIONS_PREFIX, nullptr)) || @@ -2167,6 +2172,10 @@ RuntimeService::Cleanup() WorkerPrefChanged, PREF_DOM_WORKERNOTIFICATION_ENABLED, reinterpret_cast(WORKERPREF_DOM_WORKERNOTIFICATION))) || + NS_FAILED(Preferences::UnregisterCallback( + WorkerPrefChanged, + PREF_PUSH_ENABLED, + reinterpret_cast(WORKERPREF_PUSH))) || #if DUMP_CONTROLLED_BY_PREF NS_FAILED(Preferences::UnregisterCallback( WorkerPrefChanged, @@ -2715,6 +2724,7 @@ RuntimeService::WorkerPrefChanged(const char* aPrefName, void* aClosure) case WORKERPREF_INTERCEPTION_OPAQUE_ENABLED: case WORKERPREF_SERVICEWORKERS: case WORKERPREF_SERVICEWORKERS_TESTING: + case WORKERPREF_PUSH: sDefaultPreferences[key] = Preferences::GetBool(aPrefName, false); break; diff --git a/dom/workers/ServiceWorkerEvents.h b/dom/workers/ServiceWorkerEvents.h index e5f5dc9db1d2..c1d51a5623a8 100644 --- a/dom/workers/ServiceWorkerEvents.h +++ b/dom/workers/ServiceWorkerEvents.h @@ -20,6 +20,7 @@ #endif #include "nsProxyRelease.h" +#include "nsContentUtils.h" class nsIInterceptedChannel; diff --git a/dom/workers/WorkerPrivate.h b/dom/workers/WorkerPrivate.h index b0c0eecdd8dc..a6c501aa42c6 100644 --- a/dom/workers/WorkerPrivate.h +++ b/dom/workers/WorkerPrivate.h @@ -1321,6 +1321,13 @@ public: return mPreferences[WORKERPREF_DOM_CACHES_TESTING]; } + bool + PushEnabled() const + { + AssertIsOnWorkerThread(); + return mPreferences[WORKERPREF_PUSH]; + } + bool OnLine() const { diff --git a/dom/workers/Workers.h b/dom/workers/Workers.h index 4a8fc7c791f6..749ef9c19c88 100644 --- a/dom/workers/Workers.h +++ b/dom/workers/Workers.h @@ -205,6 +205,7 @@ enum WorkerPreference WORKERPREF_DOM_CACHES_TESTING, // dom.caches.testing.enabled WORKERPREF_SERVICEWORKERS_TESTING, // dom.serviceWorkers.testing.enabled WORKERPREF_INTERCEPTION_OPAQUE_ENABLED, // dom.serviceWorkers.interception.opaque.enabled + WORKERPREF_PUSH, // dom.push.enabled WORKERPREF_COUNT }; diff --git a/dom/workers/test/serviceworkers/test_serviceworker_interfaces.js b/dom/workers/test/serviceworkers/test_serviceworker_interfaces.js index 09c8100b6616..546241750947 100644 --- a/dom/workers/test/serviceworkers/test_serviceworker_interfaces.js +++ b/dom/workers/test/serviceworkers/test_serviceworker_interfaces.js @@ -48,8 +48,6 @@ var ecmaGlobals = "Number", "Object", "Proxy", - "PushEvent", - "PushMessageData", "RangeError", "ReferenceError", "Reflect", @@ -172,9 +170,13 @@ var interfaceNamesInGlobalScope = // IMPORTANT: Do not change this list without review from a DOM peer! "Promise", // IMPORTANT: Do not change this list without review from a DOM peer! - "PushManager", + { name: "PushEvent", b2g: false, android: false, release: false }, // IMPORTANT: Do not change this list without review from a DOM peer! - "PushSubscription", + { name: "PushManager", b2g: false, android: false, release: false }, +// IMPORTANT: Do not change this list without review from a DOM peer! + { name: "PushMessageData", b2g: false, android: false, release: false }, +// IMPORTANT: Do not change this list without review from a DOM peer! + { name: "PushSubscription", b2g: false, android: false, release: false }, // IMPORTANT: Do not change this list without review from a DOM peer! "Request", // IMPORTANT: Do not change this list without review from a DOM peer! diff --git a/dom/workers/test/test_worker_interfaces.js b/dom/workers/test/test_worker_interfaces.js index 3b6bfc6b682f..fdf90b9496b8 100644 --- a/dom/workers/test/test_worker_interfaces.js +++ b/dom/workers/test/test_worker_interfaces.js @@ -161,6 +161,10 @@ var interfaceNamesInGlobalScope = "PerformanceMeasure", // IMPORTANT: Do not change this list without review from a DOM peer! "Promise", +// IMPORTANT: Do not change this list without review from a DOM peer! + { name: "PushManager", b2g: false, android: false, release: false }, +// IMPORTANT: Do not change this list without review from a DOM peer! + { name: "PushSubscription", b2g: false, android: false, release: false }, // IMPORTANT: Do not change this list without review from a DOM peer! "Request", // IMPORTANT: Do not change this list without review from a DOM peer!