2 * Copyright (c) 2008, 2009, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "ChromiumDataObject.h"
34 #include "ClipboardMimeTypes.h"
35 #include "ClipboardUtilitiesChromium.h"
36 #include "Pasteboard.h"
37 #include "PlatformSupport.h"
41 // Per RFC 2483, the line separator for "text/..." MIME types is CR-LF.
42 static char const* const textMIMETypeLineSeparator = "\r\n";
44 void ChromiumDataObject::clearData(const String& type)
46 if (type == mimeTypeTextPlain) {
51 if (type == mimeTypeURL || type == mimeTypeTextURIList) {
58 if (type == mimeTypeTextHTML) {
60 m_htmlBaseUrl = KURL();
64 if (type == mimeTypeDownloadURL) {
65 m_downloadMetadata = "";
70 void ChromiumDataObject::clearAll()
72 clearAllExceptFiles();
76 void ChromiumDataObject::clearAllExceptFiles()
81 m_downloadMetadata = "";
85 m_htmlBaseUrl = KURL();
86 m_fileContentFilename = "";
88 m_fileContent->clear();
91 bool ChromiumDataObject::hasData() const
93 return !m_url.isEmpty()
94 || !m_uriList.isEmpty()
95 || !m_downloadMetadata.isEmpty()
96 || !m_fileExtension.isEmpty()
97 || !m_filenames.isEmpty()
98 || !m_plainText.isEmpty()
99 || !m_textHtml.isEmpty()
103 HashSet<String> ChromiumDataObject::types() const
105 if (m_storageMode == Pasteboard) {
106 bool ignoredContainsFilenames;
107 return PlatformSupport::clipboardReadAvailableTypes(currentPasteboardBuffer(),
108 &ignoredContainsFilenames);
111 HashSet<String> results;
113 if (!m_plainText.isEmpty()) {
114 results.add(mimeTypeText);
115 results.add(mimeTypeTextPlain);
119 results.add(mimeTypeURL);
121 if (!m_uriList.isEmpty())
122 results.add(mimeTypeTextURIList);
124 if (!m_textHtml.isEmpty())
125 results.add(mimeTypeTextHTML);
130 String ChromiumDataObject::getData(const String& type, bool& success)
132 if (type == mimeTypeTextPlain) {
133 if (m_storageMode == Pasteboard) {
134 String text = PlatformSupport::clipboardReadPlainText(currentPasteboardBuffer());
135 success = !text.isEmpty();
138 success = !m_plainText.isEmpty();
142 if (type == mimeTypeURL) {
143 success = !m_url.isEmpty();
144 return m_url.string();
147 if (type == mimeTypeTextURIList) {
148 success = !m_uriList.isEmpty();
152 if (type == mimeTypeTextHTML) {
153 if (m_storageMode == Pasteboard) {
157 PlatformSupport::clipboardReadHTML(currentPasteboardBuffer(), &htmlText, &sourceURL, &ignored, &ignored);
158 success = !htmlText.isEmpty();
161 success = !m_textHtml.isEmpty();
165 if (type == mimeTypeDownloadURL) {
166 success = !m_downloadMetadata.isEmpty();
167 return m_downloadMetadata;
174 bool ChromiumDataObject::setData(const String& type, const String& data)
176 if (type == mimeTypeTextPlain) {
181 if (type == mimeTypeURL || type == mimeTypeTextURIList) {
183 Vector<String> uriList;
184 // Line separator is \r\n per RFC 2483 - however, for compatibility
185 // reasons we also allow just \n here.
186 data.split('\n', uriList);
187 // Process the input and copy the first valid URL into the url member.
188 // In case no URLs can be found, subsequent calls to getData("URL")
189 // will get an empty string. This is in line with the HTML5 spec (see
190 // "The DragEvent and DataTransfer interfaces").
191 for (size_t i = 0; i < uriList.size(); ++i) {
192 String& line = uriList[i];
193 line = line.stripWhiteSpace();
194 if (line.isEmpty()) {
199 KURL url = KURL(ParsedURLString, line);
209 if (type == mimeTypeTextHTML) {
211 m_htmlBaseUrl = KURL();
215 if (type == mimeTypeDownloadURL) {
216 m_downloadMetadata = data;
223 bool ChromiumDataObject::containsFilenames() const
225 bool containsFilenames;
226 if (m_storageMode == Pasteboard) {
227 HashSet<String> ignoredResults =
228 PlatformSupport::clipboardReadAvailableTypes(currentPasteboardBuffer(),
231 containsFilenames = !m_filenames.isEmpty();
232 return containsFilenames;
235 ChromiumDataObject::ChromiumDataObject(StorageMode storageMode)
236 : m_storageMode(storageMode)
240 ChromiumDataObject::ChromiumDataObject(const ChromiumDataObject& other)
241 : RefCounted<ChromiumDataObject>()
242 , m_storageMode(other.m_storageMode)
243 , m_urlTitle(other.m_urlTitle)
244 , m_downloadMetadata(other.m_downloadMetadata)
245 , m_fileExtension(other.m_fileExtension)
246 , m_filenames(other.m_filenames)
247 , m_plainText(other.m_plainText)
248 , m_textHtml(other.m_textHtml)
249 , m_htmlBaseUrl(other.m_htmlBaseUrl)
250 , m_fileContentFilename(other.m_fileContentFilename)
252 , m_uriList(other.m_uriList)
254 if (other.m_fileContent.get())
255 m_fileContent = other.m_fileContent->copy();
258 } // namespace WebCore