@@ -56,8 +56,8 @@ FetchBodyOwner::FetchBodyOwner(ScriptExecutionContext* context, std::optional<Fe
5656
5757FetchBodyOwner::~FetchBodyOwner ()
5858{
59- if (m_readableStreamSource)
60- m_readableStreamSource ->detach ();
59+ if (RefPtr readableStreamSource = m_readableStreamSource)
60+ readableStreamSource ->detach ();
6161}
6262
6363void FetchBodyOwner::stop ()
@@ -83,8 +83,8 @@ bool FetchBodyOwner::isDisturbed() const
8383 if (m_isDisturbed)
8484 return true ;
8585
86- if (body ().readableStream ())
87- return body (). readableStream () ->isDisturbed ();
86+ if (RefPtr readableStream = body ().readableStream ())
87+ return readableStream->isDisturbed ();
8888
8989 return false ;
9090}
@@ -97,8 +97,8 @@ bool FetchBodyOwner::isDisturbedOrLocked() const
9797 if (m_isDisturbed)
9898 return true ;
9999
100- if (body ().readableStream ())
101- return body (). readableStream () ->isDisturbed () || body (). readableStream () ->isLocked ();
100+ if (RefPtr readableStream = body ().readableStream ())
101+ return readableStream->isDisturbed () || readableStream->isLocked ();
102102
103103 return false ;
104104}
@@ -211,7 +211,7 @@ void FetchBodyOwner::formData(Ref<DeferredPromise>&& promise)
211211 if (isBodyNullOrOpaque ()) {
212212 if (isBodyNull ()) {
213213 // If the content-type is 'application/x-www-form-urlencoded', a body is not required and we should package an empty byte sequence as per the specification.
214- if (auto formData = FetchBodyConsumer::packageFormData (promise->scriptExecutionContext (), contentType (), { })) {
214+ if (auto formData = FetchBodyConsumer::packageFormData (promise->protectedScriptExecutionContext (). get (), contentType (), { })) {
215215 promise->resolve <IDLInterface<DOMFormData>>(*formData);
216216 return ;
217217 }
@@ -277,7 +277,7 @@ void FetchBodyOwner::loadBlob(const Blob& blob, FetchBodyConsumer* consumer)
277277 m_blobLoader.emplace (*this );
278278 m_blobLoader->loader = makeUnique<FetchLoader>(*m_blobLoader, consumer);
279279
280- m_blobLoader->loader ->start (*scriptExecutionContext (), blob);
280+ m_blobLoader->loader ->start (*protectedScriptExecutionContext (), blob);
281281 if (!m_blobLoader->loader ->isStarted ()) {
282282 m_body->loadingFailed (Exception { ExceptionCode::TypeError, " Blob loading failed" _s });
283283 m_blobLoader = std::nullopt ;
@@ -295,10 +295,8 @@ void FetchBodyOwner::finishBlobLoading()
295295void FetchBodyOwner::blobLoadingSucceeded ()
296296{
297297 ASSERT (!isBodyNull ());
298- if (m_readableStreamSource) {
299- m_readableStreamSource->close ();
300- m_readableStreamSource = nullptr ;
301- }
298+ if (RefPtr readableStreamSource = std::exchange (m_readableStreamSource, nullptr ))
299+ readableStreamSource->close ();
302300
303301 m_body->loadingSucceeded (contentType ());
304302 if (!m_blobLoader)
@@ -310,19 +308,19 @@ void FetchBodyOwner::blobLoadingSucceeded()
310308void FetchBodyOwner::blobLoadingFailed ()
311309{
312310 ASSERT (!isBodyNull ());
313- if (m_readableStreamSource) {
314- if (!m_readableStreamSource->isCancelling ())
315- m_readableStreamSource->error (Exception { ExceptionCode::TypeError, " Blob loading failed" _s });
316- m_readableStreamSource = nullptr ;
311+ if (RefPtr readableStreamSource = std::exchange (m_readableStreamSource, nullptr )) {
312+ if (!readableStreamSource->isCancelling ())
313+ readableStreamSource->error (Exception { ExceptionCode::TypeError, " Blob loading failed" _s });
317314 } else
318315 m_body->loadingFailed (Exception { ExceptionCode::TypeError, " Blob loading failed" _s });
319316 finishBlobLoading ();
320317}
321318
322319void FetchBodyOwner::blobChunk (const SharedBuffer& buffer)
323320{
324- ASSERT (m_readableStreamSource);
325- if (!m_readableStreamSource->enqueue (buffer.tryCreateArrayBuffer ()))
321+ RefPtr readableStreamSource = m_readableStreamSource;
322+ ASSERT (readableStreamSource);
323+ if (!readableStreamSource->enqueue (buffer.tryCreateArrayBuffer ()))
326324 stop ();
327325}
328326
@@ -341,13 +339,12 @@ void FetchBodyOwner::BlobLoader::didFail(const ResourceError&)
341339{
342340 // didFail might be called within FetchLoader::start call.
343341 if (loader->isStarted ())
344- owner. blobLoadingFailed ();
342+ protectedOwner ()-> blobLoadingFailed ();
345343}
346344
347345void FetchBodyOwner::BlobLoader::didSucceed (const NetworkLoadMetrics&)
348346{
349- Ref protectedOwner = Ref { owner };
350- protectedOwner->blobLoadingSucceeded ();
347+ protectedOwner ()->blobLoadingSucceeded ();
351348}
352349
353350ExceptionOr<RefPtr<ReadableStream>> FetchBodyOwner::readableStream (JSC::JSGlobalObject& state)
@@ -372,7 +369,7 @@ ExceptionOr<void> FetchBodyOwner::createReadableStream(JSC::JSGlobalObject& stat
372369 if (UNLIKELY (streamOrException.hasException ()))
373370 return streamOrException.releaseException ();
374371 m_body->setReadableStream (streamOrException.releaseReturnValue ());
375- m_body->readableStream ()->lock ();
372+ m_body->protectedReadableStream ()->lock ();
376373 return { };
377374 }
378375
@@ -388,15 +385,16 @@ ExceptionOr<void> FetchBodyOwner::createReadableStream(JSC::JSGlobalObject& stat
388385
389386void FetchBodyOwner::consumeBodyAsStream ()
390387{
391- ASSERT (m_readableStreamSource);
388+ RefPtr readableStreamSource = m_readableStreamSource;
389+ ASSERT (readableStreamSource);
392390
393391 if (auto exception = loadingException ()) {
394- m_readableStreamSource ->error (*exception);
392+ readableStreamSource ->error (*exception);
395393 return ;
396394 }
397395
398- body ().consumeAsStream (*this , *m_readableStreamSource );
399- if (!m_readableStreamSource ->isPulling ())
396+ body ().consumeAsStream (*this , *readableStreamSource );
397+ if (!readableStreamSource ->isPulling ())
400398 m_readableStreamSource = nullptr ;
401399}
402400
0 commit comments