Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2006-2008 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 #include <CoreServices/CoreServices.h>
      8 
      9 #include <string>
     10 
     11 #include "base/mac/scoped_cftyperef.h"
     12 #include "base/sys_string_conversions.h"
     13 
     14 namespace net {
     15 
     16 bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension(
     17     const FilePath::StringType& ext, std::string* result) const {
     18   std::string ext_nodot = ext;
     19   if (ext_nodot.length() >= 1 && ext_nodot[0] == L'.')
     20     ext_nodot.erase(ext_nodot.begin());
     21   base::mac::ScopedCFTypeRef<CFStringRef> ext_ref(
     22       base::SysUTF8ToCFStringRef(ext_nodot));
     23   if (!ext_ref)
     24     return false;
     25   base::mac::ScopedCFTypeRef<CFStringRef> uti(
     26       UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
     27                                             ext_ref,
     28                                             NULL));
     29   if (!uti)
     30     return false;
     31   base::mac::ScopedCFTypeRef<CFStringRef> mime_ref(
     32       UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType));
     33   if (!mime_ref)
     34     return false;
     35 
     36   *result = base::SysCFStringRefToUTF8(mime_ref);
     37   return true;
     38 }
     39 
     40 bool PlatformMimeUtil::GetPreferredExtensionForMimeType(
     41     const std::string& mime_type, FilePath::StringType* ext) const {
     42   base::mac::ScopedCFTypeRef<CFStringRef> mime_ref(
     43       base::SysUTF8ToCFStringRef(mime_type));
     44   if (!mime_ref)
     45     return false;
     46   base::mac::ScopedCFTypeRef<CFStringRef> uti(
     47       UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType,
     48                                             mime_ref,
     49                                             NULL));
     50   if (!uti)
     51     return false;
     52   base::mac::ScopedCFTypeRef<CFStringRef> ext_ref(
     53       UTTypeCopyPreferredTagWithClass(uti, kUTTagClassFilenameExtension));
     54   if (!ext_ref)
     55     return false;
     56 
     57   *ext = base::SysCFStringRefToUTF8(ext_ref);
     58   return true;
     59 }
     60 
     61 }  // namespace net
     62