KHTML
MIMETypeRegistry.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "config.h"
00027 #include "MIMETypeRegistry.h"
00028
00029 #include "ArchiveFactory.h"
00030 #include "MediaPlayer.h"
00031 #include "StringHash.h"
00032 #include <wtf/HashMap.h>
00033 #include <wtf/HashSet.h>
00034
00035 #if PLATFORM(CG)
00036 #include <ApplicationServices/ApplicationServices.h>
00037 #include <wtf/RetainPtr.h>
00038 #endif
00039 #if PLATFORM(QT)
00040 #include <qimagereader.h>
00041 #include <qimagewriter.h>
00042 #endif
00043
00044 namespace WebCore {
00045
00046 static HashSet<String>* supportedImageResourceMIMETypes;
00047 static HashSet<String>* supportedImageMIMETypes;
00048 static HashSet<String>* supportedImageMIMETypesForEncoding;
00049 static HashSet<String>* supportedJavaScriptMIMETypes;
00050 static HashSet<String>* supportedNonImageMIMETypes;
00051 static HashSet<String>* supportedMediaMIMETypes;
00052
00053 #if PLATFORM(CG)
00054 extern String getMIMETypeForUTI(const String& uti);
00055 #endif
00056
00057 static void initializeSupportedImageMIMETypes()
00058 {
00059 #if PLATFORM(CG)
00060 RetainPtr<CFArrayRef> supportedTypes(AdoptCF, CGImageSourceCopyTypeIdentifiers());
00061 CFIndex count = CFArrayGetCount(supportedTypes.get());
00062 for (CFIndex i = 0; i < count; i++) {
00063 RetainPtr<CFStringRef> supportedType(AdoptCF, reinterpret_cast<CFStringRef>(CFArrayGetValueAtIndex(supportedTypes.get(), i)));
00064 String mimeType = getMIMETypeForUTI(supportedType.get());
00065 if (!mimeType.isEmpty()) {
00066 supportedImageMIMETypes->add(mimeType);
00067 supportedImageResourceMIMETypes->add(mimeType);
00068 }
00069 }
00070
00071
00072 supportedImageMIMETypes->add("image/bmp");
00073 supportedImageResourceMIMETypes->add("image/bmp");
00074
00075
00076 supportedImageMIMETypes->add("image/x-icon");
00077 supportedImageResourceMIMETypes->add("image/x-icon");
00078
00079
00080 supportedImageMIMETypes->add("image/pjpeg");
00081 supportedImageResourceMIMETypes->add("image/pjpeg");
00082
00083
00084 supportedImageMIMETypes->remove("application/octet-stream");
00085 supportedImageResourceMIMETypes->remove("application/octet-stream");
00086
00087
00088 supportedImageMIMETypes->remove("application/pdf");
00089 supportedImageMIMETypes->remove("application/postscript");
00090
00091 #elif PLATFORM(QT)
00092 QList<QByteArray> formats = QImageReader::supportedImageFormats();
00093 for (size_t i = 0; i < formats.size(); ++i) {
00094 #if ENABLE(SVG)
00095
00096
00097
00098 if (formats.at(i).toLower().startsWith("svg"))
00099 continue;
00100 #endif
00101 String mimeType = MIMETypeRegistry::getMIMETypeForExtension(formats.at(i).constData());
00102 supportedImageMIMETypes->add(mimeType);
00103 supportedImageResourceMIMETypes->add(mimeType);
00104 }
00105 #else
00106
00107
00108 static const char* types[] = {
00109 "image/jpeg",
00110 "image/png",
00111 "image/gif",
00112 "image/bmp",
00113 "image/x-icon",
00114 "image/x-xbitmap"
00115 };
00116 for (size_t i = 0; i < sizeof(types) / sizeof(types[0]); ++i) {
00117 supportedImageMIMETypes->add(types[i]);
00118 supportedImageResourceMIMETypes->add(types[i]);
00119 }
00120 #endif
00121 }
00122
00123 static void initializeSupportedImageMIMETypesForEncoding()
00124 {
00125 supportedImageMIMETypesForEncoding = new HashSet<String>;
00126
00127 #if PLATFORM(CG)
00128 #if PLATFORM(MAC)
00129 RetainPtr<CFArrayRef> supportedTypes(AdoptCF, CGImageDestinationCopyTypeIdentifiers());
00130 CFIndex count = CFArrayGetCount(supportedTypes.get());
00131 for (CFIndex i = 0; i < count; i++) {
00132 RetainPtr<CFStringRef> supportedType(AdoptCF, reinterpret_cast<CFStringRef>(CFArrayGetValueAtIndex(supportedTypes.get(), i)));
00133 String mimeType = getMIMETypeForUTI(supportedType.get());
00134 if (!mimeType.isEmpty())
00135 supportedImageMIMETypesForEncoding->add(mimeType);
00136 }
00137 #else
00138
00139
00140 supportedImageMIMETypesForEncoding->add("image/png");
00141 #endif
00142 #elif PLATFORM(QT)
00143 QList<QByteArray> formats = QImageWriter::supportedImageFormats();
00144 for (size_t i = 0; i < formats.size(); ++i) {
00145 String mimeType = MIMETypeRegistry::getMIMETypeForExtension(formats.at(i).constData());
00146 supportedImageMIMETypesForEncoding->add(mimeType);
00147 }
00148 #endif
00149 }
00150
00151 static void initializeSupportedJavaScriptMIMETypes()
00152 {
00153
00154
00155
00156
00157
00158
00159
00160
00161 static const char* types[] = {
00162 "text/javascript",
00163 "text/ecmascript",
00164 "application/javascript",
00165 "application/ecmascript",
00166 "application/x-javascript",
00167 "text/javascript1.1",
00168 "text/javascript1.2",
00169 "text/javascript1.3",
00170 "text/jscript",
00171 "text/livescript",
00172 };
00173 for (size_t i = 0; i < sizeof(types) / sizeof(types[0]); ++i)
00174 supportedJavaScriptMIMETypes->add(types[i]);
00175 }
00176
00177 static void initializeSupportedNonImageMimeTypes()
00178 {
00179 static const char* types[] = {
00180 "text/html",
00181 "text/xml",
00182 "text/xsl",
00183 "text/plain",
00184 "text/",
00185 "application/xml",
00186 "application/xhtml+xml",
00187 "application/rss+xml",
00188 "application/atom+xml",
00189 #if ENABLE(SVG)
00190 "image/svg+xml",
00191 #endif
00192 #if ENABLE(FTPDIR)
00193 "application/x-ftp-directory",
00194 #endif
00195 "multipart/x-mixed-replace"
00196 };
00197 for (size_t i = 0; i < sizeof(types)/sizeof(types[0]); ++i)
00198 supportedNonImageMIMETypes->add(types[i]);
00199
00200 ArchiveFactory::registerKnownArchiveMIMETypes();
00201 }
00202
00203 static void initializeSupportedMediaMIMETypes()
00204 {
00205 supportedMediaMIMETypes = new HashSet<String>;
00206 #if ENABLE(VIDEO)
00207 MediaPlayer::getSupportedTypes(*supportedMediaMIMETypes);
00208 #endif
00209 }
00210
00211 static void initializeMIMETypeRegistry()
00212 {
00213 supportedJavaScriptMIMETypes = new HashSet<String>;
00214 initializeSupportedJavaScriptMIMETypes();
00215
00216 supportedNonImageMIMETypes = new HashSet<String>(*supportedJavaScriptMIMETypes);
00217 initializeSupportedNonImageMimeTypes();
00218
00219 supportedImageResourceMIMETypes = new HashSet<String>;
00220 supportedImageMIMETypes = new HashSet<String>;
00221 initializeSupportedImageMIMETypes();
00222 }
00223
00224 String MIMETypeRegistry::getMIMETypeForPath(const String& path)
00225 {
00226 int pos = path.reverseFind('.');
00227 if (pos >= 0) {
00228 String extension = path.substring(pos + 1);
00229 return getMIMETypeForExtension(extension);
00230 }
00231 return "application/octet-stream";
00232 }
00233
00234 bool MIMETypeRegistry::isSupportedImageMIMEType(const String& mimeType)
00235 {
00236 if (mimeType.isEmpty())
00237 return false;
00238 if (!supportedImageMIMETypes)
00239 initializeMIMETypeRegistry();
00240 return supportedImageMIMETypes->contains(mimeType);
00241 }
00242
00243 bool MIMETypeRegistry::isSupportedImageResourceMIMEType(const String& mimeType)
00244 {
00245 if (mimeType.isEmpty())
00246 return false;
00247 if (!supportedImageResourceMIMETypes)
00248 initializeMIMETypeRegistry();
00249 return supportedImageResourceMIMETypes->contains(mimeType);
00250 }
00251
00252 bool MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(const String& mimeType)
00253 {
00254 if (mimeType.isEmpty())
00255 return false;
00256 if (!supportedImageMIMETypesForEncoding)
00257 initializeSupportedImageMIMETypesForEncoding();
00258 return supportedImageMIMETypesForEncoding->contains(mimeType);
00259 }
00260
00261 bool MIMETypeRegistry::isSupportedJavaScriptMIMEType(const String& mimeType)
00262 {
00263 if (mimeType.isEmpty())
00264 return false;
00265 if (!supportedJavaScriptMIMETypes)
00266 initializeMIMETypeRegistry();
00267 return supportedJavaScriptMIMETypes->contains(mimeType);
00268 }
00269
00270 bool MIMETypeRegistry::isSupportedNonImageMIMEType(const String& mimeType)
00271 {
00272 if (mimeType.isEmpty())
00273 return false;
00274 if (!supportedNonImageMIMETypes)
00275 initializeMIMETypeRegistry();
00276 return supportedNonImageMIMETypes->contains(mimeType);
00277 }
00278
00279 bool MIMETypeRegistry::isSupportedMediaMIMEType(const String& mimeType)
00280 {
00281 if (mimeType.isEmpty())
00282 return false;
00283 if (!supportedMediaMIMETypes)
00284 initializeSupportedMediaMIMETypes();
00285 return supportedMediaMIMETypes->contains(mimeType);
00286 }
00287
00288 bool MIMETypeRegistry::isJavaAppletMIMEType(const String& mimeType)
00289 {
00290
00291
00292
00293
00294 return mimeType.startsWith("application/x-java-applet", false)
00295 || mimeType.startsWith("application/x-java-bean", false)
00296 || mimeType.startsWith("application/x-java-vm", false);
00297 }
00298
00299 HashSet<String>& MIMETypeRegistry::getSupportedImageMIMETypes()
00300 {
00301 if (!supportedImageMIMETypes)
00302 initializeMIMETypeRegistry();
00303 return *supportedImageMIMETypes;
00304 }
00305
00306 HashSet<String>& MIMETypeRegistry::getSupportedImageResourceMIMETypes()
00307 {
00308 if (!supportedImageResourceMIMETypes)
00309 initializeMIMETypeRegistry();
00310 return *supportedImageResourceMIMETypes;
00311 }
00312
00313 HashSet<String>& MIMETypeRegistry::getSupportedImageMIMETypesForEncoding()
00314 {
00315 if (!supportedImageMIMETypesForEncoding)
00316 initializeSupportedImageMIMETypesForEncoding();
00317 return *supportedImageMIMETypesForEncoding;
00318 }
00319
00320 HashSet<String>& MIMETypeRegistry::getSupportedNonImageMIMETypes()
00321 {
00322 if (!supportedNonImageMIMETypes)
00323 initializeMIMETypeRegistry();
00324 return *supportedNonImageMIMETypes;
00325 }
00326
00327 HashSet<String>& MIMETypeRegistry::getSupportedMediaMIMETypes()
00328 {
00329 if (!supportedMediaMIMETypes)
00330 initializeSupportedMediaMIMETypes();
00331 return *supportedMediaMIMETypes;
00332 }
00333
00334 }