Switch from PLATFORM(IOS) to PLATFORM(IOS_FAMILY)
[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_FAMILY)
41 #import <MobileCoreServices/MobileCoreServices.h>
42 #endif
43
44 namespace WebCore {
45
46 static inline String rtfPasteboardType()
47 {
48 #if PLATFORM(IOS_FAMILY)
49     return String(kUTTypeRTF);
50 #else
51     return String(legacyRTFPasteboardType());
52 #endif
53 }
54
55 static inline String rtfdPasteboardType()
56 {
57 #if PLATFORM(IOS_FAMILY)
58     return String(kUTTypeFlatRTFD);
59 #else
60     return String(legacyRTFDPasteboardType());
61 #endif
62 }
63
64 static inline String stringPasteboardType()
65 {
66 #if PLATFORM(IOS_FAMILY)
67     return String(kUTTypeText);
68 #else
69     return String(legacyStringPasteboardType());
70 #endif
71 }
72
73 static inline String urlPasteboardType()
74 {
75 #if PLATFORM(IOS_FAMILY)
76     return String(kUTTypeURL);
77 #else
78     return String(legacyURLPasteboardType());
79 #endif
80 }
81
82 static inline String htmlPasteboardType()
83 {
84 #if PLATFORM(IOS_FAMILY)
85     return String(kUTTypeHTML);
86 #else
87     return String(legacyHTMLPasteboardType());
88 #endif
89 }
90
91 static inline String colorPasteboardType()
92 {
93 #if PLATFORM(IOS_FAMILY)
94     return String { UIColorPboardType };
95 #else
96     return String(legacyColorPasteboardType());
97 #endif
98 }
99
100 static inline String pdfPasteboardType()
101 {
102 #if PLATFORM(IOS_FAMILY)
103     return String(kUTTypePDF);
104 #else
105     return String(legacyPDFPasteboardType());
106 #endif
107 }
108
109 static inline String tiffPasteboardType()
110 {
111 #if PLATFORM(IOS_FAMILY)
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> types;
177     platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
178     if (types.contains(String(legacyFilesPromisePasteboardType())))
179         return fileNames();
180
181     Vector<String> results;
182     platformStrategies()->pasteboardStrategy()->getPathnamesForType(results, String(legacyFilenamesPasteboardType()), m_pasteboardName);
183     return results;
184 #else
185     return fileNames();
186 #endif
187 }
188
189 bool DragData::containsPlainText() const
190 {
191     Vector<String> types;
192     platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
193
194     return types.contains(stringPasteboardType())
195         || types.contains(rtfdPasteboardType())
196         || types.contains(rtfPasteboardType())
197 #if PLATFORM(MAC)
198         || types.contains(String(legacyFilenamesPasteboardType()))
199 #endif
200         || platformStrategies()->pasteboardStrategy()->stringForType(urlPasteboardType(), m_pasteboardName).length();
201 }
202
203 String DragData::asPlainText() const
204 {
205     Pasteboard pasteboard(m_pasteboardName);
206     PasteboardPlainText text;
207     pasteboard.read(text);
208     String string = text.text;
209
210     // FIXME: It's not clear this is 100% correct since we know -[NSURL URLWithString:] does not handle
211     // all the same cases we handle well in the URL code for creating an NSURL.
212     if (text.isURL)
213         return userVisibleString([NSURL URLWithString:string]);
214
215     // FIXME: WTF should offer a non-Mac-specific way to convert string to precomposed form so we can do it for all platforms.
216     return [(NSString *)string precomposedStringWithCanonicalMapping];
217 }
218
219 Color DragData::asColor() const
220 {
221     return platformStrategies()->pasteboardStrategy()->color(m_pasteboardName);
222 }
223
224 bool DragData::containsCompatibleContent(DraggingPurpose purpose) const
225 {
226     if (purpose == DraggingPurpose::ForFileUpload)
227         return containsFiles();
228
229     if (purpose == DraggingPurpose::ForColorControl)
230         return containsColor();
231
232     if (purpose == DraggingPurpose::ForEditing && RuntimeEnabledFeatures::sharedFeatures().attachmentElementEnabled() && containsFiles())
233         return true;
234
235     Vector<String> types;
236     platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
237     return types.contains(String(WebArchivePboardType))
238         || types.contains(htmlPasteboardType())
239 #if PLATFORM(MAC)
240         || types.contains(String(legacyFilenamesPasteboardType()))
241         || types.contains(String(legacyFilesPromisePasteboardType()))
242 #endif
243         || types.contains(tiffPasteboardType())
244         || types.contains(pdfPasteboardType())
245         || types.contains(urlPasteboardType())
246         || types.contains(rtfdPasteboardType())
247         || types.contains(rtfPasteboardType())
248         || types.contains(String(kUTTypeUTF8PlainText))
249         || types.contains(stringPasteboardType())
250         || types.contains(colorPasteboardType())
251         || types.contains(String(kUTTypeJPEG))
252         || types.contains(String(kUTTypePNG));
253 }
254
255 bool DragData::containsPromise() const
256 {
257     // FIXME: legacyFilesPromisePasteboardType() contains UTIs, not path names. Also, why do we
258     // think promises should only contain one file (or UTI)?
259     Vector<String> files;
260 #if PLATFORM(MAC)
261     platformStrategies()->pasteboardStrategy()->getPathnamesForType(files, String(legacyFilesPromisePasteboardType()), m_pasteboardName);
262 #endif
263     return files.size() == 1;
264 }
265
266 bool DragData::containsURL(FilenameConversionPolicy filenamePolicy) const
267 {
268 #if PLATFORM(IOS_FAMILY)
269     UNUSED_PARAM(filenamePolicy);
270     Vector<String> types;
271     platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
272     if (!types.contains(urlPasteboardType()))
273         return false;
274
275     auto urlString = platformStrategies()->pasteboardStrategy()->stringForType(urlPasteboardType(), m_pasteboardName);
276     if (urlString.isEmpty()) {
277         // On iOS, we don't get access to the contents of UIItemProviders until we perform the drag operation.
278         // Thus, we consider DragData to contain an URL if it contains the `public.url` UTI type. Later down the
279         // road, when we perform the drag operation, we can then check if the URL's protocol is http or https,
280         // and if it isn't, we bail out of page navigation.
281         return true;
282     }
283
284     URL webcoreURL = [NSURL URLWithString:urlString];
285     return webcoreURL.protocolIs("http") || webcoreURL.protocolIs("https");
286 #else
287     return !asURL(filenamePolicy).isEmpty();
288 #endif
289 }
290
291 String DragData::asURL(FilenameConversionPolicy, String* title) const
292 {
293     // FIXME: Use filenamePolicy.
294
295     if (title) {
296 #if PLATFORM(MAC)
297         String URLTitleString = platformStrategies()->pasteboardStrategy()->stringForType(String(WebURLNamePboardType), m_pasteboardName);
298         if (!URLTitleString.isEmpty())
299             *title = URLTitleString;
300 #endif
301     }
302     
303     Vector<String> types;
304     platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
305
306     if (types.contains(urlPasteboardType())) {
307         NSURL *URLFromPasteboard = [NSURL URLWithString:platformStrategies()->pasteboardStrategy()->stringForType(urlPasteboardType(), m_pasteboardName)];
308         NSString *scheme = [URLFromPasteboard scheme];
309         // Cannot drop other schemes unless <rdar://problem/10562662> and <rdar://problem/11187315> are fixed.
310         if ([scheme isEqualToString:@"http"] || [scheme isEqualToString:@"https"])
311             return [URLByCanonicalizingURL(URLFromPasteboard) absoluteString];
312     }
313     
314     if (types.contains(stringPasteboardType())) {
315         NSURL *URLFromPasteboard = [NSURL URLWithString:platformStrategies()->pasteboardStrategy()->stringForType(stringPasteboardType(), m_pasteboardName)];
316         NSString *scheme = [URLFromPasteboard scheme];
317         // 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.
318         // The result of this function is used to initiate navigation, so we shouldn't allow arbitrary file URLs.
319         // FIXME: Should we allow only http family schemes, or anything non-local?
320         if ([scheme isEqualToString:@"http"] || [scheme isEqualToString:@"https"])
321             return [URLByCanonicalizingURL(URLFromPasteboard) absoluteString];
322     }
323     
324 #if PLATFORM(MAC)
325     if (types.contains(String(legacyFilenamesPasteboardType()))) {
326         Vector<String> files;
327         platformStrategies()->pasteboardStrategy()->getPathnamesForType(files, String(legacyFilenamesPasteboardType()), m_pasteboardName);
328         if (files.size() == 1) {
329             BOOL isDirectory;
330             if ([[NSFileManager defaultManager] fileExistsAtPath:files[0] isDirectory:&isDirectory] && isDirectory)
331                 return String();
332             return [URLByCanonicalizingURL([NSURL fileURLWithPath:files[0]]) absoluteString];
333         }
334     }
335
336     if (types.contains(String(legacyFilesPromisePasteboardType())) && fileNames().size() == 1)
337         return [URLByCanonicalizingURL([NSURL fileURLWithPath:fileNames()[0]]) absoluteString];
338 #endif
339
340     return String();        
341 }
342
343 } // namespace WebCore
344
345 #endif // ENABLE(DRAG_SUPPORT)