diff options
Diffstat (limited to 'Source/WebCore/platform/chromium/ChromiumDataObject.cpp')
-rw-r--r-- | Source/WebCore/platform/chromium/ChromiumDataObject.cpp | 318 |
1 files changed, 182 insertions, 136 deletions
diff --git a/Source/WebCore/platform/chromium/ChromiumDataObject.cpp b/Source/WebCore/platform/chromium/ChromiumDataObject.cpp index f5732df..c3632e6 100644 --- a/Source/WebCore/platform/chromium/ChromiumDataObject.cpp +++ b/Source/WebCore/platform/chromium/ChromiumDataObject.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010, Google Inc. All rights reserved. + * Copyright (c) 2008, 2009, Google Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are @@ -31,189 +31,235 @@ #include "config.h" #include "ChromiumDataObject.h" +#include "ClipboardMimeTypes.h" +#include "Pasteboard.h" +#include "PlatformBridge.h" + namespace WebCore { -ChromiumDataObject::ChromiumDataObject(PassRefPtr<ChromiumDataObjectLegacy> data) - : RefCounted<ChromiumDataObject>() - , m_legacyData(data) -{ -} +// Per RFC 2483, the line separator for "text/..." MIME types is CR-LF. +static char const* const textMIMETypeLineSeparator = "\r\n"; -ChromiumDataObject::ChromiumDataObject(PassRefPtr<ReadableDataObject> data) - : RefCounted<ChromiumDataObject>() - , m_readableData(data) +void ChromiumDataObject::clearData(const String& type) { -} + if (type == mimeTypeTextPlain) { + m_plainText = ""; + return; + } -ChromiumDataObject::ChromiumDataObject(PassRefPtr<WritableDataObject> data) - : RefCounted<ChromiumDataObject>() - , m_writableData(data) -{ -} + if (type == mimeTypeURL || type == mimeTypeTextURIList) { + m_uriList = ""; + m_url = KURL(); + m_urlTitle = ""; + return; + } -PassRefPtr<ChromiumDataObject> ChromiumDataObject::create(PassRefPtr<ChromiumDataObjectLegacy> data) -{ - return adoptRef(new ChromiumDataObject(data)); -} + if (type == mimeTypeTextHTML) { + m_textHtml = ""; + m_htmlBaseUrl = KURL(); + return; + } -PassRefPtr<ChromiumDataObject> ChromiumDataObject::createReadable(const Frame* frame, Clipboard::ClipboardType clipboardType) -{ - return adoptRef(new ChromiumDataObject(ReadableDataObject::create(frame, clipboardType))); -} - -PassRefPtr<ChromiumDataObject> ChromiumDataObject::createWritable(Clipboard::ClipboardType clipboardType) -{ - return adoptRef(new ChromiumDataObject(WritableDataObject::create(clipboardType))); -} - -void ChromiumDataObject::clearData(const String& type) -{ - if (m_legacyData) - m_legacyData->clearData(type); - else - m_writableData->clearData(type); + if (type == mimeTypeDownloadURL) { + m_downloadMetadata = ""; + return; + } } void ChromiumDataObject::clearAll() { - if (m_legacyData) - m_legacyData->clearAll(); - else - m_writableData->clearAll(); + clearAllExceptFiles(); + m_filenames.clear(); } void ChromiumDataObject::clearAllExceptFiles() { - if (m_legacyData) - m_legacyData->clearAllExceptFiles(); - else - m_writableData->clearAllExceptFiles(); + m_urlTitle = ""; + m_url = KURL(); + m_uriList = ""; + m_downloadMetadata = ""; + m_fileExtension = ""; + m_plainText = ""; + m_textHtml = ""; + m_htmlBaseUrl = KURL(); + m_fileContentFilename = ""; + if (m_fileContent) + m_fileContent->clear(); } bool ChromiumDataObject::hasData() const { - if (m_legacyData) - return m_legacyData->hasData(); - return m_readableData->hasData(); + return !m_url.isEmpty() + || !m_uriList.isEmpty() + || !m_downloadMetadata.isEmpty() + || !m_fileExtension.isEmpty() + || !m_filenames.isEmpty() + || !m_plainText.isEmpty() + || !m_textHtml.isEmpty() + || m_fileContent; } HashSet<String> ChromiumDataObject::types() const { - if (m_legacyData) - return m_legacyData->types(); - return m_readableData->types(); -} + if (m_clipboardType == Clipboard::CopyAndPaste) { + bool ignoredContainsFilenames; + return PlatformBridge::clipboardReadAvailableTypes(PasteboardPrivate::StandardBuffer, + &ignoredContainsFilenames); + } -String ChromiumDataObject::getData(const String& type, bool& success) -{ - if (m_legacyData) - return m_legacyData->getData(type, success); - return m_readableData->getData(type, success); -} + HashSet<String> results; -bool ChromiumDataObject::setData(const String& type, const String& data) -{ - if (m_legacyData) - return m_legacyData->setData(type, data); - return m_writableData->setData(type, data); -} + if (!m_plainText.isEmpty()) { + results.add(mimeTypeText); + results.add(mimeTypeTextPlain); + } -String ChromiumDataObject::urlTitle() const -{ - if (m_legacyData) - return m_legacyData->urlTitle(); - return m_readableData->urlTitle(); -} + if (m_url.isValid()) + results.add(mimeTypeURL); -void ChromiumDataObject::setUrlTitle(const String& urlTitle) -{ - if (m_legacyData) - m_legacyData->setUrlTitle(urlTitle); - else - m_writableData->setUrlTitle(urlTitle); -} + if (!m_uriList.isEmpty()) + results.add(mimeTypeTextURIList); -KURL ChromiumDataObject::htmlBaseUrl() const -{ - if (m_legacyData) - return m_legacyData->htmlBaseUrl(); - return m_readableData->htmlBaseUrl(); -} + if (!m_textHtml.isEmpty()) + results.add(mimeTypeTextHTML); -void ChromiumDataObject::setHtmlBaseUrl(const KURL& url) -{ - if (m_legacyData) - m_legacyData->setHtmlBaseUrl(url); - else - m_writableData->setHtmlBaseUrl(url); + return results; } -bool ChromiumDataObject::containsFilenames() const +String ChromiumDataObject::getData(const String& type, bool& success) { - if (m_legacyData) - return m_legacyData->containsFilenames(); - return m_readableData->containsFilenames(); -} + if (type == mimeTypeTextPlain) { + if (m_clipboardType == Clipboard::CopyAndPaste) { + PasteboardPrivate::ClipboardBuffer buffer = + Pasteboard::generalPasteboard()->isSelectionMode() ? + PasteboardPrivate::SelectionBuffer : + PasteboardPrivate::StandardBuffer; + String text = PlatformBridge::clipboardReadPlainText(buffer); + success = !text.isEmpty(); + return text; + } + success = !m_plainText.isEmpty(); + return m_plainText; + } -Vector<String> ChromiumDataObject::filenames() const -{ - if (m_legacyData) - return m_legacyData->filenames(); - return m_readableData->filenames(); -} + if (type == mimeTypeURL) { + success = !m_url.isEmpty(); + return m_url.string(); + } -void ChromiumDataObject::setFilenames(const Vector<String>& filenames) -{ - if (m_legacyData) - m_legacyData->setFilenames(filenames); - else - ASSERT_NOT_REACHED(); -} + if (type == mimeTypeTextURIList) { + success = !m_uriList.isEmpty(); + return m_uriList; + } -String ChromiumDataObject::fileExtension() const -{ - if (m_legacyData) - return m_legacyData->fileExtension(); - return m_writableData->fileExtension(); -} + if (type == mimeTypeTextHTML) { + if (m_clipboardType == Clipboard::CopyAndPaste) { + PasteboardPrivate::ClipboardBuffer buffer = + Pasteboard::generalPasteboard()->isSelectionMode() ? + PasteboardPrivate::SelectionBuffer : + PasteboardPrivate::StandardBuffer; + String htmlText; + KURL sourceURL; + PlatformBridge::clipboardReadHTML(buffer, &htmlText, &sourceURL); + success = !htmlText.isEmpty(); + return htmlText; + } + success = !m_textHtml.isEmpty(); + return m_textHtml; + } -void ChromiumDataObject::setFileExtension(const String& fileExtension) -{ - if (m_legacyData) - m_legacyData->setFileExtension(fileExtension); - else - m_writableData->setFileExtension(fileExtension); + if (type == mimeTypeDownloadURL) { + success = !m_downloadMetadata.isEmpty(); + return m_downloadMetadata; + } + + success = false; + return String(); } -String ChromiumDataObject::fileContentFilename() const +bool ChromiumDataObject::setData(const String& type, const String& data) { - if (m_legacyData) - return m_legacyData->fileContentFilename(); - return m_writableData->fileContentFilename(); + if (type == mimeTypeTextPlain) { + m_plainText = data; + return true; + } + + if (type == mimeTypeURL || type == mimeTypeTextURIList) { + m_url = KURL(); + Vector<String> uriList; + // Line separator is \r\n per RFC 2483 - however, for compatibility + // reasons we also allow just \n here. + data.split('\n', uriList); + // Process the input and copy the first valid URL into the url member. + // In case no URLs can be found, subsequent calls to getData("URL") + // will get an empty string. This is in line with the HTML5 spec (see + // "The DragEvent and DataTransfer interfaces"). + for (size_t i = 0; i < uriList.size(); ++i) { + String& line = uriList[i]; + line = line.stripWhiteSpace(); + if (line.isEmpty()) { + continue; + } + if (line[0] == '#') + continue; + KURL url = KURL(ParsedURLString, line); + if (url.isValid()) { + m_url = url; + break; + } + } + m_uriList = data; + return true; + } + + if (type == mimeTypeTextHTML) { + m_textHtml = data; + m_htmlBaseUrl = KURL(); + return true; + } + + if (type == mimeTypeDownloadURL) { + m_downloadMetadata = data; + return true; + } + + return false; } -void ChromiumDataObject::setFileContentFilename(const String& fileContentFilename) +bool ChromiumDataObject::containsFilenames() const { - if (m_legacyData) - m_legacyData->setFileContentFilename(fileContentFilename); - else - m_writableData->setFileContentFilename(fileContentFilename); + bool containsFilenames; + if (m_clipboardType == Clipboard::CopyAndPaste) { + HashSet<String> ignoredResults = + PlatformBridge::clipboardReadAvailableTypes(PasteboardPrivate::StandardBuffer, + &containsFilenames); + } else + containsFilenames = !m_filenames.isEmpty(); + return containsFilenames; } -PassRefPtr<SharedBuffer> ChromiumDataObject::fileContent() const +ChromiumDataObject::ChromiumDataObject(Clipboard::ClipboardType clipboardType) + : m_clipboardType(clipboardType) { - if (m_legacyData) - return m_legacyData->fileContent(); - return m_writableData->fileContent(); } -void ChromiumDataObject::setFileContent(PassRefPtr<SharedBuffer> fileContent) +ChromiumDataObject::ChromiumDataObject(const ChromiumDataObject& other) + : RefCounted<ChromiumDataObject>() + , m_clipboardType(other.m_clipboardType) + , m_urlTitle(other.m_urlTitle) + , m_downloadMetadata(other.m_downloadMetadata) + , m_fileExtension(other.m_fileExtension) + , m_filenames(other.m_filenames) + , m_plainText(other.m_plainText) + , m_textHtml(other.m_textHtml) + , m_htmlBaseUrl(other.m_htmlBaseUrl) + , m_fileContentFilename(other.m_fileContentFilename) + , m_url(other.m_url) + , m_uriList(other.m_uriList) { - if (m_legacyData) - m_legacyData->setFileContent(fileContent); - else - m_writableData->setFileContent(fileContent); + if (other.m_fileContent.get()) + m_fileContent = other.m_fileContent->copy(); } -} +} // namespace WebCore |