MIMETypeRegistry.cpp [plain text]
#include "config.h"
#include "MIMETypeRegistry.h"
#include "MediaPlayer.h"
#include <wtf/HashMap.h>
#include <wtf/HashSet.h>
#include <wtf/StdLibExtras.h>
#include <wtf/text/StringHash.h>
#if USE(CG)
#include "ImageSourceCG.h"
#include <ImageIO/CGImageDestination.h>
#include <wtf/RetainPtr.h>
#endif
#if PLATFORM(QT)
#include <qimagereader.h>
#include <qimagewriter.h>
#endif
#if ENABLE(WEB_ARCHIVE)
#include "ArchiveFactory.h"
#endif
namespace WebCore {
static HashSet<String>* supportedImageResourceMIMETypes;
static HashSet<String>* supportedImageMIMETypes;
static HashSet<String>* supportedImageMIMETypesForEncoding;
static HashSet<String>* supportedJavaScriptMIMETypes;
static HashSet<String>* supportedNonImageMIMETypes;
static HashSet<String>* supportedMediaMIMETypes;
static HashSet<String>* unsupportedTextMIMETypes;
typedef HashMap<String, Vector<String>*, CaseFoldingHash> MediaMIMETypeMap;
static void initializeSupportedImageMIMETypes()
{
#if USE(CG)
RetainPtr<CFArrayRef> supportedTypes(AdoptCF, CGImageSourceCopyTypeIdentifiers());
CFIndex count = CFArrayGetCount(supportedTypes.get());
for (CFIndex i = 0; i < count; i++) {
RetainPtr<CFStringRef> supportedType(AdoptCF, reinterpret_cast<CFStringRef>(CFArrayGetValueAtIndex(supportedTypes.get(), i)));
String mimeType = MIMETypeForImageSourceType(supportedType.get());
if (!mimeType.isEmpty()) {
supportedImageMIMETypes->add(mimeType);
supportedImageResourceMIMETypes->add(mimeType);
}
}
supportedImageMIMETypes->add("image/bmp");
supportedImageResourceMIMETypes->add("image/bmp");
supportedImageMIMETypes->add("image/vnd.microsoft.icon");
supportedImageMIMETypes->add("image/x-icon");
supportedImageResourceMIMETypes->add("image/vnd.microsoft.icon");
supportedImageResourceMIMETypes->add("image/x-icon");
supportedImageMIMETypes->add("image/pjpeg");
supportedImageResourceMIMETypes->add("image/pjpeg");
supportedImageMIMETypes->remove("application/octet-stream");
supportedImageResourceMIMETypes->remove("application/octet-stream");
supportedImageMIMETypes->remove("application/pdf");
supportedImageMIMETypes->remove("application/postscript");
static const char* types[] = {
"image/jpg", "image/jp_", "image/jpe_", "application/jpg", "application/x-jpg", "image/pipeg", "image/vnd.switfview-jpeg", "image/x-xbitmap",
"image/gi_",
"application/png", "application/x-png",
"image/x-tif", "image/tif", "image/x-tiff", "application/tif", "application/x-tif", "application/tiff", "application/x-tiff",
"image/x-bmp", "image/x-win-bitmap", "image/x-windows-bmp", "image/ms-bmp", "image/x-ms-bmp", "application/bmp", "application/x-bmp", "application/x-win-bitmap",
};
for (size_t i = 0; i < sizeof(types) / sizeof(types[0]); ++i) {
supportedImageMIMETypes->add(types[i]);
supportedImageResourceMIMETypes->add(types[i]);
}
#elif PLATFORM(QT)
QList<QByteArray> formats = QImageReader::supportedImageFormats();
for (size_t i = 0; i < static_cast<size_t>(formats.size()); ++i) {
#if ENABLE(SVG)
if (formats.at(i).toLower().startsWith("svg"))
continue;
#endif
String mimeType = MIMETypeRegistry::getMIMETypeForExtension(formats.at(i).constData());
if (!mimeType.isEmpty()) {
supportedImageMIMETypes->add(mimeType);
supportedImageResourceMIMETypes->add(mimeType);
}
}
#else
static const char* types[] = {
"image/jpeg",
"image/png",
"image/gif",
"image/bmp",
"image/vnd.microsoft.icon", "image/x-icon", "image/x-xbitmap" };
for (size_t i = 0; i < WTF_ARRAY_LENGTH(types); ++i) {
supportedImageMIMETypes->add(types[i]);
supportedImageResourceMIMETypes->add(types[i]);
}
#endif
}
static void initializeSupportedImageMIMETypesForEncoding()
{
supportedImageMIMETypesForEncoding = new HashSet<String>;
#if USE(CG)
RetainPtr<CFArrayRef> supportedTypes(AdoptCF, CGImageDestinationCopyTypeIdentifiers());
CFIndex count = CFArrayGetCount(supportedTypes.get());
for (CFIndex i = 0; i < count; i++) {
RetainPtr<CFStringRef> supportedType(AdoptCF, reinterpret_cast<CFStringRef>(CFArrayGetValueAtIndex(supportedTypes.get(), i)));
String mimeType = MIMETypeForImageSourceType(supportedType.get());
if (!mimeType.isEmpty())
supportedImageMIMETypesForEncoding->add(mimeType);
}
#elif PLATFORM(QT)
QList<QByteArray> formats = QImageWriter::supportedImageFormats();
for (int i = 0; i < formats.size(); ++i) {
String mimeType = MIMETypeRegistry::getMIMETypeForExtension(formats.at(i).constData());
if (!mimeType.isEmpty())
supportedImageMIMETypesForEncoding->add(mimeType);
}
#elif PLATFORM(GTK)
supportedImageMIMETypesForEncoding->add("image/png");
supportedImageMIMETypesForEncoding->add("image/jpeg");
supportedImageMIMETypesForEncoding->add("image/tiff");
supportedImageMIMETypesForEncoding->add("image/bmp");
supportedImageMIMETypesForEncoding->add("image/ico");
#elif USE(CAIRO)
supportedImageMIMETypesForEncoding->add("image/png");
#endif
}
static void initializeSupportedJavaScriptMIMETypes()
{
static const char* types[] = {
"text/javascript",
"text/ecmascript",
"application/javascript",
"application/ecmascript",
"application/x-javascript",
"text/javascript1.1",
"text/javascript1.2",
"text/javascript1.3",
"text/jscript",
"text/livescript",
};
for (size_t i = 0; i < WTF_ARRAY_LENGTH(types); ++i)
supportedJavaScriptMIMETypes->add(types[i]);
}
static void initializeSupportedNonImageMimeTypes()
{
static const char* types[] = {
"text/html",
"text/xml",
"text/xsl",
"text/plain",
"text/",
"application/xml",
"application/xhtml+xml",
"application/json",
#if ENABLE(SVG)
"image/svg+xml",
#endif
#if ENABLE(FTPDIR)
"application/x-ftp-directory",
#endif
"multipart/x-mixed-replace"
};
COMPILE_ASSERT(sizeof(types) / sizeof(types[0]) <= 16,
nonimage_mime_types_must_be_less_than_or_equal_to_16);
for (size_t i = 0; i < WTF_ARRAY_LENGTH(types); ++i)
supportedNonImageMIMETypes->add(types[i]);
#if ENABLE(WEB_ARCHIVE)
ArchiveFactory::registerKnownArchiveMIMETypes();
#endif
}
static MediaMIMETypeMap& mediaMIMETypeMap()
{
struct TypeExtensionPair {
const char* type;
const char* extension;
};
static const TypeExtensionPair pairs[] = {
{ "application/ogg", "ogx" },
{ "audio/ogg", "ogg" },
{ "audio/ogg", "oga" },
{ "video/ogg", "ogv" },
{ "application/annodex", "anx" },
{ "audio/annodex", "axa" },
{ "video/annodex", "axv" },
{ "audio/speex", "spx" },
{ "video/webm", "webm" },
{ "audio/webm", "webm" },
{ "audio/mpeg", "m1a" },
{ "audio/mpeg", "m2a" },
{ "audio/mpeg", "m1s" },
{ "audio/mpeg", "mpa" },
{ "video/mpeg", "mpg" },
{ "video/mpeg", "m15" },
{ "video/mpeg", "m1s" },
{ "video/mpeg", "m1v" },
{ "video/mpeg", "m75" },
{ "video/mpeg", "mpa" },
{ "video/mpeg", "mpeg" },
{ "video/mpeg", "mpm" },
{ "video/mpeg", "mpv" },
{ "application/vnd.apple.mpegurl", "m3u8" },
{ "application/mpegurl", "m3u8" },
{ "application/x-mpegurl", "m3u8" },
{ "audio/mpegurl", "m3url" },
{ "audio/x-mpegurl", "m3url" },
{ "audio/mpegurl", "m3u" },
{ "audio/x-mpegurl", "m3u" },
{ "video/x-m4v", "m4v" },
{ "audio/x-m4a", "m4a" },
{ "audio/x-m4b", "m4b" },
{ "audio/x-m4p", "m4p" },
{ "audio/mp4", "m4a" },
{ "audio/mp3", "mp3" },
{ "audio/x-mp3", "mp3" },
{ "audio/x-mpeg", "mp3" },
{ "video/x-mpeg2", "mp2" },
{ "video/mpeg2", "vob" },
{ "video/mpeg2", "mod" },
{ "video/m2ts", "m2ts" },
{ "video/x-m2ts", "m2t" },
{ "video/x-m2ts", "ts" },
{ "audio/3gpp", "3gpp" },
{ "audio/3gpp2", "3g2" },
{ "application/x-mpeg", "amc" },
{ "audio/aac", "aac" },
{ "audio/aac", "adts" },
{ "audio/x-aac", "m4r" },
{ "audio/x-caf", "caf" },
{ "audio/x-gsm", "gsm" },
{ "audio/x-wav", "wav" }
};
DEFINE_STATIC_LOCAL(MediaMIMETypeMap, mediaMIMETypeForExtensionMap, ());
if (!mediaMIMETypeForExtensionMap.isEmpty())
return mediaMIMETypeForExtensionMap;
const unsigned numPairs = sizeof(pairs) / sizeof(pairs[0]);
for (unsigned ndx = 0; ndx < numPairs; ++ndx) {
if (mediaMIMETypeForExtensionMap.contains(pairs[ndx].extension))
mediaMIMETypeForExtensionMap.get(pairs[ndx].extension)->append(pairs[ndx].type);
else {
Vector<String>* synonyms = new Vector<String>;
String systemType = MIMETypeRegistry::getMIMETypeForExtension(pairs[ndx].extension);
if (!systemType.isEmpty() && pairs[ndx].type != systemType)
synonyms->append(systemType);
synonyms->append(pairs[ndx].type);
mediaMIMETypeForExtensionMap.add(pairs[ndx].extension, synonyms);
}
}
return mediaMIMETypeForExtensionMap;
}
#if ENABLE(FILE_SYSTEM) && ENABLE(WORKERS)
String MIMETypeRegistry::getMIMETypeForExtension(const String& extension)
{
return getMIMETypeForExtensionThreadSafe(extension);
}
#endif
String MIMETypeRegistry::getMediaMIMETypeForExtension(const String& ext)
{
String type = getMIMETypeForExtension(ext);
if (!type.isEmpty())
return type;
Vector<String>* typeList = mediaMIMETypeMap().get(ext);
if (typeList)
return (*typeList)[0];
return String();
}
Vector<String> MIMETypeRegistry::getMediaMIMETypesForExtension(const String& ext)
{
Vector<String>* typeList = mediaMIMETypeMap().get(ext);
if (typeList)
return *typeList;
String type = getMIMETypeForExtension(ext);
if (!type.isEmpty()) {
Vector<String> typeList;
typeList.append(type);
return typeList;
}
return Vector<String>();
}
static void initializeSupportedMediaMIMETypes()
{
supportedMediaMIMETypes = new HashSet<String>;
#if ENABLE(VIDEO)
MediaPlayer::getSupportedTypes(*supportedMediaMIMETypes);
#endif
}
static void initializeUnsupportedTextMIMETypes()
{
static const char* types[] = {
"text/calendar",
"text/x-calendar",
"text/x-vcalendar",
"text/vcalendar",
"text/vcard",
"text/x-vcard",
"text/directory",
"text/ldif",
"text/qif",
"text/x-qif",
"text/x-csv",
"text/x-vcf",
"text/vnd.sun.j2me.app-descriptor",
};
for (size_t i = 0; i < WTF_ARRAY_LENGTH(types); ++i)
unsupportedTextMIMETypes->add(types[i]);
}
static void initializeMIMETypeRegistry()
{
supportedJavaScriptMIMETypes = new HashSet<String>;
initializeSupportedJavaScriptMIMETypes();
supportedNonImageMIMETypes = new HashSet<String>(*supportedJavaScriptMIMETypes);
initializeSupportedNonImageMimeTypes();
supportedImageResourceMIMETypes = new HashSet<String>;
supportedImageMIMETypes = new HashSet<String>;
initializeSupportedImageMIMETypes();
unsupportedTextMIMETypes = new HashSet<String>;
initializeUnsupportedTextMIMETypes();
}
String MIMETypeRegistry::getMIMETypeForPath(const String& path)
{
size_t pos = path.reverseFind('.');
if (pos != notFound) {
String extension = path.substring(pos + 1);
String result = getMIMETypeForExtension(extension);
if (result.length())
return result;
}
return "application/octet-stream";
}
bool MIMETypeRegistry::isSupportedImageMIMEType(const String& mimeType)
{
if (mimeType.isEmpty())
return false;
if (!supportedImageMIMETypes)
initializeMIMETypeRegistry();
return supportedImageMIMETypes->contains(mimeType);
}
bool MIMETypeRegistry::isSupportedImageResourceMIMEType(const String& mimeType)
{
if (mimeType.isEmpty())
return false;
if (!supportedImageResourceMIMETypes)
initializeMIMETypeRegistry();
return supportedImageResourceMIMETypes->contains(mimeType);
}
bool MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(const String& mimeType)
{
ASSERT(isMainThread());
if (mimeType.isEmpty())
return false;
if (!supportedImageMIMETypesForEncoding)
initializeSupportedImageMIMETypesForEncoding();
return supportedImageMIMETypesForEncoding->contains(mimeType);
}
bool MIMETypeRegistry::isSupportedJavaScriptMIMEType(const String& mimeType)
{
if (mimeType.isEmpty())
return false;
if (!supportedJavaScriptMIMETypes)
initializeMIMETypeRegistry();
return supportedJavaScriptMIMETypes->contains(mimeType);
}
bool MIMETypeRegistry::isSupportedNonImageMIMEType(const String& mimeType)
{
if (mimeType.isEmpty())
return false;
if (!supportedNonImageMIMETypes)
initializeMIMETypeRegistry();
return supportedNonImageMIMETypes->contains(mimeType);
}
bool MIMETypeRegistry::isSupportedMediaMIMEType(const String& mimeType)
{
if (mimeType.isEmpty())
return false;
if (!supportedMediaMIMETypes)
initializeSupportedMediaMIMETypes();
return supportedMediaMIMETypes->contains(mimeType);
}
bool MIMETypeRegistry::isUnsupportedTextMIMEType(const String& mimeType)
{
if (mimeType.isEmpty())
return false;
if (!unsupportedTextMIMETypes)
initializeMIMETypeRegistry();
return unsupportedTextMIMETypes->contains(mimeType);
}
bool MIMETypeRegistry::isJavaAppletMIMEType(const String& mimeType)
{
return mimeType.startsWith("application/x-java-applet", false)
|| mimeType.startsWith("application/x-java-bean", false)
|| mimeType.startsWith("application/x-java-vm", false);
}
HashSet<String>& MIMETypeRegistry::getSupportedImageMIMETypes()
{
if (!supportedImageMIMETypes)
initializeMIMETypeRegistry();
return *supportedImageMIMETypes;
}
HashSet<String>& MIMETypeRegistry::getSupportedImageResourceMIMETypes()
{
if (!supportedImageResourceMIMETypes)
initializeMIMETypeRegistry();
return *supportedImageResourceMIMETypes;
}
HashSet<String>& MIMETypeRegistry::getSupportedImageMIMETypesForEncoding()
{
if (!supportedImageMIMETypesForEncoding)
initializeSupportedImageMIMETypesForEncoding();
return *supportedImageMIMETypesForEncoding;
}
HashSet<String>& MIMETypeRegistry::getSupportedNonImageMIMETypes()
{
if (!supportedNonImageMIMETypes)
initializeMIMETypeRegistry();
return *supportedNonImageMIMETypes;
}
HashSet<String>& MIMETypeRegistry::getSupportedMediaMIMETypes()
{
if (!supportedMediaMIMETypes)
initializeSupportedMediaMIMETypes();
return *supportedMediaMIMETypes;
}
HashSet<String>& MIMETypeRegistry::getUnsupportedTextMIMETypes()
{
if (!unsupportedTextMIMETypes)
initializeMIMETypeRegistry();
return *unsupportedTextMIMETypes;
}
const String& defaultMIMEType()
{
DEFINE_STATIC_LOCAL(const String, defaultMIMEType, ("application/octet-stream"));
return defaultMIMEType;
}
}