This source file includes following definitions.
- readAsArrayBuffer
- readAsBinaryString
- readAsText
- readAsDataURL
- startLoading
#include "config.h"
#include "core/fileapi/FileReaderSync.h"
#include "bindings/v8/ExceptionState.h"
#include "core/dom/ExceptionCode.h"
#include "core/fileapi/Blob.h"
#include "core/fileapi/FileError.h"
#include "core/fileapi/FileReaderLoader.h"
#include "wtf/ArrayBuffer.h"
#include "wtf/PassRefPtr.h"
namespace WebCore {
FileReaderSync::FileReaderSync()
{
ScriptWrappable::init(this);
}
PassRefPtr<ArrayBuffer> FileReaderSync::readAsArrayBuffer(ExecutionContext* executionContext, Blob* blob, ExceptionState& exceptionState)
{
if (!blob) {
exceptionState.throwDOMException(NotFoundError, FileError::notFoundErrorMessage);
return nullptr;
}
FileReaderLoader loader(FileReaderLoader::ReadAsArrayBuffer, 0);
startLoading(executionContext, loader, *blob, exceptionState);
return loader.arrayBufferResult();
}
String FileReaderSync::readAsBinaryString(ExecutionContext* executionContext, Blob* blob, ExceptionState& exceptionState)
{
if (!blob) {
exceptionState.throwDOMException(NotFoundError, FileError::notFoundErrorMessage);
return String();
}
FileReaderLoader loader(FileReaderLoader::ReadAsBinaryString, 0);
startLoading(executionContext, loader, *blob, exceptionState);
return loader.stringResult();
}
String FileReaderSync::readAsText(ExecutionContext* executionContext, Blob* blob, const String& encoding, ExceptionState& exceptionState)
{
if (!blob) {
exceptionState.throwDOMException(NotFoundError, FileError::notFoundErrorMessage);
return String();
}
FileReaderLoader loader(FileReaderLoader::ReadAsText, 0);
loader.setEncoding(encoding);
startLoading(executionContext, loader, *blob, exceptionState);
return loader.stringResult();
}
String FileReaderSync::readAsDataURL(ExecutionContext* executionContext, Blob* blob, ExceptionState& exceptionState)
{
if (!blob) {
exceptionState.throwDOMException(NotFoundError, FileError::notFoundErrorMessage);
return String();
}
FileReaderLoader loader(FileReaderLoader::ReadAsDataURL, 0);
loader.setDataType(blob->type());
startLoading(executionContext, loader, *blob, exceptionState);
return loader.stringResult();
}
void FileReaderSync::startLoading(ExecutionContext* executionContext, FileReaderLoader& loader, const Blob& blob, ExceptionState& exceptionState)
{
loader.start(executionContext, blob.blobDataHandle());
if (loader.errorCode())
FileError::throwDOMException(exceptionState, loader.errorCode());
}
}