This source file includes following definitions.
- GetPlatformMimeTypeFromExtension
- GetPreferredExtensionForMimeType
- GetPlatformExtensionsForMimeType
#include "net/base/platform_mime_util.h"
#include <string>
#include "base/strings/utf_string_conversions.h"
#include "base/win/registry.h"
namespace net {
bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension(
const base::FilePath::StringType& ext, std::string* result) const {
std::wstring value, key = L"." + ext;
base::win::RegKey(HKEY_CLASSES_ROOT, key.c_str(), KEY_READ).ReadValue(
L"Content Type", &value);
if (!value.empty()) {
*result = base::WideToUTF8(value);
return true;
}
return false;
}
bool PlatformMimeUtil::GetPreferredExtensionForMimeType(
const std::string& mime_type, base::FilePath::StringType* ext) const {
std::wstring key(
L"MIME\\Database\\Content Type\\" + base::UTF8ToWide(mime_type));
if (base::win::RegKey(HKEY_CLASSES_ROOT, key.c_str(), KEY_READ).ReadValue(
L"Extension", ext) != ERROR_SUCCESS) {
return false;
}
if (!ext->empty() && ext->at(0) == L'.')
ext->erase(ext->begin());
return true;
}
void PlatformMimeUtil::GetPlatformExtensionsForMimeType(
const std::string& mime_type,
base::hash_set<base::FilePath::StringType>* extensions) const {
base::FilePath::StringType ext;
if (GetPreferredExtensionForMimeType(mime_type, &ext))
extensions->insert(ext);
}
}