#include "config.h"
#include "FileReader.h"
#include "EventNames.h"
#include "ExceptionCode.h"
#include "File.h"
#include "Logging.h"
#include "ProgressEvent.h"
#include "ScriptExecutionContext.h"
#include <runtime/ArrayBuffer.h>
#include <wtf/CurrentTime.h>
#include <wtf/text/CString.h>
namespace WebCore {
static const auto progressNotificationInterval = 50ms;
Ref<FileReader> FileReader::create(ScriptExecutionContext& context)
{
Ref<FileReader> fileReader = adoptRef(*new FileReader(context));
fileReader->suspendIfNeeded();
return fileReader;
}
FileReader::FileReader(ScriptExecutionContext& context)
: ActiveDOMObject(&context)
, m_state(EMPTY)
, m_aborting(false)
, m_readType(FileReaderLoader::ReadAsBinaryString)
{
}
FileReader::~FileReader()
{
terminate();
}
bool FileReader::canSuspendForDocumentSuspension() const
{
return false;
}
const char* FileReader::activeDOMObjectName() const
{
return "FileReader";
}
void FileReader::stop()
{
terminate();
}
void FileReader::readAsArrayBuffer(Blob* blob, ExceptionCode& ec)
{
if (!blob)
return;
LOG(FileAPI, "FileReader: reading as array buffer: %s %s\n", blob->url().string().utf8().data(), is<File>(*blob) ? downcast<File>(*blob).path().utf8().data() : "");
readInternal(*blob, FileReaderLoader::ReadAsArrayBuffer, ec);
}
void FileReader::readAsBinaryString(Blob* blob, ExceptionCode& ec)
{
if (!blob)
return;
LOG(FileAPI, "FileReader: reading as binary: %s %s\n", blob->url().string().utf8().data(), is<File>(*blob) ? downcast<File>(*blob).path().utf8().data() : "");
readInternal(*blob, FileReaderLoader::ReadAsBinaryString, ec);
}
void FileReader::readAsText(Blob* blob, const String& encoding, ExceptionCode& ec)
{
if (!blob)
return;
LOG(FileAPI, "FileReader: reading as text: %s %s\n", blob->url().string().utf8().data(), is<File>(*blob) ? downcast<File>(*blob).path().utf8().data() : "");
m_encoding = encoding;
readInternal(*blob, FileReaderLoader::ReadAsText, ec);
}
void FileReader::readAsDataURL(Blob* blob, ExceptionCode& ec)
{
if (!blob)
return;
LOG(FileAPI, "FileReader: reading as data URL: %s %s\n", blob->url().string().utf8().data(), is<File>(*blob) ? downcast<File>(*blob).path().utf8().data() : "");
readInternal(*blob, FileReaderLoader::ReadAsDataURL, ec);
}
void FileReader::readInternal(Blob& blob, FileReaderLoader::ReadType type, ExceptionCode& ec)
{
if (m_state == LOADING) {
ec = INVALID_STATE_ERR;
return;
}
setPendingActivity(this);
m_blob = &blob;
m_readType = type;
m_state = LOADING;
m_error = nullptr;
m_loader = std::make_unique<FileReaderLoader>(m_readType, this);
m_loader->setEncoding(m_encoding);
m_loader->setDataType(m_blob->type());
m_loader->start(scriptExecutionContext(), blob);
}
void FileReader::abort()
{
LOG(FileAPI, "FileReader: aborting\n");
if (m_aborting || m_state != LOADING)
return;
m_aborting = true;
scriptExecutionContext()->postTask([this] (ScriptExecutionContext&) {
ASSERT(m_state != DONE);
terminate();
m_aborting = false;
m_error = FileError::create(FileError::ABORT_ERR);
fireEvent(eventNames().errorEvent);
fireEvent(eventNames().abortEvent);
fireEvent(eventNames().loadendEvent);
unsetPendingActivity(this);
});
}
void FileReader::terminate()
{
if (m_loader) {
m_loader->cancel();
m_loader = nullptr;
}
m_state = DONE;
}
void FileReader::didStartLoading()
{
fireEvent(eventNames().loadstartEvent);
}
void FileReader::didReceiveData()
{
auto now = std::chrono::steady_clock::now();
if (!m_lastProgressNotificationTime.time_since_epoch().count()) {
m_lastProgressNotificationTime = now;
return;
}
if (now - m_lastProgressNotificationTime > progressNotificationInterval) {
fireEvent(eventNames().progressEvent);
m_lastProgressNotificationTime = now;
}
}
void FileReader::didFinishLoading()
{
if (m_aborting)
return;
ASSERT(m_state != DONE);
m_state = DONE;
fireEvent(eventNames().progressEvent);
fireEvent(eventNames().loadEvent);
fireEvent(eventNames().loadendEvent);
unsetPendingActivity(this);
}
void FileReader::didFail(int errorCode)
{
if (m_aborting)
return;
ASSERT(m_state != DONE);
m_state = DONE;
m_error = FileError::create(static_cast<FileError::ErrorCode>(errorCode));
fireEvent(eventNames().errorEvent);
fireEvent(eventNames().loadendEvent);
unsetPendingActivity(this);
}
void FileReader::fireEvent(const AtomicString& type)
{
dispatchEvent(ProgressEvent::create(type, true, m_loader ? m_loader->bytesLoaded() : 0, m_loader ? m_loader->totalBytes() : 0));
}
RefPtr<ArrayBuffer> FileReader::arrayBufferResult() const
{
if (!m_loader || m_error)
return nullptr;
return m_loader->arrayBufferResult();
}
String FileReader::stringResult()
{
if (!m_loader || m_error)
return String();
return m_loader->stringResult();
}
}