ASSERTION FAILED: ASSERT(!containsImage || MIMETypeRegistry::isSupportedImageResourceMIMEType([resource MIMEType])) in -[NSPasteboard(WebExtras) _web_writePromisedRTFDFromArchive:containsImage:]
https://bugs.webkit.org/show_bug.cgi?id=184161
<rdar://problem/39051645>

Reviewed by Dan Bernstein.

.:

* ManualTests/DragInlinePDFImageDocument.html: Added.
* ManualTests/resources/simple.pdf: Added.

Source/WebKitLegacy/mac:

Fixes an assertion failure when quitting an app that uses a Legacy WebKit web view after dragging-and-
dropping a PDF embedded using an HTML image element into the same web view.

When performing a drag-and-drop of a PDF document image (WebCore::PDFDocumentImage) we create a WebArchive
from the main frame's WebHTMLView and promise AppKit that we will provide a Rich Text Format (RTF) document
from this archive if needed. For some reason, on app termination AppKit requests that the WebHTMLView
fulfill its RTF document promise for the WebArchive created at the start of the drag operation. To do this,
we need to extract the image resource from the Web Archive. Currently we query MIMETypeRegistry::isSupportedImageResourceMIMEType()
to see if the contained image is one that we can handle. However MIMETypeRegistry::isSupportedImageResourceMIMEType()
only returns true if WebKit supports creating an image document for the specified MIME type. Disregarding
the iOS motivated setting Settings::useImageDocumentForSubframePDF, PDFs and PostScripts do not create an
image document when navigated to directly. Since we can support dragging PDF document images we need to
query if MIMETypeRegistry::isPDFOrPostScriptMIMEType() in addition to querying MIMETypeRegistry::isSupportedImageResourceMIMEType().
We need to do both such queries before falling back to using the main resource of the Web Archive as
the image. Otherwise, we will cause an assertion failure if the main resource of the Web Archive is
not an image document.

* Misc/WebNSPasteboardExtras.mm:
(-[NSPasteboard _web_writePromisedRTFDFromArchive:containsImage:]):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@231061 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/ChangeLog b/ChangeLog
index cbd07ba..58ba7eb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2018-04-26  Daniel Bates  <dabates@apple.com>
+
+        ASSERTION FAILED: ASSERT(!containsImage || MIMETypeRegistry::isSupportedImageResourceMIMEType([resource MIMEType])) in -[NSPasteboard(WebExtras) _web_writePromisedRTFDFromArchive:containsImage:]
+        https://bugs.webkit.org/show_bug.cgi?id=184161
+        <rdar://problem/39051645>
+
+        Reviewed by Dan Bernstein.
+
+        * ManualTests/DragInlinePDFImageDocument.html: Added.
+        * ManualTests/resources/simple.pdf: Added.
+
 2018-04-26  Andy VanWagoner  <thetalecrafter@gmail.com>
 
         [INTL] Implement Intl.PluralRules
diff --git a/ManualTests/DragInlinePDFImageDocument.html b/ManualTests/DragInlinePDFImageDocument.html
new file mode 100644
index 0000000..99f7eaf
--- /dev/null
+++ b/ManualTests/DragInlinePDFImageDocument.html
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html>
+<body>
+<p>This is a test for <a href="https://bugs.webkit.org/show_bug.cgi?id=184161">Bug 184161</a>. Perform the following using a debug build of Legacy WebKit:</p>
+<ol>
+    <li>Drag and drop the PDF (the content demarcated with a black border on the page below) within the web view.</li>
+    <li>Quit this app.</li>
+</ol>
+<p>This test PASSED if this app does not crash with an assertion failure.</p>
+<img src="resources/simple.pdf" style="border:1px solid black">
+</body>
+</html>
diff --git a/ManualTests/resources/simple.pdf b/ManualTests/resources/simple.pdf
new file mode 100644
index 0000000..8f2eeaf
--- /dev/null
+++ b/ManualTests/resources/simple.pdf
Binary files differ
diff --git a/Source/WebKitLegacy/mac/ChangeLog b/Source/WebKitLegacy/mac/ChangeLog
index 0f5774d..2842ff0 100644
--- a/Source/WebKitLegacy/mac/ChangeLog
+++ b/Source/WebKitLegacy/mac/ChangeLog
@@ -1,3 +1,31 @@
+2018-04-26  Daniel Bates  <dabates@apple.com>
+
+        ASSERTION FAILED: ASSERT(!containsImage || MIMETypeRegistry::isSupportedImageResourceMIMEType([resource MIMEType])) in -[NSPasteboard(WebExtras) _web_writePromisedRTFDFromArchive:containsImage:]
+        https://bugs.webkit.org/show_bug.cgi?id=184161
+        <rdar://problem/39051645>
+
+        Reviewed by Dan Bernstein.
+
+        Fixes an assertion failure when quitting an app that uses a Legacy WebKit web view after dragging-and-
+        dropping a PDF embedded using an HTML image element into the same web view.
+
+        When performing a drag-and-drop of a PDF document image (WebCore::PDFDocumentImage) we create a WebArchive
+        from the main frame's WebHTMLView and promise AppKit that we will provide a Rich Text Format (RTF) document
+        from this archive if needed. For some reason, on app termination AppKit requests that the WebHTMLView
+        fulfill its RTF document promise for the WebArchive created at the start of the drag operation. To do this,
+        we need to extract the image resource from the Web Archive. Currently we query MIMETypeRegistry::isSupportedImageResourceMIMEType()
+        to see if the contained image is one that we can handle. However MIMETypeRegistry::isSupportedImageResourceMIMEType()
+        only returns true if WebKit supports creating an image document for the specified MIME type. Disregarding
+        the iOS motivated setting Settings::useImageDocumentForSubframePDF, PDFs and PostScripts do not create an
+        image document when navigated to directly. Since we can support dragging PDF document images we need to
+        query if MIMETypeRegistry::isPDFOrPostScriptMIMEType() in addition to querying MIMETypeRegistry::isSupportedImageResourceMIMEType().
+        We need to do both such queries before falling back to using the main resource of the Web Archive as
+        the image. Otherwise, we will cause an assertion failure if the main resource of the Web Archive is
+        not an image document.
+
+        * Misc/WebNSPasteboardExtras.mm:
+        (-[NSPasteboard _web_writePromisedRTFDFromArchive:containsImage:]):
+
 2018-04-26  Per Arne Vollan  <pvollan@apple.com>
 
         Disable content filtering in minimal simulator mode
diff --git a/Source/WebKitLegacy/mac/Misc/WebNSPasteboardExtras.mm b/Source/WebKitLegacy/mac/Misc/WebNSPasteboardExtras.mm
index cd12c2a..fa23c6c 100644
--- a/Source/WebKitLegacy/mac/Misc/WebNSPasteboardExtras.mm
+++ b/Source/WebKitLegacy/mac/Misc/WebNSPasteboardExtras.mm
@@ -212,13 +212,16 @@
     // or the main resource (standalone image case).
     NSArray *subresources = [archive subresources];
     WebResource *resource = [archive mainResource];
-    if (containsImage && [subresources count] > 0 
-        && MIMETypeRegistry::isSupportedImageResourceMIMEType([[subresources objectAtIndex:0] MIMEType]))
-        resource = (WebResource *)[subresources objectAtIndex:0];
+    if (containsImage && [subresources count] > 0) {
+        WebResource *subresource = [subresources objectAtIndex:0];
+        NSString *subresourceMIMEType = [subresource MIMEType];
+        if (MIMETypeRegistry::isSupportedImageResourceMIMEType(subresourceMIMEType) || MIMETypeRegistry::isPDFOrPostScriptMIMEType(subresourceMIMEType))
+            resource = subresource;
+    }
     ASSERT(resource != nil);
     
-    ASSERT(!containsImage || MIMETypeRegistry::isSupportedImageResourceMIMEType([resource MIMEType]));
-    if (!containsImage || MIMETypeRegistry::isSupportedImageResourceMIMEType([resource MIMEType]))
+    ASSERT(!containsImage || MIMETypeRegistry::isSupportedImageResourceMIMEType([resource MIMEType]) || MIMETypeRegistry::isPDFOrPostScriptMIMEType([resource MIMEType]));
+    if (!containsImage || MIMETypeRegistry::isSupportedImageResourceMIMEType([resource MIMEType]) || MIMETypeRegistry::isPDFOrPostScriptMIMEType([resource MIMEType]))
         [self _web_writeFileWrapperAsRTFDAttachment:[resource _fileWrapperRepresentation]];
     
 }