1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/base/platform_mime_util.h"
7 #import <Foundation/Foundation.h>
11 #include "base/mac/foundation_util.h"
12 #include "base/mac/scoped_cftyperef.h"
13 #include "base/strings/sys_string_conversions.h"
16 #include <MobileCoreServices/MobileCoreServices.h>
18 #include <CoreServices/CoreServices.h>
19 #endif // defined(OS_IOS)
22 // SPI declaration; see the commentary in GetPlatformExtensionsForMimeType.
23 // iOS must not use any private API, per Apple guideline.
25 @interface NSURLFileTypeMappings : NSObject
26 + (NSURLFileTypeMappings*)sharedMappings;
27 - (NSArray*)extensionsForMIMEType:(NSString*)mimeType;
29 #endif // !defined(OS_IOS)
33 bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension(
34 const base::FilePath::StringType& ext, std::string* result) const {
35 std::string ext_nodot = ext;
36 if (ext_nodot.length() >= 1 && ext_nodot[0] == L'.')
37 ext_nodot.erase(ext_nodot.begin());
38 base::ScopedCFTypeRef<CFStringRef> ext_ref(
39 base::SysUTF8ToCFStringRef(ext_nodot));
42 base::ScopedCFTypeRef<CFStringRef> uti(UTTypeCreatePreferredIdentifierForTag(
43 kUTTagClassFilenameExtension, ext_ref, NULL));
46 base::ScopedCFTypeRef<CFStringRef> mime_ref(
47 UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType));
51 *result = base::SysCFStringRefToUTF8(mime_ref);
55 bool PlatformMimeUtil::GetPreferredExtensionForMimeType(
56 const std::string& mime_type, base::FilePath::StringType* ext) const {
57 base::ScopedCFTypeRef<CFStringRef> mime_ref(
58 base::SysUTF8ToCFStringRef(mime_type));
61 base::ScopedCFTypeRef<CFStringRef> uti(UTTypeCreatePreferredIdentifierForTag(
62 kUTTagClassMIMEType, mime_ref, NULL));
65 base::ScopedCFTypeRef<CFStringRef> ext_ref(
66 UTTypeCopyPreferredTagWithClass(uti, kUTTagClassFilenameExtension));
70 *ext = base::SysCFStringRefToUTF8(ext_ref);
74 void PlatformMimeUtil::GetPlatformExtensionsForMimeType(
75 const std::string& mime_type,
76 base::hash_set<base::FilePath::StringType>* extensions) const {
78 NSArray* extensions_list = nil;
80 // There is no API for this that uses UTIs. The WebKitSystemInterface call
81 // WKGetExtensionsForMIMEType() is a thin wrapper around
82 // [[NSURLFileTypeMappings sharedMappings] extensionsForMIMEType:], which is
83 // used by Firefox as well.
86 // http://mxr.mozilla.org/mozilla-central/search?string=extensionsForMIMEType
87 // http://www.openradar.me/11384153
89 NSArray* extensions_list =
90 [[NSURLFileTypeMappings sharedMappings]
91 extensionsForMIMEType:base::SysUTF8ToNSString(mime_type)];
92 #endif // defined(OS_IOS)
94 if (extensions_list) {
95 for (NSString* extension in extensions_list)
96 extensions->insert(base::SysNSStringToUTF8(extension));
99 base::FilePath::StringType ext;
100 if (GetPreferredExtensionForMimeType(mime_type, &ext))
101 extensions->insert(ext);