bfcecafb1f20e6a67c5c56eb448531762d50ef52
[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/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     return nullptr;
344 }
345
346 Vector<String> PlatformPasteboard::typesSafeForDOMToReadAndWrite() const
347 {
348     ListHashSet<String> domPasteboardTypes;
349     for (NSItemProvider *provider in [m_pasteboard itemProviders]) {
350         if (!provider.teamData.length)
351             continue;
352
353         id teamDataObject = [NSKeyedUnarchiver unarchiveObjectWithData:provider.teamData];
354         if (!teamDataObject || ![teamDataObject isKindOfClass:[NSDictionary class]])
355             continue;
356
357         id customTypes = [(NSDictionary *)teamDataObject objectForKey:@(PasteboardCustomData::cocoaType())];
358         if (![customTypes isKindOfClass:[NSArray class]])
359             continue;
360
361         for (NSString *type in customTypes)
362             domPasteboardTypes.add(type);
363     }
364
365     if (NSData *serializedCustomData = [m_pasteboard dataForPasteboardType:@(PasteboardCustomData::cocoaType())]) {
366         auto buffer = SharedBuffer::create(serializedCustomData);
367         for (auto& type : PasteboardCustomData::fromSharedBuffer(buffer.get()).orderedTypes)
368             domPasteboardTypes.add(type);
369     }
370
371     for (NSString *type in [m_pasteboard pasteboardTypes]) {
372         if ([type isEqualToString:@(PasteboardCustomData::cocoaType())])
373             continue;
374
375         if (Pasteboard::isSafeTypeForDOMToReadAndWrite(type)) {
376             domPasteboardTypes.add(type);
377             continue;
378         }
379
380         if (auto* coercedType = safeTypeForDOMToReadAndWriteForPlatformType(type))
381             domPasteboardTypes.add(String::fromUTF8(coercedType));
382     }
383
384     Vector<String> result;
385     copyToVector(domPasteboardTypes, result);
386     return result;
387 }
388
389 long PlatformPasteboard::write(const PasteboardCustomData& data)
390 {
391     auto representationsToRegister = adoptNS([[WebItemProviderRegistrationInfoList alloc] init]);
392     [representationsToRegister setPreferredPresentationStyle:WebPreferredPresentationStyleInline];
393
394     if (data.sameOriginCustomData.size()) {
395         if (auto serializedSharedBuffer = data.createSharedBuffer()->createNSData()) {
396             // We stash the list of supplied pasteboard types in teamData here for compatibility with drag and drop.
397             // Since the contents of item providers cannot be loaded prior to drop, but the pasteboard types are
398             // contained within the custom data blob and we need to vend them to the page when firing `dragover`
399             // events, we need an additional in-memory representation of the pasteboard types array that contains
400             // all of the custom types. We use the teamData property, available on NSItemProvider on iOS, to store
401             // this information, since the contents of teamData are immediately available prior to the drop.
402             NSMutableArray<NSString *> *typesAsNSArray = [NSMutableArray array];
403             for (auto& type : data.orderedTypes)
404                 [typesAsNSArray addObject:type];
405             [representationsToRegister setTeamData:[NSKeyedArchiver archivedDataWithRootObject:@{ @(PasteboardCustomData::cocoaType()) : typesAsNSArray }]];
406             [representationsToRegister addData:serializedSharedBuffer.get() forType:@(PasteboardCustomData::cocoaType())];
407         }
408     }
409
410     for (auto& type : data.orderedTypes) {
411         NSString *stringValue = data.platformData.get(type);
412         if (!stringValue.length)
413             continue;
414
415         auto cocoaType = platformPasteboardTypeForSafeTypeForDOMToReadAndWrite(type).createCFString();
416         if (UTTypeConformsTo(cocoaType.get(), kUTTypeURL))
417             [representationsToRegister addRepresentingObject:[NSURL URLWithString:stringValue]];
418         else if (UTTypeConformsTo(cocoaType.get(), kUTTypePlainText))
419             [representationsToRegister addRepresentingObject:stringValue];
420         else
421             [representationsToRegister addData:[stringValue dataUsingEncoding:NSUTF8StringEncoding] forType:(NSString *)cocoaType.get()];
422     }
423
424     registerItemToPasteboard(representationsToRegister.get(), m_pasteboard.get());
425     return [m_pasteboard changeCount];
426 }
427
428 #else
429
430 bool PlatformPasteboard::allowReadingURLAtIndex(const URL&, int) const
431 {
432     return false;
433 }
434
435 void PlatformPasteboard::write(const PasteboardWebContent&)
436 {
437 }
438
439 void PlatformPasteboard::write(const PasteboardImage&)
440 {
441 }
442
443 void PlatformPasteboard::write(const String&, const String&)
444 {
445 }
446
447 void PlatformPasteboard::write(const PasteboardURL&)
448 {
449 }
450
451 Vector<String> PlatformPasteboard::typesSafeForDOMToReadAndWrite() const
452 {
453     return { };
454 }
455
456 long PlatformPasteboard::write(const PasteboardCustomData&)
457 {
458     return 0;
459 }
460
461 #endif
462
463 int PlatformPasteboard::count()
464 {
465     return [m_pasteboard numberOfItems];
466 }
467
468 RefPtr<SharedBuffer> PlatformPasteboard::readBuffer(int index, const String& type)
469 {
470     NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:index];
471
472     RetainPtr<NSArray> pasteboardItem = [m_pasteboard dataForPasteboardType:type inItemSet:indexSet];
473
474     if (![pasteboardItem count])
475         return nullptr;
476     return SharedBuffer::create([pasteboardItem.get() objectAtIndex:0]);
477 }
478
479 String PlatformPasteboard::readString(int index, const String& type)
480 {
481     NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:index];
482
483     NSArray *pasteboardValues = [m_pasteboard valuesForPasteboardType:type inItemSet:indexSet];
484     if (!pasteboardValues.count) {
485         NSArray<NSData *> *pasteboardData = [m_pasteboard dataForPasteboardType:type inItemSet:indexSet];
486         if (!pasteboardData.count)
487             return { };
488         pasteboardValues = pasteboardData;
489     }
490
491     RetainPtr<id> value = [pasteboardValues objectAtIndex:0];
492     if ([value isKindOfClass:[NSData class]])
493         value = adoptNS([[NSString alloc] initWithData:(NSData *)value.get() encoding:NSUTF8StringEncoding]);
494     
495     if (type == String(kUTTypePlainText) || type == String(kUTTypeHTML)) {
496         ASSERT([value isKindOfClass:[NSString class]]);
497         return [value isKindOfClass:[NSString class]] ? value.get() : nil;
498     }
499     if (type == String(kUTTypeText)) {
500         ASSERT([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSAttributedString class]]);
501         if ([value isKindOfClass:[NSString class]])
502             return value.get();
503         if ([value isKindOfClass:[NSAttributedString class]])
504             return [(NSAttributedString *)value string];
505     } else if (type == String(kUTTypeURL)) {
506         ASSERT([value isKindOfClass:[NSURL class]] || [value isKindOfClass:[NSString class]]);
507         if ([value isKindOfClass:[NSString class]])
508             value = [NSURL URLWithString:value.get()];
509         if ([value isKindOfClass:[NSURL class]] && allowReadingURLAtIndex((NSURL *)value, index))
510             return [(NSURL *)value absoluteString];
511     }
512
513     return String();
514 }
515
516 URL PlatformPasteboard::readURL(int index, const String& type, String& title)
517 {
518     NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:index];
519
520     RetainPtr<NSArray> pasteboardItem = [m_pasteboard valuesForPasteboardType:type inItemSet:indexSet];
521
522     if (![pasteboardItem count])
523         return URL();
524
525     id value = [pasteboardItem objectAtIndex:0];
526     ASSERT([value isKindOfClass:[NSURL class]]);
527     if (![value isKindOfClass:[NSURL class]])
528         return URL();
529
530     if (!allowReadingURLAtIndex((NSURL *)value, index))
531         return { };
532
533 #if PLATFORM(IOS) && !(PLATFORM(WATCHOS) || PLATFORM(APPLETV))
534     title = [value _title];
535 #else
536     UNUSED_PARAM(title);
537 #endif
538
539     return (NSURL *)value;
540 }
541
542 void PlatformPasteboard::updateSupportedTypeIdentifiers(const Vector<String>& types)
543 {
544     if (![m_pasteboard respondsToSelector:@selector(updateSupportedTypeIdentifiers:)])
545         return;
546
547     NSMutableArray *typesArray = [NSMutableArray arrayWithCapacity:types.size()];
548     for (auto type : types)
549         [typesArray addObject:(NSString *)type];
550
551     [m_pasteboard updateSupportedTypeIdentifiers:typesArray];
552 }
553
554 }