[iOS DnD] Support DataTransfer.getData and DataTransfer.setData when dragging or...
[WebKit-https.git] / Source / WebCore / platform / ios / PlatformPasteboardIOS.mm
1 /*
2  * Copyright (C) 2013 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 "PlatformPasteboard.h"
28
29 #import "Color.h"
30 #import "Image.h"
31 #import "Pasteboard.h"
32 #import "SharedBuffer.h"
33 #import "URL.h"
34 #import "UTIUtilities.h"
35 #import "WebItemProviderPasteboard.h"
36 #import <MobileCoreServices/MobileCoreServices.h>
37 #import <UIKit/UIImage.h>
38 #import <UIKit/UIPasteboard.h>
39 #import <pal/spi/ios/UIKitSPI.h>
40 #import <wtf/SoftLinking.h>
41
42 SOFT_LINK_FRAMEWORK(UIKit)
43 SOFT_LINK_CLASS(UIKit, UIImage)
44 SOFT_LINK_CLASS(UIKit, UIPasteboard)
45
46 namespace WebCore {
47
48 PlatformPasteboard::PlatformPasteboard()
49     : m_pasteboard([getUIPasteboardClass() generalPasteboard])
50 {
51 }
52
53 #if ENABLE(DATA_INTERACTION)
54 PlatformPasteboard::PlatformPasteboard(const String& name)
55 {
56     if (name == "data interaction pasteboard")
57         m_pasteboard = [WebItemProviderPasteboard sharedInstance];
58     else
59         m_pasteboard = [getUIPasteboardClass() generalPasteboard];
60 }
61 #else
62 PlatformPasteboard::PlatformPasteboard(const String&)
63     : m_pasteboard([getUIPasteboardClass() generalPasteboard])
64 {
65 }
66 #endif
67
68 void PlatformPasteboard::getTypes(Vector<String>& types)
69 {
70     for (NSString *pasteboardType in [m_pasteboard pasteboardTypes])
71         types.append(pasteboardType);
72 }
73
74 void PlatformPasteboard::getTypesByFidelityForItemAtIndex(Vector<String>& types, int index)
75 {
76     if (index >= [m_pasteboard numberOfItems] || ![m_pasteboard respondsToSelector:@selector(pasteboardTypesByFidelityForItemAtIndex:)])
77         return;
78
79     NSArray *pasteboardTypesByFidelity = [m_pasteboard pasteboardTypesByFidelityForItemAtIndex:index];
80     for (NSString *typeIdentifier in pasteboardTypesByFidelity)
81         types.append(typeIdentifier);
82 }
83
84 RefPtr<SharedBuffer> PlatformPasteboard::bufferForType(const String&)
85 {
86     return nullptr;
87 }
88
89 void PlatformPasteboard::getPathnamesForType(Vector<String>&, const String&)
90 {
91 }
92
93 int PlatformPasteboard::numberOfFiles()
94 {
95     return [m_pasteboard respondsToSelector:@selector(numberOfFiles)] ? [m_pasteboard numberOfFiles] : 0;
96 }
97
98 Vector<String> PlatformPasteboard::filenamesForDataInteraction()
99 {
100     if (![m_pasteboard respondsToSelector:@selector(droppedFileURLs)])
101         return { };
102
103     Vector<String> filenames;
104     for (NSURL *fileURL in [m_pasteboard droppedFileURLs])
105         filenames.append(fileURL.path);
106
107     return filenames;
108 }
109
110 String PlatformPasteboard::stringForType(const String& type)
111 {
112     NSArray *values = [m_pasteboard valuesForPasteboardType:type inItemSet:[NSIndexSet indexSetWithIndex:0]];
113     for (id value in values) {
114         if ([value isKindOfClass:[NSURL class]])
115             return [(NSURL *)value absoluteString];
116
117         if ([value isKindOfClass:[NSAttributedString class]])
118             return [(NSAttributedString *)value string];
119
120         if ([value isKindOfClass:[NSString class]])
121             return (NSString *)value;
122     }
123     return String();
124 }
125
126 Color PlatformPasteboard::color()
127 {
128     return Color();
129 }
130
131 URL PlatformPasteboard::url()
132 {
133     return URL();
134 }
135
136 long PlatformPasteboard::copy(const String&)
137 {
138     return 0;
139 }
140
141 long PlatformPasteboard::addTypes(const Vector<String>&)
142 {
143     return 0;
144 }
145
146 long PlatformPasteboard::setTypes(const Vector<String>&)
147 {
148     return 0;
149 }
150
151 long PlatformPasteboard::setBufferForType(SharedBuffer*, const String&)
152 {
153     return 0;
154 }
155
156 long PlatformPasteboard::setPathnamesForType(const Vector<String>&, const String&)
157 {
158     return 0;
159 }
160
161 long PlatformPasteboard::setStringForType(const String&, const String&)
162 {
163     return 0;
164 }
165
166 long PlatformPasteboard::changeCount() const
167 {
168     return [m_pasteboard changeCount];
169 }
170
171 String PlatformPasteboard::uniqueName()
172 {
173     return String();
174 }
175
176 static NSString *webIOSPastePboardType = @"iOS rich content paste pasteboard type";
177
178 #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 110000
179
180 static void registerItemToPasteboard(WebItemProviderRegistrationInfoList *representationsToRegister, id <AbstractPasteboard> pasteboard)
181 {
182     if (UIItemProvider *itemProvider = representationsToRegister.itemProvider)
183         [pasteboard setItemProviders:@[ itemProvider ]];
184     else
185         [pasteboard setItemProviders:@[ ]];
186
187     if ([pasteboard respondsToSelector:@selector(stageRegistrationList:)])
188         [pasteboard stageRegistrationList:representationsToRegister];
189 }
190
191 #else
192
193 static RetainPtr<NSDictionary> richTextRepresentationsForPasteboardWebContent(const PasteboardWebContent& content)
194 {
195     RetainPtr<NSMutableDictionary> representations = adoptNS([[NSMutableDictionary alloc] init]);
196
197     ASSERT(content.clientTypes.size() == content.clientData.size());
198     for (size_t i = 0, size = content.clientTypes.size(); i < size; ++i)
199         [representations setValue:content.clientData[i]->createNSData().get() forKey:content.clientTypes[i]];
200
201     if (content.dataInWebArchiveFormat) {
202         [representations setValue:(NSData *)content.dataInWebArchiveFormat->createNSData().get() forKey:WebArchivePboardType];
203         // Flag for UIKit to know that this copy contains rich content. This will trigger a two-step paste.
204         [representations setValue:webIOSPastePboardType forKey:webIOSPastePboardType];
205     }
206
207     if (content.dataInRTFDFormat)
208         [representations setValue:content.dataInRTFDFormat->createNSData().get() forKey:(NSString *)kUTTypeFlatRTFD];
209     if (content.dataInRTFFormat)
210         [representations setValue:content.dataInRTFFormat->createNSData().get() forKey:(NSString *)kUTTypeRTF];
211
212     return representations;
213 }
214
215 #endif
216
217 #if ENABLE(DATA_INTERACTION)
218
219 static void addRepresentationsForPlainText(WebItemProviderRegistrationInfoList *itemsToRegister, const String& plainText)
220 {
221     if (plainText.isEmpty())
222         return;
223
224     NSURL *platformURL = [NSURL URLWithString:plainText];
225     if (URL(platformURL).isValid())
226         [itemsToRegister addRepresentingObject:platformURL];
227
228     [itemsToRegister addData:[(NSString *)plainText dataUsingEncoding:NSUTF8StringEncoding] forType:(NSString *)kUTTypeUTF8PlainText];
229 }
230
231 bool PlatformPasteboard::allowReadingURLAtIndex(const URL& url, int index) const
232 {
233     NSItemProvider *itemProvider = (NSUInteger)index < [m_pasteboard itemProviders].count ? [[m_pasteboard itemProviders] objectAtIndex:index] : nil;
234     for (NSString *type in itemProvider.registeredTypeIdentifiers) {
235         if (UTTypeConformsTo((CFStringRef)type, kUTTypeURL))
236             return true;
237     }
238
239     return url.isValid();
240 }
241
242 #else
243
244 bool PlatformPasteboard::allowReadingURLAtIndex(const URL&, int) const
245 {
246     return true;
247 }
248
249 #endif
250
251 void PlatformPasteboard::write(const PasteboardWebContent& content)
252 {
253 #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 110000
254     auto representationsToRegister = adoptNS([[WebItemProviderRegistrationInfoList alloc] init]);
255
256     [representationsToRegister addData:[webIOSPastePboardType dataUsingEncoding:NSUTF8StringEncoding] forType:webIOSPastePboardType];
257
258     ASSERT(content.clientTypes.size() == content.clientData.size());
259     for (size_t i = 0, size = content.clientTypes.size(); i < size; ++i)
260         [representationsToRegister addData:content.clientData[i]->createNSData().get() forType:content.clientTypes[i]];
261
262     if (content.dataInWebArchiveFormat)
263         [representationsToRegister addData:content.dataInWebArchiveFormat->createNSData().get() forType:WebArchivePboardType];
264
265     if (content.dataInAttributedStringFormat) {
266         NSAttributedString *attributedString = [NSKeyedUnarchiver unarchiveObjectWithData:content.dataInAttributedStringFormat->createNSData().get()];
267         if (attributedString)
268             [representationsToRegister addRepresentingObject:attributedString];
269     }
270
271     if (content.dataInRTFDFormat)
272         [representationsToRegister addData:content.dataInRTFDFormat->createNSData().get() forType:(NSString *)kUTTypeFlatRTFD];
273
274     if (content.dataInRTFFormat)
275         [representationsToRegister addData:content.dataInRTFFormat->createNSData().get() forType:(NSString *)kUTTypeRTF];
276
277     if (!content.dataInStringFormat.isEmpty())
278         addRepresentationsForPlainText(representationsToRegister.get(), content.dataInStringFormat);
279
280     registerItemToPasteboard(representationsToRegister.get(), m_pasteboard.get());
281 #else
282     RetainPtr<NSMutableDictionary> representations = adoptNS([[NSMutableDictionary alloc] init]);
283     [representations addEntriesFromDictionary:richTextRepresentationsForPasteboardWebContent(content).autorelease()];
284
285     NSString *textAsString = content.dataInStringFormat;
286     [representations setValue:[textAsString dataUsingEncoding:NSUTF8StringEncoding] forKey:(NSString *)kUTTypeUTF8PlainText];
287     [representations setValue:[textAsString dataUsingEncoding:NSUTF16StringEncoding] forKey:(NSString *)kUTTypeUTF16PlainText];
288     // FIXME: We vend "public.text" here for backwards compatibility with pre-iOS 11 apps. In the future, we should stop vending this UTI,
289     // and instead set data for concrete plain text types. See <https://bugs.webkit.org/show_bug.cgi?id=173317>.
290     [representations setValue:textAsString forKey:(NSString *)kUTTypeText];
291
292     // Explicitly cast m_pasteboard to UIPasteboard * to work around rdar://problem/33383354.
293     ASSERT([m_pasteboard isKindOfClass:getUIPasteboardClass()]);
294     [(UIPasteboard *)m_pasteboard setItems:@[representations.get()]];
295 #endif
296 }
297
298 void PlatformPasteboard::write(const PasteboardImage& pasteboardImage)
299 {
300 #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 110000
301     auto representationsToRegister = adoptNS([[WebItemProviderRegistrationInfoList alloc] init]);
302
303     auto& types = pasteboardImage.clientTypes;
304     auto& data = pasteboardImage.clientData;
305     ASSERT(types.size() == data.size());
306     for (size_t i = 0, size = types.size(); i < size; ++i)
307         [representationsToRegister addData:data[i]->createNSData().get() forType:types[i]];
308
309     if (pasteboardImage.resourceData && !pasteboardImage.resourceMIMEType.isEmpty()) {
310         auto utiOrMIMEType = pasteboardImage.resourceMIMEType;
311         if (!isDeclaredUTI(utiOrMIMEType))
312             utiOrMIMEType = UTIFromMIMEType(utiOrMIMEType);
313
314         auto imageData = pasteboardImage.resourceData->createNSData();
315         [representationsToRegister addData:imageData.get() forType:(NSString *)utiOrMIMEType];
316         [representationsToRegister setPreferredPresentationSize:pasteboardImage.imageSize];
317         [representationsToRegister setSuggestedName:pasteboardImage.suggestedName];
318     }
319
320     // FIXME: When writing a PasteboardImage, we currently always place the image data at a higer fidelity than the
321     // associated image URL. However, in the case of an image enclosed by an anchor, we might want to consider the
322     // the URL (i.e. the anchor's href attribute) to be a higher fidelity representation.
323     if (!pasteboardImage.url.url.isEmpty()) {
324         if (NSURL *nsURL = pasteboardImage.url.url)
325             [representationsToRegister addRepresentingObject:nsURL];
326     }
327
328     registerItemToPasteboard(representationsToRegister.get(), m_pasteboard.get());
329 #else
330     RetainPtr<NSMutableDictionary> representations = adoptNS([[NSMutableDictionary alloc] init]);
331     if (!pasteboardImage.resourceMIMEType.isNull()) {
332         [representations setObject:pasteboardImage.resourceData->createNSData().get() forKey:pasteboardImage.resourceMIMEType];
333         if (!pasteboardImage.url.url.isNull())
334             [representations setObject:(NSURL *)pasteboardImage.url.url forKey:(NSString *)kUTTypeURL];
335     }
336
337     // Explicitly cast m_pasteboard to UIPasteboard * to work around rdar://problem/33383354.
338     ASSERT([m_pasteboard isKindOfClass:getUIPasteboardClass()]);
339     [(UIPasteboard *)m_pasteboard setItems:@[representations.get()]];
340 #endif
341 }
342
343 void PlatformPasteboard::write(const String& pasteboardType, const String& text)
344 {
345 #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 110000
346     auto representationsToRegister = adoptNS([[WebItemProviderRegistrationInfoList alloc] init]);
347     [representationsToRegister setPreferredPresentationStyle:WebPreferredPresentationStyleInline];
348
349     NSString *pasteboardTypeAsNSString = pasteboardType;
350     if (!text.isEmpty() && pasteboardTypeAsNSString.length) {
351         auto pasteboardTypeAsCFString = (CFStringRef)pasteboardTypeAsNSString;
352         if (UTTypeConformsTo(pasteboardTypeAsCFString, kUTTypeURL) || UTTypeConformsTo(pasteboardTypeAsCFString, kUTTypeText))
353             addRepresentationsForPlainText(representationsToRegister.get(), text);
354         else
355             [representationsToRegister addData:[pasteboardTypeAsNSString dataUsingEncoding:NSUTF8StringEncoding] forType:pasteboardType];
356     }
357
358     registerItemToPasteboard(representationsToRegister.get(), m_pasteboard.get());
359 #else
360     RetainPtr<NSDictionary> representations = adoptNS([[NSMutableDictionary alloc] init]);
361
362     NSString *textAsString = text;
363     if (pasteboardType == String(kUTTypeURL)) {
364         [representations setValue:adoptNS([[NSURL alloc] initWithString:text]).get() forKey:pasteboardType];
365         [representations setValue:textAsString forKey:(NSString *)kUTTypeText];
366     } else if (!pasteboardType.isNull())
367         [representations setValue:textAsString forKey:pasteboardType];
368
369     auto cfPasteboardType = pasteboardType.createCFString();
370     if (UTTypeConformsTo(cfPasteboardType.get(), kUTTypeText) || UTTypeConformsTo(cfPasteboardType.get(), kUTTypeURL)) {
371         [representations setValue:[textAsString dataUsingEncoding:NSUTF8StringEncoding] forKey:(NSString *)kUTTypeUTF8PlainText];
372         [representations setValue:[textAsString dataUsingEncoding:NSUTF16StringEncoding] forKey:(NSString *)kUTTypeUTF16PlainText];
373     }
374
375     // Explicitly cast m_pasteboard to UIPasteboard * to work around rdar://problem/33383354.
376     ASSERT([m_pasteboard isKindOfClass:getUIPasteboardClass()]);
377     [(UIPasteboard *)m_pasteboard setItems:@[representations.get()]];
378 #endif
379 }
380
381 void PlatformPasteboard::write(const PasteboardURL& url)
382 {
383 #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 110000
384     auto representationsToRegister = adoptNS([[WebItemProviderRegistrationInfoList alloc] init]);
385     [representationsToRegister setPreferredPresentationStyle:WebPreferredPresentationStyleInline];
386
387     if (NSURL *nsURL = url.url) {
388         if (!url.title.isEmpty())
389             nsURL._title = url.title;
390         [representationsToRegister addRepresentingObject:nsURL];
391     }
392
393     registerItemToPasteboard(representationsToRegister.get(), m_pasteboard.get());
394 #else
395     write(kUTTypeURL, url.url.string());
396 #endif
397 }
398
399 int PlatformPasteboard::count()
400 {
401     return [m_pasteboard numberOfItems];
402 }
403
404 RefPtr<SharedBuffer> PlatformPasteboard::readBuffer(int index, const String& type)
405 {
406     NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:index];
407
408     RetainPtr<NSArray> pasteboardItem = [m_pasteboard dataForPasteboardType:type inItemSet:indexSet];
409
410     if (![pasteboardItem count])
411         return nullptr;
412     return SharedBuffer::create([pasteboardItem.get() objectAtIndex:0]);
413 }
414
415 String PlatformPasteboard::readString(int index, const String& type)
416 {
417     NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:index];
418
419     RetainPtr<NSArray> pasteboardItem = [m_pasteboard valuesForPasteboardType:type inItemSet:indexSet];
420
421     if (![pasteboardItem count])
422         return String();
423
424     id value = [pasteboardItem objectAtIndex:0];
425     
426     if (type == String(kUTTypePlainText) || type == String(kUTTypeHTML)) {
427         ASSERT([value isKindOfClass:[NSString class]]);
428         return [value isKindOfClass:[NSString class]] ? value : nil;
429     }
430     if (type == String(kUTTypeText)) {
431         ASSERT([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSAttributedString class]]);
432         if ([value isKindOfClass:[NSString class]])
433             return value;
434         if ([value isKindOfClass:[NSAttributedString class]])
435             return [(NSAttributedString *)value string];
436     } else if (type == String(kUTTypeURL)) {
437         ASSERT([value isKindOfClass:[NSURL class]]);
438         if ([value isKindOfClass:[NSURL class]] && allowReadingURLAtIndex((NSURL *)value, index))
439             return [(NSURL *)value absoluteString];
440     }
441
442     return String();
443 }
444
445 URL PlatformPasteboard::readURL(int index, const String& type, String& title)
446 {
447     NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:index];
448
449     RetainPtr<NSArray> pasteboardItem = [m_pasteboard valuesForPasteboardType:type inItemSet:indexSet];
450
451     if (![pasteboardItem count])
452         return URL();
453
454     id value = [pasteboardItem objectAtIndex:0];
455     ASSERT([value isKindOfClass:[NSURL class]]);
456     if (![value isKindOfClass:[NSURL class]])
457         return URL();
458
459     if (!allowReadingURLAtIndex((NSURL *)value, index))
460         return { };
461
462 #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 110000
463     title = [value _title];
464 #else
465     UNUSED_PARAM(title);
466 #endif
467
468     return (NSURL *)value;
469 }
470
471 void PlatformPasteboard::updateSupportedTypeIdentifiers(const Vector<String>& types)
472 {
473     if (![m_pasteboard respondsToSelector:@selector(updateSupportedTypeIdentifiers:)])
474         return;
475
476     NSMutableArray *typesArray = [NSMutableArray arrayWithCapacity:types.size()];
477     for (auto type : types)
478         [typesArray addObject:(NSString *)type];
479
480     [m_pasteboard updateSupportedTypeIdentifiers:typesArray];
481 }
482
483 }