[Attachment Support] Create attachment elements when dropping files on iOS
[WebKit-https.git] / Source / WebCore / platform / mac / DragDataMac.mm
1 /*
2  * Copyright (C) 2007 Apple 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
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #import "config.h"
27 #import "DragData.h"
28
29 #if ENABLE(DRAG_SUPPORT)
30 #import "LegacyNSPasteboardTypes.h"
31 #import "MIMETypeRegistry.h"
32 #import "NotImplemented.h"
33 #import "Pasteboard.h"
34 #import "PasteboardStrategy.h"
35 #import "PlatformPasteboard.h"
36 #import "PlatformStrategies.h"
37 #import "RuntimeEnabledFeatures.h"
38 #import "WebCoreNSURLExtras.h"
39
40 #if PLATFORM(IOS)
41 #import <MobileCoreServices/MobileCoreServices.h>
42 #endif
43
44 namespace WebCore {
45
46 static inline String rtfPasteboardType()
47 {
48 #if PLATFORM(IOS)
49     return String(kUTTypeRTF);
50 #else
51     return String(legacyRTFPasteboardType());
52 #endif
53 }
54
55 static inline String rtfdPasteboardType()
56 {
57 #if PLATFORM(IOS)
58     return String(kUTTypeFlatRTFD);
59 #else
60     return String(legacyRTFDPasteboardType());
61 #endif
62 }
63
64 static inline String stringPasteboardType()
65 {
66 #if PLATFORM(IOS)
67     return String(kUTTypeText);
68 #else
69     return String(legacyStringPasteboardType());
70 #endif
71 }
72
73 static inline String urlPasteboardType()
74 {
75 #if PLATFORM(IOS)
76     return String(kUTTypeURL);
77 #else
78     return String(legacyURLPasteboardType());
79 #endif
80 }
81
82 static inline String htmlPasteboardType()
83 {
84 #if PLATFORM(IOS)
85     return String(kUTTypeHTML);
86 #else
87     return String(legacyHTMLPasteboardType());
88 #endif
89 }
90
91 static inline String colorPasteboardType()
92 {
93 #if PLATFORM(IOS)
94     return "com.apple.uikit.color";
95 #else
96     return String(legacyColorPasteboardType());
97 #endif
98 }
99
100 static inline String pdfPasteboardType()
101 {
102 #if PLATFORM(IOS)
103     return String(kUTTypePDF);
104 #else
105     return String(legacyPDFPasteboardType());
106 #endif
107 }
108
109 static inline String tiffPasteboardType()
110 {
111 #if PLATFORM(IOS)
112     return String(kUTTypeTIFF);
113 #else
114     return String(legacyTIFFPasteboardType());
115 #endif
116 }
117
118 DragData::DragData(DragDataRef data, const IntPoint& clientPosition, const IntPoint& globalPosition, DragOperation sourceOperationMask, DragApplicationFlags flags, DragDestinationAction destinationAction)
119     : m_clientPosition(clientPosition)
120     , m_globalPosition(globalPosition)
121     , m_platformDragData(data)
122     , m_draggingSourceOperationMask(sourceOperationMask)
123     , m_applicationFlags(flags)
124     , m_dragDestinationAction(destinationAction)
125 #if PLATFORM(MAC)
126     , m_pasteboardName([[m_platformDragData draggingPasteboard] name])
127 #else
128     , m_pasteboardName("data interaction pasteboard")
129 #endif
130 {
131 }
132
133 DragData::DragData(const String& dragStorageName, const IntPoint& clientPosition, const IntPoint& globalPosition, DragOperation sourceOperationMask, DragApplicationFlags flags, DragDestinationAction destinationAction)
134     : m_clientPosition(clientPosition)
135     , m_globalPosition(globalPosition)
136     , m_platformDragData(0)
137     , m_draggingSourceOperationMask(sourceOperationMask)
138     , m_applicationFlags(flags)
139     , m_dragDestinationAction(destinationAction)
140     , m_pasteboardName(dragStorageName)
141 {
142 }
143
144 bool DragData::containsURLTypeIdentifier() const
145 {
146     Vector<String> types;
147     platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
148     return types.contains(urlPasteboardType());
149 }
150     
151 bool DragData::canSmartReplace() const
152 {
153     return Pasteboard(m_pasteboardName).canSmartReplace();
154 }
155
156 bool DragData::containsColor() const
157 {
158     Vector<String> types;
159     platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
160     return types.contains(colorPasteboardType());
161 }
162
163 bool DragData::containsFiles() const
164 {
165     return numberOfFiles();
166 }
167
168 unsigned DragData::numberOfFiles() const
169 {
170     return platformStrategies()->pasteboardStrategy()->getNumberOfFiles(m_pasteboardName);
171 }
172
173 Vector<String> DragData::asFilenames() const
174 {
175 #if PLATFORM(MAC)
176     Vector<String> results;
177     platformStrategies()->pasteboardStrategy()->getPathnamesForType(results, String(legacyFilenamesPasteboardType()), m_pasteboardName);
178     if (!results.isEmpty())
179         return results;
180 #endif
181     return fileNames();
182 }
183
184 bool DragData::containsPlainText() const
185 {
186     Vector<String> types;
187     platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
188
189     return types.contains(stringPasteboardType())
190         || types.contains(rtfdPasteboardType())
191         || types.contains(rtfPasteboardType())
192 #if PLATFORM(MAC)
193         || types.contains(String(legacyFilenamesPasteboardType()))
194 #endif
195         || platformStrategies()->pasteboardStrategy()->stringForType(urlPasteboardType(), m_pasteboardName).length();
196 }
197
198 String DragData::asPlainText() const
199 {
200     Pasteboard pasteboard(m_pasteboardName);
201     PasteboardPlainText text;
202     pasteboard.read(text);
203     String string = text.text;
204
205     // FIXME: It's not clear this is 100% correct since we know -[NSURL URLWithString:] does not handle
206     // all the same cases we handle well in the URL code for creating an NSURL.
207     if (text.isURL)
208         return userVisibleString([NSURL URLWithString:string]);
209
210     // FIXME: WTF should offer a non-Mac-specific way to convert string to precomposed form so we can do it for all platforms.
211     return [(NSString *)string precomposedStringWithCanonicalMapping];
212 }
213
214 Color DragData::asColor() const
215 {
216     return platformStrategies()->pasteboardStrategy()->color(m_pasteboardName);
217 }
218
219 bool DragData::containsCompatibleContent(DraggingPurpose purpose) const
220 {
221     if (purpose == DraggingPurpose::ForFileUpload)
222         return containsFiles();
223
224     if (purpose == DraggingPurpose::ForEditing && RuntimeEnabledFeatures::sharedFeatures().attachmentElementEnabled() && containsFiles())
225         return true;
226
227     Vector<String> types;
228     platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
229     return types.contains(String(WebArchivePboardType))
230         || types.contains(htmlPasteboardType())
231 #if PLATFORM(MAC)
232         || types.contains(String(legacyFilenamesPasteboardType()))
233         || types.contains(String(legacyFilesPromisePasteboardType()))
234 #endif
235         || types.contains(tiffPasteboardType())
236         || types.contains(pdfPasteboardType())
237         || types.contains(urlPasteboardType())
238         || types.contains(rtfdPasteboardType())
239         || types.contains(rtfPasteboardType())
240         || types.contains(String(kUTTypeUTF8PlainText))
241         || types.contains(stringPasteboardType())
242         || types.contains(colorPasteboardType())
243         || types.contains(String(kUTTypeJPEG))
244         || types.contains(String(kUTTypePNG));
245 }
246
247 bool DragData::containsPromise() const
248 {
249     Vector<String> files;
250 #if PLATFORM(MAC)
251     platformStrategies()->pasteboardStrategy()->getPathnamesForType(files, String(legacyFilesPromisePasteboardType()), m_pasteboardName);
252 #endif
253     return files.size() == 1;
254 }
255
256 bool DragData::containsURL(FilenameConversionPolicy filenamePolicy) const
257 {
258 #if PLATFORM(IOS)
259     UNUSED_PARAM(filenamePolicy);
260     Vector<String> types;
261     platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
262     if (!types.contains(urlPasteboardType()))
263         return false;
264
265     auto urlString = platformStrategies()->pasteboardStrategy()->stringForType(urlPasteboardType(), m_pasteboardName);
266     if (urlString.isEmpty()) {
267         // On iOS, we don't get access to the contents of UIItemProviders until we perform the drag operation.
268         // Thus, we consider DragData to contain an URL if it contains the `public.url` UTI type. Later down the
269         // road, when we perform the drag operation, we can then check if the URL's protocol is http or https,
270         // and if it isn't, we bail out of page navigation.
271         return true;
272     }
273
274     URL webcoreURL = [NSURL URLWithString:urlString];
275     return webcoreURL.protocolIs("http") || webcoreURL.protocolIs("https");
276 #else
277     return !asURL(filenamePolicy).isEmpty();
278 #endif
279 }
280
281 String DragData::asURL(FilenameConversionPolicy, String* title) const
282 {
283     // FIXME: Use filenamePolicy.
284
285     if (title) {
286 #if PLATFORM(MAC)
287         String URLTitleString = platformStrategies()->pasteboardStrategy()->stringForType(String(WebURLNamePboardType), m_pasteboardName);
288         if (!URLTitleString.isEmpty())
289             *title = URLTitleString;
290 #endif
291     }
292     
293     Vector<String> types;
294     platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
295
296     if (types.contains(urlPasteboardType())) {
297         NSURL *URLFromPasteboard = [NSURL URLWithString:platformStrategies()->pasteboardStrategy()->stringForType(urlPasteboardType(), m_pasteboardName)];
298         NSString *scheme = [URLFromPasteboard scheme];
299         // Cannot drop other schemes unless <rdar://problem/10562662> and <rdar://problem/11187315> are fixed.
300         if ([scheme isEqualToString:@"http"] || [scheme isEqualToString:@"https"])
301             return [URLByCanonicalizingURL(URLFromPasteboard) absoluteString];
302     }
303     
304     if (types.contains(stringPasteboardType())) {
305         NSURL *URLFromPasteboard = [NSURL URLWithString:platformStrategies()->pasteboardStrategy()->stringForType(stringPasteboardType(), m_pasteboardName)];
306         NSString *scheme = [URLFromPasteboard scheme];
307         // Pasteboard content is not trusted, because JavaScript code can modify it. We can sanitize it for URLs and other typed content, but not for strings.
308         // The result of this function is used to initiate navigation, so we shouldn't allow arbitrary file URLs.
309         // FIXME: Should we allow only http family schemes, or anything non-local?
310         if ([scheme isEqualToString:@"http"] || [scheme isEqualToString:@"https"])
311             return [URLByCanonicalizingURL(URLFromPasteboard) absoluteString];
312     }
313     
314 #if PLATFORM(MAC)
315     if (types.contains(String(legacyFilenamesPasteboardType()))) {
316         Vector<String> files;
317         platformStrategies()->pasteboardStrategy()->getPathnamesForType(files, String(legacyFilenamesPasteboardType()), m_pasteboardName);
318         if (files.size() == 1) {
319             BOOL isDirectory;
320             if ([[NSFileManager defaultManager] fileExistsAtPath:files[0] isDirectory:&isDirectory] && isDirectory)
321                 return String();
322             return [URLByCanonicalizingURL([NSURL fileURLWithPath:files[0]]) absoluteString];
323         }
324     }
325
326     if (types.contains(String(legacyFilesPromisePasteboardType())) && fileNames().size() == 1)
327         return [URLByCanonicalizingURL([NSURL fileURLWithPath:fileNames()[0]]) absoluteString];
328 #endif
329
330     return String();        
331 }
332
333 } // namespace WebCore
334
335 #endif // ENABLE(DRAG_SUPPORT)