Bug 1314858 - Refactor multiple callers of HasPluginForAPI into a helper. r=gerald draft
authorChris Pearce <cpearce@mozilla.com>
Thu, 03 Nov 2016 14:33:31 +1300
changeset 433503 10774a329e3dd3b80468986d83a632b6162a24f5
parent 433502 fc6b582bb302b8c9aa26d055c6a7297e740a4e54
child 535912 b32e012391a035b6a598f2d0ebd40c7cf0ed7725
push id34604
push usercpearce@mozilla.com
push dateThu, 03 Nov 2016 21:15:09 +0000
reviewersgerald
bugs1314858
milestone52.0a1
Bug 1314858 - Refactor multiple callers of HasPluginForAPI into a helper. r=gerald I've repeated myself a few times, so make a helper to make determining which GMPs are available easier. MozReview-Commit-ID: 2fFLeaA5o8u
dom/media/eme/MediaKeySystemAccess.cpp
dom/media/gmp/GMPUtils.cpp
dom/media/gmp/GMPUtils.h
dom/media/platforms/agnostic/gmp/GMPDecoderModule.cpp
--- a/dom/media/eme/MediaKeySystemAccess.cpp
+++ b/dom/media/eme/MediaKeySystemAccess.cpp
@@ -93,46 +93,20 @@ MediaKeySystemAccess::CreateMediaKeys(Er
 {
   RefPtr<MediaKeys> keys(new MediaKeys(mParent,
                                        mKeySystem,
                                        mConfig));
   return keys->Init(aRv);
 }
 
 static bool
-HaveGMPFor(mozIGeckoMediaPluginService* aGMPService,
-           const nsCString& aKeySystem,
-           const nsCString& aAPI,
-           const nsCString& aTag = EmptyCString())
-{
-  nsTArray<nsCString> tags;
-  tags.AppendElement(aKeySystem);
-  if (!aTag.IsEmpty()) {
-    tags.AppendElement(aTag);
-  }
-  bool hasPlugin = false;
-  if (NS_FAILED(aGMPService->HasPluginForAPI(aAPI,
-                                             &tags,
-                                             &hasPlugin))) {
-    return false;
-  }
-  return hasPlugin;
-}
-
-static bool
 HavePluginForKeySystem(const nsCString& aKeySystem)
 {
-  bool havePlugin = false;
-  nsCOMPtr<mozIGeckoMediaPluginService> mps =
-    do_GetService("@mozilla.org/gecko-media-plugin-service;1");
-  if (mps) {
-    havePlugin = HaveGMPFor(mps,
-                            aKeySystem,
-                            NS_LITERAL_CSTRING(GMP_API_DECRYPTOR));
-  }
+  bool havePlugin = HaveGMPFor(NS_LITERAL_CSTRING(GMP_API_DECRYPTOR),
+                               { aKeySystem });
 #ifdef MOZ_WIDGET_ANDROID
   // Check if we can use MediaDrm for this keysystem.
   if (!havePlugin) {
     havePlugin = mozilla::java::MediaDrmProxy::IsSchemeSupported(aKeySystem);
   }
 #endif
   return havePlugin;
 }
--- a/dom/media/gmp/GMPUtils.cpp
+++ b/dom/media/gmp/GMPUtils.cpp
@@ -203,9 +203,27 @@ GMPInfoFileParser::Get(const nsCString& 
   ToLowerCase(key);
   nsCString* p = nullptr;
   if (mValues.Get(key, &p)) {
     return nsCString(*p);
   }
   return EmptyCString();
 }
 
+bool
+HaveGMPFor(const nsCString& aAPI,
+           nsTArray<nsCString>&& aTags)
+{
+  nsCOMPtr<mozIGeckoMediaPluginService> mps =
+    do_GetService("@mozilla.org/gecko-media-plugin-service;1");
+  if (NS_WARN_IF(!mps)) {
+    return false;
+  }
+
+  bool hasPlugin = false;
+  if (NS_FAILED(mps->HasPluginForAPI(aAPI, &aTags, &hasPlugin))) {
+    return false;
+  }
+  return hasPlugin;
+}
+
+
 } // namespace mozilla
--- a/dom/media/gmp/GMPUtils.h
+++ b/dom/media/gmp/GMPUtils.h
@@ -75,11 +75,15 @@ ReadIntoArray(nsIFile* aFile,
               nsTArray<uint8_t>& aOutDst,
               size_t aMaxLength);
 
 bool
 ReadIntoString(nsIFile* aFile,
                nsCString& aOutDst,
                size_t aMaxLength);
 
+bool
+HaveGMPFor(const nsCString& aAPI,
+           nsTArray<nsCString>&& aTags);
+
 } // namespace mozilla
 
 #endif
--- a/dom/media/platforms/agnostic/gmp/GMPDecoderModule.cpp
+++ b/dom/media/platforms/agnostic/gmp/GMPDecoderModule.cpp
@@ -3,16 +3,17 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "GMPDecoderModule.h"
 #include "DecoderDoctorDiagnostics.h"
 #include "GMPAudioDecoder.h"
 #include "GMPVideoDecoder.h"
+#include "GMPUtils.h"
 #include "MediaDataDecoderProxy.h"
 #include "MediaPrefs.h"
 #include "VideoUtils.h"
 #include "mozIGeckoMediaPluginService.h"
 #include "nsServiceManagerUtils.h"
 #include "mozilla/StaticMutex.h"
 #include "gmp-audio-decode.h"
 #include "gmp-video-decode.h"
@@ -95,36 +96,16 @@ GMPDecoderModule::DecoderNeedsConversion
   // GMPVideoCodecType::kGMPVideoCodecH264 specifies that encoded frames must be in AVCC format.
   if (aConfig.IsVideo() && MP4Decoder::IsH264(aConfig.mMimeType)) {
     return ConversionRequired::kNeedAVCC;
   } else {
     return ConversionRequired::kNeedNone;
   }
 }
 
-static bool
-HasGMPFor(const nsACString& aAPI,
-          const nsACString& aCodec,
-          const nsACString& aGMP)
-{
-  nsTArray<nsCString> tags;
-  tags.AppendElement(aCodec);
-  tags.AppendElement(aGMP);
-  nsCOMPtr<mozIGeckoMediaPluginService> mps =
-    do_GetService("@mozilla.org/gecko-media-plugin-service;1");
-  if (NS_WARN_IF(!mps)) {
-    return false;
-  }
-  bool hasPlugin = false;
-  if (NS_FAILED(mps->HasPluginForAPI(aAPI, &tags, &hasPlugin))) {
-    return false;
-  }
-  return hasPlugin;
-}
-
 /* static */
 const Maybe<nsCString>
 GMPDecoderModule::PreferredGMP(const nsACString& aMimeType)
 {
   Maybe<nsCString> rv;
   if (aMimeType.EqualsLiteral("audio/mp4a-latm")) {
     switch (MediaPrefs::GMPAACPreferred()) {
       case 1: rv.emplace(kEMEKeySystemClearkey); break;
@@ -149,37 +130,33 @@ bool
 GMPDecoderModule::SupportsMimeType(const nsACString& aMimeType,
                                    const Maybe<nsCString>& aGMP)
 {
   if (aGMP.isNothing()) {
     return false;
   }
 
   if (MP4Decoder::IsH264(aMimeType)) {
-    return HasGMPFor(NS_LITERAL_CSTRING(GMP_API_VIDEO_DECODER),
-                     NS_LITERAL_CSTRING("h264"),
-                     aGMP.value());
+    return HaveGMPFor(NS_LITERAL_CSTRING(GMP_API_VIDEO_DECODER),
+                      { NS_LITERAL_CSTRING("h264"), aGMP.value()});
   }
 
   if (VPXDecoder::IsVP9(aMimeType)) {
-    return HasGMPFor(NS_LITERAL_CSTRING(GMP_API_VIDEO_DECODER),
-                     NS_LITERAL_CSTRING("vp9"),
-                     aGMP.value());
+    return HaveGMPFor(NS_LITERAL_CSTRING(GMP_API_VIDEO_DECODER),
+                      { NS_LITERAL_CSTRING("vp9"), aGMP.value()});
   }
 
   if (VPXDecoder::IsVP8(aMimeType)) {
-    return HasGMPFor(NS_LITERAL_CSTRING(GMP_API_VIDEO_DECODER),
-                     NS_LITERAL_CSTRING("vp8"),
-                     aGMP.value());
+    return HaveGMPFor(NS_LITERAL_CSTRING(GMP_API_VIDEO_DECODER),
+                      { NS_LITERAL_CSTRING("vp8"), aGMP.value()});
   }
 
   if (MP4Decoder::IsAAC(aMimeType)) {
-    return HasGMPFor(NS_LITERAL_CSTRING(GMP_API_AUDIO_DECODER),
-                     NS_LITERAL_CSTRING("aac"),
-                     aGMP.value());
+    return HaveGMPFor(NS_LITERAL_CSTRING(GMP_API_AUDIO_DECODER),
+                      { NS_LITERAL_CSTRING("aac"), aGMP.value()});
   }
 
   return false;
 }
 
 bool
 GMPDecoderModule::SupportsMimeType(const nsACString& aMimeType,
                                    DecoderDoctorDiagnostics* aDiagnostics) const