2 * Copyright (c) 2008, 2009, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "MIMETypeRegistry.h"
34 #include "ChromiumBridge.h"
35 #include "MediaPlayer.h"
36 #include "PluginDataChromium.h"
37 #include <wtf/text/CString.h>
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.
45 String MIMETypeRegistry::getMIMETypeForExtension(const String &ext)
47 return ChromiumBridge::mimeTypeForExtension(ext);
50 // Returns the file extension if one is found. Does not include the dot in the
51 // filename. E.g., 'html'.
52 String MIMETypeRegistry::getPreferredExtensionForMIMEType(const String& type)
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(';')));
58 String ext = ChromiumBridge::preferredExtensionForMIMEType(type);
59 if (!ext.isEmpty() && ext[0] == '.')
60 ext = ext.substring(1);
65 String MIMETypeRegistry::getMIMETypeForPath(const String& path)
67 int pos = path.reverseFind('.');
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);
77 if (mimeType.isEmpty())
78 return "application/octet-stream";
82 bool MIMETypeRegistry::isSupportedImageMIMEType(const String& mimeType)
84 return ChromiumBridge::isSupportedImageMIMEType(mimeType);
87 bool MIMETypeRegistry::isSupportedImageResourceMIMEType(const String& mimeType)
89 return isSupportedImageMIMEType(mimeType);
92 bool MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(const String& mimeType)
94 return mimeType == "image/jpeg" || mimeType == "image/png";
97 bool MIMETypeRegistry::isSupportedJavaScriptMIMEType(const String& mimeType)
99 return ChromiumBridge::isSupportedJavaScriptMIMEType(mimeType);
102 bool MIMETypeRegistry::isSupportedNonImageMIMEType(const String& mimeType)
104 return ChromiumBridge::isSupportedNonImageMIMEType(mimeType);
107 bool MIMETypeRegistry::isSupportedMediaMIMEType(const String& mimeType)
109 HashSet<String> supportedMediaMIMETypes;
111 MediaPlayer::getSupportedTypes(supportedMediaMIMETypes);
113 return !mimeType.isEmpty() && supportedMediaMIMETypes.contains(mimeType);
116 bool MIMETypeRegistry::isJavaAppletMIMEType(const String& mimeType)
118 // Since this set is very limited and is likely to remain so we won't bother with the overhead
119 // of using a hash set.
120 // Any of the MIME types below may be followed by any number of specific versions of the JVM,
121 // which is why we use startsWith()
122 return mimeType.startsWith("application/x-java-applet", false)
123 || mimeType.startsWith("application/x-java-bean", false)
124 || mimeType.startsWith("application/x-java-vm", false);
127 String MIMETypeRegistry::getMediaMIMETypeForExtension(const String&)
132 bool MIMETypeRegistry::isApplicationPluginMIMEType(const String&)
137 static HashSet<String>& dummyHashSet()
139 ASSERT_NOT_REACHED();
140 static HashSet<String> dummy;
144 // NOTE: the following methods should never be reached
145 HashSet<String>& MIMETypeRegistry::getSupportedImageMIMETypes() { return dummyHashSet(); }
146 HashSet<String>& MIMETypeRegistry::getSupportedImageResourceMIMETypes() { return dummyHashSet(); }
147 HashSet<String>& MIMETypeRegistry::getSupportedImageMIMETypesForEncoding() { return dummyHashSet(); }
148 HashSet<String>& MIMETypeRegistry::getSupportedNonImageMIMETypes() { return dummyHashSet(); }
149 HashSet<String>& MIMETypeRegistry::getSupportedMediaMIMETypes() { return dummyHashSet(); }
151 } // namespace WebCore