Bug 1313878 - Use lambda to create runnable for dispatching to main thread in GMPCDMCallbackProxy.cpp draft
authorJames Cheng <jacheng@mozilla.com>
Sun, 30 Oct 2016 03:48:26 +0800
changeset 431541 bba7564e6dbd4d1efbeb4fc74efb630424a30526
parent 431500 969c3295d3aa77931cca26eddb047d9d74bd9858
child 535422 a99e741677af268d45bca4a1f00fcc65988545ec
push id34061
push userbmo:jacheng@mozilla.com
push dateSun, 30 Oct 2016 02:38:21 +0000
bugs1313878
milestone52.0a1
Bug 1313878 - Use lambda to create runnable for dispatching to main thread in GMPCDMCallbackProxy.cpp MozReview-Commit-ID: 2X1oWsL29n9
dom/media/gmp/GMPCDMCallbackProxy.cpp
--- a/dom/media/gmp/GMPCDMCallbackProxy.cpp
+++ b/dom/media/gmp/GMPCDMCallbackProxy.cpp
@@ -14,274 +14,172 @@
 #include "nsServiceManagerUtils.h"
 #include "MainThreadUtils.h"
 #include "mozilla/EMEUtils.h"
 
 namespace mozilla {
 
 GMPCDMCallbackProxy::GMPCDMCallbackProxy(CDMProxy* aProxy)
   : mProxy(aProxy)
-{
-
-}
-
-class SetSessionIdTask : public Runnable {
-public:
-  SetSessionIdTask(CDMProxy* aProxy,
-                   uint32_t aToken,
-                   const nsCString& aSessionId)
-    : mProxy(aProxy)
-    , mToken(aToken)
-    , mSid(NS_ConvertUTF8toUTF16(aSessionId))
-  {
-  }
-
-  NS_IMETHOD Run() override {
-    mProxy->OnSetSessionId(mToken, mSid);
-    return NS_OK;
-  }
-
-  RefPtr<CDMProxy> mProxy;
-  uint32_t mToken;
-  nsString mSid;
-};
+{}
 
 void
 GMPCDMCallbackProxy::SetSessionId(uint32_t aToken,
                                   const nsCString& aSessionId)
 {
   MOZ_ASSERT(mProxy->IsOnOwnerThread());
 
-  nsCOMPtr<nsIRunnable> task(new SetSessionIdTask(mProxy,
-                                                  aToken,
-                                                  aSessionId));
-  NS_DispatchToMainThread(task);
+  RefPtr<CDMProxy> proxy = mProxy;
+  auto sid = NS_ConvertUTF8toUTF16(aSessionId);
+  NS_DispatchToMainThread(
+    NS_NewRunnableFunction([proxy,
+                            aToken,
+                            sid] ()
+    {
+      proxy->OnSetSessionId(aToken, sid);
+    })
+  );
 }
 
-class LoadSessionTask : public Runnable {
-public:
-  LoadSessionTask(CDMProxy* aProxy,
-                  uint32_t aPromiseId,
-                  bool aSuccess)
-    : mProxy(aProxy)
-    , mPid(aPromiseId)
-    , mSuccess(aSuccess)
-  {
-  }
-
-  NS_IMETHOD Run() override {
-    mProxy->OnResolveLoadSessionPromise(mPid, mSuccess);
-    return NS_OK;
-  }
-
-  RefPtr<CDMProxy> mProxy;
-  dom::PromiseId mPid;
-  bool mSuccess;
-};
-
 void
 GMPCDMCallbackProxy::ResolveLoadSessionPromise(uint32_t aPromiseId,
                                                bool aSuccess)
 {
   MOZ_ASSERT(mProxy->IsOnOwnerThread());
 
-  nsCOMPtr<nsIRunnable> task(new LoadSessionTask(mProxy,
-                                                 aPromiseId,
-                                                 aSuccess));
-  NS_DispatchToMainThread(task);
+  RefPtr<CDMProxy> proxy = mProxy;
+  NS_DispatchToMainThread(
+    NS_NewRunnableFunction([proxy, aPromiseId, aSuccess] ()
+    {
+      proxy->OnResolveLoadSessionPromise(aPromiseId, aSuccess);
+    })
+  );
 }
 
 void
 GMPCDMCallbackProxy::ResolvePromise(uint32_t aPromiseId)
 {
   MOZ_ASSERT(mProxy->IsOnOwnerThread());
 
   // Note: CDMProxy proxies this from non-main threads to main thread.
   mProxy->ResolvePromise(aPromiseId);
 }
 
-class RejectPromiseTask : public Runnable {
-public:
-  RejectPromiseTask(CDMProxy* aProxy,
-                    uint32_t aPromiseId,
-                    nsresult aException,
-                    const nsCString& aMessage)
-    : mProxy(aProxy)
-    , mPid(aPromiseId)
-    , mException(aException)
-    , mMsg(aMessage)
-  {
-  }
-
-  NS_IMETHOD Run() override {
-    mProxy->OnRejectPromise(mPid, mException, mMsg);
-    return NS_OK;
-  }
-
-  RefPtr<CDMProxy> mProxy;
-  dom::PromiseId mPid;
-  nsresult mException;
-  nsCString mMsg;
-};
-
-
 void
 GMPCDMCallbackProxy::RejectPromise(uint32_t aPromiseId,
                                    nsresult aException,
                                    const nsCString& aMessage)
 {
   MOZ_ASSERT(mProxy->IsOnOwnerThread());
 
-  nsCOMPtr<nsIRunnable> task;
-  task = new RejectPromiseTask(mProxy,
-                               aPromiseId,
-                               aException,
-                               aMessage);
-  NS_DispatchToMainThread(task);
+  RefPtr<CDMProxy> proxy = mProxy;
+  NS_DispatchToMainThread(
+    NS_NewRunnableFunction([proxy,
+                            aPromiseId,
+                            aException,
+                            aMessage] ()
+    {
+      proxy->OnRejectPromise(aPromiseId, aException, aMessage);
+    })
+  );
 }
 
-class SessionMessageTask : public Runnable {
-public:
-  SessionMessageTask(CDMProxy* aProxy,
-                     const nsCString& aSessionId,
-                     dom::MediaKeyMessageType aMessageType,
-                     const nsTArray<uint8_t>& aMessage)
-    : mProxy(aProxy)
-    , mSid(NS_ConvertUTF8toUTF16(aSessionId))
-    , mMsgType(aMessageType)
-  {
-    mMsg.AppendElements(aMessage);
-  }
-
-  NS_IMETHOD Run() override {
-    mProxy->OnSessionMessage(mSid, mMsgType, mMsg);
-    return NS_OK;
-  }
-
-  RefPtr<CDMProxy> mProxy;
-  nsString mSid;
-  dom::MediaKeyMessageType mMsgType;
-  nsTArray<uint8_t> mMsg;
-};
-
 void
 GMPCDMCallbackProxy::SessionMessage(const nsCString& aSessionId,
                                     dom::MediaKeyMessageType aMessageType,
                                     const nsTArray<uint8_t>& aMessage)
 {
   MOZ_ASSERT(mProxy->IsOnOwnerThread());
 
-  nsCOMPtr<nsIRunnable> task;
-  task = new SessionMessageTask(mProxy,
-                                aSessionId,
-                                aMessageType,
-                                aMessage);
-  NS_DispatchToMainThread(task);
+  RefPtr<CDMProxy> proxy = mProxy;
+  auto sid = NS_ConvertUTF8toUTF16(aSessionId);
+  nsTArray<uint8_t> msg(aMessage);
+  NS_DispatchToMainThread(
+    NS_NewRunnableFunction([proxy,
+                            sid,
+                            aMessageType,
+                            msg] () mutable
+    {
+      proxy->OnSessionMessage(sid, aMessageType, msg);
+    })
+  );
 }
 
-class ExpirationChangeTask : public Runnable {
-public:
-  ExpirationChangeTask(CDMProxy* aProxy,
-                       const nsCString& aSessionId,
-                       GMPTimestamp aExpiryTime)
-    : mProxy(aProxy)
-    , mSid(NS_ConvertUTF8toUTF16(aSessionId))
-    , mTimestamp(aExpiryTime)
-  {}
-
-  NS_IMETHOD Run() override {
-    mProxy->OnExpirationChange(mSid, mTimestamp);
-    return NS_OK;
-  }
-
-  RefPtr<CDMProxy> mProxy;
-  nsString mSid;
-  GMPTimestamp mTimestamp;
-};
-
 void
 GMPCDMCallbackProxy::ExpirationChange(const nsCString& aSessionId,
                                       GMPTimestamp aExpiryTime)
 {
   MOZ_ASSERT(mProxy->IsOnOwnerThread());
 
-  nsCOMPtr<nsIRunnable> task;
-  task = new ExpirationChangeTask(mProxy,
-                                  aSessionId,
-                                  aExpiryTime);
-  NS_DispatchToMainThread(task);
+  RefPtr<CDMProxy> proxy = mProxy;
+  auto sid = NS_ConvertUTF8toUTF16(aSessionId);
+  NS_DispatchToMainThread(
+    NS_NewRunnableFunction([proxy,
+                            sid,
+                            aExpiryTime] ()
+    {
+      proxy->OnExpirationChange(sid, aExpiryTime);
+    })
+  );
 }
 
 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) {
-    nsCOMPtr<nsIRunnable> task;
-    task = NewRunnableMethod<nsString>(mProxy,
-      &CDMProxy::OnKeyStatusesChange,
-      NS_ConvertUTF8toUTF16(aSessionId));
-    NS_DispatchToMainThread(task);
+    RefPtr<CDMProxy> proxy = mProxy;
+    NS_DispatchToMainThread(
+      NS_NewRunnableFunction([proxy, sid] ()
+      {
+        proxy->OnKeyStatusesChange(sid);
+      })
+    );
   }
 
-  nsCOMPtr<nsIRunnable> task;
-  task = NewRunnableMethod<nsString>(mProxy,
-                                     &CDMProxy::OnSessionClosed,
-                                     NS_ConvertUTF8toUTF16(aSessionId));
-  NS_DispatchToMainThread(task);
+  RefPtr<CDMProxy> proxy = mProxy;
+  NS_DispatchToMainThread(
+    NS_NewRunnableFunction([proxy, sid] ()
+    {
+      proxy->OnSessionClosed(sid);
+    })
+  );
 }
 
-class SessionErrorTask : public Runnable {
-public:
-  SessionErrorTask(CDMProxy* aProxy,
-                   const nsCString& aSessionId,
-                   nsresult aException,
-                   uint32_t aSystemCode,
-                   const nsCString& aMessage)
-    : mProxy(aProxy)
-    , mSid(NS_ConvertUTF8toUTF16(aSessionId))
-    , mException(aException)
-    , mSystemCode(aSystemCode)
-    , mMsg(NS_ConvertUTF8toUTF16(aMessage))
-  {}
-
-  NS_IMETHOD Run() override {
-    mProxy->OnSessionError(mSid, mException, mSystemCode, mMsg);
-    return NS_OK;
-  }
-
-  RefPtr<CDMProxy> mProxy;
-  dom::PromiseId mPid;
-  nsString mSid;
-  nsresult mException;
-  uint32_t mSystemCode;
-  nsString mMsg;
-};
-
 void
 GMPCDMCallbackProxy::SessionError(const nsCString& aSessionId,
                                   nsresult aException,
                                   uint32_t aSystemCode,
                                   const nsCString& aMessage)
 {
   MOZ_ASSERT(mProxy->IsOnOwnerThread());
 
-  nsCOMPtr<nsIRunnable> task;
-  task = new SessionErrorTask(mProxy,
-                              aSessionId,
-                              aException,
-                              aSystemCode,
-                              aMessage);
-  NS_DispatchToMainThread(task);
+  RefPtr<CDMProxy> proxy = mProxy;
+  auto sid = NS_ConvertUTF8toUTF16(aSessionId);
+  auto msg = NS_ConvertUTF8toUTF16(aMessage);
+  NS_DispatchToMainThread(
+    NS_NewRunnableFunction([proxy,
+                            sid,
+                            aException,
+                            aSystemCode,
+                            msg] ()
+    {
+      proxy->OnSessionError(sid,
+                        aException,
+                        aSystemCode,
+                        msg);
+    })
+  );
 }
 
 void
 GMPCDMCallbackProxy::BatchedKeyStatusChanged(const nsCString& aSessionId,
                                              const nsTArray<CDMKeyInfo>& aKeyInfos)
 {
   MOZ_ASSERT(mProxy->IsOnOwnerThread());
   BatchedKeyStatusChangedInternal(aSessionId, aKeyInfos);
@@ -297,21 +195,24 @@ GMPCDMCallbackProxy::BatchedKeyStatusCha
     for (size_t i = 0; i < aKeyInfos.Length(); i++) {
       keyStatusesChange |=
         caps.SetKeyStatus(aKeyInfos[i].mKeyId,
                           NS_ConvertUTF8toUTF16(aSessionId),
                           aKeyInfos[i].mStatus);
     }
   }
   if (keyStatusesChange) {
-    nsCOMPtr<nsIRunnable> task;
-    task = NewRunnableMethod<nsString>(mProxy,
-                                       &CDMProxy::OnKeyStatusesChange,
-                                       NS_ConvertUTF8toUTF16(aSessionId));
-    NS_DispatchToMainThread(task);
+    RefPtr<CDMProxy> proxy = mProxy;
+    auto sid = NS_ConvertUTF8toUTF16(aSessionId);
+    NS_DispatchToMainThread(
+      NS_NewRunnableFunction([proxy, sid] ()
+      {
+        proxy->OnKeyStatusesChange(sid);
+      })
+    );
   }
 }
 
 void
 GMPCDMCallbackProxy::Decrypted(uint32_t aId,
                                DecryptStatus aResult,
                                const nsTArray<uint8_t>& aDecryptedData)
 {
@@ -319,13 +220,19 @@ GMPCDMCallbackProxy::Decrypted(uint32_t 
 
   mProxy->OnDecrypted(aId, aResult, aDecryptedData);
 }
 
 void
 GMPCDMCallbackProxy::Terminated()
 {
   MOZ_ASSERT(mProxy->IsOnOwnerThread());
-  nsCOMPtr<nsIRunnable> task = NewRunnableMethod(mProxy, &CDMProxy::Terminated);
-  NS_DispatchToMainThread(task);
+
+  RefPtr<CDMProxy> proxy = mProxy;
+  NS_DispatchToMainThread(
+      NS_NewRunnableFunction([proxy] ()
+      {
+        proxy->Terminated();
+      })
+  );
 }
 
 } // namespace mozilla