tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebCore / platform / chromium / ChromiumDataObject.cpp
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 "ChromiumDataObject.h"
33
34 #include "ClipboardMimeTypes.h"
35 #include "ClipboardUtilitiesChromium.h"
36 #include "Pasteboard.h"
37 #include "PlatformSupport.h"
38
39 namespace WebCore {
40
41 // Per RFC 2483, the line separator for "text/..." MIME types is CR-LF.
42 static char const* const textMIMETypeLineSeparator = "\r\n";
43
44 void ChromiumDataObject::clearData(const String& type)
45 {
46     if (type == mimeTypeTextPlain) {
47         m_plainText = "";
48         return;
49     }
50
51     if (type == mimeTypeURL || type == mimeTypeTextURIList) {
52         m_uriList = "";
53         m_url = KURL();
54         m_urlTitle = "";
55         return;
56     }
57
58     if (type == mimeTypeTextHTML) {
59         m_textHtml = "";
60         m_htmlBaseUrl = KURL();
61         return;
62     }
63
64     if (type == mimeTypeDownloadURL) {
65         m_downloadMetadata = "";
66         return;
67     }
68 }
69
70 void ChromiumDataObject::clearAll()
71 {
72     clearAllExceptFiles();
73     m_filenames.clear();
74 }
75
76 void ChromiumDataObject::clearAllExceptFiles()
77 {
78     m_urlTitle = "";
79     m_url = KURL();
80     m_uriList = "";
81     m_downloadMetadata = "";
82     m_fileExtension = "";
83     m_plainText = "";
84     m_textHtml = "";
85     m_htmlBaseUrl = KURL();
86     m_fileContentFilename = "";
87     if (m_fileContent)
88         m_fileContent->clear();
89 }
90
91 bool ChromiumDataObject::hasData() const
92 {
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()
100         || m_fileContent;
101 }
102
103 HashSet<String> ChromiumDataObject::types() const
104 {
105     if (m_storageMode == Pasteboard) {
106         bool ignoredContainsFilenames;
107         return PlatformSupport::clipboardReadAvailableTypes(currentPasteboardBuffer(),
108                                                             &ignoredContainsFilenames);
109     }
110
111     HashSet<String> results;
112
113     if (!m_plainText.isEmpty()) {
114         results.add(mimeTypeText);
115         results.add(mimeTypeTextPlain);
116     }
117
118     if (m_url.isValid())
119         results.add(mimeTypeURL);
120
121     if (!m_uriList.isEmpty())
122         results.add(mimeTypeTextURIList);
123
124     if (!m_textHtml.isEmpty())
125         results.add(mimeTypeTextHTML);
126
127     return results;
128 }
129
130 String ChromiumDataObject::getData(const String& type, bool& success)
131 {
132     if (type == mimeTypeTextPlain) {
133         if (m_storageMode == Pasteboard) {
134             String text = PlatformSupport::clipboardReadPlainText(currentPasteboardBuffer());
135             success = !text.isEmpty();
136             return text;
137         }
138         success = !m_plainText.isEmpty();
139         return m_plainText;
140     }
141
142     if (type == mimeTypeURL) {
143         success = !m_url.isEmpty();
144         return m_url.string();
145     }
146
147     if (type == mimeTypeTextURIList) {
148         success = !m_uriList.isEmpty();
149         return m_uriList;
150     }
151
152     if (type == mimeTypeTextHTML) {
153         if (m_storageMode == Pasteboard) {
154             String htmlText;
155             KURL sourceURL;
156             unsigned ignored;
157             PlatformSupport::clipboardReadHTML(currentPasteboardBuffer(), &htmlText, &sourceURL, &ignored, &ignored);
158             success = !htmlText.isEmpty();
159             return htmlText;
160         }
161         success = !m_textHtml.isEmpty();
162         return m_textHtml;
163     }
164
165     if (type == mimeTypeDownloadURL) {
166         success = !m_downloadMetadata.isEmpty();
167         return m_downloadMetadata;
168     }
169
170     success = false;
171     return String();
172 }
173
174 bool ChromiumDataObject::setData(const String& type, const String& data)
175 {
176     if (type == mimeTypeTextPlain) {
177         m_plainText = data;
178         return true;
179     }
180
181     if (type == mimeTypeURL || type == mimeTypeTextURIList) {
182         m_url = KURL();
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()) {
195                 continue;
196             }
197             if (line[0] == '#')
198                 continue;
199             KURL url = KURL(ParsedURLString, line);
200             if (url.isValid()) {
201                 m_url = url;
202                 break;
203             }
204         }
205         m_uriList = data;
206         return true;
207     }
208
209     if (type == mimeTypeTextHTML) {
210         m_textHtml = data;
211         m_htmlBaseUrl = KURL();
212         return true;
213     }
214
215     if (type == mimeTypeDownloadURL) {
216         m_downloadMetadata = data;
217         return true;
218     }
219
220     return false;
221 }
222
223 bool ChromiumDataObject::containsFilenames() const
224 {
225     bool containsFilenames;
226     if (m_storageMode == Pasteboard) {
227         HashSet<String> ignoredResults =
228             PlatformSupport::clipboardReadAvailableTypes(currentPasteboardBuffer(),
229                                                          &containsFilenames);
230     } else
231         containsFilenames = !m_filenames.isEmpty();
232     return containsFilenames;
233 }
234
235 ChromiumDataObject::ChromiumDataObject(StorageMode storageMode)
236     : m_storageMode(storageMode)
237 {
238 }
239
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)
251     , m_url(other.m_url)
252     , m_uriList(other.m_uriList)
253 {
254     if (other.m_fileContent.get())
255         m_fileContent = other.m_fileContent->copy();
256 }
257
258 } // namespace WebCore
259