1 /*
2 * Copyright (C) 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2013 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 #include "config.h"
28 #include "core/page/DragData.h"
29
30 #include "core/clipboard/DataObject.h"
31 #include "core/dom/Document.h"
32 #include "core/dom/DocumentFragment.h"
33 #include "core/dom/Range.h"
34 #include "core/editing/markup.h"
35 #include "core/frame/LocalFrame.h"
36 #include "platform/FileMetadata.h"
37 #include "platform/clipboard/ClipboardMimeTypes.h"
38 #include "platform/weborigin/KURL.h"
39 #include "wtf/text/WTFString.h"
40
41 namespace WebCore {
42
DragData(DataObject * data,const IntPoint & clientPosition,const IntPoint & globalPosition,DragOperation sourceOperationMask,DragApplicationFlags flags)43 DragData::DragData(DataObject* data, const IntPoint& clientPosition, const IntPoint& globalPosition,
44 DragOperation sourceOperationMask, DragApplicationFlags flags)
45 : m_clientPosition(clientPosition)
46 , m_globalPosition(globalPosition)
47 , m_platformDragData(data)
48 , m_draggingSourceOperationMask(sourceOperationMask)
49 , m_applicationFlags(flags)
50 {
51 }
52
DragData(const String &,const IntPoint & clientPosition,const IntPoint & globalPosition,DragOperation sourceOperationMask,DragApplicationFlags flags)53 DragData::DragData(const String&, const IntPoint& clientPosition, const IntPoint& globalPosition,
54 DragOperation sourceOperationMask, DragApplicationFlags flags)
55 : m_clientPosition(clientPosition)
56 , m_globalPosition(globalPosition)
57 , m_platformDragData(0)
58 , m_draggingSourceOperationMask(sourceOperationMask)
59 , m_applicationFlags(flags)
60 {
61 }
62
containsHTML(const DataObject * dropData)63 static bool containsHTML(const DataObject* dropData)
64 {
65 return dropData->types().contains(mimeTypeTextHTML);
66 }
67
containsURL(FilenameConversionPolicy filenamePolicy) const68 bool DragData::containsURL(FilenameConversionPolicy filenamePolicy) const
69 {
70 return m_platformDragData->types().contains(mimeTypeTextURIList)
71 || (filenamePolicy == ConvertFilenames && m_platformDragData->containsFilenames());
72 }
73
asURL(FilenameConversionPolicy filenamePolicy,String * title) const74 String DragData::asURL(FilenameConversionPolicy filenamePolicy, String* title) const
75 {
76 String url;
77 if (m_platformDragData->types().contains(mimeTypeTextURIList))
78 m_platformDragData->urlAndTitle(url, title);
79 else if (filenamePolicy == ConvertFilenames && containsFiles())
80 url = filePathToURL(m_platformDragData->filenames()[0]);
81 return url;
82 }
83
containsFiles() const84 bool DragData::containsFiles() const
85 {
86 return m_platformDragData->containsFilenames();
87 }
88
numberOfFiles() const89 unsigned DragData::numberOfFiles() const
90 {
91 return m_platformDragData->filenames().size();
92 }
93
modifierKeyState() const94 int DragData::modifierKeyState() const
95 {
96 return m_platformDragData->modifierKeyState();
97 }
98
asFilenames(Vector<String> & result) const99 void DragData::asFilenames(Vector<String>& result) const
100 {
101 const Vector<String>& filenames = m_platformDragData->filenames();
102 for (size_t i = 0; i < filenames.size(); ++i)
103 result.append(filenames[i]);
104 }
105
containsPlainText() const106 bool DragData::containsPlainText() const
107 {
108 return m_platformDragData->types().contains(mimeTypeTextPlain);
109 }
110
asPlainText() const111 String DragData::asPlainText() const
112 {
113 return m_platformDragData->getData(mimeTypeTextPlain);
114 }
115
canSmartReplace() const116 bool DragData::canSmartReplace() const
117 {
118 // Mimic the situations in which mac allows drag&drop to do a smart replace.
119 // This is allowed whenever the drag data contains a 'range' (ie.,
120 // ClipboardWin::writeRange is called). For example, dragging a link
121 // should not result in a space being added.
122 return m_platformDragData->types().contains(mimeTypeTextPlain)
123 && !m_platformDragData->types().contains(mimeTypeTextURIList);
124 }
125
containsCompatibleContent() const126 bool DragData::containsCompatibleContent() const
127 {
128 return containsPlainText()
129 || containsURL()
130 || containsHTML(m_platformDragData)
131 || containsFiles();
132 }
133
asFragment(LocalFrame * frame,PassRefPtrWillBeRawPtr<Range>,bool,bool &) const134 PassRefPtrWillBeRawPtr<DocumentFragment> DragData::asFragment(LocalFrame* frame, PassRefPtrWillBeRawPtr<Range>, bool, bool&) const
135 {
136 /*
137 * Order is richest format first. On OSX this is:
138 * * Web Archive
139 * * Filenames
140 * * HTML
141 * * RTF
142 * * TIFF
143 * * PICT
144 */
145
146 if (containsFiles()) {
147 // FIXME: Implement this. Should be pretty simple to make some HTML
148 // and call createFragmentFromMarkup.
149 }
150
151 if (m_platformDragData->types().contains(mimeTypeTextHTML)) {
152 String html;
153 KURL baseURL;
154 m_platformDragData->htmlAndBaseURL(html, baseURL);
155 ASSERT(frame->document());
156 if (RefPtrWillBeRawPtr<DocumentFragment> fragment = createFragmentFromMarkup(*frame->document(), html, baseURL, DisallowScriptingAndPluginContent))
157 return fragment.release();
158 }
159
160 return nullptr;
161 }
162
droppedFileSystemId() const163 String DragData::droppedFileSystemId() const
164 {
165 return m_platformDragData->filesystemId();
166 }
167
168 } // namespace WebCore
169