initial import
[vuplus_webkit] / Source / WebCore / platform / wince / MIMETypeRegistryWinCE.cpp
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
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
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(WBXML)
90         mimetypeMap.add("wbxml", "application/vnd.wap.wmlc");
91 #endif
92     }
93 }
94
95 String MIMETypeRegistry::getPreferredExtensionForMIMEType(const String& type)
96 {
97     if (type.isEmpty())
98         return String();
99
100     // Avoid conflicts with "ttf" and "otf"
101     if (equalIgnoringCase(type, "text/plain"))
102         return "txt";
103
104     initMIMETypeEntensionMap();
105
106     for (HashMap<String, String>::iterator i = mimetypeMap.begin(); i != mimetypeMap.end(); ++i) {
107         if (equalIgnoringCase(i->second, type))
108             return i->first;
109     }
110
111 #if ENABLE(XHTMLMP)
112     if (equalIgnoringCase("application/vnd.wap.xhtml+xml", type))
113         return String("xml");
114 #endif
115
116     return String();
117 }
118
119 String MIMETypeRegistry::getMIMETypeForExtension(const String &ext)
120 {
121     ASSERT(isMainThread());
122
123     if (ext.isEmpty())
124         return String();
125
126     initMIMETypeEntensionMap();
127
128     String result = mimetypeMap.get(ext.lower());
129     if (result.isEmpty()) {
130         result = mimeTypeForExtension(ext);
131         if (!result.isEmpty())
132             mimetypeMap.add(ext, result);
133     }
134     return result.isEmpty() ? "unknown/unknown" : result;
135 }
136
137 bool MIMETypeRegistry::isApplicationPluginMIMEType(const String&)
138 {
139     return false;
140 }
141
142 } // namespace WebCore