#pragma once
#include "Blob.h"
#include "Document.h"
#include "ExceptionCode.h"
#include "FileReaderLoader.h"
#include "FileReaderLoaderClient.h"
#include "SharedBuffer.h"
#include <JavaScriptCore/ArrayBuffer.h>
#include <wtf/CompletionHandler.h>
#include <wtf/Optional.h>
namespace WebCore {
class BlobLoader final : public FileReaderLoaderClient {
WTF_MAKE_FAST_ALLOCATED;
public:
explicit BlobLoader(CompletionHandler<void(BlobLoader&)>&&);
~BlobLoader();
void start(Blob&, ScriptExecutionContext*, FileReaderLoader::ReadType);
void cancel();
bool isLoading() const { return m_loader && m_completionHandler; }
String stringResult() const { return m_loader ? m_loader->stringResult() : String(); }
RefPtr<JSC::ArrayBuffer> arrayBufferResult() const { return m_loader ? m_loader->arrayBufferResult() : nullptr; }
Optional<ExceptionCode> errorCode() const { return m_loader ? m_loader->errorCode() : WTF::nullopt; }
private:
void didStartLoading() final { }
void didReceiveData() final { }
void didFinishLoading() final;
void didFail(ExceptionCode errorCode) final;
void complete();
std::unique_ptr<FileReaderLoader> m_loader;
CompletionHandler<void(BlobLoader&)> m_completionHandler;
};
inline BlobLoader::BlobLoader(CompletionHandler<void(BlobLoader&)>&& completionHandler)
: m_completionHandler(WTFMove(completionHandler))
{
}
inline BlobLoader::~BlobLoader()
{
if (isLoading())
cancel();
}
inline void BlobLoader::cancel()
{
if (m_loader)
m_loader->cancel();
if (m_completionHandler)
m_completionHandler(*this);
}
inline void BlobLoader::start(Blob& blob, ScriptExecutionContext* context, FileReaderLoader::ReadType readType)
{
ASSERT(!m_loader);
m_loader = makeUnique<FileReaderLoader>(readType, this);
m_loader->start(context, blob);
}
inline void BlobLoader::didFinishLoading()
{
m_completionHandler(*this);
}
inline void BlobLoader::didFail(ExceptionCode)
{
m_completionHandler(*this);
}
}