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 <string>
      6 
      7 #include "net/base/platform_mime_util.h"
      8 
      9 #include "base/registry.h"
     10 #include "base/string_util.h"
     11 
     12 namespace net {
     13 
     14 bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension(
     15     const FilePath::StringType& ext, std::string* result) const {
     16   // check windows registry for file extension's mime type (registry key
     17   // names are not case-sensitive).
     18   std::wstring value, key = L"." + ext;
     19   RegKey(HKEY_CLASSES_ROOT, key.c_str()).ReadValue(L"Content Type", &value);
     20   if (!value.empty()) {
     21     *result = WideToUTF8(value);
     22     return true;
     23   }
     24   return false;
     25 }
     26 
     27 bool PlatformMimeUtil::GetPreferredExtensionForMimeType(
     28     const std::string& mime_type, FilePath::StringType* ext) const {
     29   std::wstring key(L"MIME\\Database\\Content Type\\" + UTF8ToWide(mime_type));
     30   if (!RegKey(HKEY_CLASSES_ROOT, key.c_str()).ReadValue(L"Extension", ext))
     31     return false;
     32 
     33   // Strip off the leading dot, this should always be the case.
     34   if (!ext->empty() && ext->at(0) == L'.')
     35     ext->erase(ext->begin());
     36 
     37   return true;
     38 }
     39 
     40 }  // namespace net
     41