From 03e77dbb2f0ab680daaf51999b2fd22030d71af8 Mon Sep 17 00:00:00 2001 From: Ben Kelly Date: Tue, 29 Jul 2014 17:24:22 -0400 Subject: [PATCH] Bug 1045561 Use a WebIDL Func attribute to conditionally enable Fetch Headers. r=ehsan r=bz --- dom/fetch/Headers.cpp | 28 ++++++++++++++++++++++++++++ dom/fetch/Headers.h | 2 ++ dom/fetch/moz.build | 4 ++++ dom/webidl/Headers.webidl | 2 +- dom/workers/RegisterBindings.cpp | 8 ++------ 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/dom/fetch/Headers.cpp b/dom/fetch/Headers.cpp index 7123dbf0633..c16712202f5 100644 --- a/dom/fetch/Headers.cpp +++ b/dom/fetch/Headers.cpp @@ -8,6 +8,8 @@ #include "mozilla/ErrorResult.h" #include "mozilla/dom/UnionTypes.h" +#include "mozilla/dom/WorkerPrivate.h" +#include "mozilla/Preferences.h" #include "nsCharSeparatedTokenizer.h" #include "nsContentUtils.h" @@ -28,6 +30,32 @@ NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Headers) NS_INTERFACE_MAP_ENTRY(nsISupports) NS_INTERFACE_MAP_END +// static +bool +Headers::PrefEnabled(JSContext* aCx, JSObject* aObj) +{ + using mozilla::dom::workers::WorkerPrivate; + using mozilla::dom::workers::GetWorkerPrivateFromContext; + + if (NS_IsMainThread()) { + static bool sPrefCacheInit = false; + static bool sPrefEnabled = false; + if (sPrefCacheInit) { + return sPrefEnabled; + } + Preferences::AddBoolVarCache(&sPrefEnabled, "dom.fetch.enabled"); + sPrefCacheInit = true; + return sPrefEnabled; + } + + WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx); + if (!workerPrivate) { + return false; + } + + return workerPrivate->DOMFetchEnabled(); +} + // static already_AddRefed Headers::Constructor(const GlobalObject& aGlobal, diff --git a/dom/fetch/Headers.h b/dom/fetch/Headers.h index 48be83562d2..fab5f5c5311 100644 --- a/dom/fetch/Headers.h +++ b/dom/fetch/Headers.h @@ -54,6 +54,8 @@ public: SetIsDOMBinding(); } + static bool PrefEnabled(JSContext* cx, JSObject* obj); + static already_AddRefed Constructor(const GlobalObject& aGlobal, const Optional& aInit, diff --git a/dom/fetch/moz.build b/dom/fetch/moz.build index 297bc4ad273..df9e145ef48 100644 --- a/dom/fetch/moz.build +++ b/dom/fetch/moz.build @@ -12,6 +12,10 @@ UNIFIED_SOURCES += [ 'Headers.cpp', ] +LOCAL_INCLUDES += [ + '../workers', +] + FAIL_ON_WARNINGS = True MSVC_ENABLE_PGO = True FINAL_LIBRARY = 'xul' diff --git a/dom/webidl/Headers.webidl b/dom/webidl/Headers.webidl index f6267c24dad..a0b6783d524 100644 --- a/dom/webidl/Headers.webidl +++ b/dom/webidl/Headers.webidl @@ -20,7 +20,7 @@ enum HeadersGuardEnum { [Constructor(optional HeadersInit init), // FIXME: Exposed=Window,Worker, - Pref="dom.fetch.enabled"] + Func="mozilla::dom::Headers::PrefEnabled"] interface Headers { [Throws] void append(ByteString name, ByteString value); [Throws] void delete(ByteString name); diff --git a/dom/workers/RegisterBindings.cpp b/dom/workers/RegisterBindings.cpp index 23489b63ecc..c4525bb14b7 100644 --- a/dom/workers/RegisterBindings.cpp +++ b/dom/workers/RegisterBindings.cpp @@ -64,6 +64,8 @@ WorkerPrivate::RegisterBindings(JSContext* aCx, JS::Handle aGlobal) !DOMExceptionBinding::GetConstructorObject(aCx, aGlobal) || !EventBinding::GetConstructorObject(aCx, aGlobal) || !FileReaderSyncBinding_workers::GetConstructorObject(aCx, aGlobal) || + (HeadersBinding::ConstructorEnabled(aCx, aGlobal) && + !HeadersBinding::GetConstructorObject(aCx, aGlobal)) || !ImageDataBinding::GetConstructorObject(aCx, aGlobal) || !MessageEventBinding::GetConstructorObject(aCx, aGlobal) || !MessagePortBinding::GetConstructorObject(aCx, aGlobal) || @@ -80,12 +82,6 @@ WorkerPrivate::RegisterBindings(JSContext* aCx, JS::Handle aGlobal) return false; } - if (DOMFetchEnabled()) { - if (!HeadersBinding::GetConstructorObject(aCx, aGlobal)) { - return false; - } - } - if (!JS_DefineProfilingFunctions(aCx, aGlobal)) { return false; }