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"
|
|
|
|
#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
|
|
|
namespace workers {
|
|
|
|
class WorkerHolder;
|
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
class FetchStreamHolder;
|
|
|
|
|
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
|
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:56 +03:00
|
|
|
static void
|
2017-08-11 04:04:55 +03:00
|
|
|
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();
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
static nsresult
|
|
|
|
RetrieveInputStream(void* aUnderlyingReadableStreamSource,
|
|
|
|
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();
|
|
|
|
|
|
|
|
static void
|
|
|
|
RequestDataCallback(JSContext* aCx, JS::HandleObject aStream,
|
|
|
|
void* aUnderlyingSource, uint8_t aFlags,
|
|
|
|
size_t aDesiredSize);
|
|
|
|
|
|
|
|
static void
|
|
|
|
WriteIntoReadRequestCallback(JSContext* aCx, JS::HandleObject aStream,
|
|
|
|
void* aUnderlyingSource, uint8_t aFlags,
|
|
|
|
void* aBuffer, size_t aLength,
|
|
|
|
size_t* aByteWritten);
|
|
|
|
|
|
|
|
static JS::Value
|
|
|
|
CancelCallback(JSContext* aCx, JS::HandleObject aStream,
|
|
|
|
void* aUnderlyingSource, uint8_t aFlags,
|
|
|
|
JS::HandleValue aReason);
|
|
|
|
|
|
|
|
static void
|
|
|
|
ClosedCallback(JSContext* aCx, JS::HandleObject aStream,
|
|
|
|
void* aUnderlyingSource, uint8_t aFlags);
|
|
|
|
|
|
|
|
static void
|
|
|
|
ErroredCallback(JSContext* aCx, JS::HandleObject aStream,
|
|
|
|
void* aUnderlyingSource, uint8_t aFlags,
|
|
|
|
JS::HandleValue reason);
|
|
|
|
|
|
|
|
static void
|
|
|
|
FinalizeCallback(void* aUnderlyingSource, uint8_t aFlags);
|
|
|
|
|
|
|
|
void
|
|
|
|
ErrorPropagation(JSContext* aCx, JS::HandleObject aStream, nsresult aRv);
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
void
|
2017-08-11 04:04:55 +03:00
|
|
|
CloseAndReleaseObjects(JSContext* aCx, JS::HandleObject aSteam);
|
|
|
|
|
|
|
|
void
|
|
|
|
ReleaseObjects();
|
2017-08-11 04:04:55 +03:00
|
|
|
|
2017-08-11 04:04:54 +03:00
|
|
|
// Common methods
|
|
|
|
|
|
|
|
enum State {
|
|
|
|
// 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,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Touched only on the target thread.
|
|
|
|
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;
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
UniquePtr<workers::WorkerHolder> mWorkerHolder;
|
2017-08-11 04:04:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // dom namespace
|
|
|
|
} // mozilla namespace
|
|
|
|
|
|
|
|
#endif // mozilla_dom_FetchStream_h
|