PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/development/fontCache/BAL/OWBAL/Concretizations/Facilities/WK/BCMIMETypeRegistryWK.cpp

https://github.com/weissms/owb-mirror
C++ | 336 lines | 252 code | 36 blank | 48 comment | 30 complexity | 4ba4bc838d2f8e409efa9a29f6a80f46 MD5 | raw file
  1. /*
  2. * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "MIMETypeRegistry.h"
  27. #include "ArchiveFactory.h"
  28. #include "MediaPlayer.h"
  29. #include "StringHash.h"
  30. #include <wtf/HashMap.h>
  31. #include <wtf/HashSet.h>
  32. #if PLATFORM(CG)
  33. #include <ApplicationServices/ApplicationServices.h>
  34. #include <wtf/RetainPtr.h>
  35. #endif
  36. #if PLATFORM(QT)
  37. #include <qimagereader.h>
  38. #include <qimagewriter.h>
  39. #endif
  40. namespace OWBAL {
  41. static HashSet<String>* supportedImageResourceMIMETypes;
  42. static HashSet<String>* supportedImageMIMETypes;
  43. static HashSet<String>* supportedImageMIMETypesForEncoding;
  44. static HashSet<String>* supportedJavaScriptMIMETypes;
  45. static HashSet<String>* supportedNonImageMIMETypes;
  46. static HashSet<String>* supportedMediaMIMETypes;
  47. #if PLATFORM(CG)
  48. extern String getMIMETypeForUTI(const String& uti);
  49. #endif
  50. static void initializeSupportedImageMIMETypes()
  51. {
  52. #if PLATFORM(CG)
  53. RetainPtr<CFArrayRef> supportedTypes(AdoptCF, CGImageSourceCopyTypeIdentifiers());
  54. CFIndex count = CFArrayGetCount(supportedTypes.get());
  55. for (CFIndex i = 0; i < count; i++) {
  56. RetainPtr<CFStringRef> supportedType(AdoptCF, reinterpret_cast<CFStringRef>(CFArrayGetValueAtIndex(supportedTypes.get(), i)));
  57. String mimeType = getMIMETypeForUTI(supportedType.get());
  58. if (!mimeType.isEmpty()) {
  59. supportedImageMIMETypes->add(mimeType);
  60. supportedImageResourceMIMETypes->add(mimeType);
  61. }
  62. }
  63. // On Tiger and Leopard, com.microsoft.bmp doesn't have a MIME type in the registry.
  64. supportedImageMIMETypes->add("image/bmp");
  65. supportedImageResourceMIMETypes->add("image/bmp");
  66. // Favicons don't have a MIME type in the registry either.
  67. supportedImageMIMETypes->add("image/x-icon");
  68. supportedImageResourceMIMETypes->add("image/x-icon");
  69. // We only get one MIME type per UTI, hence our need to add these manually
  70. supportedImageMIMETypes->add("image/pjpeg");
  71. supportedImageResourceMIMETypes->add("image/pjpeg");
  72. // We don't want to try to treat all binary data as an image
  73. supportedImageMIMETypes->remove("application/octet-stream");
  74. supportedImageResourceMIMETypes->remove("application/octet-stream");
  75. // Don't treat pdf/postscript as images directly
  76. supportedImageMIMETypes->remove("application/pdf");
  77. supportedImageMIMETypes->remove("application/postscript");
  78. #elif PLATFORM(QT)
  79. QList<QByteArray> formats = QImageReader::supportedImageFormats();
  80. for (size_t i = 0; i < formats.size(); ++i) {
  81. #if ENABLE(SVG)
  82. /*
  83. * Qt has support for SVG, but we want to use KSVG2
  84. */
  85. if (formats.at(i).toLower().startsWith("svg"))
  86. continue;
  87. #endif
  88. String mimeType = MIMETypeRegistry::getMIMETypeForExtension(formats.at(i).constData());
  89. supportedImageMIMETypes->add(mimeType);
  90. supportedImageResourceMIMETypes->add(mimeType);
  91. }
  92. #else
  93. // assume that all implementations at least support the following standard
  94. // image types:
  95. static const char* types[] = {
  96. "image/jpeg",
  97. "image/png",
  98. "image/gif",
  99. "image/bmp",
  100. "image/x-icon", // ico
  101. "image/x-xbitmap" // xbm
  102. };
  103. for (size_t i = 0; i < sizeof(types) / sizeof(types[0]); ++i) {
  104. supportedImageMIMETypes->add(types[i]);
  105. supportedImageResourceMIMETypes->add(types[i]);
  106. }
  107. #endif
  108. }
  109. static void initializeSupportedImageMIMETypesForEncoding()
  110. {
  111. supportedImageMIMETypesForEncoding = new HashSet<String>;
  112. #if PLATFORM(CG)
  113. #if PLATFORM(MAC)
  114. RetainPtr<CFArrayRef> supportedTypes(AdoptCF, CGImageDestinationCopyTypeIdentifiers());
  115. CFIndex count = CFArrayGetCount(supportedTypes.get());
  116. for (CFIndex i = 0; i < count; i++) {
  117. RetainPtr<CFStringRef> supportedType(AdoptCF, reinterpret_cast<CFStringRef>(CFArrayGetValueAtIndex(supportedTypes.get(), i)));
  118. String mimeType = getMIMETypeForUTI(supportedType.get());
  119. if (!mimeType.isEmpty())
  120. supportedImageMIMETypesForEncoding->add(mimeType);
  121. }
  122. #else
  123. // FIXME: Add Windows support for all the supported UTI's when a way to convert from MIMEType to UTI reliably is found.
  124. // For now, only support PNG, JPEG and GIF. See <rdar://problem/6095286>.
  125. supportedImageMIMETypesForEncoding->add("image/png");
  126. supportedImageMIMETypesForEncoding->add("image/jpeg");
  127. supportedImageMIMETypesForEncoding->add("image/gif");
  128. #endif
  129. #elif PLATFORM(QT)
  130. QList<QByteArray> formats = QImageWriter::supportedImageFormats();
  131. for (size_t i = 0; i < formats.size(); ++i) {
  132. String mimeType = MIMETypeRegistry::getMIMETypeForExtension(formats.at(i).constData());
  133. supportedImageMIMETypesForEncoding->add(mimeType);
  134. }
  135. #endif
  136. }
  137. static void initializeSupportedJavaScriptMIMETypes()
  138. {
  139. /*
  140. Mozilla 1.8 and WinIE 7 both accept text/javascript and text/ecmascript.
  141. Mozilla 1.8 accepts application/javascript, application/ecmascript, and application/x-javascript, but WinIE 7 doesn't.
  142. WinIE 7 accepts text/javascript1.1 - text/javascript1.3, text/jscript, and text/livescript, but Mozilla 1.8 doesn't.
  143. Mozilla 1.8 allows leading and trailing whitespace, but WinIE 7 doesn't.
  144. Mozilla 1.8 and WinIE 7 both accept the empty string, but neither accept a whitespace-only string.
  145. We want to accept all the values that either of these browsers accept, but not other values.
  146. */
  147. static const char* types[] = {
  148. "text/javascript",
  149. "text/ecmascript",
  150. "application/javascript",
  151. "application/ecmascript",
  152. "application/x-javascript",
  153. "text/javascript1.1",
  154. "text/javascript1.2",
  155. "text/javascript1.3",
  156. "text/jscript",
  157. "text/livescript",
  158. };
  159. for (size_t i = 0; i < sizeof(types) / sizeof(types[0]); ++i)
  160. supportedJavaScriptMIMETypes->add(types[i]);
  161. }
  162. static void initializeSupportedNonImageMimeTypes()
  163. {
  164. static const char* types[] = {
  165. "text/html",
  166. "text/xml",
  167. "text/xsl",
  168. "text/plain",
  169. "text/",
  170. "application/xml",
  171. "application/xhtml+xml",
  172. "application/rss+xml",
  173. "application/atom+xml",
  174. #if ENABLE(SVG)
  175. "image/svg+xml",
  176. #endif
  177. #if ENABLE(FTPDIR)
  178. "application/x-ftp-directory",
  179. #endif
  180. "multipart/x-mixed-replace"
  181. };
  182. for (size_t i = 0; i < sizeof(types)/sizeof(types[0]); ++i)
  183. supportedNonImageMIMETypes->add(types[i]);
  184. ArchiveFactory::registerKnownArchiveMIMETypes();
  185. }
  186. static void initializeSupportedMediaMIMETypes()
  187. {
  188. supportedMediaMIMETypes = new HashSet<String>;
  189. #if ENABLE(VIDEO)
  190. MediaPlayer::getSupportedTypes(*supportedMediaMIMETypes);
  191. #endif
  192. }
  193. static void initializeMIMETypeRegistry()
  194. {
  195. supportedJavaScriptMIMETypes = new HashSet<String>;
  196. initializeSupportedJavaScriptMIMETypes();
  197. supportedNonImageMIMETypes = new HashSet<String>(*supportedJavaScriptMIMETypes);
  198. initializeSupportedNonImageMimeTypes();
  199. supportedImageResourceMIMETypes = new HashSet<String>;
  200. supportedImageMIMETypes = new HashSet<String>;
  201. initializeSupportedImageMIMETypes();
  202. }
  203. String MIMETypeRegistry::getMIMETypeForPath(const String& path)
  204. {
  205. int pos = path.reverseFind('.');
  206. if (pos >= 0) {
  207. String extension = path.substring(pos + 1);
  208. return getMIMETypeForExtension(extension);
  209. }
  210. return "application/octet-stream";
  211. }
  212. bool MIMETypeRegistry::isSupportedImageMIMEType(const String& mimeType)
  213. {
  214. if (mimeType.isEmpty())
  215. return false;
  216. if (!supportedImageMIMETypes)
  217. initializeMIMETypeRegistry();
  218. return supportedImageMIMETypes->contains(mimeType);
  219. }
  220. bool MIMETypeRegistry::isSupportedImageResourceMIMEType(const String& mimeType)
  221. {
  222. if (mimeType.isEmpty())
  223. return false;
  224. if (!supportedImageResourceMIMETypes)
  225. initializeMIMETypeRegistry();
  226. return supportedImageResourceMIMETypes->contains(mimeType);
  227. }
  228. bool MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(const String& mimeType)
  229. {
  230. if (mimeType.isEmpty())
  231. return false;
  232. if (!supportedImageMIMETypesForEncoding)
  233. initializeSupportedImageMIMETypesForEncoding();
  234. return supportedImageMIMETypesForEncoding->contains(mimeType);
  235. }
  236. bool MIMETypeRegistry::isSupportedJavaScriptMIMEType(const String& mimeType)
  237. {
  238. if (mimeType.isEmpty())
  239. return false;
  240. if (!supportedJavaScriptMIMETypes)
  241. initializeMIMETypeRegistry();
  242. return supportedJavaScriptMIMETypes->contains(mimeType);
  243. }
  244. bool MIMETypeRegistry::isSupportedNonImageMIMEType(const String& mimeType)
  245. {
  246. if (mimeType.isEmpty())
  247. return false;
  248. if (!supportedNonImageMIMETypes)
  249. initializeMIMETypeRegistry();
  250. return supportedNonImageMIMETypes->contains(mimeType);
  251. }
  252. bool MIMETypeRegistry::isSupportedMediaMIMEType(const String& mimeType)
  253. {
  254. if (mimeType.isEmpty())
  255. return false;
  256. if (!supportedMediaMIMETypes)
  257. initializeSupportedMediaMIMETypes();
  258. return supportedMediaMIMETypes->contains(mimeType);
  259. }
  260. bool MIMETypeRegistry::isJavaAppletMIMEType(const String& mimeType)
  261. {
  262. // Since this set is very limited and is likely to remain so we won't bother with the overhead
  263. // of using a hash set.
  264. // Any of the MIME types below may be followed by any number of specific versions of the JVM,
  265. // which is why we use startsWith()
  266. return mimeType.startsWith("application/x-java-applet", false)
  267. || mimeType.startsWith("application/x-java-bean", false)
  268. || mimeType.startsWith("application/x-java-vm", false);
  269. }
  270. HashSet<String>& MIMETypeRegistry::getSupportedImageMIMETypes()
  271. {
  272. if (!supportedImageMIMETypes)
  273. initializeMIMETypeRegistry();
  274. return *supportedImageMIMETypes;
  275. }
  276. HashSet<String>& MIMETypeRegistry::getSupportedImageResourceMIMETypes()
  277. {
  278. if (!supportedImageResourceMIMETypes)
  279. initializeMIMETypeRegistry();
  280. return *supportedImageResourceMIMETypes;
  281. }
  282. HashSet<String>& MIMETypeRegistry::getSupportedImageMIMETypesForEncoding()
  283. {
  284. if (!supportedImageMIMETypesForEncoding)
  285. initializeSupportedImageMIMETypesForEncoding();
  286. return *supportedImageMIMETypesForEncoding;
  287. }
  288. HashSet<String>& MIMETypeRegistry::getSupportedNonImageMIMETypes()
  289. {
  290. if (!supportedNonImageMIMETypes)
  291. initializeMIMETypeRegistry();
  292. return *supportedNonImageMIMETypes;
  293. }
  294. HashSet<String>& MIMETypeRegistry::getSupportedMediaMIMETypes()
  295. {
  296. if (!supportedMediaMIMETypes)
  297. initializeSupportedMediaMIMETypes();
  298. return *supportedMediaMIMETypes;
  299. }
  300. } // namespace WebCore