1/*
2 * Copyright (C) 2007 Apple Inc.  All rights reserved.
3 * Copyright (C) 2008, 2009 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27// Modified from DragDataWin.cpp to not directly call any windows methods as
28// they may not be available to us in the multiprocess
29
30#include "config.h"
31#include "DragData.h"
32
33#include "ChromiumBridge.h"
34#include "ChromiumDataObject.h"
35#include "Clipboard.h"
36#include "ClipboardChromium.h"
37#include "DocumentFragment.h"
38#include "FileSystem.h"
39#include "KURL.h"
40#include "markup.h"
41#include "NotImplemented.h"
42#include "PlatformString.h"
43
44namespace WebCore {
45
46static bool containsHTML(const ChromiumDataObject* dropData)
47{
48    return dropData->textHtml.length() > 0;
49}
50
51PassRefPtr<Clipboard> DragData::createClipboard(ClipboardAccessPolicy policy) const
52{
53    RefPtr<ClipboardChromium> clipboard = ClipboardChromium::create(true,
54        m_platformDragData, policy);
55
56    return clipboard.release();
57}
58
59bool DragData::containsURL() const
60{
61    return !asURL().isEmpty();
62}
63
64String DragData::asURL(String* title) const
65{
66    String url;
67    if (m_platformDragData->url.isValid())
68        url = m_platformDragData->url.string();
69    else if (m_platformDragData->filenames.size() == 1) {
70        String fileName = m_platformDragData->filenames[0];
71        fileName = ChromiumBridge::getAbsolutePath(fileName);
72        if (fileExists(fileName) && !ChromiumBridge::isDirectory(fileName))
73            url = ChromiumBridge::filePathToURL(fileName).string();
74    }
75
76    // |title| can be NULL
77    if (title)
78        *title = m_platformDragData->urlTitle;
79    return url;
80}
81
82bool DragData::containsFiles() const
83{
84    return !m_platformDragData->filenames.isEmpty();
85}
86
87void DragData::asFilenames(Vector<String>& result) const
88{
89    for (size_t i = 0; i < m_platformDragData->filenames.size(); ++i)
90        result.append(m_platformDragData->filenames[i]);
91}
92
93bool DragData::containsPlainText() const
94{
95    return !m_platformDragData->plainText.isEmpty();
96}
97
98String DragData::asPlainText() const
99{
100    return m_platformDragData->plainText;
101}
102
103bool DragData::containsColor() const
104{
105    notImplemented();
106    return false;
107}
108
109bool DragData::canSmartReplace() const
110{
111    // Mimic the situations in which mac allows drag&drop to do a smart replace.
112    // This is allowed whenever the drag data contains a 'range' (ie.,
113    // ClipboardWin::writeRange is called).  For example, dragging a link
114    // should not result in a space being added.
115    return !m_platformDragData->plainText.isEmpty()
116        && !m_platformDragData->url.isValid();
117}
118
119bool DragData::containsCompatibleContent() const
120{
121    return containsPlainText()
122        || containsURL()
123        || containsHTML(m_platformDragData)
124        || containsColor()
125        || containsFiles();
126}
127
128PassRefPtr<DocumentFragment> DragData::asFragment(Document* doc) const
129{
130    /*
131     * Order is richest format first. On OSX this is:
132     * * Web Archive
133     * * Filenames
134     * * HTML
135     * * RTF
136     * * TIFF
137     * * PICT
138     */
139
140    if (containsFiles()) {
141        // FIXME: Implement this.  Should be pretty simple to make some HTML
142        // and call createFragmentFromMarkup.
143        //if (RefPtr<DocumentFragment> fragment = createFragmentFromMarkup(doc,
144        //    ?, KURL()))
145        //    return fragment;
146    }
147
148    if (!m_platformDragData->textHtml.isEmpty()) {
149        RefPtr<DocumentFragment> fragment = createFragmentFromMarkup(doc,
150            m_platformDragData->textHtml, m_platformDragData->htmlBaseUrl, FragmentScriptingNotAllowed);
151        return fragment.release();
152    }
153
154    return 0;
155}
156
157Color DragData::asColor() const
158{
159    notImplemented();
160    return Color();
161}
162
163} // namespace WebCore
164