ShareDataReader.cpp [plain text]
#include "config.h"
#include "ShareDataReader.h"
#include "BlobLoader.h"
#include "Document.h"
#include "SharedBuffer.h"
namespace WebCore {
ShareDataReader::ShareDataReader(CompletionHandler<void(ExceptionOr<ShareDataWithParsedURL&>)>&& completionHandler)
: m_completionHandler(WTFMove(completionHandler))
{
}
ShareDataReader::~ShareDataReader()
{
cancel();
}
void ShareDataReader::start(Document* document, ShareDataWithParsedURL&& shareData)
{
m_filesReadSoFar = 0;
m_shareData = WTFMove(shareData);
int count = 0;
m_pendingFileLoads.reserveInitialCapacity(m_shareData.shareData.files.size());
for (auto& blob : m_shareData.shareData.files) {
m_pendingFileLoads.uncheckedAppend(makeUniqueRef<BlobLoader>([this, count, fileName = blob->name()](BlobLoader&) {
this->didFinishLoading(count, fileName);
}));
m_pendingFileLoads.last()->start(*blob, document, FileReaderLoader::ReadAsArrayBuffer);
++count;
}
}
void ShareDataReader::didFinishLoading(int loadIndex, const String& fileName)
{
if (m_pendingFileLoads.isEmpty()) {
return;
}
if (m_pendingFileLoads[loadIndex]->errorCode()) {
if (auto completionHandler = std::exchange(m_completionHandler, { }))
completionHandler(Exception { AbortError, "Abort due to error while reading files."_s });
cancel();
return;
}
auto arrayBuffer = m_pendingFileLoads[loadIndex]->arrayBufferResult();
RawFile file;
file.fileName = fileName;
file.fileData = SharedBuffer::create(static_cast<const unsigned char*>(arrayBuffer->data()), arrayBuffer->byteLength());
m_shareData.files.append(WTFMove(file));
m_filesReadSoFar++;
if (m_filesReadSoFar == static_cast<int>(m_pendingFileLoads.size())) {
m_pendingFileLoads.clear();
if (auto completionHandler = std::exchange(m_completionHandler, { }))
completionHandler(m_shareData);
}
}
void ShareDataReader::cancel()
{
std::exchange(m_pendingFileLoads, { });
}
}