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
--- 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".
};