[iOS] Respect type fidelities when copying image elements to the pasteboard
authorwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 17 Aug 2017 18:34:15 +0000 (18:34 +0000)
committerwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 17 Aug 2017 18:34:15 +0000 (18:34 +0000)
commit354f2f4602355ef942bf7c497026173f63ae94b2
tree3c6ef4c47ce80c4e226226e4a37df5fdfd46099f
parent8ac570ffbbd449788053a9ef1b638ded912eea1b
[iOS] Respect type fidelities when copying image elements to the pasteboard
https://bugs.webkit.org/show_bug.cgi?id=175638
<rdar://problem/26556043>

Reviewed by Ryosuke Niwa.

Source/WebCore:

Ensures that we respect type fidelities when copying on iOS, by unifying pasteboard writing codepaths across
dragging and copying. When dragging and copying, we now generate and set UIItemProviders on the UIPasteboard.

Tests: ActionSheetTests.CopyImageElementWithHREF
       ActionSheetTests.CopyImageElementWithoutHREF

* platform/PlatformPasteboard.h:

Remove all variants of the writeObjectRepresentations helper method. Now that both paths for writing to the
pasteboard are unified, it's no longer useful to separate out item-provider-based pasteboard logic.

* platform/ios/AbstractPasteboard.h:
* platform/ios/PlatformPasteboardIOS.mm:

Replaces -setItemsUsingRegistrationInfoLists: with -setRegistrationInfoLists:. Rather than have a helper that
both sets up item providers and item registration lists, split this functionality out into two setters. This is
because UIPasteboard does not need to know about the registration info lists used to set up the item providers
in the first place, but internal drag-and-drop clients require this information immediately when starting a drag
in order to construct custom drag previews.

(WebCore::richTextRepresentationsForPasteboardWebContent):
(WebCore::registerItemToPasteboard):

Add a new helper to register item providers to the pasteboard, given an WebItemProviderRegistrationInfoList.

(WebCore::PlatformPasteboard::write):
(WebCore::PlatformPasteboard::writeObjectRepresentations): Deleted.

Refactors PlatformPasteboard::write to always generate and set item providers, unless we're building for an SDK
prior to iOS 11. For images, strings and URLs, we can simply transition to using the new item-provider-based
codepaths used for drag and drop. For web content, we add two additional UTIs that were present when copying,
but not when dragging: flat RTFD and a string constant used to indicate to UIKit that the pasteboard contains
rich text.

* platform/ios/WebItemProviderPasteboard.h:
* platform/ios/WebItemProviderPasteboard.mm:
(-[WebItemProviderRegistrationInfoList init]):
(-[WebItemProviderRegistrationInfoList addData:forType:]):
(-[WebItemProviderRegistrationInfoList addRepresentingObject:]):
(-[WebItemProviderRegistrationInfoList numberOfItems]):
(-[WebItemProviderRegistrationInfoList itemAtIndex:]):
(-[WebItemProviderRegistrationInfoList itemProvider]):
(-[WebItemProviderRegistrationInfoList description]):

Make some small tweaks in WebItemProviderPasteboard (and related classes):
1. Transition to preferredPresentationSize from estimatedDisplayedSize, which is now deprecated.
2. Remove calls to -initWithItemProviderData:typeIdentifier:error:, which is deprecated.
3. Rename _items to _representations to better reflect the role of WebItemProviderRegistrationInfo.
4. Implement -description for WebItemProviderRegistrationInfoList for debugging purposes.

(-[WebItemProviderPasteboard valuesForPasteboardType:inItemSet:]):
(-[WebItemProviderPasteboard setRegistrationInfoLists:]):
(-[WebItemProviderPasteboard setItemsUsingRegistrationInfoLists:]): Deleted.

Source/WebCore/PAL:

Replace -estimatedDisplayedSize with its non-deprecated counterpart, -preferredPresentationSize.

* pal/spi/ios/UIKitSPI.h:

Source/WebKit:

Currently, we treat the case when copying an image enclosed by a link by only writing a URL to the pasteboard.
This patch tweaks logic in WebPage::performActionOnElement to write both an image and a link to the pasteboard
if the hit-tested image element is enclosed by an anchor.

* Platform/spi/ios/UIKitSPI.h:
* WebProcess/WebPage/ios/WebPageIOS.mm:
(WebKit::containingLinkElement):

Tweak this helper to be more restrictive when finding an enclosing 'link' -- only return an element if isLink()
is true, and it is an HTMLAnchorElement. Currently, the isLink() also matches HTMLLinkElements, but this isn't
the intention here.

(WebKit::WebPage::performActionOnElement):

Tools:

Add API tests that simulate copying an image element using the action sheet, and verifying that the pasteboard
contains UTIs in order of fidelity (image type before URL). Also pulls out UIKit SPI (UIApplicationInitialize)
used in UIPasteboardTests and ActionSheetTests into a common UIKitSPI header.

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/ios/ActionSheetTests.mm:
(-[ActionSheetObserver _webView:actionsForElement:defaultActions:]):
(TestWebKitAPI::TEST):
(TestWebKitAPI::presentActionSheetAndChooseAction):
(-[ActionSheetObserver waitForActionSheetAfterBlock:]): Deleted.
* TestWebKitAPI/Tests/ios/DataInteractionTests.mm:
(checkEstimatedSize):
(checkSuggestedNameAndEstimatedSize):

Replace -estimatedDisplayedSize with its non-deprecated counterpart, -preferredPresentationSize.

* TestWebKitAPI/Tests/ios/UIPasteboardTests.mm:
(TestWebKitAPI::TEST):
* TestWebKitAPI/ios/DataInteractionSimulator.h:
* TestWebKitAPI/ios/UIKitSPI.h: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@220865 268f45cc-cd09-0410-ab3c-d52691b4dbfc
18 files changed:
Source/WebCore/ChangeLog
Source/WebCore/PAL/ChangeLog
Source/WebCore/PAL/pal/spi/ios/UIKitSPI.h
Source/WebCore/platform/PlatformPasteboard.h
Source/WebCore/platform/ios/AbstractPasteboard.h
Source/WebCore/platform/ios/PlatformPasteboardIOS.mm
Source/WebCore/platform/ios/WebItemProviderPasteboard.h
Source/WebCore/platform/ios/WebItemProviderPasteboard.mm
Source/WebKit/ChangeLog
Source/WebKit/Platform/spi/ios/UIKitSPI.h
Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm
Tools/ChangeLog
Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Tools/TestWebKitAPI/Tests/ios/ActionSheetTests.mm
Tools/TestWebKitAPI/Tests/ios/DataInteractionTests.mm
Tools/TestWebKitAPI/Tests/ios/UIPasteboardTests.mm
Tools/TestWebKitAPI/ios/DataInteractionSimulator.h
Tools/TestWebKitAPI/ios/UIKitSPI.h [new file with mode: 0644]