| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/base/platform_mime_util.h" | 5 #include "net/base/platform_mime_util.h" |
| 6 | 6 |
| 7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 | 24 |
| 25 @interface NSURLFileTypeMappings : NSObject | 25 @interface NSURLFileTypeMappings : NSObject |
| 26 + (NSURLFileTypeMappings*)sharedMappings; | 26 + (NSURLFileTypeMappings*)sharedMappings; |
| 27 - (NSArray*)extensionsForMIMEType:(NSString*)mimeType; | 27 - (NSArray*)extensionsForMIMEType:(NSString*)mimeType; |
| 28 @end | 28 @end |
| 29 #endif // !defined(OS_IOS) | 29 #endif // !defined(OS_IOS) |
| 30 | 30 |
| 31 namespace net { | 31 namespace net { |
| 32 | 32 |
| 33 bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension( | 33 bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension( |
| 34 const base::FilePath::StringType& ext, std::string* result) const { | 34 const base::FilePath::StringType& ext, |
| 35 std::string* result) const { |
| 35 std::string ext_nodot = ext; | 36 std::string ext_nodot = ext; |
| 36 if (ext_nodot.length() >= 1 && ext_nodot[0] == L'.') | 37 if (ext_nodot.length() >= 1 && ext_nodot[0] == L'.') |
| 37 ext_nodot.erase(ext_nodot.begin()); | 38 ext_nodot.erase(ext_nodot.begin()); |
| 38 base::ScopedCFTypeRef<CFStringRef> ext_ref( | 39 base::ScopedCFTypeRef<CFStringRef> ext_ref( |
| 39 base::SysUTF8ToCFStringRef(ext_nodot)); | 40 base::SysUTF8ToCFStringRef(ext_nodot)); |
| 40 if (!ext_ref) | 41 if (!ext_ref) |
| 41 return false; | 42 return false; |
| 42 base::ScopedCFTypeRef<CFStringRef> uti(UTTypeCreatePreferredIdentifierForTag( | 43 base::ScopedCFTypeRef<CFStringRef> uti(UTTypeCreatePreferredIdentifierForTag( |
| 43 kUTTagClassFilenameExtension, ext_ref, NULL)); | 44 kUTTagClassFilenameExtension, ext_ref, NULL)); |
| 44 if (!uti) | 45 if (!uti) |
| 45 return false; | 46 return false; |
| 46 base::ScopedCFTypeRef<CFStringRef> mime_ref( | 47 base::ScopedCFTypeRef<CFStringRef> mime_ref( |
| 47 UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)); | 48 UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)); |
| 48 if (!mime_ref) | 49 if (!mime_ref) |
| 49 return false; | 50 return false; |
| 50 | 51 |
| 51 *result = base::SysCFStringRefToUTF8(mime_ref); | 52 *result = base::SysCFStringRefToUTF8(mime_ref); |
| 52 return true; | 53 return true; |
| 53 } | 54 } |
| 54 | 55 |
| 55 bool PlatformMimeUtil::GetPreferredExtensionForMimeType( | 56 bool PlatformMimeUtil::GetPreferredExtensionForMimeType( |
| 56 const std::string& mime_type, base::FilePath::StringType* ext) const { | 57 const std::string& mime_type, |
| 58 base::FilePath::StringType* ext) const { |
| 57 base::ScopedCFTypeRef<CFStringRef> mime_ref( | 59 base::ScopedCFTypeRef<CFStringRef> mime_ref( |
| 58 base::SysUTF8ToCFStringRef(mime_type)); | 60 base::SysUTF8ToCFStringRef(mime_type)); |
| 59 if (!mime_ref) | 61 if (!mime_ref) |
| 60 return false; | 62 return false; |
| 61 base::ScopedCFTypeRef<CFStringRef> uti(UTTypeCreatePreferredIdentifierForTag( | 63 base::ScopedCFTypeRef<CFStringRef> uti(UTTypeCreatePreferredIdentifierForTag( |
| 62 kUTTagClassMIMEType, mime_ref, NULL)); | 64 kUTTagClassMIMEType, mime_ref, NULL)); |
| 63 if (!uti) | 65 if (!uti) |
| 64 return false; | 66 return false; |
| 65 base::ScopedCFTypeRef<CFStringRef> ext_ref( | 67 base::ScopedCFTypeRef<CFStringRef> ext_ref( |
| 66 UTTypeCopyPreferredTagWithClass(uti, kUTTagClassFilenameExtension)); | 68 UTTypeCopyPreferredTagWithClass(uti, kUTTagClassFilenameExtension)); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 79 #else | 81 #else |
| 80 // There is no API for this that uses UTIs. The WebKitSystemInterface call | 82 // There is no API for this that uses UTIs. The WebKitSystemInterface call |
| 81 // WKGetExtensionsForMIMEType() is a thin wrapper around | 83 // WKGetExtensionsForMIMEType() is a thin wrapper around |
| 82 // [[NSURLFileTypeMappings sharedMappings] extensionsForMIMEType:], which is | 84 // [[NSURLFileTypeMappings sharedMappings] extensionsForMIMEType:], which is |
| 83 // used by Firefox as well. | 85 // used by Firefox as well. |
| 84 // | 86 // |
| 85 // See: | 87 // See: |
| 86 // http://mxr.mozilla.org/mozilla-central/search?string=extensionsForMIMEType | 88 // http://mxr.mozilla.org/mozilla-central/search?string=extensionsForMIMEType |
| 87 // http://www.openradar.me/11384153 | 89 // http://www.openradar.me/11384153 |
| 88 // rdar://11384153 | 90 // rdar://11384153 |
| 89 NSArray* extensions_list = | 91 NSArray* extensions_list = [[NSURLFileTypeMappings sharedMappings] |
| 90 [[NSURLFileTypeMappings sharedMappings] | 92 extensionsForMIMEType:base::SysUTF8ToNSString(mime_type)]; |
| 91 extensionsForMIMEType:base::SysUTF8ToNSString(mime_type)]; | |
| 92 #endif // defined(OS_IOS) | 93 #endif // defined(OS_IOS) |
| 93 | 94 |
| 94 if (extensions_list) { | 95 if (extensions_list) { |
| 95 for (NSString* extension in extensions_list) | 96 for (NSString* extension in extensions_list) |
| 96 extensions->insert(base::SysNSStringToUTF8(extension)); | 97 extensions->insert(base::SysNSStringToUTF8(extension)); |
| 97 } else { | 98 } else { |
| 98 // Huh? Give up. | 99 // Huh? Give up. |
| 99 base::FilePath::StringType ext; | 100 base::FilePath::StringType ext; |
| 100 if (GetPreferredExtensionForMimeType(mime_type, &ext)) | 101 if (GetPreferredExtensionForMimeType(mime_type, &ext)) |
| 101 extensions->insert(ext); | 102 extensions->insert(ext); |
| 102 } | 103 } |
| 103 } | 104 } |
| 104 | 105 |
| 105 } // namespace net | 106 } // namespace net |
| OLD | NEW |