1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 ts=8 et tw=80 : */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #ifndef mozilla_net_HttpChannelParent_h
9 #define mozilla_net_HttpChannelParent_h
11 #include "HttpBaseChannel.h"
13 #include "mozilla/net/PHttpChannelParent.h"
14 #include "mozilla/net/NeckoCommon.h"
15 #include "mozilla/net/NeckoParent.h"
16 #include "mozilla/MozPromise.h"
17 #include "nsIParentRedirectingChannel.h"
18 #include "nsIProgressEventSink.h"
19 #include "nsIChannelEventSink.h"
20 #include "nsIRedirectResultListener.h"
21 #include "nsHttpChannel.h"
22 #include "mozilla/dom/ipc/IdType.h"
23 #include "nsIMultiPartChannel.h"
28 #define HTTP_CHANNEL_PARENT_IID \
30 0x982b2372, 0x7aa5, 0x4e8a, { \
31 0xbd, 0x9f, 0x89, 0x74, 0xd7, 0xf0, 0x58, 0xeb \
43 class HttpBackgroundChannelParent
;
44 class ParentChannelListener
;
45 class ChannelEventQueue
;
47 class HttpChannelParent final
: public nsIInterfaceRequestor
,
48 public PHttpChannelParent
,
49 public nsIParentRedirectingChannel
,
50 public nsIProgressEventSink
,
51 public HttpChannelSecurityWarningReporter
,
52 public nsIAsyncVerifyRedirectReadyCallback
,
53 public nsIChannelEventSink
,
54 public nsIRedirectResultListener
,
55 public nsIMultiPartChannelListener
{
56 virtual ~HttpChannelParent();
60 NS_DECL_NSIREQUESTOBSERVER
61 NS_DECL_NSISTREAMLISTENER
62 NS_DECL_NSIPARENTCHANNEL
63 NS_DECL_NSIPARENTREDIRECTINGCHANNEL
64 NS_DECL_NSIPROGRESSEVENTSINK
65 NS_DECL_NSIINTERFACEREQUESTOR
66 NS_DECL_NSIASYNCVERIFYREDIRECTREADYCALLBACK
67 NS_DECL_NSICHANNELEVENTSINK
68 NS_DECL_NSIREDIRECTRESULTLISTENER
69 NS_DECL_NSIMULTIPARTCHANNELLISTENER
71 NS_DECLARE_STATIC_IID_ACCESSOR(HTTP_CHANNEL_PARENT_IID
)
73 HttpChannelParent(dom::BrowserParent
* iframeEmbedding
,
74 nsILoadContext
* aLoadContext
,
75 PBOverrideStatus aOverrideStatus
);
77 [[nodiscard
]] bool Init(const HttpChannelCreationArgs
& aArgs
);
79 // Forwarded to nsHttpChannel::SetApplyConversion.
80 void SetApplyConversion(bool aApplyConversion
) {
82 mChannel
->SetApplyConversion(aApplyConversion
);
86 [[nodiscard
]] nsresult
OpenAlternativeOutputStream(
87 const nsACString
& type
, int64_t predictedSize
,
88 nsIAsyncOutputStream
** _retval
);
90 // Callbacks for each asynchronous tasks required in AsyncOpen
91 // procedure, will call InvokeAsyncOpen when all the expected
92 // tasks is finished successfully or when any failure happened.
93 // @see mAsyncOpenBarrier.
94 void TryInvokeAsyncOpen(nsresult aRv
);
96 void InvokeAsyncOpen(nsresult rv
);
98 void InvokeEarlyHintPreloader(nsresult rv
, uint64_t aEarlyHintPreloaderId
);
100 // Calls SendSetPriority if mIPCClosed is false.
101 void DoSendSetPriority(int16_t aValue
);
103 // Callback while background channel is ready.
104 void OnBackgroundParentReady(HttpBackgroundChannelParent
* aBgParent
);
105 // Callback while background channel is destroyed.
106 void OnBackgroundParentDestroyed();
108 base::ProcessId
OtherPid() const;
110 // Inform the child actor that our referrer info was modified late during
112 void OverrideReferrerInfoDuringBeginConnect(nsIReferrerInfo
* aReferrerInfo
);
114 // Set the cookie string, which will be informed to the child actor during
115 // PHttpBackgroundChannel::OnStartRequest. Note that CookieService also sends
116 // the information to all actors via PContent, a main thread IPC, which could
117 // be slower than background IPC PHttpBackgroundChannel::OnStartRequest.
118 // Therefore, another cookie notification via PBackground is needed to
119 // guarantee the listener in child has the necessary cookies before
121 void SetCookie(nsCString
&& aCookie
);
123 using ChildEndpointPromise
=
124 MozPromise
<ipc::Endpoint
<extensions::PStreamFilterChild
>, bool, true>;
125 [[nodiscard
]] RefPtr
<ChildEndpointPromise
> AttachStreamFilter(
126 Endpoint
<extensions::PStreamFilterParent
>&& aParentEndpoint
,
127 Endpoint
<extensions::PStreamFilterChild
>&& aChildEndpoint
);
128 [[nodiscard
]] RefPtr
<GenericPromise
> DetachStreamFilters();
130 // Should only be called from EarlyHintPreloader. mChannel should be null at
131 // the point of calling. Sets mChannel to aChannel. Used by the
132 // EarlyHintPreloader to redirect the channel to this parent as soon as the
133 // final channel becomes available after all http redirects.
134 void SetHttpChannelFromEarlyHintPreloader(HttpBaseChannel
* aChannel
);
137 // used to connect redirected-to channel in parent with just created
138 // ChildChannel. Used during redirects.
139 [[nodiscard
]] bool ConnectChannel(const uint32_t& registrarId
);
141 [[nodiscard
]] bool DoAsyncOpen(
142 nsIURI
* uri
, nsIURI
* originalUri
, nsIURI
* docUri
,
143 nsIReferrerInfo
* aReferrerInfo
, nsIURI
* aAPIRedirectToURI
,
144 nsIURI
* topWindowUri
, const uint32_t& loadFlags
,
145 const RequestHeaderTuples
& requestHeaders
, const nsCString
& requestMethod
,
146 const Maybe
<IPCStream
>& uploadStream
, const bool& uploadStreamHasHeaders
,
147 const int16_t& priority
, const ClassOfService
& classOfService
,
148 const uint8_t& redirectionLimit
, const bool& allowSTS
,
149 const uint32_t& thirdPartyFlags
, const bool& doResumeAt
,
150 const uint64_t& startPos
, const nsCString
& entityID
,
151 const bool& allowSpdy
, const bool& allowHttp3
, const bool& allowAltSvc
,
152 const bool& beConservative
, const bool& bypassProxy
,
153 const uint32_t& tlsFlags
, const LoadInfoArgs
& aLoadInfoArgs
,
154 const uint32_t& aCacheKey
, const uint64_t& aRequestContextID
,
155 const Maybe
<CorsPreflightArgs
>& aCorsPreflightArgs
,
156 const uint32_t& aInitialRwin
, const bool& aBlockAuthPrompt
,
157 const bool& aAllowStaleCacheContent
,
158 const bool& aPreferCacheLoadOverBypass
, const nsCString
& aContentTypeHint
,
159 const dom::RequestMode
& aRequestMode
, const uint32_t& aRedirectMode
,
160 const uint64_t& aChannelId
, const nsString
& aIntegrityMetadata
,
161 const uint64_t& aContentWindowId
,
162 const nsTArray
<PreferredAlternativeDataTypeParams
>&
163 aPreferredAlternativeTypes
,
164 const uint64_t& aBrowserId
, const TimeStamp
& aLaunchServiceWorkerStart
,
165 const TimeStamp
& aLaunchServiceWorkerEnd
,
166 const TimeStamp
& aDispatchFetchEventStart
,
167 const TimeStamp
& aDispatchFetchEventEnd
,
168 const TimeStamp
& aHandleFetchEventStart
,
169 const TimeStamp
& aHandleFetchEventEnd
,
170 const bool& aForceMainDocumentChannel
,
171 const TimeStamp
& aNavigationStartTimeStamp
,
172 const uint64_t& aEarlyHintPreloaderId
,
173 const nsAString
& aClassicScriptHintCharset
,
174 const nsAString
& aDocumentCharacterSet
);
176 virtual mozilla::ipc::IPCResult
RecvSetPriority(
177 const int16_t& priority
) override
;
178 virtual mozilla::ipc::IPCResult
RecvSetClassOfService(
179 const ClassOfService
& cos
) override
;
180 virtual mozilla::ipc::IPCResult
RecvSuspend() override
;
181 virtual mozilla::ipc::IPCResult
RecvResume() override
;
182 virtual mozilla::ipc::IPCResult
RecvCancel(
183 const nsresult
& status
, const uint32_t& requestBlockingReason
,
184 const nsACString
& reason
,
185 const mozilla::Maybe
<nsCString
>& logString
) override
;
186 virtual mozilla::ipc::IPCResult
RecvRedirect2Verify(
187 const nsresult
& result
, const RequestHeaderTuples
& changedHeaders
,
188 const uint32_t& aSourceRequestBlockingReason
,
189 const Maybe
<ChildLoadInfoForwarderArgs
>& aTargetLoadInfoForwarder
,
190 const uint32_t& loadFlags
, nsIReferrerInfo
* aReferrerInfo
,
191 nsIURI
* apiRedirectUri
,
192 const Maybe
<CorsPreflightArgs
>& aCorsPreflightArgs
) override
;
193 virtual mozilla::ipc::IPCResult
RecvDocumentChannelCleanup(
194 const bool& clearCacheEntry
) override
;
195 virtual mozilla::ipc::IPCResult
RecvRemoveCorsPreflightCacheEntry(
196 nsIURI
* uri
, const mozilla::ipc::PrincipalInfo
& requestingPrincipal
,
197 const OriginAttributes
& originAttributes
) override
;
198 virtual mozilla::ipc::IPCResult
RecvSetCookies(
199 const nsACString
& aBaseDomain
, const OriginAttributes
& aOriginAttributes
,
200 nsIURI
* aHost
, const bool& aFromHttp
,
201 nsTArray
<CookieStruct
>&& aCookies
) override
;
202 virtual mozilla::ipc::IPCResult
RecvBytesRead(const int32_t& aCount
) override
;
203 virtual mozilla::ipc::IPCResult
RecvOpenOriginalCacheInputStream() override
;
204 virtual void ActorDestroy(ActorDestroyReason why
) override
;
206 friend class ParentChannelListener
;
207 RefPtr
<mozilla::dom::BrowserParent
> mBrowserParent
;
209 [[nodiscard
]] nsresult
ReportSecurityMessage(
210 const nsAString
& aMessageTag
, const nsAString
& aMessageCategory
) override
;
211 nsresult
LogBlockedCORSRequest(const nsAString
& aMessage
,
212 const nsACString
& aCategory
,
213 bool aIsWarning
= false) override
;
214 nsresult
LogMimeTypeMismatch(const nsACString
& aMessageName
, bool aWarning
,
215 const nsAString
& aURL
,
216 const nsAString
& aContentType
) override
;
218 // Calls SendDeleteSelf and sets mIPCClosed to true because we should not
219 // send any more messages after that. Bug 1274886
220 [[nodiscard
]] bool DoSendDeleteSelf();
221 // Called to notify the parent channel to not send any more IPC messages.
222 virtual mozilla::ipc::IPCResult
RecvDeletingChannel() override
;
225 already_AddRefed
<nsITransportSecurityInfo
> SecurityInfo();
227 // final step for Redirect2Verify procedure, will be invoked while both
228 // redirecting and redirected channel are ready or any error happened.
229 // OnRedirectVerifyCallback will be invoked for finishing the async
230 // redirect verification procedure.
231 void ContinueRedirect2Verify(const nsresult
& aResult
);
233 void AsyncOpenFailed(nsresult aRv
);
235 // Request to pair with a HttpBackgroundChannelParent with the same channel
236 // id, a promise will be returned so the caller can append callbacks on it.
237 // If called multiple times before mBgParent is available, the same promise
238 // will be returned and the callbacks will be invoked in order.
239 [[nodiscard
]] RefPtr
<GenericNonExclusivePromise
> WaitForBgParent(
240 uint64_t aChannelId
);
242 // Remove the association with background channel after main-thread IPC
243 // is about to be destroyed or no further event is going to be sent, i.e.,
244 // DocumentChannelCleanup.
245 void CleanupBackgroundChannel();
247 // Check if the channel needs to enable the flow control on the IPC channel.
248 // That is, we may suspend the channel if the ODA-s to child process are not
249 // consumed quickly enough. Otherwise, memory explosion could happen.
250 bool NeedFlowControl();
251 int32_t mSendWindowSize
;
253 friend class HttpBackgroundChannelParent
;
255 uint64_t mEarlyHintPreloaderId
;
257 RefPtr
<HttpBaseChannel
> mChannel
;
258 nsCOMPtr
<nsICacheEntry
> mCacheEntry
;
260 nsCOMPtr
<nsIChannel
> mRedirectChannel
;
261 nsCOMPtr
<nsIAsyncVerifyRedirectCallback
> mRedirectCallback
;
263 nsCOMPtr
<nsILoadContext
> mLoadContext
;
264 RefPtr
<nsHttpHandler
> mHttpHandler
;
266 RefPtr
<ParentChannelListener
> mParentListener
;
268 RefPtr
<ChannelEventQueue
> mEventQ
;
270 RefPtr
<HttpBackgroundChannelParent
> mBgParent
;
272 MozPromiseHolder
<GenericNonExclusivePromise
> mPromise
;
273 MozPromiseRequestHolder
<GenericNonExclusivePromise
> mRequest
;
275 // To calculate the delay caused by the e10s back-pressure suspension
276 TimeStamp mResumedTimestamp
;
278 Atomic
<bool> mIPCClosed
; // PHttpChannel actor has been Closed()
280 // Corresponding redirect channel registrar Id. 0 means redirection is not
282 uint64_t mRedirectChannelId
= 0;
284 PBOverrideStatus mPBOverride
;
286 // Set to the canceled status value if the main channel was canceled.
289 // The referrer info, set during nsHttpChannel::BeginConnect, to override the
290 // original one. This info will be sent in OnStartRequest.
291 nsCOMPtr
<nsIReferrerInfo
> mOverrideReferrerInfo
;
293 // The cookie string in Set-Cookie header. This info will be sent in
297 // OnStatus is always called before OnProgress.
298 // Set true in OnStatus if next OnProgress can be ignored
299 // since the information can be recontructed from ODA.
300 uint8_t mIgnoreProgress
: 1;
302 uint8_t mHasSuspendedByBackPressure
: 1;
304 // Set if we get the result of and cache |mNeedFlowControl|
305 uint8_t mCacheNeedFlowControlInitialized
: 1;
306 uint8_t mNeedFlowControl
: 1;
307 uint8_t mSuspendedForFlowControl
: 1;
309 // Defaults to false. Is set to true at the begining of OnStartRequest.
310 // Used to ensure methods can't be called before OnStartRequest.
311 uint8_t mAfterOnStartRequestBegun
: 1;
313 // Number of events to wait before actually invoking AsyncOpen on the main
314 // channel. For each asynchronous step required before InvokeAsyncOpen, should
315 // increase 1 to mAsyncOpenBarrier and invoke TryInvokeAsyncOpen after
316 // finished. This attribute is main thread only.
317 uint8_t mAsyncOpenBarrier
= 0;
319 // When true, ODAs are sent from the socket process to the child process
321 uint8_t mDataSentToChildProcess
: 1;
324 NS_DEFINE_STATIC_IID_ACCESSOR(HttpChannelParent
, HTTP_CHANNEL_PARENT_IID
)
327 } // namespace mozilla
329 #endif // mozilla_net_HttpChannelParent_h