OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <algorithm> | 5 #include <algorithm> |
6 #include <iterator> | 6 #include <iterator> |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
14 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
17 #include "net/base/mime_util.h" | 17 #include "net/base/mime_util.h" |
18 #include "net/base/platform_mime_util.h" | 18 #include "net/base/platform_mime_util.h" |
19 #include "net/http/http_util.h" | 19 #include "net/http/http_util.h" |
20 | 20 |
21 #if defined(OS_ANDROID) | 21 #if defined(OS_ANDROID) |
22 #include "base/android/build_info.h" | 22 #include "base/android/build_info.h" |
scherkus (not reviewing)
2014/07/18 02:00:00
header doesn't look like it's needed anymore
acolwell GONE FROM CHROMIUM
2014/07/18 16:17:12
Done.
| |
23 #endif | 23 #endif |
24 | 24 |
25 using std::string; | 25 using std::string; |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 struct MediaType { | 29 struct MediaType { |
30 const char name[12]; | 30 const char name[12]; |
31 const char matcher[13]; | 31 const char matcher[13]; |
32 }; | 32 }; |
(...skipping 20 matching lines...) Expand all Loading... | |
53 bool GetMimeTypeFromExtension(const base::FilePath::StringType& ext, | 53 bool GetMimeTypeFromExtension(const base::FilePath::StringType& ext, |
54 std::string* mime_type) const; | 54 std::string* mime_type) const; |
55 | 55 |
56 bool GetMimeTypeFromFile(const base::FilePath& file_path, | 56 bool GetMimeTypeFromFile(const base::FilePath& file_path, |
57 std::string* mime_type) const; | 57 std::string* mime_type) const; |
58 | 58 |
59 bool GetWellKnownMimeTypeFromExtension(const base::FilePath::StringType& ext, | 59 bool GetWellKnownMimeTypeFromExtension(const base::FilePath::StringType& ext, |
60 std::string* mime_type) const; | 60 std::string* mime_type) const; |
61 | 61 |
62 bool IsSupportedImageMimeType(const 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; | 63 bool IsSupportedNonImageMimeType(const std::string& mime_type) const; |
65 bool IsUnsupportedTextMimeType(const std::string& mime_type) const; | 64 bool IsUnsupportedTextMimeType(const std::string& mime_type) const; |
66 bool IsSupportedJavascriptMimeType(const std::string& mime_type) const; | 65 bool IsSupportedJavascriptMimeType(const std::string& mime_type) const; |
67 | 66 |
68 bool IsSupportedMimeType(const std::string& mime_type) const; | 67 bool IsSupportedMimeType(const std::string& mime_type) const; |
69 | 68 |
70 bool MatchesMimeType(const std::string &mime_type_pattern, | 69 bool MatchesMimeType(const std::string &mime_type_pattern, |
71 const std::string &mime_type) const; | 70 const std::string &mime_type) const; |
72 | 71 |
73 bool ParseMimeTypeWithoutParameter(const std::string& type_string, | 72 bool ParseMimeTypeWithoutParameter(const std::string& type_string, |
74 std::string* top_level_type, | 73 std::string* top_level_type, |
75 std::string* subtype) const; | 74 std::string* subtype) const; |
76 | 75 |
77 bool IsValidTopLevelMimeType(const std::string& type_string) const; | 76 bool IsValidTopLevelMimeType(const std::string& type_string) const; |
78 | 77 |
79 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) const; | |
80 | |
81 void ParseCodecString(const std::string& codecs, | |
82 std::vector<std::string>* codecs_out, | |
83 bool strip); | |
84 | |
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; | |
89 | |
90 void RemoveProprietaryMediaTypesAndCodecsForTests(); | |
91 | |
92 private: | 78 private: |
93 friend struct base::DefaultLazyInstanceTraits<MimeUtil>; | 79 friend struct base::DefaultLazyInstanceTraits<MimeUtil>; |
94 | 80 |
95 typedef base::hash_set<std::string> MimeMappings; | 81 typedef base::hash_set<std::string> MimeMappings; |
96 typedef std::map<std::string, MimeMappings> StrictMappings; | |
97 | |
98 typedef std::vector<std::string> MimeExpressionMappings; | |
99 typedef std::map<std::string, MimeExpressionMappings> | |
100 StrictExpressionMappings; | |
101 | 82 |
102 MimeUtil(); | 83 MimeUtil(); |
103 | 84 |
104 // Returns true if |codecs| is nonempty and all the items in it are present in | |
105 // |supported_codecs|. | |
106 static bool AreSupportedCodecs(const MimeMappings& supported_codecs, | |
107 const std::vector<std::string>& codecs); | |
108 // Returns true is |codecs| is nonempty and all the items in it match with the | |
109 // codecs expression in |supported_codecs|. | |
110 static bool AreSupportedCodecsWithProfile( | |
111 const MimeExpressionMappings& supported_codecs, | |
112 const std::vector<std::string>& codecs); | |
113 | |
114 // For faster lookup, keep hash sets. | 85 // For faster lookup, keep hash sets. |
115 void InitializeMimeTypeMaps(); | 86 void InitializeMimeTypeMaps(); |
116 | 87 |
117 bool GetMimeTypeFromExtensionHelper(const base::FilePath::StringType& ext, | 88 bool GetMimeTypeFromExtensionHelper(const base::FilePath::StringType& ext, |
118 bool include_platform_types, | 89 bool include_platform_types, |
119 std::string* mime_type) const; | 90 std::string* mime_type) const; |
120 | 91 |
121 MimeMappings image_map_; | 92 MimeMappings image_map_; |
122 MimeMappings media_map_; | |
123 MimeMappings non_image_map_; | 93 MimeMappings non_image_map_; |
124 MimeMappings unsupported_text_map_; | 94 MimeMappings unsupported_text_map_; |
125 MimeMappings javascript_map_; | 95 MimeMappings javascript_map_; |
126 MimeMappings codecs_map_; | |
127 | |
128 // A map of mime_types and hash map of the supported codecs for the mime_type. | |
129 StrictMappings strict_format_map_; | |
130 // A map of MP4 mime_types which expect codecs with profile parameter and | |
131 // vector of supported codecs expressions for the mime_type. | |
132 StrictExpressionMappings strict_mp4_format_map_; | |
133 }; // class MimeUtil | 96 }; // class MimeUtil |
134 | 97 |
135 // This variable is Leaky because we need to access it from WorkerPool threads. | 98 // This variable is Leaky because we need to access it from WorkerPool threads. |
136 static base::LazyInstance<MimeUtil>::Leaky g_mime_util = | 99 static base::LazyInstance<MimeUtil>::Leaky g_mime_util = |
137 LAZY_INSTANCE_INITIALIZER; | 100 LAZY_INSTANCE_INITIALIZER; |
138 | 101 |
139 struct MimeInfo { | 102 struct MimeInfo { |
140 const char* mime_type; | 103 const char* mime_type; |
141 const char* extensions; // comma separated list | 104 const char* extensions; // comma separated list |
142 }; | 105 }; |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
276 "image/webp", | 239 "image/webp", |
277 "image/png", | 240 "image/png", |
278 "image/gif", | 241 "image/gif", |
279 "image/bmp", | 242 "image/bmp", |
280 "image/vnd.microsoft.icon", // ico | 243 "image/vnd.microsoft.icon", // ico |
281 "image/x-icon", // ico | 244 "image/x-icon", // ico |
282 "image/x-xbitmap", // xbm | 245 "image/x-xbitmap", // xbm |
283 "image/x-png" | 246 "image/x-png" |
284 }; | 247 }; |
285 | 248 |
286 // A list of media types: http://en.wikipedia.org/wiki/Internet_media_type | |
287 // A comprehensive mime type list: http://plugindoc.mozdev.org/winmime.php | |
288 // This set of codecs is supported by all variations of Chromium. | |
289 static const char* const common_media_types[] = { | |
290 // Ogg. | |
291 "audio/ogg", | |
292 "application/ogg", | |
293 #if !defined(OS_ANDROID) // Android doesn't support Ogg Theora. | |
294 "video/ogg", | |
295 #endif | |
296 | |
297 // WebM. | |
298 "video/webm", | |
299 "audio/webm", | |
300 | |
301 // Wav. | |
302 "audio/wav", | |
303 "audio/x-wav", | |
304 | |
305 #if defined(OS_ANDROID) | |
306 // HLS. Supported by Android ICS and above. | |
307 "application/vnd.apple.mpegurl", | |
308 "application/x-mpegurl", | |
309 #endif | |
310 }; | |
311 | |
312 // List of proprietary types only supported by Google Chrome. | |
313 static const char* const proprietary_media_types[] = { | |
314 // MPEG-4. | |
315 "video/mp4", | |
316 "video/x-m4v", | |
317 "audio/mp4", | |
318 "audio/x-m4a", | |
319 | |
320 // MP3. | |
321 "audio/mp3", | |
322 "audio/x-mp3", | |
323 "audio/mpeg", | |
324 }; | |
325 | |
326 // List of supported codecs when passed in with <source type="...">. | |
327 // This set of codecs is supported by all variations of Chromium. | |
328 // | |
329 // Refer to http://wiki.whatwg.org/wiki/Video_type_parameters#Browser_Support | |
330 // for more information. | |
331 // | |
332 // The codecs for WAV are integers as defined in Appendix A of RFC2361: | |
333 // http://tools.ietf.org/html/rfc2361 | |
334 static const char* const common_media_codecs[] = { | |
335 #if !defined(OS_ANDROID) // Android doesn't support Ogg Theora. | |
336 "theora", | |
337 #endif | |
338 "opus", | |
339 "vorbis", | |
340 "vp8", | |
341 "vp9", | |
342 "1" // WAVE_FORMAT_PCM. | |
343 }; | |
344 | |
345 // List of proprietary codecs only supported by Google Chrome. | |
346 static const char* const proprietary_media_codecs[] = { | |
347 "avc1", | |
348 "avc3", | |
349 "mp4a" | |
350 }; | |
351 | 249 |
352 // Note: | 250 // Note: |
353 // - does not include javascript types list (see supported_javascript_types) | 251 // - does not include javascript types list (see supported_javascript_types) |
354 // - does not include types starting with "text/" (see | 252 // - does not include types starting with "text/" (see |
355 // IsSupportedNonImageMimeType()) | 253 // IsSupportedNonImageMimeType()) |
356 static const char* const supported_non_image_types[] = { | 254 static const char* const supported_non_image_types[] = { |
357 "image/svg+xml", // SVG is text-based XML, even though it has an image/ type | 255 "image/svg+xml", // SVG is text-based XML, even though it has an image/ type |
358 "application/xml", | 256 "application/xml", |
359 "application/atom+xml", | 257 "application/atom+xml", |
360 "application/rss+xml", | 258 "application/rss+xml", |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
422 "application/javascript", | 320 "application/javascript", |
423 "application/ecmascript", | 321 "application/ecmascript", |
424 "application/x-javascript", | 322 "application/x-javascript", |
425 "text/javascript1.1", | 323 "text/javascript1.1", |
426 "text/javascript1.2", | 324 "text/javascript1.2", |
427 "text/javascript1.3", | 325 "text/javascript1.3", |
428 "text/jscript", | 326 "text/jscript", |
429 "text/livescript" | 327 "text/livescript" |
430 }; | 328 }; |
431 | 329 |
432 #if defined(OS_ANDROID) | |
433 static bool IsCodecSupportedOnAndroid(const std::string& codec) { | |
434 // Theora is not supported in Android | |
435 if (!codec.compare("theora")) | |
436 return false; | |
437 | |
438 // VP9 is supported only in KitKat+ (API Level 19). | |
439 if ((!codec.compare("vp9") || !codec.compare("vp9.0")) && | |
440 base::android::BuildInfo::GetInstance()->sdk_int() < 19) { | |
441 return false; | |
442 } | |
443 | |
444 // TODO(vigneshv): Change this similar to the VP9 check once Opus is | |
445 // supported on Android (http://crbug.com/318436). | |
446 if (!codec.compare("opus")) { | |
447 return false; | |
448 } | |
449 return true; | |
450 } | |
451 | |
452 static bool IsMimeTypeSupportedOnAndroid(const std::string& mimeType) { | |
453 // HLS codecs are supported in ICS and above (API level 14) | |
454 if ((!mimeType.compare("application/vnd.apple.mpegurl") || | |
455 !mimeType.compare("application/x-mpegurl")) && | |
456 base::android::BuildInfo::GetInstance()->sdk_int() < 14) { | |
457 return false; | |
458 } | |
459 return true; | |
460 } | |
461 #endif | |
462 | |
463 struct MediaFormatStrict { | |
464 const char* mime_type; | |
465 const char* codecs_list; | |
466 }; | |
467 | |
468 static const MediaFormatStrict format_codec_mappings[] = { | |
469 { "video/webm", "opus,vorbis,vp8,vp8.0,vp9,vp9.0" }, | |
470 { "audio/webm", "opus,vorbis" }, | |
471 { "audio/wav", "1" }, | |
472 { "audio/x-wav", "1" }, | |
473 { "video/ogg", "opus,theora,vorbis" }, | |
474 { "audio/ogg", "opus,vorbis" }, | |
475 { "application/ogg", "opus,theora,vorbis" }, | |
476 { "audio/mpeg", ",mp3" }, // Note: The comma before the 'mp3'results in an | |
477 // empty string codec ID and indicates | |
478 // a missing codecs= parameter is also valid. | |
479 // The presense of 'mp3' is not RFC compliant, | |
480 // but is common in the wild so it is a defacto | |
481 // standard. | |
482 { "audio/mp3", "" }, | |
483 { "audio/x-mp3", "" } | |
484 }; | |
485 | |
486 // Following is the list of RFC 6381 compliant codecs: | |
487 // mp4a.6B - MPEG-1 audio | |
488 // mp4a.69 - MPEG-2 extension to MPEG-1 | |
489 // mp4a.67 - MPEG-2 AAC | |
490 // mp4a.40.2 - MPEG-4 AAC | |
491 // mp4a.40.5 - MPEG-4 HE-AAC | |
492 // | |
493 // avc1.42E0xx - H.264 Baseline | |
494 // avc1.4D40xx - H.264 Main | |
495 // avc1.6400xx - H.264 High | |
496 // | |
497 // Additionally, several non-RFC compliant codecs are allowed, due to their | |
498 // existing use on web. | |
499 // mp4a.40 | |
500 // avc1.xxxxxx | |
501 // avc3.xxxxxx | |
502 // mp4a.6x | |
503 static const char kProprietaryAudioCodecsExpression[] = | |
504 "mp4a.6?,mp4a.40,mp4a.40.?"; | |
505 static const char kProprietaryCodecsExpression[] = | |
506 "avc1,avc3,avc1.??????,avc3.??????,mp4a.6?,mp4a.40,mp4a.40.?"; | |
507 | |
508 static const MediaFormatStrict format_mp4_codec_mappings[] = { | |
509 { "audio/mp4", kProprietaryAudioCodecsExpression }, | |
510 { "audio/x-m4a", kProprietaryAudioCodecsExpression }, | |
511 { "video/mp4", kProprietaryCodecsExpression }, | |
512 { "video/x-m4v", kProprietaryCodecsExpression }, | |
513 { "application/x-mpegurl", kProprietaryCodecsExpression }, | |
514 { "application/vnd.apple.mpegurl", kProprietaryCodecsExpression } | |
515 }; | |
516 | |
517 MimeUtil::MimeUtil() { | 330 MimeUtil::MimeUtil() { |
518 InitializeMimeTypeMaps(); | 331 InitializeMimeTypeMaps(); |
519 } | 332 } |
520 | 333 |
521 // static | |
522 bool MimeUtil::AreSupportedCodecs(const MimeMappings& supported_codecs, | |
523 const std::vector<std::string>& codecs) { | |
524 if (supported_codecs.empty()) | |
525 return codecs.empty(); | |
526 | |
527 // If no codecs are specified in the mimetype, check to see if a missing | |
528 // codecs parameter is allowed. | |
529 if (codecs.empty()) | |
530 return supported_codecs.find(std::string()) != supported_codecs.end(); | |
531 | |
532 for (size_t i = 0; i < codecs.size(); ++i) { | |
533 if (codecs[i].empty() || | |
534 supported_codecs.find(codecs[i]) == supported_codecs.end()) { | |
535 return false; | |
536 } | |
537 } | |
538 | |
539 return true; | |
540 } | |
541 | |
542 // Checks all the codecs present in the |codecs| against the entries in | |
543 // |supported_codecs|. Returns true only if |codecs| is non-empty and all the | |
544 // codecs match |supported_codecs| expressions. | |
545 bool MimeUtil::AreSupportedCodecsWithProfile( | |
546 const MimeExpressionMappings& supported_codecs, | |
547 const std::vector<std::string>& codecs) { | |
548 DCHECK(!supported_codecs.empty()); | |
549 for (size_t i = 0; i < codecs.size(); ++i) { | |
550 bool codec_matched = false; | |
551 for (size_t j = 0; j < supported_codecs.size(); ++j) { | |
552 if (!MatchPattern(base::StringPiece(codecs[i]), | |
553 base::StringPiece(supported_codecs[j]))) { | |
554 continue; | |
555 } | |
556 // If suffix exists, check whether it is hexadecimal. | |
557 for (size_t wildcard_pos = supported_codecs[j].find('?'); | |
558 wildcard_pos != std::string::npos && | |
559 wildcard_pos < supported_codecs[j].length(); | |
560 wildcard_pos = supported_codecs[j].find('?', wildcard_pos + 1)) { | |
561 // Don't enforce case sensitivity, even though it's called for, as it | |
562 // would break some websites. | |
563 if (wildcard_pos >= codecs[i].length() || | |
564 !IsHexDigit(codecs[i].at(wildcard_pos))) { | |
565 return false; | |
566 } | |
567 } | |
568 codec_matched = true; | |
569 break; | |
570 } | |
571 if (!codec_matched) | |
572 return false; | |
573 } | |
574 return !codecs.empty(); | |
575 } | |
576 | |
577 void MimeUtil::InitializeMimeTypeMaps() { | 334 void MimeUtil::InitializeMimeTypeMaps() { |
578 for (size_t i = 0; i < arraysize(supported_image_types); ++i) | 335 for (size_t i = 0; i < arraysize(supported_image_types); ++i) |
579 image_map_.insert(supported_image_types[i]); | 336 image_map_.insert(supported_image_types[i]); |
580 | 337 |
581 // Initialize the supported non-image types. | 338 // Initialize the supported non-image types. |
582 for (size_t i = 0; i < arraysize(supported_non_image_types); ++i) | 339 for (size_t i = 0; i < arraysize(supported_non_image_types); ++i) |
583 non_image_map_.insert(supported_non_image_types[i]); | 340 non_image_map_.insert(supported_non_image_types[i]); |
584 for (size_t i = 0; i < arraysize(supported_certificate_types); ++i) | 341 for (size_t i = 0; i < arraysize(supported_certificate_types); ++i) |
585 non_image_map_.insert(supported_certificate_types[i].mime_type); | 342 non_image_map_.insert(supported_certificate_types[i].mime_type); |
586 for (size_t i = 0; i < arraysize(unsupported_text_types); ++i) | 343 for (size_t i = 0; i < arraysize(unsupported_text_types); ++i) |
587 unsupported_text_map_.insert(unsupported_text_types[i]); | 344 unsupported_text_map_.insert(unsupported_text_types[i]); |
588 for (size_t i = 0; i < arraysize(supported_javascript_types); ++i) | 345 for (size_t i = 0; i < arraysize(supported_javascript_types); ++i) |
589 non_image_map_.insert(supported_javascript_types[i]); | 346 non_image_map_.insert(supported_javascript_types[i]); |
590 for (size_t i = 0; i < arraysize(common_media_types); ++i) { | |
591 #if defined(OS_ANDROID) | |
592 if (!IsMimeTypeSupportedOnAndroid(common_media_types[i])) | |
593 continue; | |
594 #endif | |
595 non_image_map_.insert(common_media_types[i]); | |
596 } | |
597 #if defined(USE_PROPRIETARY_CODECS) | |
598 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i) | |
599 non_image_map_.insert(proprietary_media_types[i]); | |
600 #endif | |
601 | |
602 // Initialize the supported media types. | |
603 for (size_t i = 0; i < arraysize(common_media_types); ++i) { | |
604 #if defined(OS_ANDROID) | |
605 if (!IsMimeTypeSupportedOnAndroid(common_media_types[i])) | |
606 continue; | |
607 #endif | |
608 media_map_.insert(common_media_types[i]); | |
609 } | |
610 #if defined(USE_PROPRIETARY_CODECS) | |
611 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i) | |
612 media_map_.insert(proprietary_media_types[i]); | |
613 #endif | |
614 | |
615 for (size_t i = 0; i < arraysize(supported_javascript_types); ++i) | |
616 javascript_map_.insert(supported_javascript_types[i]); | |
617 | |
618 for (size_t i = 0; i < arraysize(common_media_codecs); ++i) { | |
619 #if defined(OS_ANDROID) | |
620 if (!IsCodecSupportedOnAndroid(common_media_codecs[i])) | |
621 continue; | |
622 #endif | |
623 codecs_map_.insert(common_media_codecs[i]); | |
624 } | |
625 #if defined(USE_PROPRIETARY_CODECS) | |
626 for (size_t i = 0; i < arraysize(proprietary_media_codecs); ++i) | |
627 codecs_map_.insert(proprietary_media_codecs[i]); | |
628 #endif | |
629 | |
630 // Initialize the strict supported media types. | |
631 for (size_t i = 0; i < arraysize(format_codec_mappings); ++i) { | |
632 std::vector<std::string> mime_type_codecs; | |
633 ParseCodecString(format_codec_mappings[i].codecs_list, | |
634 &mime_type_codecs, | |
635 false); | |
636 | |
637 MimeMappings codecs; | |
638 for (size_t j = 0; j < mime_type_codecs.size(); ++j) { | |
639 #if defined(OS_ANDROID) | |
640 if (!IsCodecSupportedOnAndroid(mime_type_codecs[j])) | |
641 continue; | |
642 #endif | |
643 codecs.insert(mime_type_codecs[j]); | |
644 } | |
645 strict_format_map_[format_codec_mappings[i].mime_type] = codecs; | |
646 } | |
647 for (size_t i = 0; i < arraysize(format_mp4_codec_mappings); ++i) { | |
648 std::vector<std::string> mime_type_codecs; | |
649 ParseCodecString( | |
650 format_mp4_codec_mappings[i].codecs_list, &mime_type_codecs, false); | |
651 | |
652 MimeExpressionMappings codecs; | |
653 for (size_t j = 0; j < mime_type_codecs.size(); ++j) | |
654 codecs.push_back(mime_type_codecs[j]); | |
655 strict_mp4_format_map_[format_mp4_codec_mappings[i].mime_type] = codecs; | |
656 } | |
657 } | 347 } |
658 | 348 |
659 bool MimeUtil::IsSupportedImageMimeType(const std::string& mime_type) const { | 349 bool MimeUtil::IsSupportedImageMimeType(const std::string& mime_type) const { |
660 return image_map_.find(mime_type) != image_map_.end(); | 350 return image_map_.find(mime_type) != image_map_.end(); |
661 } | 351 } |
662 | 352 |
663 bool MimeUtil::IsSupportedMediaMimeType(const std::string& mime_type) const { | |
664 return media_map_.find(mime_type) != media_map_.end(); | |
665 } | |
666 | |
667 bool MimeUtil::IsSupportedNonImageMimeType(const std::string& mime_type) const { | 353 bool MimeUtil::IsSupportedNonImageMimeType(const std::string& mime_type) const { |
668 return non_image_map_.find(mime_type) != non_image_map_.end() || | 354 return non_image_map_.find(mime_type) != non_image_map_.end() || |
669 (mime_type.compare(0, 5, "text/") == 0 && | 355 (mime_type.compare(0, 5, "text/") == 0 && |
670 !IsUnsupportedTextMimeType(mime_type)) || | 356 !IsUnsupportedTextMimeType(mime_type)) || |
671 (mime_type.compare(0, 12, "application/") == 0 && | 357 (mime_type.compare(0, 12, "application/") == 0 && |
672 MatchesMimeType("application/*+json", mime_type)); | 358 MatchesMimeType("application/*+json", mime_type)); |
673 } | 359 } |
674 | 360 |
675 bool MimeUtil::IsUnsupportedTextMimeType(const std::string& mime_type) const { | 361 bool MimeUtil::IsUnsupportedTextMimeType(const std::string& mime_type) const { |
676 return unsupported_text_map_.find(mime_type) != unsupported_text_map_.end(); | 362 return unsupported_text_map_.find(mime_type) != unsupported_text_map_.end(); |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
801 bool MimeUtil::IsValidTopLevelMimeType(const std::string& type_string) const { | 487 bool MimeUtil::IsValidTopLevelMimeType(const std::string& type_string) const { |
802 std::string lower_type = StringToLowerASCII(type_string); | 488 std::string lower_type = StringToLowerASCII(type_string); |
803 for (size_t i = 0; i < arraysize(legal_top_level_types); ++i) { | 489 for (size_t i = 0; i < arraysize(legal_top_level_types); ++i) { |
804 if (lower_type.compare(legal_top_level_types[i]) == 0) | 490 if (lower_type.compare(legal_top_level_types[i]) == 0) |
805 return true; | 491 return true; |
806 } | 492 } |
807 | 493 |
808 return type_string.size() > 2 && StartsWithASCII(type_string, "x-", false); | 494 return type_string.size() > 2 && StartsWithASCII(type_string, "x-", false); |
809 } | 495 } |
810 | 496 |
811 bool MimeUtil::AreSupportedMediaCodecs( | |
812 const std::vector<std::string>& codecs) const { | |
813 return AreSupportedCodecs(codecs_map_, codecs); | |
814 } | |
815 | |
816 void MimeUtil::ParseCodecString(const std::string& codecs, | |
817 std::vector<std::string>* codecs_out, | |
818 bool strip) { | |
819 std::string no_quote_codecs; | |
820 base::TrimString(codecs, "\"", &no_quote_codecs); | |
821 base::SplitString(no_quote_codecs, ',', codecs_out); | |
822 | |
823 if (!strip) | |
824 return; | |
825 | |
826 // Strip everything past the first '.' | |
827 for (std::vector<std::string>::iterator it = codecs_out->begin(); | |
828 it != codecs_out->end(); | |
829 ++it) { | |
830 size_t found = it->find_first_of('.'); | |
831 if (found != std::string::npos) | |
832 it->resize(found); | |
833 } | |
834 } | |
835 | |
836 bool MimeUtil::IsStrictMediaMimeType(const std::string& mime_type) const { | |
837 if (strict_format_map_.find(mime_type) == strict_format_map_.end() && | |
838 strict_mp4_format_map_.find(mime_type) == strict_mp4_format_map_.end()) | |
839 return false; | |
840 return true; | |
841 } | |
842 | |
843 SupportsType MimeUtil::IsSupportedStrictMediaMimeType( | |
844 const std::string& mime_type, | |
845 const std::vector<std::string>& codecs) const { | |
846 StrictMappings::const_iterator it_strict_map = | |
847 strict_format_map_.find(mime_type); | |
848 if ((it_strict_map != strict_format_map_.end()) && | |
849 AreSupportedCodecs(it_strict_map->second, codecs)) { | |
850 return IsSupported; | |
851 } | |
852 | |
853 StrictExpressionMappings::const_iterator it_expression_map = | |
854 strict_mp4_format_map_.find(mime_type); | |
855 if ((it_expression_map != strict_mp4_format_map_.end()) && | |
856 AreSupportedCodecsWithProfile(it_expression_map->second, codecs)) { | |
857 return MayBeSupported; | |
858 } | |
859 | |
860 if (codecs.empty()) | |
861 return MayBeSupported; | |
862 | |
863 return IsNotSupported; | |
864 } | |
865 | |
866 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() { | |
867 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i) { | |
868 non_image_map_.erase(proprietary_media_types[i]); | |
869 media_map_.erase(proprietary_media_types[i]); | |
870 } | |
871 for (size_t i = 0; i < arraysize(proprietary_media_codecs); ++i) | |
872 codecs_map_.erase(proprietary_media_codecs[i]); | |
873 } | |
874 | |
875 //---------------------------------------------------------------------------- | 497 //---------------------------------------------------------------------------- |
876 // Wrappers for the singleton | 498 // Wrappers for the singleton |
877 //---------------------------------------------------------------------------- | 499 //---------------------------------------------------------------------------- |
878 | 500 |
879 bool GetMimeTypeFromExtension(const base::FilePath::StringType& ext, | 501 bool GetMimeTypeFromExtension(const base::FilePath::StringType& ext, |
880 std::string* mime_type) { | 502 std::string* mime_type) { |
881 return g_mime_util.Get().GetMimeTypeFromExtension(ext, mime_type); | 503 return g_mime_util.Get().GetMimeTypeFromExtension(ext, mime_type); |
882 } | 504 } |
883 | 505 |
884 bool GetMimeTypeFromFile(const base::FilePath& file_path, | 506 bool GetMimeTypeFromFile(const base::FilePath& file_path, |
885 std::string* mime_type) { | 507 std::string* mime_type) { |
886 return g_mime_util.Get().GetMimeTypeFromFile(file_path, mime_type); | 508 return g_mime_util.Get().GetMimeTypeFromFile(file_path, mime_type); |
887 } | 509 } |
888 | 510 |
889 bool GetWellKnownMimeTypeFromExtension(const base::FilePath::StringType& ext, | 511 bool GetWellKnownMimeTypeFromExtension(const base::FilePath::StringType& ext, |
890 std::string* mime_type) { | 512 std::string* mime_type) { |
891 return g_mime_util.Get().GetWellKnownMimeTypeFromExtension(ext, mime_type); | 513 return g_mime_util.Get().GetWellKnownMimeTypeFromExtension(ext, mime_type); |
892 } | 514 } |
893 | 515 |
894 bool GetPreferredExtensionForMimeType(const std::string& mime_type, | 516 bool GetPreferredExtensionForMimeType(const std::string& mime_type, |
895 base::FilePath::StringType* extension) { | 517 base::FilePath::StringType* extension) { |
896 return g_mime_util.Get().GetPreferredExtensionForMimeType(mime_type, | 518 return g_mime_util.Get().GetPreferredExtensionForMimeType(mime_type, |
897 extension); | 519 extension); |
898 } | 520 } |
899 | 521 |
900 bool IsSupportedImageMimeType(const std::string& mime_type) { | 522 bool IsSupportedImageMimeType(const std::string& mime_type) { |
901 return g_mime_util.Get().IsSupportedImageMimeType(mime_type); | 523 return g_mime_util.Get().IsSupportedImageMimeType(mime_type); |
902 } | 524 } |
903 | 525 |
904 bool IsSupportedMediaMimeType(const std::string& mime_type) { | |
905 return g_mime_util.Get().IsSupportedMediaMimeType(mime_type); | |
906 } | |
907 | |
908 bool IsSupportedNonImageMimeType(const std::string& mime_type) { | 526 bool IsSupportedNonImageMimeType(const std::string& mime_type) { |
909 return g_mime_util.Get().IsSupportedNonImageMimeType(mime_type); | 527 return g_mime_util.Get().IsSupportedNonImageMimeType(mime_type); |
910 } | 528 } |
911 | 529 |
912 bool IsUnsupportedTextMimeType(const std::string& mime_type) { | 530 bool IsUnsupportedTextMimeType(const std::string& mime_type) { |
913 return g_mime_util.Get().IsUnsupportedTextMimeType(mime_type); | 531 return g_mime_util.Get().IsUnsupportedTextMimeType(mime_type); |
914 } | 532 } |
915 | 533 |
916 bool IsSupportedJavascriptMimeType(const std::string& mime_type) { | 534 bool IsSupportedJavascriptMimeType(const std::string& mime_type) { |
917 return g_mime_util.Get().IsSupportedJavascriptMimeType(mime_type); | 535 return g_mime_util.Get().IsSupportedJavascriptMimeType(mime_type); |
(...skipping 12 matching lines...) Expand all Loading... | |
930 std::string* top_level_type, | 548 std::string* top_level_type, |
931 std::string* subtype) { | 549 std::string* subtype) { |
932 return g_mime_util.Get().ParseMimeTypeWithoutParameter( | 550 return g_mime_util.Get().ParseMimeTypeWithoutParameter( |
933 type_string, top_level_type, subtype); | 551 type_string, top_level_type, subtype); |
934 } | 552 } |
935 | 553 |
936 bool IsValidTopLevelMimeType(const std::string& type_string) { | 554 bool IsValidTopLevelMimeType(const std::string& type_string) { |
937 return g_mime_util.Get().IsValidTopLevelMimeType(type_string); | 555 return g_mime_util.Get().IsValidTopLevelMimeType(type_string); |
938 } | 556 } |
939 | 557 |
940 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) { | |
941 return g_mime_util.Get().AreSupportedMediaCodecs(codecs); | |
942 } | |
943 | |
944 bool IsStrictMediaMimeType(const std::string& mime_type) { | |
945 return g_mime_util.Get().IsStrictMediaMimeType(mime_type); | |
946 } | |
947 | |
948 SupportsType IsSupportedStrictMediaMimeType( | |
949 const std::string& mime_type, | |
950 const std::vector<std::string>& codecs) { | |
951 return g_mime_util.Get().IsSupportedStrictMediaMimeType(mime_type, codecs); | |
952 } | |
953 | |
954 void ParseCodecString(const std::string& codecs, | |
955 std::vector<std::string>* codecs_out, | |
956 const bool strip) { | |
957 g_mime_util.Get().ParseCodecString(codecs, codecs_out, strip); | |
958 } | |
959 | |
960 namespace { | 558 namespace { |
961 | 559 |
962 // From http://www.w3schools.com/media/media_mimeref.asp and | 560 // From http://www.w3schools.com/media/media_mimeref.asp and |
963 // http://plugindoc.mozdev.org/winmime.php | 561 // http://plugindoc.mozdev.org/winmime.php |
964 static const char* const kStandardImageTypes[] = { | 562 static const char* const kStandardImageTypes[] = { |
965 "image/bmp", | 563 "image/bmp", |
966 "image/cis-cod", | 564 "image/cis-cod", |
967 "image/gif", | 565 "image/gif", |
968 "image/ief", | 566 "image/ief", |
969 "image/jpeg", | 567 "image/jpeg", |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1132 | 730 |
1133 GetExtensionsFromHardCodedMappings(secondary_mappings, | 731 GetExtensionsFromHardCodedMappings(secondary_mappings, |
1134 arraysize(secondary_mappings), | 732 arraysize(secondary_mappings), |
1135 mime_type, | 733 mime_type, |
1136 &unique_extensions); | 734 &unique_extensions); |
1137 } | 735 } |
1138 | 736 |
1139 HashSetToVector(&unique_extensions, extensions); | 737 HashSetToVector(&unique_extensions, extensions); |
1140 } | 738 } |
1141 | 739 |
1142 void RemoveProprietaryMediaTypesAndCodecsForTests() { | |
1143 g_mime_util.Get().RemoveProprietaryMediaTypesAndCodecsForTests(); | |
1144 } | |
1145 | |
1146 const std::string GetIANAMediaType(const std::string& mime_type) { | 740 const std::string GetIANAMediaType(const std::string& mime_type) { |
1147 for (size_t i = 0; i < arraysize(kIanaMediaTypes); ++i) { | 741 for (size_t i = 0; i < arraysize(kIanaMediaTypes); ++i) { |
1148 if (StartsWithASCII(mime_type, kIanaMediaTypes[i].matcher, true)) { | 742 if (StartsWithASCII(mime_type, kIanaMediaTypes[i].matcher, true)) { |
1149 return kIanaMediaTypes[i].name; | 743 return kIanaMediaTypes[i].name; |
1150 } | 744 } |
1151 } | 745 } |
1152 return std::string(); | 746 return std::string(); |
1153 } | 747 } |
1154 | 748 |
1155 CertificateMimeType GetCertificateMimeTypeForMimeType( | 749 CertificateMimeType GetCertificateMimeTypeForMimeType( |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1188 post_data->append("\r\n" + value + "\r\n"); | 782 post_data->append("\r\n" + value + "\r\n"); |
1189 } | 783 } |
1190 | 784 |
1191 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, | 785 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, |
1192 std::string* post_data) { | 786 std::string* post_data) { |
1193 DCHECK(post_data); | 787 DCHECK(post_data); |
1194 post_data->append("--" + mime_boundary + "--\r\n"); | 788 post_data->append("--" + mime_boundary + "--\r\n"); |
1195 } | 789 } |
1196 | 790 |
1197 } // namespace net | 791 } // namespace net |
OLD | NEW |