Bug 1329568 - MediaMIMEType 'audio/' and 'video/' checks - r?jya draft
authorGerald Squelart <gsquelart@mozilla.com>
Sat, 17 Dec 2016 17:43:02 +1100
changeset 460521 f992ece1d80d17130a671183855206ae038c2905
parent 460520 d8bc3b5b84944fed849657878bc4d2a44262bb2e
child 460522 0af34d201b9e33dcde63f4f678127de873ec6a6a
push id41406
push usergsquelart@mozilla.com
push dateFri, 13 Jan 2017 06:23:59 +0000
reviewersjya
bugs1329568
milestone53.0a1
Bug 1329568 - MediaMIMEType 'audio/' and 'video/' checks - r?jya A lot of code wants to check if the type starts with 'audio/' or 'video/', MediaMIMEType::IsAudio() and IsVideo() will help with that -- and could later be optimized if needed. Note that types starting with 'application/' will still need manual testing, but they are rare anyway. MozReview-Commit-ID: UBcxS69Hcb
dom/media/MediaMIMETypes.cpp
dom/media/MediaMIMETypes.h
--- a/dom/media/MediaMIMETypes.cpp
+++ b/dom/media/MediaMIMETypes.cpp
@@ -5,16 +5,44 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "MediaMIMETypes.h"
 
 #include "nsContentTypeParser.h"
 
 namespace mozilla {
 
+template <int N>
+static bool
+StartsWith(const nsACString& string, const char (&prefix)[N])
+{
+    if (N - 1 > string.Length()) {
+      return false;
+    }
+    return memcmp(string.Data(), prefix, N - 1) == 0;
+}
+
+bool
+MediaMIMEType::HasApplicationMajorType() const
+{
+  return StartsWith(mMIMEType, "application/");
+}
+
+bool
+MediaMIMEType::HasAudioMajorType() const
+{
+  return StartsWith(mMIMEType, "audio/");
+}
+
+bool
+MediaMIMEType::HasVideoMajorType() const
+{
+  return StartsWith(mMIMEType, "video/");
+}
+
 MediaMIMEType::MediaMIMEType(const nsACString& aType)
   : mMIMEType(aType)
 {
 }
 
 Maybe<MediaMIMEType>
 MakeMediaMIMEType(const nsAString& aType)
 {
--- a/dom/media/MediaMIMETypes.h
+++ b/dom/media/MediaMIMETypes.h
@@ -71,16 +71,26 @@ public:
   {
     return mMIMEType.Equals(aOther.mMIMEType);
   }
   bool operator!=(const MediaMIMEType& aOther) const
   {
     return !mMIMEType.Equals(aOther.mMIMEType);
   }
 
+  // True if type starts with "application/".
+  bool HasApplicationMajorType() const;
+  // True if type starts with "audio/".
+  // Note that some audio content could be stored in a "video/..." container!
+  bool HasAudioMajorType() const;
+  // True if type starts with "video/".
+  // Note that this does not guarantee 100% that the content is actually video!
+  // (e.g., "video/webm" could contain a vorbis audio track.)
+  bool HasVideoMajorType() const;
+
 private:
   friend Maybe<MediaMIMEType> MakeMediaMIMEType(const nsAString& aType);
   friend class MediaExtendedMIMEType;
   explicit MediaMIMEType(const nsACString& aType);
 
   nsCString mMIMEType; // UTF8 MIME "type/subtype".
 };