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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_FetchStream_h
|
|
|
|
#define mozilla_dom_FetchStream_h
|
|
|
|
|
|
|
|
#include "Fetch.h"
|
|
|
|
#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 {
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
class FetchStreamHolder;
|
2018-03-16 18:52:29 +03:00
|
|
|
class WeakWorkerRef;
|
2017-08-11 04:04:55 +03:00
|
|
|
|
2017-08-11 04:04:54 +03:00
|
|
|
class FetchStream final : public nsIInputStreamCallback,
|
2017-08-11 04:04:55 +03:00
|
|
|
public nsIObserver,
|
|
|
|
public nsSupportsWeakReference,
|
2018-11-30 01:33:43 +03:00
|
|
|
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
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
static void Create(JSContext* aCx, FetchStreamHolder* aStreamHolder,
|
|
|
|
nsIGlobalObject* aGlobal, nsIInputStream* aInputStream,
|
2017-08-11 04:04:56 +03:00
|
|
|
JS::MutableHandle<JSObject*> aStream, 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:
|
2017-08-11 04:04:55 +03:00
|
|
|
FetchStream(nsIGlobalObject* aGlobal, FetchStreamHolder* aStreamHolder,
|
|
|
|
nsIInputStream* aInputStream);
|
2017-08-11 04:04:54 +03:00
|
|
|
~FetchStream();
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2018-03-16 18:52:29 +03:00
|
|
|
// We need a mutex because JS engine can release FetchStream on a non-owning
|
|
|
|
// 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;
|
2017-08-11 04:04:55 +03:00
|
|
|
RefPtr<FetchStreamHolder> mStreamHolder;
|
|
|
|
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
|
|
|
|
|
|
|
|
#endif // mozilla_dom_FetchStream_h
|