Bug 1884032 [wpt PR 44942] - [css-color] add missing colorscheme-aware tests, a=testonly
[gecko.git] / dom / fetch / FetchService.h
blob2b9a4d21630131974112c3cab38aa42242ba486a
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 #ifndef _mozilla_dom_FetchService_h
5 #define _mozilla_dom_FetchService_h
7 #include "nsIChannel.h"
8 #include "nsIObserver.h"
9 #include "nsTHashMap.h"
10 #include "mozilla/ErrorResult.h"
11 #include "mozilla/MozPromise.h"
12 #include "mozilla/RefPtr.h"
13 #include "mozilla/dom/FetchDriver.h"
14 #include "mozilla/dom/FetchTypes.h"
15 #include "mozilla/dom/PerformanceTimingTypes.h"
16 #include "mozilla/dom/SafeRefPtr.h"
17 #include "mozilla/ipc/PBackgroundSharedTypes.h"
18 #include "mozilla/net/NeckoChannelParams.h"
20 class nsILoadGroup;
21 class nsIPrincipal;
22 class nsICookieJarSettings;
23 class PerformanceStorage;
25 namespace mozilla::dom {
27 class InternalRequest;
28 class InternalResponse;
29 class ClientInfo;
30 class ServiceWorkerDescriptor;
32 using FetchServiceResponse = SafeRefPtr<InternalResponse>;
33 using FetchServiceResponseAvailablePromise =
34 MozPromise<FetchServiceResponse, CopyableErrorResult, true>;
35 using FetchServiceResponseTimingPromise =
36 MozPromise<ResponseTiming, CopyableErrorResult, true>;
37 using FetchServiceResponseEndPromise =
38 MozPromise<ResponseEndArgs, CopyableErrorResult, true>;
40 class FetchServicePromises final {
41 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FetchServicePromises);
43 public:
44 FetchServicePromises();
46 RefPtr<FetchServiceResponseAvailablePromise> GetResponseAvailablePromise();
47 RefPtr<FetchServiceResponseTimingPromise> GetResponseTimingPromise();
48 RefPtr<FetchServiceResponseEndPromise> GetResponseEndPromise();
50 void ResolveResponseAvailablePromise(FetchServiceResponse&& aResponse,
51 const char* aMethodName);
52 void RejectResponseAvailablePromise(const CopyableErrorResult&& aError,
53 const char* aMethodName);
54 void ResolveResponseTimingPromise(ResponseTiming&& aTiming,
55 const char* aMethodName);
56 void RejectResponseTimingPromise(const CopyableErrorResult&& aError,
57 const char* aMethodName);
58 void ResolveResponseEndPromise(ResponseEndArgs&& aArgs,
59 const char* aMethodName);
60 void RejectResponseEndPromise(const CopyableErrorResult&& aError,
61 const char* aMethodName);
63 private:
64 ~FetchServicePromises() = default;
66 RefPtr<FetchServiceResponseAvailablePromise::Private> mAvailablePromise;
67 RefPtr<FetchServiceResponseTimingPromise::Private> mTimingPromise;
68 RefPtr<FetchServiceResponseEndPromise::Private> mEndPromise;
71 /**
72 * FetchService is a singleton object which designed to be used in parent
73 * process main thread only. It is used to handle the special fetch requests
74 * from ServiceWorkers(by Navigation Preload) and PFetch.
76 * FetchService creates FetchInstance internally to represent each Fetch
77 * request. It supports an asynchronous fetching, FetchServicePromises is
78 * created when a Fetch starts, once the response is ready or any error happens,
79 * the FetchServicePromises would be resolved or rejected. The promises
80 * consumers can set callbacks to handle the Fetch result.
82 class FetchService final : public nsIObserver {
83 public:
84 NS_DECL_ISUPPORTS
85 NS_DECL_NSIOBSERVER
87 struct NavigationPreloadArgs {
88 SafeRefPtr<InternalRequest> mRequest;
89 nsCOMPtr<nsIChannel> mChannel;
92 struct WorkerFetchArgs {
93 SafeRefPtr<InternalRequest> mRequest;
94 mozilla::ipc::PrincipalInfo mPrincipalInfo;
95 nsCString mWorkerScript;
96 Maybe<ClientInfo> mClientInfo;
97 Maybe<ServiceWorkerDescriptor> mController;
98 Maybe<net::CookieJarSettingsArgs> mCookieJarSettings;
99 bool mNeedOnDataAvailable;
100 nsCOMPtr<nsICSPEventListener> mCSPEventListener;
101 uint64_t mAssociatedBrowsingContextID;
102 nsCOMPtr<nsISerialEventTarget> mEventTarget;
103 nsID mActorID;
106 struct UnknownArgs {};
108 using FetchArgs =
109 Variant<NavigationPreloadArgs, WorkerFetchArgs, UnknownArgs>;
111 static already_AddRefed<FetchService> GetInstance();
113 static RefPtr<FetchServicePromises> NetworkErrorResponse(
114 nsresult aRv, const FetchArgs& aArgs = AsVariant(UnknownArgs{}));
116 FetchService();
118 // This method creates a FetchInstance to trigger fetch.
119 // The created FetchInstance is saved in mFetchInstanceTable
120 RefPtr<FetchServicePromises> Fetch(FetchArgs&& aArgs);
122 void CancelFetch(const RefPtr<FetchServicePromises>&& aPromises);
124 private:
126 * FetchInstance is an internal representation for each Fetch created by
127 * FetchService.
128 * FetchInstance is also a FetchDriverObserver which has responsibility to
129 * resolve/reject the FetchServicePromises.
130 * FetchInstance triggers fetch by instancing a FetchDriver with proper
131 * initialization. The general usage flow of FetchInstance is as follows
133 * RefPtr<FetchInstance> fetch = MakeRefPtr<FetchInstance>();
134 * fetch->Initialize(FetchArgs args);
135 * RefPtr<FetchServicePromises> fetch->Fetch();
137 class FetchInstance final : public FetchDriverObserver {
138 public:
139 FetchInstance() = default;
141 nsresult Initialize(FetchArgs&& aArgs);
143 const FetchArgs& Args() { return mArgs; }
145 RefPtr<FetchServicePromises> Fetch();
147 void Cancel();
149 /* FetchDriverObserver interface */
150 void OnResponseEnd(FetchDriverObserver::EndReason aReason,
151 JS::Handle<JS::Value> aReasonDetails) override;
152 void OnResponseAvailableInternal(
153 SafeRefPtr<InternalResponse> aResponse) override;
154 bool NeedOnDataAvailable() override;
155 void OnDataAvailable() override;
156 void FlushConsoleReport() override;
157 void OnReportPerformanceTiming() override;
158 void OnNotifyNetworkMonitorAlternateStack(uint64_t aChannelID) override;
160 private:
161 ~FetchInstance() = default;
163 SafeRefPtr<InternalRequest> mRequest;
164 nsCOMPtr<nsIPrincipal> mPrincipal;
165 nsCOMPtr<nsILoadGroup> mLoadGroup;
166 nsCOMPtr<nsICookieJarSettings> mCookieJarSettings;
167 RefPtr<PerformanceStorage> mPerformanceStorage;
168 FetchArgs mArgs{AsVariant(FetchService::UnknownArgs())};
169 RefPtr<FetchDriver> mFetchDriver;
170 SafeRefPtr<InternalResponse> mResponse;
171 RefPtr<FetchServicePromises> mPromises;
172 bool mIsWorkerFetch{false};
175 ~FetchService();
177 nsresult RegisterNetworkObserver();
178 nsresult UnregisterNetworkObserver();
180 // This is a container to manage the generated fetches.
181 nsTHashMap<nsRefPtrHashKey<FetchServicePromises>, RefPtr<FetchInstance> >
182 mFetchInstanceTable;
183 bool mObservingNetwork{false};
184 bool mOffline{false};
187 } // namespace mozilla::dom
189 #endif // _mozilla_dom_FetchService_h