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