MIMETypeRegistry.cpp [plain text]
#include "config.h"
#include "MIMETypeRegistry.h"
#include "StringHash.h"
#include <wtf/HashMap.h>
#include <wtf/HashSet.h>
#if PLATFORM(CG)
#include <ApplicationServices/ApplicationServices.h>
#endif
#if PLATFORM(MAC)
#include "WebCoreSystemInterface.h"
#endif
#if PLATFORM(QT)
#include <qimagereader.h>
#endif
namespace WebCore
{
static WTF::HashSet<String>* supportedImageResourceMIMETypes;
static WTF::HashSet<String>* supportedImageMIMETypes;
static WTF::HashSet<String>* supportedNonImageMIMETypes;
#if PLATFORM(CG)
extern String getMIMETypeForUTI(const String& uti);
#endif
static void initialiseSupportedImageMIMETypes()
{
#if PLATFORM(CG)
CFArrayRef supportedTypes = CGImageSourceCopyTypeIdentifiers();
int cnt = CFArrayGetCount(supportedTypes);
for(int i = 0; i < cnt; i++) {
CFStringRef supportedType = (CFStringRef)CFArrayGetValueAtIndex(supportedTypes, i);
String mimeType=getMIMETypeForUTI(supportedType);
if (!mimeType.isEmpty()) {
supportedImageMIMETypes->add(mimeType);
supportedImageResourceMIMETypes->add(mimeType);
}
CFRelease(supportedType);
}
CFRelease(supportedTypes);
supportedImageMIMETypes->add("image/bmp");
supportedImageResourceMIMETypes->add("image/bmp");
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");
#elif PLATFORM(QT)
QList<QByteArray> formats = QImageReader::supportedImageFormats();
for (size_t i = 0; i < formats.size(); ++i) {
#if ENABLE(SVG)
if (formats.at(i).toLower().startsWith("svg"))
continue;
#endif
String mimeType = MIMETypeRegistry::getMIMETypeForExtension(formats.at(i).constData());
supportedImageMIMETypes->add(mimeType);
supportedImageResourceMIMETypes->add(mimeType);
}
#else
static const char* types[] = {
"image/jpeg",
"image/png",
"image/gif",
"image/bmp",
"image/x-icon", "image/x-xbitmap" };
for (size_t i = 0; i < sizeof(types)/sizeof(types[0]); ++i) {
supportedImageMIMETypes->add(types[i]);
supportedImageResourceMIMETypes->add(types[i]);
}
#endif
}
static void initialiseSupportedNonImageMimeTypes()
{
static const char* types[] = {
"text/html",
"text/xml",
"text/xsl",
"text/plain",
"text/",
"application/x-javascript",
"application/xml",
"application/xhtml+xml",
"application/rss+xml",
"application/atom+xml",
#if PLATFORM(MAC)
"application/x-webarchive",
#endif
"multipart/x-mixed-replace"
#if ENABLE(SVG)
, "image/svg+xml"
#endif
#if ENABLE(FTPDIR)
, "application/x-ftp-directory"
#endif
};
for (size_t i = 0; i < sizeof(types)/sizeof(types[0]); ++i)
supportedNonImageMIMETypes->add(types[i]);
}
static void initialiseMIMETypeRegistry()
{
supportedImageResourceMIMETypes = new WTF::HashSet<String>();
supportedImageMIMETypes = new WTF::HashSet<String>();
supportedNonImageMIMETypes = new WTF::HashSet<String>();
initialiseSupportedNonImageMimeTypes();
initialiseSupportedImageMIMETypes();
}
String MIMETypeRegistry::getMIMETypeForPath(const String& path)
{
int pos = path.reverseFind('.');
if(pos >= 0) {
String extension = path.substring(pos + 1);
return getMIMETypeForExtension(extension);
}
return "application/octet-stream";
}
bool MIMETypeRegistry::isSupportedImageMIMEType(const String& mimeType)
{
if (!supportedImageMIMETypes)
initialiseMIMETypeRegistry();
return !mimeType.isEmpty() && supportedImageMIMETypes->contains(mimeType);
}
bool MIMETypeRegistry::isSupportedImageResourceMIMEType(const String& mimeType)
{
if (!supportedImageResourceMIMETypes)
initialiseMIMETypeRegistry();
return !mimeType.isEmpty() && supportedImageResourceMIMETypes->contains(mimeType);
}
bool MIMETypeRegistry::isSupportedNonImageMIMEType(const String& mimeType)
{
if (!supportedNonImageMIMETypes)
initialiseMIMETypeRegistry();
return !mimeType.isEmpty() && supportedNonImageMIMETypes->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)
initialiseMIMETypeRegistry();
return *supportedImageMIMETypes;
}
HashSet<String> &MIMETypeRegistry::getSupportedImageResourceMIMETypes()
{
if (!supportedImageResourceMIMETypes)
initialiseMIMETypeRegistry();
return *supportedImageResourceMIMETypes;
}
HashSet<String> &MIMETypeRegistry::getSupportedNonImageMIMETypes()
{
if (!supportedNonImageMIMETypes)
initialiseMIMETypeRegistry();
return *supportedNonImageMIMETypes;
}
}