• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006, 2007 Apple Inc.  All rights reserved.
3  * Copyright (C) 2007-2009 Torch Mobile, Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
25  */
26 
27 #include "config.h"
28 #include "MIMETypeRegistry.h"
29 
30 #include <wtf/Assertions.h>
31 #include <wtf/HashMap.h>
32 #include <wtf/MainThread.h>
33 #include <windows.h>
34 #include <winreg.h>
35 
36 namespace WebCore {
37 
mimeTypeForExtension(const String & extension)38 static String mimeTypeForExtension(const String& extension)
39 {
40     String ext = "." + extension;
41     WCHAR contentTypeStr[256];
42     DWORD contentTypeStrLen = sizeof(contentTypeStr);
43     DWORD valueType;
44 
45     HKEY key;
46     String result;
47     if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_CLASSES_ROOT, ext.charactersWithNullTermination(), 0, 0, &key))
48         return result;
49 
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);
52 
53     RegCloseKey(key);
54 
55     return result;
56 }
57 
58 static HashMap<String, String> mimetypeMap;
59 
initMIMETypeEntensionMap()60 static void initMIMETypeEntensionMap()
61 {
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"
89 #if ENABLE(WML)
90         mimetypeMap.add("wml", "text/vnd.wap.wml");
91 #endif
92 #if ENABLE(WBXML)
93         mimetypeMap.add("wbxml", "application/vnd.wap.wmlc");
94 #endif
95     }
96 }
97 
getPreferredExtensionForMIMEType(const String & type)98 String MIMETypeRegistry::getPreferredExtensionForMIMEType(const String& type)
99 {
100     if (type.isEmpty())
101         return String();
102 
103     // Avoid conflicts with "ttf" and "otf"
104     if (equalIgnoringCase(type, "text/plain"))
105         return "txt";
106 
107     initMIMETypeEntensionMap();
108 
109     for (HashMap<String, String>::iterator i = mimetypeMap.begin(); i != mimetypeMap.end(); ++i) {
110         if (equalIgnoringCase(i->second, type))
111             return i->first;
112     }
113 
114 #if ENABLE(XHTMLMP)
115     if (equalIgnoringCase("application/vnd.wap.xhtml+xml", type))
116         return String("xml");
117 #endif
118 
119     return String();
120 }
121 
getMIMETypeForExtension(const String & ext)122 String MIMETypeRegistry::getMIMETypeForExtension(const String &ext)
123 {
124     ASSERT(isMainThread());
125 
126     if (ext.isEmpty())
127         return String();
128 
129     initMIMETypeEntensionMap();
130 
131     String result = mimetypeMap.get(ext.lower());
132     if (result.isEmpty()) {
133         result = mimeTypeForExtension(ext);
134         if (!result.isEmpty())
135             mimetypeMap.add(ext, result);
136     }
137     return result.isEmpty() ? "unknown/unknown" : result;
138 }
139 
isApplicationPluginMIMEType(const String &)140 bool MIMETypeRegistry::isApplicationPluginMIMEType(const String&)
141 {
142     return false;
143 }
144 
145 } // namespace WebCore
146