Issue #1442 - Part 14 - Starting body consuming and passing the JSContext down from the binding entrypoints to where the ReadableStream could be read. https://bugzilla.mozilla.org/show_bug.cgi?id=1128959

This commit is contained in:
Brian Smith 2023-09-28 19:35:59 -05:00 • committed by roytam1
commit 842b9d47b3
15 changed files with 192 additions and 77 deletions

View file

@ -948,6 +948,52 @@ template
bool
FetchBody<Response>::BodyUsed() const;
template <class Derived>
void
FetchBody<Derived>::SetBodyUsed(JSContext* aCx, ErrorResult& aRv)
{
MOZ_ASSERT(aCx);
MOZ_ASSERT(mOwner->EventTargetFor(TaskCategory::Other)->IsOnCurrentThread());
if (mBodyUsed) {
return;
}
mBodyUsed = true;
// If we already have a ReadableStreamBody and it has been created by DOM, we
// have to lock it now because it can have been shared with other objects.
if (mReadableStreamBody) {
JS::Rooted<JSObject*> readableStreamObj(aCx, mReadableStreamBody);
if (JS::ReadableStreamGetMode(readableStreamObj) ==
JS::ReadableStreamMode::ExternalSource) {
LockStream(aCx, readableStreamObj, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return;
}
} else {
// If this is not a native ReadableStream, let's activate the
// FetchStreamReader.
MOZ_ASSERT(mFetchStreamReader);
JS::Rooted<JSObject*> reader(aCx);
mFetchStreamReader->StartConsuming(aCx, readableStreamObj, &reader, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return;
}
mReadableStreamReader = reader;
}
}
}
template
void
FetchBody<Request>::SetBodyUsed(JSContext* aCx, ErrorResult& aRv);
template
void
FetchBody<Response>::SetBodyUsed(JSContext* aCx, ErrorResult& aRv);
template <class Derived>
already_AddRefed<Promise>
FetchBody<Derived>::ConsumeBody(JSContext* aCx, FetchConsumeType aType, ErrorResult& aRv)
@ -963,30 +1009,9 @@ FetchBody<Derived>::ConsumeBody(JSContext* aCx, FetchConsumeType aType, ErrorRes
return nullptr;
}
SetBodyUsed();
// If we already have a ReadableStreamBody and it has been created by DOM, we
// have to lock it now because it can have been shared with other objects.
if (mReadableStreamBody) {
JS::Rooted<JSObject*> readableStreamObj(aCx, mReadableStreamBody);
if (JS::ReadableStreamGetMode(readableStreamObj) ==
JS::ReadableStreamMode::ExternalSource) {
LockStream(aCx, readableStreamObj, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
} else {
// If this is not a native ReadableStream, let's activate the
// FetchStreamReader.
MOZ_ASSERT(mFetchStreamReader);
JS::Rooted<JSObject*> reader(aCx);
mFetchStreamReader->StartConsuming(aCx, readableStreamObj, &reader, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
mReadableStreamReader = reader;
}
SetBodyUsed(aCx, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
nsCOMPtr<nsIGlobalObject> global = DerivedClass()->GetParentObject();

View file

@ -190,11 +190,28 @@ public:
// Utility public methods accessed by various runnables.
// This method _must_ be called in order to set the body as used. If the body
// is a ReadableStream, this method will start reading the stream.
// More in details, this method does:
// 1) It uses an internal flag to track if the body is used. This is tracked
// separately from the ReadableStream disturbed state due to purely native
// streams.
// 2) If there is a ReadableStream reflector for the native stream it is
// Locked.
// 3) If there is a JS ReadableStream then we begin pumping it into the native
// body stream. This effectively locks and disturbs the stream.
//
// Note that JSContext is used only if there is a ReadableStream (this can
// happen because the body is a ReadableStream or because attribute body has
// already been used by content). If something goes wrong using
// ReadableStream, errors will be reported via ErrorResult and not as JS
// exceptions in JSContext. This is done in order to have a centralized error
// reporting way.
//
// Exceptions generated when reading from the ReadableStream are directly sent
// to the Console (NOTE FOR THE REVIEWER: this is part of patch 16)
void
SetBodyUsed()
{
mBodyUsed = true;
}
SetBodyUsed(JSContext* aCx, ErrorResult& aRv);
const nsCString&
MimeType() const

View file

@ -614,7 +614,10 @@ Request::Constructor(const GlobalObject& aGlobal,
inputReq->GetBody(getter_AddRefs(body));
if (body) {
inputReq->SetBody(nullptr);
inputReq->SetBodyUsed();
inputReq->SetBodyUsed(aGlobal.Context(), aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
}
}
return domRequest.forget();