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