2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2007-2009 Torch Mobile, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "MIMETypeRegistry.h"
30 #include <wtf/Assertions.h>
31 #include <wtf/HashMap.h>
32 #include <wtf/MainThread.h>
38 static String mimeTypeForExtension(const String& extension)
40 String ext = "." + extension;
41 WCHAR contentTypeStr[256];
42 DWORD contentTypeStrLen = sizeof(contentTypeStr);
47 if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_CLASSES_ROOT, ext.charactersWithNullTermination(), 0, 0, &key))
50 if (ERROR_SUCCESS == RegQueryValueEx(key, L"Content Type", 0, &valueType, (LPBYTE)contentTypeStr, &contentTypeStrLen) && valueType == REG_SZ)
51 result = String(contentTypeStr, contentTypeStrLen / sizeof(contentTypeStr[0]) - 1);
58 static HashMap<String, String> mimetypeMap;
60 static void initMIMETypeEntensionMap()
62 if (mimetypeMap.isEmpty()) {
63 //fill with initial values
64 mimetypeMap.add("txt", "text/plain");
65 mimetypeMap.add("pdf", "application/pdf");
66 mimetypeMap.add("ps", "application/postscript");
67 mimetypeMap.add("html", "text/html");
68 mimetypeMap.add("htm", "text/html");
69 mimetypeMap.add("xml", "text/xml");
70 mimetypeMap.add("xsl", "text/xsl");
71 mimetypeMap.add("js", "application/x-javascript");
72 mimetypeMap.add("xhtml", "application/xhtml+xml");
73 mimetypeMap.add("rss", "application/rss+xml");
74 mimetypeMap.add("webarchive", "application/x-webarchive");
75 mimetypeMap.add("svg", "image/svg+xml");
76 mimetypeMap.add("svgz", "image/svg+xml");
77 mimetypeMap.add("jpg", "image/jpeg");
78 mimetypeMap.add("jpeg", "image/jpeg");
79 mimetypeMap.add("png", "image/png");
80 mimetypeMap.add("tif", "image/tiff");
81 mimetypeMap.add("tiff", "image/tiff");
82 mimetypeMap.add("ico", "image/ico");
83 mimetypeMap.add("cur", "image/ico");
84 mimetypeMap.add("bmp", "image/bmp");
85 mimetypeMap.add("css", "text/css");
86 // FIXME: Custom font works only when MIME is "text/plain"
87 mimetypeMap.add("ttf", "text/plain"); // "font/ttf"
88 mimetypeMap.add("otf", "text/plain"); // "font/otf"
90 mimetypeMap.add("wbxml", "application/vnd.wap.wmlc");
95 String MIMETypeRegistry::getPreferredExtensionForMIMEType(const String& type)
100 // Avoid conflicts with "ttf" and "otf"
101 if (equalIgnoringCase(type, "text/plain"))
104 initMIMETypeEntensionMap();
106 for (HashMap<String, String>::iterator i = mimetypeMap.begin(); i != mimetypeMap.end(); ++i) {
107 if (equalIgnoringCase(i->second, type))
112 if (equalIgnoringCase("application/vnd.wap.xhtml+xml", type))
113 return String("xml");
119 String MIMETypeRegistry::getMIMETypeForExtension(const String &ext)
121 ASSERT(isMainThread());
126 initMIMETypeEntensionMap();
128 String result = mimetypeMap.get(ext.lower());
129 if (result.isEmpty()) {
130 result = mimeTypeForExtension(ext);
131 if (!result.isEmpty())
132 mimetypeMap.add(ext, result);
134 return result.isEmpty() ? "unknown/unknown" : result;
137 bool MIMETypeRegistry::isApplicationPluginMIMEType(const String&)
142 } // namespace WebCore