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