[iOS] Remove unused pre-iOS 11 codepaths for writing to the pasteboard
[WebKit.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/ListHashSet.h>
41 #import <wtf/SoftLinking.h>
42 #import <wtf/text/StringHash.h>
43
44 SOFT_LINK_FRAMEWORK(UIKit)
45 SOFT_LINK_CLASS(UIKit, UIImage)
46 SOFT_LINK_CLASS(UIKit, UIPasteboard)
47
48 namespace WebCore {
49
50 PlatformPasteboard::PlatformPasteboard()
51     : m_pasteboard([getUIPasteboardClass() generalPasteboard])
52 {
53 }
54
55 #if PLATFORM(IOS) && !(PLATFORM(WATCHOS) || PLATFORM(APPLETV))
56 PlatformPasteboard::PlatformPasteboard(const String& name)
57 {
58     if (name == "data interaction pasteboard")
59         m_pasteboard = [WebItemProviderPasteboard sharedInstance];
60     else
61         m_pasteboard = [getUIPasteboardClass() generalPasteboard];
62 }
63 #else
64 PlatformPasteboard::PlatformPasteboard(const String&)
65     : m_pasteboard([getUIPasteboardClass() generalPasteboard])
66 {
67 }
68 #endif
69
70 void PlatformPasteboard::getTypes(Vector<String>& types)
71 {
72     for (NSString *pasteboardType in [m_pasteboard pasteboardTypes])
73         types.append(pasteboardType);
74 }
75
76 void PlatformPasteboard::getTypesByFidelityForItemAtIndex(Vector<String>& types, int index)
77 {
78     if (index >= [m_pasteboard numberOfItems] || ![m_pasteboard respondsToSelector:@selector(pasteboardTypesByFidelityForItemAtIndex:)])
79         return;
80
81     NSArray *pasteboardTypesByFidelity = [m_pasteboard pasteboardTypesByFidelityForItemAtIndex:index];
82     for (NSString *typeIdentifier in pasteboardTypesByFidelity)
83         types.append(typeIdentifier);
84 }
85
86 RefPtr<SharedBuffer> PlatformPasteboard::bufferForType(const String& type)
87 {
88     if (NSData *data = [m_pasteboard dataForPasteboardType:type])
89         return SharedBuffer::create(data);
90     return nullptr;
91 }
92
93 void PlatformPasteboard::getPathnamesForType(Vector<String>&, const String&) const
94 {
95 }
96
97 int PlatformPasteboard::numberOfFiles() const
98 {
99     return [m_pasteboard respondsToSelector:@selector(numberOfFiles)] ? [m_pasteboard numberOfFiles] : 0;
100 }
101
102 Vector<String> PlatformPasteboard::filenamesForDataInteraction()
103 {
104     if (![m_pasteboard respondsToSelector:@selector(droppedFileURLs)])
105         return { };
106
107     Vector<String> filenames;
108     for (NSURL *fileURL in [m_pasteboard droppedFileURLs])
109         filenames.append(fileURL.path);
110
111     return filenames;
112 }
113
114 String PlatformPasteboard::stringForType(const String& type)
115 {
116     NSArray *values = [m_pasteboard valuesForPasteboardType:type inItemSet:[NSIndexSet indexSetWithIndex:0]];
117     for (id value in values) {
118         if ([value isKindOfClass:[NSURL class]])
119             return [(NSURL *)value absoluteString];
120
121         if ([value isKindOfClass:[NSAttributedString class]])
122             return [(NSAttributedString *)value string];
123
124         if ([value isKindOfClass:[NSString class]])
125             return (NSString *)value;
126     }
127     return String();
128 }
129
130 Color PlatformPasteboard::color()
131 {
132     return Color();
133 }
134
135 URL PlatformPasteboard::url()
136 {
137     return URL();
138 }
139
140 long PlatformPasteboard::copy(const String&)
141 {
142     return 0;
143 }
144
145 long PlatformPasteboard::addTypes(const Vector<String>&)
146 {
147     return 0;
148 }
149
150 long PlatformPasteboard::setTypes(const Vector<String>&)
151 {
152     return 0;
153 }
154
155 long PlatformPasteboard::setBufferForType(SharedBuffer*, const String&)
156 {
157     return 0;
158 }
159
160 long PlatformPasteboard::setPathnamesForType(const Vector<String>&, const String&)
161 {
162     return 0;
163 }
164
165 long PlatformPasteboard::setStringForType(const String&, const String&)
166 {
167     return 0;
168 }
169
170 long PlatformPasteboard::changeCount() const
171 {
172     return [m_pasteboard changeCount];
173 }
174
175 String PlatformPasteboard::uniqueName()
176 {
177     return String();
178 }
179
180 String PlatformPasteboard::platformPasteboardTypeForSafeTypeForDOMToReadAndWrite(const String& domType)
181 {
182     if (domType == "text/plain")
183         return kUTTypePlainText;
184
185     if (domType == "text/html")
186         return kUTTypeHTML;
187
188     if (domType == "text/uri-list")
189         return kUTTypeURL;
190
191     return { };
192 }
193
194 #if PLATFORM(IOS) && !(PLATFORM(WATCHOS) || PLATFORM(APPLETV))
195
196 static NSString *webIOSPastePboardType = @"iOS rich content paste pasteboard type";
197
198 static void registerItemToPasteboard(WebItemProviderRegistrationInfoList *representationsToRegister, id <AbstractPasteboard> pasteboard)
199 {
200     if (UIItemProvider *itemProvider = representationsToRegister.itemProvider)
201         [pasteboard setItemProviders:@[ itemProvider ]];
202     else
203         [pasteboard setItemProviders:@[ ]];
204
205     if ([pasteboard respondsToSelector:@selector(stageRegistrationList:)])
206         [pasteboard stageRegistrationList:representationsToRegister];
207 }
208
209 static void addRepresentationsForPlainText(WebItemProviderRegistrationInfoList *itemsToRegister, const String& plainText)
210 {
211     if (plainText.isEmpty())
212         return;
213
214     NSURL *platformURL = [NSURL URLWithString:plainText];
215     if (URL(platformURL).isValid())
216         [itemsToRegister addRepresentingObject:platformURL];
217
218     [itemsToRegister addData:[(NSString *)plainText dataUsingEncoding:NSUTF8StringEncoding] forType:(NSString *)kUTTypeUTF8PlainText];
219 }
220
221 bool PlatformPasteboard::allowReadingURLAtIndex(const URL& url, int index) const
222 {
223     NSItemProvider *itemProvider = (NSUInteger)index < [m_pasteboard itemProviders].count ? [[m_pasteboard itemProviders] objectAtIndex:index] : nil;
224     for (NSString *type in itemProvider.registeredTypeIdentifiers) {
225         if (UTTypeConformsTo((CFStringRef)type, kUTTypeURL))
226             return true;
227     }
228
229     return url.isValid();
230 }
231
232 void PlatformPasteboard::write(const PasteboardWebContent& content)
233 {
234     auto representationsToRegister = adoptNS([[WebItemProviderRegistrationInfoList alloc] init]);
235
236     [representationsToRegister addData:[webIOSPastePboardType dataUsingEncoding:NSUTF8StringEncoding] forType:webIOSPastePboardType];
237
238     ASSERT(content.clientTypes.size() == content.clientData.size());
239     for (size_t i = 0, size = content.clientTypes.size(); i < size; ++i)
240         [representationsToRegister addData:content.clientData[i]->createNSData().get() forType:content.clientTypes[i]];
241
242     if (content.dataInWebArchiveFormat)
243         [representationsToRegister addData:content.dataInWebArchiveFormat->createNSData().get() forType:WebArchivePboardType];
244
245     if (content.dataInAttributedStringFormat) {
246         NSAttributedString *attributedString = [NSKeyedUnarchiver unarchiveObjectWithData:content.dataInAttributedStringFormat->createNSData().get()];
247         if (attributedString)
248             [representationsToRegister addRepresentingObject:attributedString];
249     }
250
251     if (content.dataInRTFDFormat)
252         [representationsToRegister addData:content.dataInRTFDFormat->createNSData().get() forType:(NSString *)kUTTypeFlatRTFD];
253
254     if (content.dataInRTFFormat)
255         [representationsToRegister addData:content.dataInRTFFormat->createNSData().get() forType:(NSString *)kUTTypeRTF];
256
257     if (!content.dataInHTMLFormat.isEmpty()) {
258         NSData *htmlAsData = [(NSString *)content.dataInHTMLFormat dataUsingEncoding:NSUTF8StringEncoding];
259         [representationsToRegister addData:htmlAsData forType:(NSString *)kUTTypeHTML];
260     }
261
262     if (!content.dataInStringFormat.isEmpty())
263         addRepresentationsForPlainText(representationsToRegister.get(), content.dataInStringFormat);
264
265     registerItemToPasteboard(representationsToRegister.get(), m_pasteboard.get());
266 }
267
268 void PlatformPasteboard::write(const PasteboardImage& pasteboardImage)
269 {
270     auto representationsToRegister = adoptNS([[WebItemProviderRegistrationInfoList alloc] init]);
271
272     auto& types = pasteboardImage.clientTypes;
273     auto& data = pasteboardImage.clientData;
274     ASSERT(types.size() == data.size());
275     for (size_t i = 0, size = types.size(); i < size; ++i)
276         [representationsToRegister addData:data[i]->createNSData().get() forType:types[i]];
277
278     if (pasteboardImage.resourceData && !pasteboardImage.resourceMIMEType.isEmpty()) {
279         auto utiOrMIMEType = pasteboardImage.resourceMIMEType;
280         if (!isDeclaredUTI(utiOrMIMEType))
281             utiOrMIMEType = UTIFromMIMEType(utiOrMIMEType);
282
283         auto imageData = pasteboardImage.resourceData->createNSData();
284         [representationsToRegister addData:imageData.get() forType:(NSString *)utiOrMIMEType];
285         [representationsToRegister setPreferredPresentationSize:pasteboardImage.imageSize];
286         [representationsToRegister setSuggestedName:pasteboardImage.suggestedName];
287     }
288
289     // FIXME: When writing a PasteboardImage, we currently always place the image data at a higer fidelity than the
290     // associated image URL. However, in the case of an image enclosed by an anchor, we might want to consider the
291     // the URL (i.e. the anchor's href attribute) to be a higher fidelity representation.
292     if (!pasteboardImage.url.url.isEmpty()) {
293         if (NSURL *nsURL = pasteboardImage.url.url)
294             [representationsToRegister addRepresentingObject:nsURL];
295     }
296
297     registerItemToPasteboard(representationsToRegister.get(), m_pasteboard.get());
298 }
299
300 void PlatformPasteboard::write(const String& pasteboardType, const String& text)
301 {
302     auto representationsToRegister = adoptNS([[WebItemProviderRegistrationInfoList alloc] init]);
303     [representationsToRegister setPreferredPresentationStyle:WebPreferredPresentationStyleInline];
304
305     NSString *pasteboardTypeAsNSString = pasteboardType;
306     if (!text.isEmpty() && pasteboardTypeAsNSString.length) {
307         auto pasteboardTypeAsCFString = (CFStringRef)pasteboardTypeAsNSString;
308         if (UTTypeConformsTo(pasteboardTypeAsCFString, kUTTypeURL) || UTTypeConformsTo(pasteboardTypeAsCFString, kUTTypeText))
309             addRepresentationsForPlainText(representationsToRegister.get(), text);
310         else
311             [representationsToRegister addData:[pasteboardTypeAsNSString dataUsingEncoding:NSUTF8StringEncoding] forType:pasteboardType];
312     }
313
314     registerItemToPasteboard(representationsToRegister.get(), m_pasteboard.get());
315 }
316
317 void PlatformPasteboard::write(const PasteboardURL& url)
318 {
319     auto representationsToRegister = adoptNS([[WebItemProviderRegistrationInfoList alloc] init]);
320     [representationsToRegister setPreferredPresentationStyle:WebPreferredPresentationStyleInline];
321
322     if (NSURL *nsURL = url.url) {
323         if (!url.title.isEmpty())
324             nsURL._title = url.title;
325         [representationsToRegister addRepresentingObject:nsURL];
326     }
327
328     registerItemToPasteboard(representationsToRegister.get(), m_pasteboard.get());
329 }
330
331 static const char *safeTypeForDOMToReadAndWriteForPlatformType(const String& platformType)
332 {
333     auto cfType = platformType.createCFString();
334     if (UTTypeConformsTo(cfType.get(), kUTTypePlainText))
335         return ASCIILiteral("text/plain");
336
337     if (UTTypeConformsTo(cfType.get(), kUTTypeHTML))
338         return ASCIILiteral("text/html");
339
340     if (UTTypeConformsTo(cfType.get(), kUTTypeURL))
341         return ASCIILiteral("text/uri-list");
342
343     if (Pasteboard::shouldTreatCocoaTypeAsFile(platformType))
344         return ASCIILiteral("Files");
345
346     return nullptr;
347 }
348
349 Vector<String> PlatformPasteboard::typesSafeForDOMToReadAndWrite() const
350 {
351     ListHashSet<String> domPasteboardTypes;
352     for (NSItemProvider *provider in [m_pasteboard itemProviders]) {
353         if (!provider.teamData.length)
354             continue;
355
356         id teamDataObject = [NSKeyedUnarchiver unarchiveObjectWithData:provider.teamData];
357         if (!teamDataObject || ![teamDataObject isKindOfClass:[NSDictionary class]])
358             continue;
359
360         id customTypes = [(NSDictionary *)teamDataObject objectForKey:@(customWebKitPasteboardDataType)];
361         if (![customTypes isKindOfClass:[NSArray class]])
362             continue;
363
364         for (NSString *type in customTypes)
365             domPasteboardTypes.add(type);
366     }
367
368     if (NSData *serializedCustomData = [m_pasteboard dataForPasteboardType:@(customWebKitPasteboardDataType)]) {
369         auto buffer = SharedBuffer::create(serializedCustomData);
370         for (auto& type : customDataFromSharedBuffer(buffer.get()).orderedTypes)
371             domPasteboardTypes.add(type);
372     }
373
374     for (NSString *type in [m_pasteboard pasteboardTypes]) {
375         if ([type isEqualToString:@(customWebKitPasteboardDataType)])
376             continue;
377
378         if (isSafeTypeForDOMToReadAndWrite(type)) {
379             domPasteboardTypes.add(type);
380             continue;
381         }
382
383         if (auto* coercedType = safeTypeForDOMToReadAndWriteForPlatformType(type))
384             domPasteboardTypes.add(String::fromUTF8(coercedType));
385     }
386
387     Vector<String> result;
388     copyToVector(domPasteboardTypes, result);
389     return result;
390 }
391
392 long PlatformPasteboard::write(const PasteboardCustomData& data)
393 {
394     auto representationsToRegister = adoptNS([[WebItemProviderRegistrationInfoList alloc] init]);
395     [representationsToRegister setPreferredPresentationStyle:WebPreferredPresentationStyleInline];
396
397     if (data.sameOriginCustomData.size()) {
398         if (auto serializedSharedBuffer = sharedBufferFromCustomData(data)->createNSData()) {
399             // We stash the list of supplied pasteboard types in teamData here for compatibility with drag and drop.
400             // Since the contents of item providers cannot be loaded prior to drop, but the pasteboard types are
401             // contained within the custom data blob and we need to vend them to the page when firing `dragover`
402             // events, we need an additional in-memory representation of the pasteboard types array that contains
403             // all of the custom types. We use the teamData property, available on NSItemProvider on iOS, to store
404             // this information, since the contents of teamData are immediately available prior to the drop.
405             NSMutableArray<NSString *> *typesAsNSArray = [NSMutableArray array];
406             for (auto& type : data.orderedTypes)
407                 [typesAsNSArray addObject:type];
408             [representationsToRegister setTeamData:[NSKeyedArchiver archivedDataWithRootObject:@{ @(customWebKitPasteboardDataType) : typesAsNSArray }]];
409             [representationsToRegister addData:serializedSharedBuffer.get() forType:@(customWebKitPasteboardDataType)];
410         }
411     }
412
413     for (auto& type : data.orderedTypes) {
414         NSString *stringValue = data.platformData.get(type);
415         if (!stringValue.length)
416             continue;
417
418         auto cocoaType = platformPasteboardTypeForSafeTypeForDOMToReadAndWrite(type).createCFString();
419         if (UTTypeConformsTo(cocoaType.get(), kUTTypeURL))
420             [representationsToRegister addRepresentingObject:[NSURL URLWithString:stringValue]];
421         else if (UTTypeConformsTo(cocoaType.get(), kUTTypePlainText))
422             [representationsToRegister addRepresentingObject:stringValue];
423         else
424             [representationsToRegister addData:[stringValue dataUsingEncoding:NSUTF8StringEncoding] forType:(NSString *)cocoaType.get()];
425     }
426
427     registerItemToPasteboard(representationsToRegister.get(), m_pasteboard.get());
428     return [m_pasteboard changeCount];
429 }
430
431 #else
432
433 bool PlatformPasteboard::allowReadingURLAtIndex(const URL&, int) const
434 {
435     return false;
436 }
437
438 void PlatformPasteboard::write(const PasteboardWebContent&)
439 {
440 }
441
442 void PlatformPasteboard::write(const PasteboardImage&)
443 {
444 }
445
446 void PlatformPasteboard::write(const String&, const String&)
447 {
448 }
449
450 void PlatformPasteboard::write(const PasteboardURL&)
451 {
452 }
453
454 Vector<String> PlatformPasteboard::typesSafeForDOMToReadAndWrite() const
455 {
456     return { };
457 }
458
459 long PlatformPasteboard::write(const PasteboardCustomData&)
460 {
461     return 0;
462 }
463
464 #endif
465
466 int PlatformPasteboard::count()
467 {
468     return [m_pasteboard numberOfItems];
469 }
470
471 RefPtr<SharedBuffer> PlatformPasteboard::readBuffer(int index, const String& type)
472 {
473     NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:index];
474
475     RetainPtr<NSArray> pasteboardItem = [m_pasteboard dataForPasteboardType:type inItemSet:indexSet];
476
477     if (![pasteboardItem count])
478         return nullptr;
479     return SharedBuffer::create([pasteboardItem.get() objectAtIndex:0]);
480 }
481
482 String PlatformPasteboard::readString(int index, const String& type)
483 {
484     NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:index];
485
486     NSArray *pasteboardValues = [m_pasteboard valuesForPasteboardType:type inItemSet:indexSet];
487     if (!pasteboardValues.count) {
488         NSArray<NSData *> *pasteboardData = [m_pasteboard dataForPasteboardType:type inItemSet:indexSet];
489         if (!pasteboardData.count)
490             return { };
491         pasteboardValues = pasteboardData;
492     }
493
494     RetainPtr<id> value = [pasteboardValues objectAtIndex:0];
495     if ([value isKindOfClass:[NSData class]])
496         value = adoptNS([[NSString alloc] initWithData:(NSData *)value.get() encoding:NSUTF8StringEncoding]);
497     
498     if (type == String(kUTTypePlainText) || type == String(kUTTypeHTML)) {
499         ASSERT([value isKindOfClass:[NSString class]]);
500         return [value isKindOfClass:[NSString class]] ? value.get() : nil;
501     }
502     if (type == String(kUTTypeText)) {
503         ASSERT([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSAttributedString class]]);
504         if ([value isKindOfClass:[NSString class]])
505             return value.get();
506         if ([value isKindOfClass:[NSAttributedString class]])
507             return [(NSAttributedString *)value string];
508     } else if (type == String(kUTTypeURL)) {
509         ASSERT([value isKindOfClass:[NSURL class]] || [value isKindOfClass:[NSString class]]);
510         if ([value isKindOfClass:[NSString class]])
511             value = [NSURL URLWithString:value.get()];
512         if ([value isKindOfClass:[NSURL class]] && allowReadingURLAtIndex((NSURL *)value, index))
513             return [(NSURL *)value absoluteString];
514     }
515
516     return String();
517 }
518
519 URL PlatformPasteboard::readURL(int index, const String& type, String& title)
520 {
521     NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:index];
522
523     RetainPtr<NSArray> pasteboardItem = [m_pasteboard valuesForPasteboardType:type inItemSet:indexSet];
524
525     if (![pasteboardItem count])
526         return URL();
527
528     id value = [pasteboardItem objectAtIndex:0];
529     ASSERT([value isKindOfClass:[NSURL class]]);
530     if (![value isKindOfClass:[NSURL class]])
531         return URL();
532
533     if (!allowReadingURLAtIndex((NSURL *)value, index))
534         return { };
535
536 #if PLATFORM(IOS) && !(PLATFORM(WATCHOS) || PLATFORM(APPLETV))
537     title = [value _title];
538 #else
539     UNUSED_PARAM(title);
540 #endif
541
542     return (NSURL *)value;
543 }
544
545 void PlatformPasteboard::updateSupportedTypeIdentifiers(const Vector<String>& types)
546 {
547     if (![m_pasteboard respondsToSelector:@selector(updateSupportedTypeIdentifiers:)])
548         return;
549
550     NSMutableArray *typesArray = [NSMutableArray arrayWithCapacity:types.size()];
551     for (auto type : types)
552         [typesArray addObject:(NSString *)type];
553
554     [m_pasteboard updateSupportedTypeIdentifiers:typesArray];
555 }
556
557 }