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