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"
|
2017-08-11 04:04:54 +03:00
|
|
|
#include "nsIAsyncInputStream.h"
|
2017-08-11 04:04:55 +03:00
|
|
|
#include "nsIObserver.h"
|
2017-08-11 04:04:54 +03:00
|
|
|
#include "nsISupportsImpl.h"
|
2017-08-11 04:04:55 +03:00
|
|
|
#include "nsWeakReference.h"
|
2017-08-11 04:04:54 +03:00
|
|
|
|
|
|
|
class nsIGlobalObject;
|
|
|
|
|
|
|
|
class nsIInputStream;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2018-03-16 18:52:29 +03:00
|
|
|
class WeakWorkerRef;
|
2017-08-11 04:04:55 +03:00
|
|
|
|
2019-07-01 23:01:17 +03:00
|
|
|
class BodyStreamHolder : public nsISupports {
|
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
|
|
|
|
|
|
|
virtual void NullifyStream() = 0;
|
|
|
|
|
|
|
|
virtual void MarkAsRead() = 0;
|
|
|
|
|
2019-07-24 02:23:41 +03:00
|
|
|
virtual void SetReadableStreamBody(JSObject* aBody) = 0;
|
|
|
|
|
2019-07-03 19:08:30 +03:00
|
|
|
virtual JSObject* GetReadableStreamBody() = 0;
|
2019-07-01 23:01:17 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~BodyStreamHolder() = default;
|
2019-06-07 16:09:12 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class BodyStream final : public nsIInputStreamCallback,
|
|
|
|
public nsIObserver,
|
|
|
|
public nsSupportsWeakReference,
|
|
|
|
private JS::ReadableStreamUnderlyingSource {
|
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().
|
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();
|
|
|
|
|
2018-11-30 01:33:43 +03:00
|
|
|
static nsresult RetrieveInputStream(
|
|
|
|
JS::ReadableStreamUnderlyingSource* aUnderlyingReadableStreamSource,
|
2017-08-11 04:04:55 +03:00
|
|
|
nsIInputStream** aInputStream);
|
|
|
|
|
2017-08-11 04:04:54 +03:00
|
|
|
private:
|
2019-06-07 16:09:12 +03:00
|
|
|
BodyStream(nsIGlobalObject* aGlobal, BodyStreamHolder* aStreamHolder,
|
|
|
|
nsIInputStream* aInputStream);
|
|
|
|
~BodyStream();
|
2017-08-11 04:04:54 +03:00
|
|
|
|
2018-03-16 18:52:29 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
void AssertIsOnOwningThread();
|
|
|
|
#else
|
|
|
|
void AssertIsOnOwningThread() {}
|
|
|
|
#endif
|
|
|
|
|
2018-11-30 01:33:43 +03:00
|
|
|
void requestData(JSContext* aCx, JS::HandleObject aStream,
|
|
|
|
size_t aDesiredSize) override;
|
2017-08-11 04:04:54 +03:00
|
|
|
|
2018-11-30 01:33:43 +03:00
|
|
|
void writeIntoReadRequestBuffer(JSContext* aCx, JS::HandleObject aStream,
|
|
|
|
void* aBuffer, size_t aLength,
|
|
|
|
size_t* aBytesWritten) override;
|
2017-08-11 04:04:54 +03:00
|
|
|
|
2018-11-30 01:33:43 +03:00
|
|
|
JS::Value cancel(JSContext* aCx, JS::HandleObject aStream,
|
|
|
|
JS::HandleValue aReason) override;
|
2017-08-11 04:04:54 +03:00
|
|
|
|
2018-11-30 01:33:43 +03:00
|
|
|
void onClosed(JSContext* aCx, JS::HandleObject aStream) override;
|
2017-08-11 04:04:54 +03:00
|
|
|
|
2018-11-30 01:33:43 +03:00
|
|
|
void onErrored(JSContext* aCx, JS::HandleObject aStream,
|
|
|
|
JS::HandleValue aReason) override;
|
|
|
|
|
|
|
|
void finalize() override;
|
2017-08-11 04:04:54 +03:00
|
|
|
|
2018-03-16 18:52:29 +03:00
|
|
|
void ErrorPropagation(JSContext* aCx, const MutexAutoLock& aProofOfLock,
|
|
|
|
JS::HandleObject aStream, nsresult aRv);
|
2017-08-11 04:04:54 +03:00
|
|
|
|
2018-03-16 18:52:29 +03:00
|
|
|
void CloseAndReleaseObjects(JSContext* aCx, const MutexAutoLock& aProofOfLock,
|
|
|
|
JS::HandleObject aSteam);
|
|
|
|
|
|
|
|
class WorkerShutdown;
|
|
|
|
|
|
|
|
void ReleaseObjects(const MutexAutoLock& aProofOfLock);
|
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.
|
|
|
|
Mutex mMutex;
|
|
|
|
|
|
|
|
// Protected by mutex.
|
2017-08-11 04:04:54 +03:00
|
|
|
State mState;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> mGlobal;
|
2019-06-07 16:09:12 +03:00
|
|
|
RefPtr<BodyStreamHolder> mStreamHolder;
|
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
|