2015-05-03 22:32:37 +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: */
|
2014-09-24 09:03:20 +04:00
|
|
|
/* 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_Fetch_h
|
|
|
|
#define mozilla_dom_Fetch_h
|
|
|
|
|
2014-10-01 22:43:26 +04:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsError.h"
|
2014-12-10 11:51:59 +03:00
|
|
|
#include "nsProxyRelease.h"
|
2014-10-01 22:43:26 +04:00
|
|
|
#include "nsString.h"
|
2014-12-10 11:51:59 +03:00
|
|
|
|
|
|
|
#include "mozilla/DebugOnly.h"
|
2017-08-29 12:31:06 +03:00
|
|
|
#include "mozilla/dom/AbortSignal.h"
|
2019-06-07 17:16:59 +03:00
|
|
|
#include "mozilla/dom/BodyConsumer.h"
|
2019-06-07 16:09:12 +03:00
|
|
|
#include "mozilla/dom/BodyStream.h"
|
2015-03-02 16:08:00 +03:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2017-08-11 04:04:55 +03:00
|
|
|
#include "mozilla/dom/FetchStreamReader.h"
|
2014-10-06 22:01:20 +04:00
|
|
|
#include "mozilla/dom/RequestBinding.h"
|
2014-09-24 09:03:20 +04:00
|
|
|
|
2014-10-06 22:01:20 +04:00
|
|
|
class nsIGlobalObject;
|
2017-05-16 13:12:24 +03:00
|
|
|
class nsIEventTarget;
|
2014-09-24 09:03:20 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
2020-11-23 19:12:02 +03:00
|
|
|
class ErrorResult;
|
|
|
|
|
2014-09-24 09:03:20 +04:00
|
|
|
namespace dom {
|
|
|
|
|
2017-02-09 12:37:54 +03:00
|
|
|
class BlobOrArrayBufferViewOrArrayBufferOrFormDataOrURLSearchParamsOrUSVString;
|
2017-08-11 04:04:55 +03:00
|
|
|
class
|
|
|
|
BlobOrArrayBufferViewOrArrayBufferOrFormDataOrURLSearchParamsOrReadableStreamOrUSVString;
|
2016-10-26 10:07:25 +03:00
|
|
|
class BlobImpl;
|
2014-10-06 22:01:20 +04:00
|
|
|
class InternalRequest;
|
2017-02-09 12:37:54 +03:00
|
|
|
class
|
|
|
|
OwningBlobOrArrayBufferViewOrArrayBufferOrFormDataOrURLSearchParamsOrUSVString;
|
2017-08-11 04:04:55 +03:00
|
|
|
struct ReadableStream;
|
2014-11-20 14:58:00 +03:00
|
|
|
class RequestOrUSVString;
|
2014-10-06 22:01:20 +04:00
|
|
|
class WorkerPrivate;
|
2018-01-31 10:24:08 +03:00
|
|
|
|
|
|
|
enum class CallerType : uint32_t;
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2014-11-20 14:58:00 +03:00
|
|
|
already_AddRefed<Promise> FetchRequest(nsIGlobalObject* aGlobal,
|
|
|
|
const RequestOrUSVString& aInput,
|
2017-02-01 23:43:37 +03:00
|
|
|
const RequestInit& aInit,
|
|
|
|
CallerType aCallerType,
|
|
|
|
ErrorResult& aRv);
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2014-12-23 18:56:19 +03:00
|
|
|
nsresult UpdateRequestReferrer(nsIGlobalObject* aGlobal,
|
|
|
|
InternalRequest* aRequest);
|
2014-09-27 03:41:15 +04:00
|
|
|
|
2017-02-09 12:37:54 +03:00
|
|
|
namespace fetch {
|
|
|
|
typedef BlobOrArrayBufferViewOrArrayBufferOrFormDataOrURLSearchParamsOrUSVString
|
|
|
|
BodyInit;
|
2017-08-11 04:04:55 +03:00
|
|
|
typedef BlobOrArrayBufferViewOrArrayBufferOrFormDataOrURLSearchParamsOrReadableStreamOrUSVString
|
|
|
|
ResponseBodyInit;
|
2017-02-09 12:37:54 +03:00
|
|
|
typedef OwningBlobOrArrayBufferViewOrArrayBufferOrFormDataOrURLSearchParamsOrUSVString
|
|
|
|
OwningBodyInit;
|
|
|
|
}; // namespace fetch
|
|
|
|
|
2014-09-24 09:03:20 +04:00
|
|
|
/*
|
|
|
|
* Creates an nsIInputStream based on the fetch specifications 'extract a byte
|
|
|
|
* stream algorithm' - http://fetch.spec.whatwg.org/#concept-bodyinit-extract.
|
|
|
|
* Stores content type in out param aContentType.
|
|
|
|
*/
|
2017-02-09 12:37:54 +03:00
|
|
|
nsresult ExtractByteStreamFromBody(const fetch::OwningBodyInit& aBodyInit,
|
2014-09-24 09:03:20 +04:00
|
|
|
nsIInputStream** aStream,
|
2016-06-02 00:02:29 +03:00
|
|
|
nsCString& aContentType,
|
|
|
|
uint64_t& aContentLength);
|
2014-09-24 09:03:20 +04:00
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
/*
|
|
|
|
* Non-owning version.
|
|
|
|
*/
|
2017-02-09 12:37:54 +03:00
|
|
|
nsresult ExtractByteStreamFromBody(const fetch::BodyInit& aBodyInit,
|
2014-09-27 03:41:15 +04:00
|
|
|
nsIInputStream** aStream,
|
2016-06-02 00:02:29 +03:00
|
|
|
nsCString& aContentType,
|
|
|
|
uint64_t& aContentLength);
|
2014-09-27 03:41:15 +04:00
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
/*
|
|
|
|
* Non-owning version. This method should go away when BodyInit will contain
|
|
|
|
* ReadableStream.
|
|
|
|
*/
|
|
|
|
nsresult ExtractByteStreamFromBody(const fetch::ResponseBodyInit& aBodyInit,
|
|
|
|
nsIInputStream** aStream,
|
|
|
|
nsCString& aContentType,
|
|
|
|
uint64_t& aContentLength);
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
/*
|
|
|
|
* FetchBody's body consumption uses nsIInputStreamPump to read from the
|
|
|
|
* underlying stream to a block of memory, which is then adopted by
|
|
|
|
* ContinueConsumeBody() and converted to the right type based on the JS
|
|
|
|
* function called.
|
|
|
|
*
|
|
|
|
* Use of the nsIInputStreamPump complicates things on the worker thread.
|
|
|
|
* The solution used here is similar to WebSockets.
|
|
|
|
* The difference is that we are only interested in completion and not data
|
|
|
|
* events, and nsIInputStreamPump can only deliver completion on the main
|
|
|
|
* thread.
|
|
|
|
*
|
|
|
|
* Before starting the pump on the main thread, we addref the FetchBody to keep
|
|
|
|
* it alive. Then we add a feature, to track the status of the worker.
|
|
|
|
*
|
|
|
|
* ContinueConsumeBody() is the function that cleans things up in both success
|
|
|
|
* and error conditions and so all callers call it with the appropriate status.
|
|
|
|
*
|
|
|
|
* Once the read is initiated on the main thread there are two possibilities.
|
|
|
|
*
|
|
|
|
* 1) Pump finishes before worker has finished Running.
|
|
|
|
* In this case we adopt the data and dispatch a runnable to the worker,
|
|
|
|
* which derefs FetchBody and removes the feature and resolves the Promise.
|
|
|
|
*
|
|
|
|
* 2) Pump still working while worker has stopped Running.
|
|
|
|
* The feature is Notify()ed and ContinueConsumeBody() is called with
|
|
|
|
* NS_BINDING_ABORTED. We first Cancel() the pump using a sync runnable to
|
|
|
|
* ensure that mFetchBody remains alive (since mConsumeBodyPump is strongly
|
|
|
|
* held by it) until pump->Cancel() is called. OnStreamComplete() will not
|
|
|
|
* do anything if the error code is NS_BINDING_ABORTED, so we don't have to
|
|
|
|
* worry about keeping anything alive.
|
|
|
|
*
|
|
|
|
* The pump is always released on the main thread.
|
|
|
|
*/
|
2014-09-27 03:41:15 +04:00
|
|
|
template <class Derived>
|
2019-06-07 16:09:12 +03:00
|
|
|
class FetchBody : public BodyStreamHolder, public AbortFollower {
|
2014-09-27 03:41:15 +04:00
|
|
|
public:
|
2020-10-29 11:02:05 +03:00
|
|
|
using BodyStreamHolder::QueryInterface;
|
|
|
|
|
|
|
|
NS_INLINE_DECL_REFCOUNTING_INHERITED(FetchBody, BodyStreamHolder)
|
2020-09-22 01:11:19 +03:00
|
|
|
|
2018-10-11 22:18:43 +03:00
|
|
|
bool GetBodyUsed(ErrorResult& aRv) const;
|
|
|
|
|
|
|
|
// For use in assertions. On success, returns true if the body is used, false
|
|
|
|
// if not. On error, this sweeps the error under the rug and returns true.
|
|
|
|
bool CheckBodyUsed() const;
|
2014-09-27 03:41:15 +04:00
|
|
|
|
2017-08-11 04:04:54 +03:00
|
|
|
already_AddRefed<Promise> ArrayBuffer(JSContext* aCx, ErrorResult& aRv) {
|
2019-06-07 17:16:59 +03:00
|
|
|
return ConsumeBody(aCx, BodyConsumer::CONSUME_ARRAYBUFFER, aRv);
|
2014-09-27 03:41:15 +04:00
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:54 +03:00
|
|
|
already_AddRefed<Promise> Blob(JSContext* aCx, ErrorResult& aRv) {
|
2019-06-07 17:16:59 +03:00
|
|
|
return ConsumeBody(aCx, BodyConsumer::CONSUME_BLOB, aRv);
|
2014-09-27 03:41:15 +04:00
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:54 +03:00
|
|
|
already_AddRefed<Promise> FormData(JSContext* aCx, ErrorResult& aRv) {
|
2019-06-07 17:16:59 +03:00
|
|
|
return ConsumeBody(aCx, BodyConsumer::CONSUME_FORMDATA, aRv);
|
2015-04-04 08:55:15 +03:00
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:54 +03:00
|
|
|
already_AddRefed<Promise> Json(JSContext* aCx, ErrorResult& aRv) {
|
2019-06-07 17:16:59 +03:00
|
|
|
return ConsumeBody(aCx, BodyConsumer::CONSUME_JSON, aRv);
|
2014-09-27 03:41:15 +04:00
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:54 +03:00
|
|
|
already_AddRefed<Promise> Text(JSContext* aCx, ErrorResult& aRv) {
|
2019-06-07 17:16:59 +03:00
|
|
|
return ConsumeBody(aCx, BodyConsumer::CONSUME_TEXT, aRv);
|
2014-09-27 03:41:15 +04:00
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:54 +03:00
|
|
|
void GetBody(JSContext* aCx, JS::MutableHandle<JSObject*> aBodyOut,
|
|
|
|
ErrorResult& aRv);
|
2017-08-11 04:04:54 +03:00
|
|
|
|
2018-10-06 20:34:24 +03:00
|
|
|
const nsACString& BodyBlobURISpec() const;
|
|
|
|
|
2018-09-11 22:13:15 +03:00
|
|
|
const nsAString& BodyLocalPath() const;
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
// If the body contains a ReadableStream body object, this method produces a
|
|
|
|
// tee() of it.
|
|
|
|
void MaybeTeeReadableStreamBody(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JSObject*> aBodyOut,
|
2017-08-11 04:04:55 +03:00
|
|
|
FetchStreamReader** aStreamReader,
|
|
|
|
nsIInputStream** aInputStream,
|
2017-08-11 04:04:55 +03:00
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
// Utility public methods accessed by various runnables.
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
// This method _must_ be called in order to set the body as used. If the body
|
|
|
|
// is a ReadableStream, this method will start reading the stream.
|
|
|
|
// More in details, this method does:
|
|
|
|
// 1) It uses an internal flag to track if the body is used. This is tracked
|
|
|
|
// separately from the ReadableStream disturbed state due to purely native
|
|
|
|
// streams.
|
|
|
|
// 2) If there is a ReadableStream reflector for the native stream it is
|
|
|
|
// Locked.
|
|
|
|
// 3) If there is a JS ReadableStream then we begin pumping it into the native
|
|
|
|
// body stream. This effectively locks and disturbs the stream.
|
|
|
|
//
|
|
|
|
// Note that JSContext is used only if there is a ReadableStream (this can
|
|
|
|
// happen because the body is a ReadableStream or because attribute body has
|
|
|
|
// already been used by content). If something goes wrong using
|
|
|
|
// ReadableStream, errors will be reported via ErrorResult and not as JS
|
|
|
|
// exceptions in JSContext. This is done in order to have a centralized error
|
|
|
|
// reporting way.
|
|
|
|
//
|
|
|
|
// Exceptions generated when reading from the ReadableStream are directly sent
|
2017-08-11 04:04:56 +03:00
|
|
|
// to the Console.
|
2017-08-11 04:04:55 +03:00
|
|
|
void SetBodyUsed(JSContext* aCx, ErrorResult& aRv);
|
2015-02-19 04:34:29 +03:00
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
const nsCString& MimeType() const { return mMimeType; }
|
2014-12-10 11:51:59 +03:00
|
|
|
|
2019-06-07 16:09:12 +03:00
|
|
|
// BodyStreamHolder
|
2017-08-11 04:04:55 +03:00
|
|
|
void NullifyStream() override {
|
|
|
|
mReadableStreamBody = nullptr;
|
2017-08-11 04:04:55 +03:00
|
|
|
mReadableStreamReader = nullptr;
|
|
|
|
mFetchStreamReader = nullptr;
|
2017-08-11 04:04:55 +03:00
|
|
|
}
|
|
|
|
|
2019-07-24 02:23:41 +03:00
|
|
|
void SetReadableStreamBody(JSObject* aBody) override {
|
|
|
|
mReadableStreamBody = aBody;
|
|
|
|
}
|
|
|
|
|
2019-07-03 19:08:30 +03:00
|
|
|
JSObject* GetReadableStreamBody() override { return mReadableStreamBody; }
|
2017-08-31 17:29:02 +03:00
|
|
|
|
2018-03-16 18:52:28 +03:00
|
|
|
void MarkAsRead() override { mBodyUsed = true; }
|
|
|
|
|
2018-08-26 15:16:21 +03:00
|
|
|
virtual AbortSignalImpl* GetSignalImpl() const = 0;
|
2017-08-29 12:31:06 +03:00
|
|
|
|
2017-08-29 12:31:07 +03:00
|
|
|
// AbortFollower
|
2020-10-22 10:42:18 +03:00
|
|
|
void RunAbortAlgorithm() override;
|
2017-08-29 12:31:06 +03:00
|
|
|
|
2019-06-07 17:16:59 +03:00
|
|
|
already_AddRefed<Promise> ConsumeBody(JSContext* aCx,
|
|
|
|
BodyConsumer::ConsumeType aType,
|
2018-11-06 16:48:07 +03:00
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
protected:
|
2017-05-16 13:12:24 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> mOwner;
|
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
// Always set whenever the FetchBody is created on the worker thread.
|
2018-01-31 10:24:08 +03:00
|
|
|
WorkerPrivate* mWorkerPrivate;
|
2017-06-20 18:53:21 +03:00
|
|
|
|
2017-08-11 04:04:54 +03:00
|
|
|
// This is the ReadableStream exposed to content. It's underlying source is a
|
2019-06-07 16:09:12 +03:00
|
|
|
// BodyStream object.
|
2017-08-11 04:04:54 +03:00
|
|
|
JS::Heap<JSObject*> mReadableStreamBody;
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
// This is the Reader used to retrieve data from the body.
|
|
|
|
JS::Heap<JSObject*> mReadableStreamReader;
|
|
|
|
RefPtr<FetchStreamReader> mFetchStreamReader;
|
|
|
|
|
2017-05-16 13:12:24 +03:00
|
|
|
explicit FetchBody(nsIGlobalObject* aOwner);
|
2014-12-10 11:51:59 +03:00
|
|
|
|
2015-02-19 04:34:29 +03:00
|
|
|
virtual ~FetchBody();
|
2014-09-27 03:41:15 +04:00
|
|
|
|
2015-02-03 23:59:32 +03:00
|
|
|
void SetMimeType();
|
2014-09-27 03:41:15 +04:00
|
|
|
|
2018-11-06 16:48:07 +03:00
|
|
|
void OverrideMimeType(const nsACString& aMimeType);
|
|
|
|
|
2017-08-29 12:31:06 +03:00
|
|
|
void SetReadableStreamBody(JSContext* aCx, JSObject* aBody);
|
2017-08-11 04:04:55 +03:00
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
private:
|
2014-09-27 03:41:15 +04:00
|
|
|
Derived* DerivedClass() const {
|
|
|
|
return static_cast<Derived*>(const_cast<FetchBody*>(this));
|
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
void LockStream(JSContext* aCx, JS::HandleObject aStream, ErrorResult& aRv);
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
bool IsOnTargetThread() { return NS_IsMainThread() == !mWorkerPrivate; }
|
|
|
|
|
|
|
|
void AssertIsOnTargetThread() { MOZ_ASSERT(IsOnTargetThread()); }
|
|
|
|
|
|
|
|
// Only ever set once, always on target thread.
|
2014-09-27 03:41:15 +04:00
|
|
|
bool mBodyUsed;
|
|
|
|
nsCString mMimeType;
|
2014-12-10 11:51:59 +03:00
|
|
|
|
2017-05-16 13:12:24 +03:00
|
|
|
// The main-thread event target for runnable dispatching.
|
|
|
|
nsCOMPtr<nsIEventTarget> mMainThreadEventTarget;
|
2014-09-27 03:41:15 +04:00
|
|
|
};
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2014-09-24 09:03:20 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_Fetch_h
|