Add signalSyncPoint to the WebGraphicsContext3D command buffer impls.
[chromium-blink-merge.git] / net / base / platform_mime_util_mac.mm
blob99315db98825e126160d6c2935524a460e556304
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>
9 #include <string>
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"
16 #if defined(OS_IOS)
17 #include <MobileCoreServices/MobileCoreServices.h>
18 #else
19 #include <CoreServices/CoreServices.h>
20 #endif  // defined(OS_IOS)
22 #if !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;
29 @end
30 #endif  // !defined(OS_IOS)
32 namespace net {
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));
41   if (!ext_ref)
42     return false;
43   base::mac::ScopedCFTypeRef<CFStringRef> uti(
44       UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
45                                             ext_ref,
46                                             NULL));
47   if (!uti)
48     return false;
49   base::mac::ScopedCFTypeRef<CFStringRef> mime_ref(
50       UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType));
51   if (!mime_ref)
52     return false;
54   *result = base::SysCFStringRefToUTF8(mime_ref);
55   return true;
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));
62   if (!mime_ref)
63     return false;
64   base::mac::ScopedCFTypeRef<CFStringRef> uti(
65       UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType,
66                                             mime_ref,
67                                             NULL));
68   if (!uti)
69     return false;
70   base::mac::ScopedCFTypeRef<CFStringRef> ext_ref(
71       UTTypeCopyPreferredTagWithClass(uti, kUTTagClassFilenameExtension));
72   if (!ext_ref)
73     return false;
75   *ext = base::SysCFStringRefToUTF8(ext_ref);
76   return true;
79 void PlatformMimeUtil::GetPlatformExtensionsForMimeType(
80     const std::string& mime_type,
81     base::hash_set<base::FilePath::StringType>* extensions) const {
82 #if defined(OS_IOS)
83   NSArray* extensions_list = nil;
84 #else
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.
89   //
90   // See:
91   // http://mxr.mozilla.org/mozilla-central/search?string=extensionsForMIMEType
92   // http://www.openradar.me/11384153
93   // rdar://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));
102   } else {
103     // Huh? Give up.
104     base::FilePath::StringType ext;
105     if (GetPreferredExtensionForMimeType(mime_type, &ext))
106       extensions->insert(ext);
107   }
110 }  // namespace net