Extract UTI mapping and allow for additions
authordino@apple.com <dino@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 11 Apr 2019 23:13:44 +0000 (23:13 +0000)
committerdino@apple.com <dino@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 11 Apr 2019 23:13:44 +0000 (23:13 +0000)
https://bugs.webkit.org/show_bug.cgi?id=196822
<rdar://problem/49822339>

Reviewed by Tim Horton.

Source/WebCore:

Add an ADDITIONAL_SYSTEM_PREVIEW_TYPES macro to the list
of accepted MIME types. And add a new helper to map
MIME types to UTI types when the system was unable to
find an existing type.

* platform/MIMETypeRegistry.cpp:
(WebCore::MIMETypeRegistry::systemPreviewMIMETypes):
* platform/network/mac/UTIUtilities.mm:
(WebCore::UTIFromUnknownMIMEType):
(WebCore::UTIFromMIMETypeCachePolicy::createValueForKey):

Source/WebKit:

Use the new helper to map MIME type to UTI.

* UIProcess/Cocoa/SystemPreviewControllerCocoa.mm:
(-[_WKPreviewControllerDataSource previewController:previewItemAtIndex:]):
* UIProcess/ios/WKSystemPreviewView.mm:
(getUTIForSystemPreviewMIMEType):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@244201 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/platform/MIMETypeRegistry.cpp
Source/WebCore/platform/network/mac/UTIUtilities.mm
Source/WebKit/ChangeLog
Source/WebKit/UIProcess/Cocoa/SystemPreviewControllerCocoa.mm
Source/WebKit/UIProcess/ios/WKSystemPreviewView.mm

index f67d0d2..d6d0d90 100644 (file)
@@ -1,3 +1,22 @@
+2019-04-11  Dean Jackson  <dino@apple.com>
+
+        Extract UTI mapping and allow for additions
+        https://bugs.webkit.org/show_bug.cgi?id=196822
+        <rdar://problem/49822339>
+
+        Reviewed by Tim Horton.
+
+        Add an ADDITIONAL_SYSTEM_PREVIEW_TYPES macro to the list
+        of accepted MIME types. And add a new helper to map
+        MIME types to UTI types when the system was unable to
+        find an existing type.
+
+        * platform/MIMETypeRegistry.cpp:
+        (WebCore::MIMETypeRegistry::systemPreviewMIMETypes):
+        * platform/network/mac/UTIUtilities.mm:
+        (WebCore::UTIFromUnknownMIMEType):
+        (WebCore::UTIFromMIMETypeCachePolicy::createValueForKey):
+
 2019-04-09  Ryosuke Niwa  <rniwa@webkit.org>
 
         [iOS] Moving backwards by word granularity does not work if the previous line was inside another block element
index 6cae14a..a04feac 100644 (file)
 #include "PreviewConverter.h"
 #endif
 
+#if USE(APPLE_INTERNAL_SDK)
+#include <WebKitAdditions/AdditionalSystemPreviewTypes.h>
+#else
+#define ADDITIONAL_SYSTEM_PREVIEW_TYPES
+#endif
+
 namespace WebCore {
 
 const HashSet<String, ASCIICaseInsensitiveHash>& MIMETypeRegistry::supportedImageMIMETypes()
@@ -671,7 +677,8 @@ const HashSet<String, ASCIICaseInsensitiveHash>& MIMETypeRegistry::systemPreview
         "model/vnd.usdz+zip",
         // Unofficial, but supported because we documented them.
         "model/usd",
-        "model/vnd.pixar.usd"
+        "model/vnd.pixar.usd",
+        ADDITIONAL_SYSTEM_PREVIEW_TYPES
     };
     return systemPreviewMIMETypes;
 }
index 2e361ce..59f7391 100644 (file)
 #import <MobileCoreServices/MobileCoreServices.h>
 #endif
 
+#if USE(APPLE_INTERNAL_SDK)
+#import <WebKitAdditions/AdditionalUTIMappings.h>
+#else
+#define ADDITIONAL_UTI_MAPPINGS
+#endif
+
 namespace WebCore {
 
 String MIMETypeFromUTI(const String& uti)
@@ -78,11 +84,42 @@ String MIMETypeFromUTITree(const String& uti)
     return String();
 }
 
+static String UTIFromUnknownMIMEType(const String& mimeType)
+{
+    static const auto map = makeNeverDestroyed([] {
+        struct TypeExtensionPair {
+            ASCIILiteral type;
+            ASCIILiteral uti;
+        };
+
+        static const TypeExtensionPair pairs[] = {
+            { "model/vnd.usdz+zip"_s, "com.pixar.universal-scene-description-mobile"_s },
+            { "model/usd"_s, "com.pixar.universal-scene-description-mobile"_s },
+            { "model/vnd.pixar.usd"_s, "com.pixar.universal-scene-description-mobile"_s },
+            ADDITIONAL_UTI_MAPPINGS
+        };
+
+        HashMap<String, String, ASCIICaseInsensitiveHash> map;
+        for (auto& pair : pairs)
+            map.add(pair.type, pair.uti);
+        return map;
+    }());
+
+    auto mapEntry = map.get().find(mimeType);
+    if (mapEntry == map.get().end())
+        return emptyString();
+
+    return mapEntry->value;
+}
+
 struct UTIFromMIMETypeCachePolicy : TinyLRUCachePolicy<String, String> {
 public:
     static String createValueForKey(const String& key)
     {
-        return String(adoptCF(UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, key.createCFString().get(), 0)).get());
+        auto type = adoptCF(UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, key.createCFString().get(), 0));
+        if (type)
+            return String(type.get());
+        return UTIFromUnknownMIMEType(key);
     }
 };
 
index 157d3a0..2474177 100644 (file)
@@ -1,3 +1,19 @@
+2019-04-11  Dean Jackson  <dino@apple.com>
+
+        Extract UTI mapping and allow for additions
+        https://bugs.webkit.org/show_bug.cgi?id=196822
+        <rdar://problem/49822339>
+
+        Reviewed by Tim Horton.
+
+        Use the new helper to map MIME type to UTI.
+
+        * UIProcess/Cocoa/SystemPreviewControllerCocoa.mm:
+        (-[_WKPreviewControllerDataSource previewController:previewItemAtIndex:]):
+        * UIProcess/ios/WKSystemPreviewView.mm:
+        (getUTIForSystemPreviewMIMEType):
+
+
 2019-04-11  Said Abou-Hallawa  <sabouhallawa@apple.com>
 
         REGRESSION (r244182): RemoteLayerTreeDrawingArea::flushLayers() should not be reentrant
index 7498ba4..58834e9 100644 (file)
@@ -34,6 +34,7 @@
 #import <QuickLook/QuickLook.h>
 #import <UIKit/UIViewController.h>
 #import <WebCore/MIMETypeRegistry.h>
+#import <WebCore/UTIUtilities.h>
 #import <pal/ios/QuickLookSoftLink.h>
 #import <pal/spi/ios/QuickLookSPI.h>
 #import <wtf/WeakObjCPtr.h>
@@ -81,9 +82,7 @@
     _itemProvider = adoptNS([[NSItemProvider alloc] init]);
     // FIXME: We are launching the preview controller before getting a response from the resource, which
     // means we don't actually know the real MIME type yet.
-    // FIXME: At the moment we only have one supported UTI, but if we start supporting more types,
-    // then we'll need a table.
-    static NSString *contentType = (__bridge NSString *) UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, CFSTR("usdz"), nil);
+    NSString *contentType = WebCore::UTIFromMIMEType("model/vnd.usdz+zip"_s);
 
     _item = adoptNS([PAL::allocQLItemInstance() initWithPreviewItemProvider:_itemProvider.get() contentType:contentType previewTitle:@"Preview" fileSize:@(0)]);
     [_item setUseLoadingTimeout:NO];
index 7ad2ad5..ba2e6da 100644 (file)
@@ -36,6 +36,7 @@
 #import <WebCore/FloatRect.h>
 #import <WebCore/LocalizedStrings.h>
 #import <WebCore/MIMETypeRegistry.h>
+#import <WebCore/UTIUtilities.h>
 #import <pal/ios/QuickLookSoftLink.h>
 #import <pal/spi/cg/CoreGraphicsSPI.h>
 #import <pal/spi/ios/SystemPreviewSPI.h>
 SOFT_LINK_PRIVATE_FRAMEWORK(AssetViewer);
 SOFT_LINK_CLASS(AssetViewer, ASVThumbnailView);
 
-// FIXME: At the moment we only have one supported UTI, but
-// if we start supporting more types, then we'll need a table.
 static NSString *getUTIForSystemPreviewMIMEType(const String& mimeType)
 {
-    static NSString *uti = (__bridge NSString *) UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, CFSTR("usdz"), nil);
-
     if (!WebCore::MIMETypeRegistry::isSystemPreviewMIMEType(mimeType))
         return nil;
 
-    return uti;
+    return WebCore::UTIFromMIMEType(mimeType);
 }
 
 @interface WKSystemPreviewView () <ASVThumbnailViewDelegate>