1/*
2 * Copyright (c) 2008, 2009, Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
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
13 * distribution.
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.
17 *
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.
29 */
30
31#include "config.h"
32#include "ChromiumDataObjectLegacy.h"
33
34#include "ClipboardMimeTypes.h"
35#include "Pasteboard.h"
36#include "PlatformBridge.h"
37
38namespace WebCore {
39
40// Per RFC 2483, the line separator for "text/..." MIME types is CR-LF.
41static char const* const textMIMETypeLineSeparator = "\r\n";
42
43void ChromiumDataObjectLegacy::clearData(const String& type)
44{
45    if (type == mimeTypeTextPlain) {
46        m_plainText = "";
47        return;
48    }
49
50    if (type == mimeTypeURL || type == mimeTypeTextURIList) {
51        m_uriList = "";
52        m_url = KURL();
53        m_urlTitle = "";
54        return;
55    }
56
57    if (type == mimeTypeTextHTML) {
58        m_textHtml = "";
59        m_htmlBaseUrl = KURL();
60        return;
61    }
62
63    if (type == mimeTypeDownloadURL) {
64        m_downloadMetadata = "";
65        return;
66    }
67}
68
69void ChromiumDataObjectLegacy::clearAll()
70{
71    clearAllExceptFiles();
72    m_filenames.clear();
73}
74
75void ChromiumDataObjectLegacy::clearAllExceptFiles()
76{
77    m_urlTitle = "";
78    m_url = KURL();
79    m_uriList = "";
80    m_downloadMetadata = "";
81    m_fileExtension = "";
82    m_plainText = "";
83    m_textHtml = "";
84    m_htmlBaseUrl = KURL();
85    m_fileContentFilename = "";
86    if (m_fileContent)
87        m_fileContent->clear();
88}
89
90bool ChromiumDataObjectLegacy::hasData() const
91{
92    return !m_url.isEmpty()
93        || !m_uriList.isEmpty()
94        || !m_downloadMetadata.isEmpty()
95        || !m_fileExtension.isEmpty()
96        || !m_filenames.isEmpty()
97        || !m_plainText.isEmpty()
98        || !m_textHtml.isEmpty()
99        || m_fileContent;
100}
101
102HashSet<String> ChromiumDataObjectLegacy::types() const
103{
104    // This is currently broken for pasteboard events, and always has been.
105    HashSet<String> results;
106
107    if (!m_plainText.isEmpty()) {
108        results.add(mimeTypeText);
109        results.add(mimeTypeTextPlain);
110    }
111
112    if (m_url.isValid())
113        results.add(mimeTypeURL);
114
115    if (!m_uriList.isEmpty())
116        results.add(mimeTypeTextURIList);
117
118    if (!m_textHtml.isEmpty())
119        results.add(mimeTypeTextHTML);
120
121    if (!m_filenames.isEmpty())
122        results.add("Files");
123
124    return results;
125}
126
127String ChromiumDataObjectLegacy::getData(const String& type, bool& success)
128{
129    if (type == mimeTypeTextPlain) {
130        if (m_clipboardType == Clipboard::CopyAndPaste) {
131            PasteboardPrivate::ClipboardBuffer buffer =
132                Pasteboard::generalPasteboard()->isSelectionMode() ?
133                PasteboardPrivate::SelectionBuffer :
134                PasteboardPrivate::StandardBuffer;
135            String text = PlatformBridge::clipboardReadPlainText(buffer);
136            success = !text.isEmpty();
137            return text;
138        }
139        success = !m_plainText.isEmpty();
140        return m_plainText;
141    }
142
143    if (type == mimeTypeURL) {
144        success = !m_url.isEmpty();
145        return m_url.string();
146    }
147
148    if (type == mimeTypeTextURIList) {
149        success = !m_uriList.isEmpty();
150        return m_uriList;
151    }
152
153    if (type == mimeTypeTextHTML) {
154        if (m_clipboardType == Clipboard::CopyAndPaste) {
155            PasteboardPrivate::ClipboardBuffer buffer =
156                Pasteboard::generalPasteboard()->isSelectionMode() ?
157                PasteboardPrivate::SelectionBuffer :
158                PasteboardPrivate::StandardBuffer;
159            String htmlText;
160            KURL sourceURL;
161            PlatformBridge::clipboardReadHTML(buffer, &htmlText, &sourceURL);
162            success = !htmlText.isEmpty();
163            return htmlText;
164        }
165        success = !m_textHtml.isEmpty();
166        return m_textHtml;
167    }
168
169    if (type == mimeTypeDownloadURL) {
170        success = !m_downloadMetadata.isEmpty();
171        return m_downloadMetadata;
172    }
173
174    success = false;
175    return String();
176}
177
178bool ChromiumDataObjectLegacy::setData(const String& type, const String& data)
179{
180    if (type == mimeTypeTextPlain) {
181        m_plainText = data;
182        return true;
183    }
184
185    if (type == mimeTypeURL || type == mimeTypeTextURIList) {
186        m_url = KURL();
187        Vector<String> uriList;
188        // Line separator is \r\n per RFC 2483 - however, for compatibility
189        // reasons we also allow just \n here.
190        data.split('\n', uriList);
191        // Process the input and copy the first valid URL into the url member.
192        // In case no URLs can be found, subsequent calls to getData("URL")
193        // will get an empty string. This is in line with the HTML5 spec (see
194        // "The DragEvent and DataTransfer interfaces").
195        for (size_t i = 0; i < uriList.size(); ++i) {
196            String& line = uriList[i];
197            line = line.stripWhiteSpace();
198            if (line.isEmpty())
199                continue;
200            if (line[0] == '#')
201                continue;
202            KURL url = KURL(ParsedURLString, line);
203            if (url.isValid()) {
204                m_url = url;
205                break;
206            }
207        }
208        m_uriList = data;
209        return true;
210    }
211
212    if (type == mimeTypeTextHTML) {
213        m_textHtml = data;
214        m_htmlBaseUrl = KURL();
215        return true;
216    }
217
218    if (type == mimeTypeDownloadURL) {
219        m_downloadMetadata = data;
220        return true;
221    }
222
223    return false;
224}
225
226ChromiumDataObjectLegacy::ChromiumDataObjectLegacy(Clipboard::ClipboardType clipboardType)
227    : m_clipboardType(clipboardType)
228{
229}
230
231ChromiumDataObjectLegacy::ChromiumDataObjectLegacy(const ChromiumDataObjectLegacy& other)
232    : RefCounted<ChromiumDataObjectLegacy>()
233    , m_clipboardType(other.m_clipboardType)
234    , m_urlTitle(other.m_urlTitle)
235    , m_downloadMetadata(other.m_downloadMetadata)
236    , m_fileExtension(other.m_fileExtension)
237    , m_filenames(other.m_filenames)
238    , m_plainText(other.m_plainText)
239    , m_textHtml(other.m_textHtml)
240    , m_htmlBaseUrl(other.m_htmlBaseUrl)
241    , m_fileContentFilename(other.m_fileContentFilename)
242    , m_url(other.m_url)
243    , m_uriList(other.m_uriList)
244{
245    if (other.m_fileContent.get())
246        m_fileContent = other.m_fileContent->copy();
247}
248
249} // namespace WebCore
250