2017-08-11 04:04:54 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
2019-06-07 16:09:12 +03:00
|
|
|
#ifndef mozilla_dom_BodyStream_h
|
|
|
|
#define mozilla_dom_BodyStream_h
|
2017-08-11 04:04:54 +03:00
|
|
|
|
|
|
|
#include "jsapi.h"
|
2018-11-22 20:34:39 +03:00
|
|
|
#include "js/Stream.h"
|
2022-01-15 00:09:20 +03:00
|
|
|
#include "mozilla/AlreadyAddRefed.h"
|
2022-04-13 21:57:48 +03:00
|
|
|
#include "mozilla/dom/ByteStreamHelpers.h"
|
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
2017-08-11 04:04:54 +03:00
|
|
|
#include "nsIAsyncInputStream.h"
|
2020-04-29 12:02:05 +03:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2017-08-11 04:04:55 +03:00
|
|
|
#include "nsIObserver.h"
|
2017-08-11 04:04:54 +03:00
|
|
|
#include "nsISupportsImpl.h"
|
2020-04-29 12:02:05 +03:00
|
|
|
#include "nsNetCID.h"
|
2017-08-11 04:04:55 +03:00
|
|
|
#include "nsWeakReference.h"
|
2020-04-29 12:02:05 +03:00
|
|
|
#include "mozilla/Mutex.h"
|
2017-08-11 04:04:54 +03:00
|
|
|
|
|
|
|
class nsIGlobalObject;
|
|
|
|
|
|
|
|
class nsIInputStream;
|
|
|
|
|
|
|
|
namespace mozilla {
|
2020-11-23 19:12:02 +03:00
|
|
|
class ErrorResult;
|
|
|
|
|
2017-08-11 04:04:54 +03:00
|
|
|
namespace dom {
|
|
|
|
|
2019-09-14 01:18:13 +03:00
|
|
|
class BodyStream;
|
2018-03-16 18:52:29 +03:00
|
|
|
class WeakWorkerRef;
|
2022-01-15 00:09:20 +03:00
|
|
|
class ReadableStream;
|
2022-01-18 13:08:17 +03:00
|
|
|
class ReadableStreamController;
|
2022-01-15 00:09:20 +03:00
|
|
|
|
2022-03-08 22:29:14 +03:00
|
|
|
class BodyStreamUnderlyingSourceAlgorithms;
|
2017-08-11 04:04:55 +03:00
|
|
|
|
2019-07-01 23:01:17 +03:00
|
|
|
class BodyStreamHolder : public nsISupports {
|
2019-09-14 01:18:13 +03:00
|
|
|
friend class BodyStream;
|
2022-03-08 22:29:14 +03:00
|
|
|
friend class BodyStreamUnderlyingSourceAlgorithms;
|
2022-01-15 00:09:20 +03:00
|
|
|
friend class BodyStreamUnderlyingSourceErrorCallbackHelper;
|
2019-09-14 01:18:13 +03:00
|
|
|
|
2019-06-07 16:09:12 +03:00
|
|
|
public:
|
2019-07-01 23:01:17 +03:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS(BodyStreamHolder)
|
2019-06-07 16:09:12 +03:00
|
|
|
|
2019-09-14 01:18:13 +03:00
|
|
|
BodyStreamHolder();
|
|
|
|
|
2019-06-07 16:09:12 +03:00
|
|
|
virtual void NullifyStream() = 0;
|
|
|
|
|
|
|
|
virtual void MarkAsRead() = 0;
|
|
|
|
|
2022-01-15 00:09:20 +03:00
|
|
|
virtual void SetReadableStreamBody(ReadableStream* aBody) = 0;
|
|
|
|
virtual ReadableStream* GetReadableStreamBody() = 0;
|
2019-07-01 23:01:17 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~BodyStreamHolder() = default;
|
2019-09-14 01:18:13 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
void StoreBodyStream(BodyStream* aBodyStream);
|
2022-01-15 00:09:20 +03:00
|
|
|
already_AddRefed<BodyStream> TakeBodyStream() {
|
|
|
|
MOZ_ASSERT_IF(mStreamCreated, mBodyStream);
|
|
|
|
return mBodyStream.forget();
|
|
|
|
}
|
|
|
|
BodyStream* GetBodyStream() { return mBodyStream; }
|
2019-09-14 01:18:13 +03:00
|
|
|
|
2022-01-15 00:09:20 +03:00
|
|
|
RefPtr<BodyStream> mBodyStream;
|
2019-10-02 10:58:10 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool mStreamCreated = false;
|
|
|
|
#endif
|
2019-06-07 16:09:12 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class BodyStream final : public nsIInputStreamCallback,
|
|
|
|
public nsIObserver,
|
2022-03-23 17:58:11 +03:00
|
|
|
public nsSupportsWeakReference,
|
2022-04-13 21:57:48 +03:00
|
|
|
public SingleWriterLockOwner {
|
2019-09-14 01:18:13 +03:00
|
|
|
friend class BodyStreamHolder;
|
|
|
|
|
2017-08-11 04:04:54 +03:00
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIINPUTSTREAMCALLBACK
|
2017-08-11 04:04:55 +03:00
|
|
|
NS_DECL_NSIOBSERVER
|
2017-08-11 04:04:54 +03:00
|
|
|
|
2019-07-24 02:23:41 +03:00
|
|
|
// This method creates a JS ReadableStream object and it assigns it to the
|
|
|
|
// aStreamHolder calling SetReadableStreamBody().
|
2022-01-18 17:39:45 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT_BOUNDARY
|
2019-06-07 16:09:12 +03:00
|
|
|
static void Create(JSContext* aCx, BodyStreamHolder* aStreamHolder,
|
2017-08-11 04:04:55 +03:00
|
|
|
nsIGlobalObject* aGlobal, nsIInputStream* aInputStream,
|
2019-07-24 02:23:41 +03:00
|
|
|
ErrorResult& aRv);
|
2017-08-11 04:04:54 +03:00
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
void Close();
|
|
|
|
|
2022-03-23 17:58:11 +03:00
|
|
|
bool OnWritingThread() const override {
|
|
|
|
#ifdef MOZ_THREAD_SAFETY_OWNERSHIP_CHECKS_SUPPORTED
|
|
|
|
return _mOwningThread.IsCurrentThread();
|
|
|
|
#else
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-01-15 00:09:20 +03:00
|
|
|
static nsresult RetrieveInputStream(BodyStreamHolder* aStream,
|
|
|
|
nsIInputStream** aInputStream);
|
2017-08-11 04:04:55 +03:00
|
|
|
|
2017-08-11 04:04:54 +03:00
|
|
|
private:
|
2019-06-07 16:09:12 +03:00
|
|
|
BodyStream(nsIGlobalObject* aGlobal, BodyStreamHolder* aStreamHolder,
|
|
|
|
nsIInputStream* aInputStream);
|
2022-01-15 00:09:20 +03:00
|
|
|
~BodyStream() = default;
|
2017-08-11 04:04:54 +03:00
|
|
|
|
2018-03-16 18:52:29 +03:00
|
|
|
#ifdef DEBUG
|
2022-03-23 17:58:11 +03:00
|
|
|
void AssertIsOnOwningThread() const;
|
2018-03-16 18:52:29 +03:00
|
|
|
#else
|
2022-03-23 17:58:11 +03:00
|
|
|
void AssertIsOnOwningThread() const {}
|
2018-03-16 18:52:29 +03:00
|
|
|
#endif
|
|
|
|
|
2022-01-15 00:09:20 +03:00
|
|
|
public:
|
|
|
|
// Cancel Callback
|
2022-01-18 13:08:17 +03:00
|
|
|
already_AddRefed<Promise> CancelCallback(
|
2022-01-15 00:09:20 +03:00
|
|
|
JSContext* aCx, const Optional<JS::Handle<JS::Value>>& aReason,
|
2022-01-18 13:08:17 +03:00
|
|
|
ErrorResult& aRv);
|
2022-01-15 00:09:20 +03:00
|
|
|
|
|
|
|
// Pull Callback
|
2022-01-18 13:08:17 +03:00
|
|
|
already_AddRefed<Promise> PullCallback(JSContext* aCx,
|
|
|
|
ReadableStreamController& aController,
|
|
|
|
ErrorResult& aRv);
|
2022-01-15 00:09:20 +03:00
|
|
|
|
2022-01-18 13:08:17 +03:00
|
|
|
void ErrorCallback();
|
2022-01-15 00:09:20 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Fills a buffer with bytes from the stream.
|
|
|
|
void WriteIntoReadRequestBuffer(JSContext* aCx, ReadableStream* aStream,
|
2022-04-05 22:15:26 +03:00
|
|
|
JS::Handle<JSObject*> aBuffer,
|
|
|
|
uint32_t aLength, uint32_t* aByteWritten);
|
2022-01-15 00:09:20 +03:00
|
|
|
|
2022-01-18 17:39:45 +03:00
|
|
|
// This is a script boundary until Bug 1750605 is resolved and allows us
|
|
|
|
// to replace this with MOZ_CAN_RUN_SCRIPT.
|
|
|
|
MOZ_CAN_RUN_SCRIPT_BOUNDARY void EnqueueChunkWithSizeIntoStream(
|
|
|
|
JSContext* aCx, ReadableStream* aStream, uint64_t bytes,
|
|
|
|
ErrorResult& aRv);
|
2022-01-15 00:09:20 +03:00
|
|
|
|
2022-03-23 17:58:11 +03:00
|
|
|
void ErrorPropagation(JSContext* aCx,
|
|
|
|
const MutexSingleWriterAutoLock& aProofOfLock,
|
|
|
|
ReadableStream* aStream, nsresult aRv) REQUIRES(mMutex);
|
2022-01-15 00:09:20 +03:00
|
|
|
|
2022-02-04 23:35:14 +03:00
|
|
|
// TODO: convert this to MOZ_CAN_RUN_SCRIPT (bug 1750605)
|
|
|
|
MOZ_CAN_RUN_SCRIPT_BOUNDARY void CloseAndReleaseObjects(
|
2022-03-23 17:58:11 +03:00
|
|
|
JSContext* aCx, const MutexSingleWriterAutoLock& aProofOfLock,
|
|
|
|
ReadableStream* aStream) REQUIRES(mMutex);
|
2018-03-16 18:52:29 +03:00
|
|
|
|
|
|
|
class WorkerShutdown;
|
|
|
|
|
2022-03-23 17:58:11 +03:00
|
|
|
void ReleaseObjects(const MutexSingleWriterAutoLock& aProofOfLock)
|
|
|
|
REQUIRES(mMutex);
|
2017-08-11 04:04:55 +03:00
|
|
|
|
|
|
|
void ReleaseObjects();
|
2017-08-11 04:04:55 +03:00
|
|
|
|
2017-08-11 04:04:54 +03:00
|
|
|
// Common methods
|
|
|
|
|
|
|
|
enum State {
|
2018-03-16 18:52:28 +03:00
|
|
|
// This is the beginning state before any reading operation.
|
|
|
|
eInitializing,
|
|
|
|
|
2017-08-11 04:04:54 +03:00
|
|
|
// RequestDataCallback has not been called yet. We haven't started to read
|
|
|
|
// data from the stream yet.
|
|
|
|
eWaiting,
|
|
|
|
|
|
|
|
// We are reading data in a separate I/O thread.
|
|
|
|
eReading,
|
|
|
|
|
|
|
|
// We are ready to write something in the JS Buffer.
|
|
|
|
eWriting,
|
|
|
|
|
|
|
|
// After a writing, we want to check if the stream is closed. After the
|
|
|
|
// check, we go back to eWaiting. If a reading request happens in the
|
|
|
|
// meantime, we move to eReading state.
|
|
|
|
eChecking,
|
|
|
|
|
|
|
|
// Operation completed.
|
|
|
|
eClosed,
|
|
|
|
};
|
|
|
|
|
2019-06-07 16:09:12 +03:00
|
|
|
// We need a mutex because JS engine can release BodyStream on a non-owning
|
2018-03-16 18:52:29 +03:00
|
|
|
// thread. We must be sure that the releasing of resources doesn't trigger
|
|
|
|
// race conditions.
|
2022-03-23 17:58:11 +03:00
|
|
|
MutexSingleWriter mMutex;
|
2018-03-16 18:52:29 +03:00
|
|
|
|
|
|
|
// Protected by mutex.
|
2022-03-23 17:58:11 +03:00
|
|
|
State mState GUARDED_BY(mMutex); // all writes are from the owning thread
|
2017-08-11 04:04:54 +03:00
|
|
|
|
2022-03-23 17:58:11 +03:00
|
|
|
// mGlobal is set on creation, and isn't modified off the owning thread.
|
|
|
|
// It isn't set to nullptr until ReleaseObjects() runs.
|
2017-08-11 04:04:54 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> mGlobal;
|
2022-03-23 17:58:11 +03:00
|
|
|
RefPtr<BodyStreamHolder> mStreamHolder GUARDED_BY(mMutex);
|
2017-08-11 04:04:55 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> mOwningEventTarget;
|
2017-08-11 04:04:54 +03:00
|
|
|
|
|
|
|
// This is the original inputStream received during the CTOR. It will be
|
|
|
|
// converted into an nsIAsyncInputStream and stored into mInputStream at the
|
|
|
|
// first use.
|
|
|
|
nsCOMPtr<nsIInputStream> mOriginalInputStream;
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> mInputStream;
|
|
|
|
|
2018-03-16 18:52:29 +03:00
|
|
|
RefPtr<WeakWorkerRef> mWorkerRef;
|
2017-08-11 04:04:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2019-06-07 16:09:12 +03:00
|
|
|
#endif // mozilla_dom_BodyStream_h
|