WebKitBlobBuilder.cpp [plain text]
#include "config.h"
#include "WebKitBlobBuilder.h"
#include "Blob.h"
#include "ExceptionCode.h"
#include "File.h"
#include "LineEnding.h"
#include "TextEncoding.h"
#include <wtf/ArrayBuffer.h>
#include <wtf/PassRefPtr.h>
#include <wtf/Vector.h>
#include <wtf/text/AtomicString.h>
#include <wtf/text/CString.h>
namespace WebCore {
WebKitBlobBuilder::WebKitBlobBuilder()
: m_size(0)
{
}
Vector<char>& WebKitBlobBuilder::getBuffer()
{
if (m_items.isEmpty() || m_items[m_items.size() - 1].type != BlobDataItem::Data)
m_items.append(BlobDataItem(RawData::create()));
return *m_items[m_items.size() - 1].data->mutableData();
}
void WebKitBlobBuilder::append(const String& text, const String& endingType, ExceptionCode& ec)
{
bool isEndingTypeTransparent = endingType == "transparent";
bool isEndingTypeNative = endingType == "native";
if (!endingType.isEmpty() && !isEndingTypeTransparent && !isEndingTypeNative) {
ec = SYNTAX_ERR;
return;
}
CString utf8Text = UTF8Encoding().encode(text.characters(), text.length(), EntitiesForUnencodables);
Vector<char>& buffer = getBuffer();
size_t oldSize = buffer.size();
if (isEndingTypeNative)
normalizeLineEndingsToNative(utf8Text, buffer);
else
buffer.append(utf8Text.data(), utf8Text.length());
m_size += buffer.size() - oldSize;
}
void WebKitBlobBuilder::append(const String& text, ExceptionCode& ec)
{
append(text, String(), ec);
}
#if ENABLE(BLOB)
void WebKitBlobBuilder::append(ArrayBuffer* arrayBuffer)
{
if (!arrayBuffer)
return;
Vector<char>& buffer = getBuffer();
size_t oldSize = buffer.size();
buffer.append(static_cast<const char*>(arrayBuffer->data()), arrayBuffer->byteLength());
m_size += buffer.size() - oldSize;
}
#endif
void WebKitBlobBuilder::append(Blob* blob)
{
if (!blob)
return;
if (blob->isFile()) {
File* file = static_cast<File*>(blob);
long long snapshotSize;
double snapshotModificationTime;
file->captureSnapshot(snapshotSize, snapshotModificationTime);
m_size += snapshotSize;
m_items.append(BlobDataItem(file->path(), 0, snapshotSize, snapshotModificationTime));
} else {
long long blobSize = static_cast<long long>(blob->size());
m_size += blobSize;
m_items.append(BlobDataItem(blob->url(), 0, blobSize));
}
}
PassRefPtr<Blob> WebKitBlobBuilder::getBlob(const String& contentType)
{
OwnPtr<BlobData> blobData = BlobData::create();
blobData->setContentType(contentType);
blobData->swapItems(m_items);
RefPtr<Blob> blob = Blob::create(blobData.release(), m_size);
m_items.append(BlobDataItem(blob->url(), 0, m_size));
return blob;
}
}