tizen beta release
[framework/web/webkit-efl.git] / Source / WebKit / chromium / src / WebDragData.cpp
1 /*
2  * Copyright (C) 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 "WebDragData.h"
33
34 #include "ChromiumDataObject.h"
35 #include "ClipboardMimeTypes.h"
36 #include "WebData.h"
37 #include "WebString.h"
38 #include "WebURL.h"
39 #include "WebVector.h"
40
41 #include <wtf/PassRefPtr.h>
42
43 using namespace WebCore;
44
45 namespace WebKit {
46
47 class WebDragDataPrivate : public ChromiumDataObject {
48 };
49
50 void WebDragData::initialize()
51 {
52     assign(static_cast<WebDragDataPrivate*>(ChromiumDataObject::create().leakRef()));
53 }
54
55 void WebDragData::reset()
56 {
57     assign(0);
58 }
59
60 void WebDragData::assign(const WebDragData& other)
61 {
62     WebDragDataPrivate* p = const_cast<WebDragDataPrivate*>(other.m_private);
63     if (p)
64         p->ref();
65     assign(p);
66 }
67
68 WebString WebDragData::url() const
69 {
70     ASSERT(!isNull());
71     bool ignoredSuccess;
72     return m_private->getData(mimeTypeURL, ignoredSuccess);
73 }
74
75 void WebDragData::setURL(const WebURL& url)
76 {
77     ensureMutable();
78     m_private->setData(mimeTypeURL, KURL(url).string());
79 }
80
81 WebString WebDragData::urlTitle() const
82 {
83     ASSERT(!isNull());
84     return m_private->urlTitle();
85 }
86
87 void WebDragData::setURLTitle(const WebString& urlTitle)
88 {
89     ensureMutable();
90     m_private->setUrlTitle(urlTitle);
91 }
92
93 WebString WebDragData::downloadMetadata() const
94 {
95     ASSERT(!isNull());
96     bool ignoredSuccess;
97     return m_private->getData(mimeTypeDownloadURL, ignoredSuccess);
98 }
99
100 void WebDragData::setDownloadMetadata(const WebString& downloadMetadata)
101 {
102     ensureMutable();
103     m_private->setData(mimeTypeDownloadURL, downloadMetadata);
104 }
105
106 WebString WebDragData::fileExtension() const
107 {
108     ASSERT(!isNull());
109     return m_private->fileExtension();
110 }
111
112 void WebDragData::setFileExtension(const WebString& fileExtension)
113 {
114     ensureMutable();
115     m_private->setFileExtension(fileExtension);
116 }
117
118 bool WebDragData::containsFilenames() const
119 {
120     ASSERT(!isNull());
121     return m_private->containsFilenames();
122 }
123
124 void WebDragData::filenames(WebVector<WebString>& filenames) const
125 {
126     ASSERT(!isNull());
127     filenames = m_private->filenames();
128 }
129
130 void WebDragData::setFilenames(const WebVector<WebString>& filenames)
131 {
132     ensureMutable();
133     Vector<String> filenamesCopy;
134     filenamesCopy.append(filenames.data(), filenames.size());
135     m_private->setFilenames(filenamesCopy);
136 }
137
138 void WebDragData::appendToFilenames(const WebString& filename)
139 {
140     ensureMutable();
141     Vector<String> filenames = m_private->filenames();
142     filenames.append(filename);
143     m_private->setFilenames(filenames);
144 }
145
146 WebString WebDragData::plainText() const
147 {
148     ASSERT(!isNull());
149     bool ignoredSuccess;
150     return m_private->getData(mimeTypeTextPlain, ignoredSuccess);
151 }
152
153 void WebDragData::setPlainText(const WebString& plainText)
154 {
155     ensureMutable();
156     m_private->setData(mimeTypeTextPlain, plainText);
157 }
158
159 WebString WebDragData::htmlText() const
160 {
161     ASSERT(!isNull());
162     bool ignoredSuccess;
163     return m_private->getData(mimeTypeTextHTML, ignoredSuccess);
164 }
165
166 void WebDragData::setHTMLText(const WebString& htmlText)
167 {
168     ensureMutable();
169     m_private->setData(mimeTypeTextHTML, htmlText);
170 }
171
172 WebURL WebDragData::htmlBaseURL() const
173 {
174     ASSERT(!isNull());
175     return m_private->htmlBaseUrl();
176 }
177
178 void WebDragData::setHTMLBaseURL(const WebURL& htmlBaseURL)
179 {
180     ensureMutable();
181     m_private->setHtmlBaseUrl(htmlBaseURL);
182 }
183
184 WebString WebDragData::fileContentFilename() const
185 {
186     ASSERT(!isNull());
187     return m_private->fileContentFilename();
188 }
189
190 void WebDragData::setFileContentFilename(const WebString& filename)
191 {
192     ensureMutable();
193     m_private->setFileContentFilename(filename);
194 }
195
196 WebData WebDragData::fileContent() const
197 {
198     ASSERT(!isNull());
199     return WebData(m_private->fileContent());
200 }
201
202 void WebDragData::setFileContent(const WebData& fileContent)
203 {
204     ensureMutable();
205     m_private->setFileContent(fileContent);
206 }
207
208 WebDragData::WebDragData(const WTF::PassRefPtr<WebCore::ChromiumDataObject>& data)
209     : m_private(static_cast<WebDragDataPrivate*>(data.leakRef()))
210 {
211 }
212
213 WebDragData& WebDragData::operator=(const WTF::PassRefPtr<WebCore::ChromiumDataObject>& data)
214 {
215     assign(static_cast<WebDragDataPrivate*>(data.leakRef()));
216     return *this;
217 }
218
219 WebDragData::operator WTF::PassRefPtr<WebCore::ChromiumDataObject>() const
220 {
221     return PassRefPtr<ChromiumDataObject>(const_cast<WebDragDataPrivate*>(m_private));
222 }
223
224 void WebDragData::assign(WebDragDataPrivate* p)
225 {
226     ASSERT(!p || p->storageMode() == ChromiumDataObject::Buffered);
227     // p is already ref'd for us by the caller
228     if (m_private)
229         m_private->deref();
230     m_private = p;
231 }
232
233 void WebDragData::ensureMutable()
234 {
235     ASSERT(!isNull());
236     ASSERT(m_private->hasOneRef());
237 }
238
239 } // namespace WebKit