[iOS] Respect type fidelities when copying image elements to the pasteboard
[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(fileURLsForDataInteraction)])
101         return { };
102
103     Vector<String> filenames;
104     for (NSURL *fileURL in [m_pasteboard fileURLsForDataInteraction])
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_MAX_ALLOWED < 110000
179
180 static RetainPtr<NSDictionary> richTextRepresentationsForPasteboardWebContent(const PasteboardWebContent& content)
181 {
182     RetainPtr<NSMutableDictionary> representations = adoptNS([[NSMutableDictionary alloc] init]);
183
184     ASSERT(content.clientTypes.size() == content.clientData.size());
185     for (size_t i = 0, size = content.clientTypes.size(); i < size; ++i)
186         [representations setValue:content.clientData[i]->createNSData().get() forKey:content.clientTypes[i]];
187
188     if (content.dataInWebArchiveFormat) {
189         [representations setValue:(NSData *)content.dataInWebArchiveFormat->createNSData().get() forKey:WebArchivePboardType];
190         // Flag for UIKit to know that this copy contains rich content. This will trigger a two-step paste.
191         [representations setValue:webIOSPastePboardType forKey:webIOSPastePboardType];
192     }
193
194     if (content.dataInRTFDFormat)
195         [representations setValue:content.dataInRTFDFormat->createNSData().get() forKey:(NSString *)kUTTypeFlatRTFD];
196     if (content.dataInRTFFormat)
197         [representations setValue:content.dataInRTFFormat->createNSData().get() forKey:(NSString *)kUTTypeRTF];
198
199     return representations;
200 }
201
202 #else
203
204 static void registerItemToPasteboard(WebItemProviderRegistrationInfoList *representationsToRegister, id <AbstractPasteboard> pasteboard)
205 {
206     UIItemProvider *itemProvider = [representationsToRegister itemProvider];
207     if (!itemProvider) {
208         [pasteboard setItemProviders:@[ ]];
209         return;
210     }
211
212     [pasteboard setItemProviders:@[ itemProvider ]];
213     if ([pasteboard respondsToSelector:@selector(setRegistrationInfoLists:)])
214         [pasteboard setRegistrationInfoLists:@[ representationsToRegister ]];
215 }
216
217 #endif
218
219 #if ENABLE(DATA_INTERACTION)
220
221 static void addRepresentationsForPlainText(WebItemProviderRegistrationInfoList *itemsToRegister, const String& plainText)
222 {
223     if (plainText.isEmpty())
224         return;
225
226     NSURL *platformURL = [NSURL URLWithString:plainText];
227     if (URL(platformURL).isValid())
228         [itemsToRegister addRepresentingObject:platformURL];
229
230     [itemsToRegister addData:[(NSString *)plainText dataUsingEncoding:NSUTF8StringEncoding] forType:(NSString *)kUTTypeUTF8PlainText];
231 }
232
233 bool PlatformPasteboard::allowReadingURLAtIndex(const URL& url, int index) const
234 {
235     NSItemProvider *itemProvider = (NSUInteger)index < [m_pasteboard itemProviders].count ? [[m_pasteboard itemProviders] objectAtIndex:index] : nil;
236     for (NSString *type in itemProvider.registeredTypeIdentifiers) {
237         if (UTTypeConformsTo((CFStringRef)type, kUTTypeURL))
238             return true;
239     }
240
241     return url.isValid();
242 }
243
244 #else
245
246 bool PlatformPasteboard::allowReadingURLAtIndex(const URL&, int) const
247 {
248     return true;
249 }
250
251 #endif
252
253 void PlatformPasteboard::write(const PasteboardWebContent& content)
254 {
255 #if __IPHONE_OS_VERSION_MAX_ALLOWED < 110000
256     RetainPtr<NSMutableDictionary> representations = adoptNS([[NSMutableDictionary alloc] init]);
257     [representations addEntriesFromDictionary:richTextRepresentationsForPasteboardWebContent(content).autorelease()];
258
259     NSString *textAsString = content.dataInStringFormat;
260     [representations setValue:[textAsString dataUsingEncoding:NSUTF8StringEncoding] forKey:(NSString *)kUTTypeUTF8PlainText];
261     [representations setValue:[textAsString dataUsingEncoding:NSUTF16StringEncoding] forKey:(NSString *)kUTTypeUTF16PlainText];
262     // FIXME: We vend "public.text" here for backwards compatibility with pre-iOS 11 apps. In the future, we should stop vending this UTI,
263     // and instead set data for concrete plain text types. See <https://bugs.webkit.org/show_bug.cgi?id=173317>.
264     [representations setValue:textAsString forKey:(NSString *)kUTTypeText];
265
266     // Explicitly cast m_pasteboard to UIPasteboard * to work around rdar://problem/33383354.
267     ASSERT([m_pasteboard isKindOfClass:getUIPasteboardClass()]);
268     [(UIPasteboard *)m_pasteboard setItems:@[representations.get()]];
269 #else
270     auto representationsToRegister = adoptNS([[WebItemProviderRegistrationInfoList alloc] init]);
271
272     [representationsToRegister addData:[webIOSPastePboardType dataUsingEncoding:NSUTF8StringEncoding] forType:webIOSPastePboardType];
273
274     ASSERT(content.clientTypes.size() == content.clientData.size());
275     for (size_t i = 0, size = content.clientTypes.size(); i < size; ++i)
276         [representationsToRegister addData:content.clientData[i]->createNSData().get() forType:content.clientTypes[i]];
277
278     if (content.dataInWebArchiveFormat)
279         [representationsToRegister addData:content.dataInWebArchiveFormat->createNSData().get() forType:WebArchivePboardType];
280
281     if (content.dataInAttributedStringFormat) {
282         NSAttributedString *attributedString = [NSKeyedUnarchiver unarchiveObjectWithData:content.dataInAttributedStringFormat->createNSData().get()];
283         if (attributedString)
284             [representationsToRegister addRepresentingObject:attributedString];
285     }
286
287     if (content.dataInRTFDFormat)
288         [representationsToRegister addData:content.dataInRTFDFormat->createNSData().get() forType:(NSString *)kUTTypeFlatRTFD];
289
290     if (content.dataInRTFFormat)
291         [representationsToRegister addData:content.dataInRTFFormat->createNSData().get() forType:(NSString *)kUTTypeRTF];
292
293     if (!content.dataInStringFormat.isEmpty())
294         addRepresentationsForPlainText(representationsToRegister.get(), content.dataInStringFormat);
295
296     registerItemToPasteboard(representationsToRegister.get(), m_pasteboard.get());
297 #endif
298 }
299
300 void PlatformPasteboard::write(const PasteboardImage& pasteboardImage)
301 {
302 #if __IPHONE_OS_VERSION_MAX_ALLOWED < 110000
303     RetainPtr<NSMutableDictionary> representations = adoptNS([[NSMutableDictionary alloc] init]);
304     if (!pasteboardImage.resourceMIMEType.isNull()) {
305         [representations setObject:pasteboardImage.resourceData->createNSData().get() forKey:pasteboardImage.resourceMIMEType];
306         if (!pasteboardImage.url.url.isNull())
307             [representations setObject:(NSURL *)pasteboardImage.url.url forKey:(NSString *)kUTTypeURL];
308     }
309
310     // Explicitly cast m_pasteboard to UIPasteboard * to work around rdar://problem/33383354.
311     ASSERT([m_pasteboard isKindOfClass:getUIPasteboardClass()]);
312     [(UIPasteboard *)m_pasteboard setItems:@[representations.get()]];
313 #else
314     auto representationsToRegister = adoptNS([[WebItemProviderRegistrationInfoList alloc] init]);
315
316     auto& types = pasteboardImage.clientTypes;
317     auto& data = pasteboardImage.clientData;
318     ASSERT(types.size() == data.size());
319     for (size_t i = 0, size = types.size(); i < size; ++i)
320         [representationsToRegister addData:data[i]->createNSData().get() forType:types[i]];
321
322     if (pasteboardImage.resourceData && !pasteboardImage.resourceMIMEType.isEmpty()) {
323         auto utiOrMIMEType = pasteboardImage.resourceMIMEType;
324         if (!isDeclaredUTI(utiOrMIMEType))
325             utiOrMIMEType = UTIFromMIMEType(utiOrMIMEType);
326
327         auto imageData = pasteboardImage.resourceData->createNSData();
328         [representationsToRegister addData:imageData.get() forType:(NSString *)utiOrMIMEType];
329         [representationsToRegister setPreferredPresentationSize:pasteboardImage.imageSize];
330         [representationsToRegister setSuggestedName:pasteboardImage.suggestedName];
331     }
332
333     // FIXME: When writing a PasteboardImage, we currently always place the image data at a higer fidelity than the
334     // associated image URL. However, in the case of an image enclosed by an anchor, we might want to consider the
335     // the URL (i.e. the anchor's href attribute) to be a higher fidelity representation.
336     if (!pasteboardImage.url.url.isEmpty()) {
337         if (NSURL *nsURL = pasteboardImage.url.url)
338             [representationsToRegister addRepresentingObject:nsURL];
339     }
340
341     registerItemToPasteboard(representationsToRegister.get(), m_pasteboard.get());
342 #endif
343 }
344
345 void PlatformPasteboard::write(const String& pasteboardType, const String& text)
346 {
347 #if __IPHONE_OS_VERSION_MAX_ALLOWED < 110000
348     RetainPtr<NSDictionary> representations = adoptNS([[NSMutableDictionary alloc] init]);
349
350     NSString *textAsString = text;
351     if (pasteboardType == String(kUTTypeURL)) {
352         [representations setValue:adoptNS([[NSURL alloc] initWithString:text]).get() forKey:pasteboardType];
353         [representations setValue:textAsString forKey:(NSString *)kUTTypeText];
354     } else if (!pasteboardType.isNull())
355         [representations setValue:textAsString forKey:pasteboardType];
356
357     auto cfPasteboardType = pasteboardType.createCFString();
358     if (UTTypeConformsTo(cfPasteboardType.get(), kUTTypeText) || UTTypeConformsTo(cfPasteboardType.get(), kUTTypeURL)) {
359         [representations setValue:[textAsString dataUsingEncoding:NSUTF8StringEncoding] forKey:(NSString *)kUTTypeUTF8PlainText];
360         [representations setValue:[textAsString dataUsingEncoding:NSUTF16StringEncoding] forKey:(NSString *)kUTTypeUTF16PlainText];
361     }
362
363     // Explicitly cast m_pasteboard to UIPasteboard * to work around rdar://problem/33383354.
364     ASSERT([m_pasteboard isKindOfClass:getUIPasteboardClass()]);
365     [(UIPasteboard *)m_pasteboard setItems:@[representations.get()]];
366 #else
367     auto representationsToRegister = adoptNS([[WebItemProviderRegistrationInfoList alloc] init]);
368
369     NSString *pasteboardTypeAsNSString = pasteboardType;
370     if (!text.isEmpty() && pasteboardTypeAsNSString.length) {
371         auto pasteboardTypeAsCFString = (CFStringRef)pasteboardTypeAsNSString;
372         if (UTTypeConformsTo(pasteboardTypeAsCFString, kUTTypeURL) || UTTypeConformsTo(pasteboardTypeAsCFString, kUTTypeText))
373             addRepresentationsForPlainText(representationsToRegister.get(), text);
374         else
375             [representationsToRegister addData:[pasteboardTypeAsNSString dataUsingEncoding:NSUTF8StringEncoding] forType:pasteboardType];
376     }
377
378     registerItemToPasteboard(representationsToRegister.get(), m_pasteboard.get());
379 #endif
380 }
381
382 void PlatformPasteboard::write(const PasteboardURL& url)
383 {
384 #if __IPHONE_OS_VERSION_MAX_ALLOWED < 110000
385     write(kUTTypeURL, url.url.string());
386 #else
387     auto representationsToRegister = adoptNS([[WebItemProviderRegistrationInfoList alloc] init]);
388
389     if (NSURL *nsURL = url.url) {
390         if (!url.title.isEmpty())
391             nsURL._title = url.title;
392         [representationsToRegister addRepresentingObject:nsURL];
393     }
394
395     registerItemToPasteboard(representationsToRegister.get(), m_pasteboard.get());
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 }