No DOM API to instantiate an attachment for an img element
authorwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 29 Sep 2018 02:12:57 +0000 (02:12 +0000)
committerwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 29 Sep 2018 02:12:57 +0000 (02:12 +0000)
commit138330a609193454b53279e55702b5d0975030b1
tree65a2d8b424461a6af8217fbed157e0fee836205b
parente7de75fea24ee95536515931281674cda4a155df
No DOM API to instantiate an attachment for an img element
https://bugs.webkit.org/show_bug.cgi?id=189934
<rdar://problem/44743222>

Reviewed by Ryosuke Niwa.

Source/WebCore:

Adds support for HTMLAttachmentElement.getAttachmentIdentifier, a function that internal WebKit clients can use
to ensure that an image element is backed by a unique _WKAttachment. See below for more details.

Tests:  WKAttachmentTests.AddAttachmentToConnectedImageElement
        WKAttachmentTests.ChangeFileWrapperForPastedImage
        WKAttachmentTests.ConnectImageWithAttachmentToDocument

* dom/Document.cpp:
(WebCore::Document::registerAttachmentIdentifier):

Add a new hook to register an empty _WKAttachment in the UI process with a given identifier. Used when creating
a new empty attachment to back an image element.

* dom/Document.h:
* editing/Editor.cpp:
(WebCore::Editor::registerAttachmentIdentifier):
(WebCore::Editor::notifyClientOfAttachmentUpdates):
* editing/Editor.h:
* html/HTMLAttachmentElement.cpp:
(WebCore::HTMLAttachmentElement::getAttachmentIdentifier):

Creates an attachment element to back the image element, if an attachment does not already exist, and returns
the unique identifier. This also causes an empty corresponding _WKAttachment to be created in the client, whose
file wrapper determines the contents of the image.

(WebCore::HTMLAttachmentElement::ensureUniqueIdentifier):
(WebCore::HTMLAttachmentElement::hasEnclosingImage const):
(WebCore::HTMLAttachmentElement::updateEnclosingImageWithData):

Add a helper that updates the source of the enclosing image element given a content type and image data, by
creating a new blob and blob URL.

* html/HTMLAttachmentElement.h:
* html/HTMLAttachmentElement.idl:
* html/HTMLImageElement.idl:

Rename webkitAttachmentIdentifier to just attachmentIdentifier.

* page/EditorClient.h:
(WebCore::EditorClient::registerAttachmentIdentifier):
(WebCore::EditorClient::didInsertAttachmentWithIdentifier):

Source/WebKit:

Makes some adjustments to support using _WKAttachment's file wrapper to change the contents of any image element
hosting the attachment in its shadow root. To do this, we add some plumbing to allow the UI process to update an
attachment element's enclosing image with data from its file wrapper.

* UIProcess/API/APIAttachment.cpp:
(API::Attachment::isEmpty const):
(API::Attachment::enclosingImageData const):

Helper method that creates a SharedBuffer representing image data for the attachment. Only returns a non-null
value for attachment elements that are enclosed within an image.

* UIProcess/API/APIAttachment.h:
* UIProcess/API/Cocoa/APIAttachmentCocoa.mm:
(API::Attachment::enclosingImageData const):
(API::Attachment::isEmpty const):
* UIProcess/API/Cocoa/WKWebView.mm:
(-[WKWebView _attachmentForIdentifier:]):

Add new SPI to request a _WKAttachment for a given unique identifier. Currently, this is only used for testing.

* UIProcess/API/Cocoa/WKWebViewPrivate.h:
* UIProcess/Cocoa/WebPageProxyCocoa.mm:
(WebKit::WebPageProxy::platformRegisterAttachment):
* UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::updateAttachmentAttributes):
(WebKit::WebPageProxy::registerAttachmentIdentifier):
(WebKit::WebPageProxy::didInsertAttachmentWithIdentifier):

Plumb whether or not the attachment is enclosed by an image from the web process to the UI process.

(WebKit::WebPageProxy::didRemoveAttachmentWithIdentifier):
(WebKit::WebPageProxy::didInsertAttachment): Deleted.
(WebKit::WebPageProxy::didRemoveAttachment):
* UIProcess/WebPageProxy.h:
* UIProcess/WebPageProxy.messages.in:
* WebProcess/WebCoreSupport/WebEditorClient.cpp:
(WebKit::WebEditorClient::registerAttachmentIdentifier):
(WebKit::WebEditorClient::didInsertAttachmentWithIdentifier):

Update attachment attributes after inserting an attachment. This ensures that an attachment that was created and
later inserted via script into the document will be synced with state in the UI process, if the client has
changed the contents of the attachment.

* WebProcess/WebCoreSupport/WebEditorClient.h:
* WebProcess/WebPage/WebPage.cpp:
(WebKit::WebPage::updateAttachmentAttributes):

Plumb attachment data from the UI process to the web process.

* WebProcess/WebPage/WebPage.h:
* WebProcess/WebPage/WebPage.messages.in:

Tools:

Adds 3 new API tests in WKAttachmentTests:

`AddAttachmentToConnectedImageElement` verifies that an image element that's already in the document can gain an
attachment element via `HTMLAttachmentElement.getAttachmentIdentifier`.

`ChangeFileWrapperForPastedImage` verifies that an image that has been pasted produces a _WKAttachment in the UI
process, and changing the file wrapper of that _WKAttachment changes the pasted image.

`ConnectImageWithAttachmentToDocument` verifies that script can create an image element, ensure that it has an
attachment, and set a file wrapper for the generated _WKAttachment. Connecting the image to the document should
then result in an image element with the contents of the _WKAttachment's file wrapper.

* TestWebKitAPI/Tests/WebKitCocoa/WKAttachmentTests.mm:
(-[TestWKWebView imageElementSize]):
(-[TestWKWebView waitForImageElementSizeToBecome:]):
(TestWebKitAPI::TEST):
(-[TestWKWebView waitForAttachmentElementSizeToBecome:]): Deleted.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@236634 268f45cc-cd09-0410-ab3c-d52691b4dbfc
27 files changed:
Source/WebCore/ChangeLog
Source/WebCore/dom/Document.cpp
Source/WebCore/dom/Document.h
Source/WebCore/editing/Editor.cpp
Source/WebCore/editing/Editor.h
Source/WebCore/html/HTMLAttachmentElement.cpp
Source/WebCore/html/HTMLAttachmentElement.h
Source/WebCore/html/HTMLAttachmentElement.idl
Source/WebCore/html/HTMLImageElement.idl
Source/WebCore/page/EditorClient.h
Source/WebKit/ChangeLog
Source/WebKit/UIProcess/API/APIAttachment.cpp
Source/WebKit/UIProcess/API/APIAttachment.h
Source/WebKit/UIProcess/API/Cocoa/APIAttachmentCocoa.mm
Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm
Source/WebKit/UIProcess/API/Cocoa/WKWebViewPrivate.h
Source/WebKit/UIProcess/Cocoa/WebPageProxyCocoa.mm
Source/WebKit/UIProcess/WebPageProxy.cpp
Source/WebKit/UIProcess/WebPageProxy.h
Source/WebKit/UIProcess/WebPageProxy.messages.in
Source/WebKit/WebProcess/WebCoreSupport/WebEditorClient.cpp
Source/WebKit/WebProcess/WebCoreSupport/WebEditorClient.h
Source/WebKit/WebProcess/WebPage/WebPage.cpp
Source/WebKit/WebProcess/WebPage/WebPage.h
Source/WebKit/WebProcess/WebPage/WebPage.messages.in
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WebKitCocoa/WKAttachmentTests.mm