⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 263832 in webkit


Ignore:
Timestamp:
Jul 1, 2020, 10:02:18 PM (6 years ago)
Author:
commit-queue@webkit.org
Message:

MIMETypeRegistry::getExtensionsForMIMEType() needs to handle wildcard MIME types
https://bugs.webkit.org/show_bug.cgi?id=213826

Patch by Said Abou-Hallawa <sabouhallawa@apple.com> on 2020-07-01
Reviewed by Darin Adler.

Source/WebCore:

Working towards webkit.org/b/213347, it needs to be possible for WebCore
to get the file extensions for wildcard MIME types, e.g. "image/*" or "video/*".

For Cocoa platforms, we will enumerate the UTIs of the system. Get the
MIMEType and the extensions of each UTI. Add the following pairs to a
singleton HashMap:

{ MIMEType, extension }
{ Type(MIMEType)/*, extension }

Change MIMETypeRegistry::getExtensionsForMIMEType() such that it calls
extensionsForWildcardMIMEType() if the MIMEType ends with "*".

  • platform/MIMETypeRegistry.h:
  • platform/cocoa/MIMETypeRegistryCocoa.mm:

(WebCore::extensionsForMIMETypeMap):
(WebCore::extensionsForWildcardMIMEType):
(WebCore::MIMETypeRegistry::getExtensionsForMIMEType):

  • platform/playstation/MIMETypeRegistryPlayStation.cpp:

(WebCore::MIMETypeRegistry::getExtensionsForMIMEType):

  • platform/win/MIMETypeRegistryWin.cpp:

(WebCore::MIMETypeRegistry::getExtensionsForMIMEType):

  • platform/xdg/MIMETypeRegistryXdg.cpp:

(WebCore::MIMETypeRegistry::getExtensionsForMIMEType):

Source/WebKit:

Replace extensionsForMIMEType() with MIMETypeRegistry::getExtensionsForMIMEType().

  • UIProcess/API/Cocoa/WKOpenPanelParameters.mm:

(-[WKOpenPanelParameters _allowedFileExtensions]):

Location:
trunk/Source
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r263830 r263832  
     12020-07-01  Said Abou-Hallawa  <sabouhallawa@apple.com>
     2
     3        MIMETypeRegistry::getExtensionsForMIMEType() needs to handle wildcard MIME types
     4        https://bugs.webkit.org/show_bug.cgi?id=213826
     5
     6        Reviewed by Darin Adler.
     7
     8        Working towards webkit.org/b/213347, it needs to be possible for WebCore
     9        to get the file extensions for wildcard MIME types, e.g. "image/*" or "video/*".
     10
     11        For Cocoa platforms, we will enumerate the UTIs of the system. Get the
     12        MIMEType and the extensions of each UTI. Add the following pairs to a
     13        singleton HashMap:
     14
     15            { MIMEType, extension }
     16            { Type(MIMEType)/*, extension }
     17
     18        Change MIMETypeRegistry::getExtensionsForMIMEType() such that it calls
     19        extensionsForWildcardMIMEType() if the MIMEType ends with "*".
     20
     21        * platform/MIMETypeRegistry.h:
     22        * platform/cocoa/MIMETypeRegistryCocoa.mm:
     23        (WebCore::extensionsForMIMETypeMap):
     24        (WebCore::extensionsForWildcardMIMEType):
     25        (WebCore::MIMETypeRegistry::getExtensionsForMIMEType):
     26        * platform/playstation/MIMETypeRegistryPlayStation.cpp:
     27        (WebCore::MIMETypeRegistry::getExtensionsForMIMEType):
     28        * platform/win/MIMETypeRegistryWin.cpp:
     29        (WebCore::MIMETypeRegistry::getExtensionsForMIMEType):
     30        * platform/xdg/MIMETypeRegistryXdg.cpp:
     31        (WebCore::MIMETypeRegistry::getExtensionsForMIMEType):
     32
    1332020-07-01  Said Abou-Hallawa  <sabouhallawa@apple.com>
    234
  • trunk/Source/WebCore/platform/MIMETypeRegistry.h

    r261479 r263832  
    5858
    5959    // FIXME: WebKit coding style says we should not have the word "get" in the names of these functions.
    60     static Vector<String> getExtensionsForMIMEType(const String& type);
     60    WEBCORE_EXPORT static Vector<String> getExtensionsForMIMEType(const String& type);
    6161    WEBCORE_EXPORT static String getPreferredExtensionForMIMEType(const String& type);
    6262    WEBCORE_EXPORT static String getMediaMIMETypeForExtension(const String& extension);
  • trunk/Source/WebCore/platform/cocoa/MIMETypeRegistryCocoa.mm

    r259843 r263832  
    2828#import "MIMETypeRegistry.h"
    2929
     30#import <pal/spi/cocoa/CoreServicesSPI.h>
    3031#import <pal/spi/cocoa/NSURLFileTypeMappingsSPI.h>
    3132#import <wtf/cocoa/VectorCocoa.h>
    3233
    3334namespace WebCore {
     35
     36static HashMap<String, HashSet<String>>& extensionsForMIMETypeMap()
     37{
     38    static auto extensionsForMIMETypeMap = makeNeverDestroyed([] {
     39        HashMap<String, HashSet<String>> map;
     40
     41        auto addExtension = [&](const String& type, const String& extension) {
     42            map.add(type, HashSet<String>()).iterator->value.add(extension);
     43        };
     44
     45        auto addExtensions = [&](const String& type, NSArray<NSString *> *extensions) {
     46            size_t pos = type.reverseFind('/');
     47
     48            ASSERT(pos != notFound);
     49            auto wildcardMIMEType = makeString(type.left(pos), "/*"_s);
     50
     51            for (NSString *extension in extensions) {
     52                if (!extension)
     53                    continue;
     54
     55                // Add extension to wildcardMIMEType, for example add "png" to "image/*"
     56                addExtension(wildcardMIMEType, extension);
     57                // Add extension to its mimeType, for example add "png" to "image/png"
     58                addExtension(type, extension);
     59            }
     60        };
     61
     62        auto allUTIs = adoptNS((__bridge NSArray<NSString *> *)_UTCopyDeclaredTypeIdentifiers());
     63
     64        for (NSString *uti in allUTIs.get()) {
     65            auto type = adoptCF(UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)uti, kUTTagClassMIMEType));
     66            if (!type)
     67                continue;
     68            auto extensions = adoptCF(UTTypeCopyAllTagsWithClass((__bridge CFStringRef)uti, kUTTagClassFilenameExtension));
     69            if (!extensions || !CFArrayGetCount(extensions.get()))
     70                continue;
     71            addExtensions(type.get(), (__bridge NSArray<NSString *> *)extensions.get());
     72        }
     73
     74        return map;
     75    }());
     76
     77    return extensionsForMIMETypeMap;
     78}
     79
     80static Vector<String> extensionsForWildcardMIMEType(const String& type)
     81{
     82    Vector<String> extensions;
     83
     84    auto iterator = extensionsForMIMETypeMap().find(type);
     85    if (iterator != extensionsForMIMETypeMap().end())
     86        extensions.appendRange(iterator->value.begin(), iterator->value.end());
     87
     88    return extensions;
     89}
    3490
    3591String MIMETypeRegistry::getMIMETypeForExtension(const String& extension)
     
    4096Vector<String> MIMETypeRegistry::getExtensionsForMIMEType(const String& type)
    4197{
     98    if (type.endsWith('*'))
     99        return extensionsForWildcardMIMEType(type);
    42100    return makeVector<String>([[NSURLFileTypeMappings sharedMappings] extensionsForMIMEType:type]);
    43101}
  • trunk/Source/WebCore/platform/playstation/MIMETypeRegistryPlayStation.cpp

    r255391 r263832  
    8181}
    8282
     83Vector<String> MIMETypeRegistry::getExtensionsForMIMEType(const String&)
     84{
     85    ASSERT_NOT_IMPLEMENTED_YET();
     86    return { };
     87}
     88
    8389} // namespace WebCore
  • trunk/Source/WebCore/platform/win/MIMETypeRegistryWin.cpp

    r242592 r263832  
    115115}
    116116
     117Vector<String> MIMETypeRegistry::getExtensionsForMIMEType(const String&)
     118{
     119    ASSERT_NOT_IMPLEMENTED_YET();
     120    return { };
    117121}
     122
     123}
  • trunk/Source/WebCore/platform/xdg/MIMETypeRegistryXdg.cpp

    r214397 r263832  
    6767}
    6868
     69Vector<String> MIMETypeRegistry::getExtensionsForMIMEType(const String&)
     70{
     71    ASSERT_NOT_IMPLEMENTED_YET();
     72    return { };
    6973}
     74
     75}
  • trunk/Source/WebKit/ChangeLog

    r263831 r263832  
     12020-07-01  Said Abou-Hallawa  <sabouhallawa@apple.com>
     2
     3        MIMETypeRegistry::getExtensionsForMIMEType() needs to handle wildcard MIME types
     4        https://bugs.webkit.org/show_bug.cgi?id=213826
     5
     6        Reviewed by Darin Adler.
     7
     8        Replace extensionsForMIMEType() with MIMETypeRegistry::getExtensionsForMIMEType().
     9
     10        * UIProcess/API/Cocoa/WKOpenPanelParameters.mm:
     11        (-[WKOpenPanelParameters _allowedFileExtensions]):
     12
    1132020-07-01  Lauro Moura  <lmoura@igalia.com>
    214
  • trunk/Source/WebKit/UIProcess/API/Cocoa/WKOpenPanelParameters.mm

    r262895 r263832  
    2626#import "config.h"
    2727#import "WKOpenPanelParametersInternal.h"
    28 #import <pal/spi/cocoa/CoreServicesSPI.h>
     28#import <WebCore/MIMETypeRegistry.h>
    2929
    3030#if PLATFORM(MAC)
    3131
    3232#import "WKNSArray.h"
    33 
    34 static NSDictionary<NSString *, NSSet<NSString *> *> *extensionsForMIMETypeMap()
    35 {
    36     static auto extensionsForMIMETypeMap = makeNeverDestroyed([] {
    37         auto extensionsForMIMETypeMap = adoptNS([[NSMutableDictionary alloc] init]);
    38         auto allUTIs = adoptCF(_UTCopyDeclaredTypeIdentifiers());
    39 
    40         auto addExtensionForMIMEType = ^(NSString *mimeType, NSString *extension) {
    41             if (!extensionsForMIMETypeMap.get()[mimeType])
    42                 extensionsForMIMETypeMap.get()[mimeType] = [NSMutableSet set];
    43             [extensionsForMIMETypeMap.get()[mimeType] addObject:extension];
    44         };
    45 
    46         auto addExtensionsForMIMEType = ^(NSString *mimeType, NSArray<NSString *> *extensions) {
    47             auto wildcardMIMEType = [[mimeType componentsSeparatedByString:@"/"][0] stringByAppendingString:@"/*"];
    48 
    49             for (NSString *extension in extensions) {
    50                 if (!extension)
    51                     continue;
    52                 // Add extension to wildcardMIMEType, for example add "png" to "image/*"
    53                 addExtensionForMIMEType(wildcardMIMEType, extension);
    54                 // Add extension to itsmimeType, for example add "png" to "image/png"
    55                 addExtensionForMIMEType(mimeType, extension);
    56             }
    57         };
    58 
    59         for (CFIndex i = 0, count = CFArrayGetCount(allUTIs.get()); i < count; ++i) {
    60             auto uti = static_cast<CFStringRef>(CFArrayGetValueAtIndex(allUTIs.get(), i));
    61             auto mimeType = adoptCF(UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType));
    62             if (!mimeType)
    63                 continue;
    64             auto extensions = adoptCF(UTTypeCopyAllTagsWithClass(uti, kUTTagClassFilenameExtension));
    65             addExtensionsForMIMEType((__bridge NSString *)mimeType.get(), (__bridge NSArray<NSString *> *)extensions.get());
    66         }
    67 
    68         // Add additional mime types which _UTCopyDeclaredTypeIdentifiers() may not return.
    69         addExtensionForMIMEType(@"image/webp", @"webp");
    70 
    71         return extensionsForMIMETypeMap;
    72     }());
    73 
    74     return extensionsForMIMETypeMap.get().get();
    75 }
    76 
    77 static NSSet<NSString *> *extensionsForMIMEType(NSString *mimetype)
    78 {
    79     return [extensionsForMIMETypeMap() objectForKey:mimetype];
    80 }
    8133
    8234@implementation WKOpenPanelParameters
     
    12375    [acceptedMIMETypes enumerateObjectsUsingBlock:^(NSString *mimeType, NSUInteger index, BOOL* stop) {
    12476        ASSERT([mimeType containsString:@"/"]);
    125         [allowedFileExtensions unionSet:extensionsForMIMEType(mimeType)];
     77        auto extensions = API::Array::createStringArray(WebCore::MIMETypeRegistry::getExtensionsForMIMEType(mimeType));
     78        [allowedFileExtensions addObjectsFromArray:wrapper(extensions)];
    12679    }];
    12780
Note: See TracChangeset for help on using the changeset viewer.