/* * Copyright (C) 2013 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #import "config.h" #import "PlatformPasteboard.h" #import "Color.h" #import "URL.h" #import "Image.h" #import "Pasteboard.h" #import "SharedBuffer.h" #import "SoftLinking.h" #import "WebItemProviderPasteboard.h" #import #import #import #if ENABLE(DATA_INTERACTION) #import #import #import #import #endif SOFT_LINK_FRAMEWORK(UIKit) SOFT_LINK_CLASS(UIKit, UIImage) SOFT_LINK_CLASS(UIKit, UIPasteboard) namespace WebCore { PlatformPasteboard::PlatformPasteboard() : m_pasteboard([getUIPasteboardClass() generalPasteboard]) { } #if ENABLE(DATA_INTERACTION) PlatformPasteboard::PlatformPasteboard(const String& name) { if (name == "data interaction pasteboard") m_pasteboard = [WebItemProviderPasteboard sharedInstance]; else m_pasteboard = [getUIPasteboardClass() generalPasteboard]; } #else PlatformPasteboard::PlatformPasteboard(const String&) : m_pasteboard([getUIPasteboardClass() generalPasteboard]) { } #endif void PlatformPasteboard::getTypes(Vector& types) { for (NSString *pasteboardType in [m_pasteboard pasteboardTypes]) types.append(pasteboardType); } void PlatformPasteboard::getTypesByFidelityForItemAtIndex(Vector& types, int index) { if (index >= [m_pasteboard numberOfItems] || ![m_pasteboard respondsToSelector:@selector(pasteboardTypesByFidelityForItemAtIndex:)]) return; NSArray *pasteboardTypesByFidelity = [m_pasteboard pasteboardTypesByFidelityForItemAtIndex:index]; for (NSString *typeIdentifier in pasteboardTypesByFidelity) types.append(typeIdentifier); } RefPtr PlatformPasteboard::bufferForType(const String&) { return nullptr; } void PlatformPasteboard::getPathnamesForType(Vector&, const String&) { } int PlatformPasteboard::numberOfFiles() { return [m_pasteboard respondsToSelector:@selector(numberOfFiles)] ? [m_pasteboard numberOfFiles] : 0; } Vector PlatformPasteboard::filenamesForDataInteraction() { if (![m_pasteboard respondsToSelector:@selector(fileURLsForDataInteraction)]) return { }; Vector filenames; for (NSURL *fileURL in [m_pasteboard fileURLsForDataInteraction]) filenames.append(fileURL.path); return filenames; } String PlatformPasteboard::stringForType(const String& type) { NSArray *values = [m_pasteboard valuesForPasteboardType:type inItemSet:[NSIndexSet indexSetWithIndex:0]]; for (id value in values) { if ([value isKindOfClass:[NSURL class]]) return [(NSURL *)value absoluteString]; if ([value isKindOfClass:[NSString class]]) return (NSString *)value; } return String(); } Color PlatformPasteboard::color() { return Color(); } URL PlatformPasteboard::url() { return URL(); } long PlatformPasteboard::copy(const String&) { return 0; } long PlatformPasteboard::addTypes(const Vector&) { return 0; } long PlatformPasteboard::setTypes(const Vector&) { return 0; } long PlatformPasteboard::setBufferForType(SharedBuffer*, const String&) { return 0; } long PlatformPasteboard::setPathnamesForType(const Vector&, const String&) { return 0; } long PlatformPasteboard::setStringForType(const String&, const String&) { return 0; } long PlatformPasteboard::changeCount() const { return [m_pasteboard changeCount]; } String PlatformPasteboard::uniqueName() { return String(); } static RetainPtr richTextRepresentationsForPasteboardWebContent(const PasteboardWebContent& content) { RetainPtr representations = adoptNS([[NSMutableDictionary alloc] init]); ASSERT(content.clientTypes.size() == content.clientData.size()); for (size_t i = 0, size = content.clientTypes.size(); i < size; ++i) [representations setValue:content.clientData[i]->createNSData().get() forKey:content.clientTypes[i]]; if (content.dataInWebArchiveFormat) { [representations setValue:(NSData *)content.dataInWebArchiveFormat->createNSData().get() forKey:WebArchivePboardType]; // Flag for UIKit to know that this copy contains rich content. This will trigger a two-step paste. NSString *webIOSPastePboardType = @"iOS rich content paste pasteboard type"; [representations setValue:webIOSPastePboardType forKey:webIOSPastePboardType]; } if (content.dataInRTFDFormat) [representations setValue:content.dataInRTFDFormat->createNSData().get() forKey:(NSString *)kUTTypeFlatRTFD]; if (content.dataInRTFFormat) [representations setValue:content.dataInRTFFormat->createNSData().get() forKey:(NSString *)kUTTypeRTF]; return representations; } void PlatformPasteboard::writeObjectRepresentations(const PasteboardWebContent& content) { #if ENABLE(DATA_INTERACTION) RetainPtr itemsToRegister = adoptNS([[WebItemProviderRegistrationInfoList alloc] init]); ASSERT(content.clientTypes.size() == content.clientData.size()); for (size_t i = 0, size = content.clientTypes.size(); i < size; ++i) [itemsToRegister addData:content.clientData[i]->createNSData().get() forType:content.clientTypes[i]]; if (content.dataInWebArchiveFormat) [itemsToRegister addData:content.dataInWebArchiveFormat->createNSData().get() forType:WebArchivePboardType]; if (content.dataInAttributedStringFormat) { NSAttributedString *attributedString = [NSKeyedUnarchiver unarchiveObjectWithData:content.dataInAttributedStringFormat->createNSData().get()]; if (attributedString) [itemsToRegister addRepresentingObject:attributedString]; } if (content.dataInRTFFormat) [itemsToRegister addData:content.dataInRTFFormat->createNSData().get() forType:(NSString *)kUTTypeRTF]; if (!content.dataInStringFormat.isEmpty()) [itemsToRegister addRepresentingObject:(NSString *)content.dataInStringFormat]; [m_pasteboard setItemsUsingRegistrationInfoLists:@[ itemsToRegister.get() ]]; #else UNUSED_PARAM(content); #endif } void PlatformPasteboard::write(const PasteboardWebContent& content) { if ([m_pasteboard respondsToSelector:@selector(setItemsUsingRegistrationInfoLists:)]) { writeObjectRepresentations(content); return; } RetainPtr representations = adoptNS([[NSMutableDictionary alloc] init]); [representations addEntriesFromDictionary:richTextRepresentationsForPasteboardWebContent(content).autorelease()]; [representations setValue:content.dataInStringFormat forKey:(NSString *)kUTTypeText]; [m_pasteboard setItems:@[representations.get()]]; } void PlatformPasteboard::writeObjectRepresentations(const PasteboardImage& pasteboardImage) { #if ENABLE(DATA_INTERACTION) RetainPtr itemsToRegister = adoptNS([[WebItemProviderRegistrationInfoList alloc] init]); if (!pasteboardImage.resourceMIMEType.isNull()) [itemsToRegister addData:pasteboardImage.resourceData->createNSData().get() forType:pasteboardImage.resourceMIMEType]; if (auto nativeImage = pasteboardImage.image->nativeImage()) { UIImage *uiImage = (UIImage *)[getUIImageClass() imageWithCGImage:nativeImage.get()]; if (uiImage) [itemsToRegister addRepresentingObject:uiImage]; } if (!pasteboardImage.url.url.isEmpty()) { NSURL *nsURL = pasteboardImage.url.url; if (nsURL) [itemsToRegister addRepresentingObject:nsURL]; } [m_pasteboard setItemsUsingRegistrationInfoLists:@[ itemsToRegister.get() ]]; #else UNUSED_PARAM(pasteboardImage); #endif } void PlatformPasteboard::write(const PasteboardImage& pasteboardImage) { if ([m_pasteboard respondsToSelector:@selector(setItemsUsingRegistrationInfoLists:)]) { writeObjectRepresentations(pasteboardImage); return; } RetainPtr representations = adoptNS([[NSMutableDictionary alloc] init]); if (!pasteboardImage.resourceMIMEType.isNull()) { [representations setObject:pasteboardImage.resourceData->createNSData().get() forKey:pasteboardImage.resourceMIMEType]; [representations setObject:(NSURL *)pasteboardImage.url.url forKey:(NSString *)kUTTypeURL]; } [m_pasteboard setItems:@[representations.get()]]; } void PlatformPasteboard::writeObjectRepresentations(const String& pasteboardType, const String& text) { #if ENABLE(DATA_INTERACTION) RetainPtr itemsToRegister = adoptNS([[WebItemProviderRegistrationInfoList alloc] init]); NSString *pasteboardTypeAsNSString = pasteboardType; NSString *textAsNSString = text; if (textAsNSString && pasteboardTypeAsNSString.length) { if (UTTypeConformsTo((__bridge CFStringRef)pasteboardTypeAsNSString, kUTTypeURL)) [itemsToRegister addRepresentingObject:[[[NSURL alloc] initWithString:textAsNSString] autorelease]]; if (UTTypeConformsTo((__bridge CFStringRef)pasteboardTypeAsNSString, kUTTypeText)) [itemsToRegister addRepresentingObject:textAsNSString]; } [m_pasteboard setItemsUsingRegistrationInfoLists:@[ itemsToRegister.get() ]]; #else UNUSED_PARAM(pasteboardType); UNUSED_PARAM(text); #endif } void PlatformPasteboard::write(const String& pasteboardType, const String& text) { if ([m_pasteboard respondsToSelector:@selector(setItemsUsingRegistrationInfoLists:)]) { writeObjectRepresentations(pasteboardType, text); return; } RetainPtr representations = adoptNS([[NSMutableDictionary alloc] init]); if (pasteboardType == String(kUTTypeURL)) { [representations setValue:adoptNS([[NSURL alloc] initWithString:text]).get() forKey:pasteboardType]; [representations setValue:text forKey:(NSString *)kUTTypeText]; } else if (!pasteboardType.isNull()) [representations setValue:text forKey:pasteboardType]; [m_pasteboard setItems:@[representations.get()]]; } void PlatformPasteboard::writeObjectRepresentations(const PasteboardURL& url) { #if ENABLE(DATA_INTERACTION) RetainPtr itemsToRegister = adoptNS([[WebItemProviderRegistrationInfoList alloc] init]); if (NSURL *nsURL = url.url) { [itemsToRegister addRepresentingObject:nsURL]; if (!url.title.isEmpty()) nsURL._title = url.title; } [m_pasteboard setItemsUsingRegistrationInfoLists:@[ itemsToRegister.get() ]]; #else UNUSED_PARAM(url); #endif } void PlatformPasteboard::write(const PasteboardURL& url) { if ([m_pasteboard respondsToSelector:@selector(setItemsUsingRegistrationInfoLists:)]) { writeObjectRepresentations(url); return; } write(kUTTypeURL, url.url.string()); } int PlatformPasteboard::count() { return [m_pasteboard numberOfItems]; } RefPtr PlatformPasteboard::readBuffer(int index, const String& type) { NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:index]; RetainPtr pasteboardItem = [m_pasteboard dataForPasteboardType:type inItemSet:indexSet]; if (![pasteboardItem count]) return nullptr; return SharedBuffer::create([pasteboardItem.get() objectAtIndex:0]); } String PlatformPasteboard::readString(int index, const String& type) { NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:index]; RetainPtr pasteboardItem = [m_pasteboard valuesForPasteboardType:type inItemSet:indexSet]; if (![pasteboardItem count]) return String(); id value = [pasteboardItem objectAtIndex:0]; if (type == String(kUTTypeText) || type == String(kUTTypePlainText)) { ASSERT([value isKindOfClass:[NSString class]]); if ([value isKindOfClass:[NSString class]]) return String(value); } else if (type == String(kUTTypeURL)) { ASSERT([value isKindOfClass:[NSURL class]]); if ([value isKindOfClass:[NSURL class]]) return [(NSURL *)value absoluteString]; } return String(); } URL PlatformPasteboard::readURL(int index, const String& type, String& title) { NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:index]; RetainPtr pasteboardItem = [m_pasteboard valuesForPasteboardType:type inItemSet:indexSet]; if (![pasteboardItem count]) return URL(); id value = [pasteboardItem objectAtIndex:0]; ASSERT([value isKindOfClass:[NSURL class]]); if (![value isKindOfClass:[NSURL class]]) return URL(); #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 110000 title = [value _title]; #else UNUSED_PARAM(title); #endif return (NSURL *)value; } void PlatformPasteboard::updatePreferredTypeIdentifiers(const Vector& types) { if (![m_pasteboard respondsToSelector:@selector(updatePreferredTypeIdentifiers:)]) return; NSMutableArray *typesArray = [NSMutableArray arrayWithCapacity:types.size()]; for (auto type : types) [typesArray addObject:(NSString *)type]; [m_pasteboard updatePreferredTypeIdentifiers:typesArray]; } }