Bug 1346679 - Label runnables in dom/media/gmp/GMPCDMCallbackProxy.cpp. r?cpearce draft
authorJW Wang <jwwang@mozilla.com>
Fri, 10 Mar 2017 16:07:04 +0800
changeset 497255 6a1ba18bca51b18ba08957ebbef87c0ac2df01ac
parent 497254 05b1855db21c755192ead612edce6ae66a861880
child 497257 9dac5e430681c9e807f5d05c5245a215de6b3b4a
push id48849
push userjwwang@mozilla.com
push dateMon, 13 Mar 2017 03:59:56 +0000
reviewerscpearce
bugs1346679
milestone55.0a1
Bug 1346679 - Label runnables in dom/media/gmp/GMPCDMCallbackProxy.cpp. r?cpearce MozReview-Commit-ID: 1h2SaxHf0F
dom/media/gmp/GMPCDMCallbackProxy.cpp
dom/media/gmp/GMPCDMCallbackProxy.h
dom/media/gmp/GMPCDMProxy.cpp
--- a/dom/media/gmp/GMPCDMCallbackProxy.cpp
+++ b/dom/media/gmp/GMPCDMCallbackProxy.cpp
@@ -12,63 +12,69 @@
 #include "mozIGeckoMediaPluginService.h"
 #include "nsContentCID.h"
 #include "nsServiceManagerUtils.h"
 #include "MainThreadUtils.h"
 #include "mozilla/EMEUtils.h"
 
 namespace mozilla {
 
-GMPCDMCallbackProxy::GMPCDMCallbackProxy(CDMProxy* aProxy)
+GMPCDMCallbackProxy::GMPCDMCallbackProxy(CDMProxy* aProxy,
+                                         nsIEventTarget* aMainThread)
   : mProxy(aProxy)
+  , mMainThread(aMainThread)
 {}
 
 void
 GMPCDMCallbackProxy::SetDecryptorId(uint32_t aId)
 {
   MOZ_ASSERT(mProxy->IsOnOwnerThread());
 
   RefPtr<CDMProxy> proxy = mProxy;
-  NS_DispatchToMainThread(
+  mMainThread->Dispatch(
     NS_NewRunnableFunction([proxy, aId] ()
     {
       proxy->OnSetDecryptorId(aId);
-    })
-  );}
+    }),
+    NS_DISPATCH_NORMAL
+  );
+}
 
 void
 GMPCDMCallbackProxy::SetSessionId(uint32_t aToken,
                                   const nsCString& aSessionId)
 {
   MOZ_ASSERT(mProxy->IsOnOwnerThread());
 
   RefPtr<CDMProxy> proxy = mProxy;
   auto sid = NS_ConvertUTF8toUTF16(aSessionId);
-  NS_DispatchToMainThread(
+  mMainThread->Dispatch(
     NS_NewRunnableFunction([proxy,
                             aToken,
                             sid] ()
     {
       proxy->OnSetSessionId(aToken, sid);
-    })
+    }),
+    NS_DISPATCH_NORMAL
   );
 }
 
 void
 GMPCDMCallbackProxy::ResolveLoadSessionPromise(uint32_t aPromiseId,
                                                bool aSuccess)
 {
   MOZ_ASSERT(mProxy->IsOnOwnerThread());
 
   RefPtr<CDMProxy> proxy = mProxy;
-  NS_DispatchToMainThread(
+  mMainThread->Dispatch(
     NS_NewRunnableFunction([proxy, aPromiseId, aSuccess] ()
     {
       proxy->OnResolveLoadSessionPromise(aPromiseId, aSuccess);
-    })
+    }),
+    NS_DISPATCH_NORMAL
   );
 }
 
 void
 GMPCDMCallbackProxy::ResolvePromise(uint32_t aPromiseId)
 {
   MOZ_ASSERT(mProxy->IsOnOwnerThread());
 
@@ -79,119 +85,125 @@ GMPCDMCallbackProxy::ResolvePromise(uint
 void
 GMPCDMCallbackProxy::RejectPromise(uint32_t aPromiseId,
                                    nsresult aException,
                                    const nsCString& aMessage)
 {
   MOZ_ASSERT(mProxy->IsOnOwnerThread());
 
   RefPtr<CDMProxy> proxy = mProxy;
-  NS_DispatchToMainThread(
+  mMainThread->Dispatch(
     NS_NewRunnableFunction([proxy,
                             aPromiseId,
                             aException,
                             aMessage] ()
     {
       proxy->OnRejectPromise(aPromiseId, aException, aMessage);
-    })
+    }),
+    NS_DISPATCH_NORMAL
   );
 }
 
 void
 GMPCDMCallbackProxy::SessionMessage(const nsCString& aSessionId,
                                     dom::MediaKeyMessageType aMessageType,
                                     const nsTArray<uint8_t>& aMessage)
 {
   MOZ_ASSERT(mProxy->IsOnOwnerThread());
 
   RefPtr<CDMProxy> proxy = mProxy;
   auto sid = NS_ConvertUTF8toUTF16(aSessionId);
   nsTArray<uint8_t> msg(aMessage);
-  NS_DispatchToMainThread(
+  mMainThread->Dispatch(
     NS_NewRunnableFunction([proxy,
                             sid,
                             aMessageType,
                             msg] () mutable
     {
       proxy->OnSessionMessage(sid, aMessageType, msg);
-    })
+    }),
+    NS_DISPATCH_NORMAL
   );
 }
 
 void
 GMPCDMCallbackProxy::ExpirationChange(const nsCString& aSessionId,
                                       GMPTimestamp aExpiryTime)
 {
   MOZ_ASSERT(mProxy->IsOnOwnerThread());
 
   RefPtr<CDMProxy> proxy = mProxy;
   auto sid = NS_ConvertUTF8toUTF16(aSessionId);
-  NS_DispatchToMainThread(
+  mMainThread->Dispatch(
     NS_NewRunnableFunction([proxy,
                             sid,
                             aExpiryTime] ()
     {
       proxy->OnExpirationChange(sid, aExpiryTime);
-    })
+    }),
+    NS_DISPATCH_NORMAL
   );
 }
 
 void
 GMPCDMCallbackProxy::SessionClosed(const nsCString& aSessionId)
 {
   MOZ_ASSERT(mProxy->IsOnOwnerThread());
 
   bool keyStatusesChange = false;
   auto sid = NS_ConvertUTF8toUTF16(aSessionId);
   {
     CDMCaps::AutoLock caps(mProxy->Capabilites());
     keyStatusesChange = caps.RemoveKeysForSession(NS_ConvertUTF8toUTF16(aSessionId));
   }
   if (keyStatusesChange) {
     RefPtr<CDMProxy> proxy = mProxy;
-    NS_DispatchToMainThread(
+    mMainThread->Dispatch(
       NS_NewRunnableFunction([proxy, sid] ()
       {
         proxy->OnKeyStatusesChange(sid);
-      })
+      }),
+      NS_DISPATCH_NORMAL
     );
   }
 
   RefPtr<CDMProxy> proxy = mProxy;
-  NS_DispatchToMainThread(
+  mMainThread->Dispatch(
     NS_NewRunnableFunction([proxy, sid] ()
     {
       proxy->OnSessionClosed(sid);
-    })
+    }),
+    NS_DISPATCH_NORMAL
   );
 }
 
 void
 GMPCDMCallbackProxy::SessionError(const nsCString& aSessionId,
                                   nsresult aException,
                                   uint32_t aSystemCode,
                                   const nsCString& aMessage)
 {
   MOZ_ASSERT(mProxy->IsOnOwnerThread());
 
   RefPtr<CDMProxy> proxy = mProxy;
   auto sid = NS_ConvertUTF8toUTF16(aSessionId);
   auto msg = NS_ConvertUTF8toUTF16(aMessage);
-  NS_DispatchToMainThread(
+  mMainThread->Dispatch(
     NS_NewRunnableFunction([proxy,
                             sid,
                             aException,
                             aSystemCode,
                             msg] ()
     {
       proxy->OnSessionError(sid,
                         aException,
                         aSystemCode,
                         msg);
-    })
+    }),
+    NS_DISPATCH_NORMAL
   );
 }
 
 void
 GMPCDMCallbackProxy::BatchedKeyStatusChanged(const nsCString& aSessionId,
                                              const nsTArray<CDMKeyInfo>& aKeyInfos)
 {
   MOZ_ASSERT(mProxy->IsOnOwnerThread());
@@ -210,21 +222,22 @@ GMPCDMCallbackProxy::BatchedKeyStatusCha
         caps.SetKeyStatus(aKeyInfos[i].mKeyId,
                           NS_ConvertUTF8toUTF16(aSessionId),
                           aKeyInfos[i].mStatus);
     }
   }
   if (keyStatusesChange) {
     RefPtr<CDMProxy> proxy = mProxy;
     auto sid = NS_ConvertUTF8toUTF16(aSessionId);
-    NS_DispatchToMainThread(
+    mMainThread->Dispatch(
       NS_NewRunnableFunction([proxy, sid] ()
       {
         proxy->OnKeyStatusesChange(sid);
-      })
+      }),
+      NS_DISPATCH_NORMAL
     );
   }
 }
 
 void
 GMPCDMCallbackProxy::Decrypted(uint32_t aId,
                                DecryptStatus aResult,
                                const nsTArray<uint8_t>& aDecryptedData)
@@ -235,17 +248,18 @@ GMPCDMCallbackProxy::Decrypted(uint32_t 
 }
 
 void
 GMPCDMCallbackProxy::Terminated()
 {
   MOZ_ASSERT(mProxy->IsOnOwnerThread());
 
   RefPtr<CDMProxy> proxy = mProxy;
-  NS_DispatchToMainThread(
-      NS_NewRunnableFunction([proxy] ()
-      {
-        proxy->Terminated();
-      })
+  mMainThread->Dispatch(
+    NS_NewRunnableFunction([proxy] ()
+    {
+      proxy->Terminated();
+    }),
+    NS_DISPATCH_NORMAL
   );
 }
 
 } // namespace mozilla
--- a/dom/media/gmp/GMPCDMCallbackProxy.h
+++ b/dom/media/gmp/GMPCDMCallbackProxy.h
@@ -54,19 +54,21 @@ public:
                                const nsTArray<CDMKeyInfo>& aKeyInfos) override;
 
   void Terminated() override;
 
   ~GMPCDMCallbackProxy() {}
 
 private:
   friend class GMPCDMProxy;
-  explicit GMPCDMCallbackProxy(CDMProxy* aProxy);
+  GMPCDMCallbackProxy(CDMProxy* aProxy, nsIEventTarget* aMainThread);
 
   void BatchedKeyStatusChangedInternal(const nsCString& aSessionId,
                                        const nsTArray<CDMKeyInfo>& aKeyInfos);
   // Warning: Weak ref.
   CDMProxy* mProxy;
+
+  const nsCOMPtr<nsIEventTarget> mMainThread;
 };
 
 } // namespace mozilla
 
 #endif // GMPCDMCallbackProxy_h_
--- a/dom/media/gmp/GMPCDMProxy.cpp
+++ b/dom/media/gmp/GMPCDMProxy.cpp
@@ -121,17 +121,17 @@ GMPCDMProxy::gmp_InitDone(GMPDecryptorPr
   }
   if (!aCDM) {
     RejectPromise(aData->mPromiseId, NS_ERROR_DOM_INVALID_STATE_ERR,
                   NS_LITERAL_CSTRING("GetGMPDecryptor failed to return a CDM"));
     return;
   }
 
   mCDM = aCDM;
-  mCallback.reset(new GMPCDMCallbackProxy(this));
+  mCallback.reset(new GMPCDMCallbackProxy(this, mMainThread));
   mCDM->Init(mCallback.get(),
              mDistinctiveIdentifierRequired,
              mPersistentStateRequired);
 
   // Await the OnSetDecryptorId callback.
   mCreatePromiseId = aData->mPromiseId;
 }