tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

InternalRequest.cpp (21937B)


      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/. */
      6 
      7 #include "InternalRequest.h"
      8 
      9 #include "InternalResponse.h"
     10 #include "mozilla/ErrorResult.h"
     11 #include "mozilla/RemoteLazyInputStreamChild.h"
     12 #include "mozilla/dom/Document.h"
     13 #include "mozilla/dom/FetchTypes.h"
     14 #include "mozilla/dom/ScriptSettings.h"
     15 #include "mozilla/dom/WorkerCommon.h"
     16 #include "mozilla/dom/WorkerPrivate.h"
     17 #include "mozilla/ipc/IPCStreamUtils.h"
     18 #include "mozilla/ipc/PBackgroundChild.h"
     19 #include "nsIContentPolicy.h"
     20 #include "nsStreamUtils.h"
     21 
     22 namespace mozilla::dom {
     23 // The global is used to extract the principal.
     24 SafeRefPtr<InternalRequest> InternalRequest::GetRequestConstructorCopy(
     25    nsIGlobalObject* aGlobal, ErrorResult& aRv) const {
     26  MOZ_RELEASE_ASSERT(!mURLList.IsEmpty(),
     27                     "Internal Request's urlList should not be empty when "
     28                     "copied from constructor.");
     29  auto copy =
     30      MakeSafeRefPtr<InternalRequest>(mURLList.LastElement(), mFragment);
     31  copy->SetMethod(mMethod);
     32  copy->mHeaders = new InternalHeaders(*mHeaders);
     33  copy->mTriggeringPrincipalOverride = mTriggeringPrincipalOverride;
     34  copy->mNeverTaint = mNeverTaint;
     35  copy->SetUnsafeRequest();
     36  copy->mBodyStream = mBodyStream;
     37  copy->mBodyLength = mBodyLength;
     38  // The "client" is not stored in our implementation. Fetch API users should
     39  // use the appropriate window/document/principal and other Gecko security
     40  // mechanisms as appropriate.
     41  copy->mReferrer = mReferrer;
     42  copy->mReferrerPolicy = mReferrerPolicy;
     43  copy->mEnvironmentReferrerPolicy = mEnvironmentReferrerPolicy;
     44  copy->mIntegrity = mIntegrity;
     45  copy->mMozErrors = mMozErrors;
     46 
     47  copy->mContentPolicyType = mContentPolicyTypeOverridden
     48                                 ? mContentPolicyType
     49                                 : nsIContentPolicy::TYPE_FETCH;
     50  copy->mInternalPriority = mInternalPriority;
     51  copy->mMode = mMode;
     52  copy->mCredentialsMode = mCredentialsMode;
     53  copy->mCacheMode = mCacheMode;
     54  copy->mRedirectMode = mRedirectMode;
     55  copy->mPriorityMode = mPriorityMode;
     56  copy->mContentPolicyTypeOverridden = mContentPolicyTypeOverridden;
     57 
     58  copy->mPreferredAlternativeDataType = mPreferredAlternativeDataType;
     59  copy->mSkipWasmCaching = mSkipWasmCaching;
     60  copy->mEmbedderPolicy = mEmbedderPolicy;
     61  return copy;
     62 }
     63 
     64 SafeRefPtr<InternalRequest> InternalRequest::Clone() {
     65  auto clone = MakeSafeRefPtr<InternalRequest>(*this, ConstructorGuard{});
     66 
     67  if (!mBodyStream) {
     68    return clone;
     69  }
     70 
     71  nsCOMPtr<nsIInputStream> clonedBody;
     72  nsCOMPtr<nsIInputStream> replacementBody;
     73 
     74  nsresult rv = NS_CloneInputStream(mBodyStream, getter_AddRefs(clonedBody),
     75                                    getter_AddRefs(replacementBody));
     76  if (NS_WARN_IF(NS_FAILED(rv))) {
     77    return nullptr;
     78  }
     79 
     80  clone->mBodyStream.swap(clonedBody);
     81  if (replacementBody) {
     82    mBodyStream.swap(replacementBody);
     83  }
     84  return clone;
     85 }
     86 InternalRequest::InternalRequest(const nsACString& aURL,
     87                                 const nsACString& aFragment)
     88    : mMethod("GET"),
     89      mHeaders(new InternalHeaders(HeadersGuardEnum::None)),
     90      mBodyLength(InternalResponse::UNKNOWN_BODY_SIZE),
     91      mContentPolicyType(nsIContentPolicy::TYPE_FETCH),
     92      mReferrer(nsLiteralCString(kFETCH_CLIENT_REFERRER_STR)),
     93      mReferrerPolicy(ReferrerPolicy::_empty),
     94      mEnvironmentReferrerPolicy(ReferrerPolicy::_empty),
     95      mMode(RequestMode::No_cors),
     96      mCredentialsMode(RequestCredentials::Omit),
     97      mCacheMode(RequestCache::Default),
     98      mRedirectMode(RequestRedirect::Follow),
     99      mPriorityMode(RequestPriority::Auto) {
    100  MOZ_ASSERT(!aURL.IsEmpty());
    101  AddURL(aURL, aFragment);
    102 }
    103 
    104 InternalRequest::InternalRequest(const InternalRequest& aOther,
    105                                 ConstructorGuard)
    106    : mMethod(aOther.mMethod),
    107      mURLList(aOther.mURLList.Clone()),
    108      mHeaders(new InternalHeaders(*aOther.mHeaders)),
    109      mTriggeringPrincipalOverride(aOther.mTriggeringPrincipalOverride),
    110      mNeverTaint(aOther.mNeverTaint),
    111      mBodyLength(InternalResponse::UNKNOWN_BODY_SIZE),
    112      mContentPolicyType(aOther.mContentPolicyType),
    113      mInternalPriority(aOther.mInternalPriority),
    114      mReferrer(aOther.mReferrer),
    115      mReferrerPolicy(aOther.mReferrerPolicy),
    116      mEnvironmentReferrerPolicy(aOther.mEnvironmentReferrerPolicy),
    117      mMode(aOther.mMode),
    118      mCredentialsMode(aOther.mCredentialsMode),
    119      mResponseTainting(aOther.mResponseTainting),
    120      mCacheMode(aOther.mCacheMode),
    121      mRedirectMode(aOther.mRedirectMode),
    122      mPriorityMode(aOther.mPriorityMode),
    123      mIntegrity(aOther.mIntegrity),
    124      mKeepalive(aOther.mKeepalive),
    125      mMozErrors(aOther.mMozErrors),
    126      mFragment(aOther.mFragment),
    127      mSkipServiceWorker(aOther.mSkipServiceWorker),
    128      mSkipWasmCaching(aOther.mSkipWasmCaching),
    129      mSynchronous(aOther.mSynchronous),
    130      mUnsafeRequest(aOther.mUnsafeRequest),
    131      mUseURLCredentials(aOther.mUseURLCredentials),
    132      mContentPolicyTypeOverridden(aOther.mContentPolicyTypeOverridden),
    133      mEmbedderPolicy(aOther.mEmbedderPolicy),
    134      mInterceptionContentPolicyType(aOther.mInterceptionContentPolicyType),
    135      mInterceptionRedirectChain(aOther.mInterceptionRedirectChain),
    136      mInterceptionFromThirdParty(aOther.mInterceptionFromThirdParty) {
    137  // NOTE: does not copy body stream... use the fallible Clone() for that
    138  if (aOther.GetInterceptionTriggeringPrincipalInfo()) {
    139    mInterceptionTriggeringPrincipalInfo =
    140        MakeUnique<mozilla::ipc::PrincipalInfo>(
    141            *(aOther.GetInterceptionTriggeringPrincipalInfo().get()));
    142  }
    143 }
    144 
    145 InternalRequest::InternalRequest(const IPCInternalRequest& aIPCRequest)
    146    : mMethod(aIPCRequest.method()),
    147      mURLList(aIPCRequest.urlList().Clone()),
    148      mHeaders(new InternalHeaders(aIPCRequest.headers(),
    149                                   aIPCRequest.headersGuard())),
    150      mBodyLength(aIPCRequest.bodySize()),
    151      mPreferredAlternativeDataType(aIPCRequest.preferredAlternativeDataType()),
    152      mContentPolicyType(aIPCRequest.contentPolicyType()),
    153      mInternalPriority(aIPCRequest.internalPriority()),
    154      mReferrer(aIPCRequest.referrer()),
    155      mReferrerPolicy(aIPCRequest.referrerPolicy()),
    156      mEnvironmentReferrerPolicy(aIPCRequest.environmentReferrerPolicy()),
    157      mMode(aIPCRequest.requestMode()),
    158      mCredentialsMode(aIPCRequest.requestCredentials()),
    159      mCacheMode(aIPCRequest.cacheMode()),
    160      mRedirectMode(aIPCRequest.requestRedirect()),
    161      mPriorityMode(aIPCRequest.requestPriority()),
    162      mIntegrity(aIPCRequest.integrity()),
    163      mKeepalive(aIPCRequest.keepalive()),
    164      mFragment(aIPCRequest.fragment()),
    165      mEmbedderPolicy(aIPCRequest.embedderPolicy()),
    166      mInterceptionContentPolicyType(
    167          aIPCRequest.interceptionContentPolicyType()),
    168      mInterceptionRedirectChain(aIPCRequest.interceptionRedirectChain()),
    169      mInterceptionFromThirdParty(aIPCRequest.interceptionFromThirdParty()) {
    170  if (aIPCRequest.principalInfo()) {
    171    mPrincipalInfo = MakeUnique<mozilla::ipc::PrincipalInfo>(
    172        aIPCRequest.principalInfo().ref());
    173  }
    174 
    175  if (aIPCRequest.interceptionTriggeringPrincipalInfo()) {
    176    mInterceptionTriggeringPrincipalInfo =
    177        MakeUnique<mozilla::ipc::PrincipalInfo>(
    178            aIPCRequest.interceptionTriggeringPrincipalInfo().ref());
    179  }
    180 
    181  const Maybe<BodyStreamVariant>& body = aIPCRequest.body();
    182 
    183  if (body) {
    184    if (body->type() == BodyStreamVariant::TParentToChildStream) {
    185      mBodyStream = body->get_ParentToChildStream().get_RemoteLazyInputStream();
    186    }
    187    if (body->type() == BodyStreamVariant::TChildToParentStream) {
    188      mBodyStream =
    189          DeserializeIPCStream(body->get_ChildToParentStream().stream());
    190    }
    191  }
    192 }
    193 
    194 void InternalRequest::ToIPCInternalRequest(
    195    IPCInternalRequest* aIPCRequest, mozilla::ipc::PBackgroundChild* aManager) {
    196  aIPCRequest->method() = mMethod;
    197  for (const auto& url : mURLList) {
    198    aIPCRequest->urlList().AppendElement(url);
    199  }
    200  mHeaders->ToIPC(aIPCRequest->headers(), aIPCRequest->headersGuard());
    201  aIPCRequest->bodySize() = mBodyLength;
    202  aIPCRequest->preferredAlternativeDataType() = mPreferredAlternativeDataType;
    203  aIPCRequest->contentPolicyType() = mContentPolicyType;
    204  aIPCRequest->internalPriority() = mInternalPriority;
    205  aIPCRequest->referrer() = mReferrer;
    206  aIPCRequest->referrerPolicy() = mReferrerPolicy;
    207  aIPCRequest->environmentReferrerPolicy() = mEnvironmentReferrerPolicy;
    208  aIPCRequest->requestMode() = mMode;
    209  aIPCRequest->requestCredentials() = mCredentialsMode;
    210  aIPCRequest->cacheMode() = mCacheMode;
    211  aIPCRequest->requestRedirect() = mRedirectMode;
    212  aIPCRequest->requestPriority() = mPriorityMode;
    213  aIPCRequest->integrity() = mIntegrity;
    214  aIPCRequest->keepalive() = mKeepalive;
    215  aIPCRequest->fragment() = mFragment;
    216  aIPCRequest->embedderPolicy() = mEmbedderPolicy;
    217 
    218  if (mPrincipalInfo) {
    219    aIPCRequest->principalInfo() = Some(*mPrincipalInfo);
    220  }
    221 
    222  if (mInterceptionTriggeringPrincipalInfo) {
    223    aIPCRequest->interceptionTriggeringPrincipalInfo() =
    224        Some(*mInterceptionTriggeringPrincipalInfo);
    225    aIPCRequest->interceptionContentPolicyType() =
    226        mInterceptionContentPolicyType;
    227    if (!mInterceptionRedirectChain.IsEmpty()) {
    228      aIPCRequest->interceptionRedirectChain().Assign(
    229          mInterceptionRedirectChain);
    230    }
    231    aIPCRequest->interceptionFromThirdParty() = mInterceptionFromThirdParty;
    232  }
    233 
    234  if (mBodyStream) {
    235    nsCOMPtr<nsIInputStream> body = mBodyStream;
    236    aIPCRequest->body().emplace(ChildToParentStream());
    237    DebugOnly<bool> ok = mozilla::ipc::SerializeIPCStream(
    238        body.forget(), aIPCRequest->body()->get_ChildToParentStream().stream(),
    239        /* aAllowLazy */ false);
    240    MOZ_ASSERT(ok);
    241  }
    242 }
    243 
    244 InternalRequest::~InternalRequest() = default;
    245 
    246 void InternalRequest::SetContentPolicyType(
    247    nsContentPolicyType aContentPolicyType) {
    248  mContentPolicyType = aContentPolicyType;
    249 }
    250 
    251 void InternalRequest::OverrideContentPolicyType(
    252    nsContentPolicyType aContentPolicyType) {
    253  SetContentPolicyType(aContentPolicyType);
    254  mContentPolicyTypeOverridden = true;
    255 }
    256 
    257 void InternalRequest::SetInterceptionContentPolicyType(
    258    nsContentPolicyType aContentPolicyType) {
    259  mInterceptionContentPolicyType = aContentPolicyType;
    260 }
    261 
    262 /* static */
    263 /* static */
    264 RequestDestination InternalRequest::MapContentPolicyTypeToRequestDestination(
    265    nsContentPolicyType aContentPolicyType) {
    266  switch (aContentPolicyType) {
    267    case nsIContentPolicy::TYPE_OTHER:
    268      return RequestDestination::_empty;
    269    case nsIContentPolicy::TYPE_INTERNAL_SCRIPT:
    270    case nsIContentPolicy::TYPE_INTERNAL_SCRIPT_PRELOAD:
    271    case nsIContentPolicy::TYPE_INTERNAL_MODULE:
    272    case nsIContentPolicy::TYPE_INTERNAL_MODULE_PRELOAD:
    273    case nsIContentPolicy::TYPE_INTERNAL_SERVICE_WORKER:
    274    case nsIContentPolicy::TYPE_INTERNAL_WORKER_IMPORT_SCRIPTS:
    275    case nsIContentPolicy::TYPE_INTERNAL_CHROMEUTILS_COMPILED_SCRIPT:
    276    case nsIContentPolicy::TYPE_INTERNAL_FRAME_MESSAGEMANAGER_SCRIPT:
    277    case nsIContentPolicy::TYPE_SCRIPT:
    278      return RequestDestination::Script;
    279    case nsIContentPolicy::TYPE_INTERNAL_WORKER:
    280    case nsIContentPolicy::TYPE_INTERNAL_WORKER_STATIC_MODULE:
    281      return RequestDestination::Worker;
    282    case nsIContentPolicy::TYPE_INTERNAL_SHARED_WORKER:
    283      return RequestDestination::Sharedworker;
    284    case nsIContentPolicy::TYPE_IMAGESET:
    285    case nsIContentPolicy::TYPE_INTERNAL_IMAGE:
    286    case nsIContentPolicy::TYPE_INTERNAL_IMAGE_PRELOAD:
    287    case nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON:
    288    case nsIContentPolicy::TYPE_IMAGE:
    289      return RequestDestination::Image;
    290    case nsIContentPolicy::TYPE_STYLESHEET:
    291    case nsIContentPolicy::TYPE_INTERNAL_STYLESHEET:
    292    case nsIContentPolicy::TYPE_INTERNAL_STYLESHEET_PRELOAD:
    293      return RequestDestination::Style;
    294    case nsIContentPolicy::TYPE_OBJECT:
    295    case nsIContentPolicy::TYPE_INTERNAL_OBJECT:
    296      return RequestDestination::Object;
    297    case nsIContentPolicy::TYPE_INTERNAL_EMBED:
    298      return RequestDestination::Embed;
    299    case nsIContentPolicy::TYPE_DOCUMENT:
    300      return RequestDestination::Document;
    301    case nsIContentPolicy::TYPE_SUBDOCUMENT:
    302    case nsIContentPolicy::TYPE_INTERNAL_IFRAME:
    303      return RequestDestination::Iframe;
    304    case nsIContentPolicy::TYPE_INTERNAL_FRAME:
    305      return RequestDestination::Frame;
    306    case nsIContentPolicy::TYPE_PING:
    307      return RequestDestination::_empty;
    308    case nsIContentPolicy::TYPE_XMLHTTPREQUEST:
    309    case nsIContentPolicy::TYPE_INTERNAL_XMLHTTPREQUEST_ASYNC:
    310    case nsIContentPolicy::TYPE_INTERNAL_XMLHTTPREQUEST_SYNC:
    311      return RequestDestination::_empty;
    312    case nsIContentPolicy::TYPE_INTERNAL_EVENTSOURCE:
    313      return RequestDestination::_empty;
    314    case nsIContentPolicy::TYPE_DTD:
    315    case nsIContentPolicy::TYPE_INTERNAL_DTD:
    316    case nsIContentPolicy::TYPE_INTERNAL_FORCE_ALLOWED_DTD:
    317      return RequestDestination::_empty;
    318    case nsIContentPolicy::TYPE_FONT:
    319    case nsIContentPolicy::TYPE_INTERNAL_FONT_PRELOAD:
    320    case nsIContentPolicy::TYPE_UA_FONT:
    321      return RequestDestination::Font;
    322    case nsIContentPolicy::TYPE_MEDIA:
    323      return RequestDestination::_empty;
    324    case nsIContentPolicy::TYPE_INTERNAL_AUDIO:
    325      return RequestDestination::Audio;
    326    case nsIContentPolicy::TYPE_INTERNAL_VIDEO:
    327      return RequestDestination::Video;
    328    case nsIContentPolicy::TYPE_INTERNAL_TRACK:
    329      return RequestDestination::Track;
    330    case nsIContentPolicy::TYPE_WEBSOCKET:
    331      return RequestDestination::_empty;
    332    case nsIContentPolicy::TYPE_CSP_REPORT:
    333      return RequestDestination::Report;
    334    case nsIContentPolicy::TYPE_XSLT:
    335      return RequestDestination::Xslt;
    336    case nsIContentPolicy::TYPE_BEACON:
    337      return RequestDestination::_empty;
    338    case nsIContentPolicy::TYPE_FETCH:
    339    case nsIContentPolicy::TYPE_INTERNAL_FETCH_PRELOAD:
    340      return RequestDestination::_empty;
    341    case nsIContentPolicy::TYPE_WEB_MANIFEST:
    342      return RequestDestination::Manifest;
    343    case nsIContentPolicy::TYPE_SAVEAS_DOWNLOAD:
    344      return RequestDestination::_empty;
    345    case nsIContentPolicy::TYPE_SPECULATIVE:
    346      return RequestDestination::_empty;
    347    case nsIContentPolicy::TYPE_INTERNAL_AUDIOWORKLET:
    348      return RequestDestination::Audioworklet;
    349    case nsIContentPolicy::TYPE_INTERNAL_PAINTWORKLET:
    350      return RequestDestination::Paintworklet;
    351    case nsIContentPolicy::TYPE_PROXIED_WEBRTC_MEDIA:
    352      return RequestDestination::_empty;
    353    case nsIContentPolicy::TYPE_WEB_IDENTITY:
    354      return RequestDestination::_empty;
    355    case nsIContentPolicy::TYPE_WEB_TRANSPORT:
    356      return RequestDestination::_empty;
    357    case nsIContentPolicy::TYPE_INTERNAL_EXTERNAL_RESOURCE:
    358      return RequestDestination::Image;
    359    case nsIContentPolicy::TYPE_JSON:
    360    case nsIContentPolicy::TYPE_INTERNAL_JSON_PRELOAD:
    361      return RequestDestination::Json;
    362    case nsIContentPolicy::TYPE_INVALID:
    363    case nsIContentPolicy::TYPE_END:
    364      break;
    365      // Do not add default: so that compilers can catch the missing case.
    366  }
    367 
    368  MOZ_ASSERT(false, "Unhandled nsContentPolicyType value");
    369  return RequestDestination::_empty;
    370 }
    371 
    372 /* static */
    373 RequestDestination InternalRequest::MapContentPolicyTypeToRequestDestination(
    374    ExtContentPolicyType aContentPolicyType) {
    375  switch (aContentPolicyType) {
    376    case ExtContentPolicyType::TYPE_INVALID:
    377    case ExtContentPolicyType::TYPE_OTHER:
    378      return RequestDestination::_empty;
    379    case ExtContentPolicyType::TYPE_SCRIPT:
    380      return RequestDestination::Script;
    381    case ExtContentPolicyType::TYPE_IMAGE:
    382      return RequestDestination::Image;
    383    case ExtContentPolicyType::TYPE_STYLESHEET:
    384      return RequestDestination::Style;
    385    case ExtContentPolicyType::TYPE_OBJECT:
    386      return RequestDestination::Object;
    387    case ExtContentPolicyType::TYPE_DOCUMENT:
    388      return RequestDestination::Document;
    389    case ExtContentPolicyType::TYPE_SUBDOCUMENT:
    390      return RequestDestination::Iframe;
    391    case ExtContentPolicyType::TYPE_PING:
    392    case ExtContentPolicyType::TYPE_XMLHTTPREQUEST:
    393    case ExtContentPolicyType::TYPE_DTD:
    394      return RequestDestination::_empty;
    395    case ExtContentPolicyType::TYPE_FONT:
    396      return RequestDestination::Font;
    397    case ExtContentPolicyType::TYPE_MEDIA:
    398    case ExtContentPolicyType::TYPE_WEBSOCKET:
    399      return RequestDestination::_empty;
    400    case ExtContentPolicyType::TYPE_CSP_REPORT:
    401      return RequestDestination::Report;
    402    case ExtContentPolicyType::TYPE_XSLT:
    403      return RequestDestination::Xslt;
    404    case ExtContentPolicyType::TYPE_BEACON:
    405    case ExtContentPolicyType::TYPE_FETCH:
    406      return RequestDestination::_empty;
    407    case ExtContentPolicyType::TYPE_IMAGESET:
    408      return RequestDestination::Image;
    409    case ExtContentPolicyType::TYPE_WEB_MANIFEST:
    410      return RequestDestination::Manifest;
    411    case ExtContentPolicyType::TYPE_SAVEAS_DOWNLOAD:
    412    case ExtContentPolicyType::TYPE_SPECULATIVE:
    413      return RequestDestination::_empty;
    414    case ExtContentPolicyType::TYPE_UA_FONT:
    415      return RequestDestination::Font;
    416    case ExtContentPolicyType::TYPE_PROXIED_WEBRTC_MEDIA:
    417    case ExtContentPolicyType::TYPE_WEB_IDENTITY:
    418    case ExtContentPolicyType::TYPE_WEB_TRANSPORT:
    419      return RequestDestination::_empty;
    420    case ExtContentPolicyType::TYPE_JSON:
    421      return RequestDestination::Json;
    422      // Do not add default: so that compilers can catch the missing case.
    423  }
    424 
    425  MOZ_ASSERT(false, "Unhandled ExContentPolicyType value");
    426  return RequestDestination::_empty;
    427 }
    428 
    429 // static
    430 bool InternalRequest::IsNavigationContentPolicy(
    431    nsContentPolicyType aContentPolicyType) {
    432  // https://fetch.spec.whatwg.org/#navigation-request-context
    433  //
    434  // A navigation request context is one of "form", "frame", "hyperlink",
    435  // "iframe", "internal" (as long as context frame type is not "none"),
    436  // "location", "metarefresh", and "prerender".
    437  //
    438  // Note, all of these request types are effectively initiated by nsDocShell.
    439  return aContentPolicyType == nsIContentPolicy::TYPE_DOCUMENT ||
    440         aContentPolicyType == nsIContentPolicy::TYPE_SUBDOCUMENT ||
    441         aContentPolicyType == nsIContentPolicy::TYPE_INTERNAL_FRAME ||
    442         aContentPolicyType == nsIContentPolicy::TYPE_INTERNAL_IFRAME;
    443 }
    444 
    445 // static
    446 bool InternalRequest::IsWorkerContentPolicy(
    447    nsContentPolicyType aContentPolicyType) {
    448  // https://fetch.spec.whatwg.org/#worker-request-context
    449  //
    450  // A worker request context is one of "serviceworker", "sharedworker", and
    451  // "worker".
    452  //
    453  // Note, service workers are not included here because currently there is
    454  // no way to generate a Request with a "serviceworker" RequestDestination.
    455  // ServiceWorker scripts cannot be intercepted.
    456  return aContentPolicyType == nsIContentPolicy::TYPE_INTERNAL_WORKER ||
    457         aContentPolicyType == nsIContentPolicy::TYPE_INTERNAL_SHARED_WORKER;
    458 }
    459 
    460 bool InternalRequest::IsNavigationRequest() const {
    461  return IsNavigationContentPolicy(mContentPolicyType);
    462 }
    463 
    464 bool InternalRequest::IsWorkerRequest() const {
    465  return IsWorkerContentPolicy(mContentPolicyType);
    466 }
    467 
    468 bool InternalRequest::IsClientRequest() const {
    469  return IsNavigationRequest() || IsWorkerRequest();
    470 }
    471 
    472 // static
    473 RequestMode InternalRequest::MapChannelToRequestMode(nsIChannel* aChannel) {
    474  MOZ_ASSERT(aChannel);
    475 
    476  nsCOMPtr<nsILoadInfo> loadInfo = aChannel->LoadInfo();
    477 
    478  nsContentPolicyType contentPolicy = loadInfo->InternalContentPolicyType();
    479  if (IsNavigationContentPolicy(contentPolicy)) {
    480    return RequestMode::Navigate;
    481  }
    482 
    483  // TODO: remove the worker override once securityMode is fully implemented
    484  // (bug 1189945)
    485  if (IsWorkerContentPolicy(contentPolicy)) {
    486    return RequestMode::Same_origin;
    487  }
    488 
    489  uint32_t securityMode = loadInfo->GetSecurityMode();
    490 
    491  switch (securityMode) {
    492    case nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_INHERITS_SEC_CONTEXT:
    493    case nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_IS_BLOCKED:
    494      return RequestMode::Same_origin;
    495    case nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_INHERITS_SEC_CONTEXT:
    496    case nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL:
    497      return RequestMode::No_cors;
    498    case nsILoadInfo::SEC_REQUIRE_CORS_INHERITS_SEC_CONTEXT:
    499      // TODO: Check additional flag force-preflight after bug 1199693 (bug
    500      // 1189945)
    501      return RequestMode::Cors;
    502    default:
    503      MOZ_ASSERT_UNREACHABLE("Unexpected security mode!");
    504      return RequestMode::Same_origin;
    505  }
    506 }
    507 
    508 // static
    509 RequestCredentials InternalRequest::MapChannelToRequestCredentials(
    510    nsIChannel* aChannel) {
    511  MOZ_ASSERT(aChannel);
    512 
    513  nsCOMPtr<nsILoadInfo> loadInfo = aChannel->LoadInfo();
    514 
    515  uint32_t cookiePolicy = loadInfo->GetCookiePolicy();
    516 
    517  if (cookiePolicy == nsILoadInfo::SEC_COOKIES_INCLUDE) {
    518    return RequestCredentials::Include;
    519  } else if (cookiePolicy == nsILoadInfo::SEC_COOKIES_OMIT) {
    520    return RequestCredentials::Omit;
    521  } else if (cookiePolicy == nsILoadInfo::SEC_COOKIES_SAME_ORIGIN) {
    522    return RequestCredentials::Same_origin;
    523  }
    524 
    525  MOZ_ASSERT_UNREACHABLE("Unexpected cookie policy!");
    526  return RequestCredentials::Same_origin;
    527 }
    528 
    529 void InternalRequest::MaybeSkipCacheIfPerformingRevalidation() {
    530  if (mCacheMode == RequestCache::Default &&
    531      mHeaders->HasRevalidationHeaders()) {
    532    mCacheMode = RequestCache::No_store;
    533  }
    534 }
    535 
    536 void InternalRequest::SetPrincipalInfo(
    537    UniquePtr<mozilla::ipc::PrincipalInfo> aPrincipalInfo) {
    538  mPrincipalInfo = std::move(aPrincipalInfo);
    539 }
    540 
    541 void InternalRequest::SetInterceptionTriggeringPrincipalInfo(
    542    UniquePtr<mozilla::ipc::PrincipalInfo> aPrincipalInfo) {
    543  mInterceptionTriggeringPrincipalInfo = std::move(aPrincipalInfo);
    544 }
    545 }  // namespace mozilla::dom