WorkletModuleLoader.h (4972B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_worklet_WorkletModuleLoader_h 8 #define mozilla_dom_worklet_WorkletModuleLoader_h 9 10 #include "js/loader/LoadContextBase.h" 11 #include "js/loader/ModuleLoaderBase.h" 12 #include "js/loader/ResolveResult.h" // For ResolveError 13 #include "mozilla/dom/WorkletFetchHandler.h" 14 15 namespace mozilla::dom { 16 namespace loader { 17 class WorkletScriptLoader : public JS::loader::ScriptLoaderInterface { 18 public: 19 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 20 NS_DECL_CYCLE_COLLECTION_CLASS(WorkletScriptLoader) 21 22 nsIURI* GetBaseURI() const override { return nullptr; } 23 24 void ReportErrorToConsole(ScriptLoadRequest* aRequest, 25 nsresult aResult) const override {} 26 27 void ReportWarningToConsole( 28 ScriptLoadRequest* aRequest, const char* aMessageName, 29 const nsTArray<nsString>& aParams) const override {} 30 31 nsresult FillCompileOptionsForRequest( 32 JSContext* cx, ScriptLoadRequest* aRequest, JS::CompileOptions* aOptions, 33 JS::MutableHandle<JSScript*> aIntroductionScript) override; 34 35 private: 36 ~WorkletScriptLoader() = default; 37 }; 38 39 class WorkletModuleLoader : public JS::loader::ModuleLoaderBase { 40 public: 41 NS_DECL_ISUPPORTS_INHERITED 42 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(WorkletModuleLoader, 43 JS::loader::ModuleLoaderBase) 44 45 WorkletModuleLoader(WorkletScriptLoader* aScriptLoader, 46 nsIGlobalObject* aGlobalObject); 47 48 void InsertRequest(nsIURI* aURI, JS::loader::ModuleLoadRequest* aRequest); 49 void RemoveRequest(nsIURI* aURI); 50 JS::loader::ModuleLoadRequest* GetRequest(nsIURI* aURI) const; 51 52 bool HasSetLocalizedStrings() const { return (bool)mLocalizedStrs; } 53 void SetLocalizedStrings(const nsTArray<nsString>* aStrings) { 54 mLocalizedStrs = aStrings; 55 } 56 57 private: 58 ~WorkletModuleLoader() = default; 59 60 already_AddRefed<ModuleLoadRequest> CreateRequest( 61 JSContext* aCx, nsIURI* aURI, JS::Handle<JSObject*> aModuleRequest, 62 JS::Handle<JS::Value> aHostDefined, JS::Handle<JS::Value> aPayload, 63 bool aIsDynamicImport, JS::loader::ScriptFetchOptions* aOptions, 64 dom::ReferrerPolicy aReferrerPolicy, nsIURI* aBaseURL, 65 const dom::SRIMetadata& aSriMetadata) override; 66 67 bool IsDynamicImportSupported() override { return false; } 68 69 bool CanStartLoad(JS::loader::ModuleLoadRequest* aRequest, 70 nsresult* aRvOut) override; 71 72 nsresult StartFetch(JS::loader::ModuleLoadRequest* aRequest) override; 73 74 nsresult CompileFetchedModule( 75 JSContext* aCx, JS::Handle<JSObject*> aGlobal, 76 JS::CompileOptions& aOptions, JS::loader::ModuleLoadRequest* aRequest, 77 JS::MutableHandle<JSObject*> aModuleScript) override; 78 79 nsresult CompileJavaScriptOrWasmModule( 80 JSContext* aCx, JS::CompileOptions& aOptions, ModuleLoadRequest* aRequest, 81 JS::MutableHandle<JSObject*> aModuleScript); 82 83 nsresult CompileJsonModule(JSContext* aCx, JS::CompileOptions& aOptions, 84 ModuleLoadRequest* aRequest, 85 JS::MutableHandle<JSObject*> aModuleScript); 86 87 void OnModuleLoadComplete(JS::loader::ModuleLoadRequest* aRequest) override; 88 89 nsresult GetResolveFailureMessage(JS::loader::ResolveError aError, 90 const nsAString& aSpecifier, 91 nsAString& aResult) override; 92 93 bool IsModuleTypeAllowed(JS::ModuleType aModuleType) override { 94 // https://html.spec.whatwg.org/#module-type-allowed 95 // If moduleType is "css" and the CSSStyleSheet interface is not exposed in 96 // settings's realm, then return false. 97 return aModuleType == JS::ModuleType::JavaScript || 98 aModuleType == JS::ModuleType::JSON; 99 } 100 101 // A hashtable to map a nsIURI(from main thread) to a ModuleLoadRequest(in 102 // worklet thread). 103 nsRefPtrHashtable<nsURIHashKey, JS::loader::ModuleLoadRequest> 104 mFetchingRequests; 105 106 // We get the localized strings on the main thread, and pass it to 107 // WorkletModuleLoader. 108 const nsTArray<nsString>* mLocalizedStrs = nullptr; 109 }; 110 } // namespace loader 111 112 class WorkletLoadContext : public JS::loader::LoadContextBase { 113 public: 114 explicit WorkletLoadContext( 115 const nsMainThreadPtrHandle<WorkletFetchHandler>& aHandlerRef) 116 : JS::loader::LoadContextBase(JS::loader::ContextKind::Worklet), 117 mHandlerRef(aHandlerRef) {} 118 119 const nsMainThreadPtrHandle<WorkletFetchHandler>& GetHandlerRef() const { 120 return mHandlerRef; 121 } 122 123 private: 124 ~WorkletLoadContext() = default; 125 126 nsMainThreadPtrHandle<WorkletFetchHandler> mHandlerRef; 127 }; 128 } // namespace mozilla::dom 129 #endif // mozilla_dom_worklet_WorkletModuleLoader_h