WorkletFetchHandler.h (3795B)
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_WorkletFetchHandler_h 8 #define mozilla_dom_WorkletFetchHandler_h 9 10 #include "mozilla/dom/PromiseNativeHandler.h" 11 #include "mozilla/dom/RequestBinding.h" // RequestCredentials 12 #include "nsIStreamLoader.h" 13 14 namespace mozilla::dom { 15 class Worklet; 16 struct WorkletOptions; 17 class WorkletScriptHandler; 18 19 namespace loader { 20 class AddModuleThrowErrorRunnable; 21 } // namespace loader 22 23 // WorkletFetchHandler is used to fetch the module scripts on the main thread, 24 // and notifies the result of addModule back to |aWorklet|. 25 class WorkletFetchHandler final : public nsISupports { 26 public: 27 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 28 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(WorkletFetchHandler) 29 30 static already_AddRefed<Promise> AddModule(Worklet* aWorklet, JSContext* aCx, 31 const nsAString& aModuleURL, 32 const WorkletOptions& aOptions, 33 ErrorResult& aRv); 34 35 // Load a module script on main thread. 36 nsresult StartFetch(JSContext* aCx, nsIURI* aURI, nsIURI* aReferrer); 37 38 void ExecutionFailed(); 39 void ExecutionFailed(JS::Handle<JS::Value> aError); 40 41 void ExecutionSucceeded(); 42 43 void HandleFetchFailed(nsIURI* aURI); 44 45 private: 46 WorkletFetchHandler(Worklet* aWorklet, Promise* aPromise, 47 RequestCredentials aCredentials); 48 49 ~WorkletFetchHandler(); 50 51 void AddPromise(JSContext* aCx, Promise* aPromise); 52 53 void RejectPromises(nsresult aResult); 54 void RejectPromises(JS::Handle<JS::Value> aValue); 55 56 void ResolvePromises(); 57 58 friend class StartFetchRunnable; 59 friend class loader::AddModuleThrowErrorRunnable; 60 RefPtr<Worklet> mWorklet; 61 nsTArray<RefPtr<Promise>> mPromises; 62 63 enum { ePending, eRejected, eResolved } mStatus; 64 65 RequestCredentials mCredentials; 66 67 bool mHasError = false; 68 JS::Heap<JS::Value> mErrorToRethrow; 69 }; 70 71 // A Runnable to call WorkletFetchHandler::StartFetch on the main thread. 72 class StartFetchRunnable final : public Runnable { 73 public: 74 StartFetchRunnable( 75 const nsMainThreadPtrHandle<WorkletFetchHandler>& aHandlerRef, 76 nsIURI* aURI, nsIURI* aReferrer); 77 ~StartFetchRunnable() = default; 78 79 NS_IMETHOD 80 Run() override; 81 82 private: 83 nsMainThreadPtrHandle<WorkletFetchHandler> mHandlerRef; 84 nsCOMPtr<nsIURI> mURI; 85 nsCOMPtr<nsIURI> mReferrer; 86 }; 87 88 // WorkletScriptHandler is used to handle the result of fetching the module 89 // script. 90 class WorkletScriptHandler final : public PromiseNativeHandler, 91 public nsIStreamLoaderObserver { 92 public: 93 NS_DECL_THREADSAFE_ISUPPORTS 94 95 WorkletScriptHandler(Worklet* aWorklet, nsIURI* aURI); 96 97 void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue, 98 ErrorResult& aRv) override; 99 100 NS_IMETHOD 101 OnStreamComplete(nsIStreamLoader* aLoader, nsISupports* aContext, 102 nsresult aStatus, uint32_t aStringLen, 103 const uint8_t* aString) override; 104 105 void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue, 106 ErrorResult& aRv) override; 107 108 void HandleFailure(nsresult aResult); 109 110 private: 111 ~WorkletScriptHandler() = default; 112 113 void DispatchFetchCompleteToWorklet(nsresult aRv); 114 115 RefPtr<Worklet> mWorklet; 116 nsCOMPtr<nsIURI> mURI; 117 #ifdef NIGHTLY_BUILD 118 bool mHasWasmMimeTypeEssence = false; 119 #endif 120 }; 121 122 } // namespace mozilla::dom 123 #endif // mozilla_dom_WorkletFetchHandler_h