Bug 1834537 - Part 6: Simplify GCRuntime::checkAllocatorState a little r=sfink
[gecko.git] / ipc / glue / BackgroundUtils.cpp
blobf3071bb6271376b5842598074c3008b5caa83b03
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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "BackgroundUtils.h"
9 #include "MainThreadUtils.h"
10 #include "mozilla/Assertions.h"
11 #include "mozilla/BasePrincipal.h"
12 #include "mozilla/ContentPrincipal.h"
13 #include "mozilla/NullPrincipal.h"
14 #include "mozilla/SystemPrincipal.h"
15 #include "mozilla/ipc/PBackgroundSharedTypes.h"
16 #include "mozilla/ipc/URIUtils.h"
17 #include "mozilla/net/CookieJarSettings.h"
18 #include "mozilla/net/InterceptionInfo.h"
19 #include "mozilla/net/NeckoChannelParams.h"
20 #include "ExpandedPrincipal.h"
21 #include "nsIScriptSecurityManager.h"
22 #include "nsIURI.h"
23 #include "nsNetUtil.h"
24 #include "mozilla/LoadInfo.h"
25 #include "nsContentUtils.h"
26 #include "nsString.h"
27 #include "nsTArray.h"
28 #include "mozilla/nsRedirectHistoryEntry.h"
29 #include "mozilla/dom/nsCSPUtils.h"
30 #include "mozilla/dom/nsCSPContext.h"
31 #include "mozilla/dom/BrowsingContext.h"
32 #include "mozilla/dom/CanonicalBrowsingContext.h"
33 #include "mozilla/dom/Document.h"
34 #include "mozilla/dom/WindowGlobalParent.h"
35 #include "mozilla/LoadInfo.h"
37 using namespace mozilla::dom;
38 using namespace mozilla::net;
40 namespace mozilla {
42 namespace ipc {
44 Result<nsCOMPtr<nsIPrincipal>, nsresult> PrincipalInfoToPrincipal(
45 const PrincipalInfo& aPrincipalInfo) {
46 MOZ_ASSERT(aPrincipalInfo.type() != PrincipalInfo::T__None);
48 nsCOMPtr<nsIPrincipal> principal;
49 nsresult rv;
51 switch (aPrincipalInfo.type()) {
52 case PrincipalInfo::TSystemPrincipalInfo: {
53 principal = SystemPrincipal::Get();
54 if (NS_WARN_IF(!principal)) {
55 return Err(NS_ERROR_NOT_INITIALIZED);
58 return principal;
61 case PrincipalInfo::TNullPrincipalInfo: {
62 const NullPrincipalInfo& info = aPrincipalInfo.get_NullPrincipalInfo();
64 nsCOMPtr<nsIURI> uri;
65 rv = NS_NewURI(getter_AddRefs(uri), info.spec());
66 if (NS_WARN_IF(NS_FAILED(rv))) {
67 return Err(rv);
70 if (!uri->SchemeIs(NS_NULLPRINCIPAL_SCHEME)) {
71 return Err(NS_ERROR_ILLEGAL_VALUE);
74 principal = NullPrincipal::Create(info.attrs(), uri);
75 return principal;
78 case PrincipalInfo::TContentPrincipalInfo: {
79 const ContentPrincipalInfo& info =
80 aPrincipalInfo.get_ContentPrincipalInfo();
82 nsCOMPtr<nsIURI> uri;
83 rv = NS_NewURI(getter_AddRefs(uri), info.spec());
84 if (NS_WARN_IF(NS_FAILED(rv))) {
85 return Err(rv);
88 nsCOMPtr<nsIURI> domain;
89 if (info.domain()) {
90 rv = NS_NewURI(getter_AddRefs(domain), *info.domain());
91 if (NS_WARN_IF(NS_FAILED(rv))) {
92 return Err(rv);
96 principal =
97 BasePrincipal::CreateContentPrincipal(uri, info.attrs(), domain);
98 if (NS_WARN_IF(!principal)) {
99 return Err(NS_ERROR_NULL_POINTER);
102 // Origin must match what the_new_principal.getOrigin returns.
103 nsAutoCString originNoSuffix;
104 rv = principal->GetOriginNoSuffix(originNoSuffix);
105 if (NS_WARN_IF(NS_FAILED(rv))) {
106 return Err(rv);
109 if (NS_WARN_IF(!info.originNoSuffix().Equals(originNoSuffix))) {
110 return Err(NS_ERROR_FAILURE);
113 if (!info.baseDomain().IsVoid()) {
114 nsAutoCString baseDomain;
115 rv = principal->GetBaseDomain(baseDomain);
116 if (NS_WARN_IF(NS_FAILED(rv))) {
117 return Err(rv);
120 if (NS_WARN_IF(!info.baseDomain().Equals(baseDomain))) {
121 return Err(NS_ERROR_FAILURE);
124 return principal;
127 case PrincipalInfo::TExpandedPrincipalInfo: {
128 const ExpandedPrincipalInfo& info =
129 aPrincipalInfo.get_ExpandedPrincipalInfo();
131 nsTArray<nsCOMPtr<nsIPrincipal>> allowlist;
132 nsCOMPtr<nsIPrincipal> alPrincipal;
134 for (uint32_t i = 0; i < info.allowlist().Length(); i++) {
135 auto principalOrErr = PrincipalInfoToPrincipal(info.allowlist()[i]);
136 if (NS_WARN_IF(principalOrErr.isErr())) {
137 nsresult ret = principalOrErr.unwrapErr();
138 return Err(ret);
140 // append that principal to the allowlist
141 allowlist.AppendElement(principalOrErr.unwrap());
144 RefPtr<ExpandedPrincipal> expandedPrincipal =
145 ExpandedPrincipal::Create(allowlist, info.attrs());
146 if (!expandedPrincipal) {
147 return Err(NS_ERROR_FAILURE);
150 principal = expandedPrincipal;
151 return principal;
154 default:
155 return Err(NS_ERROR_FAILURE);
159 bool StorageKeysEqual(const PrincipalInfo& aLeft, const PrincipalInfo& aRight) {
160 MOZ_RELEASE_ASSERT(aLeft.type() == PrincipalInfo::TContentPrincipalInfo ||
161 aLeft.type() == PrincipalInfo::TSystemPrincipalInfo);
162 MOZ_RELEASE_ASSERT(aRight.type() == PrincipalInfo::TContentPrincipalInfo ||
163 aRight.type() == PrincipalInfo::TSystemPrincipalInfo);
165 if (aLeft.type() != aRight.type()) {
166 return false;
169 if (aLeft.type() == PrincipalInfo::TContentPrincipalInfo) {
170 const ContentPrincipalInfo& leftContent = aLeft.get_ContentPrincipalInfo();
171 const ContentPrincipalInfo& rightContent =
172 aRight.get_ContentPrincipalInfo();
174 return leftContent.attrs() == rightContent.attrs() &&
175 leftContent.originNoSuffix() == rightContent.originNoSuffix();
178 // Storage keys for the System principal always equal.
179 return true;
182 already_AddRefed<nsIContentSecurityPolicy> CSPInfoToCSP(
183 const CSPInfo& aCSPInfo, Document* aRequestingDoc,
184 nsresult* aOptionalResult) {
185 MOZ_ASSERT(NS_IsMainThread());
187 nsresult stackResult;
188 nsresult& rv = aOptionalResult ? *aOptionalResult : stackResult;
190 RefPtr<nsCSPContext> csp = new nsCSPContext();
192 if (aRequestingDoc) {
193 rv = csp->SetRequestContextWithDocument(aRequestingDoc);
194 if (NS_WARN_IF(NS_FAILED(rv))) {
195 return nullptr;
197 } else {
198 auto principalOrErr =
199 PrincipalInfoToPrincipal(aCSPInfo.requestPrincipalInfo());
200 if (NS_WARN_IF(principalOrErr.isErr())) {
201 return nullptr;
204 nsCOMPtr<nsIURI> selfURI;
205 if (!aCSPInfo.selfURISpec().IsEmpty()) {
206 rv = NS_NewURI(getter_AddRefs(selfURI), aCSPInfo.selfURISpec());
207 if (NS_WARN_IF(NS_FAILED(rv))) {
208 return nullptr;
212 nsCOMPtr<nsIPrincipal> principal = principalOrErr.unwrap();
214 rv = csp->SetRequestContextWithPrincipal(
215 principal, selfURI, aCSPInfo.referrer(), aCSPInfo.innerWindowID());
216 if (NS_WARN_IF(NS_FAILED(rv))) {
217 return nullptr;
220 csp->SetSkipAllowInlineStyleCheck(aCSPInfo.skipAllowInlineStyleCheck());
222 for (uint32_t i = 0; i < aCSPInfo.policyInfos().Length(); i++) {
223 csp->AddIPCPolicy(aCSPInfo.policyInfos()[i]);
225 return csp.forget();
228 nsresult CSPToCSPInfo(nsIContentSecurityPolicy* aCSP, CSPInfo* aCSPInfo) {
229 MOZ_ASSERT(NS_IsMainThread());
230 MOZ_ASSERT(aCSP);
231 MOZ_ASSERT(aCSPInfo);
233 if (!aCSP || !aCSPInfo) {
234 return NS_ERROR_FAILURE;
237 nsCOMPtr<nsIPrincipal> requestPrincipal = aCSP->GetRequestPrincipal();
239 PrincipalInfo requestingPrincipalInfo;
240 nsresult rv =
241 PrincipalToPrincipalInfo(requestPrincipal, &requestingPrincipalInfo);
242 if (NS_WARN_IF(NS_FAILED(rv))) {
243 return rv;
246 nsCOMPtr<nsIURI> selfURI = aCSP->GetSelfURI();
247 nsAutoCString selfURISpec;
248 if (selfURI) {
249 selfURI->GetSpec(selfURISpec);
252 nsAutoString referrer;
253 aCSP->GetReferrer(referrer);
255 uint64_t windowID = aCSP->GetInnerWindowID();
256 bool skipAllowInlineStyleCheck = aCSP->GetSkipAllowInlineStyleCheck();
258 nsTArray<ContentSecurityPolicy> policies;
259 static_cast<nsCSPContext*>(aCSP)->SerializePolicies(policies);
261 *aCSPInfo = CSPInfo(std::move(policies), requestingPrincipalInfo, selfURISpec,
262 referrer, windowID, skipAllowInlineStyleCheck);
263 return NS_OK;
266 nsresult PrincipalToPrincipalInfo(nsIPrincipal* aPrincipal,
267 PrincipalInfo* aPrincipalInfo,
268 bool aSkipBaseDomain) {
269 MOZ_ASSERT(aPrincipal);
270 MOZ_ASSERT(aPrincipalInfo);
272 nsresult rv;
273 if (aPrincipal->GetIsNullPrincipal()) {
274 nsAutoCString spec;
275 rv = aPrincipal->GetAsciiSpec(spec);
276 if (NS_WARN_IF(NS_FAILED(rv))) {
277 return rv;
280 *aPrincipalInfo =
281 NullPrincipalInfo(aPrincipal->OriginAttributesRef(), spec);
282 return NS_OK;
285 if (aPrincipal->IsSystemPrincipal()) {
286 *aPrincipalInfo = SystemPrincipalInfo();
287 return NS_OK;
290 // might be an expanded principal
291 auto* basePrin = BasePrincipal::Cast(aPrincipal);
292 if (basePrin->Is<ExpandedPrincipal>()) {
293 auto* expanded = basePrin->As<ExpandedPrincipal>();
295 nsTArray<PrincipalInfo> allowlistInfo;
296 PrincipalInfo info;
298 for (auto& prin : expanded->AllowList()) {
299 rv = PrincipalToPrincipalInfo(prin, &info, aSkipBaseDomain);
300 if (NS_WARN_IF(NS_FAILED(rv))) {
301 return rv;
303 // append that spec to the allowlist
304 allowlistInfo.AppendElement(info);
307 *aPrincipalInfo = ExpandedPrincipalInfo(aPrincipal->OriginAttributesRef(),
308 std::move(allowlistInfo));
309 return NS_OK;
312 nsAutoCString spec;
313 rv = aPrincipal->GetAsciiSpec(spec);
314 if (NS_WARN_IF(NS_FAILED(rv))) {
315 return rv;
318 nsCString originNoSuffix;
319 rv = aPrincipal->GetOriginNoSuffix(originNoSuffix);
320 if (NS_WARN_IF(NS_FAILED(rv))) {
321 return rv;
324 nsCOMPtr<nsIURI> domainUri;
325 rv = aPrincipal->GetDomain(getter_AddRefs(domainUri));
326 if (NS_WARN_IF(NS_FAILED(rv))) {
327 return rv;
330 Maybe<nsCString> domain;
331 if (domainUri) {
332 domain.emplace();
333 rv = domainUri->GetSpec(domain.ref());
334 if (NS_WARN_IF(NS_FAILED(rv))) {
335 return rv;
339 // This attribute is not crucial.
340 nsCString baseDomain;
341 if (aSkipBaseDomain) {
342 baseDomain.SetIsVoid(true);
343 } else {
344 if (NS_FAILED(aPrincipal->GetBaseDomain(baseDomain))) {
345 // No warning here. Some principal URLs do not have a base-domain.
346 baseDomain.SetIsVoid(true);
350 *aPrincipalInfo =
351 ContentPrincipalInfo(aPrincipal->OriginAttributesRef(), originNoSuffix,
352 spec, domain, baseDomain);
353 return NS_OK;
356 bool IsPrincipalInfoPrivate(const PrincipalInfo& aPrincipalInfo) {
357 if (aPrincipalInfo.type() != ipc::PrincipalInfo::TContentPrincipalInfo) {
358 return false;
361 const ContentPrincipalInfo& info = aPrincipalInfo.get_ContentPrincipalInfo();
362 return !!info.attrs().mPrivateBrowsingId;
365 already_AddRefed<nsIRedirectHistoryEntry> RHEntryInfoToRHEntry(
366 const RedirectHistoryEntryInfo& aRHEntryInfo) {
367 auto principalOrErr = PrincipalInfoToPrincipal(aRHEntryInfo.principalInfo());
368 if (NS_WARN_IF(principalOrErr.isErr())) {
369 return nullptr;
372 nsCOMPtr<nsIPrincipal> principal = principalOrErr.unwrap();
373 nsCOMPtr<nsIURI> referrerUri = DeserializeURI(aRHEntryInfo.referrerUri());
375 nsCOMPtr<nsIRedirectHistoryEntry> entry = new nsRedirectHistoryEntry(
376 principal, referrerUri, aRHEntryInfo.remoteAddress());
378 return entry.forget();
381 nsresult RHEntryToRHEntryInfo(nsIRedirectHistoryEntry* aRHEntry,
382 RedirectHistoryEntryInfo* aRHEntryInfo) {
383 MOZ_ASSERT(aRHEntry);
384 MOZ_ASSERT(aRHEntryInfo);
386 nsresult rv;
387 aRHEntry->GetRemoteAddress(aRHEntryInfo->remoteAddress());
389 nsCOMPtr<nsIURI> referrerUri;
390 rv = aRHEntry->GetReferrerURI(getter_AddRefs(referrerUri));
391 NS_ENSURE_SUCCESS(rv, rv);
392 SerializeURI(referrerUri, aRHEntryInfo->referrerUri());
394 nsCOMPtr<nsIPrincipal> principal;
395 rv = aRHEntry->GetPrincipal(getter_AddRefs(principal));
396 NS_ENSURE_SUCCESS(rv, rv);
398 return PrincipalToPrincipalInfo(principal, &aRHEntryInfo->principalInfo());
401 nsresult LoadInfoToLoadInfoArgs(nsILoadInfo* aLoadInfo,
402 Maybe<LoadInfoArgs>* aOptionalLoadInfoArgs) {
403 nsresult rv = NS_OK;
404 Maybe<PrincipalInfo> loadingPrincipalInfo;
405 if (nsIPrincipal* loadingPrin = aLoadInfo->GetLoadingPrincipal()) {
406 loadingPrincipalInfo.emplace();
407 rv = PrincipalToPrincipalInfo(loadingPrin, loadingPrincipalInfo.ptr());
408 NS_ENSURE_SUCCESS(rv, rv);
411 PrincipalInfo triggeringPrincipalInfo;
412 rv = PrincipalToPrincipalInfo(aLoadInfo->TriggeringPrincipal(),
413 &triggeringPrincipalInfo);
414 NS_ENSURE_SUCCESS(rv, rv);
416 Maybe<PrincipalInfo> principalToInheritInfo;
417 if (nsIPrincipal* principalToInherit = aLoadInfo->PrincipalToInherit()) {
418 principalToInheritInfo.emplace();
419 rv = PrincipalToPrincipalInfo(principalToInherit,
420 principalToInheritInfo.ptr());
421 NS_ENSURE_SUCCESS(rv, rv);
424 Maybe<PrincipalInfo> topLevelPrincipalInfo;
425 if (nsIPrincipal* topLevenPrin = aLoadInfo->GetTopLevelPrincipal()) {
426 topLevelPrincipalInfo.emplace();
427 rv = PrincipalToPrincipalInfo(topLevenPrin, topLevelPrincipalInfo.ptr());
428 NS_ENSURE_SUCCESS(rv, rv);
431 Maybe<URIParams> optionalResultPrincipalURI;
432 nsCOMPtr<nsIURI> resultPrincipalURI;
433 Unused << aLoadInfo->GetResultPrincipalURI(
434 getter_AddRefs(resultPrincipalURI));
435 if (resultPrincipalURI) {
436 SerializeURI(resultPrincipalURI, optionalResultPrincipalURI);
439 nsCString triggeringRemoteType;
440 rv = aLoadInfo->GetTriggeringRemoteType(triggeringRemoteType);
441 NS_ENSURE_SUCCESS(rv, rv);
443 nsTArray<RedirectHistoryEntryInfo> redirectChainIncludingInternalRedirects;
444 for (const nsCOMPtr<nsIRedirectHistoryEntry>& redirectEntry :
445 aLoadInfo->RedirectChainIncludingInternalRedirects()) {
446 RedirectHistoryEntryInfo* entry =
447 redirectChainIncludingInternalRedirects.AppendElement();
448 rv = RHEntryToRHEntryInfo(redirectEntry, entry);
449 NS_ENSURE_SUCCESS(rv, rv);
452 nsTArray<RedirectHistoryEntryInfo> redirectChain;
453 for (const nsCOMPtr<nsIRedirectHistoryEntry>& redirectEntry :
454 aLoadInfo->RedirectChain()) {
455 RedirectHistoryEntryInfo* entry = redirectChain.AppendElement();
456 rv = RHEntryToRHEntryInfo(redirectEntry, entry);
457 NS_ENSURE_SUCCESS(rv, rv);
460 Maybe<IPCClientInfo> ipcClientInfo;
461 const Maybe<ClientInfo>& clientInfo = aLoadInfo->GetClientInfo();
462 if (clientInfo.isSome()) {
463 ipcClientInfo.emplace(clientInfo.ref().ToIPC());
466 Maybe<IPCClientInfo> ipcReservedClientInfo;
467 const Maybe<ClientInfo>& reservedClientInfo =
468 aLoadInfo->GetReservedClientInfo();
469 if (reservedClientInfo.isSome()) {
470 ipcReservedClientInfo.emplace(reservedClientInfo.ref().ToIPC());
473 Maybe<IPCClientInfo> ipcInitialClientInfo;
474 const Maybe<ClientInfo>& initialClientInfo =
475 aLoadInfo->GetInitialClientInfo();
476 if (initialClientInfo.isSome()) {
477 ipcInitialClientInfo.emplace(initialClientInfo.ref().ToIPC());
480 Maybe<IPCServiceWorkerDescriptor> ipcController;
481 const Maybe<ServiceWorkerDescriptor>& controller = aLoadInfo->GetController();
482 if (controller.isSome()) {
483 ipcController.emplace(controller.ref().ToIPC());
486 nsAutoString cspNonce;
487 Unused << NS_WARN_IF(NS_FAILED(aLoadInfo->GetCspNonce(cspNonce)));
489 nsCOMPtr<nsICookieJarSettings> cookieJarSettings;
490 rv = aLoadInfo->GetCookieJarSettings(getter_AddRefs(cookieJarSettings));
491 NS_ENSURE_SUCCESS(rv, rv);
493 CookieJarSettingsArgs cookieJarSettingsArgs;
494 static_cast<CookieJarSettings*>(cookieJarSettings.get())
495 ->Serialize(cookieJarSettingsArgs);
497 Maybe<CSPInfo> maybeCspToInheritInfo;
498 nsCOMPtr<nsIContentSecurityPolicy> cspToInherit =
499 aLoadInfo->GetCspToInherit();
500 if (cspToInherit) {
501 CSPInfo cspToInheritInfo;
502 Unused << NS_WARN_IF(
503 NS_FAILED(CSPToCSPInfo(cspToInherit, &cspToInheritInfo)));
504 maybeCspToInheritInfo.emplace(cspToInheritInfo);
507 nsCOMPtr<nsIURI> unstrippedURI;
508 Unused << aLoadInfo->GetUnstrippedURI(getter_AddRefs(unstrippedURI));
510 Maybe<bool> isThirdPartyContextToTopWindow;
511 if (static_cast<LoadInfo*>(aLoadInfo)
512 ->HasIsThirdPartyContextToTopWindowSet()) {
513 isThirdPartyContextToTopWindow.emplace(
514 aLoadInfo->GetIsThirdPartyContextToTopWindow());
517 Maybe<InterceptionInfoArg> interceptionInfoArg;
518 nsIInterceptionInfo* interceptionInfo = aLoadInfo->InterceptionInfo();
519 if (interceptionInfo) {
520 Maybe<PrincipalInfo> triggeringPrincipalInfo;
521 if (interceptionInfo->TriggeringPrincipal()) {
522 triggeringPrincipalInfo.emplace();
523 rv = PrincipalToPrincipalInfo(interceptionInfo->TriggeringPrincipal(),
524 triggeringPrincipalInfo.ptr());
527 nsTArray<RedirectHistoryEntryInfo> redirectChain;
528 for (const nsCOMPtr<nsIRedirectHistoryEntry>& redirectEntry :
529 interceptionInfo->RedirectChain()) {
530 RedirectHistoryEntryInfo* entry = redirectChain.AppendElement();
531 rv = RHEntryToRHEntryInfo(redirectEntry, entry);
532 NS_ENSURE_SUCCESS(rv, rv);
535 interceptionInfoArg = Some(InterceptionInfoArg(
536 triggeringPrincipalInfo, interceptionInfo->ContentPolicyType(),
537 redirectChain, interceptionInfo->FromThirdParty()));
540 *aOptionalLoadInfoArgs = Some(LoadInfoArgs(
541 loadingPrincipalInfo, triggeringPrincipalInfo, principalToInheritInfo,
542 topLevelPrincipalInfo, optionalResultPrincipalURI, triggeringRemoteType,
543 aLoadInfo->GetSandboxedNullPrincipalID(), aLoadInfo->GetSecurityFlags(),
544 aLoadInfo->GetSandboxFlags(), aLoadInfo->GetTriggeringSandboxFlags(),
545 aLoadInfo->InternalContentPolicyType(),
546 static_cast<uint32_t>(aLoadInfo->GetTainting()),
547 aLoadInfo->GetBlockAllMixedContent(),
548 aLoadInfo->GetUpgradeInsecureRequests(),
549 aLoadInfo->GetBrowserUpgradeInsecureRequests(),
550 aLoadInfo->GetBrowserDidUpgradeInsecureRequests(),
551 aLoadInfo->GetBrowserWouldUpgradeInsecureRequests(),
552 aLoadInfo->GetForceAllowDataURI(),
553 aLoadInfo->GetAllowInsecureRedirectToDataURI(),
554 aLoadInfo->GetSkipContentPolicyCheckForWebRequest(),
555 aLoadInfo->GetOriginalFrameSrcLoad(),
556 aLoadInfo->GetForceInheritPrincipalDropped(),
557 aLoadInfo->GetInnerWindowID(), aLoadInfo->GetBrowsingContextID(),
558 aLoadInfo->GetFrameBrowsingContextID(),
559 aLoadInfo->GetInitialSecurityCheckDone(),
560 aLoadInfo->GetIsInThirdPartyContext(), isThirdPartyContextToTopWindow,
561 aLoadInfo->GetIsFormSubmission(), aLoadInfo->GetSendCSPViolationEvents(),
562 aLoadInfo->GetOriginAttributes(), redirectChainIncludingInternalRedirects,
563 redirectChain, aLoadInfo->GetHasInjectedCookieForCookieBannerHandling(),
564 ipcClientInfo, ipcReservedClientInfo, ipcInitialClientInfo, ipcController,
565 aLoadInfo->CorsUnsafeHeaders(), aLoadInfo->GetForcePreflight(),
566 aLoadInfo->GetIsPreflight(), aLoadInfo->GetLoadTriggeredFromExternal(),
567 aLoadInfo->GetServiceWorkerTaintingSynthesized(),
568 aLoadInfo->GetDocumentHasUserInteracted(),
569 aLoadInfo->GetAllowListFutureDocumentsCreatedFromThisRedirectChain(),
570 aLoadInfo->GetNeedForCheckingAntiTrackingHeuristic(), cspNonce,
571 aLoadInfo->GetSkipContentSniffing(), aLoadInfo->GetHttpsOnlyStatus(),
572 aLoadInfo->GetHstsStatus(), aLoadInfo->GetHasValidUserGestureActivation(),
573 aLoadInfo->GetAllowDeprecatedSystemRequests(),
574 aLoadInfo->GetIsInDevToolsContext(), aLoadInfo->GetParserCreatedScript(),
575 aLoadInfo->GetIsFromProcessingFrameAttributes(),
576 aLoadInfo->GetIsMediaRequest(), aLoadInfo->GetIsMediaInitialRequest(),
577 aLoadInfo->GetIsFromObjectOrEmbed(), cookieJarSettingsArgs,
578 aLoadInfo->GetRequestBlockingReason(), maybeCspToInheritInfo,
579 aLoadInfo->GetStoragePermission(), aLoadInfo->GetIsMetaRefresh(),
580 aLoadInfo->GetLoadingEmbedderPolicy(),
581 aLoadInfo->GetIsOriginTrialCoepCredentiallessEnabledForTopLevel(),
582 unstrippedURI, interceptionInfoArg));
584 return NS_OK;
587 nsresult LoadInfoArgsToLoadInfo(
588 const Maybe<LoadInfoArgs>& aOptionalLoadInfoArgs,
589 const nsACString& aOriginRemoteType, nsILoadInfo** outLoadInfo) {
590 return LoadInfoArgsToLoadInfo(aOptionalLoadInfoArgs, aOriginRemoteType,
591 nullptr, outLoadInfo);
593 nsresult LoadInfoArgsToLoadInfo(
594 const Maybe<LoadInfoArgs>& aOptionalLoadInfoArgs,
595 const nsACString& aOriginRemoteType, nsINode* aCspToInheritLoadingContext,
596 nsILoadInfo** outLoadInfo) {
597 RefPtr<LoadInfo> loadInfo;
598 nsresult rv = LoadInfoArgsToLoadInfo(aOptionalLoadInfoArgs, aOriginRemoteType,
599 aCspToInheritLoadingContext,
600 getter_AddRefs(loadInfo));
601 NS_ENSURE_SUCCESS(rv, rv);
603 loadInfo.forget(outLoadInfo);
604 return NS_OK;
607 nsresult LoadInfoArgsToLoadInfo(
608 const Maybe<LoadInfoArgs>& aOptionalLoadInfoArgs,
609 const nsACString& aOriginRemoteType, LoadInfo** outLoadInfo) {
610 return LoadInfoArgsToLoadInfo(aOptionalLoadInfoArgs, aOriginRemoteType,
611 nullptr, outLoadInfo);
613 nsresult LoadInfoArgsToLoadInfo(
614 const Maybe<LoadInfoArgs>& aOptionalLoadInfoArgs,
615 const nsACString& aOriginRemoteType, nsINode* aCspToInheritLoadingContext,
616 LoadInfo** outLoadInfo) {
617 if (aOptionalLoadInfoArgs.isNothing()) {
618 *outLoadInfo = nullptr;
619 return NS_OK;
622 const LoadInfoArgs& loadInfoArgs = aOptionalLoadInfoArgs.ref();
624 nsCOMPtr<nsIPrincipal> loadingPrincipal;
625 if (loadInfoArgs.requestingPrincipalInfo().isSome()) {
626 auto loadingPrincipalOrErr =
627 PrincipalInfoToPrincipal(loadInfoArgs.requestingPrincipalInfo().ref());
628 if (NS_WARN_IF(loadingPrincipalOrErr.isErr())) {
629 return loadingPrincipalOrErr.unwrapErr();
631 loadingPrincipal = loadingPrincipalOrErr.unwrap();
634 auto triggeringPrincipalOrErr =
635 PrincipalInfoToPrincipal(loadInfoArgs.triggeringPrincipalInfo());
636 if (NS_WARN_IF(triggeringPrincipalOrErr.isErr())) {
637 return triggeringPrincipalOrErr.unwrapErr();
639 nsCOMPtr<nsIPrincipal> triggeringPrincipal =
640 triggeringPrincipalOrErr.unwrap();
642 nsCOMPtr<nsIPrincipal> principalToInherit;
643 nsCOMPtr<nsIPrincipal> flattenedPrincipalToInherit;
644 if (loadInfoArgs.principalToInheritInfo().isSome()) {
645 auto principalToInheritOrErr =
646 PrincipalInfoToPrincipal(loadInfoArgs.principalToInheritInfo().ref());
647 if (NS_WARN_IF(principalToInheritOrErr.isErr())) {
648 return principalToInheritOrErr.unwrapErr();
650 flattenedPrincipalToInherit = principalToInheritOrErr.unwrap();
653 if (XRE_IsContentProcess()) {
654 auto targetBrowsingContextId = loadInfoArgs.frameBrowsingContextID()
655 ? loadInfoArgs.frameBrowsingContextID()
656 : loadInfoArgs.browsingContextID();
657 if (RefPtr<BrowsingContext> bc =
658 BrowsingContext::Get(targetBrowsingContextId)) {
659 auto [originalTriggeringPrincipal, originalPrincipalToInherit] =
660 bc->GetTriggeringAndInheritPrincipalsForCurrentLoad();
662 if (originalTriggeringPrincipal &&
663 originalTriggeringPrincipal->Equals(triggeringPrincipal)) {
664 triggeringPrincipal = originalTriggeringPrincipal;
666 if (originalPrincipalToInherit &&
667 (loadInfoArgs.securityFlags() &
668 nsILoadInfo::SEC_FORCE_INHERIT_PRINCIPAL) &&
669 originalPrincipalToInherit->Equals(flattenedPrincipalToInherit)) {
670 principalToInherit = originalPrincipalToInherit;
674 if (!principalToInherit && loadInfoArgs.principalToInheritInfo().isSome()) {
675 principalToInherit = flattenedPrincipalToInherit;
678 nsCOMPtr<nsIPrincipal> topLevelPrincipal;
679 if (loadInfoArgs.topLevelPrincipalInfo().isSome()) {
680 auto topLevelPrincipalOrErr =
681 PrincipalInfoToPrincipal(loadInfoArgs.topLevelPrincipalInfo().ref());
682 if (NS_WARN_IF(topLevelPrincipalOrErr.isErr())) {
683 return topLevelPrincipalOrErr.unwrapErr();
685 topLevelPrincipal = topLevelPrincipalOrErr.unwrap();
688 nsCOMPtr<nsIURI> resultPrincipalURI;
689 if (loadInfoArgs.resultPrincipalURI().isSome()) {
690 resultPrincipalURI = DeserializeURI(loadInfoArgs.resultPrincipalURI());
691 NS_ENSURE_TRUE(resultPrincipalURI, NS_ERROR_UNEXPECTED);
694 // If we received this message from a content process, reset
695 // triggeringRemoteType to the process which sent us the message. If the
696 // parent sent us the message, we trust it to provide the correct triggering
697 // remote type.
699 // This means that the triggering remote type will be reset if a LoadInfo is
700 // bounced through a content process, as the LoadInfo can no longer be
701 // validated to be coming from the originally specified remote type.
702 nsCString triggeringRemoteType = loadInfoArgs.triggeringRemoteType();
703 if (aOriginRemoteType != NOT_REMOTE_TYPE &&
704 aOriginRemoteType != triggeringRemoteType) {
705 triggeringRemoteType = aOriginRemoteType;
708 RedirectHistoryArray redirectChainIncludingInternalRedirects;
709 for (const RedirectHistoryEntryInfo& entryInfo :
710 loadInfoArgs.redirectChainIncludingInternalRedirects()) {
711 nsCOMPtr<nsIRedirectHistoryEntry> redirectHistoryEntry =
712 RHEntryInfoToRHEntry(entryInfo);
713 NS_ENSURE_TRUE(redirectHistoryEntry, NS_ERROR_UNEXPECTED);
714 redirectChainIncludingInternalRedirects.AppendElement(
715 redirectHistoryEntry.forget());
718 RedirectHistoryArray redirectChain;
719 for (const RedirectHistoryEntryInfo& entryInfo :
720 loadInfoArgs.redirectChain()) {
721 nsCOMPtr<nsIRedirectHistoryEntry> redirectHistoryEntry =
722 RHEntryInfoToRHEntry(entryInfo);
723 NS_ENSURE_TRUE(redirectHistoryEntry, NS_ERROR_UNEXPECTED);
724 redirectChain.AppendElement(redirectHistoryEntry.forget());
726 nsTArray<nsCOMPtr<nsIPrincipal>> ancestorPrincipals;
727 nsTArray<uint64_t> ancestorBrowsingContextIDs;
728 if (XRE_IsParentProcess() &&
729 (nsContentUtils::InternalContentPolicyTypeToExternal(
730 loadInfoArgs.contentPolicyType()) !=
731 ExtContentPolicy::TYPE_DOCUMENT)) {
732 // Only fill out ancestor principals and browsing context IDs when we
733 // are deserializing LoadInfoArgs to be LoadInfo for a subresource
734 RefPtr<BrowsingContext> parentBC =
735 BrowsingContext::Get(loadInfoArgs.browsingContextID());
736 if (parentBC) {
737 LoadInfo::ComputeAncestors(parentBC->Canonical(), ancestorPrincipals,
738 ancestorBrowsingContextIDs);
742 Maybe<ClientInfo> clientInfo;
743 if (loadInfoArgs.clientInfo().isSome()) {
744 clientInfo.emplace(ClientInfo(loadInfoArgs.clientInfo().ref()));
747 Maybe<ClientInfo> reservedClientInfo;
748 if (loadInfoArgs.reservedClientInfo().isSome()) {
749 reservedClientInfo.emplace(
750 ClientInfo(loadInfoArgs.reservedClientInfo().ref()));
753 Maybe<ClientInfo> initialClientInfo;
754 if (loadInfoArgs.initialClientInfo().isSome()) {
755 initialClientInfo.emplace(
756 ClientInfo(loadInfoArgs.initialClientInfo().ref()));
759 // We can have an initial client info or a reserved client info, but not both.
760 MOZ_DIAGNOSTIC_ASSERT(reservedClientInfo.isNothing() ||
761 initialClientInfo.isNothing());
762 NS_ENSURE_TRUE(
763 reservedClientInfo.isNothing() || initialClientInfo.isNothing(),
764 NS_ERROR_UNEXPECTED);
766 Maybe<ServiceWorkerDescriptor> controller;
767 if (loadInfoArgs.controller().isSome()) {
768 controller.emplace(
769 ServiceWorkerDescriptor(loadInfoArgs.controller().ref()));
772 nsCOMPtr<nsICookieJarSettings> cookieJarSettings;
773 CookieJarSettings::Deserialize(loadInfoArgs.cookieJarSettings(),
774 getter_AddRefs(cookieJarSettings));
776 nsCOMPtr<nsIContentSecurityPolicy> cspToInherit;
777 Maybe<mozilla::ipc::CSPInfo> cspToInheritInfo =
778 loadInfoArgs.cspToInheritInfo();
779 if (cspToInheritInfo.isSome()) {
780 nsCOMPtr<Document> doc = do_QueryInterface(aCspToInheritLoadingContext);
781 cspToInherit = CSPInfoToCSP(cspToInheritInfo.ref(), doc);
784 // Restore the loadingContext for frames using the BrowsingContext's
785 // embedder element. Note that this only works if the embedder is
786 // same-process, so won't be fission compatible.
787 nsCOMPtr<nsINode> loadingContext;
788 RefPtr<BrowsingContext> frameBrowsingContext =
789 BrowsingContext::Get(loadInfoArgs.frameBrowsingContextID());
790 if (frameBrowsingContext) {
791 loadingContext = frameBrowsingContext->GetEmbedderElement();
794 Maybe<bool> isThirdPartyContextToTopWindow;
795 if (loadInfoArgs.isThirdPartyContextToTopWindow().isSome()) {
796 isThirdPartyContextToTopWindow.emplace(
797 loadInfoArgs.isThirdPartyContextToTopWindow().ref());
800 nsCOMPtr<nsIInterceptionInfo> interceptionInfo;
801 if (loadInfoArgs.interceptionInfo().isSome()) {
802 const InterceptionInfoArg& interceptionInfoArg =
803 loadInfoArgs.interceptionInfo().ref();
804 nsCOMPtr<nsIPrincipal> triggeringPrincipal;
805 if (interceptionInfoArg.triggeringPrincipalInfo().isSome()) {
806 auto triggeringPrincipalOrErr = PrincipalInfoToPrincipal(
807 interceptionInfoArg.triggeringPrincipalInfo().ref());
808 if (NS_WARN_IF(triggeringPrincipalOrErr.isErr())) {
809 return triggeringPrincipalOrErr.unwrapErr();
811 triggeringPrincipal = triggeringPrincipalOrErr.unwrap();
814 RedirectHistoryArray redirectChain;
815 for (const RedirectHistoryEntryInfo& entryInfo :
816 interceptionInfoArg.redirectChain()) {
817 nsCOMPtr<nsIRedirectHistoryEntry> redirectHistoryEntry =
818 RHEntryInfoToRHEntry(entryInfo);
819 NS_ENSURE_TRUE(redirectHistoryEntry, NS_ERROR_UNEXPECTED);
820 redirectChain.AppendElement(redirectHistoryEntry.forget());
823 interceptionInfo = new InterceptionInfo(
824 triggeringPrincipal, interceptionInfoArg.contentPolicyType(),
825 redirectChain, interceptionInfoArg.fromThirdParty());
828 RefPtr<mozilla::net::LoadInfo> loadInfo = new mozilla::net::LoadInfo(
829 loadingPrincipal, triggeringPrincipal, principalToInherit,
830 topLevelPrincipal, resultPrincipalURI, cookieJarSettings, cspToInherit,
831 triggeringRemoteType, loadInfoArgs.sandboxedNullPrincipalID(), clientInfo,
832 reservedClientInfo, initialClientInfo, controller,
833 loadInfoArgs.securityFlags(), loadInfoArgs.sandboxFlags(),
834 loadInfoArgs.triggeringSandboxFlags(), loadInfoArgs.contentPolicyType(),
835 static_cast<LoadTainting>(loadInfoArgs.tainting()),
836 loadInfoArgs.blockAllMixedContent(),
837 loadInfoArgs.upgradeInsecureRequests(),
838 loadInfoArgs.browserUpgradeInsecureRequests(),
839 loadInfoArgs.browserDidUpgradeInsecureRequests(),
840 loadInfoArgs.browserWouldUpgradeInsecureRequests(),
841 loadInfoArgs.forceAllowDataURI(),
842 loadInfoArgs.allowInsecureRedirectToDataURI(),
843 loadInfoArgs.skipContentPolicyCheckForWebRequest(),
844 loadInfoArgs.originalFrameSrcLoad(),
845 loadInfoArgs.forceInheritPrincipalDropped(), loadInfoArgs.innerWindowID(),
846 loadInfoArgs.browsingContextID(), loadInfoArgs.frameBrowsingContextID(),
847 loadInfoArgs.initialSecurityCheckDone(),
848 loadInfoArgs.isInThirdPartyContext(), isThirdPartyContextToTopWindow,
849 loadInfoArgs.isFormSubmission(), loadInfoArgs.sendCSPViolationEvents(),
850 loadInfoArgs.originAttributes(),
851 std::move(redirectChainIncludingInternalRedirects),
852 std::move(redirectChain), std::move(ancestorPrincipals),
853 ancestorBrowsingContextIDs, loadInfoArgs.corsUnsafeHeaders(),
854 loadInfoArgs.forcePreflight(), loadInfoArgs.isPreflight(),
855 loadInfoArgs.loadTriggeredFromExternal(),
856 loadInfoArgs.serviceWorkerTaintingSynthesized(),
857 loadInfoArgs.documentHasUserInteracted(),
858 loadInfoArgs.allowListFutureDocumentsCreatedFromThisRedirectChain(),
859 loadInfoArgs.needForCheckingAntiTrackingHeuristic(),
860 loadInfoArgs.cspNonce(), loadInfoArgs.skipContentSniffing(),
861 loadInfoArgs.httpsOnlyStatus(), loadInfoArgs.hstsStatus(),
862 loadInfoArgs.hasValidUserGestureActivation(),
863 loadInfoArgs.allowDeprecatedSystemRequests(),
864 loadInfoArgs.isInDevToolsContext(), loadInfoArgs.parserCreatedScript(),
865 loadInfoArgs.storagePermission(), loadInfoArgs.isMetaRefresh(),
866 loadInfoArgs.requestBlockingReason(), loadingContext,
867 loadInfoArgs.loadingEmbedderPolicy(),
868 loadInfoArgs.originTrialCoepCredentiallessEnabledForTopLevel(),
869 loadInfoArgs.unstrippedURI(), interceptionInfo,
870 loadInfoArgs.hasInjectedCookieForCookieBannerHandling());
872 if (loadInfoArgs.isFromProcessingFrameAttributes()) {
873 loadInfo->SetIsFromProcessingFrameAttributes();
876 if (loadInfoArgs.isMediaRequest()) {
877 loadInfo->SetIsMediaRequest(true);
879 if (loadInfoArgs.isMediaInitialRequest()) {
880 loadInfo->SetIsMediaInitialRequest(true);
884 if (loadInfoArgs.isFromObjectOrEmbed()) {
885 loadInfo->SetIsFromObjectOrEmbed(true);
888 loadInfo.forget(outLoadInfo);
889 return NS_OK;
892 void LoadInfoToParentLoadInfoForwarder(
893 nsILoadInfo* aLoadInfo, ParentLoadInfoForwarderArgs* aForwarderArgsOut) {
894 Maybe<IPCServiceWorkerDescriptor> ipcController;
895 Maybe<ServiceWorkerDescriptor> controller(aLoadInfo->GetController());
896 if (controller.isSome()) {
897 ipcController.emplace(controller.ref().ToIPC());
900 uint32_t tainting = nsILoadInfo::TAINTING_BASIC;
901 Unused << aLoadInfo->GetTainting(&tainting);
903 Maybe<CookieJarSettingsArgs> cookieJarSettingsArgs;
905 nsCOMPtr<nsICookieJarSettings> cookieJarSettings;
906 nsresult rv =
907 aLoadInfo->GetCookieJarSettings(getter_AddRefs(cookieJarSettings));
908 CookieJarSettings* cs =
909 static_cast<CookieJarSettings*>(cookieJarSettings.get());
910 if (NS_SUCCEEDED(rv) && cookieJarSettings && cs->HasBeenChanged()) {
911 CookieJarSettingsArgs args;
912 cs->Serialize(args);
913 cookieJarSettingsArgs = Some(args);
916 nsCOMPtr<nsIURI> unstrippedURI;
917 Unused << aLoadInfo->GetUnstrippedURI(getter_AddRefs(unstrippedURI));
919 Maybe<bool> isThirdPartyContextToTopWindow;
920 if (static_cast<LoadInfo*>(aLoadInfo)
921 ->HasIsThirdPartyContextToTopWindowSet()) {
922 isThirdPartyContextToTopWindow.emplace(
923 aLoadInfo->GetIsThirdPartyContextToTopWindow());
926 *aForwarderArgsOut = ParentLoadInfoForwarderArgs(
927 aLoadInfo->GetAllowInsecureRedirectToDataURI(), ipcController, tainting,
928 aLoadInfo->GetSkipContentSniffing(), aLoadInfo->GetHttpsOnlyStatus(),
929 aLoadInfo->GetHstsStatus(), aLoadInfo->GetHasValidUserGestureActivation(),
930 aLoadInfo->GetAllowDeprecatedSystemRequests(),
931 aLoadInfo->GetIsInDevToolsContext(), aLoadInfo->GetParserCreatedScript(),
932 aLoadInfo->GetTriggeringSandboxFlags(),
933 aLoadInfo->GetServiceWorkerTaintingSynthesized(),
934 aLoadInfo->GetDocumentHasUserInteracted(),
935 aLoadInfo->GetAllowListFutureDocumentsCreatedFromThisRedirectChain(),
936 cookieJarSettingsArgs, aLoadInfo->GetRequestBlockingReason(),
937 aLoadInfo->GetStoragePermission(), aLoadInfo->GetIsMetaRefresh(),
938 isThirdPartyContextToTopWindow, aLoadInfo->GetIsInThirdPartyContext(),
939 unstrippedURI);
942 nsresult MergeParentLoadInfoForwarder(
943 ParentLoadInfoForwarderArgs const& aForwarderArgs, nsILoadInfo* aLoadInfo) {
944 nsresult rv;
946 rv = aLoadInfo->SetAllowInsecureRedirectToDataURI(
947 aForwarderArgs.allowInsecureRedirectToDataURI());
948 NS_ENSURE_SUCCESS(rv, rv);
950 aLoadInfo->ClearController();
951 auto& controller = aForwarderArgs.controller();
952 if (controller.isSome()) {
953 aLoadInfo->SetController(ServiceWorkerDescriptor(controller.ref()));
956 if (aForwarderArgs.serviceWorkerTaintingSynthesized()) {
957 aLoadInfo->SynthesizeServiceWorkerTainting(
958 static_cast<LoadTainting>(aForwarderArgs.tainting()));
959 } else {
960 aLoadInfo->MaybeIncreaseTainting(aForwarderArgs.tainting());
963 rv = aLoadInfo->SetSkipContentSniffing(aForwarderArgs.skipContentSniffing());
964 NS_ENSURE_SUCCESS(rv, rv);
966 rv = aLoadInfo->SetHttpsOnlyStatus(aForwarderArgs.httpsOnlyStatus());
967 NS_ENSURE_SUCCESS(rv, rv);
969 rv = aLoadInfo->SetHstsStatus(aForwarderArgs.hstsStatus());
970 NS_ENSURE_SUCCESS(rv, rv);
972 rv = aLoadInfo->SetTriggeringSandboxFlags(
973 aForwarderArgs.triggeringSandboxFlags());
974 NS_ENSURE_SUCCESS(rv, rv);
976 rv = aLoadInfo->SetHasValidUserGestureActivation(
977 aForwarderArgs.hasValidUserGestureActivation());
978 NS_ENSURE_SUCCESS(rv, rv);
980 rv = aLoadInfo->SetAllowDeprecatedSystemRequests(
981 aForwarderArgs.allowDeprecatedSystemRequests());
982 NS_ENSURE_SUCCESS(rv, rv);
984 rv = aLoadInfo->SetIsInDevToolsContext(aForwarderArgs.isInDevToolsContext());
985 NS_ENSURE_SUCCESS(rv, rv);
987 rv = aLoadInfo->SetParserCreatedScript(aForwarderArgs.parserCreatedScript());
988 NS_ENSURE_SUCCESS(rv, rv);
990 MOZ_ALWAYS_SUCCEEDS(aLoadInfo->SetDocumentHasUserInteracted(
991 aForwarderArgs.documentHasUserInteracted()));
992 MOZ_ALWAYS_SUCCEEDS(
993 aLoadInfo->SetAllowListFutureDocumentsCreatedFromThisRedirectChain(
994 aForwarderArgs
995 .allowListFutureDocumentsCreatedFromThisRedirectChain()));
996 MOZ_ALWAYS_SUCCEEDS(aLoadInfo->SetRequestBlockingReason(
997 aForwarderArgs.requestBlockingReason()));
999 const Maybe<CookieJarSettingsArgs>& cookieJarSettingsArgs =
1000 aForwarderArgs.cookieJarSettings();
1001 if (cookieJarSettingsArgs.isSome()) {
1002 nsCOMPtr<nsICookieJarSettings> cookieJarSettings;
1003 nsresult rv =
1004 aLoadInfo->GetCookieJarSettings(getter_AddRefs(cookieJarSettings));
1005 if (NS_SUCCEEDED(rv) && cookieJarSettings) {
1006 static_cast<CookieJarSettings*>(cookieJarSettings.get())
1007 ->Merge(cookieJarSettingsArgs.ref());
1011 rv = aLoadInfo->SetStoragePermission(aForwarderArgs.storagePermission());
1012 NS_ENSURE_SUCCESS(rv, rv);
1014 rv = aLoadInfo->SetIsMetaRefresh(aForwarderArgs.isMetaRefresh());
1015 NS_ENSURE_SUCCESS(rv, rv);
1017 static_cast<LoadInfo*>(aLoadInfo)->ClearIsThirdPartyContextToTopWindow();
1018 if (aForwarderArgs.isThirdPartyContextToTopWindow().isSome()) {
1019 rv = aLoadInfo->SetIsThirdPartyContextToTopWindow(
1020 aForwarderArgs.isThirdPartyContextToTopWindow().ref());
1022 NS_ENSURE_SUCCESS(rv, rv);
1024 rv = aLoadInfo->SetIsInThirdPartyContext(
1025 aForwarderArgs.isInThirdPartyContext());
1026 NS_ENSURE_SUCCESS(rv, rv);
1028 rv = aLoadInfo->SetUnstrippedURI(aForwarderArgs.unstrippedURI());
1029 NS_ENSURE_SUCCESS(rv, rv);
1031 return NS_OK;
1034 void LoadInfoToChildLoadInfoForwarder(
1035 nsILoadInfo* aLoadInfo, ChildLoadInfoForwarderArgs* aForwarderArgsOut) {
1036 Maybe<IPCClientInfo> ipcReserved;
1037 Maybe<ClientInfo> reserved(aLoadInfo->GetReservedClientInfo());
1038 if (reserved.isSome()) {
1039 ipcReserved.emplace(reserved.ref().ToIPC());
1042 Maybe<IPCClientInfo> ipcInitial;
1043 Maybe<ClientInfo> initial(aLoadInfo->GetInitialClientInfo());
1044 if (initial.isSome()) {
1045 ipcInitial.emplace(initial.ref().ToIPC());
1048 Maybe<IPCServiceWorkerDescriptor> ipcController;
1049 Maybe<ServiceWorkerDescriptor> controller(aLoadInfo->GetController());
1050 if (controller.isSome()) {
1051 ipcController.emplace(controller.ref().ToIPC());
1054 *aForwarderArgsOut =
1055 ChildLoadInfoForwarderArgs(ipcReserved, ipcInitial, ipcController,
1056 aLoadInfo->GetRequestBlockingReason());
1059 nsresult MergeChildLoadInfoForwarder(
1060 const ChildLoadInfoForwarderArgs& aForwarderArgs, nsILoadInfo* aLoadInfo) {
1061 Maybe<ClientInfo> reservedClientInfo;
1062 auto& ipcReserved = aForwarderArgs.reservedClientInfo();
1063 if (ipcReserved.isSome()) {
1064 reservedClientInfo.emplace(ClientInfo(ipcReserved.ref()));
1067 Maybe<ClientInfo> initialClientInfo;
1068 auto& ipcInitial = aForwarderArgs.initialClientInfo();
1069 if (ipcInitial.isSome()) {
1070 initialClientInfo.emplace(ClientInfo(ipcInitial.ref()));
1073 // There should only be at most one reserved or initial ClientInfo.
1074 if (NS_WARN_IF(reservedClientInfo.isSome() && initialClientInfo.isSome())) {
1075 return NS_ERROR_FAILURE;
1078 // If we received no reserved or initial ClientInfo, then we must not
1079 // already have one set. There are no use cases where this should
1080 // happen and we don't have a way to clear the current value.
1081 if (NS_WARN_IF(reservedClientInfo.isNothing() &&
1082 initialClientInfo.isNothing() &&
1083 (aLoadInfo->GetReservedClientInfo().isSome() ||
1084 aLoadInfo->GetInitialClientInfo().isSome()))) {
1085 return NS_ERROR_FAILURE;
1088 if (reservedClientInfo.isSome()) {
1089 // We need to override here instead of simply set the value. This
1090 // allows us to change the reserved client. This is necessary when
1091 // the ClientChannelHelper created a new reserved client in the
1092 // child-side of the redirect.
1093 aLoadInfo->OverrideReservedClientInfoInParent(reservedClientInfo.ref());
1094 } else if (initialClientInfo.isSome()) {
1095 aLoadInfo->SetInitialClientInfo(initialClientInfo.ref());
1098 aLoadInfo->ClearController();
1099 auto& controller = aForwarderArgs.controller();
1100 if (controller.isSome()) {
1101 aLoadInfo->SetController(ServiceWorkerDescriptor(controller.ref()));
1104 uint32_t blockingReason = aForwarderArgs.requestBlockingReason();
1105 if (blockingReason) {
1106 // We only want to override when non-null, so that any earlier set non-null
1107 // value is not reverted to 0.
1108 aLoadInfo->SetRequestBlockingReason(blockingReason);
1111 return NS_OK;
1114 } // namespace ipc
1115 } // namespace mozilla