OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/mime_util/mime_util.h" | |
6 | |
7 #include "base/containers/hash_tables.h" | |
8 #include "base/lazy_instance.h" | |
9 #include "base/strings/string_util.h" | |
10 #include "build/build_config.h" | |
11 | |
12 namespace mime_util { | |
13 | |
14 namespace { | |
15 | |
16 // Dictionary of cryptographic file mime types. | |
17 struct CertificateMimeTypeInfo { | |
18 const char* const mime_type; | |
19 net::CertificateMimeType cert_type; | |
20 }; | |
21 | |
22 const CertificateMimeTypeInfo kSupportedCertificateTypes[] = { | |
23 {"application/x-x509-user-cert", net::CERTIFICATE_MIME_TYPE_X509_USER_CERT}, | |
24 #if defined(OS_ANDROID) | |
25 {"application/x-x509-ca-cert", net::CERTIFICATE_MIME_TYPE_X509_CA_CERT}, | |
26 {"application/x-pkcs12", net::CERTIFICATE_MIME_TYPE_PKCS12_ARCHIVE}, | |
27 #endif | |
28 }; | |
29 | |
30 // From WebKit's WebCore/platform/MIMETypeRegistry.cpp: | |
31 | |
32 const char* const kSupportedImageTypes[] = {"image/jpeg", | |
33 "image/pjpeg", | |
34 "image/jpg", | |
35 "image/webp", | |
36 "image/png", | |
37 "image/gif", | |
38 "image/bmp", | |
39 "image/vnd.microsoft.icon", // ico | |
40 "image/x-icon", // ico | |
41 "image/x-xbitmap", // xbm | |
42 "image/x-png"}; | |
43 | |
44 // Mozilla 1.8 and WinIE 7 both accept text/javascript and text/ecmascript. | |
45 // Mozilla 1.8 accepts application/javascript, application/ecmascript, and | |
46 // application/x-javascript, but WinIE 7 doesn't. | |
47 // WinIE 7 accepts text/javascript1.1 - text/javascript1.3, text/jscript, and | |
48 // text/livescript, but Mozilla 1.8 doesn't. | |
49 // Mozilla 1.8 allows leading and trailing whitespace, but WinIE 7 doesn't. | |
50 // Mozilla 1.8 and WinIE 7 both accept the empty string, but neither accept a | |
51 // whitespace-only string. | |
52 // We want to accept all the values that either of these browsers accept, but | |
53 // not other values. | |
54 const char* const kSupportedJavascriptTypes[] = {"text/javascript", | |
55 "text/ecmascript", | |
56 "application/javascript", | |
57 "application/ecmascript", | |
58 "application/x-javascript", | |
59 "text/javascript1.1", | |
60 "text/javascript1.2", | |
61 "text/javascript1.3", | |
62 "text/jscript", | |
63 "text/livescript"}; | |
64 | |
65 // These types are excluded from the logic that allows all text/ types because | |
66 // while they are technically text, it's very unlikely that a user expects to | |
67 // see them rendered in text form. | |
68 static const char* const kUnsupportedTextTypes[] = { | |
69 "text/calendar", | |
70 "text/x-calendar", | |
71 "text/x-vcalendar", | |
72 "text/vcalendar", | |
73 "text/vcard", | |
74 "text/x-vcard", | |
75 "text/directory", | |
76 "text/ldif", | |
77 "text/qif", | |
78 "text/x-qif", | |
79 "text/x-csv", | |
80 "text/x-vcf", | |
81 "text/rtf", | |
82 "text/comma-separated-values", | |
83 "text/csv", | |
84 "text/tab-separated-values", | |
85 "text/tsv", | |
86 "text/ofx", // http://crbug.com/162238 | |
87 "text/vnd.sun.j2me.app-descriptor" // http://crbug.com/176450 | |
88 }; | |
89 | |
90 // Note: | |
91 // - does not include javascript types list (see supported_javascript_types) | |
92 // - does not include types starting with "text/" (see | |
93 // IsSupportedNonImageMimeType()) | |
94 static const char* const kSupportedNonImageTypes[] = { | |
95 "image/svg+xml", // SVG is text-based XML, even though it has an image/ | |
96 // type | |
97 "application/xml", | |
98 "application/atom+xml", | |
99 "application/rss+xml", | |
100 "application/xhtml+xml", | |
101 "application/json", | |
102 "multipart/related", // For MHTML support. | |
103 "multipart/x-mixed-replace" | |
104 // Note: ADDING a new type here will probably render it AS HTML. This can | |
105 // result in cross site scripting. | |
106 }; | |
107 | |
108 // Singleton utility class for mime types | |
109 class MimeUtil { | |
110 public: | |
111 bool IsSupportedImageMimeType(const std::string& mime_type) const; | |
112 bool IsSupportedNonImageMimeType(const std::string& mime_type) const; | |
113 bool IsUnsupportedTextMimeType(const std::string& mime_type) const; | |
114 bool IsSupportedJavascriptMimeType(const std::string& mime_type) const; | |
115 | |
116 bool IsSupportedMimeType(const std::string& mime_type) const; | |
117 | |
118 private: | |
119 friend struct base::DefaultLazyInstanceTraits<MimeUtil>; | |
120 | |
121 using MimeMappings = base::hash_set<std::string>; | |
asanka
2015/04/29 00:14:06
Minor nit: there are sets. Not maps.
(I know you
| |
122 | |
123 MimeUtil(); | |
124 | |
125 MimeMappings image_map_; | |
126 MimeMappings non_image_map_; | |
127 MimeMappings unsupported_text_map_; | |
128 MimeMappings javascript_map_; | |
129 | |
130 DISALLOW_COPY_AND_ASSIGN(MimeUtil); | |
131 }; | |
132 | |
133 MimeUtil::MimeUtil() { | |
134 // Initialize the supported non-image types. | |
asanka
2015/04/29 00:14:06
Nit: remove.
| |
135 for (size_t i = 0; i < arraysize(kSupportedNonImageTypes); ++i) | |
136 non_image_map_.insert(kSupportedNonImageTypes[i]); | |
137 for (size_t i = 0; i < arraysize(kSupportedImageTypes); ++i) | |
138 image_map_.insert(kSupportedImageTypes[i]); | |
139 for (size_t i = 0; i < arraysize(kUnsupportedTextTypes); ++i) | |
140 unsupported_text_map_.insert(kUnsupportedTextTypes[i]); | |
141 for (size_t i = 0; i < arraysize(kSupportedJavascriptTypes); ++i) { | |
142 javascript_map_.insert(kSupportedJavascriptTypes[i]); | |
143 non_image_map_.insert(kSupportedJavascriptTypes[i]); | |
144 } | |
145 for (size_t i = 0; i < arraysize(kSupportedCertificateTypes); ++i) | |
146 non_image_map_.insert(kSupportedCertificateTypes[i].mime_type); | |
147 } | |
148 | |
149 bool MimeUtil::IsSupportedImageMimeType(const std::string& mime_type) const { | |
150 return image_map_.find(base::StringToLowerASCII(mime_type)) != | |
151 image_map_.end(); | |
152 } | |
153 | |
154 bool MimeUtil::IsSupportedNonImageMimeType(const std::string& mime_type) const { | |
155 return non_image_map_.find(base::StringToLowerASCII(mime_type)) != | |
156 non_image_map_.end() || | |
157 (StartsWithASCII(mime_type, "text/", false /* case insensitive */) && | |
158 !IsUnsupportedTextMimeType(mime_type)) || | |
159 (StartsWithASCII(mime_type, "application/", false) && | |
160 net::MatchesMimeType("application/*+json", mime_type)) || | |
161 net::IsSupportedMediaMimeType(mime_type); | |
162 } | |
163 | |
164 bool MimeUtil::IsUnsupportedTextMimeType(const std::string& mime_type) const { | |
165 return unsupported_text_map_.find(base::StringToLowerASCII(mime_type)) != | |
166 unsupported_text_map_.end(); | |
167 } | |
168 | |
169 bool MimeUtil::IsSupportedJavascriptMimeType( | |
170 const std::string& mime_type) const { | |
171 return javascript_map_.find(mime_type) != javascript_map_.end(); | |
172 } | |
173 | |
174 // Mirrors WebViewImpl::CanShowMIMEType() | |
asanka
2015/04/29 00:14:06
Nit: Comment appears to have bitrotted.
| |
175 bool MimeUtil::IsSupportedMimeType(const std::string& mime_type) const { | |
176 return (StartsWithASCII(mime_type, "image/", false) && | |
177 IsSupportedImageMimeType(mime_type)) || | |
178 IsSupportedNonImageMimeType(mime_type); | |
179 } | |
180 | |
181 // This variable is Leaky because it is accessed from WorkerPool threads. | |
182 static base::LazyInstance<MimeUtil>::Leaky g_mime_util = | |
183 LAZY_INSTANCE_INITIALIZER; | |
184 | |
185 } // namespace | |
186 | |
187 bool IsSupportedImageMimeType(const std::string& mime_type) { | |
188 return g_mime_util.Get().IsSupportedImageMimeType(mime_type); | |
189 } | |
190 | |
191 bool IsSupportedNonImageMimeType(const std::string& mime_type) { | |
192 return g_mime_util.Get().IsSupportedNonImageMimeType(mime_type); | |
193 } | |
194 | |
195 bool IsUnsupportedTextMimeType(const std::string& mime_type) { | |
196 return g_mime_util.Get().IsUnsupportedTextMimeType(mime_type); | |
197 } | |
198 | |
199 bool IsSupportedJavascriptMimeType(const std::string& mime_type) { | |
200 return g_mime_util.Get().IsSupportedJavascriptMimeType(mime_type); | |
201 } | |
202 | |
203 bool IsSupportedCertificateMimeType(const std::string& mime_type) { | |
204 net::CertificateMimeType file_type = | |
205 GetCertificateMimeTypeForMimeType(mime_type); | |
206 return file_type != net::CERTIFICATE_MIME_TYPE_UNKNOWN; | |
207 } | |
208 | |
209 bool IsSupportedMimeType(const std::string& mime_type) { | |
210 return g_mime_util.Get().IsSupportedMimeType(mime_type); | |
211 } | |
212 | |
213 net::CertificateMimeType GetCertificateMimeTypeForMimeType( | |
214 const std::string& mime_type) { | |
215 // Don't create a map, there is only one entry in the table, | |
216 // except on Android. | |
217 for (size_t i = 0; i < arraysize(kSupportedCertificateTypes); ++i) { | |
218 if (base::strcasecmp(mime_type.c_str(), | |
219 kSupportedCertificateTypes[i].mime_type) == 0) { | |
220 return kSupportedCertificateTypes[i].cert_type; | |
221 } | |
222 } | |
223 | |
224 return net::CERTIFICATE_MIME_TYPE_UNKNOWN; | |
225 } | |
226 | |
227 } // namespace mime_util | |
OLD | NEW |