From 7004df09e8ecb0cc2efa9cb54bb6e55007e4e4eb Mon Sep 17 00:00:00 2001 From: Olli Pettay Date: Wed, 12 Jun 2019 18:37:49 +0000 Subject: [PATCH] Bug 1538516 - Resolve the Promise returned by fetch() later during page loads, r=baku The patch depends on https://bugzilla.mozilla.org/show_bug.cgi?id=1558215 Depends on D34366 Differential Revision: https://phabricator.services.mozilla.com/D34367 --HG-- extra : moz-landing-system : lando --- dom/fetch/Fetch.cpp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/dom/fetch/Fetch.cpp b/dom/fetch/Fetch.cpp index 0876a7e546b1..c8596df4a2b0 100644 --- a/dom/fetch/Fetch.cpp +++ b/dom/fetch/Fetch.cpp @@ -540,6 +540,21 @@ already_AddRefed FetchRequest(nsIGlobalObject* aGlobal, return p.forget(); } +class ResolveFetchPromise : public Runnable { + public: + ResolveFetchPromise(Promise* aPromise, Response* aResponse) + : Runnable("ResolveFetchPromise"), + mPromise(aPromise), + mResponse(aResponse) {} + + NS_IMETHOD Run() { + mPromise->MaybeResolve(mResponse); + return NS_OK; + } + RefPtr mPromise; + RefPtr mResponse; +}; + void MainThreadFetchResolver::OnResponseAvailableInternal( InternalResponse* aResponse) { NS_ASSERT_OWNINGTHREAD(MainThreadFetchResolver); @@ -552,7 +567,15 @@ void MainThreadFetchResolver::OnResponseAvailableInternal( nsCOMPtr go = mPromise->GetParentObject(); mResponse = new Response(go, aResponse, mSignalImpl); - mPromise->MaybeResolve(mResponse); + nsCOMPtr inner = do_QueryInterface(go); + nsPIDOMWindowInner* topLevel = + inner ? inner->GetWindowForDeprioritizedLoadRunner() : nullptr; + if (topLevel) { + topLevel->AddDeprioritizedLoadRunner( + new ResolveFetchPromise(mPromise, mResponse)); + } else { + mPromise->MaybeResolve(mResponse); + } } else { if (mFetchObserver) { mFetchObserver->SetState(FetchState::Errored);