This source file includes following definitions.
- GetPlatformMimeTypeFromExtension
- GetPlatformMimeTypeFromExtension
- GetPreferredExtensionForMimeType
- GetPlatformExtensionsForMimeType
#include "net/base/platform_mime_util.h"
#include <string>
#include "base/logging.h"
#include "build/build_config.h"
#if defined(OS_ANDROID)
#include "net/android/network_library.h"
#else
#include "base/nix/mime_util_xdg.h"
#endif
namespace net {
#if defined(OS_ANDROID)
bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension(
const base::FilePath::StringType& ext, std::string* result) const {
return android::GetMimeTypeFromExtension(ext, result);
}
#else
bool PlatformMimeUtil::GetPlatformMimeTypeFromExtension(
const base::FilePath::StringType& ext, std::string* result) const {
if (ext == "pl")
return false;
base::FilePath dummy_path("foo." + ext);
std::string out = base::nix::GetFileMimeType(dummy_path);
if (out == "application/octet-stream" || out.empty())
return false;
if (out == "image/x-ico")
out = "image/x-icon";
*result = out;
return true;
}
#endif
struct MimeToExt {
const char* mime_type;
const char* ext;
};
const struct MimeToExt mime_type_ext_map[] = {
{"application/pdf", "pdf"},
{"application/x-tar", "tar"},
{"application/zip", "zip"},
{"audio/mpeg", "mp3"},
{"image/gif", "gif"},
{"image/jpeg", "jpg"},
{"image/png", "png"},
{"text/html", "html"},
{"video/mp4", "mp4"},
{"video/mpeg", "mpg"},
{"text/plain", "txt"},
{"text/x-sh", "sh"},
};
bool PlatformMimeUtil::GetPreferredExtensionForMimeType(
const std::string& mime_type, base::FilePath::StringType* ext) const {
for (size_t x = 0;
x < (sizeof(mime_type_ext_map) / sizeof(MimeToExt));
x++) {
if (mime_type_ext_map[x].mime_type == mime_type) {
*ext = mime_type_ext_map[x].ext;
return true;
}
}
return false;
}
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);
}
}