[Attachment Support] Implement WKWebView SPI for inserting attachment elements
authorwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 31 Oct 2017 19:29:12 +0000 (19:29 +0000)
committerwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 31 Oct 2017 19:29:12 +0000 (19:29 +0000)
commit51b0feb58565a1694cde9f84f0b417daeab26819
tree76245160b48c95ae26da094cd38d8c55c0b0c2ee
parentfd06c1550d5339b2a4bfdbe1bdff65d3777c2268
[Attachment Support] Implement WKWebView SPI for inserting attachment elements
https://bugs.webkit.org/show_bug.cgi?id=179013
<rdar://problem/35249668>

Reviewed by Tim Horton.

Source/WebCore:

Adds Editor helper methods to insert an attachment element at the current selection.

Test: WKAttachmentTests.AttachmentElementInsertion

* editing/Editor.cpp:
(WebCore::Editor::insertAttachment):
(WebCore::Editor::insertAttachmentFromFile):
* editing/Editor.h:

Add two new helpers for inserting attachment elements, either from a filename or a data blob. This is a wrapper
around Editor::replaceSelectionWithFragment.

* html/HTMLAttachmentElement.cpp:
(WebCore::HTMLAttachmentElement::HTMLAttachmentElement):
(WebCore::HTMLAttachmentElement::create):
* html/HTMLAttachmentElement.h:

Add a unique identifier string to HTMLAttachmentElement. This unique identifier allows the UI process to
reference and indirectly manipulate attachment elements.

Source/WebCore/PAL:

Add a PAL library function to convert a file size to a human-readable string. On Cocoa platforms, this is a
wrapper around NSLocalizedFileSizeDescription; otherwise, we fall back to a naive platform-agnostic
implementation in FileSizeFormatter.cpp.

* PAL.xcodeproj/project.pbxproj:
* pal/CMakeLists.txt:
* pal/FileSizeFormatter.cpp: Added.
(fileSizeDescription):
* pal/FileSizeFormatter.h: Added.
* pal/cocoa/FileSizeFormatterCocoa.mm: Added.
(fileSizeDescription):
* pal/spi/cocoa/NSFileSizeFormatterSPI.h: Added.

Source/WebKit:

Adds native SPI to insert attachment elements given a filename, an optional MIME type, and a data blob. See
change-by-change comments below for more detail. Covered by a new API test.

* PlatformMac.cmake:
* Shared/API/APIObject.h:
* Shared/API/Cocoa/WebKitPrivate.h:
* UIProcess/API/APIAttachment.cpp: Copied from Source/WebKit/Shared/API/Cocoa/WebKitPrivate.h.
(API::Attachment::create):
(API::Attachment::Attachment):
(API::Attachment::~Attachment):

Introduce API::Attachment, which represents a platform-agnostic Attachment.

* UIProcess/API/APIAttachment.h: Copied from Source/WebKit/Shared/API/Cocoa/WebKitPrivate.h.
* UIProcess/API/Cocoa/WKWebView.mm:
(-[WKWebView _insertAttachmentWithFilename:contentType:data:options:completion:]):

Adds support for inserting a new attachment element. Calls out to WebPageProxy to insert the attachment from the
given data blob, and creates a new API-object-backed _WKAttachment.

(-[WKWebView _executeEditCommand:argument:completion:]):

Support passing in a nil completion block.

* UIProcess/API/Cocoa/WKWebViewInternal.h:
* UIProcess/API/Cocoa/WKWebViewPrivate.h:
* UIProcess/API/Cocoa/_WKAttachment.h: Copied from Source/WebKit/Shared/API/Cocoa/WebKitPrivate.h.
* UIProcess/API/Cocoa/_WKAttachment.mm: Copied from Source/WebCore/html/HTMLAttachmentElement.h.

Introduces _WKAttachment, an Objective C wrapper around the API::Attachment.

(-[_WKAttachment _apiObject]):
(-[_WKAttachment isEqual:]):
(-[_WKAttachment uniqueIdentifier]):
(-[_WKAttachment hash]):

Add _WKAttachment, a WebKit SPI object that wraps an attachment element unique identifier. Also, allow
_WKAttachment to be used as a hashable NSDictionary key.

(-[_WKAttachment description]):
* UIProcess/API/Cocoa/_WKAttachmentInternal.h: Copied from Source/WebKit/Shared/API/Cocoa/WebKitPrivate.h.
(API::wrapper):
* UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::insertAttachment):

Implement IPC support for inserting an attachment.

* UIProcess/WebPageProxy.h:
* WebKit.xcodeproj/project.pbxproj:
* WebProcess/WebPage/WebPage.cpp:
(WebKit::WebPage::insertAttachment):
* WebProcess/WebPage/WebPage.h:
* WebProcess/WebPage/WebPage.messages.in:

Tools:

Adds a new API test suite to exercise attachment element insertion and manipulation.

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WebKitCocoa/WKAttachmentTests.mm: Added.
(webViewForTestingAttachments):
(testHTMLData):
(testImageData):
(-[TestWKWebView synchronouslyInsertAttachmentWithFilename:contentType:data:options:]):
(-[TestWKWebView valueOfAttribute:forQuerySelector:]):
(TestWebKitAPI::TEST):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@224238 268f45cc-cd09-0410-ab3c-d52691b4dbfc
34 files changed:
Source/WebCore/ChangeLog
Source/WebCore/PAL/ChangeLog
Source/WebCore/PAL/PAL.xcodeproj/project.pbxproj
Source/WebCore/PAL/pal/CMakeLists.txt
Source/WebCore/PAL/pal/FileSizeFormatter.cpp [new file with mode: 0644]
Source/WebCore/PAL/pal/FileSizeFormatter.h [new file with mode: 0644]
Source/WebCore/PAL/pal/cocoa/FileSizeFormatterCocoa.mm [new file with mode: 0644]
Source/WebCore/PAL/pal/spi/cocoa/NSFileSizeFormatterSPI.h [new file with mode: 0644]
Source/WebCore/editing/Editor.cpp
Source/WebCore/editing/Editor.h
Source/WebCore/html/HTMLAttachmentElement.cpp
Source/WebCore/html/HTMLAttachmentElement.h
Source/WebKit/ChangeLog
Source/WebKit/Platform/cocoa/SharedMemoryCocoa.cpp
Source/WebKit/PlatformMac.cmake
Source/WebKit/Shared/API/APIObject.h
Source/WebKit/Shared/API/Cocoa/WebKitPrivate.h
Source/WebKit/UIProcess/API/APIAttachment.cpp [new file with mode: 0644]
Source/WebKit/UIProcess/API/APIAttachment.h [new file with mode: 0644]
Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm
Source/WebKit/UIProcess/API/Cocoa/WKWebViewInternal.h
Source/WebKit/UIProcess/API/Cocoa/WKWebViewPrivate.h
Source/WebKit/UIProcess/API/Cocoa/_WKAttachment.h [new file with mode: 0644]
Source/WebKit/UIProcess/API/Cocoa/_WKAttachment.mm [new file with mode: 0644]
Source/WebKit/UIProcess/API/Cocoa/_WKAttachmentInternal.h [new file with mode: 0644]
Source/WebKit/UIProcess/WebPageProxy.cpp
Source/WebKit/UIProcess/WebPageProxy.h
Source/WebKit/WebKit.xcodeproj/project.pbxproj
Source/WebKit/WebProcess/WebPage/WebPage.cpp
Source/WebKit/WebProcess/WebPage/WebPage.h
Source/WebKit/WebProcess/WebPage/WebPage.messages.in
Tools/ChangeLog
Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Tools/TestWebKitAPI/Tests/WebKitCocoa/WKAttachmentTests.mm [new file with mode: 0644]