From 7453988cd78e34bb1c0b22ae81835742ef02a8c0 Mon Sep 17 00:00:00 2001 From: Andrea Marchesini Date: Wed, 17 May 2017 16:48:54 +0200 Subject: [PATCH] Bug 1364297 - Worker and SharedWorker constructors have an optional WorkerOptions parameter, r=smaug --- dom/webidl/DedicatedWorkerGlobalScope.webidl | 2 ++ dom/webidl/SharedWorker.webidl | 2 +- dom/webidl/Worker.webidl | 8 +++++++- dom/workers/SharedWorker.cpp | 10 +++++++--- dom/workers/SharedWorker.h | 3 ++- dom/workers/WorkerPrivate.cpp | 19 +++++-------------- dom/workers/WorkerPrivate.h | 11 ++++++----- dom/workers/WorkerScope.cpp | 6 ++++-- dom/workers/WorkerScope.h | 10 +++++++++- .../open-url-redirected-worker-origin.htm | 2 +- .../SharedWorker/null-arguments.html | 4 ++-- .../unexpected-self-properties.worker.js | 2 +- 12 files changed, 47 insertions(+), 32 deletions(-) diff --git a/dom/webidl/DedicatedWorkerGlobalScope.webidl b/dom/webidl/DedicatedWorkerGlobalScope.webidl index 315ec5948255..fd64ee55d9f3 100644 --- a/dom/webidl/DedicatedWorkerGlobalScope.webidl +++ b/dom/webidl/DedicatedWorkerGlobalScope.webidl @@ -15,6 +15,8 @@ [Global=(Worker,DedicatedWorker), Exposed=DedicatedWorker] interface DedicatedWorkerGlobalScope : WorkerGlobalScope { + readonly attribute DOMString name; + [Throws] void postMessage(any message, optional sequence transfer = []); diff --git a/dom/webidl/SharedWorker.webidl b/dom/webidl/SharedWorker.webidl index e847081bf42c..9504f302e065 100644 --- a/dom/webidl/SharedWorker.webidl +++ b/dom/webidl/SharedWorker.webidl @@ -4,7 +4,7 @@ * You can obtain one at http://mozilla.org/MPL/2.0/. */ -[Constructor(USVString scriptURL, optional DOMString name)] +[Constructor(USVString scriptURL, optional (DOMString or WorkerOptions) options)] interface SharedWorker : EventTarget { readonly attribute MessagePort port; }; diff --git a/dom/webidl/Worker.webidl b/dom/webidl/Worker.webidl index 4233ad7fb185..d10e40aa16c0 100644 --- a/dom/webidl/Worker.webidl +++ b/dom/webidl/Worker.webidl @@ -12,7 +12,7 @@ * this document. */ -[Constructor(USVString scriptURL), +[Constructor(USVString scriptURL, optional WorkerOptions options), Func="mozilla::dom::workers::WorkerPrivate::WorkerAvailable", Exposed=(Window,DedicatedWorker,SharedWorker,System)] interface Worker : EventTarget { @@ -26,6 +26,12 @@ interface Worker : EventTarget { Worker implements AbstractWorker; +dictionary WorkerOptions { + // WorkerType type = "classic"; TODO: Bug 1247687 + // RequestCredentials credentials = "omit"; // credentials is only used if type is "module" TODO: Bug 1247687 + DOMString name = ""; +}; + [Constructor(USVString scriptURL), Func="mozilla::dom::workers::ChromeWorkerPrivate::WorkerAvailable", Exposed=(Window,DedicatedWorker,SharedWorker,System)] diff --git a/dom/workers/SharedWorker.cpp b/dom/workers/SharedWorker.cpp index d1faf04be322..8ed737ecb1a4 100644 --- a/dom/workers/SharedWorker.cpp +++ b/dom/workers/SharedWorker.cpp @@ -12,6 +12,7 @@ #include "mozilla/Preferences.h" #include "mozilla/dom/MessagePort.h" #include "mozilla/dom/SharedWorkerBinding.h" +#include "mozilla/dom/WorkerBinding.h" #include "mozilla/Telemetry.h" #include "nsContentUtils.h" #include "nsIClassInfoImpl.h" @@ -49,7 +50,7 @@ SharedWorker::~SharedWorker() already_AddRefed SharedWorker::Constructor(const GlobalObject& aGlobal, const nsAString& aScriptURL, - const mozilla::dom::Optional& aName, + const StringOrWorkerOptions& aOptions, ErrorResult& aRv) { AssertIsOnMainThread(); @@ -61,8 +62,11 @@ SharedWorker::Constructor(const GlobalObject& aGlobal, } nsCString name; - if (aName.WasPassed()) { - name = NS_ConvertUTF16toUTF8(aName.Value()); + if (aOptions.IsString()) { + name = NS_ConvertUTF16toUTF8(aOptions.GetAsString()); + } else { + MOZ_ASSERT(aOptions.IsWorkerOptions()); + name = NS_ConvertUTF16toUTF8(aOptions.GetAsWorkerOptions().mName); } RefPtr sharedWorker; diff --git a/dom/workers/SharedWorker.h b/dom/workers/SharedWorker.h index af14d58f9c30..c705002ddfbd 100644 --- a/dom/workers/SharedWorker.h +++ b/dom/workers/SharedWorker.h @@ -20,6 +20,7 @@ class EventChainPreVisitor; namespace dom { class MessagePort; +class StringOrWorkerOptions; } } // namespace mozilla @@ -43,7 +44,7 @@ class SharedWorker final : public DOMEventTargetHelper public: static already_AddRefed Constructor(const GlobalObject& aGlobal, const nsAString& aScriptURL, - const Optional& aName, ErrorResult& aRv); + const StringOrWorkerOptions& aOptions, ErrorResult& aRv); MessagePort* Port(); diff --git a/dom/workers/WorkerPrivate.cpp b/dom/workers/WorkerPrivate.cpp index bbe9621325c2..19e8153c0de8 100644 --- a/dom/workers/WorkerPrivate.cpp +++ b/dom/workers/WorkerPrivate.cpp @@ -2739,9 +2739,7 @@ WorkerPrivateParent::WorkerPrivateParent( mCreationTimeStamp(TimeStamp::Now()), mCreationTimeHighRes((double)PR_Now() / PR_USEC_PER_MSEC) { - MOZ_ASSERT_IF(!IsDedicatedWorker(), - !aWorkerName.IsVoid() && NS_IsMainThread()); - MOZ_ASSERT_IF(IsDedicatedWorker(), aWorkerName.IsEmpty()); + MOZ_ASSERT_IF(!IsDedicatedWorker(), NS_IsMainThread()); if (aLoadInfo.mWindow) { AssertIsOnMainThread(); @@ -4447,9 +4445,6 @@ WorkerPrivate::WorkerPrivate(WorkerPrivate* aParent, , mFetchHandlerWasAdded(false) , mOnLine(false) { - MOZ_ASSERT_IF(!IsDedicatedWorker(), !aWorkerName.IsVoid()); - MOZ_ASSERT_IF(IsDedicatedWorker(), aWorkerName.IsEmpty()); - if (aParent) { aParent->AssertIsOnWorkerThread(); aParent->GetAllPreferences(mPreferences); @@ -4506,10 +4501,12 @@ WorkerPrivate::~WorkerPrivate() already_AddRefed WorkerPrivate::Constructor(const GlobalObject& aGlobal, const nsAString& aScriptURL, + const WorkerOptions& aOptions, ErrorResult& aRv) { return WorkerPrivate::Constructor(aGlobal, aScriptURL, false, - WorkerTypeDedicated, EmptyCString(), + WorkerTypeDedicated, + NS_ConvertUTF16toUTF8(aOptions.mName), nullptr, aRv); } @@ -4588,12 +4585,6 @@ WorkerPrivate::Constructor(JSContext* aCx, AssertIsOnMainThread(); } - // Only service and shared workers can have names. - MOZ_ASSERT_IF(aWorkerType != WorkerTypeDedicated, - !aWorkerName.IsVoid()); - MOZ_ASSERT_IF(aWorkerType == WorkerTypeDedicated, - aWorkerName.IsEmpty()); - Maybe stackLoadInfo; if (!aLoadInfo) { stackLoadInfo.emplace(); @@ -6921,7 +6912,7 @@ WorkerPrivate::GetOrCreateGlobalScope(JSContext* aCx) } else if (IsServiceWorker()) { globalScope = new ServiceWorkerGlobalScope(this, ServiceWorkerScope()); } else { - globalScope = new DedicatedWorkerGlobalScope(this); + globalScope = new DedicatedWorkerGlobalScope(this, WorkerName()); } JS::Rooted global(aCx); diff --git a/dom/workers/WorkerPrivate.h b/dom/workers/WorkerPrivate.h index 35d52af19770..50ccd8249006 100644 --- a/dom/workers/WorkerPrivate.h +++ b/dom/workers/WorkerPrivate.h @@ -69,6 +69,7 @@ class PromiseNativeHandler; class StructuredCloneHolder; class WorkerDebuggerGlobalScope; class WorkerGlobalScope; +struct WorkerOptions; } // namespace dom namespace ipc { class PrincipalInfo; @@ -266,7 +267,7 @@ protected: WorkerPrivateParent(WorkerPrivate* aParent, const nsAString& aScriptURL, bool aIsChromeWorker, WorkerType aWorkerType, - const nsACString& aSharedWorkerName, + const nsACString& aWorkerName, WorkerLoadInfo& aLoadInfo); ~WorkerPrivateParent(); @@ -838,7 +839,6 @@ public: const nsCString& WorkerName() const { - MOZ_ASSERT(IsSharedWorker()); return mWorkerName; } @@ -1053,17 +1053,18 @@ protected: public: static already_AddRefed Constructor(const GlobalObject& aGlobal, const nsAString& aScriptURL, + const WorkerOptions& aOptions, ErrorResult& aRv); static already_AddRefed Constructor(const GlobalObject& aGlobal, const nsAString& aScriptURL, bool aIsChromeWorker, WorkerType aWorkerType, - const nsACString& aSharedWorkerName, + const nsACString& aWorkerName, WorkerLoadInfo* aLoadInfo, ErrorResult& aRv); static already_AddRefed Constructor(JSContext* aCx, const nsAString& aScriptURL, bool aIsChromeWorker, - WorkerType aWorkerType, const nsACString& aSharedWorkerName, + WorkerType aWorkerType, const nsACString& aWorkerName, WorkerLoadInfo* aLoadInfo, ErrorResult& aRv); static bool @@ -1462,7 +1463,7 @@ public: private: WorkerPrivate(WorkerPrivate* aParent, const nsAString& aScriptURL, bool aIsChromeWorker, - WorkerType aWorkerType, const nsACString& aSharedWorkerName, + WorkerType aWorkerType, const nsACString& aWorkerName, WorkerLoadInfo& aLoadInfo); bool diff --git a/dom/workers/WorkerScope.cpp b/dom/workers/WorkerScope.cpp index 1ab647e70108..668b41f24175 100644 --- a/dom/workers/WorkerScope.cpp +++ b/dom/workers/WorkerScope.cpp @@ -489,8 +489,10 @@ WorkerGlobalScope::CreateImageBitmap(JSContext* aCx, } } -DedicatedWorkerGlobalScope::DedicatedWorkerGlobalScope(WorkerPrivate* aWorkerPrivate) -: WorkerGlobalScope(aWorkerPrivate) +DedicatedWorkerGlobalScope::DedicatedWorkerGlobalScope(WorkerPrivate* aWorkerPrivate, + const nsCString& aName) + : WorkerGlobalScope(aWorkerPrivate) + , mName(aName) { } diff --git a/dom/workers/WorkerScope.h b/dom/workers/WorkerScope.h index 1e2da071c40f..65aec3f0e412 100644 --- a/dom/workers/WorkerScope.h +++ b/dom/workers/WorkerScope.h @@ -207,15 +207,23 @@ public: class DedicatedWorkerGlobalScope final : public WorkerGlobalScope { + const nsCString mName; + ~DedicatedWorkerGlobalScope() { } public: - explicit DedicatedWorkerGlobalScope(WorkerPrivate* aWorkerPrivate); + DedicatedWorkerGlobalScope(WorkerPrivate* aWorkerPrivate, + const nsCString& aName); virtual bool WrapGlobalObject(JSContext* aCx, JS::MutableHandle aReflector) override; + void GetName(DOMString& aName) const + { + aName.AsAString() = NS_ConvertUTF8toUTF16(mName); + } + void PostMessage(JSContext* aCx, JS::Handle aMessage, const Sequence& aTransferable, diff --git a/testing/web-platform/tests/XMLHttpRequest/open-url-redirected-worker-origin.htm b/testing/web-platform/tests/XMLHttpRequest/open-url-redirected-worker-origin.htm index 4583074aaabc..5e2065a13c8f 100644 --- a/testing/web-platform/tests/XMLHttpRequest/open-url-redirected-worker-origin.htm +++ b/testing/web-platform/tests/XMLHttpRequest/open-url-redirected-worker-origin.htm @@ -18,7 +18,7 @@ // now start the worker var finalWorkerURL = "workerxhr-origin-referrer.js"; var url = "resources/redirect.py?location=" + encodeURIComponent(finalWorkerURL); - var worker = new Worker(url, true) + var worker = new Worker(url) worker.onmessage = function (e) { var subtest = async_test(e.data.test) subtest.step(function(){ diff --git a/testing/web-platform/tests/workers/constructors/SharedWorker/null-arguments.html b/testing/web-platform/tests/workers/constructors/SharedWorker/null-arguments.html index c711b2cb5ac7..c516eed21546 100644 --- a/testing/web-platform/tests/workers/constructors/SharedWorker/null-arguments.html +++ b/testing/web-platform/tests/workers/constructors/SharedWorker/null-arguments.html @@ -14,7 +14,7 @@ onconnect = function(e) { var worker = new SharedWorker(null, null); worker.port.addEventListener('message', this.step_func(function(e) { assert_equals(e.data[0], 'null', 'first arg'); - assert_equals(e.data[1], 'null', 'second arg'); + assert_equals(e.data[1], '', 'second arg'); this.done(); }), false); worker.port.start(); @@ -22,4 +22,4 @@ onconnect = function(e) { \ No newline at end of file +//--> diff --git a/testing/web-platform/tests/workers/constructors/Worker/unexpected-self-properties.worker.js b/testing/web-platform/tests/workers/constructors/Worker/unexpected-self-properties.worker.js index aba850116dc2..69d29b229784 100644 --- a/testing/web-platform/tests/workers/constructors/Worker/unexpected-self-properties.worker.js +++ b/testing/web-platform/tests/workers/constructors/Worker/unexpected-self-properties.worker.js @@ -1,6 +1,6 @@ importScripts("/resources/testharness.js"); -var unexpected = ['open', 'print', 'stop', 'getComputedStyle', 'getSelection', 'releaseEvents', 'captureEvents', 'alert', 'confirm', 'prompt', 'addEventStream', 'removeEventStream', 'back', 'forward', 'attachEvent', 'detachEvent', 'navigate', 'DOMParser', 'XMLSerializer', 'XPathEvaluator', 'XSLTProcessor', 'opera', 'Image', 'Option', 'frames', 'Audio', 'SVGUnitTypes', 'SVGZoomAndPan', 'java', 'netscape', 'sun', 'Packages', 'ByteArray', 'closed', 'defaultStatus', 'document', 'event', 'frameElement', 'history', 'innerHeight', 'innerWidth', 'name', 'opener', 'outerHeight', 'outerWidth', 'pageXOffset', 'pageYOffset', 'parent', 'screen', 'screenLeft', 'screenTop', 'screenX', 'screenY', 'status', 'top', 'window', 'length']; // iterated window in opera and removed expected ones +var unexpected = ['open', 'print', 'stop', 'getComputedStyle', 'getSelection', 'releaseEvents', 'captureEvents', 'alert', 'confirm', 'prompt', 'addEventStream', 'removeEventStream', 'back', 'forward', 'attachEvent', 'detachEvent', 'navigate', 'DOMParser', 'XMLSerializer', 'XPathEvaluator', 'XSLTProcessor', 'opera', 'Image', 'Option', 'frames', 'Audio', 'SVGUnitTypes', 'SVGZoomAndPan', 'java', 'netscape', 'sun', 'Packages', 'ByteArray', 'closed', 'defaultStatus', 'document', 'event', 'frameElement', 'history', 'innerHeight', 'innerWidth', 'opener', 'outerHeight', 'outerWidth', 'pageXOffset', 'pageYOffset', 'parent', 'screen', 'screenLeft', 'screenTop', 'screenX', 'screenY', 'status', 'top', 'window', 'length']; // iterated window in opera and removed expected ones for (var i = 0; i < unexpected.length; ++i) { var property = unexpected[i]; test(function() {