| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <map> |
| 5 #include <string> | 6 #include <string> |
| 6 | 7 |
| 7 #include "net/base/mime_util.h" | 8 #include "net/base/mime_util.h" |
| 8 #include "net/base/platform_mime_util.h" | 9 #include "net/base/platform_mime_util.h" |
| 9 | 10 |
| 10 #include "base/hash_tables.h" | 11 #include "base/hash_tables.h" |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 12 #include "base/singleton.h" | 13 #include "base/singleton.h" |
| 13 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 14 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 34 bool IsViewSourceMimeType(const char* mime_type) const; | 35 bool IsViewSourceMimeType(const char* mime_type) const; |
| 35 | 36 |
| 36 bool IsSupportedMimeType(const std::string& mime_type) const; | 37 bool IsSupportedMimeType(const std::string& mime_type) const; |
| 37 | 38 |
| 38 bool MatchesMimeType(const std::string &mime_type_pattern, | 39 bool MatchesMimeType(const std::string &mime_type_pattern, |
| 39 const std::string &mime_type) const; | 40 const std::string &mime_type) const; |
| 40 | 41 |
| 41 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) const; | 42 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) const; |
| 42 | 43 |
| 43 void ParseCodecString(const std::string& codecs, | 44 void ParseCodecString(const std::string& codecs, |
| 44 std::vector<std::string>* codecs_out); | 45 std::vector<std::string>* codecs_out, |
| 46 bool strip); |
| 47 |
| 48 bool IsStrictMediaMimeType(const std::string& mime_type) const; |
| 49 bool IsSupportedStrictMediaMimeType(const std::string& mime_type, |
| 50 const std::vector<std::string>& codecs) const; |
| 45 | 51 |
| 46 private: | 52 private: |
| 47 friend struct DefaultSingletonTraits<MimeUtil>; | 53 friend struct DefaultSingletonTraits<MimeUtil>; |
| 48 MimeUtil() { | 54 MimeUtil() { |
| 49 InitializeMimeTypeMaps(); | 55 InitializeMimeTypeMaps(); |
| 50 } | 56 } |
| 51 | 57 |
| 52 // For faster lookup, keep hash sets. | 58 // For faster lookup, keep hash sets. |
| 53 void InitializeMimeTypeMaps(); | 59 void InitializeMimeTypeMaps(); |
| 54 | 60 |
| 55 typedef base::hash_set<std::string> MimeMappings; | 61 typedef base::hash_set<std::string> MimeMappings; |
| 56 MimeMappings image_map_; | 62 MimeMappings image_map_; |
| 57 MimeMappings media_map_; | 63 MimeMappings media_map_; |
| 58 MimeMappings non_image_map_; | 64 MimeMappings non_image_map_; |
| 59 MimeMappings javascript_map_; | 65 MimeMappings javascript_map_; |
| 60 MimeMappings view_source_map_; | 66 MimeMappings view_source_map_; |
| 61 MimeMappings codecs_map_; | 67 MimeMappings codecs_map_; |
| 68 |
| 69 typedef std::map<std::string, base::hash_set<std::string> > StrictMappings; |
| 70 StrictMappings strict_format_map_; |
| 62 }; // class MimeUtil | 71 }; // class MimeUtil |
| 63 | 72 |
| 64 struct MimeInfo { | 73 struct MimeInfo { |
| 65 const char* mime_type; | 74 const char* mime_type; |
| 66 const char* extensions; // comma separated list | 75 const char* extensions; // comma separated list |
| 67 }; | 76 }; |
| 68 | 77 |
| 69 static const MimeInfo primary_mappings[] = { | 78 static const MimeInfo primary_mappings[] = { |
| 70 { "text/html", "html,htm" }, | 79 { "text/html", "html,htm" }, |
| 71 { "text/css", "css" }, | 80 { "text/css", "css" }, |
| 72 { "text/xml", "xml" }, | 81 { "text/xml", "xml" }, |
| 73 { "image/gif", "gif" }, | 82 { "image/gif", "gif" }, |
| 74 { "image/jpeg", "jpeg,jpg" }, | 83 { "image/jpeg", "jpeg,jpg" }, |
| 75 { "image/png", "png" }, | 84 { "image/png", "png" }, |
| 76 { "video/mp4", "mp4,m4v" }, | 85 { "video/mp4", "mp4,m4v" }, |
| 77 { "audio/x-m4a", "m4a" }, | 86 { "audio/x-m4a", "m4a" }, |
| 78 { "audio/mp3", "mp3" }, | 87 { "audio/mp3", "mp3" }, |
| 79 { "video/ogg", "ogv,ogm" }, | 88 { "video/ogg", "ogv,ogm" }, |
| 80 { "audio/ogg", "ogg,oga" }, | 89 { "audio/ogg", "ogg,oga" }, |
| 90 { "video/webm", "webm" }, |
| 91 { "audio/webm", "webm" }, |
| 81 { "application/xhtml+xml", "xhtml,xht" }, | 92 { "application/xhtml+xml", "xhtml,xht" }, |
| 82 { "application/x-chrome-extension", "crx" } | 93 { "application/x-chrome-extension", "crx" } |
| 83 }; | 94 }; |
| 84 | 95 |
| 85 static const MimeInfo secondary_mappings[] = { | 96 static const MimeInfo secondary_mappings[] = { |
| 86 { "application/octet-stream", "exe,com,bin" }, | 97 { "application/octet-stream", "exe,com,bin" }, |
| 87 { "application/gzip", "gz" }, | 98 { "application/gzip", "gz" }, |
| 88 { "application/pdf", "pdf" }, | 99 { "application/pdf", "pdf" }, |
| 89 { "application/postscript", "ps,eps,ai" }, | 100 { "application/postscript", "ps,eps,ai" }, |
| 90 { "application/x-javascript", "js" }, | 101 { "application/x-javascript", "js" }, |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 "image/x-xbitmap" // xbm | 192 "image/x-xbitmap" // xbm |
| 182 }; | 193 }; |
| 183 | 194 |
| 184 // A list of media types: http://en.wikipedia.org/wiki/Internet_media_type | 195 // A list of media types: http://en.wikipedia.org/wiki/Internet_media_type |
| 185 // A comprehensive mime type list: http://plugindoc.mozdev.org/winmime.php | 196 // A comprehensive mime type list: http://plugindoc.mozdev.org/winmime.php |
| 186 static const char* const supported_media_types[] = { | 197 static const char* const supported_media_types[] = { |
| 187 // Ogg. | 198 // Ogg. |
| 188 "video/ogg", | 199 "video/ogg", |
| 189 "audio/ogg", | 200 "audio/ogg", |
| 190 "application/ogg", | 201 "application/ogg", |
| 202 "video/webm", |
| 203 "audio/webm", |
| 191 | 204 |
| 192 #if defined(GOOGLE_CHROME_BUILD) || defined(USE_PROPRIETARY_CODECS) | 205 #if defined(GOOGLE_CHROME_BUILD) || defined(USE_PROPRIETARY_CODECS) |
| 193 // MPEG-4. | 206 // MPEG-4. |
| 194 "video/mp4", | 207 "video/mp4", |
| 195 "video/x-m4v", | 208 "video/x-m4v", |
| 196 "audio/mp4", | 209 "audio/mp4", |
| 197 "audio/x-m4a", | 210 "audio/x-m4a", |
| 198 | 211 |
| 199 // MP3. | 212 // MP3. |
| 200 "audio/mp3", | 213 "audio/mp3", |
| 201 "audio/x-mp3", | 214 "audio/x-mp3", |
| 202 "audio/mpeg", | 215 "audio/mpeg", |
| 203 #endif | 216 #endif |
| 204 }; | 217 }; |
| 205 | 218 |
| 206 // List of supported codecs when passed in with <source type="...">. | 219 // List of supported codecs when passed in with <source type="...">. |
| 207 // | 220 // |
| 208 // Refer to http://wiki.whatwg.org/wiki/Video_type_parameters#Browser_Support | 221 // Refer to http://wiki.whatwg.org/wiki/Video_type_parameters#Browser_Support |
| 209 // for more information. | 222 // for more information. |
| 210 static const char* const supported_media_codecs[] = { | 223 static const char* const supported_media_codecs[] = { |
| 211 #if defined(GOOGLE_CHROME_BUILD) || defined(USE_PROPRIETARY_CODECS) | 224 #if defined(GOOGLE_CHROME_BUILD) || defined(USE_PROPRIETARY_CODECS) |
| 212 "avc1", | 225 "avc1", |
| 213 "mp4a", | 226 "mp4a", |
| 214 #endif | 227 #endif |
| 215 "theora", | 228 "theora", |
| 216 "vorbis", | 229 "vorbis", |
| 230 "vp8" |
| 217 }; | 231 }; |
| 218 | 232 |
| 219 // Note: does not include javascript types list (see supported_javascript_types) | 233 // Note: does not include javascript types list (see supported_javascript_types) |
| 220 static const char* const supported_non_image_types[] = { | 234 static const char* const supported_non_image_types[] = { |
| 221 "text/cache-manifest", | 235 "text/cache-manifest", |
| 222 "text/html", | 236 "text/html", |
| 223 "text/xml", | 237 "text/xml", |
| 224 "text/xsl", | 238 "text/xsl", |
| 225 "text/plain", | 239 "text/plain", |
| 226 // Many users complained about css files served for | 240 // Many users complained about css files served for |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 | 284 |
| 271 static const char* const view_source_types[] = { | 285 static const char* const view_source_types[] = { |
| 272 "text/xml", | 286 "text/xml", |
| 273 "text/xsl", | 287 "text/xsl", |
| 274 "application/xml", | 288 "application/xml", |
| 275 "application/rss+xml", | 289 "application/rss+xml", |
| 276 "application/atom+xml", | 290 "application/atom+xml", |
| 277 "image/svg+xml" | 291 "image/svg+xml" |
| 278 }; | 292 }; |
| 279 | 293 |
| 294 struct MediaFormatStrict { |
| 295 const char* mime_type; |
| 296 const char* codecs_list; |
| 297 }; |
| 298 |
| 299 static const MediaFormatStrict format_codec_mappings[] = { |
| 300 { "video/webm", "vorbis,vp8,vp8.0" }, |
| 301 { "audio/webm", "vorbis" } |
| 302 }; |
| 303 |
| 280 void MimeUtil::InitializeMimeTypeMaps() { | 304 void MimeUtil::InitializeMimeTypeMaps() { |
| 281 for (size_t i = 0; i < arraysize(supported_image_types); ++i) | 305 for (size_t i = 0; i < arraysize(supported_image_types); ++i) |
| 282 image_map_.insert(supported_image_types[i]); | 306 image_map_.insert(supported_image_types[i]); |
| 283 | 307 |
| 284 // Initialize the supported non-image types. | 308 // Initialize the supported non-image types. |
| 285 for (size_t i = 0; i < arraysize(supported_non_image_types); ++i) | 309 for (size_t i = 0; i < arraysize(supported_non_image_types); ++i) |
| 286 non_image_map_.insert(supported_non_image_types[i]); | 310 non_image_map_.insert(supported_non_image_types[i]); |
| 287 for (size_t i = 0; i < arraysize(supported_javascript_types); ++i) | 311 for (size_t i = 0; i < arraysize(supported_javascript_types); ++i) |
| 288 non_image_map_.insert(supported_javascript_types[i]); | 312 non_image_map_.insert(supported_javascript_types[i]); |
| 289 for (size_t i = 0; i < arraysize(supported_media_types); ++i) | 313 for (size_t i = 0; i < arraysize(supported_media_types); ++i) |
| 290 non_image_map_.insert(supported_media_types[i]); | 314 non_image_map_.insert(supported_media_types[i]); |
| 291 | 315 |
| 292 // Initialize the supported media types. | 316 // Initialize the supported media types. |
| 293 for (size_t i = 0; i < arraysize(supported_media_types); ++i) | 317 for (size_t i = 0; i < arraysize(supported_media_types); ++i) |
| 294 media_map_.insert(supported_media_types[i]); | 318 media_map_.insert(supported_media_types[i]); |
| 295 | 319 |
| 296 for (size_t i = 0; i < arraysize(supported_javascript_types); ++i) | 320 for (size_t i = 0; i < arraysize(supported_javascript_types); ++i) |
| 297 javascript_map_.insert(supported_javascript_types[i]); | 321 javascript_map_.insert(supported_javascript_types[i]); |
| 298 | 322 |
| 299 for (size_t i = 0; i < arraysize(view_source_types); ++i) | 323 for (size_t i = 0; i < arraysize(view_source_types); ++i) |
| 300 view_source_map_.insert(view_source_types[i]); | 324 view_source_map_.insert(view_source_types[i]); |
| 301 | 325 |
| 302 for (size_t i = 0; i < arraysize(supported_media_codecs); ++i) | 326 for (size_t i = 0; i < arraysize(supported_media_codecs); ++i) |
| 303 codecs_map_.insert(supported_media_codecs[i]); | 327 codecs_map_.insert(supported_media_codecs[i]); |
| 328 |
| 329 // Initialize the strict supported media types. |
| 330 for (size_t i = 0; i < arraysize(format_codec_mappings); ++i) { |
| 331 std::vector<std::string> mime_type_codecs; |
| 332 ParseCodecString(format_codec_mappings[i].codecs_list, |
| 333 &mime_type_codecs, |
| 334 false); |
| 335 |
| 336 MimeMappings codecs; |
| 337 for (size_t j = 0; j < mime_type_codecs.size(); ++j) |
| 338 codecs.insert(mime_type_codecs[j]); |
| 339 strict_format_map_[format_codec_mappings[i].mime_type] = codecs; |
| 340 } |
| 304 } | 341 } |
| 305 | 342 |
| 306 bool MimeUtil::IsSupportedImageMimeType(const char* mime_type) const { | 343 bool MimeUtil::IsSupportedImageMimeType(const char* mime_type) const { |
| 307 return image_map_.find(mime_type) != image_map_.end(); | 344 return image_map_.find(mime_type) != image_map_.end(); |
| 308 } | 345 } |
| 309 | 346 |
| 310 bool MimeUtil::IsSupportedMediaMimeType(const char* mime_type) const { | 347 bool MimeUtil::IsSupportedMediaMimeType(const char* mime_type) const { |
| 311 return media_map_.find(mime_type) != media_map_.end(); | 348 return media_map_.find(mime_type) != media_map_.end(); |
| 312 } | 349 } |
| 313 | 350 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 371 const std::vector<std::string>& codecs) const { | 408 const std::vector<std::string>& codecs) const { |
| 372 for (size_t i = 0; i < codecs.size(); ++i) { | 409 for (size_t i = 0; i < codecs.size(); ++i) { |
| 373 if (codecs_map_.find(codecs[i]) == codecs_map_.end()) { | 410 if (codecs_map_.find(codecs[i]) == codecs_map_.end()) { |
| 374 return false; | 411 return false; |
| 375 } | 412 } |
| 376 } | 413 } |
| 377 return true; | 414 return true; |
| 378 } | 415 } |
| 379 | 416 |
| 380 void MimeUtil::ParseCodecString(const std::string& codecs, | 417 void MimeUtil::ParseCodecString(const std::string& codecs, |
| 381 std::vector<std::string>* codecs_out) { | 418 std::vector<std::string>* codecs_out, |
| 419 bool strip) { |
| 382 std::string no_quote_codecs; | 420 std::string no_quote_codecs; |
| 383 TrimString(codecs, "\"", &no_quote_codecs); | 421 TrimString(codecs, "\"", &no_quote_codecs); |
| 384 SplitString(no_quote_codecs, ',', codecs_out); | 422 SplitString(no_quote_codecs, ',', codecs_out); |
| 385 | 423 |
| 386 // Truncate each string at the '.' | 424 if (!strip) |
| 425 return; |
| 426 |
| 427 // Strip everything past the first '.' |
| 387 for (std::vector<std::string>::iterator it = codecs_out->begin(); | 428 for (std::vector<std::string>::iterator it = codecs_out->begin(); |
| 388 it != codecs_out->end(); | 429 it != codecs_out->end(); |
| 389 ++it) { | 430 ++it) { |
| 390 size_t found = it->find_first_of('.'); | 431 size_t found = it->find_first_of('.'); |
| 391 if (found != std::string::npos) | 432 if (found != std::string::npos) |
| 392 it->resize(found); | 433 it->resize(found); |
| 393 } | 434 } |
| 394 } | 435 } |
| 395 | 436 |
| 437 bool MimeUtil::IsStrictMediaMimeType(const std::string& mime_type) const { |
| 438 if (strict_format_map_.find(mime_type) == strict_format_map_.end()) |
| 439 return false; |
| 440 return true; |
| 441 } |
| 442 |
| 443 bool MimeUtil::IsSupportedStrictMediaMimeType(const std::string& mime_type, |
| 444 const std::vector<std::string>& codecs) const { |
| 445 StrictMappings::const_iterator it = strict_format_map_.find(mime_type); |
| 446 |
| 447 if (it == strict_format_map_.end()) |
| 448 return false; |
| 449 |
| 450 const MimeMappings strict_codecs_map = it->second; |
| 451 for (size_t i = 0; i < codecs.size(); ++i) { |
| 452 if (strict_codecs_map.find(codecs[i]) == strict_codecs_map.end()) { |
| 453 return false; |
| 454 } |
| 455 } |
| 456 return true; |
| 457 } |
| 458 |
| 396 //---------------------------------------------------------------------------- | 459 //---------------------------------------------------------------------------- |
| 397 // Wrappers for the singleton | 460 // Wrappers for the singleton |
| 398 //---------------------------------------------------------------------------- | 461 //---------------------------------------------------------------------------- |
| 399 | 462 |
| 400 static MimeUtil* GetMimeUtil() { | 463 static MimeUtil* GetMimeUtil() { |
| 401 return Singleton<MimeUtil>::get(); | 464 return Singleton<MimeUtil>::get(); |
| 402 } | 465 } |
| 403 | 466 |
| 404 bool GetMimeTypeFromExtension(const FilePath::StringType& ext, | 467 bool GetMimeTypeFromExtension(const FilePath::StringType& ext, |
| 405 std::string* mime_type) { | 468 std::string* mime_type) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 441 | 504 |
| 442 bool MatchesMimeType(const std::string &mime_type_pattern, | 505 bool MatchesMimeType(const std::string &mime_type_pattern, |
| 443 const std::string &mime_type) { | 506 const std::string &mime_type) { |
| 444 return GetMimeUtil()->MatchesMimeType(mime_type_pattern, mime_type); | 507 return GetMimeUtil()->MatchesMimeType(mime_type_pattern, mime_type); |
| 445 } | 508 } |
| 446 | 509 |
| 447 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) { | 510 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) { |
| 448 return GetMimeUtil()->AreSupportedMediaCodecs(codecs); | 511 return GetMimeUtil()->AreSupportedMediaCodecs(codecs); |
| 449 } | 512 } |
| 450 | 513 |
| 514 bool IsStrictMediaMimeType(const std::string& mime_type) { |
| 515 return GetMimeUtil()->IsStrictMediaMimeType(mime_type); |
| 516 } |
| 517 |
| 518 bool IsSupportedStrictMediaMimeType(const std::string& mime_type, |
| 519 const std::vector<std::string>& codecs) { |
| 520 return GetMimeUtil()->IsSupportedStrictMediaMimeType(mime_type, codecs); |
| 521 } |
| 522 |
| 451 void ParseCodecString(const std::string& codecs, | 523 void ParseCodecString(const std::string& codecs, |
| 452 std::vector<std::string>* codecs_out) { | 524 std::vector<std::string>* codecs_out, |
| 453 GetMimeUtil()->ParseCodecString(codecs, codecs_out); | 525 const bool strip) { |
| 526 GetMimeUtil()->ParseCodecString(codecs, codecs_out, strip); |
| 454 } | 527 } |
| 455 | 528 |
| 456 } // namespace net | 529 } // namespace net |
| OLD | NEW |