[Attachment Support] Support dragging attachment elements out as files on macOS
authorwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 22 Aug 2018 21:35:02 +0000 (21:35 +0000)
committerwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 22 Aug 2018 21:35:02 +0000 (21:35 +0000)
commite3861ad2632de6fcd52a0ecffe44f44fcc9dce80
tree4f8a4e2d454fe0344440408f2547520edac178fc
parentf1cfc505c2403756639cd48b319a5aa3fb5c5c09
[Attachment Support] Support dragging attachment elements out as files on macOS
https://bugs.webkit.org/show_bug.cgi?id=181294
<rdar://problem/36298801>

Reviewed by Tim Horton.

Source/WebCore:

Serialize a dragged attachment element as a web archive on macOS. This allows us to move attachment elements
around a document by dragging, without performing a file upload upon every drop. Ideally, we should do this on
iOS as well, but this currently causes attachment data to go missing; further investigation to fix this for iOS
is tracked in <https://bugs.webkit.org/show_bug.cgi?id=181514>.

Tests:  WKAttachmentTestsMac.DragAttachmentAsFilePromise
        WKAttachmentTests.MoveAttachmentElementAsIconByDragging

* editing/cocoa/EditorCocoa.mm:
(WebCore::Editor::getPasteboardTypesAndDataForAttachment):

Source/WebKit:

Add support for dragging attachment elements on macOS by writing promised files to drag pasteboard. See changes
below for more details.

* UIProcess/API/Cocoa/WKWebView.mm:
(-[WKWebView filePromiseProvider:fileNameForType:]):
(-[WKWebView filePromiseProvider:writePromiseToURL:completionHandler:]):
(-[WKWebView draggingSession:sourceOperationMaskForDraggingContext:]):
(-[WKWebView draggingSession:endedAtPoint:operation:]):
* UIProcess/API/mac/WKView.mm:
(-[WKView filePromiseProvider:fileNameForType:]):
(-[WKView filePromiseProvider:writePromiseToURL:completionHandler:]):
(-[WKView draggingSession:sourceOperationMaskForDraggingContext:]):
(-[WKView draggingSession:endedAtPoint:operation:]):

Plumb NSFilePromiseProviderDelegate and NSDraggingSource method implementations to WebViewImpl.

* UIProcess/Cocoa/WebViewImpl.h:
* UIProcess/Cocoa/WebViewImpl.mm:
(-[WKPromisedAttachmentContext initWithAttachmentInfo:]):
(-[WKPromisedAttachmentContext blobURL]):
(-[WKPromisedAttachmentContext filename]):
(-[WKPromisedAttachmentContext attachmentIdentifier]):

Add an object that contains the information needed to deliver a dragged attachment element's data via
NSFilePromiseProvider. This is stored as the userInfo of the NSFilePromiseProvider created upon drag start.

(WebKit::WebViewImpl::draggedImage):
(WebKit::WebViewImpl::sendDragEndToPage):

Add a helper method to handle cleanup after the dragging has finished, and call it from -draggedImage:… and
-draggingSessionEnded:…. The latter is only triggered in the where -beginDraggingSessionWithItems:… is used,
which currently only happens when dragging attachment elements.

(WebKit::WebViewImpl::fileNameForFilePromiseProvider):
(WebKit::webKitUnknownError):
(WebKit::WebViewImpl::writeToURLForFilePromiseProvider):

Deliver either NSFileWrapper data to the destination URL (in the case where an attachment identifier is known
and the corresponding API::Attachment is backed by a file wrapper), or save the contents of the blob URL to the
destination.

(WebKit::WebViewImpl::dragSourceOperationMask):
(WebKit::WebViewImpl::draggingSessionEnded):
(WebKit::WebViewImpl::startDrag):

Tools:

Add DragAndDropSimulator support for intercepting calls to -beginDraggingSessionWithitems:event:source:. This
enables us to write API tests for macOS that exercise the attachment SPI in combination with dragging attachment
elements.

* TestWebKitAPI/Tests/WebKitCocoa/WKAttachmentTests.mm:
(-[TestWKWebView attachmentElementMidPoint]):

Add a helper method local to this test suite that grabs the midpoint (in client coordinates) or the first
attachment element in the document.

(TestWebKitAPI::TEST):

Add a new API test to verify that dragging an attachment element on macOS produces file providers which may be
used to write attachment data to a path on disk. Additionally, refactor an existing API test,
MoveAttachmentElementAsIconByDragging, so that it runs on both iOS and macOS, to test the ability to move
attachment elements around in a document by using drag and drop.

* TestWebKitAPI/cocoa/DragAndDropSimulator.h:
* TestWebKitAPI/mac/DragAndDropSimulatorMac.mm:
(-[DragAndDropTestWKWebView beginDraggingSessionWithItems:event:source:]):
(-[DragAndDropSimulator initWithWebViewFrame:configuration:]):
(-[DragAndDropSimulator dealloc]):
(-[DragAndDropSimulator runFrom:to:]):
(-[DragAndDropSimulator beginDraggingSessionInWebView:withItems:source:]):

Begin a drag session and kick off the -continueDragSession loop. Unlike -performDragInWebView:…, which spins
the main runloop until dragging ends, this version returns execution to the web view and schedules dragging
updates asynchronously. This matches AppKit behavior.

(-[DragAndDropSimulator continueDragSession]):

Increment the dragging progress amount, send a drag update to the web view, and continue scheduling calls to
itself until the progress reaches 1.

(-[DragAndDropSimulator performDragInWebView:atLocation:withImage:pasteboard:source:]):
(-[DragAndDropSimulator initializeDraggingInfo:dragImage:source:]):

Pull out common logic for creating a new TestDraggingInfo after starting a drag.

(-[DragAndDropSimulator insertedAttachments]):
(-[DragAndDropSimulator removedAttachments]):
(-[DragAndDropSimulator draggingSession]):
(-[DragAndDropSimulator receivePromisedFiles]):

Helper method to save promised files written to the pasteboard after a drag to the temporary directory. These
files are tracked by DragAndDropSimulator and automatically cleaned up after the test finishes.

(-[DragAndDropSimulator endDataTransfer]):

Add a method stub on macOS, so that MoveAttachmentElementAsIconByDragging can be made cross-platform.

(-[DragAndDropSimulator _webView:didInsertAttachment:withSource:]):
(-[DragAndDropSimulator _webView:didRemoveAttachment:]):

Implement method stubs to keep track of inserted or removed attachments while simulating a drag.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@235202 268f45cc-cd09-0410-ab3c-d52691b4dbfc
Source/WebCore/ChangeLog
Source/WebCore/editing/cocoa/EditorCocoa.mm
Source/WebKit/ChangeLog
Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm
Source/WebKit/UIProcess/API/mac/WKView.mm
Source/WebKit/UIProcess/Cocoa/WebViewImpl.h
Source/WebKit/UIProcess/Cocoa/WebViewImpl.mm
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WebKitCocoa/WKAttachmentTests.mm
Tools/TestWebKitAPI/cocoa/DragAndDropSimulator.h
Tools/TestWebKitAPI/mac/DragAndDropSimulatorMac.mm