1/*
2 * Copyright (C) 2006-2016 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 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 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#pragma once
27
28#include <wtf/HashSet.h>
29#include <wtf/Vector.h>
30#include <wtf/text/StringHash.h>
31
32namespace WebCore {
33
34class MIMETypeRegistry {
35public:
36 WEBCORE_EXPORT static String getMIMETypeForExtension(const String& extension);
37
38 // FIXME: WebKit coding style says we should not have the word "get" in the names of these functions.
39 static Vector<String> getExtensionsForMIMEType(const String& type);
40 WEBCORE_EXPORT static String getPreferredExtensionForMIMEType(const String& type);
41 static String getMediaMIMETypeForExtension(const String& extension);
42 static Vector<String> getMediaMIMETypesForExtension(const String& extension);
43
44 static String getMIMETypeForPath(const String& path);
45
46 // Check to see if a MIME type is suitable for being loaded inline as an
47 // image (e.g., <img> tags).
48 WEBCORE_EXPORT static bool isSupportedImageMIMEType(const String& mimeType);
49
50 // Check to see if a MIME type is suitable for being loaded as an image, including SVG and Video (where supported).
51 WEBCORE_EXPORT static bool isSupportedImageVideoOrSVGMIMEType(const String& mimeType);
52
53 // Check to see if a MIME type is suitable for being encoded.
54 static bool isSupportedImageMIMETypeForEncoding(const String& mimeType);
55
56 // Check to see if a MIME type is suitable for being loaded as a JavaScript or JSON resource.
57 WEBCORE_EXPORT static bool isSupportedJavaScriptMIMEType(const String& mimeType);
58 WEBCORE_EXPORT static bool isSupportedJSONMIMEType(const String& mimeType);
59
60 // Check to see if a MIME type is suitable for being loaded as a style sheet.
61 static bool isSupportedStyleSheetMIMEType(const String& mimeType);
62
63 // Check to see if a MIME type is suitable for being loaded as a font.
64 static bool isSupportedFontMIMEType(const String& mimeType);
65
66 // Check to see if a MIME type is a text media playlist type, such as an m3u8.
67 static bool isTextMediaPlaylistMIMEType(const String& mimeType);
68
69 // Check to see if a non-image MIME type is suitable for being loaded as a
70 // document in a frame. Does not include supported JavaScript and JSON MIME types.
71 WEBCORE_EXPORT static bool isSupportedNonImageMIMEType(const String& mimeType);
72
73 // Check to see if a MIME type is suitable for being loaded using <video> and <audio>.
74 WEBCORE_EXPORT static bool isSupportedMediaMIMEType(const String& mimeType);
75
76 // Check to see if a MIME type is suitable for being loaded using <track>>.
77 WEBCORE_EXPORT static bool isSupportedTextTrackMIMEType(const String& mimeType);
78
79 // Check to see if a MIME type is a valid Java applet mime type.
80 WEBCORE_EXPORT static bool isJavaAppletMIMEType(const String& mimeType);
81
82 // Check to see if a MIME type is a plugin implemented by the browser.
83 static bool isApplicationPluginMIMEType(const String& mimeType);
84
85 // Check to see if a MIME type is one of the common PDF/PS types.
86 static bool isPDFMIMEType(const String& mimeType);
87 static bool isPostScriptMIMEType(const String& mimeType);
88 WEBCORE_EXPORT static bool isPDFOrPostScriptMIMEType(const String& mimeType);
89
90 WEBCORE_EXPORT static bool isSystemPreviewMIMEType(const String& mimeType);
91
92 // Check to see if a MIME type is suitable for being shown inside a page.
93 // Returns true if any of isSupportedImageMIMEType(), isSupportedNonImageMIMEType(),
94 // isSupportedMediaMIMEType(), isSupportedJavaScriptMIMEType(), isSupportedJSONMIMEType(),
95 // returns true or if the given MIME type begins with "text/" and
96 // isUnsupportedTextMIMEType() returns false.
97 WEBCORE_EXPORT static bool canShowMIMEType(const String& mimeType);
98
99 // Check to see if a MIME type is one where an XML document should be created
100 // rather than an HTML document.
101 WEBCORE_EXPORT static bool isXMLMIMEType(const String& mimeType);
102
103 // Used in page load algorithm to decide whether to display as a text
104 // document in a frame. Not a good idea to use elsewhere, because that code
105 // makes this test is after many other tests are done on the MIME type.
106 WEBCORE_EXPORT static bool isTextMIMEType(const String& mimeType);
107
108 // FIXME: Would be nice to find a way to avoid exposing these sets, even worse exposing non-const references.
109 WEBCORE_EXPORT static const HashSet<String, ASCIICaseInsensitiveHash>& supportedImageMIMETypes();
110 static HashSet<String, ASCIICaseInsensitiveHash>& additionalSupportedImageMIMETypes();
111 WEBCORE_EXPORT static HashSet<String, ASCIICaseInsensitiveHash>& supportedNonImageMIMETypes();
112 WEBCORE_EXPORT static const HashSet<String, ASCIICaseInsensitiveHash>& supportedMediaMIMETypes();
113 WEBCORE_EXPORT static const HashSet<String, ASCIICaseInsensitiveHash>& pdfMIMETypes();
114 WEBCORE_EXPORT static const HashSet<String, ASCIICaseInsensitiveHash>& unsupportedTextMIMETypes();
115 WEBCORE_EXPORT static const HashSet<String, ASCIICaseInsensitiveHash>& systemPreviewMIMETypes();
116
117 // FIXME: WebKit coding style says we should not have the word "get" in the name of this function.
118 // FIXME: Unclear what the concept of a normalized MIME type is; currently it's a platform-specific notion.
119 static String getNormalizedMIMEType(const String&);
120
121 WEBCORE_EXPORT static String appendFileExtensionIfNecessary(const String& filename, const String& mimeType);
122
123private:
124 // Check to see if the MIME type is not suitable for being loaded as a text
125 // document in a frame. Only valid for MIME types begining with "text/".
126 static bool isUnsupportedTextMIMEType(const String& mimeType);
127};
128
129WEBCORE_EXPORT const String& defaultMIMEType();
130
131} // namespace WebCore
132