1/*
2 * Copyright (c) 2008, 2009, Google 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 are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "MIMETypeRegistry.h"
33
34#include "MediaPlayer.h"
35#include "PlatformBridge.h"
36#include "PluginDataChromium.h"
37#include <wtf/text/CString.h>
38
39// NOTE: Unlike other ports, we don't use the shared implementation bits in
40// MIMETypeRegistry.cpp.  Instead, we need to route most functions via the
41// PlatformBridge to the embedder.
42
43namespace WebCore {
44
45#if ENABLE(FILE_SYSTEM) && ENABLE(WORKERS)
46String MIMETypeRegistry::getMIMETypeForExtensionThreadSafe(const String &ext)
47{
48    return PlatformBridge::mimeTypeForExtension(ext);
49}
50#endif
51
52// NOTE: We have to define getMIMETypeForExtension() here though the shared
53// implementation has getMIMETypeForExtension() since we don't use the shared
54// implementation bits in MIMETypeRegistry.cpp.
55
56String MIMETypeRegistry::getMIMETypeForExtension(const String &ext)
57{
58    return PlatformBridge::mimeTypeForExtension(ext);
59}
60
61// Returns the file extension if one is found.  Does not include the dot in the
62// filename.  E.g., 'html'.
63String MIMETypeRegistry::getPreferredExtensionForMIMEType(const String& type)
64{
65    // Prune out any parameters in case they happen to have snuck in there...
66    // FIXME: Is this really necessary??
67    String mimeType = type.substring(0, static_cast<unsigned>(type.find(';')));
68
69    String ext = PlatformBridge::preferredExtensionForMIMEType(type);
70    if (!ext.isEmpty() && ext[0] == '.')
71        ext = ext.substring(1);
72
73    return ext;
74}
75
76String MIMETypeRegistry::getMIMETypeForPath(const String& path)
77{
78    int pos = path.reverseFind('.');
79    if (pos < 0)
80        return "application/octet-stream";
81    String extension = path.substring(pos + 1);
82    String mimeType = getMIMETypeForExtension(extension);
83    if (mimeType.isEmpty()) {
84        // If there's no mimetype registered for the extension, check to see
85        // if a plugin can handle the extension.
86        mimeType = getPluginMimeTypeFromExtension(extension);
87    }
88    if (mimeType.isEmpty())
89        return "application/octet-stream";
90    return mimeType;
91}
92
93bool MIMETypeRegistry::isSupportedImageMIMEType(const String& mimeType)
94{
95    return PlatformBridge::isSupportedImageMIMEType(mimeType);
96}
97
98bool MIMETypeRegistry::isSupportedImageResourceMIMEType(const String& mimeType)
99{
100    return isSupportedImageMIMEType(mimeType);
101}
102
103bool MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(const String& mimeType)
104{
105    return mimeType == "image/jpeg" || mimeType == "image/png";
106}
107
108bool MIMETypeRegistry::isSupportedJavaScriptMIMEType(const String& mimeType)
109{
110    return PlatformBridge::isSupportedJavaScriptMIMEType(mimeType);
111}
112
113bool MIMETypeRegistry::isSupportedNonImageMIMEType(const String& mimeType)
114{
115    return PlatformBridge::isSupportedNonImageMIMEType(mimeType);
116}
117
118bool MIMETypeRegistry::isSupportedMediaMIMEType(const String& mimeType)
119{
120    HashSet<String> supportedMediaMIMETypes;
121#if ENABLE(VIDEO)
122    MediaPlayer::getSupportedTypes(supportedMediaMIMETypes);
123#endif
124    return !mimeType.isEmpty() && supportedMediaMIMETypes.contains(mimeType);
125}
126
127bool MIMETypeRegistry::isJavaAppletMIMEType(const String& mimeType)
128{
129    // Since this set is very limited and is likely to remain so we won't bother with the overhead
130    // of using a hash set.
131    // Any of the MIME types below may be followed by any number of specific versions of the JVM,
132    // which is why we use startsWith()
133    return mimeType.startsWith("application/x-java-applet", false)
134        || mimeType.startsWith("application/x-java-bean", false)
135        || mimeType.startsWith("application/x-java-vm", false);
136}
137
138String MIMETypeRegistry::getMediaMIMETypeForExtension(const String&)
139{
140    return String();
141}
142
143bool MIMETypeRegistry::isApplicationPluginMIMEType(const String&)
144{
145    return false;
146}
147
148static HashSet<String>& dummyHashSet()
149{
150    ASSERT_NOT_REACHED();
151    static HashSet<String> dummy;
152    return dummy;
153}
154
155// NOTE: the following methods should never be reached
156HashSet<String>& MIMETypeRegistry::getSupportedImageMIMETypes() { return dummyHashSet(); }
157HashSet<String>& MIMETypeRegistry::getSupportedImageResourceMIMETypes() { return dummyHashSet(); }
158HashSet<String>& MIMETypeRegistry::getSupportedImageMIMETypesForEncoding() { return dummyHashSet(); }
159HashSet<String>& MIMETypeRegistry::getSupportedNonImageMIMETypes() { return dummyHashSet(); }
160HashSet<String>& MIMETypeRegistry::getSupportedMediaMIMETypes() { return dummyHashSet(); }
161
162} // namespace WebCore
163