Bug 1329568 - MediaMIMEType comparisons against others - r?jya draft
authorGerald Squelart <gsquelart@mozilla.com>
Sun, 01 Jan 2017 10:06:26 +1100
changeset 460520 d8bc3b5b84944fed849657878bc4d2a44262bb2e
parent 460519 e91da7a55bd4b0b1416ead92cdb3d0c9bcd865da
child 460521 f992ece1d80d17130a671183855206ae038c2905
push id41406
push usergsquelart@mozilla.com
push dateFri, 13 Jan 2017 06:23:59 +0000
reviewersjya
bugs1329568
milestone53.0a1
Bug 1329568 - MediaMIMEType comparisons against others - r?jya `==` and `!=` against other MediaMIMEType objects, and against MEDIAMIMETYPE checked literals. This will allow simple (and compile-time-checked!) tests like: if (contentType.Type() == MEDIAMIMETYPE("audio/mp4")) { ... MozReview-Commit-ID: 5yMua5krOKD
dom/media/MediaMIMETypes.h
--- a/dom/media/MediaMIMETypes.h
+++ b/dom/media/MediaMIMETypes.h
@@ -10,17 +10,17 @@
 #include "mozilla/Maybe.h"
 #include "nsString.h"
 
 namespace mozilla {
 
 // Class containing pointing at a media MIME "type/subtype" string literal.
 // See IsMediaMIMEType for restrictions.
 // Mainly used to help construct a MediaMIMEType through the statically-checked
-// MEDIAMIMETYPE macro.
+// MEDIAMIMETYPE macro, or to compare a MediaMIMEType to a literal.
 class DependentMediaMIMEType
 {
 public:
   // Construction from a literal. Checked in debug builds.
   // Use MEDIAMIMETYPE macro instead, for static checking.
   template <size_t N>
   explicit DependentMediaMIMEType(const char (&aType)[N])
     : mMIMEType(aType, N - 1)
@@ -51,16 +51,36 @@ public:
   // Implicit so MEDIAMIMETYPE can be used wherever a MediaMIMEType is expected.
   MOZ_IMPLICIT MediaMIMEType(const DependentMediaMIMEType& aType)
     : mMIMEType(aType.AsDependentString())
   {}
 
   // MIME "type/subtype", always lowercase.
   const nsACString& AsString() const { return mMIMEType; }
 
+  // Comparison with DependentMediaMIMEType.
+  // Useful to compare to MEDIAMIMETYPE literals.
+  bool operator==(const DependentMediaMIMEType& aOther) const
+  {
+    return mMIMEType.Equals(aOther.AsDependentString());
+  }
+  bool operator!=(const DependentMediaMIMEType& aOther) const
+  {
+    return !mMIMEType.Equals(aOther.AsDependentString());
+  }
+
+  bool operator==(const MediaMIMEType& aOther) const
+  {
+    return mMIMEType.Equals(aOther.mMIMEType);
+  }
+  bool operator!=(const MediaMIMEType& aOther) const
+  {
+    return !mMIMEType.Equals(aOther.mMIMEType);
+  }
+
 private:
   friend Maybe<MediaMIMEType> MakeMediaMIMEType(const nsAString& aType);
   friend class MediaExtendedMIMEType;
   explicit MediaMIMEType(const nsACString& aType);
 
   nsCString mMIMEType; // UTF8 MIME "type/subtype".
 };