Bug 1836072 [wpt PR 40324] - Fix scrollbar-width repaint issues on non viewports...
[gecko.git] / dom / worklet / WorkletFetchHandler.h
bloba380caf9eb308a4762033f4c593ecf8ee2a57366
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/. */
7 #ifndef mozilla_dom_WorkletFetchHandler_h
8 #define mozilla_dom_WorkletFetchHandler_h
10 #include "mozilla/dom/PromiseNativeHandler.h"
11 #include "mozilla/dom/RequestBinding.h" // RequestCredentials
12 #include "nsIStreamLoader.h"
14 namespace mozilla::dom {
15 class Worklet;
16 struct WorkletOptions;
17 class WorkletScriptHandler;
19 namespace loader {
20 class AddModuleThrowErrorRunnable;
21 } // namespace loader
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)
30 static already_AddRefed<Promise> AddModule(Worklet* aWorklet, JSContext* aCx,
31 const nsAString& aModuleURL,
32 const WorkletOptions& aOptions,
33 ErrorResult& aRv);
35 // Load a module script on main thread.
36 nsresult StartFetch(JSContext* aCx, nsIURI* aURI, nsIURI* aReferrer);
38 void ExecutionFailed();
39 void ExecutionFailed(JS::Handle<JS::Value> aError);
41 void ExecutionSucceeded();
43 void HandleFetchFailed(nsIURI* aURI);
45 private:
46 WorkletFetchHandler(Worklet* aWorklet, Promise* aPromise,
47 RequestCredentials aCredentials);
49 ~WorkletFetchHandler();
51 void AddPromise(JSContext* aCx, Promise* aPromise);
53 void RejectPromises(nsresult aResult);
54 void RejectPromises(JS::Handle<JS::Value> aValue);
56 void ResolvePromises();
58 friend class StartFetchRunnable;
59 friend class loader::AddModuleThrowErrorRunnable;
60 RefPtr<Worklet> mWorklet;
61 nsTArray<RefPtr<Promise>> mPromises;
63 enum { ePending, eRejected, eResolved } mStatus;
65 RequestCredentials mCredentials;
67 bool mHasError = false;
68 JS::Heap<JS::Value> mErrorToRethrow;
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;
79 NS_IMETHOD
80 Run() override;
82 private:
83 nsMainThreadPtrHandle<WorkletFetchHandler> mHandlerRef;
84 nsCOMPtr<nsIURI> mURI;
85 nsCOMPtr<nsIURI> mReferrer;
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
95 WorkletScriptHandler(Worklet* aWorklet, nsIURI* aURI);
97 void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
98 ErrorResult& aRv) override;
100 NS_IMETHOD
101 OnStreamComplete(nsIStreamLoader* aLoader, nsISupports* aContext,
102 nsresult aStatus, uint32_t aStringLen,
103 const uint8_t* aString) override;
105 void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
106 ErrorResult& aRv) override;
108 void HandleFailure(nsresult aResult);
110 private:
111 ~WorkletScriptHandler() = default;
113 void DispatchFetchCompleteToWorklet(nsresult aRv);
115 RefPtr<Worklet> mWorklet;
116 nsCOMPtr<nsIURI> mURI;
119 } // namespace mozilla::dom
120 #endif // mozilla_dom_WorkletFetchHandler_h