We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / net / base / mime_util.cc
blobf1d44870e47fbef8e205f4debfec8ecf09d629a8
1 // Copyright (c) 2012 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.
5 #include <algorithm>
6 #include <iterator>
7 #include <map>
8 #include <string>
10 #include "base/containers/hash_tables.h"
11 #include "base/lazy_instance.h"
12 #include "base/logging.h"
13 #include "base/stl_util.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_split.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "net/base/mime_util.h"
19 #include "net/base/platform_mime_util.h"
20 #include "net/http/http_util.h"
22 #if defined(OS_ANDROID)
23 #include "base/android/build_info.h"
24 #endif
26 using std::string;
28 namespace net {
30 // Singleton utility class for mime types.
31 class MimeUtil : public PlatformMimeUtil {
32 public:
33 enum Codec {
34 INVALID_CODEC,
35 PCM,
36 MP3,
37 MPEG2_AAC_LC,
38 MPEG2_AAC_MAIN,
39 MPEG2_AAC_SSR,
40 MPEG4_AAC_LC,
41 MPEG4_AAC_SBR_v1,
42 MPEG4_AAC_SBR_PS_v2,
43 VORBIS,
44 OPUS,
45 H264_BASELINE,
46 H264_MAIN,
47 H264_HIGH,
48 VP8,
49 VP9,
50 THEORA
53 bool GetMimeTypeFromExtension(const base::FilePath::StringType& ext,
54 std::string* mime_type) const;
56 bool GetMimeTypeFromFile(const base::FilePath& file_path,
57 std::string* mime_type) const;
59 bool GetWellKnownMimeTypeFromExtension(const base::FilePath::StringType& ext,
60 std::string* mime_type) const;
62 bool IsSupportedImageMimeType(const std::string& mime_type) const;
63 bool IsSupportedMediaMimeType(const std::string& mime_type) const;
64 bool IsSupportedNonImageMimeType(const std::string& mime_type) const;
65 bool IsUnsupportedTextMimeType(const std::string& mime_type) const;
66 bool IsSupportedJavascriptMimeType(const std::string& mime_type) const;
68 bool IsSupportedMimeType(const std::string& mime_type) const;
70 bool MatchesMimeType(const std::string &mime_type_pattern,
71 const std::string &mime_type) const;
73 bool ParseMimeTypeWithoutParameter(const std::string& type_string,
74 std::string* top_level_type,
75 std::string* subtype) const;
77 bool IsValidTopLevelMimeType(const std::string& type_string) const;
79 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) const;
81 void ParseCodecString(const std::string& codecs,
82 std::vector<std::string>* codecs_out,
83 bool strip);
85 bool IsStrictMediaMimeType(const std::string& mime_type) const;
86 SupportsType IsSupportedStrictMediaMimeType(
87 const std::string& mime_type,
88 const std::vector<std::string>& codecs) const;
90 void RemoveProprietaryMediaTypesAndCodecsForTests();
92 private:
93 friend struct base::DefaultLazyInstanceTraits<MimeUtil>;
95 typedef base::hash_set<std::string> MimeMappings;
97 typedef base::hash_set<int> CodecSet;
98 typedef std::map<std::string, CodecSet> StrictMappings;
99 struct CodecEntry {
100 CodecEntry() : codec(INVALID_CODEC), is_ambiguous(true) {}
101 CodecEntry(Codec c, bool ambiguous) : codec(c), is_ambiguous(ambiguous) {}
102 Codec codec;
103 bool is_ambiguous;
105 typedef std::map<std::string, CodecEntry> StringToCodecMappings;
107 MimeUtil();
109 // Returns IsSupported if all codec IDs in |codecs| are unambiguous
110 // and are supported by the platform. MayBeSupported is returned if
111 // at least one codec ID in |codecs| is ambiguous but all the codecs
112 // are supported by the platform. IsNotSupported is returned if at
113 // least one codec ID is not supported by the platform.
114 SupportsType AreSupportedCodecs(
115 const CodecSet& supported_codecs,
116 const std::vector<std::string>& codecs) const;
118 // For faster lookup, keep hash sets.
119 void InitializeMimeTypeMaps();
121 bool GetMimeTypeFromExtensionHelper(const base::FilePath::StringType& ext,
122 bool include_platform_types,
123 std::string* mime_type) const;
125 // Converts a codec ID into an Codec enum value and indicates
126 // whether the conversion was ambiguous.
127 // Returns true if this method was able to map |codec_id| to a specific
128 // Codec enum value. |codec| and |is_ambiguous| are only valid if true
129 // is returned. Otherwise their value is undefined after the call.
130 // |is_ambiguous| is true if |codec_id| did not have enough information to
131 // unambiguously determine the proper Codec enum value. If |is_ambiguous|
132 // is true |codec| contains the best guess for the intended Codec enum value.
133 bool StringToCodec(const std::string& codec_id,
134 Codec* codec,
135 bool* is_ambiguous) const;
137 // Returns true if |codec| is supported by the platform.
138 // Note: This method will return false if the platform supports proprietary
139 // codecs but |allow_proprietary_codecs_| is set to false.
140 bool IsCodecSupported(Codec codec) const;
142 // Returns true if |codec| refers to a proprietary codec.
143 bool IsCodecProprietary(Codec codec) const;
145 // Returns true and sets |*default_codec| if |mime_type| has a default codec
146 // associated with it. Returns false otherwise and the value of
147 // |*default_codec| is undefined.
148 bool GetDefaultCodecLowerCase(const std::string& mime_type_lower_case,
149 Codec* default_codec) const;
151 // Returns true if |mime_type_lower_case| has a default codec associated with
152 // it and IsCodecSupported() returns true for that particular codec.
153 bool IsDefaultCodecSupportedLowerCase(
154 const std::string& mime_type_lower_case) const;
156 MimeMappings image_map_;
157 MimeMappings media_map_;
158 MimeMappings non_image_map_;
159 MimeMappings unsupported_text_map_;
160 MimeMappings javascript_map_;
162 // A map of mime_types and hash map of the supported codecs for the mime_type.
163 StrictMappings strict_format_map_;
165 // Keeps track of whether proprietary codec support should be
166 // advertised to callers.
167 bool allow_proprietary_codecs_;
169 // Lookup table for string compare based string -> Codec mappings.
170 StringToCodecMappings string_to_codec_map_;
171 }; // class MimeUtil
173 // This variable is Leaky because we need to access it from WorkerPool threads.
174 static base::LazyInstance<MimeUtil>::Leaky g_mime_util =
175 LAZY_INSTANCE_INITIALIZER;
177 struct MimeInfo {
178 const char* const mime_type;
179 const char* const extensions; // comma separated list
182 static const MimeInfo primary_mappings[] = {
183 { "text/html", "html,htm,shtml,shtm" },
184 { "text/css", "css" },
185 { "text/xml", "xml" },
186 { "image/gif", "gif" },
187 { "image/jpeg", "jpeg,jpg" },
188 { "image/webp", "webp" },
189 { "image/png", "png" },
190 { "video/mp4", "mp4,m4v" },
191 { "audio/x-m4a", "m4a" },
192 { "audio/mp3", "mp3" },
193 { "video/ogg", "ogv,ogm" },
194 { "audio/ogg", "ogg,oga,opus" },
195 { "video/webm", "webm" },
196 { "audio/webm", "webm" },
197 { "audio/wav", "wav" },
198 { "application/xhtml+xml", "xhtml,xht,xhtm" },
199 { "application/x-chrome-extension", "crx" },
200 { "multipart/related", "mhtml,mht" }
203 static const MimeInfo secondary_mappings[] = {
204 { "application/octet-stream", "exe,com,bin" },
205 { "application/gzip", "gz" },
206 { "application/pdf", "pdf" },
207 { "application/postscript", "ps,eps,ai" },
208 { "application/javascript", "js" },
209 { "application/font-woff", "woff" },
210 { "image/bmp", "bmp" },
211 { "image/x-icon", "ico" },
212 { "image/vnd.microsoft.icon", "ico" },
213 { "image/jpeg", "jfif,pjpeg,pjp" },
214 { "image/tiff", "tiff,tif" },
215 { "image/x-xbitmap", "xbm" },
216 { "image/svg+xml", "svg,svgz" },
217 { "image/x-png", "png"},
218 { "message/rfc822", "eml" },
219 { "text/plain", "txt,text" },
220 { "text/html", "ehtml" },
221 { "application/rss+xml", "rss" },
222 { "application/rdf+xml", "rdf" },
223 { "text/xml", "xsl,xbl,xslt" },
224 { "application/vnd.mozilla.xul+xml", "xul" },
225 { "application/x-shockwave-flash", "swf,swl" },
226 { "application/pkcs7-mime", "p7m,p7c,p7z" },
227 { "application/pkcs7-signature", "p7s" },
228 { "application/x-mpegurl", "m3u8" },
229 { "application/epub+zip", "epub" },
232 static const char* FindMimeType(const MimeInfo* mappings,
233 size_t mappings_len,
234 const char* ext) {
235 size_t ext_len = strlen(ext);
237 for (size_t i = 0; i < mappings_len; ++i) {
238 const char* extensions = mappings[i].extensions;
239 for (;;) {
240 size_t end_pos = strcspn(extensions, ",");
241 if (end_pos == ext_len &&
242 base::strncasecmp(extensions, ext, ext_len) == 0)
243 return mappings[i].mime_type;
244 extensions += end_pos;
245 if (!*extensions)
246 break;
247 extensions += 1; // skip over comma
250 return NULL;
253 bool MimeUtil::GetMimeTypeFromExtension(const base::FilePath::StringType& ext,
254 string* result) const {
255 return GetMimeTypeFromExtensionHelper(ext, true, result);
258 bool MimeUtil::GetWellKnownMimeTypeFromExtension(
259 const base::FilePath::StringType& ext,
260 string* result) const {
261 return GetMimeTypeFromExtensionHelper(ext, false, result);
264 bool MimeUtil::GetMimeTypeFromFile(const base::FilePath& file_path,
265 string* result) const {
266 base::FilePath::StringType file_name_str = file_path.Extension();
267 if (file_name_str.empty())
268 return false;
269 return GetMimeTypeFromExtension(file_name_str.substr(1), result);
272 bool MimeUtil::GetMimeTypeFromExtensionHelper(
273 const base::FilePath::StringType& ext,
274 bool include_platform_types,
275 string* result) const {
276 // Avoids crash when unable to handle a long file path. See crbug.com/48733.
277 const unsigned kMaxFilePathSize = 65536;
278 if (ext.length() > kMaxFilePathSize)
279 return false;
281 // We implement the same algorithm as Mozilla for mapping a file extension to
282 // a mime type. That is, we first check a hard-coded list (that cannot be
283 // overridden), and then if not found there, we defer to the system registry.
284 // Finally, we scan a secondary hard-coded list to catch types that we can
285 // deduce but that we also want to allow the OS to override.
287 base::FilePath path_ext(ext);
288 const string ext_narrow_str = path_ext.AsUTF8Unsafe();
289 const char* mime_type = FindMimeType(primary_mappings,
290 arraysize(primary_mappings),
291 ext_narrow_str.c_str());
292 if (mime_type) {
293 *result = mime_type;
294 return true;
297 if (include_platform_types && GetPlatformMimeTypeFromExtension(ext, result))
298 return true;
300 mime_type = FindMimeType(secondary_mappings, arraysize(secondary_mappings),
301 ext_narrow_str.c_str());
302 if (mime_type) {
303 *result = mime_type;
304 return true;
307 return false;
310 // From WebKit's WebCore/platform/MIMETypeRegistry.cpp:
312 static const char* const supported_image_types[] = {
313 "image/jpeg",
314 "image/pjpeg",
315 "image/jpg",
316 "image/webp",
317 "image/png",
318 "image/gif",
319 "image/bmp",
320 "image/vnd.microsoft.icon", // ico
321 "image/x-icon", // ico
322 "image/x-xbitmap", // xbm
323 "image/x-png"
326 // A list of media types: http://en.wikipedia.org/wiki/Internet_media_type
327 // A comprehensive mime type list: http://plugindoc.mozdev.org/winmime.php
328 // This set of codecs is supported by all variations of Chromium.
329 static const char* const common_media_types[] = {
330 // Ogg.
331 "audio/ogg",
332 "application/ogg",
333 #if !defined(OS_ANDROID) // Android doesn't support Ogg Theora.
334 "video/ogg",
335 #endif
337 // WebM.
338 "video/webm",
339 "audio/webm",
341 // Wav.
342 "audio/wav",
343 "audio/x-wav",
345 #if defined(OS_ANDROID)
346 // HLS.
347 "application/vnd.apple.mpegurl",
348 "application/x-mpegurl",
349 #endif
352 // List of proprietary types only supported by Google Chrome.
353 static const char* const proprietary_media_types[] = {
354 // MPEG-4.
355 "video/mp4",
356 "video/x-m4v",
357 "audio/mp4",
358 "audio/x-m4a",
360 // MP3.
361 "audio/mp3",
362 "audio/x-mp3",
363 "audio/mpeg",
364 "audio/aac",
366 #if defined(ENABLE_MPEG2TS_STREAM_PARSER)
367 // MPEG-2 TS.
368 "video/mp2t",
369 #endif
372 // Note:
373 // - does not include javascript types list (see supported_javascript_types)
374 // - does not include types starting with "text/" (see
375 // IsSupportedNonImageMimeType())
376 static const char* const supported_non_image_types[] = {
377 "image/svg+xml", // SVG is text-based XML, even though it has an image/ type
378 "application/xml",
379 "application/atom+xml",
380 "application/rss+xml",
381 "application/xhtml+xml",
382 "application/json",
383 "multipart/related", // For MHTML support.
384 "multipart/x-mixed-replace"
385 // Note: ADDING a new type here will probably render it AS HTML. This can
386 // result in cross site scripting.
389 // Dictionary of cryptographic file mime types.
390 struct CertificateMimeTypeInfo {
391 const char* const mime_type;
392 CertificateMimeType cert_type;
395 static const CertificateMimeTypeInfo supported_certificate_types[] = {
396 { "application/x-x509-user-cert",
397 CERTIFICATE_MIME_TYPE_X509_USER_CERT },
398 #if defined(OS_ANDROID)
399 { "application/x-x509-ca-cert", CERTIFICATE_MIME_TYPE_X509_CA_CERT },
400 { "application/x-pkcs12", CERTIFICATE_MIME_TYPE_PKCS12_ARCHIVE },
401 #endif
404 // These types are excluded from the logic that allows all text/ types because
405 // while they are technically text, it's very unlikely that a user expects to
406 // see them rendered in text form.
407 static const char* const unsupported_text_types[] = {
408 "text/calendar",
409 "text/x-calendar",
410 "text/x-vcalendar",
411 "text/vcalendar",
412 "text/vcard",
413 "text/x-vcard",
414 "text/directory",
415 "text/ldif",
416 "text/qif",
417 "text/x-qif",
418 "text/x-csv",
419 "text/x-vcf",
420 "text/rtf",
421 "text/comma-separated-values",
422 "text/csv",
423 "text/tab-separated-values",
424 "text/tsv",
425 "text/ofx", // http://crbug.com/162238
426 "text/vnd.sun.j2me.app-descriptor" // http://crbug.com/176450
429 // Mozilla 1.8 and WinIE 7 both accept text/javascript and text/ecmascript.
430 // Mozilla 1.8 accepts application/javascript, application/ecmascript, and
431 // application/x-javascript, but WinIE 7 doesn't.
432 // WinIE 7 accepts text/javascript1.1 - text/javascript1.3, text/jscript, and
433 // text/livescript, but Mozilla 1.8 doesn't.
434 // Mozilla 1.8 allows leading and trailing whitespace, but WinIE 7 doesn't.
435 // Mozilla 1.8 and WinIE 7 both accept the empty string, but neither accept a
436 // whitespace-only string.
437 // We want to accept all the values that either of these browsers accept, but
438 // not other values.
439 static const char* const supported_javascript_types[] = {
440 "text/javascript",
441 "text/ecmascript",
442 "application/javascript",
443 "application/ecmascript",
444 "application/x-javascript",
445 "text/javascript1.1",
446 "text/javascript1.2",
447 "text/javascript1.3",
448 "text/jscript",
449 "text/livescript"
452 #if defined(OS_ANDROID)
453 static bool IsCodecSupportedOnAndroid(MimeUtil::Codec codec) {
454 switch (codec) {
455 case MimeUtil::INVALID_CODEC:
456 return false;
458 case MimeUtil::PCM:
459 case MimeUtil::MP3:
460 case MimeUtil::MPEG4_AAC_LC:
461 case MimeUtil::MPEG4_AAC_SBR_v1:
462 case MimeUtil::MPEG4_AAC_SBR_PS_v2:
463 case MimeUtil::H264_BASELINE:
464 case MimeUtil::H264_MAIN:
465 case MimeUtil::H264_HIGH:
466 case MimeUtil::VP8:
467 case MimeUtil::VORBIS:
468 return true;
470 case MimeUtil::MPEG2_AAC_LC:
471 case MimeUtil::MPEG2_AAC_MAIN:
472 case MimeUtil::MPEG2_AAC_SSR:
473 // MPEG-2 variants of AAC are not supported on Android.
474 return false;
476 case MimeUtil::VP9:
477 // VP9 is supported only in KitKat+ (API Level 19).
478 return base::android::BuildInfo::GetInstance()->sdk_int() >= 19;
480 case MimeUtil::OPUS:
481 // Opus is supported only in Lollipop+ (API Level 21).
482 return base::android::BuildInfo::GetInstance()->sdk_int() >= 21;
484 case MimeUtil::THEORA:
485 return false;
488 return false;
490 #endif
492 struct MediaFormatStrict {
493 const char* const mime_type;
494 const char* const codecs_list;
497 // Following is the list of RFC 6381 compliant codecs:
498 // mp4a.66 - MPEG-2 AAC MAIN
499 // mp4a.67 - MPEG-2 AAC LC
500 // mp4a.68 - MPEG-2 AAC SSR
501 // mp4a.69 - MPEG-2 extension to MPEG-1
502 // mp4a.6B - MPEG-1 audio
503 // mp4a.40.2 - MPEG-4 AAC LC
504 // mp4a.40.02 - MPEG-4 AAC LC (leading 0 in aud-oti for compatibility)
505 // mp4a.40.5 - MPEG-4 HE-AAC v1 (AAC LC + SBR)
506 // mp4a.40.05 - MPEG-4 HE-AAC v1 (AAC LC + SBR) (leading 0 in aud-oti for
507 // compatibility)
508 // mp4a.40.29 - MPEG-4 HE-AAC v2 (AAC LC + SBR + PS)
510 // avc1.42E0xx - H.264 Baseline
511 // avc1.4D40xx - H.264 Main
512 // avc1.6400xx - H.264 High
513 static const char kMP4AudioCodecsExpression[] =
514 "mp4a.66,mp4a.67,mp4a.68,mp4a.69,mp4a.6B,mp4a.40.2,mp4a.40.02,mp4a.40.5,"
515 "mp4a.40.05,mp4a.40.29";
516 static const char kMP4VideoCodecsExpression[] =
517 // This is not a complete list of supported avc1 codecs. It is simply used
518 // to register support for the corresponding Codec enum. Instead of using
519 // strings in these three arrays, we should use the Codec enum values.
520 // This will avoid confusion and unnecessary parsing at runtime.
521 // kUnambiguousCodecStringMap/kAmbiguousCodecStringMap should be the only
522 // mapping from strings to codecs. See crbug.com/461009.
523 "avc1.42E00A,avc1.4D400A,avc1.64000A,"
524 "mp4a.66,mp4a.67,mp4a.68,mp4a.69,mp4a.6B,mp4a.40.2,mp4a.40.02,mp4a.40.5,"
525 "mp4a.40.05,mp4a.40.29";
527 // These containers are also included in
528 // common_media_types/proprietary_media_types. See crbug.com/461012.
529 static const MediaFormatStrict format_codec_mappings[] = {
530 {"video/webm", "opus,vorbis,vp8,vp8.0,vp9,vp9.0"},
531 {"audio/webm", "opus,vorbis"},
532 {"audio/wav", "1"},
533 {"audio/x-wav", "1"},
534 // Android does not support Opus in Ogg container.
535 #if defined(OS_ANDROID)
536 {"video/ogg", "theora,vorbis"},
537 {"audio/ogg", "vorbis"},
538 {"application/ogg", "theora,vorbis"},
539 #else
540 {"video/ogg", "opus,theora,vorbis"},
541 {"audio/ogg", "opus,vorbis"},
542 {"application/ogg", "opus,theora,vorbis"},
543 #endif
544 {"audio/mpeg", "mp3"},
545 {"audio/mp3", ""},
546 {"audio/x-mp3", ""},
547 {"audio/mp4", kMP4AudioCodecsExpression},
548 {"audio/x-m4a", kMP4AudioCodecsExpression},
549 {"video/mp4", kMP4VideoCodecsExpression},
550 {"video/x-m4v", kMP4VideoCodecsExpression},
551 {"application/x-mpegurl", kMP4VideoCodecsExpression},
552 {"application/vnd.apple.mpegurl", kMP4VideoCodecsExpression}};
554 struct CodecIDMappings {
555 const char* const codec_id;
556 MimeUtil::Codec codec;
559 // List of codec IDs that provide enough information to determine the
560 // codec and profile being requested.
562 // The "mp4a" strings come from RFC 6381.
563 static const CodecIDMappings kUnambiguousCodecStringMap[] = {
564 {"1", MimeUtil::PCM}, // We only allow this for WAV so it isn't ambiguous.
565 // avc1/avc3.XXXXXX may be unambiguous; handled by ParseH264CodecID().
566 {"mp3", MimeUtil::MP3},
567 {"mp4a.66", MimeUtil::MPEG2_AAC_MAIN},
568 {"mp4a.67", MimeUtil::MPEG2_AAC_LC},
569 {"mp4a.68", MimeUtil::MPEG2_AAC_SSR},
570 {"mp4a.69", MimeUtil::MP3},
571 {"mp4a.6B", MimeUtil::MP3},
572 {"mp4a.40.2", MimeUtil::MPEG4_AAC_LC},
573 {"mp4a.40.02", MimeUtil::MPEG4_AAC_LC},
574 {"mp4a.40.5", MimeUtil::MPEG4_AAC_SBR_v1},
575 {"mp4a.40.05", MimeUtil::MPEG4_AAC_SBR_v1},
576 {"mp4a.40.29", MimeUtil::MPEG4_AAC_SBR_PS_v2},
577 {"vorbis", MimeUtil::VORBIS},
578 {"opus", MimeUtil::OPUS},
579 {"vp8", MimeUtil::VP8},
580 {"vp8.0", MimeUtil::VP8},
581 {"vp9", MimeUtil::VP9},
582 {"vp9.0", MimeUtil::VP9},
583 {"theora", MimeUtil::THEORA}};
585 // List of codec IDs that are ambiguous and don't provide
586 // enough information to determine the codec and profile.
587 // The codec in these entries indicate the codec and profile
588 // we assume the user is trying to indicate.
589 static const CodecIDMappings kAmbiguousCodecStringMap[] = {
590 {"mp4a.40", MimeUtil::MPEG4_AAC_LC},
591 {"avc1", MimeUtil::H264_BASELINE},
592 {"avc3", MimeUtil::H264_BASELINE},
593 // avc1/avc3.XXXXXX may be ambiguous; handled by ParseH264CodecID().
596 MimeUtil::MimeUtil() : allow_proprietary_codecs_(false) {
597 InitializeMimeTypeMaps();
600 SupportsType MimeUtil::AreSupportedCodecs(
601 const CodecSet& supported_codecs,
602 const std::vector<std::string>& codecs) const {
603 DCHECK(!supported_codecs.empty());
604 DCHECK(!codecs.empty());
606 SupportsType result = IsSupported;
607 for (size_t i = 0; i < codecs.size(); ++i) {
608 bool is_ambiguous = true;
609 Codec codec = INVALID_CODEC;
610 if (!StringToCodec(codecs[i], &codec, &is_ambiguous))
611 return IsNotSupported;
613 if (!IsCodecSupported(codec) ||
614 supported_codecs.find(codec) == supported_codecs.end()) {
615 return IsNotSupported;
618 if (is_ambiguous)
619 result = MayBeSupported;
622 return result;
625 void MimeUtil::InitializeMimeTypeMaps() {
626 for (size_t i = 0; i < arraysize(supported_image_types); ++i)
627 image_map_.insert(supported_image_types[i]);
629 // Initialize the supported non-image types.
630 for (size_t i = 0; i < arraysize(supported_non_image_types); ++i)
631 non_image_map_.insert(supported_non_image_types[i]);
632 for (size_t i = 0; i < arraysize(supported_certificate_types); ++i)
633 non_image_map_.insert(supported_certificate_types[i].mime_type);
634 for (size_t i = 0; i < arraysize(unsupported_text_types); ++i)
635 unsupported_text_map_.insert(unsupported_text_types[i]);
636 for (size_t i = 0; i < arraysize(supported_javascript_types); ++i)
637 non_image_map_.insert(supported_javascript_types[i]);
638 for (size_t i = 0; i < arraysize(common_media_types); ++i) {
639 non_image_map_.insert(common_media_types[i]);
641 #if defined(USE_PROPRIETARY_CODECS)
642 allow_proprietary_codecs_ = true;
644 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i)
645 non_image_map_.insert(proprietary_media_types[i]);
646 #endif
648 // Initialize the supported media types.
649 for (size_t i = 0; i < arraysize(common_media_types); ++i) {
650 media_map_.insert(common_media_types[i]);
652 #if defined(USE_PROPRIETARY_CODECS)
653 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i)
654 media_map_.insert(proprietary_media_types[i]);
655 #endif
657 for (size_t i = 0; i < arraysize(supported_javascript_types); ++i)
658 javascript_map_.insert(supported_javascript_types[i]);
660 for (size_t i = 0; i < arraysize(kUnambiguousCodecStringMap); ++i) {
661 string_to_codec_map_[kUnambiguousCodecStringMap[i].codec_id] =
662 CodecEntry(kUnambiguousCodecStringMap[i].codec, false);
665 for (size_t i = 0; i < arraysize(kAmbiguousCodecStringMap); ++i) {
666 string_to_codec_map_[kAmbiguousCodecStringMap[i].codec_id] =
667 CodecEntry(kAmbiguousCodecStringMap[i].codec, true);
670 // Initialize the strict supported media types.
671 for (size_t i = 0; i < arraysize(format_codec_mappings); ++i) {
672 std::vector<std::string> mime_type_codecs;
673 ParseCodecString(format_codec_mappings[i].codecs_list,
674 &mime_type_codecs,
675 false);
677 CodecSet codecs;
678 for (size_t j = 0; j < mime_type_codecs.size(); ++j) {
679 Codec codec = INVALID_CODEC;
680 bool is_ambiguous = true;
681 CHECK(StringToCodec(mime_type_codecs[j], &codec, &is_ambiguous));
682 DCHECK(!is_ambiguous);
683 codecs.insert(codec);
686 strict_format_map_[format_codec_mappings[i].mime_type] = codecs;
690 bool MimeUtil::IsSupportedImageMimeType(const std::string& mime_type) const {
691 return image_map_.find(base::StringToLowerASCII(mime_type)) !=
692 image_map_.end();
695 bool MimeUtil::IsSupportedMediaMimeType(const std::string& mime_type) const {
696 return media_map_.find(base::StringToLowerASCII(mime_type)) !=
697 media_map_.end();
700 bool MimeUtil::IsSupportedNonImageMimeType(const std::string& mime_type) const {
701 return non_image_map_.find(base::StringToLowerASCII(mime_type)) !=
702 non_image_map_.end() ||
703 (StartsWithASCII(mime_type, "text/", false /* case insensitive */) &&
704 !IsUnsupportedTextMimeType(mime_type)) ||
705 (StartsWithASCII(mime_type, "application/", false) &&
706 MatchesMimeType("application/*+json", mime_type));
709 bool MimeUtil::IsUnsupportedTextMimeType(const std::string& mime_type) const {
710 return unsupported_text_map_.find(base::StringToLowerASCII(mime_type)) !=
711 unsupported_text_map_.end();
714 bool MimeUtil::IsSupportedJavascriptMimeType(
715 const std::string& mime_type) const {
716 return javascript_map_.find(mime_type) != javascript_map_.end();
719 // Mirrors WebViewImpl::CanShowMIMEType()
720 bool MimeUtil::IsSupportedMimeType(const std::string& mime_type) const {
721 return (StartsWithASCII(mime_type, "image/", false) &&
722 IsSupportedImageMimeType(mime_type)) ||
723 IsSupportedNonImageMimeType(mime_type);
726 // Tests for MIME parameter equality. Each parameter in the |mime_type_pattern|
727 // must be matched by a parameter in the |mime_type|. If there are no
728 // parameters in the pattern, the match is a success.
730 // According rfc2045 keys of parameters are case-insensitive, while values may
731 // or may not be case-sensitive, but they are usually case-sensitive. So, this
732 // function matches values in *case-sensitive* manner, however note that this
733 // may produce some false negatives.
734 bool MatchesMimeTypeParameters(const std::string& mime_type_pattern,
735 const std::string& mime_type) {
736 typedef std::map<std::string, std::string> StringPairMap;
738 const std::string::size_type semicolon = mime_type_pattern.find(';');
739 const std::string::size_type test_semicolon = mime_type.find(';');
740 if (semicolon != std::string::npos) {
741 if (test_semicolon == std::string::npos)
742 return false;
744 base::StringPairs pattern_parameters;
745 base::SplitStringIntoKeyValuePairs(mime_type_pattern.substr(semicolon + 1),
746 '=', ';', &pattern_parameters);
747 base::StringPairs test_parameters;
748 base::SplitStringIntoKeyValuePairs(mime_type.substr(test_semicolon + 1),
749 '=', ';', &test_parameters);
751 // Put the parameters to maps with the keys converted to lower case.
752 StringPairMap pattern_parameter_map;
753 for (const auto& pair : pattern_parameters) {
754 pattern_parameter_map[base::StringToLowerASCII(pair.first)] = pair.second;
757 StringPairMap test_parameter_map;
758 for (const auto& pair : test_parameters) {
759 test_parameter_map[base::StringToLowerASCII(pair.first)] = pair.second;
762 if (pattern_parameter_map.size() > test_parameter_map.size())
763 return false;
765 for (const auto& parameter_pair : pattern_parameter_map) {
766 const auto& test_parameter_pair_it =
767 test_parameter_map.find(parameter_pair.first);
768 if (test_parameter_pair_it == test_parameter_map.end())
769 return false;
770 if (parameter_pair.second != test_parameter_pair_it->second)
771 return false;
775 return true;
778 // This comparison handles absolute maching and also basic
779 // wildcards. The plugin mime types could be:
780 // application/x-foo
781 // application/*
782 // application/*+xml
783 // *
784 // Also tests mime parameters -- all parameters in the pattern must be present
785 // in the tested type for a match to succeed.
786 bool MimeUtil::MatchesMimeType(const std::string& mime_type_pattern,
787 const std::string& mime_type) const {
788 if (mime_type_pattern.empty())
789 return false;
791 std::string::size_type semicolon = mime_type_pattern.find(';');
792 const std::string base_pattern(mime_type_pattern.substr(0, semicolon));
793 semicolon = mime_type.find(';');
794 const std::string base_type(mime_type.substr(0, semicolon));
796 if (base_pattern == "*" || base_pattern == "*/*")
797 return MatchesMimeTypeParameters(mime_type_pattern, mime_type);
799 const std::string::size_type star = base_pattern.find('*');
800 if (star == std::string::npos) {
801 if (base_pattern.size() == base_type.size() &&
802 base::strncasecmp(base_pattern.c_str(), base_type.c_str(),
803 base_pattern.size()) == 0) {
804 return MatchesMimeTypeParameters(mime_type_pattern, mime_type);
805 } else {
806 return false;
810 // Test length to prevent overlap between |left| and |right|.
811 if (base_type.length() < base_pattern.length() - 1)
812 return false;
814 const std::string left(base_pattern.substr(0, star));
815 const std::string right(base_pattern.substr(star + 1));
817 if (!StartsWithASCII(base_type, left, false))
818 return false;
820 if (!right.empty() && !EndsWith(base_type, right, false))
821 return false;
823 return MatchesMimeTypeParameters(mime_type_pattern, mime_type);
826 // See http://www.iana.org/assignments/media-types/media-types.xhtml
827 static const char* const legal_top_level_types[] = {
828 "application",
829 "audio",
830 "example",
831 "image",
832 "message",
833 "model",
834 "multipart",
835 "text",
836 "video",
839 bool MimeUtil::ParseMimeTypeWithoutParameter(
840 const std::string& type_string,
841 std::string* top_level_type,
842 std::string* subtype) const {
843 std::vector<std::string> components;
844 base::SplitString(type_string, '/', &components);
845 if (components.size() != 2 ||
846 !HttpUtil::IsToken(components[0]) ||
847 !HttpUtil::IsToken(components[1]))
848 return false;
850 if (top_level_type)
851 *top_level_type = components[0];
852 if (subtype)
853 *subtype = components[1];
854 return true;
857 bool MimeUtil::IsValidTopLevelMimeType(const std::string& type_string) const {
858 std::string lower_type = base::StringToLowerASCII(type_string);
859 for (size_t i = 0; i < arraysize(legal_top_level_types); ++i) {
860 if (lower_type.compare(legal_top_level_types[i]) == 0)
861 return true;
864 return type_string.size() > 2 && StartsWithASCII(type_string, "x-", false);
867 bool MimeUtil::AreSupportedMediaCodecs(
868 const std::vector<std::string>& codecs) const {
869 for (size_t i = 0; i < codecs.size(); ++i) {
870 Codec codec = INVALID_CODEC;
871 bool is_ambiguous = true;
872 if (!StringToCodec(codecs[i], &codec, &is_ambiguous) ||
873 !IsCodecSupported(codec)) {
874 return false;
877 return true;
880 void MimeUtil::ParseCodecString(const std::string& codecs,
881 std::vector<std::string>* codecs_out,
882 bool strip) {
883 std::string no_quote_codecs;
884 base::TrimString(codecs, "\"", &no_quote_codecs);
885 base::SplitString(no_quote_codecs, ',', codecs_out);
887 if (!strip)
888 return;
890 // Strip everything past the first '.'
891 for (std::vector<std::string>::iterator it = codecs_out->begin();
892 it != codecs_out->end();
893 ++it) {
894 size_t found = it->find_first_of('.');
895 if (found != std::string::npos)
896 it->resize(found);
900 bool MimeUtil::IsStrictMediaMimeType(const std::string& mime_type) const {
901 return strict_format_map_.find(base::StringToLowerASCII(mime_type)) !=
902 strict_format_map_.end();
905 SupportsType MimeUtil::IsSupportedStrictMediaMimeType(
906 const std::string& mime_type,
907 const std::vector<std::string>& codecs) const {
908 const std::string mime_type_lower_case = base::StringToLowerASCII(mime_type);
909 StrictMappings::const_iterator it_strict_map =
910 strict_format_map_.find(mime_type_lower_case);
911 if (it_strict_map == strict_format_map_.end())
912 return codecs.empty() ? MayBeSupported : IsNotSupported;
914 if (it_strict_map->second.empty()) {
915 // We get here if the mimetype does not expect a codecs parameter.
916 return (codecs.empty() &&
917 IsDefaultCodecSupportedLowerCase(mime_type_lower_case))
918 ? IsSupported
919 : IsNotSupported;
922 if (codecs.empty()) {
923 // We get here if the mimetype expects to get a codecs parameter,
924 // but didn't get one. If |mime_type_lower_case| does not have a default
925 // codec the best we can do is say "maybe" because we don't have enough
926 // information.
927 Codec default_codec = INVALID_CODEC;
928 if (!GetDefaultCodecLowerCase(mime_type_lower_case, &default_codec))
929 return MayBeSupported;
931 return IsCodecSupported(default_codec) ? IsSupported : IsNotSupported;
934 return AreSupportedCodecs(it_strict_map->second, codecs);
937 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() {
938 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i) {
939 non_image_map_.erase(proprietary_media_types[i]);
940 media_map_.erase(proprietary_media_types[i]);
942 allow_proprietary_codecs_ = false;
945 // Returns true iff |profile_str| conforms to hex string "42y0", where y is one
946 // of [8..F]. Requiring constraint_set0_flag be set and profile_idc be 0x42 is
947 // taken from ISO-14496-10 7.3.2.1, 7.4.2.1, and Annex A.2.1.
949 // |profile_str| is the first four characters of the H.264 suffix string
950 // (ignoring the last 2 characters of the full 6 character suffix that are
951 // level_idc). From ISO-14496-10 7.3.2.1, it consists of:
952 // 8 bits: profile_idc: required to be 0x42 here.
953 // 1 bit: constraint_set0_flag : required to be true here.
954 // 1 bit: constraint_set1_flag : ignored here.
955 // 1 bit: constraint_set2_flag : ignored here.
956 // 1 bit: constraint_set3_flag : ignored here.
957 // 4 bits: reserved : required to be 0 here.
959 // The spec indicates other ways, not implemented here, that a |profile_str|
960 // can indicate a baseline conforming decoder is sufficient for decode in Annex
961 // A.2.1: "[profile_idc not necessarily 0x42] with constraint_set0_flag set and
962 // in which level_idc and constraint_set3_flag represent a level less than or
963 // equal to the specified level."
964 static bool IsValidH264BaselineProfile(const std::string& profile_str) {
965 uint32 constraint_set_bits;
966 if (profile_str.size() != 4 ||
967 profile_str[0] != '4' ||
968 profile_str[1] != '2' ||
969 profile_str[3] != '0' ||
970 !base::HexStringToUInt(base::StringPiece(profile_str.c_str() + 2, 1),
971 &constraint_set_bits)) {
972 return false;
975 return constraint_set_bits >= 8;
978 static bool IsValidH264Level(const std::string& level_str) {
979 uint32 level;
980 if (level_str.size() != 2 || !base::HexStringToUInt(level_str, &level))
981 return false;
983 // Valid levels taken from Table A-1 in ISO-14496-10.
984 // Essentially |level_str| is toHex(10 * level).
985 return ((level >= 10 && level <= 13) ||
986 (level >= 20 && level <= 22) ||
987 (level >= 30 && level <= 32) ||
988 (level >= 40 && level <= 42) ||
989 (level >= 50 && level <= 51));
992 // Handle parsing H.264 codec IDs as outlined in RFC 6381 and ISO-14496-10.
993 // avc1.42y0xx, y >= 8 - H.264 Baseline
994 // avc1.4D40xx - H.264 Main
995 // avc1.6400xx - H.264 High
997 // avc1.xxxxxx & avc3.xxxxxx are considered ambiguous forms that are trying to
998 // signal H.264 Baseline. For example, the idc_level, profile_idc and
999 // constraint_set3_flag pieces may explicitly require decoder to conform to
1000 // baseline profile at the specified level (see Annex A and constraint_set0 in
1001 // ISO-14496-10).
1002 static bool ParseH264CodecID(const std::string& codec_id,
1003 MimeUtil::Codec* codec,
1004 bool* is_ambiguous) {
1005 // Make sure we have avc1.xxxxxx or avc3.xxxxxx
1006 if (codec_id.size() != 11 ||
1007 (!StartsWithASCII(codec_id, "avc1.", true) &&
1008 !StartsWithASCII(codec_id, "avc3.", true))) {
1009 return false;
1012 std::string profile = StringToUpperASCII(codec_id.substr(5, 4));
1013 if (IsValidH264BaselineProfile(profile)) {
1014 *codec = MimeUtil::H264_BASELINE;
1015 } else if (profile == "4D40") {
1016 *codec = MimeUtil::H264_MAIN;
1017 } else if (profile == "6400") {
1018 *codec = MimeUtil::H264_HIGH;
1019 } else {
1020 *codec = MimeUtil::H264_BASELINE;
1021 *is_ambiguous = true;
1022 return true;
1025 *is_ambiguous = !IsValidH264Level(StringToUpperASCII(codec_id.substr(9)));
1026 return true;
1029 bool MimeUtil::StringToCodec(const std::string& codec_id,
1030 Codec* codec,
1031 bool* is_ambiguous) const {
1032 StringToCodecMappings::const_iterator itr =
1033 string_to_codec_map_.find(codec_id);
1034 if (itr != string_to_codec_map_.end()) {
1035 *codec = itr->second.codec;
1036 *is_ambiguous = itr->second.is_ambiguous;
1037 return true;
1040 // If |codec_id| is not in |string_to_codec_map_|, then we assume that it is
1041 // an H.264 codec ID because currently those are the only ones that can't be
1042 // stored in the |string_to_codec_map_| and require parsing.
1043 return ParseH264CodecID(codec_id, codec, is_ambiguous);
1046 bool MimeUtil::IsCodecSupported(Codec codec) const {
1047 DCHECK_NE(codec, INVALID_CODEC);
1049 #if defined(OS_ANDROID)
1050 if (!IsCodecSupportedOnAndroid(codec))
1051 return false;
1052 #endif
1054 return allow_proprietary_codecs_ || !IsCodecProprietary(codec);
1057 bool MimeUtil::IsCodecProprietary(Codec codec) const {
1058 switch (codec) {
1059 case INVALID_CODEC:
1060 case MP3:
1061 case MPEG2_AAC_LC:
1062 case MPEG2_AAC_MAIN:
1063 case MPEG2_AAC_SSR:
1064 case MPEG4_AAC_LC:
1065 case MPEG4_AAC_SBR_v1:
1066 case MPEG4_AAC_SBR_PS_v2:
1067 case H264_BASELINE:
1068 case H264_MAIN:
1069 case H264_HIGH:
1070 return true;
1072 case PCM:
1073 case VORBIS:
1074 case OPUS:
1075 case VP8:
1076 case VP9:
1077 case THEORA:
1078 return false;
1081 return true;
1084 bool MimeUtil::GetDefaultCodecLowerCase(const std::string& mime_type_lower_case,
1085 Codec* default_codec) const {
1086 if (mime_type_lower_case == "audio/mpeg" ||
1087 mime_type_lower_case == "audio/mp3" ||
1088 mime_type_lower_case == "audio/x-mp3") {
1089 *default_codec = MimeUtil::MP3;
1090 return true;
1093 return false;
1096 bool MimeUtil::IsDefaultCodecSupportedLowerCase(
1097 const std::string& mime_type_lower_case) const {
1098 Codec default_codec = Codec::INVALID_CODEC;
1099 if (!GetDefaultCodecLowerCase(mime_type_lower_case, &default_codec))
1100 return false;
1101 return IsCodecSupported(default_codec);
1104 //----------------------------------------------------------------------------
1105 // Wrappers for the singleton
1106 //----------------------------------------------------------------------------
1108 bool GetMimeTypeFromExtension(const base::FilePath::StringType& ext,
1109 std::string* mime_type) {
1110 return g_mime_util.Get().GetMimeTypeFromExtension(ext, mime_type);
1113 bool GetMimeTypeFromFile(const base::FilePath& file_path,
1114 std::string* mime_type) {
1115 return g_mime_util.Get().GetMimeTypeFromFile(file_path, mime_type);
1118 bool GetWellKnownMimeTypeFromExtension(const base::FilePath::StringType& ext,
1119 std::string* mime_type) {
1120 return g_mime_util.Get().GetWellKnownMimeTypeFromExtension(ext, mime_type);
1123 bool GetPreferredExtensionForMimeType(const std::string& mime_type,
1124 base::FilePath::StringType* extension) {
1125 return g_mime_util.Get().GetPreferredExtensionForMimeType(mime_type,
1126 extension);
1129 bool IsSupportedImageMimeType(const std::string& mime_type) {
1130 return g_mime_util.Get().IsSupportedImageMimeType(mime_type);
1133 bool IsSupportedMediaMimeType(const std::string& mime_type) {
1134 return g_mime_util.Get().IsSupportedMediaMimeType(mime_type);
1137 bool IsSupportedNonImageMimeType(const std::string& mime_type) {
1138 return g_mime_util.Get().IsSupportedNonImageMimeType(mime_type);
1141 bool IsUnsupportedTextMimeType(const std::string& mime_type) {
1142 return g_mime_util.Get().IsUnsupportedTextMimeType(mime_type);
1145 bool IsSupportedJavascriptMimeType(const std::string& mime_type) {
1146 return g_mime_util.Get().IsSupportedJavascriptMimeType(mime_type);
1149 bool IsSupportedMimeType(const std::string& mime_type) {
1150 return g_mime_util.Get().IsSupportedMimeType(mime_type);
1153 bool MatchesMimeType(const std::string& mime_type_pattern,
1154 const std::string& mime_type) {
1155 return g_mime_util.Get().MatchesMimeType(mime_type_pattern, mime_type);
1158 bool ParseMimeTypeWithoutParameter(const std::string& type_string,
1159 std::string* top_level_type,
1160 std::string* subtype) {
1161 return g_mime_util.Get().ParseMimeTypeWithoutParameter(
1162 type_string, top_level_type, subtype);
1165 bool IsValidTopLevelMimeType(const std::string& type_string) {
1166 return g_mime_util.Get().IsValidTopLevelMimeType(type_string);
1169 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) {
1170 return g_mime_util.Get().AreSupportedMediaCodecs(codecs);
1173 bool IsStrictMediaMimeType(const std::string& mime_type) {
1174 return g_mime_util.Get().IsStrictMediaMimeType(mime_type);
1177 SupportsType IsSupportedStrictMediaMimeType(
1178 const std::string& mime_type,
1179 const std::vector<std::string>& codecs) {
1180 return g_mime_util.Get().IsSupportedStrictMediaMimeType(mime_type, codecs);
1183 void ParseCodecString(const std::string& codecs,
1184 std::vector<std::string>* codecs_out,
1185 const bool strip) {
1186 g_mime_util.Get().ParseCodecString(codecs, codecs_out, strip);
1189 namespace {
1191 // From http://www.w3schools.com/media/media_mimeref.asp and
1192 // http://plugindoc.mozdev.org/winmime.php
1193 static const char* const kStandardImageTypes[] = {
1194 "image/bmp",
1195 "image/cis-cod",
1196 "image/gif",
1197 "image/ief",
1198 "image/jpeg",
1199 "image/webp",
1200 "image/pict",
1201 "image/pipeg",
1202 "image/png",
1203 "image/svg+xml",
1204 "image/tiff",
1205 "image/vnd.microsoft.icon",
1206 "image/x-cmu-raster",
1207 "image/x-cmx",
1208 "image/x-icon",
1209 "image/x-portable-anymap",
1210 "image/x-portable-bitmap",
1211 "image/x-portable-graymap",
1212 "image/x-portable-pixmap",
1213 "image/x-rgb",
1214 "image/x-xbitmap",
1215 "image/x-xpixmap",
1216 "image/x-xwindowdump"
1218 static const char* const kStandardAudioTypes[] = {
1219 "audio/aac",
1220 "audio/aiff",
1221 "audio/amr",
1222 "audio/basic",
1223 "audio/midi",
1224 "audio/mp3",
1225 "audio/mp4",
1226 "audio/mpeg",
1227 "audio/mpeg3",
1228 "audio/ogg",
1229 "audio/vorbis",
1230 "audio/wav",
1231 "audio/webm",
1232 "audio/x-m4a",
1233 "audio/x-ms-wma",
1234 "audio/vnd.rn-realaudio",
1235 "audio/vnd.wave"
1237 static const char* const kStandardVideoTypes[] = {
1238 "video/avi",
1239 "video/divx",
1240 "video/flc",
1241 "video/mp4",
1242 "video/mpeg",
1243 "video/ogg",
1244 "video/quicktime",
1245 "video/sd-video",
1246 "video/webm",
1247 "video/x-dv",
1248 "video/x-m4v",
1249 "video/x-mpeg",
1250 "video/x-ms-asf",
1251 "video/x-ms-wmv"
1254 struct StandardType {
1255 const char* const leading_mime_type;
1256 const char* const* standard_types;
1257 size_t standard_types_len;
1259 static const StandardType kStandardTypes[] = {
1260 { "image/", kStandardImageTypes, arraysize(kStandardImageTypes) },
1261 { "audio/", kStandardAudioTypes, arraysize(kStandardAudioTypes) },
1262 { "video/", kStandardVideoTypes, arraysize(kStandardVideoTypes) },
1263 { NULL, NULL, 0 }
1266 void GetExtensionsFromHardCodedMappings(
1267 const MimeInfo* mappings,
1268 size_t mappings_len,
1269 const std::string& leading_mime_type,
1270 base::hash_set<base::FilePath::StringType>* extensions) {
1271 for (size_t i = 0; i < mappings_len; ++i) {
1272 if (StartsWithASCII(mappings[i].mime_type, leading_mime_type, false)) {
1273 std::vector<string> this_extensions;
1274 base::SplitString(mappings[i].extensions, ',', &this_extensions);
1275 for (size_t j = 0; j < this_extensions.size(); ++j) {
1276 #if defined(OS_WIN)
1277 base::FilePath::StringType extension(
1278 base::UTF8ToWide(this_extensions[j]));
1279 #else
1280 base::FilePath::StringType extension(this_extensions[j]);
1281 #endif
1282 extensions->insert(extension);
1288 void GetExtensionsHelper(
1289 const char* const* standard_types,
1290 size_t standard_types_len,
1291 const std::string& leading_mime_type,
1292 base::hash_set<base::FilePath::StringType>* extensions) {
1293 for (size_t i = 0; i < standard_types_len; ++i) {
1294 g_mime_util.Get().GetPlatformExtensionsForMimeType(standard_types[i],
1295 extensions);
1298 // Also look up the extensions from hard-coded mappings in case that some
1299 // supported extensions are not registered in the system registry, like ogg.
1300 GetExtensionsFromHardCodedMappings(primary_mappings,
1301 arraysize(primary_mappings),
1302 leading_mime_type,
1303 extensions);
1305 GetExtensionsFromHardCodedMappings(secondary_mappings,
1306 arraysize(secondary_mappings),
1307 leading_mime_type,
1308 extensions);
1311 // Note that the elements in the source set will be appended to the target
1312 // vector.
1313 template<class T>
1314 void HashSetToVector(base::hash_set<T>* source, std::vector<T>* target) {
1315 size_t old_target_size = target->size();
1316 target->resize(old_target_size + source->size());
1317 size_t i = 0;
1318 for (typename base::hash_set<T>::iterator iter = source->begin();
1319 iter != source->end(); ++iter, ++i)
1320 (*target)[old_target_size + i] = *iter;
1323 } // namespace
1325 void GetExtensionsForMimeType(
1326 const std::string& unsafe_mime_type,
1327 std::vector<base::FilePath::StringType>* extensions) {
1328 if (unsafe_mime_type == "*/*" || unsafe_mime_type == "*")
1329 return;
1331 const std::string mime_type = base::StringToLowerASCII(unsafe_mime_type);
1332 base::hash_set<base::FilePath::StringType> unique_extensions;
1334 if (EndsWith(mime_type, "/*", false)) {
1335 std::string leading_mime_type = mime_type.substr(0, mime_type.length() - 1);
1337 // Find the matching StandardType from within kStandardTypes, or fall
1338 // through to the last (default) StandardType.
1339 const StandardType* type = NULL;
1340 for (size_t i = 0; i < arraysize(kStandardTypes); ++i) {
1341 type = &(kStandardTypes[i]);
1342 if (type->leading_mime_type &&
1343 leading_mime_type == type->leading_mime_type)
1344 break;
1346 DCHECK(type);
1347 GetExtensionsHelper(type->standard_types,
1348 type->standard_types_len,
1349 leading_mime_type,
1350 &unique_extensions);
1351 } else {
1352 g_mime_util.Get().GetPlatformExtensionsForMimeType(mime_type,
1353 &unique_extensions);
1355 // Also look up the extensions from hard-coded mappings in case that some
1356 // supported extensions are not registered in the system registry, like ogg.
1357 GetExtensionsFromHardCodedMappings(primary_mappings,
1358 arraysize(primary_mappings),
1359 mime_type,
1360 &unique_extensions);
1362 GetExtensionsFromHardCodedMappings(secondary_mappings,
1363 arraysize(secondary_mappings),
1364 mime_type,
1365 &unique_extensions);
1368 HashSetToVector(&unique_extensions, extensions);
1371 void RemoveProprietaryMediaTypesAndCodecsForTests() {
1372 g_mime_util.Get().RemoveProprietaryMediaTypesAndCodecsForTests();
1375 CertificateMimeType GetCertificateMimeTypeForMimeType(
1376 const std::string& mime_type) {
1377 // Don't create a map, there is only one entry in the table,
1378 // except on Android.
1379 for (size_t i = 0; i < arraysize(supported_certificate_types); ++i) {
1380 if (base::strcasecmp(mime_type.c_str(),
1381 net::supported_certificate_types[i].mime_type) == 0) {
1382 return net::supported_certificate_types[i].cert_type;
1385 return CERTIFICATE_MIME_TYPE_UNKNOWN;
1388 bool IsSupportedCertificateMimeType(const std::string& mime_type) {
1389 CertificateMimeType file_type =
1390 GetCertificateMimeTypeForMimeType(mime_type);
1391 return file_type != CERTIFICATE_MIME_TYPE_UNKNOWN;
1394 void AddMultipartValueForUpload(const std::string& value_name,
1395 const std::string& value,
1396 const std::string& mime_boundary,
1397 const std::string& content_type,
1398 std::string* post_data) {
1399 DCHECK(post_data);
1400 // First line is the boundary.
1401 post_data->append("--" + mime_boundary + "\r\n");
1402 // Next line is the Content-disposition.
1403 post_data->append("Content-Disposition: form-data; name=\"" +
1404 value_name + "\"\r\n");
1405 if (!content_type.empty()) {
1406 // If Content-type is specified, the next line is that.
1407 post_data->append("Content-Type: " + content_type + "\r\n");
1409 // Leave an empty line and append the value.
1410 post_data->append("\r\n" + value + "\r\n");
1413 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary,
1414 std::string* post_data) {
1415 DCHECK(post_data);
1416 post_data->append("--" + mime_boundary + "--\r\n");
1419 } // namespace net