diff --git a/dom/workers/loader/WorkerModuleLoader.cpp b/dom/workers/loader/WorkerModuleLoader.cpp new file mode 100644 index 000000000000..2e171c4a82a0 --- /dev/null +++ b/dom/workers/loader/WorkerModuleLoader.cpp @@ -0,0 +1,66 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "mozilla/dom/workerinternals/ScriptLoader.h" +#include "WorkerModuleLoader.h" + +#include "nsISupportsImpl.h" + +namespace mozilla::dom::workerinternals::loader { + +////////////////////////////////////////////////////////////// +// WorkerModuleLoader +////////////////////////////////////////////////////////////// + +NS_IMPL_ADDREF_INHERITED(WorkerModuleLoader, JS::loader::ModuleLoaderBase) +NS_IMPL_RELEASE_INHERITED(WorkerModuleLoader, JS::loader::ModuleLoaderBase) + +NS_IMPL_CYCLE_COLLECTION_INHERITED(WorkerModuleLoader, + JS::loader::ModuleLoaderBase) + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(WorkerModuleLoader) +NS_INTERFACE_MAP_END_INHERITING(JS::loader::ModuleLoaderBase) + +WorkerModuleLoader::WorkerModuleLoader(WorkerScriptLoader* aScriptLoader, + nsIGlobalObject* aGlobalObject, + nsISerialEventTarget* aEventTarget) + : ModuleLoaderBase(aScriptLoader, aGlobalObject, aEventTarget) {} + +already_AddRefed WorkerModuleLoader::CreateStaticImport( + nsIURI* aURI, ModuleLoadRequest* aParent) { + MOZ_CRASH("Not implemented yet"); +} + +already_AddRefed WorkerModuleLoader::CreateDynamicImport( + JSContext* aCx, nsIURI* aURI, LoadedScript* aMaybeActiveScript, + JS::Handle aReferencingPrivate, JS::Handle aSpecifier, + JS::Handle aPromise) { + // TODO: Implement for Dedicated workers. Not supported for Service Workers. + return nullptr; +} + +bool WorkerModuleLoader::CanStartLoad(ModuleLoadRequest* aRequest, + nsresult* aRvOut) { + return true; +} + +nsresult WorkerModuleLoader::StartFetch(ModuleLoadRequest* aRequest) { + return NS_ERROR_FAILURE; +} + +nsresult WorkerModuleLoader::CompileFetchedModule( + JSContext* aCx, JS::Handle aGlobal, JS::CompileOptions& aOptions, + ModuleLoadRequest* aRequest, JS::MutableHandle aModuleScript) { + return NS_ERROR_FAILURE; +} + +WorkerScriptLoader* WorkerModuleLoader::GetScriptLoader() { + return static_cast(mLoader.get()); +} + +void WorkerModuleLoader::OnModuleLoadComplete(ModuleLoadRequest* aRequest) {} + +} // namespace mozilla::dom::workerinternals::loader diff --git a/dom/workers/loader/WorkerModuleLoader.h b/dom/workers/loader/WorkerModuleLoader.h new file mode 100644 index 000000000000..ebca5dde3cc0 --- /dev/null +++ b/dom/workers/loader/WorkerModuleLoader.h @@ -0,0 +1,59 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_loader_WorkerModuleLoader_h +#define mozilla_loader_WorkerModuleLoader_h + +#include "js/loader/ModuleLoaderBase.h" + +namespace mozilla::dom::workerinternals::loader { +class WorkerScriptLoader; + +// alias common classes +using ScriptFetchOptions = JS::loader::ScriptFetchOptions; +using ScriptKind = JS::loader::ScriptKind; +using ScriptLoadRequest = JS::loader::ScriptLoadRequest; +using ScriptLoadRequestList = JS::loader::ScriptLoadRequestList; +using ModuleLoadRequest = JS::loader::ModuleLoadRequest; + +class WorkerModuleLoader : public JS::loader::ModuleLoaderBase { + public: + NS_DECL_ISUPPORTS_INHERITED + NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(WorkerModuleLoader, + JS::loader::ModuleLoaderBase) + + WorkerModuleLoader(WorkerScriptLoader* aScriptLoader, + nsIGlobalObject* aGlobalObject, + nsISerialEventTarget* aEventTarget); + + private: + ~WorkerModuleLoader() = default; + + WorkerScriptLoader* GetScriptLoader(); + + already_AddRefed CreateStaticImport( + nsIURI* aURI, ModuleLoadRequest* aParent) override; + + already_AddRefed CreateDynamicImport( + JSContext* aCx, nsIURI* aURI, LoadedScript* aMaybeActiveScript, + JS::Handle aReferencingPrivate, + JS::Handle aSpecifier, + JS::Handle aPromise) override; + + bool CanStartLoad(ModuleLoadRequest* aRequest, nsresult* aRvOut) override; + + nsresult StartFetch(ModuleLoadRequest* aRequest) override; + + nsresult CompileFetchedModule( + JSContext* aCx, JS::Handle aGlobal, + JS::CompileOptions& aOptions, ModuleLoadRequest* aRequest, + JS::MutableHandle aModuleScript) override; + + void OnModuleLoadComplete(ModuleLoadRequest* aRequest) override; +}; + +} // namespace mozilla::dom::workerinternals::loader +#endif // mozilla_loader_WorkerModuleLoader_h diff --git a/dom/workers/loader/moz.build b/dom/workers/loader/moz.build index 3f36fdfc595d..d39a6acb24fd 100644 --- a/dom/workers/loader/moz.build +++ b/dom/workers/loader/moz.build @@ -17,6 +17,7 @@ EXPORTS.mozilla.dom.workerinternals += [ "CacheLoadHandler.h", "NetworkLoadHandler.h", "ScriptResponseHeaderProcessor.h", + "WorkerModuleLoader.h", ] UNIFIED_SOURCES += [ @@ -24,6 +25,7 @@ UNIFIED_SOURCES += [ "NetworkLoadHandler.cpp", "ScriptResponseHeaderProcessor.cpp", "WorkerLoadContext.cpp", + "WorkerModuleLoader.cpp", ]