ChromiumDataObjectItem.cpp [plain text]
#include "config.h"
#include "ChromiumDataObjectItem.h"
#if ENABLE(DATA_TRANSFER_ITEMS)
#include "Blob.h"
#include "Clipboard.h"
#include "ClipboardChromium.h"
#include "ClipboardMimeTypes.h"
#include "ClipboardUtilitiesChromium.h"
#include "DataTransferItem.h"
#include "File.h"
#include "PlatformSupport.h"
#include "SharedBuffer.h"
#include "StringCallback.h"
namespace WebCore {
PassRefPtr<ChromiumDataObjectItem> ChromiumDataObjectItem::createFromString(const String& type, const String& data)
{
RefPtr<ChromiumDataObjectItem> item = adoptRef(new ChromiumDataObjectItem(DataTransferItem::kindString, type));
item->m_data = data;
return item.release();
}
PassRefPtr<ChromiumDataObjectItem> ChromiumDataObjectItem::createFromFile(PassRefPtr<File> file)
{
RefPtr<ChromiumDataObjectItem> item = adoptRef(new ChromiumDataObjectItem(DataTransferItem::kindFile, file->type()));
item->m_file = file;
return item.release();
}
PassRefPtr<ChromiumDataObjectItem> ChromiumDataObjectItem::createFromURL(const String& url, const String& title)
{
RefPtr<ChromiumDataObjectItem> item = adoptRef(new ChromiumDataObjectItem(DataTransferItem::kindString, mimeTypeTextURIList));
item->m_data = url;
item->m_title = title;
return item.release();
}
PassRefPtr<ChromiumDataObjectItem> ChromiumDataObjectItem::createFromHTML(const String& html, const KURL& baseURL)
{
RefPtr<ChromiumDataObjectItem> item = adoptRef(new ChromiumDataObjectItem(DataTransferItem::kindString, mimeTypeTextHTML));
item->m_data = html;
item->m_baseURL = baseURL;
return item.release();
}
PassRefPtr<ChromiumDataObjectItem> ChromiumDataObjectItem::createFromSharedBuffer(const String& name, PassRefPtr<SharedBuffer> buffer)
{
RefPtr<ChromiumDataObjectItem> item = adoptRef(new ChromiumDataObjectItem(DataTransferItem::kindFile, String()));
item->m_sharedBuffer = buffer;
item->m_title = name;
return item.release();
}
PassRefPtr<ChromiumDataObjectItem> ChromiumDataObjectItem::createFromPasteboard(const String& type, uint64_t sequenceNumber)
{
if (type == mimeTypeImagePng)
return adoptRef(new ChromiumDataObjectItem(DataTransferItem::kindFile, type, sequenceNumber));
return adoptRef(new ChromiumDataObjectItem(DataTransferItem::kindString, type, sequenceNumber));
}
ChromiumDataObjectItem::ChromiumDataObjectItem(const String& kind, const String& type)
: m_source(InternalSource)
, m_kind(kind)
, m_type(type)
, m_sequenceNumber(0)
{
}
ChromiumDataObjectItem::ChromiumDataObjectItem(const String& kind, const String& type, uint64_t sequenceNumber)
: m_source(PasteboardSource)
, m_kind(kind)
, m_type(type)
, m_sequenceNumber(sequenceNumber)
{
}
void ChromiumDataObjectItem::getAsString(PassRefPtr<StringCallback> callback, ScriptExecutionContext* context) const
{
if (!callback || kind() != DataTransferItem::kindString)
return;
callback->scheduleCallback(context, internalGetAsString());
}
PassRefPtr<Blob> ChromiumDataObjectItem::getAsFile() const
{
if (kind() != DataTransferItem::kindFile)
return 0;
if (m_source == InternalSource) {
if (m_file)
return m_file;
ASSERT(m_sharedBuffer);
return 0;
}
ASSERT(m_source == PasteboardSource);
if (type() == mimeTypeImagePng) {
RefPtr<SharedBuffer> data = PlatformSupport::clipboardReadImage(PasteboardPrivate::StandardBuffer);
RefPtr<RawData> rawData = RawData::create();
rawData->mutableData()->append(data->data(), data->size());
OwnPtr<BlobData> blobData = BlobData::create();
blobData->appendData(rawData, 0, -1);
blobData->setContentType(mimeTypeImagePng);
return Blob::create(blobData.release(), data->size());
}
return 0;
}
String ChromiumDataObjectItem::internalGetAsString() const
{
ASSERT(m_kind == DataTransferItem::kindString);
if (m_source == InternalSource)
return m_data;
ASSERT(m_source == PasteboardSource);
String data;
if (m_type == mimeTypeTextPlain)
data = PlatformSupport::clipboardReadPlainText(currentPasteboardBuffer());
else if (m_type == mimeTypeTextHTML) {
KURL ignoredSourceURL;
unsigned ignored;
PlatformSupport::clipboardReadHTML(currentPasteboardBuffer(), &data, &ignoredSourceURL, &ignored, &ignored);
} else
data = PlatformSupport::clipboardReadCustomData(currentPasteboardBuffer(), m_type);
return PlatformSupport::clipboardSequenceNumber(currentPasteboardBuffer()) == m_sequenceNumber ? data : String();
}
bool ChromiumDataObjectItem::isFilename() const
{
return m_kind == DataTransferItem::kindFile && m_file;
}
}
#endif // ENABLE(DATA_TRANSFER_ITEMS)