Unable to copy and paste a PDF from Notes into Mail compose body
authorwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 1 Apr 2019 21:21:24 +0000 (21:21 +0000)
committerwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 1 Apr 2019 21:21:24 +0000 (21:21 +0000)
commit5eead58e4c3e5d1dfabbb81860e14414f5019210
tree5f8fc0fbd8f15490ebc5aa2acb305988a8d38df3
parent3f1153832c99e7367c7f1be733d10c77ad65ca2a
Unable to copy and paste a PDF from Notes into Mail compose body
https://bugs.webkit.org/show_bug.cgi?id=196442
<rdar://problem/48573098>

Reviewed by Tim Horton.

Source/WebCore:

Refactor some logic for inserting attachment elements upon paste or drop. Currently, we only prefer inserting
content as attachment elements if the items are annotated with UIPreferredPresentationStyleAttachment. However,
many data sources around the system (both first and third party) have not adopted this API, which makes it
difficult to determine whether a given item provider should be treated as a file or not. In this bug in
particular, no preferred presentation style is set, so we fail to handle the paste command by inserting an
attachment element.

However, most apps around the system that write file or attachment-like data to the pasteboard will at least
offer a suggested name for the file, in the form of -[NSItemProvider suggestedName]. To address this, instead of
relying solely on the preferredPresentationStyle, additionally take a suggested name as an indicator that the
item is probably a file.

In fact, Pasteboard::fileContentState already has similar logic to check for either a suggested file name or
explicitly specified presentation style. We pull this out into a separate helper method on PasteboardItemInfo,
and use it for both Pasteboard::fileContentState and prefersAttachmentRepresentation.

Tests:  WKAttachmentTestsIOS.InsertPastedContactAsAttachment
        WKAttachmentTestsIOS.InsertPastedMapItemAsAttachment

* editing/cocoa/WebContentReaderCocoa.mm:
(WebCore::mimeTypeFromContentType):

Work around <rdar://problem/49478229> by using the "text/vcard" MIME type to handle "public.vcard". CoreServices
currently maps "public.vcard" to "text/directory" when using UTTypeCopyPreferredTagWithClass, despite the SPI
-[NSURLFileTypeMappings MIMETypeForExtension:] returning "text/vcard" for a ".vcf" file.

* platform/PasteboardItemInfo.h:
(WebCore::PasteboardItemInfo::canBeTreatedAsAttachmentOrFile const):

Add a helper method to determine whether the PasteboardItemInfo prefers to be represented as inline data, or an
attachment, or neither. This differs slightly from the existing value of preferredPresentationStyle in that we
consider having a suggested file name as a strong indicator that the item should be treated as an attachment,
even if the presentation style is unspecified.

* platform/cocoa/PasteboardCocoa.mm:
(WebCore::Pasteboard::fileContentState):

Use PasteboardItemInfo::canBeTreatedAsAttachmentOrFile().

* platform/ios/PasteboardIOS.mm:
(WebCore::prefersAttachmentRepresentation):

Use PasteboardItemInfo::canBeTreatedAsAttachmentOrFile().

Source/WebKit:

Relax the -canPerformAction: logic in the case of pasting an attachment. Consider an NSItemProvider to possibly
paste as an attachment if either it has a preferred presentation style of UIPreferredPresentationStyleAttachment
or has a style of UIPreferredPresentationStyleUnspecified, but has a suggested file name.

This allows for the "Paste" action to be shown in the callout menu when copying and pasting a non-text file.

* UIProcess/ios/WKContentViewInteraction.mm:
(-[WKContentView canPerformActionForWebView:withSender:]):

Tools:

Add new API tests to exercise pasting CNContact and MKMapItem-backed item providers. Additionally, adjust an
existing test that pastes a PDF file as an attachment to not require UIPreferredPresentationStyleAttachment
to be specified on the item providers.

* TestWebKitAPI/Tests/WebKitCocoa/WKAttachmentTests.mm:
(TestWebKitAPI::mapItemForTesting):
(TestWebKitAPI::contactItemForTesting):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@243713 268f45cc-cd09-0410-ab3c-d52691b4dbfc
Source/WebCore/ChangeLog
Source/WebCore/editing/cocoa/WebContentReaderCocoa.mm
Source/WebCore/platform/PasteboardItemInfo.h
Source/WebCore/platform/cocoa/PasteboardCocoa.mm
Source/WebCore/platform/ios/PasteboardIOS.mm
Source/WebKit/ChangeLog
Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WebKitCocoa/WKAttachmentTests.mm