1/*
2 * Copyright (C) 2006, 2007 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
26#include "config.h"
27#include "MIMETypeRegistry.h"
28
29#include <shlwapi.h>
30#include <wtf/Assertions.h>
31#include <wtf/HashMap.h>
32#include <wtf/MainThread.h>
33
34namespace WebCore
35{
36
37static String mimeTypeForExtension(const String& extension)
38{
39    String ext = "." + extension;
40    WCHAR contentTypeStr[256];
41    DWORD contentTypeStrLen = sizeof(contentTypeStr);
42    DWORD keyType;
43
44    HRESULT result = SHGetValue(HKEY_CLASSES_ROOT, ext.charactersWithNullTermination(), L"Content Type", &keyType, (LPVOID)contentTypeStr, &contentTypeStrLen);
45
46    if (result == ERROR_SUCCESS && keyType == REG_SZ)
47        return String(contentTypeStr, contentTypeStrLen / sizeof(contentTypeStr[0]) - 1);
48
49    return String();
50}
51
52String MIMETypeRegistry::getPreferredExtensionForMIMEType(const String& type)
53{
54    String path = "MIME\\Database\\Content Type\\" + type;
55    WCHAR extStr[MAX_PATH];
56    DWORD extStrLen = sizeof(extStr);
57    DWORD keyType;
58
59    HRESULT result = SHGetValueW(HKEY_CLASSES_ROOT, path.charactersWithNullTermination(), L"Extension", &keyType, (LPVOID)extStr, &extStrLen);
60
61    if (result == ERROR_SUCCESS && keyType == REG_SZ)
62        return String(extStr + 1, extStrLen / sizeof(extStr[0]) - 2);
63
64    return String();
65}
66
67String MIMETypeRegistry::getMIMETypeForExtension(const String &ext)
68{
69    ASSERT(isMainThread());
70
71    if (ext.isEmpty())
72        return String();
73
74    static HashMap<String, String> mimetypeMap;
75    if (mimetypeMap.isEmpty()) {
76        //fill with initial values
77        mimetypeMap.add("txt", "text/plain");
78        mimetypeMap.add("pdf", "application/pdf");
79        mimetypeMap.add("ps", "application/postscript");
80        mimetypeMap.add("html", "text/html");
81        mimetypeMap.add("htm", "text/html");
82        mimetypeMap.add("xml", "text/xml");
83        mimetypeMap.add("xsl", "text/xsl");
84        mimetypeMap.add("js", "application/x-javascript");
85        mimetypeMap.add("xhtml", "application/xhtml+xml");
86        mimetypeMap.add("rss", "application/rss+xml");
87        mimetypeMap.add("webarchive", "application/x-webarchive");
88        mimetypeMap.add("svg", "image/svg+xml");
89        mimetypeMap.add("svgz", "image/svg+xml");
90        mimetypeMap.add("jpg", "image/jpeg");
91        mimetypeMap.add("jpeg", "image/jpeg");
92        mimetypeMap.add("png", "image/png");
93        mimetypeMap.add("tif", "image/tiff");
94        mimetypeMap.add("tiff", "image/tiff");
95        mimetypeMap.add("ico", "image/ico");
96        mimetypeMap.add("cur", "image/ico");
97        mimetypeMap.add("bmp", "image/bmp");
98        mimetypeMap.add("wml", "text/vnd.wap.wml");
99        mimetypeMap.add("wmlc", "application/vnd.wap.wmlc");
100    }
101    String result = mimetypeMap.get(ext);
102    if (result.isEmpty()) {
103        result = mimeTypeForExtension(ext);
104        if (!result.isEmpty())
105            mimetypeMap.add(ext, result);
106    }
107    return result;
108}
109
110bool MIMETypeRegistry::isApplicationPluginMIMEType(const String&)
111{
112    return false;
113}
114
115}
116