#pragma once
#if ENABLE(FETCH_API)
#include "FetchBodyOwner.h"
#include "FetchHeaders.h"
#include "ResourceResponse.h"
namespace JSC {
class ArrayBuffer;
};
namespace WebCore {
class Dictionary;
class FetchRequest;
class ReadableStreamSource;
typedef int ExceptionCode;
class FetchResponse final : public FetchBodyOwner {
public:
using Type = ResourceResponse::Type;
static Ref<FetchResponse> create(ScriptExecutionContext& context) { return adoptRef(*new FetchResponse(context, { }, FetchHeaders::create(FetchHeaders::Guard::Response), ResourceResponse())); }
static Ref<FetchResponse> error(ScriptExecutionContext&);
static RefPtr<FetchResponse> redirect(ScriptExecutionContext&, const String&, int, ExceptionCode&);
using FetchPromise = DOMPromise<FetchResponse>;
static void fetch(ScriptExecutionContext&, FetchRequest&, const Dictionary&, FetchPromise&&);
static void fetch(ScriptExecutionContext&, const String&, const Dictionary&, FetchPromise&&);
void initializeWith(const Dictionary&, ExceptionCode&);
Type type() const { return m_response.type(); }
const String& url() const;
bool redirected() const { return m_response.isRedirected(); }
int status() const { return m_response.httpStatusCode(); }
bool ok() const { return m_response.isSuccessful(); }
const String& statusText() const { return m_response.httpStatusText(); }
FetchHeaders& headers() { return m_headers; }
RefPtr<FetchResponse> clone(ScriptExecutionContext&, ExceptionCode&);
#if ENABLE(STREAMS_API)
ReadableStreamSource* createReadableStreamSource();
void consumeBodyAsStream();
void cancel();
#endif
private:
FetchResponse(ScriptExecutionContext&, FetchBody&&, Ref<FetchHeaders>&&, ResourceResponse&&);
static void startFetching(ScriptExecutionContext&, const FetchRequest&, FetchPromise&&);
void stop() final;
const char* activeDOMObjectName() const final;
bool canSuspendForDocumentSuspension() const final;
class BodyLoader final : public FetchLoaderClient {
public:
BodyLoader(FetchResponse&, FetchPromise&&);
bool start(ScriptExecutionContext&, const FetchRequest&);
void stop();
#if ENABLE(STREAMS_API)
RefPtr<SharedBuffer> startStreaming();
#endif
private:
void didSucceed() final;
void didFail() final;
void didReceiveResponse(const ResourceResponse&) final;
void didReceiveData(const char*, size_t) final;
void didFinishLoadingAsArrayBuffer(RefPtr<ArrayBuffer>&&) final;
FetchResponse& m_response;
Optional<FetchPromise> m_promise;
std::unique_ptr<FetchLoader> m_loader;
};
ResourceResponse m_response;
Ref<FetchHeaders> m_headers;
Optional<BodyLoader> m_bodyLoader;
mutable String m_responseURL;
};
}
#endif // ENABLE(FETCH_API)