OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "net/base/platform_mime_util.h" | |
6 | |
7 #import <Foundation/Foundation.h> | |
8 | |
9 #include <string> | |
10 | |
11 #include "base/mac/foundation_util.h" | |
12 #include "base/mac/scoped_cftyperef.h" | |
13 #include "base/strings/sys_string_conversions.h" | |
14 | |
15 #if defined(OS_IOS) | |
16 #include <MobileCoreServices/MobileCoreServices.h> | |
17 #else | |
18 #include <CoreServices/CoreServices.h> | |
19 #endif // defined(OS_IOS) | |
20 | |
21 #if !defined(OS_IOS) | |
22 // SPI declaration; see the commentary in GetPlatformExtensionsForMimeType. | |
23 // iOS must not use any private API, per Apple guideline. | |
24 | |
25 @interface NSURLFileTypeMappings : NSObject | |
26 + (NSURLFileTypeMappings*)sharedMappings; | |
27 - (NSArray*)extensionsForMIMEType:(NSString*)mimeType; | |
28 @end | |
29 #endif // !defined(OS_IOS) | |
30 | |
31 namespace net { | |
32 | |
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)); | |
40 if (!ext_ref) | |
41 return false; | |
42 base::ScopedCFTypeRef<CFStringRef> uti(UTTypeCreatePreferredIdentifierForTag( | |
43 kUTTagClassFilenameExtension, ext_ref, NULL)); | |
44 if (!uti) | |
45 return false; | |
46 base::ScopedCFTypeRef<CFStringRef> mime_ref( | |
47 UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)); | |
48 if (!mime_ref) | |
49 return false; | |
50 | |
51 *result = base::SysCFStringRefToUTF8(mime_ref); | |
52 return true; | |
53 } | |
54 | |
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)); | |
59 if (!mime_ref) | |
60 return false; | |
61 base::ScopedCFTypeRef<CFStringRef> uti(UTTypeCreatePreferredIdentifierForTag( | |
62 kUTTagClassMIMEType, mime_ref, NULL)); | |
63 if (!uti) | |
64 return false; | |
65 base::ScopedCFTypeRef<CFStringRef> ext_ref( | |
66 UTTypeCopyPreferredTagWithClass(uti, kUTTagClassFilenameExtension)); | |
67 if (!ext_ref) | |
68 return false; | |
69 | |
70 *ext = base::SysCFStringRefToUTF8(ext_ref); | |
71 return true; | |
72 } | |
73 | |
74 void PlatformMimeUtil::GetPlatformExtensionsForMimeType( | |
75 const std::string& mime_type, | |
76 base::hash_set<base::FilePath::StringType>* extensions) const { | |
77 #if defined(OS_IOS) | |
78 NSArray* extensions_list = nil; | |
79 #else | |
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. | |
84 // | |
85 // See: | |
86 // http://mxr.mozilla.org/mozilla-central/search?string=extensionsForMIMEType | |
87 // http://www.openradar.me/11384153 | |
88 // rdar://11384153 | |
89 NSArray* extensions_list = | |
90 [[NSURLFileTypeMappings sharedMappings] | |
91 extensionsForMIMEType:base::SysUTF8ToNSString(mime_type)]; | |
92 #endif // defined(OS_IOS) | |
93 | |
94 if (extensions_list) { | |
95 for (NSString* extension in extensions_list) | |
96 extensions->insert(base::SysNSStringToUTF8(extension)); | |
97 } else { | |
98 // Huh? Give up. | |
99 base::FilePath::StringType ext; | |
100 if (GetPreferredExtensionForMimeType(mime_type, &ext)) | |
101 extensions->insert(ext); | |
102 } | |
103 } | |
104 | |
105 } // namespace net | |
OLD | NEW |