Issue #1587 - Part 3: Hook FetchSignal up to the Fetch API

This commit is contained in:
Moonchild 2020-06-10 21:51:44 +00:00 committed by Roy Tam
commit 78ff97aaf3
6 changed files with 292 additions and 30 deletions

View file

@ -67,7 +67,7 @@ FetchDriver::~FetchDriver()
}
nsresult
FetchDriver::Fetch(FetchDriverObserver* aObserver)
FetchDriver::Fetch(FetchSignal* aSignal, FetchDriverObserver* aObserver)
{
workers::AssertIsOnMainThread();
#ifdef DEBUG
@ -90,6 +90,18 @@ FetchDriver::Fetch(FetchDriverObserver* aObserver)
}
mRequest->SetPrincipalInfo(Move(principalInfo));
// If the signal is aborted, it's time to inform the observer and terminate
// the operation.
if (aSignal) {
if (aSignal->Aborted()) {
Aborted();
return NS_OK;
}
Follow(aSignal);
}
if (NS_FAILED(HttpFetch())) {
FailWithNetworkError();
}
@ -114,11 +126,7 @@ FetchDriver::HttpFetch()
nsAutoCString url;
mRequest->GetURL(url);
nsCOMPtr<nsIURI> uri;
rv = NS_NewURI(getter_AddRefs(uri),
url,
nullptr,
nullptr,
ios);
rv = NS_NewURI(getter_AddRefs(uri), url, nullptr, nullptr, ios);
NS_ENSURE_SUCCESS(rv, rv);
// Unsafe requests aren't allowed with when using no-core mode.
@ -380,6 +388,8 @@ FetchDriver::HttpFetch()
NS_ENSURE_SUCCESS(rv, rv);
// Step 4 onwards of "HTTP Fetch" is handled internally by Necko.
mChannel = chan;
return NS_OK;
}
already_AddRefed<InternalResponse>
@ -433,9 +443,11 @@ FetchDriver::FailWithNetworkError()
#ifdef DEBUG
mResponseAvailableCalled = true;
#endif
mObserver->OnResponseEnd();
mObserver->OnResponseEnd(FetchDriverObserver::eByNetworking);
mObserver = nullptr;
}
mChannel = nullptr;
}
namespace {
@ -777,10 +789,11 @@ FetchDriver::OnStopRequest(nsIRequest* aRequest,
#endif
}
mObserver->OnResponseEnd();
mObserver->OnResponseEnd(FetchDriverObserver::eByNetworking);
mObserver = nullptr;
}
mChannel = nullptr;
return NS_OK;
}
@ -921,5 +934,21 @@ FetchDriver::SetRequestHeaders(nsIHttpChannel* aChannel) const
}
}
void FetchDriver::Aborted()
{
if (mObserver) {
#ifdef DEBUG
mResponseAvailableCalled = true;
#endif
mObserver->OnResponseEnd(FetchDriverObserver::eAborted);
mObserver = nullptr;
}
if (mChannel) {
mChannel->Cancel(NS_BINDING_ABORTED);
mChannel = nullptr;
}
}
} // namespace dom
} // namespace mozilla