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 #import "base/memory/scoped_nsobject.h"
14 #include "base/strings/sys_string_conversions.h"
17 #include <MobileCoreServices/MobileCoreServices.h>
19 #include <CoreServices/CoreServices.h>
20 #endif // defined(OS_IOS)
23 // SPI declaration; see the commentary in GetPlatformExtensionsForMimeType.
24 // iOS must not use any private API, per Apple guideline.
26 @interface NSURLFileTypeMappings : NSObject
27 + (NSURLFileTypeMappings*)sharedMappings;
28 - (NSArray*)extensionsForMIMEType:(NSString*)mimeType;
30 #endif // !defined(OS_IOS)
34 bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension(
35 const base::FilePath::StringType& ext, std::string* result) const {
36 std::string ext_nodot = ext;
37 if (ext_nodot.length() >= 1 && ext_nodot[0] == L'.')
38 ext_nodot.erase(ext_nodot.begin());
39 base::mac::ScopedCFTypeRef<CFStringRef> ext_ref(
40 base::SysUTF8ToCFStringRef(ext_nodot));
43 base::mac::ScopedCFTypeRef<CFStringRef> uti(
44 UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
49 base::mac::ScopedCFTypeRef<CFStringRef> mime_ref(
50 UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType));
54 *result = base::SysCFStringRefToUTF8(mime_ref);
58 bool PlatformMimeUtil::GetPreferredExtensionForMimeType(
59 const std::string& mime_type, base::FilePath::StringType* ext) const {
60 base::mac::ScopedCFTypeRef<CFStringRef> mime_ref(
61 base::SysUTF8ToCFStringRef(mime_type));
64 base::mac::ScopedCFTypeRef<CFStringRef> uti(
65 UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType,
70 base::mac::ScopedCFTypeRef<CFStringRef> ext_ref(
71 UTTypeCopyPreferredTagWithClass(uti, kUTTagClassFilenameExtension));
75 *ext = base::SysCFStringRefToUTF8(ext_ref);
79 void PlatformMimeUtil::GetPlatformExtensionsForMimeType(
80 const std::string& mime_type,
81 base::hash_set<base::FilePath::StringType>* extensions) const {
83 NSArray* extensions_list = nil;
85 // There is no API for this that uses UTIs. The WebKitSystemInterface call
86 // WKGetExtensionsForMIMEType() is a thin wrapper around
87 // [[NSURLFileTypeMappings sharedMappings] extensionsForMIMEType:], which is
88 // used by Firefox as well.
91 // http://mxr.mozilla.org/mozilla-central/search?string=extensionsForMIMEType
92 // http://www.openradar.me/11384153
94 NSArray* extensions_list =
95 [[NSURLFileTypeMappings sharedMappings]
96 extensionsForMIMEType:base::SysUTF8ToNSString(mime_type)];
97 #endif // defined(OS_IOS)
99 if (extensions_list) {
100 for (NSString* extension in extensions_list)
101 extensions->insert(base::SysNSStringToUTF8(extension));
104 base::FilePath::StringType ext;
105 if (GetPreferredExtensionForMimeType(mime_type, &ext))
106 extensions->insert(ext);