• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KHTML

MIMETypeRegistry.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006, 2008 Apple Inc.  All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  * 1. Redistributions of source code must retain the above copyright
00008  *    notice, this list of conditions and the following disclaimer.
00009  * 2. Redistributions in binary form must reproduce the above copyright
00010  *    notice, this list of conditions and the following disclaimer in the
00011  *    documentation and/or other materials provided with the distribution.
00012  *
00013  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
00014  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00015  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00016  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
00017  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00018  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00019  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00020  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00021  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00022  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00023  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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     // On Tiger and Leopard, com.microsoft.bmp doesn't have a MIME type in the registry.
00072     supportedImageMIMETypes->add("image/bmp");
00073     supportedImageResourceMIMETypes->add("image/bmp");
00074 
00075     // Favicons don't have a MIME type in the registry either.
00076     supportedImageMIMETypes->add("image/x-icon");
00077     supportedImageResourceMIMETypes->add("image/x-icon");
00078 
00079     //  We only get one MIME type per UTI, hence our need to add these manually
00080     supportedImageMIMETypes->add("image/pjpeg");
00081     supportedImageResourceMIMETypes->add("image/pjpeg");
00082 
00083     //  We don't want to try to treat all binary data as an image
00084     supportedImageMIMETypes->remove("application/octet-stream");
00085     supportedImageResourceMIMETypes->remove("application/octet-stream");
00086 
00087     //  Don't treat pdf/postscript as images directly
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          * Qt has support for SVG, but we want to use KSVG2
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     // assume that all implementations at least support the following standard
00107     // image types:
00108     static const char* types[] = {
00109         "image/jpeg",
00110         "image/png",
00111         "image/gif",
00112         "image/bmp",
00113         "image/x-icon",    // ico
00114         "image/x-xbitmap"  // xbm
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     // FIXME: Add Windows support for all the supported UTI's when a way to convert from MIMEType to UTI reliably is found.
00139     // For now, only support PNG, the minimum that the spec requires.
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         Mozilla 1.8 and WinIE 7 both accept text/javascript and text/ecmascript.
00155         Mozilla 1.8 accepts application/javascript, application/ecmascript, and application/x-javascript, but WinIE 7 doesn't.
00156         WinIE 7 accepts text/javascript1.1 - text/javascript1.3, text/jscript, and text/livescript, but Mozilla 1.8 doesn't.
00157         Mozilla 1.8 allows leading and trailing whitespace, but WinIE 7 doesn't.
00158         Mozilla 1.8 and WinIE 7 both accept the empty string, but neither accept a whitespace-only string.
00159         We want to accept all the values that either of these browsers accept, but not other values.
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     // Since this set is very limited and is likely to remain so we won't bother with the overhead
00291     // of using a hash set.
00292     // Any of the MIME types below may be followed by any number of specific versions of the JVM,
00293     // which is why we use startsWith()
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 } // namespace WebCore

KHTML

Skip menu "KHTML"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal