Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(255)

Side by Side Diff: components/mime_util/mime_util.cc

Issue 1110833003: Move the IsSupported* mime functions out of //net and into //components/mime_util (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/mime_util/mime_util.h ('k') | components/mime_util/mime_util.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 MimeTypes = base::hash_set<std::string>;
122
123 MimeUtil();
124
125 MimeTypes image_types_;
126 MimeTypes non_image_types_;
127 MimeTypes unsupported_text_types_;
128 MimeTypes javascript_types_;
129
130 DISALLOW_COPY_AND_ASSIGN(MimeUtil);
131 };
132
133 MimeUtil::MimeUtil() {
134 for (size_t i = 0; i < arraysize(kSupportedNonImageTypes); ++i)
135 non_image_types_.insert(kSupportedNonImageTypes[i]);
136 for (size_t i = 0; i < arraysize(kSupportedImageTypes); ++i)
137 image_types_.insert(kSupportedImageTypes[i]);
138 for (size_t i = 0; i < arraysize(kUnsupportedTextTypes); ++i)
139 unsupported_text_types_.insert(kUnsupportedTextTypes[i]);
140 for (size_t i = 0; i < arraysize(kSupportedJavascriptTypes); ++i) {
141 javascript_types_.insert(kSupportedJavascriptTypes[i]);
142 non_image_types_.insert(kSupportedJavascriptTypes[i]);
143 }
144 for (size_t i = 0; i < arraysize(kSupportedCertificateTypes); ++i)
145 non_image_types_.insert(kSupportedCertificateTypes[i].mime_type);
146 }
147
148 bool MimeUtil::IsSupportedImageMimeType(const std::string& mime_type) const {
149 return image_types_.find(base::StringToLowerASCII(mime_type)) !=
150 image_types_.end();
151 }
152
153 bool MimeUtil::IsSupportedNonImageMimeType(const std::string& mime_type) const {
154 return non_image_types_.find(base::StringToLowerASCII(mime_type)) !=
155 non_image_types_.end() ||
156 (StartsWithASCII(mime_type, "text/", false /* case insensitive */) &&
157 !IsUnsupportedTextMimeType(mime_type)) ||
158 (StartsWithASCII(mime_type, "application/", false) &&
159 net::MatchesMimeType("application/*+json", mime_type)) ||
160 net::IsSupportedMediaMimeType(mime_type);
161 }
162
163 bool MimeUtil::IsUnsupportedTextMimeType(const std::string& mime_type) const {
164 return unsupported_text_types_.find(base::StringToLowerASCII(mime_type)) !=
165 unsupported_text_types_.end();
166 }
167
168 bool MimeUtil::IsSupportedJavascriptMimeType(
169 const std::string& mime_type) const {
170 return javascript_types_.find(mime_type) != javascript_types_.end();
171 }
172
173 bool MimeUtil::IsSupportedMimeType(const std::string& mime_type) const {
174 return (StartsWithASCII(mime_type, "image/", false) &&
175 IsSupportedImageMimeType(mime_type)) ||
176 IsSupportedNonImageMimeType(mime_type);
177 }
178
179 // This variable is Leaky because it is accessed from WorkerPool threads.
180 static base::LazyInstance<MimeUtil>::Leaky g_mime_util =
181 LAZY_INSTANCE_INITIALIZER;
182
183 } // namespace
184
185 bool IsSupportedImageMimeType(const std::string& mime_type) {
186 return g_mime_util.Get().IsSupportedImageMimeType(mime_type);
187 }
188
189 bool IsSupportedNonImageMimeType(const std::string& mime_type) {
190 return g_mime_util.Get().IsSupportedNonImageMimeType(mime_type);
191 }
192
193 bool IsUnsupportedTextMimeType(const std::string& mime_type) {
194 return g_mime_util.Get().IsUnsupportedTextMimeType(mime_type);
195 }
196
197 bool IsSupportedJavascriptMimeType(const std::string& mime_type) {
198 return g_mime_util.Get().IsSupportedJavascriptMimeType(mime_type);
199 }
200
201 bool IsSupportedCertificateMimeType(const std::string& mime_type) {
202 net::CertificateMimeType file_type =
203 GetCertificateMimeTypeForMimeType(mime_type);
204 return file_type != net::CERTIFICATE_MIME_TYPE_UNKNOWN;
205 }
206
207 bool IsSupportedMimeType(const std::string& mime_type) {
208 return g_mime_util.Get().IsSupportedMimeType(mime_type);
209 }
210
211 net::CertificateMimeType GetCertificateMimeTypeForMimeType(
212 const std::string& mime_type) {
213 // Don't create a map, there is only one entry in the table,
214 // except on Android.
215 for (size_t i = 0; i < arraysize(kSupportedCertificateTypes); ++i) {
216 if (base::strcasecmp(mime_type.c_str(),
217 kSupportedCertificateTypes[i].mime_type) == 0) {
218 return kSupportedCertificateTypes[i].cert_type;
219 }
220 }
221
222 return net::CERTIFICATE_MIME_TYPE_UNKNOWN;
223 }
224
225 } // namespace mime_util
OLDNEW
« no previous file with comments | « components/mime_util/mime_util.h ('k') | components/mime_util/mime_util.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698