Bug 1867190 - Initialise the PHC allocate delay later r=glandium
[gecko.git] / dom / fetch / InternalRequest.h
blob3dfbb9284dc5b2eee68855377c6cb400bf612789
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_InternalRequest_h
8 #define mozilla_dom_InternalRequest_h
10 #include "mozilla/dom/HeadersBinding.h"
11 #include "mozilla/dom/InternalHeaders.h"
12 #include "mozilla/dom/RequestBinding.h"
13 #include "mozilla/dom/SafeRefPtr.h"
14 #include "mozilla/LoadTainting.h"
15 #include "mozilla/UniquePtr.h"
17 #include "nsIChannelEventSink.h"
18 #include "nsIInputStream.h"
19 #include "nsISupportsImpl.h"
20 #include "mozilla/net/NeckoChannelParams.h"
21 #ifdef DEBUG
22 # include "nsIURLParser.h"
23 # include "nsNetCID.h"
24 # include "nsServiceManagerUtils.h"
25 #endif
27 using mozilla::net::RedirectHistoryEntryInfo;
29 namespace mozilla {
31 namespace ipc {
32 class PBackgroundChild;
33 class PrincipalInfo;
34 } // namespace ipc
36 namespace dom {
39 * The mapping of RequestDestination and nsContentPolicyType is currently as the
40 * following.
42 * RequestDestination| nsContentPolicyType
43 * ------------------+--------------------
44 * "audio" | TYPE_INTERNAL_AUDIO
45 * "audioworklet" | TYPE_INTERNAL_AUDIOWORKLET
46 * "document" | TYPE_DOCUMENT
47 * "embed" | TYPE_INTERNAL_EMBED
48 * "font" | TYPE_FONT, TYPE_INTERNAL_FONT_PRELOAD
49 * "frame" | TYPE_INTERNAL_FRAME
50 * "iframe" | TYPE_SUBDOCUMENT, TYPE_INTERNAL_IFRAME
51 * "image" | TYPE_INTERNAL_IMAGE, TYPE_INTERNAL_IMAGE_PRELOAD,
52 * | TYPE_IMAGE, TYPE_INTERNAL_IMAGE_FAVICON, TYPE_IMAGESET
53 * "manifest" | TYPE_WEB_MANIFEST
54 * "object" | TYPE_INTERNAL_OBJECT, TYPE_OBJECT
55 * "paintworklet" | TYPE_INTERNAL_PAINTWORKLET
56 * "report" | TYPE_CSP_REPORT
57 * "script" | TYPE_INTERNAL_SCRIPT, TYPE_INTERNAL_SCRIPT_PRELOAD,
58 * | TYPE_INTERNAL_MODULE, TYPE_INTERNAL_MODULE_PRELOAD,
59 * | TYPE_SCRIPT,
60 * | TYPE_INTERNAL_SERVICE_WORKER,
61 * | TYPE_INTERNAL_WORKER_IMPORT_SCRIPTS,
62 * | TYPE_INTERNAL_CHROMEUTILS_COMPILED_SCRIPT
63 * | TYPE_INTERNAL_FRAME_MESSAGEMANAGER_SCRIPT
64 * "sharedworker" | TYPE_INTERNAL_SHARED_WORKER
65 * "serviceworker" | The spec lists this as a valid value for the enum,
66 * | however it is impossible to observe a request with this
67 * | destination value.
68 * "style" | TYPE_INTERNAL_STYLESHEET,
69 * | TYPE_INTERNAL_STYLESHEET_PRELOAD,
70 * | TYPE_STYLESHEET
71 * "track" | TYPE_INTERNAL_TRACK
72 * "video" | TYPE_INTERNAL_VIDEO
73 * "worker" | TYPE_INTERNAL_WORKER, TYPE_INTERNAL_WORKER_STATIC_MODULE
74 * "xslt" | TYPE_XSLT
75 * "" | Default for everything else.
79 class IPCInternalRequest;
80 class Request;
82 #define kFETCH_CLIENT_REFERRER_STR "about:client"
83 class InternalRequest final : public AtomicSafeRefCounted<InternalRequest> {
84 friend class Request;
86 public:
87 MOZ_DECLARE_REFCOUNTED_TYPENAME(InternalRequest)
88 InternalRequest(const nsACString& aURL, const nsACString& aFragment);
89 InternalRequest(const nsACString& aURL, const nsACString& aFragment,
90 const nsACString& aMethod,
91 already_AddRefed<InternalHeaders> aHeaders,
92 RequestCache aCacheMode, RequestMode aMode,
93 RequestRedirect aRequestRedirect,
94 RequestCredentials aRequestCredentials,
95 const nsAString& aReferrer, ReferrerPolicy aReferrerPolicy,
96 nsContentPolicyType aContentPolicyType,
97 const nsAString& aIntegrity);
99 explicit InternalRequest(const IPCInternalRequest& aIPCRequest);
101 void ToIPCInternalRequest(IPCInternalRequest* aIPCRequest,
102 mozilla::ipc::PBackgroundChild* aManager);
104 SafeRefPtr<InternalRequest> Clone();
106 void GetMethod(nsCString& aMethod) const { aMethod.Assign(mMethod); }
108 void SetMethod(const nsACString& aMethod) { mMethod.Assign(aMethod); }
110 bool HasSimpleMethod() const {
111 return mMethod.LowerCaseEqualsASCII("get") ||
112 mMethod.LowerCaseEqualsASCII("post") ||
113 mMethod.LowerCaseEqualsASCII("head");
115 // GetURL should get the request's current url with fragment. A request has
116 // an associated current url. It is a pointer to the last fetch URL in
117 // request's url list.
118 void GetURL(nsACString& aURL) const {
119 aURL.Assign(GetURLWithoutFragment());
120 if (GetFragment().IsEmpty()) {
121 return;
123 aURL.AppendLiteral("#");
124 aURL.Append(GetFragment());
127 const nsCString& GetURLWithoutFragment() const {
128 MOZ_RELEASE_ASSERT(!mURLList.IsEmpty(),
129 "Internal Request's urlList should not be empty.");
131 return mURLList.LastElement();
134 // A safe guard for ensuring that request's URL is only allowed to be set in a
135 // sw internal redirect.
136 void SetURLForInternalRedirect(const uint32_t aFlag, const nsACString& aURL,
137 const nsACString& aFragment) {
138 // Only check in debug build to prevent it from being used unexpectedly.
139 MOZ_ASSERT(aFlag & nsIChannelEventSink::REDIRECT_INTERNAL);
141 return SetURL(aURL, aFragment);
144 // AddURL should append the url into url list.
145 // Normally we strip the fragment from the URL in Request::Constructor and
146 // pass the fragment as the second argument into it.
147 // If a fragment is present in the URL it must be stripped and passed in
148 // separately.
149 void AddURL(const nsACString& aURL, const nsACString& aFragment) {
150 MOZ_ASSERT(!aURL.IsEmpty());
151 MOZ_ASSERT(!aURL.Contains('#'));
153 mURLList.AppendElement(aURL);
155 mFragment.Assign(aFragment);
157 // Get the URL list without their fragments.
158 void GetURLListWithoutFragment(nsTArray<nsCString>& aURLList) {
159 aURLList.Assign(mURLList);
161 void GetReferrer(nsAString& aReferrer) const { aReferrer.Assign(mReferrer); }
163 void SetReferrer(const nsAString& aReferrer) {
164 #ifdef DEBUG
165 bool validReferrer = false;
166 if (aReferrer.IsEmpty() ||
167 aReferrer.EqualsLiteral(kFETCH_CLIENT_REFERRER_STR)) {
168 validReferrer = true;
169 } else {
170 nsCOMPtr<nsIURLParser> parser = do_GetService(NS_STDURLPARSER_CONTRACTID);
171 if (!parser) {
172 NS_WARNING("Could not get parser to validate URL!");
173 } else {
174 uint32_t schemePos;
175 int32_t schemeLen;
176 uint32_t authorityPos;
177 int32_t authorityLen;
178 uint32_t pathPos;
179 int32_t pathLen;
181 NS_ConvertUTF16toUTF8 ref(aReferrer);
182 nsresult rv =
183 parser->ParseURL(ref.get(), ref.Length(), &schemePos, &schemeLen,
184 &authorityPos, &authorityLen, &pathPos, &pathLen);
185 if (NS_FAILED(rv)) {
186 NS_WARNING("Invalid referrer URL!");
187 } else if (schemeLen < 0 || authorityLen < 0) {
188 NS_WARNING("Invalid referrer URL!");
189 } else {
190 validReferrer = true;
195 MOZ_ASSERT(validReferrer);
196 #endif
198 mReferrer.Assign(aReferrer);
201 ReferrerPolicy ReferrerPolicy_() const { return mReferrerPolicy; }
203 void SetReferrerPolicy(ReferrerPolicy aReferrerPolicy) {
204 mReferrerPolicy = aReferrerPolicy;
207 ReferrerPolicy GetEnvironmentReferrerPolicy() const {
208 return mEnvironmentReferrerPolicy;
211 void SetEnvironmentReferrerPolicy(ReferrerPolicy aReferrerPolicy) {
212 mEnvironmentReferrerPolicy = aReferrerPolicy;
215 bool SkipServiceWorker() const { return mSkipServiceWorker; }
217 void SetSkipServiceWorker() { mSkipServiceWorker = true; }
219 bool SkipWasmCaching() const { return mSkipWasmCaching; }
221 void SetSkipWasmCaching() { mSkipWasmCaching = true; }
223 bool IsSynchronous() const { return mSynchronous; }
225 RequestMode Mode() const { return mMode; }
227 void SetMode(RequestMode aMode) { mMode = aMode; }
229 RequestCredentials GetCredentialsMode() const { return mCredentialsMode; }
231 void SetCredentialsMode(RequestCredentials aCredentialsMode) {
232 mCredentialsMode = aCredentialsMode;
235 LoadTainting GetResponseTainting() const { return mResponseTainting; }
237 void MaybeIncreaseResponseTainting(LoadTainting aTainting) {
238 if (aTainting > mResponseTainting) {
239 mResponseTainting = aTainting;
243 RequestCache GetCacheMode() const { return mCacheMode; }
245 void SetCacheMode(RequestCache aCacheMode) { mCacheMode = aCacheMode; }
247 RequestRedirect GetRedirectMode() const { return mRedirectMode; }
249 void SetRedirectMode(RequestRedirect aRedirectMode) {
250 mRedirectMode = aRedirectMode;
253 const nsString& GetIntegrity() const { return mIntegrity; }
255 void SetIntegrity(const nsAString& aIntegrity) {
256 mIntegrity.Assign(aIntegrity);
259 bool MozErrors() const { return mMozErrors; }
261 void SetMozErrors() { mMozErrors = true; }
263 const nsCString& GetFragment() const { return mFragment; }
265 nsContentPolicyType ContentPolicyType() const { return mContentPolicyType; }
266 void SetContentPolicyType(nsContentPolicyType aContentPolicyType);
268 void OverrideContentPolicyType(nsContentPolicyType aContentPolicyType);
270 RequestDestination Destination() const {
271 return MapContentPolicyTypeToRequestDestination(mContentPolicyType);
274 bool UnsafeRequest() const { return mUnsafeRequest; }
276 void SetUnsafeRequest() { mUnsafeRequest = true; }
278 InternalHeaders* Headers() const { return mHeaders; }
280 void SetHeaders(InternalHeaders* aHeaders) {
281 MOZ_ASSERT(aHeaders);
282 mHeaders = aHeaders;
285 void SetBody(nsIInputStream* aStream, int64_t aBodyLength) {
286 // A request's body may not be reset once set.
287 MOZ_ASSERT_IF(aStream, !mBodyStream);
288 mBodyStream = aStream;
289 mBodyLength = aBodyLength;
292 // Will return the original stream!
293 // Use a tee or copy if you don't want to erase the original.
294 void GetBody(nsIInputStream** aStream, int64_t* aBodyLength = nullptr) const {
295 nsCOMPtr<nsIInputStream> s = mBodyStream;
296 s.forget(aStream);
298 if (aBodyLength) {
299 *aBodyLength = mBodyLength;
303 void SetBodyBlobURISpec(nsACString& aBlobURISpec) {
304 mBodyBlobURISpec = aBlobURISpec;
307 const nsACString& BodyBlobURISpec() const { return mBodyBlobURISpec; }
309 void SetBodyLocalPath(nsAString& aLocalPath) { mBodyLocalPath = aLocalPath; }
311 const nsAString& BodyLocalPath() const { return mBodyLocalPath; }
313 // The global is used as the client for the new object.
314 SafeRefPtr<InternalRequest> GetRequestConstructorCopy(
315 nsIGlobalObject* aGlobal, ErrorResult& aRv) const;
317 bool IsNavigationRequest() const;
319 bool IsWorkerRequest() const;
321 bool IsClientRequest() const;
323 void MaybeSkipCacheIfPerformingRevalidation();
325 bool IsContentPolicyTypeOverridden() const {
326 return mContentPolicyTypeOverridden;
329 static RequestMode MapChannelToRequestMode(nsIChannel* aChannel);
331 static RequestCredentials MapChannelToRequestCredentials(
332 nsIChannel* aChannel);
334 // Takes ownership of the principal info.
335 void SetPrincipalInfo(UniquePtr<mozilla::ipc::PrincipalInfo> aPrincipalInfo);
336 const UniquePtr<mozilla::ipc::PrincipalInfo>& GetPrincipalInfo() const {
337 return mPrincipalInfo;
340 const nsCString& GetPreferredAlternativeDataType() const {
341 return mPreferredAlternativeDataType;
344 void SetPreferredAlternativeDataType(const nsACString& aDataType) {
345 mPreferredAlternativeDataType = aDataType;
348 ~InternalRequest();
350 InternalRequest(const InternalRequest& aOther) = delete;
352 void SetEmbedderPolicy(nsILoadInfo::CrossOriginEmbedderPolicy aPolicy) {
353 mEmbedderPolicy = aPolicy;
356 nsILoadInfo::CrossOriginEmbedderPolicy GetEmbedderPolicy() const {
357 return mEmbedderPolicy;
360 void SetInterceptionTriggeringPrincipalInfo(
361 UniquePtr<mozilla::ipc::PrincipalInfo> aPrincipalInfo);
363 const UniquePtr<mozilla::ipc::PrincipalInfo>&
364 GetInterceptionTriggeringPrincipalInfo() const {
365 return mInterceptionTriggeringPrincipalInfo;
368 nsContentPolicyType InterceptionContentPolicyType() const {
369 return mInterceptionContentPolicyType;
371 void SetInterceptionContentPolicyType(nsContentPolicyType aContentPolicyType);
373 const nsTArray<RedirectHistoryEntryInfo>& InterceptionRedirectChain() const {
374 return mInterceptionRedirectChain;
377 void SetInterceptionRedirectChain(
378 const nsTArray<RedirectHistoryEntryInfo>& aRedirectChain) {
379 mInterceptionRedirectChain = aRedirectChain;
382 const bool& InterceptionFromThirdParty() const {
383 return mInterceptionFromThirdParty;
386 void SetInterceptionFromThirdParty(bool aFromThirdParty) {
387 mInterceptionFromThirdParty = aFromThirdParty;
390 private:
391 struct ConstructorGuard {};
393 public:
394 // Does not copy mBodyStream. Use fallible Clone() for complete copy.
395 InternalRequest(const InternalRequest& aOther, ConstructorGuard);
397 private:
398 // Map the content policy type to the associated fetch destination, as defined
399 // by the spec at https://fetch.spec.whatwg.org/#concept-request-destination.
400 // Note that while the HTML spec for the "Link" element and its "as" attribute
401 // (https://html.spec.whatwg.org/#attr-link-as) reuse fetch's definition of
402 // destination.
403 static RequestDestination MapContentPolicyTypeToRequestDestination(
404 nsContentPolicyType aContentPolicyType);
406 static bool IsNavigationContentPolicy(nsContentPolicyType aContentPolicyType);
408 static bool IsWorkerContentPolicy(nsContentPolicyType aContentPolicyType);
410 // It should only be called while there is a service-worker-internal-redirect.
411 void SetURL(const nsACString& aURL, const nsACString& aFragment) {
412 MOZ_ASSERT(!aURL.IsEmpty());
413 MOZ_ASSERT(!aURL.Contains('#'));
414 MOZ_ASSERT(mURLList.Length() > 0);
416 mURLList.LastElement() = aURL;
417 mFragment.Assign(aFragment);
420 nsCString mMethod;
421 // mURLList: a list of one or more fetch URLs
422 nsTArray<nsCString> mURLList;
423 RefPtr<InternalHeaders> mHeaders;
424 nsCString mBodyBlobURISpec;
425 nsString mBodyLocalPath;
426 nsCOMPtr<nsIInputStream> mBodyStream;
427 int64_t mBodyLength;
429 nsCString mPreferredAlternativeDataType;
431 nsContentPolicyType mContentPolicyType;
433 // Empty string: no-referrer
434 // "about:client": client (default)
435 // URL: an URL
436 nsString mReferrer;
437 ReferrerPolicy mReferrerPolicy;
439 // This will be used for request created from Window or Worker contexts
440 // In case there's no Referrer Policy in Request, this will be passed to
441 // channel.
442 ReferrerPolicy mEnvironmentReferrerPolicy;
443 RequestMode mMode;
444 RequestCredentials mCredentialsMode;
445 LoadTainting mResponseTainting = LoadTainting::Basic;
446 RequestCache mCacheMode;
447 RequestRedirect mRedirectMode;
448 nsString mIntegrity;
449 bool mMozErrors = false;
450 nsCString mFragment;
451 bool mSkipServiceWorker = false;
452 bool mSkipWasmCaching = false;
453 bool mSynchronous = false;
454 bool mUnsafeRequest = false;
455 bool mUseURLCredentials = false;
456 // This is only set when Request.overrideContentPolicyType() has been set.
457 // It is illegal to pass such a Request object to a fetch() method unless
458 // if the caller has chrome privileges.
459 bool mContentPolicyTypeOverridden = false;
460 nsILoadInfo::CrossOriginEmbedderPolicy mEmbedderPolicy =
461 nsILoadInfo::EMBEDDER_POLICY_NULL;
463 UniquePtr<mozilla::ipc::PrincipalInfo> mPrincipalInfo;
465 // Following members are specific for the FetchEvent.request or
466 // NavigationPreload request which is extracted from the
467 // InterceptedHttpChannel.
468 // Notice that these members would not be copied when calling
469 // InternalRequest::GetRequestConstructorCopy() since these information should
470 // not be propagated when copying the Request in ServiceWorker script.
472 // This is the trigging principalInfo of the InterceptedHttpChannel.
473 UniquePtr<mozilla::ipc::PrincipalInfo> mInterceptionTriggeringPrincipalInfo;
475 // This is the contentPolicyType of the InterceptedHttpChannel.
476 nsContentPolicyType mInterceptionContentPolicyType{
477 nsIContentPolicy::TYPE_INVALID};
479 // This is the redirect history of the InterceptedHttpChannel.
480 CopyableTArray<RedirectHistoryEntryInfo> mInterceptionRedirectChain;
482 // This indicates that the InterceptedHttpChannel is a third party channel.
483 bool mInterceptionFromThirdParty{false};
486 } // namespace dom
487 } // namespace mozilla
489 #endif // mozilla_dom_InternalRequest_h