diff --git a/dom/fetch/Fetch.cpp b/dom/fetch/Fetch.cpp index 25dc470f3796..97c4c82fca18 100644 --- a/dom/fetch/Fetch.cpp +++ b/dom/fetch/Fetch.cpp @@ -503,7 +503,7 @@ already_AddRefed FetchRequest(nsIGlobalObject* aGlobal, return nullptr; } - SafeRefPtr internalRequest = request->GetInternalRequest(); + SafeRefPtr r = request->GetInternalRequest(); // Restore information of InterceptedHttpChannel if they are passed with the // Request. Since Request::Constructor would not copy these members. @@ -511,16 +511,16 @@ already_AddRefed FetchRequest(nsIGlobalObject* aGlobal, RefPtr inputReq = &aInput.GetAsRequest(); SafeRefPtr inputInReq = inputReq->GetInternalRequest(); if (inputInReq->GetInterceptionTriggeringPrincipalInfo()) { - internalRequest->SetInterceptionContentPolicyType( + r->SetInterceptionContentPolicyType( inputInReq->InterceptionContentPolicyType()); - internalRequest->SetInterceptionTriggeringPrincipalInfo( + r->SetInterceptionTriggeringPrincipalInfo( MakeUnique( *(inputInReq->GetInterceptionTriggeringPrincipalInfo().get()))); if (!inputInReq->InterceptionRedirectChain().IsEmpty()) { - internalRequest->SetInterceptionRedirectChain( + r->SetInterceptionRedirectChain( inputInReq->InterceptionRedirectChain()); } - internalRequest->SetInterceptionFromThirdParty( + r->SetInterceptionFromThirdParty( inputInReq->InterceptionFromThirdParty()); } } @@ -541,7 +541,7 @@ already_AddRefed FetchRequest(nsIGlobalObject* aGlobal, JS::Realm* realm = JS::GetCurrentRealmOrNull(cx); if (realm && JS::GetDebuggerObservesWasm(realm)) { - internalRequest->SetSkipWasmCaching(); + r->SetSkipWasmCaching(); } RefPtr observer; @@ -550,7 +550,7 @@ already_AddRefed FetchRequest(nsIGlobalObject* aGlobal, aInit.mObserve.Value().HandleEvent(*observer); } - if (NS_IsMainThread() && !internalRequest->GetKeepalive()) { + if (NS_IsMainThread()) { nsCOMPtr window = do_QueryInterface(aGlobal); nsCOMPtr doc; nsCOMPtr loadGroup; @@ -588,92 +588,22 @@ already_AddRefed FetchRequest(nsIGlobalObject* aGlobal, RefPtr resolver = new MainThreadFetchResolver( p, observer, signalImpl, request->MozErrors()); - RefPtr fetch = - new FetchDriver(std::move(internalRequest), principal, loadGroup, - aGlobal->SerialEventTarget(), cookieJarSettings, - nullptr, // PerformanceStorage - isTrackingFetch); + RefPtr fetch = new FetchDriver( + std::move(r), principal, loadGroup, aGlobal->SerialEventTarget(), + cookieJarSettings, nullptr, // PerformanceStorage + isTrackingFetch); fetch->SetDocument(doc); resolver->SetLoadGroup(loadGroup); aRv = fetch->Fetch(signalImpl, resolver); if (NS_WARN_IF(aRv.Failed())) { return nullptr; } - } else if (NS_IsMainThread() && internalRequest->GetKeepalive()) { - // keepalive is set to true, route the request through PFetch - // We plan to route all main-thread fetch request through PFetch. - // See Bug 1897129. - RefPtr actor = - FetchChild::CreateForMainThread(p, signalImpl, observer); - if (!actor) { - NS_WARNING("Could not start keepalive request."); - aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR); - return nullptr; - } - - Maybe clientInfo(aGlobal->GetClientInfo()); - if (clientInfo.isNothing()) { - aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR); - return nullptr; - } - - auto* backgroundChild = - mozilla::ipc::BackgroundChild::GetOrCreateForCurrentThread(); - Unused << NS_WARN_IF(!backgroundChild->SendPFetchConstructor(actor)); - - FetchOpArgs ipcArgs; - - ipcArgs.request() = IPCInternalRequest(); - internalRequest->ToIPCInternalRequest(&(ipcArgs.request()), - backgroundChild); - - ipcArgs.clientInfo() = clientInfo.ref().ToIPC(); - nsCOMPtr window = do_QueryInterface(aGlobal); - nsCOMPtr doc; - nsCOMPtr cookieJarSettings; - nsIPrincipal* principal; - // we don't check if we this request is invoked from a tracking script - // we might add this capability in future. - // See Bug 1892406 - if (window) { - doc = window->GetExtantDoc(); - if (!doc) { - aRv.Throw(NS_ERROR_FAILURE); - return nullptr; - } - principal = doc->NodePrincipal(); - cookieJarSettings = doc->CookieJarSettings(); - - } else { - principal = aGlobal->PrincipalOrNull(); - if (NS_WARN_IF(!principal)) { - aRv.Throw(NS_ERROR_FAILURE); - return nullptr; - } - cookieJarSettings = mozilla::net::CookieJarSettings::Create(principal); - } - - if (cookieJarSettings) { - net::CookieJarSettingsArgs csArgs; - net::CookieJarSettings::Cast(cookieJarSettings)->Serialize(csArgs); - ipcArgs.cookieJarSettings() = Some(csArgs); - } - - nsresult rv = PrincipalToPrincipalInfo(principal, &ipcArgs.principalInfo()); - NS_ENSURE_SUCCESS(rv, nullptr); - - ipcArgs.hasCSPEventListener() = false; - ipcArgs.isWorkerRequest() = false; - - actor->DoFetchOp(ipcArgs); - - return p.forget(); } else { WorkerPrivate* worker = GetCurrentThreadWorkerPrivate(); MOZ_ASSERT(worker); if (worker->IsServiceWorker()) { - internalRequest->SetSkipServiceWorker(); + r->SetSkipServiceWorker(); } // PFetch gives no benefit for the fetch in the parent process. @@ -681,7 +611,7 @@ already_AddRefed FetchRequest(nsIGlobalObject* aGlobal, // For child process, dispatch fetch op to the parent. if (StaticPrefs::dom_workers_pFetch_enabled() && !XRE_IsParentProcess()) { RefPtr actor = - FetchChild::CreateForWorker(worker, p, signalImpl, observer); + FetchChild::Create(worker, p, signalImpl, observer); if (!actor) { NS_WARNING("Could not keep the worker alive."); aRv.Throw(NS_ERROR_DOM_ABORT_ERR); @@ -699,9 +629,9 @@ already_AddRefed FetchRequest(nsIGlobalObject* aGlobal, Unused << NS_WARN_IF(!backgroundChild->SendPFetchConstructor(actor)); FetchOpArgs ipcArgs; + ipcArgs.request() = IPCInternalRequest(); - internalRequest->ToIPCInternalRequest(&(ipcArgs.request()), - backgroundChild); + r->ToIPCInternalRequest(&(ipcArgs.request()), backgroundChild); ipcArgs.principalInfo() = worker->GetPrincipalInfo(); ipcArgs.clientInfo() = clientInfo.ref().ToIPC(); @@ -733,15 +663,11 @@ already_AddRefed FetchRequest(nsIGlobalObject* aGlobal, ipcArgs.isThirdPartyContext() = worker->IsThirdPartyContext(); - ipcArgs.isWorkerRequest() = true; - actor->DoFetchOp(ipcArgs); return p.forget(); } - // Dispatch worker fetch to the main thread - // We do not check if keepalive flag is set for ChromeWorkers - // See Bug 1898664 + RefPtr resolver = WorkerFetchResolver::Create(worker, p, signalImpl, observer); if (!resolver) { @@ -763,8 +689,7 @@ already_AddRefed FetchRequest(nsIGlobalObject* aGlobal, RefPtr run = new MainThreadFetchRunnable( resolver, clientInfo.ref(), worker->GlobalScope()->GetController(), - worker->CSPEventListener(), std::move(internalRequest), - std::move(stack)); + worker->CSPEventListener(), std::move(r), std::move(stack)); worker->DispatchToMainThread(run.forget()); } diff --git a/dom/fetch/FetchChild.cpp b/dom/fetch/FetchChild.cpp index 64a242df37a8..eff5b3ff3a2e 100644 --- a/dom/fetch/FetchChild.cpp +++ b/dom/fetch/FetchChild.cpp @@ -38,12 +38,8 @@ mozilla::ipc::IPCResult FetchChild::Recv__delete__(const nsresult&& aResult) { } // Shutdown has not been called, so mWorkerRef->Private() should be still // alive. - if (mWorkerRef) { - MOZ_ASSERT(mWorkerRef->Private()); - mWorkerRef->Private()->AssertIsOnWorkerThread(); - } else { - MOZ_ASSERT(mIsKeepAliveRequest); - } + MOZ_ASSERT(mWorkerRef->Private()); + mWorkerRef->Private()->AssertIsOnWorkerThread(); if (mPromise->State() == Promise::PromiseState::Pending) { if (NS_FAILED(aResult)) { @@ -69,11 +65,8 @@ mozilla::ipc::IPCResult FetchChild::RecvOnResponseAvailableInternal( } // Shutdown has not been called, so mWorkerRef->Private() should be still // alive. - if (mWorkerRef) { - MOZ_ASSERT(mWorkerRef->Private()); - mWorkerRef->Private()->AssertIsOnWorkerThread(); - } - + MOZ_ASSERT(mWorkerRef->Private()); + mWorkerRef->Private()->AssertIsOnWorkerThread(); SafeRefPtr internalResponse = InternalResponse::FromIPC(aResponse); IgnoredErrorResult result; @@ -89,8 +82,7 @@ mozilla::ipc::IPCResult FetchChild::RecvOnResponseAvailableInternal( mFetchObserver->SetState(FetchState::Complete); } nsCOMPtr global; - // global = mWorkerRef->Private()->GlobalScope(); - global = mPromise->GetGlobalObject(); + global = mWorkerRef->Private()->GlobalScope(); RefPtr response = new Response(global, internalResponse.clonePtr(), mSignalImpl); mPromise->MaybeResolve(response); @@ -116,10 +108,8 @@ mozilla::ipc::IPCResult FetchChild::RecvOnResponseEnd(ResponseEndArgs&& aArgs) { } // Shutdown has not been called, so mWorkerRef->Private() should be still // alive. - if (mWorkerRef) { - MOZ_ASSERT(mWorkerRef->Private()); - mWorkerRef->Private()->AssertIsOnWorkerThread(); - } + MOZ_ASSERT(mWorkerRef->Private()); + mWorkerRef->Private()->AssertIsOnWorkerThread(); if (aArgs.endReason() == FetchDriverObserver::eAborted) { FETCH_LOG( @@ -141,10 +131,8 @@ mozilla::ipc::IPCResult FetchChild::RecvOnDataAvailable() { } // Shutdown has not been called, so mWorkerRef->Private() should be still // alive. - if (mWorkerRef) { - MOZ_ASSERT(mWorkerRef->Private()); - mWorkerRef->Private()->AssertIsOnWorkerThread(); - } + MOZ_ASSERT(mWorkerRef->Private()); + mWorkerRef->Private()->AssertIsOnWorkerThread(); if (mFetchObserver && mFetchObserver->State() == FetchState::Requesting) { mFetchObserver->SetState(FetchState::Responding); @@ -158,36 +146,11 @@ mozilla::ipc::IPCResult FetchChild::RecvOnFlushConsoleReport( if (mIsShutdown) { return IPC_OK(); } - MOZ_ASSERT(mReporter); - - if (NS_IsMainThread()) { - MOZ_ASSERT(mIsKeepAliveRequest); - // extract doc object to flush the console report - for (const auto& report : aReports) { - mReporter->AddConsoleReport( - report.errorFlags(), report.category(), - static_cast(report.propertiesFile()), - report.sourceFileURI(), report.lineNumber(), report.columnNumber(), - report.messageName(), report.stringParams()); - } - - MOZ_ASSERT(mPromise); - nsCOMPtr window = - do_QueryInterface(mPromise->GetGlobalObject()); - if (window) { - RefPtr doc = window->GetExtantDoc(); - mReporter->FlushConsoleReports(doc); - } else { - mReporter->FlushReportsToConsole(0); - } - return IPC_OK(); - } // Shutdown has not been called, so mWorkerRef->Private() should be still // alive. - if (mWorkerRef) { - MOZ_ASSERT(mWorkerRef->Private()); - mWorkerRef->Private()->AssertIsOnWorkerThread(); - } + MOZ_ASSERT(mWorkerRef->Private()); + mWorkerRef->Private()->AssertIsOnWorkerThread(); + MOZ_ASSERT(mReporter); RefPtr workerRef = mWorkerRef; nsCOMPtr reporter = mReporter; @@ -223,9 +186,10 @@ mozilla::ipc::IPCResult FetchChild::RecvOnFlushConsoleReport( return IPC_OK(); } -RefPtr FetchChild::CreateForWorker( - WorkerPrivate* aWorkerPrivate, RefPtr aPromise, - RefPtr aSignalImpl, RefPtr aObserver) { +RefPtr FetchChild::Create(WorkerPrivate* aWorkerPrivate, + RefPtr aPromise, + RefPtr aSignalImpl, + RefPtr aObserver) { MOZ_DIAGNOSTIC_ASSERT(aWorkerPrivate); aWorkerPrivate->AssertIsOnWorkerThread(); @@ -248,15 +212,6 @@ RefPtr FetchChild::CreateForWorker( return actor; } -RefPtr FetchChild::CreateForMainThread( - RefPtr aPromise, RefPtr aSignalImpl, - RefPtr aObserver) { - RefPtr actor = MakeRefPtr( - std::move(aPromise), std::move(aSignalImpl), std::move(aObserver)); - actor->mIsKeepAliveRequest = true; - return actor; -} - mozilla::ipc::IPCResult FetchChild::RecvOnCSPViolationEvent( const nsAString& aJSON) { FETCH_LOG(("FetchChild::RecvOnCSPViolationEvent [%p] aJSON: %s\n", this, @@ -306,28 +261,15 @@ mozilla::ipc::IPCResult FetchChild::RecvOnReportPerformanceTiming( } // Shutdown has not been called, so mWorkerRef->Private() should be still // alive. - if (mWorkerRef) { - MOZ_ASSERT(mWorkerRef->Private()); - mWorkerRef->Private()->AssertIsOnWorkerThread(); + MOZ_ASSERT(mWorkerRef->Private()); + mWorkerRef->Private()->AssertIsOnWorkerThread(); - RefPtr performanceStorage = - mWorkerRef->Private()->GetPerformanceStorage(); - if (performanceStorage) { - performanceStorage->AddEntry( - aTiming.entryName(), aTiming.initiatorType(), - MakeUnique(aTiming.timingData())); - } - } else if (mIsKeepAliveRequest) { - MOZ_ASSERT(mPromise->GetGlobalObject()); - auto* innerWindow = mPromise->GetGlobalObject()->GetAsInnerWindow(); - if (innerWindow) { - mozilla::dom::Performance* performance = innerWindow->GetPerformance(); - if (performance) { - performance->AsPerformanceStorage()->AddEntry( - aTiming.entryName(), aTiming.initiatorType(), - MakeUnique(aTiming.timingData())); - } - } + RefPtr performanceStorage = + mWorkerRef->Private()->GetPerformanceStorage(); + if (performanceStorage) { + performanceStorage->AddEntry( + aTiming.entryName(), aTiming.initiatorType(), + MakeUnique(aTiming.timingData())); } return IPC_OK(); } @@ -341,32 +283,27 @@ mozilla::ipc::IPCResult FetchChild::RecvOnNotifyNetworkMonitorAlternateStack( } // Shutdown has not been called, so mWorkerRef->Private() should be still // alive. - if (mWorkerRef) { - MOZ_ASSERT(mWorkerRef->Private()); - mWorkerRef->Private()->AssertIsOnWorkerThread(); + MOZ_ASSERT(mWorkerRef->Private()); + mWorkerRef->Private()->AssertIsOnWorkerThread(); - if (!mOriginStack) { - return IPC_OK(); - } - - if (!mWorkerChannelInfo) { - mWorkerChannelInfo = MakeRefPtr( - aChannelID, mWorkerRef->Private()->AssociatedBrowsingContextID()); - } - - // Unfortunately, SerializedStackHolder can only be read on the main thread. - // However, it doesn't block the fetch execution. - nsCOMPtr r = NS_NewRunnableFunction( - __func__, [channel = mWorkerChannelInfo, - stack = std::move(mOriginStack)]() mutable { - NotifyNetworkMonitorAlternateStack(channel, std::move(stack)); - }); - - MOZ_ALWAYS_SUCCEEDS(SchedulerGroup::Dispatch(r.forget())); + if (!mOriginStack) { + return IPC_OK(); } - // Currently we only support sending notifications for worker-thread initiated - // Fetch requests. We need to extend this to main-thread fetch requests as - // well. See Bug 1897424. + + if (!mWorkerChannelInfo) { + mWorkerChannelInfo = MakeRefPtr( + aChannelID, mWorkerRef->Private()->AssociatedBrowsingContextID()); + } + + // Unfortunately, SerializedStackHolder can only be read on the main thread. + // However, it doesn't block the fetch execution. + nsCOMPtr r = NS_NewRunnableFunction( + __func__, [channel = mWorkerChannelInfo, + stack = std::move(mOriginStack)]() mutable { + NotifyNetworkMonitorAlternateStack(channel, std::move(stack)); + }); + + MOZ_ALWAYS_SUCCEEDS(SchedulerGroup::Dispatch(r.forget())); return IPC_OK(); } @@ -391,7 +328,7 @@ void FetchChild::RunAbortAlgorithm() { if (mIsShutdown) { return; } - if (mWorkerRef || mIsKeepAliveRequest) { + if (mWorkerRef) { Unused << SendAbortFetchOp(); } } @@ -424,25 +361,8 @@ void FetchChild::Shutdown() { Unfollow(); mSignalImpl = nullptr; mCSPEventListener = nullptr; - // TODO - // For workers we need to skip aborting the fetch requests if keepalive is set - // This is just a quick fix for Worker. - // Usually, we want FetchChild to get destroyed while FetchParent calls - // Senddelete(). When Worker shutdown, FetchChild must call - // FetchChild::SendAbortFetchOp() to parent, and let FetchParent decide if - // canceling the underlying fetch() or not. But currently, we have no good way - // to distinguish whether the abort is intent by script or by Worker/Window - // shutdown. So, we provide a quick fix here, which makes - // FetchChild/FetchParent live a bit longer, but corresponding resources are - // released in FetchChild::Shutdown(), so this quick fix should not cause any - // leaking. - // And we will fix it in Bug 1901082 - if (!mIsKeepAliveRequest) { - Unused << SendAbortFetchOp(); - } - + Unused << SendAbortFetchOp(); mWorkerRef = nullptr; - mIsKeepAliveRequest = false; } void FetchChild::ActorDestroy(ActorDestroyReason aReason) { @@ -452,7 +372,6 @@ void FetchChild::ActorDestroy(ActorDestroyReason aReason) { mSignalImpl = nullptr; mCSPEventListener = nullptr; mWorkerRef = nullptr; - mIsKeepAliveRequest = false; } } // namespace mozilla::dom diff --git a/dom/fetch/FetchChild.h b/dom/fetch/FetchChild.h index c74b7a4ba566..986fc728cba6 100644 --- a/dom/fetch/FetchChild.h +++ b/dom/fetch/FetchChild.h @@ -50,16 +50,10 @@ class FetchChild final : public PFetchChild, public AbortFollower { void SetCSPEventListener(nsICSPEventListener* aListener); - // Creates the actor for worker fetch requests - static RefPtr CreateForWorker(WorkerPrivate* aWorkerPrivate, - RefPtr aPromise, - RefPtr aSignalImpl, - RefPtr aObserver); - - // Creates the actor for main thread fetch requests - static RefPtr CreateForMainThread( - RefPtr aPromise, RefPtr aSignalImpl, - RefPtr aObserver); + static RefPtr Create(WorkerPrivate* aWorkerPrivate, + RefPtr aPromise, + RefPtr aSignalImpl, + RefPtr aObserver); FetchChild(RefPtr&& aPromise, RefPtr&& aSignalImpl, RefPtr&& aObserver); @@ -82,7 +76,6 @@ class FetchChild final : public PFetchChild, public AbortFollower { void ActorDestroy(ActorDestroyReason aReason) override; RefPtr mWorkerRef; - bool mIsKeepAliveRequest; RefPtr mPromise; RefPtr mSignalImpl; RefPtr mFetchObserver; diff --git a/dom/fetch/FetchParent.cpp b/dom/fetch/FetchParent.cpp index 31f56f5d9df8..2784792639c1 100644 --- a/dom/fetch/FetchParent.cpp +++ b/dom/fetch/FetchParent.cpp @@ -94,7 +94,6 @@ IPCResult FetchParent::RecvFetchOp(FetchOpArgs&& aArgs) { } mRequest = MakeSafeRefPtr(std::move(aArgs.request())); - mIsWorkerFetch = aArgs.isWorkerRequest(); mPrincipalInfo = std::move(aArgs.principalInfo()); mWorkerScript = aArgs.workerScript(); mClientInfo = Some(ClientInfo(aArgs.clientInfo())); @@ -168,26 +167,15 @@ IPCResult FetchParent::RecvFetchOp(FetchOpArgs&& aArgs) { } RefPtr fetchService = FetchService::GetInstance(); MOZ_ASSERT(fetchService); - MOZ_ASSERT(self->mRequest); MOZ_ASSERT(!self->mResponsePromises); - if (self->mIsWorkerFetch) { - self->mResponsePromises = - fetchService->Fetch(AsVariant(FetchService::WorkerFetchArgs( - {self->mRequest.clonePtr(), self->mPrincipalInfo, - self->mWorkerScript, self->mClientInfo, self->mController, - self->mCookieJarSettings, self->mNeedOnDataAvailable, - self->mCSPEventListener, self->mAssociatedBrowsingContextID, - self->mBackgroundEventTarget, self->mID, - self->mIsThirdPartyContext}))); - } else { - MOZ_ASSERT(self->mRequest->GetKeepalive()); - self->mResponsePromises = - fetchService->Fetch(AsVariant(FetchService::MainThreadFetchArgs( - {self->mRequest.clonePtr(), self->mPrincipalInfo, - self->mCookieJarSettings, self->mNeedOnDataAvailable, - self->mCSPEventListener, self->mAssociatedBrowsingContextID, - self->mBackgroundEventTarget, self->mID}))); - } + self->mResponsePromises = + fetchService->Fetch(AsVariant(FetchService::WorkerFetchArgs( + {self->mRequest.clonePtr(), self->mPrincipalInfo, + self->mWorkerScript, self->mClientInfo, self->mController, + self->mCookieJarSettings, self->mNeedOnDataAvailable, + self->mCSPEventListener, self->mAssociatedBrowsingContextID, + self->mBackgroundEventTarget, self->mID, + self->mIsThirdPartyContext}))); self->mResponsePromises->GetResponseEndPromise()->Then( GetMainThreadSerialEventTarget(), __func__, @@ -332,17 +320,9 @@ void FetchParent::ActorDestroy(ActorDestroyReason aReason) { entry.Remove(); FETCH_LOG(("FetchParent::ActorDestroy entry [%p] removed", this)); } - // mRequest can be null when FetchParent has not yet received RecvFetchOp() - if (!mRequest) { - return; - } // Force to abort the existing fetch. // Actor can be destoried by shutdown when still fetching. - if (mRequest->GetKeepalive()) { - FETCH_LOG(("Skip aborting fetch as the request is marked keepalive")); - } else { - RecvAbortFetchOp(); - } + RecvAbortFetchOp(); // mBackgroundEventTarget = nullptr; } diff --git a/dom/fetch/FetchParent.h b/dom/fetch/FetchParent.h index 244822966964..59018e8cde6c 100644 --- a/dom/fetch/FetchParent.h +++ b/dom/fetch/FetchParent.h @@ -98,7 +98,6 @@ class FetchParent final : public PFetchParent { bool mExtendForCSPEventListener{false}; uint64_t mAssociatedBrowsingContextID{0}; bool mIsThirdPartyContext{true}; - bool mIsWorkerFetch{false}; Atomic mIsDone{false}; Atomic mActorDestroyed{false}; diff --git a/dom/fetch/FetchService.cpp b/dom/fetch/FetchService.cpp index eb27aa7792d6..9af967d6af7d 100644 --- a/dom/fetch/FetchService.cpp +++ b/dom/fetch/FetchService.cpp @@ -118,7 +118,6 @@ nsresult FetchService::FetchInstance::Initialize(FetchArgs&& aArgs) { // Get needed information for FetchDriver from passed-in channel. if (mArgs.is()) { mRequest = mArgs.as().mRequest.clonePtr(); - mArgsType = FetchArgsType::NavigationPreload; nsIChannel* channel = mArgs.as().mChannel; FETCH_LOG(("FetchInstance::Initialize [%p] request[%p], channel[%p]", this, mRequest.unsafeGetRawPtr(), channel)); @@ -164,36 +163,9 @@ nsresult FetchService::FetchInstance::Initialize(FetchArgs&& aArgs) { // Get PerformanceStorage from channel mPerformanceStorage = loadInfo->GetPerformanceStorage(); - } else if (mArgs.is()) { - mArgsType = FetchArgsType::MainThreadFetch; - - mRequest = mArgs.as().mRequest.clonePtr(); - - FETCH_LOG(("FetchInstance::Initialize [%p] request[%p]", this, - mRequest.unsafeGetRawPtr())); - - auto principalOrErr = PrincipalInfoToPrincipal( - mArgs.as().mPrincipalInfo); - if (principalOrErr.isErr()) { - return principalOrErr.unwrapErr(); - } - mPrincipal = principalOrErr.unwrap(); - nsresult rv = NS_NewLoadGroup(getter_AddRefs(mLoadGroup), mPrincipal); - if (NS_WARN_IF(NS_FAILED(rv))) { - return rv; - } - - if (mArgs.as().mCookieJarSettings.isSome()) { - net::CookieJarSettings::Deserialize( - mArgs.as().mCookieJarSettings.ref(), - getter_AddRefs(mCookieJarSettings)); - } - - return NS_OK; - } else { + mIsWorkerFetch = true; mRequest = mArgs.as().mRequest.clonePtr(); - mArgsType = FetchArgsType::WorkerFetch; FETCH_LOG(("FetchInstance::Initialize [%p] request[%p]", this, mRequest.unsafeGetRawPtr())); @@ -243,14 +215,10 @@ RefPtr FetchService::FetchInstance::Fetch() { GetMainThreadSerialEventTarget(), // MainThreadEventTarget mCookieJarSettings, // CookieJarSettings mPerformanceStorage, // PerformanceStorage - // For service workers we set - // tracking fetch to false, but for Keepalive - // requests from main thread this needs to be - // changed. See Bug 1892406 - false // IsTrackingFetch + false // IsTrackingFetch ); - if (mArgsType == FetchArgsType::WorkerFetch) { + if (mIsWorkerFetch) { auto& args = mArgs.as(); mFetchDriver->SetWorkerScript(args.mWorkerScript); MOZ_ASSERT(args.mClientInfo.isSome()); @@ -265,6 +233,7 @@ RefPtr FetchService::FetchInstance::Fetch() { } mFetchDriver->EnableNetworkInterceptControl(); + mPromises = MakeRefPtr(); // Call FetchDriver::Fetch to start fetching. @@ -314,20 +283,19 @@ void FetchService::FetchInstance::OnResponseEnd( FETCH_LOG(("FetchInstance::OnResponseEnd [%p] %s", this, aReason == eAborted ? "eAborted" : "eNetworking")); - MOZ_ASSERT(mRequest); - if (mArgsType != FetchArgsType::NavigationPreload) { + if (mIsWorkerFetch) { FlushConsoleReport(); nsCOMPtr r = NS_NewRunnableFunction( - __func__, - [endArgs = ResponseEndArgs(aReason), actorID = GetActorID()]() { + __func__, [endArgs = ResponseEndArgs(aReason), + actorID = mArgs.as().mActorID]() { FETCH_LOG(("FetchInstance::OnResponseEnd, Runnable")); RefPtr actor = FetchParent::GetActorByID(actorID); if (actor) { actor->OnResponseEnd(std::move(endArgs)); } }); - MOZ_ALWAYS_SUCCEEDS( - GetBackgroundEventTarget()->Dispatch(r, nsIThread::DISPATCH_NORMAL)); + MOZ_ALWAYS_SUCCEEDS(mArgs.as().mEventTarget->Dispatch( + r, nsIThread::DISPATCH_NORMAL)); } MOZ_ASSERT(mPromises); @@ -379,20 +347,19 @@ void FetchService::FetchInstance::OnResponseAvailableInternal( FETCH_LOG( ("FetchInstance::OnResponseAvailableInternal [%p] response body: %p", this, body.get())); - MOZ_ASSERT(mRequest); - if (mArgsType != FetchArgsType::NavigationPreload) { + if (mIsWorkerFetch) { nsCOMPtr r = NS_NewRunnableFunction( - __func__, - [response = mResponse.clonePtr(), actorID = GetActorID()]() mutable { + __func__, [response = mResponse.clonePtr(), + actorID = mArgs.as().mActorID]() mutable { FETCH_LOG(("FetchInstance::OnResponseAvailableInternal Runnable")); RefPtr actor = FetchParent::GetActorByID(actorID); if (actor) { actor->OnResponseAvailableInternal(std::move(response)); } }); - MOZ_ALWAYS_SUCCEEDS( - GetBackgroundEventTarget()->Dispatch(r, nsIThread::DISPATCH_NORMAL)); + MOZ_ALWAYS_SUCCEEDS(mArgs.as().mEventTarget->Dispatch( + r, nsIThread::DISPATCH_NORMAL)); } MOZ_ASSERT(mPromises); @@ -405,11 +372,6 @@ bool FetchService::FetchInstance::NeedOnDataAvailable() { if (mArgs.is()) { return mArgs.as().mNeedOnDataAvailable; } - - if (mArgs.is()) { - return mArgs.as().mNeedOnDataAvailable; - } - return false; } @@ -420,42 +382,40 @@ void FetchService::FetchInstance::OnDataAvailable() { return; } - MOZ_ASSERT(mRequest); - - if (mArgsType != FetchArgsType::NavigationPreload) { - nsCOMPtr r = - NS_NewRunnableFunction(__func__, [actorID = GetActorID()]() { + if (mIsWorkerFetch) { + nsCOMPtr r = NS_NewRunnableFunction( + __func__, [actorID = mArgs.as().mActorID]() { FETCH_LOG(("FetchInstance::OnDataAvailable, Runnable")); RefPtr actor = FetchParent::GetActorByID(actorID); if (actor) { actor->OnDataAvailable(); } }); - MOZ_ALWAYS_SUCCEEDS( - GetBackgroundEventTarget()->Dispatch(r, nsIThread::DISPATCH_NORMAL)); + MOZ_ALWAYS_SUCCEEDS(mArgs.as().mEventTarget->Dispatch( + r, nsIThread::DISPATCH_NORMAL)); } } void FetchService::FetchInstance::FlushConsoleReport() { FETCH_LOG(("FetchInstance::FlushConsoleReport [%p]", this)); - if (mArgsType != FetchArgsType::NavigationPreload) { + if (mIsWorkerFetch) { if (!mReporter) { return; } nsTArray reports; mReporter->StealConsoleReports(reports); nsCOMPtr r = NS_NewRunnableFunction( - __func__, - [actorID = GetActorID(), consoleReports = std::move(reports)]() { + __func__, [actorID = mArgs.as().mActorID, + consoleReports = std::move(reports)]() { FETCH_LOG(("FetchInstance::FlushConsolReport, Runnable")); RefPtr actor = FetchParent::GetActorByID(actorID); if (actor) { actor->OnFlushConsoleReport(std::move(consoleReports)); } }); - MOZ_ALWAYS_SUCCEEDS( - GetBackgroundEventTarget()->Dispatch(r, nsIThread::DISPATCH_NORMAL)); + MOZ_ALWAYS_SUCCEEDS(mArgs.as().mEventTarget->Dispatch( + r, nsIThread::DISPATCH_NORMAL)); } } @@ -480,19 +440,20 @@ void FetchService::FetchInstance::OnReportPerformanceTiming() { } timing.timingData() = performanceTiming->ToIPC(); // Force replace initiatorType for ServiceWorkerNavgationPreload. - if (mArgsType == FetchArgsType::NavigationPreload) { + if (!mIsWorkerFetch) { timing.initiatorType() = u"navigation"_ns; } else { nsCOMPtr r = NS_NewRunnableFunction( - __func__, [actorID = GetActorID(), timing = timing]() { + __func__, + [actorID = mArgs.as().mActorID, timing = timing]() { FETCH_LOG(("FetchInstance::OnReportPerformanceTiming, Runnable")); RefPtr actor = FetchParent::GetActorByID(actorID); if (actor) { actor->OnReportPerformanceTiming(std::move(timing)); } }); - MOZ_ALWAYS_SUCCEEDS( - GetBackgroundEventTarget()->Dispatch(r, nsIThread::DISPATCH_NORMAL)); + MOZ_ALWAYS_SUCCEEDS(mArgs.as().mEventTarget->Dispatch( + r, nsIThread::DISPATCH_NORMAL)); } mPromises->ResolveResponseTimingPromise(std::move(timing), __func__); @@ -503,9 +464,7 @@ void FetchService::FetchInstance::OnNotifyNetworkMonitorAlternateStack( FETCH_LOG(("FetchInstance::OnNotifyNetworkMonitorAlternateStack [%p]", this)); MOZ_ASSERT(mFetchDriver); MOZ_ASSERT(mPromises); - if (mArgsType != FetchArgsType::WorkerFetch) { - // We need to support this for Main thread fetch requests as well - // See Bug 1897129 + if (!mIsWorkerFetch) { return; } @@ -524,36 +483,6 @@ void FetchService::FetchInstance::OnNotifyNetworkMonitorAlternateStack( r, nsIThread::DISPATCH_NORMAL)); } -nsID FetchService::FetchInstance::GetActorID() { - if (mArgsType == FetchArgsType::WorkerFetch) { - return mArgs.as().mActorID; - } - - if (mArgsType == FetchArgsType::MainThreadFetch) { - return mArgs.as().mActorID; - } - - MOZ_ASSERT_UNREACHABLE("GetActorID called for unexpected mArgsType"); - - return {}; -} - -nsCOMPtr -FetchService::FetchInstance::GetBackgroundEventTarget() { - if (mArgsType == FetchArgsType::WorkerFetch) { - return mArgs.as().mEventTarget; - } - - if (mArgsType == FetchArgsType::MainThreadFetch) { - return mArgs.as().mEventTarget; - } - - MOZ_ASSERT_UNREACHABLE( - "GetBackgroundEventTarget called for unexpected mArgsType"); - - return {}; -} - // FetchService NS_IMPL_ISUPPORTS(FetchService, nsIObserver) @@ -598,23 +527,6 @@ RefPtr FetchService::NetworkErrorResponse( }); MOZ_ALWAYS_SUCCEEDS( args.mEventTarget->Dispatch(r, nsIThread::DISPATCH_NORMAL)); - } else if (aArgs.is()) { - const MainThreadFetchArgs& args = aArgs.as(); - nsCOMPtr r = NS_NewRunnableFunction( - __func__, [aRv, actorID = args.mActorID]() mutable { - FETCH_LOG( - ("FetchService::PropagateErrorResponse runnable aError: 0x%X", - (uint32_t)aRv)); - RefPtr actor = FetchParent::GetActorByID(actorID); - if (actor) { - actor->OnResponseAvailableInternal( - InternalResponse::NetworkError(aRv)); - actor->OnResponseEnd( - ResponseEndArgs(FetchDriverObserver::eAborted)); - } - }); - MOZ_ALWAYS_SUCCEEDS( - args.mEventTarget->Dispatch(r, nsIThread::DISPATCH_NORMAL)); } RefPtr promises = MakeRefPtr(); @@ -723,8 +635,7 @@ RefPtr FetchService::Fetch(FetchArgs&& aArgs) { // Create FetchInstance RefPtr fetch = MakeRefPtr(); - // Call FetchInstance::Initialize() to get needed information for - // FetchDriver + // Call FetchInstance::Initialize() to get needed information for FetchDriver nsresult rv = fetch->Initialize(std::move(aArgs)); if (NS_WARN_IF(NS_FAILED(rv))) { return NetworkErrorResponse(rv, fetch->Args()); diff --git a/dom/fetch/FetchService.h b/dom/fetch/FetchService.h index 901e11a9cd94..36c2527ef0a1 100644 --- a/dom/fetch/FetchService.h +++ b/dom/fetch/FetchService.h @@ -84,13 +84,11 @@ class FetchService final : public nsIObserver { NS_DECL_ISUPPORTS NS_DECL_NSIOBSERVER - // Used for ServiceWorkerNavigationPreload struct NavigationPreloadArgs { SafeRefPtr mRequest; nsCOMPtr mChannel; }; - // Used for content process worker thread fetch() struct WorkerFetchArgs { SafeRefPtr mRequest; mozilla::ipc::PrincipalInfo mPrincipalInfo; @@ -106,33 +104,11 @@ class FetchService final : public nsIObserver { bool mIsThirdPartyContext; }; - // Used for content process main thread fetch() - // Currently this is just used for keepalive request - // This would be further used for sending all main thread fetch requests - // through PFetch - // See Bug 1897129. - struct MainThreadFetchArgs { - SafeRefPtr mRequest; - mozilla::ipc::PrincipalInfo mPrincipalInfo; - Maybe mCookieJarSettings; - bool mNeedOnDataAvailable; - nsCOMPtr mCSPEventListener; - uint64_t mAssociatedBrowsingContextID; - nsCOMPtr mEventTarget; - nsID mActorID; - }; - struct UnknownArgs {}; - using FetchArgs = Variant; + using FetchArgs = + Variant; - enum class FetchArgsType { - NavigationPreload, - WorkerFetch, - MainThreadFetch, - Unknown, - }; static already_AddRefed GetInstance(); static RefPtr NetworkErrorResponse( @@ -184,8 +160,6 @@ class FetchService final : public nsIObserver { private: ~FetchInstance() = default; - nsCOMPtr GetBackgroundEventTarget(); - nsID GetActorID(); SafeRefPtr mRequest; nsCOMPtr mPrincipal; @@ -196,8 +170,7 @@ class FetchService final : public nsIObserver { RefPtr mFetchDriver; SafeRefPtr mResponse; RefPtr mPromises; - - FetchArgsType mArgsType; + bool mIsWorkerFetch{false}; }; ~FetchService(); diff --git a/dom/fetch/FetchTypes.ipdlh b/dom/fetch/FetchTypes.ipdlh index f4e765e077e5..f75de35abde1 100644 --- a/dom/fetch/FetchTypes.ipdlh +++ b/dom/fetch/FetchTypes.ipdlh @@ -67,7 +67,6 @@ struct IPCInternalRequest { RequestCache cacheMode; RequestRedirect requestRedirect; nsString integrity; - bool keepalive; nsCString fragment; PrincipalInfo? principalInfo; PrincipalInfo? interceptionTriggeringPrincipalInfo; diff --git a/dom/fetch/InternalRequest.cpp b/dom/fetch/InternalRequest.cpp index 7b02c3944aef..d2086e58ebd1 100644 --- a/dom/fetch/InternalRequest.cpp +++ b/dom/fetch/InternalRequest.cpp @@ -104,7 +104,7 @@ InternalRequest::InternalRequest( RequestRedirect aRequestRedirect, RequestCredentials aRequestCredentials, const nsACString& aReferrer, ReferrerPolicy aReferrerPolicy, RequestPriority aPriority, nsContentPolicyType aContentPolicyType, - const nsAString& aIntegrity, const bool& aKeepAlive) + const nsAString& aIntegrity) : mMethod(aMethod), mHeaders(aHeaders), mBodyLength(InternalResponse::UNKNOWN_BODY_SIZE), @@ -117,8 +117,7 @@ InternalRequest::InternalRequest( mCacheMode(aCacheMode), mRedirectMode(aRequestRedirect), mPriorityMode(aPriority), - mIntegrity(aIntegrity), - mKeepalive(aKeepAlive) { + mIntegrity(aIntegrity) { MOZ_ASSERT(!aURL.IsEmpty()); AddURL(aURL, aFragment); } @@ -139,7 +138,6 @@ InternalRequest::InternalRequest(const InternalRequest& aOther, mRedirectMode(aOther.mRedirectMode), mPriorityMode(aOther.mPriorityMode), mIntegrity(aOther.mIntegrity), - mKeepalive(aOther.mKeepalive), mMozErrors(aOther.mMozErrors), mFragment(aOther.mFragment), mSkipServiceWorker(aOther.mSkipServiceWorker), @@ -178,7 +176,6 @@ InternalRequest::InternalRequest(const IPCInternalRequest& aIPCRequest) mCacheMode(aIPCRequest.cacheMode()), mRedirectMode(aIPCRequest.requestRedirect()), mIntegrity(aIPCRequest.integrity()), - mKeepalive(aIPCRequest.keepalive()), mFragment(aIPCRequest.fragment()), mEmbedderPolicy(aIPCRequest.embedderPolicy()), mInterceptionContentPolicyType(static_cast( @@ -227,7 +224,6 @@ void InternalRequest::ToIPCInternalRequest( aIPCRequest->cacheMode() = mCacheMode; aIPCRequest->requestRedirect() = mRedirectMode; aIPCRequest->integrity() = mIntegrity; - aIPCRequest->keepalive() = mKeepalive; aIPCRequest->fragment() = mFragment; aIPCRequest->embedderPolicy() = mEmbedderPolicy; diff --git a/dom/fetch/InternalRequest.h b/dom/fetch/InternalRequest.h index d42cfe74d399..3a14a907ad43 100644 --- a/dom/fetch/InternalRequest.h +++ b/dom/fetch/InternalRequest.h @@ -95,7 +95,7 @@ class InternalRequest final : public AtomicSafeRefCounted { const nsACString& aReferrer, ReferrerPolicy aReferrerPolicy, RequestPriority aPriority, nsContentPolicyType aContentPolicyType, - const nsAString& aIntegrity, const bool& aKeepAlive); + const nsAString& aIntegrity); explicit InternalRequest(const IPCInternalRequest& aIPCRequest); diff --git a/dom/fetch/PFetch.ipdl b/dom/fetch/PFetch.ipdl index 29c6a6d4ce0f..7ec174565872 100644 --- a/dom/fetch/PFetch.ipdl +++ b/dom/fetch/PFetch.ipdl @@ -25,7 +25,6 @@ struct FetchOpArgs{ bool hasCSPEventListener; uint64_t associatedBrowsingContextID; bool isThirdPartyContext; - bool isWorkerRequest; }; protocol PFetch { diff --git a/dom/serviceworkers/ServiceWorkerPrivate.cpp b/dom/serviceworkers/ServiceWorkerPrivate.cpp index 8cd26a035d35..664874d99176 100644 --- a/dom/serviceworkers/ServiceWorkerPrivate.cpp +++ b/dom/serviceworkers/ServiceWorkerPrivate.cpp @@ -405,7 +405,7 @@ Result GetIPCInternalRequest( method, {spec}, ipcHeadersGuard, ipcHeaders, Nothing(), -1, alternativeDataType, contentPolicyType, referrer, referrerPolicy, environmentReferrerPolicy, requestMode, requestCredentials, cacheMode, - requestRedirect, integrity, false, fragment, principalInfo, + requestRedirect, integrity, fragment, principalInfo, interceptionPrincipalInfo, contentPolicyType, redirectChain, isThirdPartyChannel, embedderPolicy); } diff --git a/testing/web-platform/meta/fetch/api/idlharness.any.js.ini b/testing/web-platform/meta/fetch/api/idlharness.any.js.ini index c5f38e2be9b6..eff812316798 100644 --- a/testing/web-platform/meta/fetch/api/idlharness.any.js.ini +++ b/testing/web-platform/meta/fetch/api/idlharness.any.js.ini @@ -2,9 +2,15 @@ [Request interface: new Request('about:blank') must inherit property "isHistoryNavigation" with the proper type] expected: FAIL + [Request interface: attribute keepalive] + expected: FAIL + [Request interface: attribute isHistoryNavigation] expected: FAIL + [Request interface: new Request('about:blank') must inherit property "keepalive" with the proper type] + expected: FAIL + [Request interface: attribute isReloadNavigation] expected: FAIL @@ -28,9 +34,15 @@ [Request interface: new Request('about:blank') must inherit property "isHistoryNavigation" with the proper type] expected: FAIL + [Request interface: attribute keepalive] + expected: FAIL + [Request interface: attribute isHistoryNavigation] expected: FAIL + [Request interface: new Request('about:blank') must inherit property "keepalive" with the proper type] + expected: FAIL + [Request interface: attribute isReloadNavigation] expected: FAIL @@ -54,9 +66,15 @@ [Request interface: new Request('about:blank') must inherit property "isHistoryNavigation" with the proper type] expected: FAIL + [Request interface: attribute keepalive] + expected: FAIL + [Request interface: attribute isHistoryNavigation] expected: FAIL + [Request interface: new Request('about:blank') must inherit property "keepalive" with the proper type] + expected: FAIL + [Request interface: attribute isReloadNavigation] expected: FAIL @@ -80,9 +98,15 @@ [Request interface: new Request('about:blank') must inherit property "isHistoryNavigation" with the proper type] expected: FAIL + [Request interface: attribute keepalive] + expected: FAIL + [Request interface: attribute isHistoryNavigation] expected: FAIL + [Request interface: new Request('about:blank') must inherit property "keepalive" with the proper type] + expected: FAIL + [Request interface: attribute isReloadNavigation] expected: FAIL diff --git a/testing/web-platform/meta/fetch/api/redirect/redirect-keepalive.https.any.js.ini b/testing/web-platform/meta/fetch/api/redirect/redirect-keepalive.https.any.js.ini new file mode 100644 index 000000000000..3ab5cf62a1d9 --- /dev/null +++ b/testing/web-platform/meta/fetch/api/redirect/redirect-keepalive.https.any.js.ini @@ -0,0 +1,18 @@ +[redirect-keepalive.https.any.html] + expected: + if (os == "linux") and fission and not debug and asan: [TIMEOUT, OK] + if (os == "win") and not debug and (processor == "x86"): OK + if (os == "linux") and fission and debug: [TIMEOUT, OK] + if (os == "mac") and debug: TIMEOUT + if (os == "linux") and not fission: OK + if os == "android": OK + [OK, TIMEOUT] + [[keepalive\][iframe\][load\] mixed content redirect; setting up] + expected: + if (os == "linux") and fission and not debug and asan: [TIMEOUT, PASS] + if (os == "win") and not debug and (processor == "x86"): PASS + if (os == "linux") and fission and debug: [TIMEOUT, PASS] + if (os == "mac") and debug: TIMEOUT + if (os == "linux") and not fission: PASS + if os == "android": PASS + [PASS, TIMEOUT] diff --git a/testing/web-platform/meta/fetch/fetch-later/new-window.tentative.https.window.js.ini b/testing/web-platform/meta/fetch/fetch-later/new-window.tentative.https.window.js.ini index 6853c673076f..350ca32215b5 100644 --- a/testing/web-platform/meta/fetch/fetch-later/new-window.tentative.https.window.js.ini +++ b/testing/web-platform/meta/fetch/fetch-later/new-window.tentative.https.window.js.ini @@ -6,7 +6,7 @@ [A same-origin window[target=''\][features='0'\] can trigger fetchLater.] expected: if os == "linux": [TIMEOUT, FAIL] - FAIL + TIMEOUT [A cross-origin window[target=''\][features='0'\] can trigger fetchLater.] expected: TIMEOUT @@ -17,7 +17,7 @@ [A same-origin window[target=''\][features='1'\] can trigger fetchLater.] expected: if os == "linux": [TIMEOUT, FAIL] - FAIL + TIMEOUT [A cross-origin window[target=''\][features='1'\] can trigger fetchLater.] expected: TIMEOUT @@ -28,7 +28,7 @@ [A same-origin window[target=''\][features='2'\] can trigger fetchLater.] expected: if os == "linux": [TIMEOUT, FAIL] - FAIL + TIMEOUT [A cross-origin window[target=''\][features='2'\] can trigger fetchLater.] expected: TIMEOUT @@ -39,7 +39,7 @@ [A same-origin window[target='_blank'\][features='0'\] can trigger fetchLater.] expected: if os == "linux": [TIMEOUT, FAIL] - FAIL + TIMEOUT [A cross-origin window[target='_blank'\][features='0'\] can trigger fetchLater.] expected: TIMEOUT @@ -50,7 +50,7 @@ [A same-origin window[target='_blank'\][features='1'\] can trigger fetchLater.] expected: if os == "linux": [TIMEOUT, FAIL] - FAIL + TIMEOUT [A cross-origin window[target='_blank'\][features='1'\] can trigger fetchLater.] expected: TIMEOUT @@ -61,7 +61,7 @@ [A same-origin window[target='_blank'\][features='2'\] can trigger fetchLater.] expected: if os == "linux": [TIMEOUT, FAIL] - FAIL + TIMEOUT [A cross-origin window[target='_blank'\][features='2'\] can trigger fetchLater.] expected: TIMEOUT