Bug 1557121 - Implement Blob.text()/Blob.arrayBuffer()/Blob.stream() - part 1 - Rename FetchStream to BodyStream, r=smaug

Differential Revision: https://phabricator.services.mozilla.com/D33826

--HG--
rename : dom/fetch/FetchStream.cpp => dom/base/BodyStream.cpp
rename : dom/fetch/FetchStream.h => dom/base/BodyStream.h
extra : moz-landing-system : lando
This commit is contained in:
Andrea Marchesini 2019-06-07 13:09:12 +00:00
Родитель 9b941f21f6
Коммит 16d8326f76
7 изменённых файлов: 86 добавлений и 86 удалений

Просмотреть файл

@ -4,10 +4,11 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "FetchStream.h" #include "BodyStream.h"
#include "mozilla/dom/DOMException.h" #include "mozilla/dom/DOMException.h"
#include "mozilla/dom/WorkerCommon.h" #include "mozilla/dom/WorkerCommon.h"
#include "mozilla/dom/WorkerPrivate.h" #include "mozilla/dom/WorkerPrivate.h"
#include "mozilla/dom/WorkerRunnable.h"
#include "nsProxyRelease.h" #include "nsProxyRelease.h"
#include "nsStreamUtils.h" #include "nsStreamUtils.h"
@ -16,9 +17,9 @@ static NS_DEFINE_CID(kStreamTransportServiceCID, NS_STREAMTRANSPORTSERVICE_CID);
namespace mozilla { namespace mozilla {
namespace dom { namespace dom {
class FetchStream::WorkerShutdown final : public WorkerControlRunnable { class BodyStream::WorkerShutdown final : public WorkerControlRunnable {
public: public:
WorkerShutdown(WorkerPrivate* aWorkerPrivate, RefPtr<FetchStream> aStream) WorkerShutdown(WorkerPrivate* aWorkerPrivate, RefPtr<BodyStream> aStream)
: WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount), : WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
mStream(aStream) {} mStream(aStream) {}
@ -36,14 +37,14 @@ class FetchStream::WorkerShutdown final : public WorkerControlRunnable {
bool aDispatchResult) override {} bool aDispatchResult) override {}
private: private:
RefPtr<FetchStream> mStream; RefPtr<BodyStream> mStream;
}; };
NS_IMPL_ISUPPORTS(FetchStream, nsIInputStreamCallback, nsIObserver, NS_IMPL_ISUPPORTS(BodyStream, nsIInputStreamCallback, nsIObserver,
nsISupportsWeakReference) nsISupportsWeakReference)
/* static */ /* static */
void FetchStream::Create(JSContext* aCx, FetchStreamHolder* aStreamHolder, void BodyStream::Create(JSContext* aCx, BodyStreamHolder* aStreamHolder,
nsIGlobalObject* aGlobal, nsIInputStream* aInputStream, nsIGlobalObject* aGlobal, nsIInputStream* aInputStream,
JS::MutableHandle<JSObject*> aStream, JS::MutableHandle<JSObject*> aStream,
ErrorResult& aRv) { ErrorResult& aRv) {
@ -51,8 +52,8 @@ void FetchStream::Create(JSContext* aCx, FetchStreamHolder* aStreamHolder,
MOZ_DIAGNOSTIC_ASSERT(aStreamHolder); MOZ_DIAGNOSTIC_ASSERT(aStreamHolder);
MOZ_DIAGNOSTIC_ASSERT(aInputStream); MOZ_DIAGNOSTIC_ASSERT(aInputStream);
RefPtr<FetchStream> stream = RefPtr<BodyStream> stream =
new FetchStream(aGlobal, aStreamHolder, aInputStream); new BodyStream(aGlobal, aStreamHolder, aInputStream);
if (NS_IsMainThread()) { if (NS_IsMainThread()) {
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService(); nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
@ -92,7 +93,7 @@ void FetchStream::Create(JSContext* aCx, FetchStreamHolder* aStreamHolder,
return; return;
} }
// This will be released in FetchStream::FinalizeCallback(). We are // This will be released in BodyStream::FinalizeCallback(). We are
// guaranteed the jsapi will call FinalizeCallback when ReadableStream // guaranteed the jsapi will call FinalizeCallback when ReadableStream
// js object is finalized. // js object is finalized.
NS_ADDREF(stream.get()); NS_ADDREF(stream.get());
@ -100,7 +101,7 @@ void FetchStream::Create(JSContext* aCx, FetchStreamHolder* aStreamHolder,
aStream.set(body); aStream.set(body);
} }
void FetchStream::requestData(JSContext* aCx, JS::HandleObject aStream, void BodyStream::requestData(JSContext* aCx, JS::HandleObject aStream,
size_t aDesiredSize) { size_t aDesiredSize) {
#if MOZ_DIAGNOSTIC_ASSERT_ENABLED #if MOZ_DIAGNOSTIC_ASSERT_ENABLED
bool disturbed; bool disturbed;
@ -167,7 +168,7 @@ void FetchStream::requestData(JSContext* aCx, JS::HandleObject aStream,
// All good. // All good.
} }
void FetchStream::writeIntoReadRequestBuffer(JSContext* aCx, void BodyStream::writeIntoReadRequestBuffer(JSContext* aCx,
JS::HandleObject aStream, JS::HandleObject aStream,
void* aBuffer, size_t aLength, void* aBuffer, size_t aLength,
size_t* aByteWritten) { size_t* aByteWritten) {
@ -206,7 +207,7 @@ void FetchStream::writeIntoReadRequestBuffer(JSContext* aCx,
// All good. // All good.
} }
JS::Value FetchStream::cancel(JSContext* aCx, JS::HandleObject aStream, JS::Value BodyStream::cancel(JSContext* aCx, JS::HandleObject aStream,
JS::HandleValue aReason) { JS::HandleValue aReason) {
AssertIsOnOwningThread(); AssertIsOnOwningThread();
@ -230,9 +231,9 @@ JS::Value FetchStream::cancel(JSContext* aCx, JS::HandleObject aStream,
return JS::UndefinedValue(); return JS::UndefinedValue();
} }
void FetchStream::onClosed(JSContext* aCx, JS::HandleObject aStream) {} void BodyStream::onClosed(JSContext* aCx, JS::HandleObject aStream) {}
void FetchStream::onErrored(JSContext* aCx, JS::HandleObject aStream, void BodyStream::onErrored(JSContext* aCx, JS::HandleObject aStream,
JS::HandleValue aReason) { JS::HandleValue aReason) {
AssertIsOnOwningThread(); AssertIsOnOwningThread();
@ -248,19 +249,19 @@ void FetchStream::onErrored(JSContext* aCx, JS::HandleObject aStream,
ReleaseObjects(); ReleaseObjects();
} }
void FetchStream::finalize() { void BodyStream::finalize() {
// This can be called in any thread. // This can be called in any thread.
// This takes ownership of the ref created in FetchStream::Create(). // This takes ownership of the ref created in BodyStream::Create().
RefPtr<FetchStream> stream = dont_AddRef(this); RefPtr<BodyStream> stream = dont_AddRef(this);
stream->ReleaseObjects(); stream->ReleaseObjects();
} }
FetchStream::FetchStream(nsIGlobalObject* aGlobal, BodyStream::BodyStream(nsIGlobalObject* aGlobal,
FetchStreamHolder* aStreamHolder, BodyStreamHolder* aStreamHolder,
nsIInputStream* aInputStream) nsIInputStream* aInputStream)
: mMutex("FetchStream::mMutex"), : mMutex("BodyStream::mMutex"),
mState(eInitializing), mState(eInitializing),
mGlobal(aGlobal), mGlobal(aGlobal),
mStreamHolder(aStreamHolder), mStreamHolder(aStreamHolder),
@ -270,9 +271,9 @@ FetchStream::FetchStream(nsIGlobalObject* aGlobal,
MOZ_DIAGNOSTIC_ASSERT(aStreamHolder); MOZ_DIAGNOSTIC_ASSERT(aStreamHolder);
} }
FetchStream::~FetchStream() {} BodyStream::~BodyStream() {}
void FetchStream::ErrorPropagation(JSContext* aCx, void BodyStream::ErrorPropagation(JSContext* aCx,
const MutexAutoLock& aProofOfLock, const MutexAutoLock& aProofOfLock,
JS::HandleObject aStream, nsresult aError) { JS::HandleObject aStream, nsresult aError) {
AssertIsOnOwningThread(); AssertIsOnOwningThread();
@ -301,7 +302,7 @@ void FetchStream::ErrorPropagation(JSContext* aCx,
} }
NS_IMETHODIMP NS_IMETHODIMP
FetchStream::OnInputStreamReady(nsIAsyncInputStream* aStream) { BodyStream::OnInputStreamReady(nsIAsyncInputStream* aStream) {
AssertIsOnOwningThread(); AssertIsOnOwningThread();
MOZ_DIAGNOSTIC_ASSERT(aStream); MOZ_DIAGNOSTIC_ASSERT(aStream);
@ -359,14 +360,14 @@ FetchStream::OnInputStreamReady(nsIAsyncInputStream* aStream) {
} }
/* static */ /* static */
nsresult FetchStream::RetrieveInputStream( nsresult BodyStream::RetrieveInputStream(
JS::ReadableStreamUnderlyingSource* aUnderlyingReadableStreamSource, JS::ReadableStreamUnderlyingSource* aUnderlyingReadableStreamSource,
nsIInputStream** aInputStream) { nsIInputStream** aInputStream) {
MOZ_ASSERT(aUnderlyingReadableStreamSource); MOZ_ASSERT(aUnderlyingReadableStreamSource);
MOZ_ASSERT(aInputStream); MOZ_ASSERT(aInputStream);
RefPtr<FetchStream> stream = RefPtr<BodyStream> stream =
static_cast<FetchStream*>(aUnderlyingReadableStreamSource); static_cast<BodyStream*>(aUnderlyingReadableStreamSource);
stream->AssertIsOnOwningThread(); stream->AssertIsOnOwningThread();
// if mOriginalInputStream is null, the reading already started. We don't want // if mOriginalInputStream is null, the reading already started. We don't want
@ -380,7 +381,7 @@ nsresult FetchStream::RetrieveInputStream(
return NS_OK; return NS_OK;
} }
void FetchStream::Close() { void BodyStream::Close() {
AssertIsOnOwningThread(); AssertIsOnOwningThread();
MutexAutoLock lock(mMutex); MutexAutoLock lock(mMutex);
@ -400,7 +401,7 @@ void FetchStream::Close() {
CloseAndReleaseObjects(cx, lock, stream); CloseAndReleaseObjects(cx, lock, stream);
} }
void FetchStream::CloseAndReleaseObjects(JSContext* aCx, void BodyStream::CloseAndReleaseObjects(JSContext* aCx,
const MutexAutoLock& aProofOfLock, const MutexAutoLock& aProofOfLock,
JS::HandleObject aStream) { JS::HandleObject aStream) {
AssertIsOnOwningThread(); AssertIsOnOwningThread();
@ -418,12 +419,12 @@ void FetchStream::CloseAndReleaseObjects(JSContext* aCx,
} }
} }
void FetchStream::ReleaseObjects() { void BodyStream::ReleaseObjects() {
MutexAutoLock lock(mMutex); MutexAutoLock lock(mMutex);
ReleaseObjects(lock); ReleaseObjects(lock);
} }
void FetchStream::ReleaseObjects(const MutexAutoLock& aProofOfLock) { void BodyStream::ReleaseObjects(const MutexAutoLock& aProofOfLock) {
// This method can be called on 2 possible threads: the owning one and a JS // This method can be called on 2 possible threads: the owning one and a JS
// thread used to release resources. If we are on the JS thread, we need to // thread used to release resources. If we are on the JS thread, we need to
// dispatch a runnable to go back to the owning thread in order to release // dispatch a runnable to go back to the owning thread in order to release
@ -446,9 +447,9 @@ void FetchStream::ReleaseObjects(const MutexAutoLock& aProofOfLock) {
} }
// A normal runnable of the owning thread is the main-thread. // A normal runnable of the owning thread is the main-thread.
RefPtr<FetchStream> self = this; RefPtr<BodyStream> self = this;
RefPtr<Runnable> r = NS_NewRunnableFunction( RefPtr<Runnable> r = NS_NewRunnableFunction(
"FetchStream::ReleaseObjects", [self]() { self->ReleaseObjects(); }); "BodyStream::ReleaseObjects", [self]() { self->ReleaseObjects(); });
mOwningEventTarget->Dispatch(r.forget()); mOwningEventTarget->Dispatch(r.forget());
return; return;
} }
@ -470,8 +471,8 @@ void FetchStream::ReleaseObjects(const MutexAutoLock& aProofOfLock) {
} }
#ifdef DEBUG #ifdef DEBUG
void FetchStream::AssertIsOnOwningThread() { void BodyStream::AssertIsOnOwningThread() {
NS_ASSERT_OWNINGTHREAD(FetchStream); NS_ASSERT_OWNINGTHREAD(BodyStream);
} }
#endif #endif
@ -479,7 +480,7 @@ void FetchStream::AssertIsOnOwningThread() {
// ----------- // -----------
NS_IMETHODIMP NS_IMETHODIMP
FetchStream::Observe(nsISupports* aSubject, const char* aTopic, BodyStream::Observe(nsISupports* aSubject, const char* aTopic,
const char16_t* aData) { const char16_t* aData) {
AssertIsOnMainThread(); AssertIsOnMainThread();
AssertIsOnOwningThread(); AssertIsOnOwningThread();

Просмотреть файл

@ -4,10 +4,9 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_dom_FetchStream_h #ifndef mozilla_dom_BodyStream_h
#define mozilla_dom_FetchStream_h #define mozilla_dom_BodyStream_h
#include "Fetch.h"
#include "jsapi.h" #include "jsapi.h"
#include "js/Stream.h" #include "js/Stream.h"
#include "nsIAsyncInputStream.h" #include "nsIAsyncInputStream.h"
@ -22,10 +21,20 @@ class nsIInputStream;
namespace mozilla { namespace mozilla {
namespace dom { namespace dom {
class FetchStreamHolder;
class WeakWorkerRef; class WeakWorkerRef;
class FetchStream final : public nsIInputStreamCallback, class BodyStreamHolder {
public:
NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
virtual void NullifyStream() = 0;
virtual void MarkAsRead() = 0;
virtual JSObject* ReadableStreamBody() = 0;
};
class BodyStream final : public nsIInputStreamCallback,
public nsIObserver, public nsIObserver,
public nsSupportsWeakReference, public nsSupportsWeakReference,
private JS::ReadableStreamUnderlyingSource { private JS::ReadableStreamUnderlyingSource {
@ -34,7 +43,7 @@ class FetchStream final : public nsIInputStreamCallback,
NS_DECL_NSIINPUTSTREAMCALLBACK NS_DECL_NSIINPUTSTREAMCALLBACK
NS_DECL_NSIOBSERVER NS_DECL_NSIOBSERVER
static void Create(JSContext* aCx, FetchStreamHolder* aStreamHolder, static void Create(JSContext* aCx, BodyStreamHolder* aStreamHolder,
nsIGlobalObject* aGlobal, nsIInputStream* aInputStream, nsIGlobalObject* aGlobal, nsIInputStream* aInputStream,
JS::MutableHandle<JSObject*> aStream, ErrorResult& aRv); JS::MutableHandle<JSObject*> aStream, ErrorResult& aRv);
@ -45,9 +54,9 @@ class FetchStream final : public nsIInputStreamCallback,
nsIInputStream** aInputStream); nsIInputStream** aInputStream);
private: private:
FetchStream(nsIGlobalObject* aGlobal, FetchStreamHolder* aStreamHolder, BodyStream(nsIGlobalObject* aGlobal, BodyStreamHolder* aStreamHolder,
nsIInputStream* aInputStream); nsIInputStream* aInputStream);
~FetchStream(); ~BodyStream();
#ifdef DEBUG #ifdef DEBUG
void AssertIsOnOwningThread(); void AssertIsOnOwningThread();
@ -109,7 +118,7 @@ class FetchStream final : public nsIInputStreamCallback,
eClosed, eClosed,
}; };
// We need a mutex because JS engine can release FetchStream on a non-owning // We need a mutex because JS engine can release BodyStream on a non-owning
// thread. We must be sure that the releasing of resources doesn't trigger // thread. We must be sure that the releasing of resources doesn't trigger
// race conditions. // race conditions.
Mutex mMutex; Mutex mMutex;
@ -118,7 +127,7 @@ class FetchStream final : public nsIInputStreamCallback,
State mState; State mState;
nsCOMPtr<nsIGlobalObject> mGlobal; nsCOMPtr<nsIGlobalObject> mGlobal;
RefPtr<FetchStreamHolder> mStreamHolder; RefPtr<BodyStreamHolder> mStreamHolder;
nsCOMPtr<nsIEventTarget> mOwningEventTarget; nsCOMPtr<nsIEventTarget> mOwningEventTarget;
// This is the original inputStream received during the CTOR. It will be // This is the original inputStream received during the CTOR. It will be
@ -133,4 +142,4 @@ class FetchStream final : public nsIInputStreamCallback,
} // namespace dom } // namespace dom
} // namespace mozilla } // namespace mozilla
#endif // mozilla_dom_FetchStream_h #endif // mozilla_dom_BodyStream_h

Просмотреть файл

@ -142,6 +142,7 @@ EXPORTS.mozilla.dom += [
'Attr.h', 'Attr.h',
'BarProps.h', 'BarProps.h',
'BindContext.h', 'BindContext.h',
'BodyStream.h',
'BodyUtil.h', 'BodyUtil.h',
'BorrowedAttrInfo.h', 'BorrowedAttrInfo.h',
'CharacterData.h', 'CharacterData.h',
@ -260,6 +261,7 @@ UNIFIED_SOURCES += [
'Attr.cpp', 'Attr.cpp',
'AttrArray.cpp', 'AttrArray.cpp',
'BarProps.cpp', 'BarProps.cpp',
'BodyStream.cpp',
'BodyUtil.cpp', 'BodyUtil.cpp',
'BorrowedAttrInfo.cpp', 'BorrowedAttrInfo.cpp',
'CharacterData.cpp', 'CharacterData.cpp',

Просмотреть файл

@ -6,7 +6,6 @@
#include "Fetch.h" #include "Fetch.h"
#include "FetchConsumer.h" #include "FetchConsumer.h"
#include "FetchStream.h"
#include "mozilla/dom/Document.h" #include "mozilla/dom/Document.h"
#include "nsIGlobalObject.h" #include "nsIGlobalObject.h"
@ -1273,7 +1272,7 @@ void FetchBody<Derived>::GetBody(JSContext* aCx,
} }
JS::Rooted<JSObject*> body(aCx); JS::Rooted<JSObject*> body(aCx);
FetchStream::Create(aCx, this, DerivedClass()->GetParentObject(), inputStream, BodyStream::Create(aCx, this, DerivedClass()->GetParentObject(), inputStream,
&body, aRv); &body, aRv);
if (NS_WARN_IF(aRv.Failed())) { if (NS_WARN_IF(aRv.Failed())) {
return; return;

Просмотреть файл

@ -18,6 +18,7 @@
#include "mozilla/DebugOnly.h" #include "mozilla/DebugOnly.h"
#include "mozilla/ErrorResult.h" #include "mozilla/ErrorResult.h"
#include "mozilla/dom/AbortSignal.h" #include "mozilla/dom/AbortSignal.h"
#include "mozilla/dom/BodyStream.h"
#include "mozilla/dom/Promise.h" #include "mozilla/dom/Promise.h"
#include "mozilla/dom/FetchStreamReader.h" #include "mozilla/dom/FetchStreamReader.h"
#include "mozilla/dom/RequestBinding.h" #include "mozilla/dom/RequestBinding.h"
@ -97,17 +98,6 @@ enum FetchConsumeType {
CONSUME_TEXT, CONSUME_TEXT,
}; };
class FetchStreamHolder {
public:
NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
virtual void NullifyStream() = 0;
virtual void MarkAsRead() = 0;
virtual JSObject* ReadableStreamBody() = 0;
};
/* /*
* FetchBody's body consumption uses nsIInputStreamPump to read from the * FetchBody's body consumption uses nsIInputStreamPump to read from the
* underlying stream to a block of memory, which is then adopted by * underlying stream to a block of memory, which is then adopted by
@ -143,7 +133,7 @@ class FetchStreamHolder {
* The pump is always released on the main thread. * The pump is always released on the main thread.
*/ */
template <class Derived> template <class Derived>
class FetchBody : public FetchStreamHolder, public AbortFollower { class FetchBody : public BodyStreamHolder, public AbortFollower {
public: public:
friend class FetchBodyConsumer<Derived>; friend class FetchBodyConsumer<Derived>;
@ -214,7 +204,7 @@ class FetchBody : public FetchStreamHolder, public AbortFollower {
const nsCString& MimeType() const { return mMimeType; } const nsCString& MimeType() const { return mMimeType; }
// FetchStreamHolder // BodyStreamHolder
void NullifyStream() override { void NullifyStream() override {
mReadableStreamBody = nullptr; mReadableStreamBody = nullptr;
mReadableStreamReader = nullptr; mReadableStreamReader = nullptr;
@ -243,7 +233,7 @@ class FetchBody : public FetchStreamHolder, public AbortFollower {
WorkerPrivate* mWorkerPrivate; WorkerPrivate* mWorkerPrivate;
// This is the ReadableStream exposed to content. It's underlying source is a // This is the ReadableStream exposed to content. It's underlying source is a
// FetchStream object. // BodyStream object.
JS::Heap<JSObject*> mReadableStreamBody; JS::Heap<JSObject*> mReadableStreamBody;
// This is the Reader used to retrieve data from the body. // This is the Reader used to retrieve data from the body.

Просмотреть файл

@ -12,6 +12,7 @@
#include "nsPIDOMWindow.h" #include "nsPIDOMWindow.h"
#include "mozilla/ErrorResult.h" #include "mozilla/ErrorResult.h"
#include "mozilla/dom/BodyStream.h"
#include "mozilla/dom/FetchBinding.h" #include "mozilla/dom/FetchBinding.h"
#include "mozilla/dom/ResponseBinding.h" #include "mozilla/dom/ResponseBinding.h"
#include "mozilla/dom/Headers.h" #include "mozilla/dom/Headers.h"
@ -22,7 +23,6 @@
#include "nsDOMString.h" #include "nsDOMString.h"
#include "BodyExtractor.h" #include "BodyExtractor.h"
#include "FetchStream.h"
#include "FetchStreamReader.h" #include "FetchStreamReader.h"
#include "InternalResponse.h" #include "InternalResponse.h"
@ -309,7 +309,7 @@ already_AddRefed<Response> Response::Constructor(
MOZ_ASSERT(underlyingSource); MOZ_ASSERT(underlyingSource);
aRv = FetchStream::RetrieveInputStream(underlyingSource, aRv = BodyStream::RetrieveInputStream(underlyingSource,
getter_AddRefs(bodyStream)); getter_AddRefs(bodyStream));
// The releasing of the external source is needed in order to avoid an // The releasing of the external source is needed in order to avoid an

Просмотреть файл

@ -32,7 +32,6 @@ UNIFIED_SOURCES += [
'FetchConsumer.cpp', 'FetchConsumer.cpp',
'FetchDriver.cpp', 'FetchDriver.cpp',
'FetchObserver.cpp', 'FetchObserver.cpp',
'FetchStream.cpp',
'FetchStreamReader.cpp', 'FetchStreamReader.cpp',
'FetchUtil.cpp', 'FetchUtil.cpp',
'Headers.cpp', 'Headers.cpp',