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 "ChromiumBridge.h"
35 #include "CString.h"
36 #include "MediaPlayer.h"
37 #include "PluginDataChromium.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 // ChromiumBridge to the embedder.
42
43 namespace WebCore {
44
getMIMETypeForExtension(const String & ext)45 String MIMETypeRegistry::getMIMETypeForExtension(const String &ext)
46 {
47 return ChromiumBridge::mimeTypeForExtension(ext);
48 }
49
50 // Returns the file extension if one is found. Does not include the dot in the
51 // filename. E.g., 'html'.
getPreferredExtensionForMIMEType(const String & type)52 String MIMETypeRegistry::getPreferredExtensionForMIMEType(const String& type)
53 {
54 // Prune out any parameters in case they happen to have snuck in there...
55 // FIXME: Is this really necessary??
56 String mimeType = type.substring(0, static_cast<unsigned>(type.find(';')));
57
58 String ext = ChromiumBridge::preferredExtensionForMIMEType(type);
59 if (!ext.isEmpty() && ext[0] == '.')
60 ext = ext.substring(1);
61
62 return ext;
63 }
64
getMIMETypeForPath(const String & path)65 String MIMETypeRegistry::getMIMETypeForPath(const String& path)
66 {
67 int pos = path.reverseFind('.');
68 if (pos < 0)
69 return "application/octet-stream";
70 String extension = path.substring(pos + 1);
71 String mimeType = getMIMETypeForExtension(extension);
72 if (mimeType.isEmpty()) {
73 // If there's no mimetype registered for the extension, check to see
74 // if a plugin can handle the extension.
75 mimeType = getPluginMimeTypeFromExtension(extension);
76 }
77 if (mimeType.isEmpty())
78 return "application/octet-stream";
79 return mimeType;
80 }
81
isSupportedImageMIMEType(const String & mimeType)82 bool MIMETypeRegistry::isSupportedImageMIMEType(const String& mimeType)
83 {
84 return ChromiumBridge::isSupportedImageMIMEType(mimeType);
85 }
86
isSupportedImageResourceMIMEType(const String & mimeType)87 bool MIMETypeRegistry::isSupportedImageResourceMIMEType(const String& mimeType)
88 {
89 return isSupportedImageMIMEType(mimeType);
90 }
91
isSupportedImageMIMETypeForEncoding(const String & mimeType)92 bool MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(const String& mimeType)
93 {
94 // FIXME: Fill this out. See: http://trac.webkit.org/changeset/30888
95 return isSupportedImageMIMEType(mimeType);
96 }
97
isSupportedJavaScriptMIMEType(const String & mimeType)98 bool MIMETypeRegistry::isSupportedJavaScriptMIMEType(const String& mimeType)
99 {
100 return ChromiumBridge::isSupportedJavaScriptMIMEType(mimeType);
101 }
102
isSupportedNonImageMIMEType(const String & mimeType)103 bool MIMETypeRegistry::isSupportedNonImageMIMEType(const String& mimeType)
104 {
105 return ChromiumBridge::isSupportedNonImageMIMEType(mimeType);
106 }
107
isSupportedMediaMIMEType(const String & mimeType)108 bool MIMETypeRegistry::isSupportedMediaMIMEType(const String& mimeType)
109 {
110 HashSet<String> supportedMediaMIMETypes;
111 #if ENABLE(VIDEO)
112 MediaPlayer::getSupportedTypes(supportedMediaMIMETypes);
113 #endif
114 return !mimeType.isEmpty() && supportedMediaMIMETypes.contains(mimeType);
115 }
116
isJavaAppletMIMEType(const String & mimeType)117 bool MIMETypeRegistry::isJavaAppletMIMEType(const String& mimeType)
118 {
119 // Since this set is very limited and is likely to remain so we won't bother with the overhead
120 // of using a hash set.
121 // Any of the MIME types below may be followed by any number of specific versions of the JVM,
122 // which is why we use startsWith()
123 return mimeType.startsWith("application/x-java-applet", false)
124 || mimeType.startsWith("application/x-java-bean", false)
125 || mimeType.startsWith("application/x-java-vm", false);
126 }
127
getMediaMIMETypeForExtension(const String &)128 String MIMETypeRegistry::getMediaMIMETypeForExtension(const String&)
129 {
130 return String();
131 }
132
dummyHashSet()133 static HashSet<String>& dummyHashSet()
134 {
135 ASSERT_NOT_REACHED();
136 static HashSet<String> dummy;
137 return dummy;
138 }
139
140 // NOTE: the following methods should never be reached
getSupportedImageMIMETypes()141 HashSet<String>& MIMETypeRegistry::getSupportedImageMIMETypes() { return dummyHashSet(); }
getSupportedImageResourceMIMETypes()142 HashSet<String>& MIMETypeRegistry::getSupportedImageResourceMIMETypes() { return dummyHashSet(); }
getSupportedImageMIMETypesForEncoding()143 HashSet<String>& MIMETypeRegistry::getSupportedImageMIMETypesForEncoding() { return dummyHashSet(); }
getSupportedNonImageMIMETypes()144 HashSet<String>& MIMETypeRegistry::getSupportedNonImageMIMETypes() { return dummyHashSet(); }
getSupportedMediaMIMETypes()145 HashSet<String>& MIMETypeRegistry::getSupportedMediaMIMETypes() { return dummyHashSet(); }
146
147 } // namespace WebCore
148