2021-10-06 21:43:01 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_ReadableStream_h
|
|
|
|
#define mozilla_dom_ReadableStream_h
|
|
|
|
|
|
|
|
#include "js/TypeDecls.h"
|
|
|
|
#include "js/Value.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/ErrorResult.h"
|
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
|
|
|
#include "mozilla/dom/QueuingStrategyBinding.h"
|
2022-01-15 00:09:20 +03:00
|
|
|
#include "mozilla/dom/ReadableStreamController.h"
|
2021-10-06 21:43:01 +03:00
|
|
|
#include "mozilla/dom/ReadableStreamDefaultController.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "nsWrapperCache.h"
|
|
|
|
|
|
|
|
#ifndef MOZ_DOM_STREAMS
|
|
|
|
# error "Shouldn't be compiling with this header without MOZ_DOM_STREAMS set"
|
|
|
|
#endif
|
|
|
|
|
2022-02-19 01:56:33 +03:00
|
|
|
namespace mozilla::dom {
|
2021-10-06 21:43:01 +03:00
|
|
|
|
|
|
|
class Promise;
|
2022-01-05 01:33:22 +03:00
|
|
|
class ReadableStreamGenericReader;
|
2021-10-06 21:43:01 +03:00
|
|
|
class ReadableStreamDefaultReader;
|
2022-01-05 01:33:22 +03:00
|
|
|
class ReadableStreamGenericReader;
|
2021-10-06 21:43:01 +03:00
|
|
|
struct ReadableStreamGetReaderOptions;
|
2022-01-05 01:33:22 +03:00
|
|
|
struct ReadIntoRequest;
|
2022-02-18 13:30:15 +03:00
|
|
|
class WritableStream;
|
2022-02-25 23:11:40 +03:00
|
|
|
struct ReadableWritablePair;
|
|
|
|
struct StreamPipeOptions;
|
2022-01-05 01:33:22 +03:00
|
|
|
|
|
|
|
using ReadableStreamReader =
|
|
|
|
ReadableStreamDefaultReaderOrReadableStreamBYOBReader;
|
|
|
|
using OwningReadableStreamReader =
|
|
|
|
OwningReadableStreamDefaultReaderOrReadableStreamBYOBReader;
|
2022-01-15 00:09:20 +03:00
|
|
|
class NativeUnderlyingSource;
|
|
|
|
class BodyStreamHolder;
|
2021-10-06 21:43:01 +03:00
|
|
|
|
|
|
|
class ReadableStream final : public nsISupports, public nsWrapperCache {
|
|
|
|
public:
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(ReadableStream)
|
|
|
|
|
|
|
|
protected:
|
|
|
|
~ReadableStream();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> mGlobal;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ReadableStream(const GlobalObject& aGlobal);
|
2021-10-06 21:43:02 +03:00
|
|
|
explicit ReadableStream(nsIGlobalObject* aGlobal);
|
2021-10-06 21:43:01 +03:00
|
|
|
|
|
|
|
enum class ReaderState { Readable, Closed, Errored };
|
|
|
|
|
|
|
|
// Slot Getter/Setters:
|
|
|
|
public:
|
2022-01-05 01:33:21 +03:00
|
|
|
ReadableStreamController* Controller() { return mController; }
|
|
|
|
ReadableStreamDefaultController* DefaultController() {
|
|
|
|
MOZ_ASSERT(mController && mController->IsDefault());
|
|
|
|
return mController->AsDefault();
|
|
|
|
}
|
|
|
|
void SetController(ReadableStreamController* aController) {
|
2021-10-06 21:43:01 +03:00
|
|
|
mController = aController;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Disturbed() const { return mDisturbed; }
|
|
|
|
void SetDisturbed(bool aDisturbed) { mDisturbed = aDisturbed; }
|
|
|
|
|
2022-01-05 01:33:22 +03:00
|
|
|
ReadableStreamGenericReader* GetReader() { return mReader; }
|
|
|
|
void SetReader(ReadableStreamGenericReader* aReader);
|
|
|
|
|
|
|
|
ReadableStreamDefaultReader* GetDefaultReader();
|
2021-10-06 21:43:01 +03:00
|
|
|
|
|
|
|
ReaderState State() const { return mState; }
|
|
|
|
void SetState(const ReaderState& aState) { mState = aState; }
|
|
|
|
|
|
|
|
JS::Value StoredError() const { return mStoredError; }
|
|
|
|
void SetStoredError(JS::HandleValue aStoredError) {
|
|
|
|
mStoredError = aStoredError;
|
|
|
|
}
|
|
|
|
|
2022-01-05 01:33:23 +03:00
|
|
|
UnderlyingSourceErrorCallbackHelper* GetErrorAlgorithm() const {
|
|
|
|
return mErrorAlgorithm;
|
|
|
|
}
|
|
|
|
void SetErrorAlgorithm(UnderlyingSourceErrorCallbackHelper* aErrorAlgorithm) {
|
|
|
|
mErrorAlgorithm = aErrorAlgorithm;
|
|
|
|
}
|
|
|
|
|
2022-01-15 00:09:20 +03:00
|
|
|
void SetNativeUnderlyingSource(BodyStreamHolder* aUnderlyingSource);
|
|
|
|
BodyStreamHolder* GetNativeUnderlyingSource() {
|
|
|
|
return mNativeUnderlyingSource;
|
|
|
|
}
|
|
|
|
bool HasNativeUnderlyingSource() { return mNativeUnderlyingSource; }
|
|
|
|
|
|
|
|
void ReleaseObjects();
|
|
|
|
|
2021-10-06 21:43:01 +03:00
|
|
|
public:
|
|
|
|
nsIGlobalObject* GetParentObject() const { return mGlobal; }
|
|
|
|
|
|
|
|
JSObject* WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) override;
|
|
|
|
|
2022-01-18 17:39:44 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT static already_AddRefed<ReadableStream> Create(
|
2022-01-15 00:09:20 +03:00
|
|
|
JSContext* aCx, nsIGlobalObject* aGlobal,
|
|
|
|
BodyStreamHolder* aUnderlyingSource, ErrorResult& aRv);
|
|
|
|
|
2021-10-06 21:43:01 +03:00
|
|
|
// IDL Methods
|
2022-01-07 23:46:19 +03:00
|
|
|
// TODO: Use MOZ_CAN_RUN_SCRIPT when IDL constructors can use it (bug 1749042)
|
|
|
|
MOZ_CAN_RUN_SCRIPT_BOUNDARY static already_AddRefed<ReadableStream>
|
|
|
|
Constructor(const GlobalObject& aGlobal,
|
|
|
|
const Optional<JS::Handle<JSObject*>>& aUnderlyingSource,
|
|
|
|
const QueuingStrategy& aStrategy, ErrorResult& aRv);
|
2021-10-06 21:43:01 +03:00
|
|
|
|
|
|
|
bool Locked() const;
|
|
|
|
|
2022-01-07 23:46:19 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT already_AddRefed<Promise> Cancel(
|
|
|
|
JSContext* cx, JS::Handle<JS::Value> aReason, ErrorResult& aRv);
|
2021-10-06 21:43:01 +03:00
|
|
|
|
2022-03-01 01:31:42 +03:00
|
|
|
void GetReader(const ReadableStreamGetReaderOptions& aOptions,
|
2022-01-05 01:33:22 +03:00
|
|
|
OwningReadableStreamReader& resultReader, ErrorResult& aRv);
|
2021-10-06 21:43:01 +03:00
|
|
|
|
2022-02-25 23:11:40 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT already_AddRefed<ReadableStream> PipeThrough(
|
|
|
|
const ReadableWritablePair& aTransform, const StreamPipeOptions& aOptions,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2022-02-18 13:30:15 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT already_AddRefed<Promise> PipeTo(
|
|
|
|
WritableStream& aDestinaton, const StreamPipeOptions& aOptions,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2022-01-07 23:46:19 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT void Tee(JSContext* aCx,
|
|
|
|
nsTArray<RefPtr<ReadableStream>>& aResult,
|
|
|
|
ErrorResult& aRv);
|
2021-10-06 21:43:02 +03:00
|
|
|
|
2021-10-06 21:43:01 +03:00
|
|
|
// Internal Slots:
|
|
|
|
private:
|
2022-01-05 01:33:21 +03:00
|
|
|
RefPtr<ReadableStreamController> mController;
|
2021-10-06 21:43:01 +03:00
|
|
|
bool mDisturbed = false;
|
2022-01-05 01:33:22 +03:00
|
|
|
RefPtr<ReadableStreamGenericReader> mReader;
|
2021-10-06 21:43:01 +03:00
|
|
|
ReaderState mState = ReaderState::Readable;
|
|
|
|
JS::Heap<JS::Value> mStoredError;
|
2022-01-05 01:33:23 +03:00
|
|
|
|
|
|
|
// Optional Callback for erroring a stream.
|
|
|
|
RefPtr<UnderlyingSourceErrorCallbackHelper> mErrorAlgorithm;
|
2022-01-15 00:09:20 +03:00
|
|
|
|
|
|
|
// Optional strong reference to an Underlying Source; This
|
|
|
|
// exists because NativeUnderlyingSource callbacks don't hold
|
|
|
|
// a strong reference to the underlying source: So we need
|
|
|
|
// something else to hold onto that. As well, some of the integration
|
|
|
|
// desires the ability to extract the underlying source from the
|
|
|
|
// ReadableStream.
|
|
|
|
//
|
|
|
|
// While theoretically this ought to be some base class type to support
|
|
|
|
// multiple native underlying source types, I'm not sure what base class
|
|
|
|
// makes any sense for BodyStream, and given there's only body streams
|
|
|
|
// as the underlying source right now, I'm going to punt that problem to
|
|
|
|
// the future where we need to provide other native underlying sources
|
|
|
|
// (i.e. perhaps WebTransport.)
|
|
|
|
RefPtr<BodyStreamHolder> mNativeUnderlyingSource;
|
2021-10-06 21:43:01 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
extern bool IsReadableStreamLocked(ReadableStream* aStream);
|
2022-01-06 14:05:28 +03:00
|
|
|
|
2021-10-06 21:43:01 +03:00
|
|
|
extern double ReadableStreamGetNumReadRequests(ReadableStream* aStream);
|
|
|
|
|
|
|
|
extern void ReadableStreamError(JSContext* aCx, ReadableStream* aStream,
|
|
|
|
JS::Handle<JS::Value> aValue, ErrorResult& aRv);
|
|
|
|
|
2022-02-04 23:35:14 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT extern void ReadableStreamClose(JSContext* aCx,
|
|
|
|
ReadableStream* aStream,
|
|
|
|
ErrorResult& aRv);
|
2021-10-06 21:43:01 +03:00
|
|
|
|
2022-02-04 23:35:14 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT extern void ReadableStreamFulfillReadRequest(
|
|
|
|
JSContext* aCx, ReadableStream* aStream, JS::Handle<JS::Value> aChunk,
|
|
|
|
bool done, ErrorResult& aRv);
|
2021-10-06 21:43:01 +03:00
|
|
|
|
|
|
|
extern void ReadableStreamAddReadRequest(ReadableStream* aStream,
|
|
|
|
ReadRequest* aReadRequest);
|
2022-01-05 01:33:22 +03:00
|
|
|
extern void ReadableStreamAddReadIntoRequest(ReadableStream* aStream,
|
|
|
|
ReadIntoRequest* aReadIntoRequest);
|
2021-10-06 21:43:01 +03:00
|
|
|
|
2022-01-07 23:46:19 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT extern already_AddRefed<Promise> ReadableStreamCancel(
|
2021-10-06 21:43:01 +03:00
|
|
|
JSContext* aCx, ReadableStream* aStream, JS::Handle<JS::Value> aError,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2021-10-06 21:43:02 +03:00
|
|
|
extern already_AddRefed<ReadableStreamDefaultReader>
|
2022-03-01 01:31:41 +03:00
|
|
|
AcquireReadableStreamDefaultReader(ReadableStream* aStream, ErrorResult& aRv);
|
2021-10-06 21:43:02 +03:00
|
|
|
|
2022-01-06 14:05:28 +03:00
|
|
|
extern bool ReadableStreamHasBYOBReader(ReadableStream* aStream);
|
2022-01-05 01:33:22 +03:00
|
|
|
extern bool ReadableStreamHasDefaultReader(ReadableStream* aStream);
|
2022-01-05 01:33:21 +03:00
|
|
|
|
2022-01-07 23:46:19 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT extern already_AddRefed<ReadableStream>
|
|
|
|
CreateReadableByteStream(JSContext* aCx, nsIGlobalObject* aGlobal,
|
|
|
|
UnderlyingSourceStartCallbackHelper* aStartAlgorithm,
|
|
|
|
UnderlyingSourcePullCallbackHelper* aPullAlgorithm,
|
|
|
|
UnderlyingSourceCancelCallbackHelper* aCancelAlgorithm,
|
|
|
|
ErrorResult& aRv);
|
2022-01-05 01:33:24 +03:00
|
|
|
|
2022-02-19 01:56:33 +03:00
|
|
|
} // namespace mozilla::dom
|
2021-10-06 21:43:01 +03:00
|
|
|
|
|
|
|
#endif // mozilla_dom_ReadableStream_h
|