Bug 1495053 - p3: implement MediaDrmCDMProxy::SetServerCertificate(). r=cpearce
authorJohn Lin <jolin@mozilla.com>
Fri, 07 Dec 2018 14:44:34 +0000
changeset 508826 8442297c6818
parent 508825 f21136ecba05
child 508827 9317931c9ce1
push id1905
push userffxbld-merge
push dateMon, 21 Jan 2019 12:33:13 +0000
treeherdermozilla-release@c2fca1944d8c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerscpearce
bugs1495053
milestone65.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1495053 - p3: implement MediaDrmCDMProxy::SetServerCertificate(). r=cpearce Differential Revision: https://phabricator.services.mozilla.com/D7792
dom/media/eme/MediaKeys.cpp
dom/media/eme/MediaKeys.h
dom/media/eme/mediadrm/MediaDrmCDMProxy.cpp
dom/media/eme/mediadrm/MediaDrmCDMProxy.h
--- a/dom/media/eme/MediaKeys.cpp
+++ b/dom/media/eme/MediaKeys.cpp
@@ -471,23 +471,29 @@ already_AddRefed<MediaKeySession> MediaK
 
   // Add session to the set of sessions awaiting their sessionId being ready.
   mPendingSessions.Put(session->Token(), session);
 
   return session.forget();
 }
 
 void MediaKeys::OnSessionLoaded(PromiseId aId, bool aSuccess) {
+  EME_LOG("MediaKeys[%p]::OnSessionLoaded() resolve promise id=%d", this, aId);
+
+  ResolvePromiseWithResult(aId, aSuccess);
+}
+
+template <typename T>
+void MediaKeys::ResolvePromiseWithResult(PromiseId aId, const T& aResult) {
   RefPtr<DetailedPromise> promise(RetrievePromise(aId));
   if (!promise) {
     return;
   }
-  EME_LOG("MediaKeys[%p]::OnSessionLoaded() resolve promise id=%d", this, aId);
 
-  promise->MaybeResolve(aSuccess);
+  promise->MaybeResolve(aResult);
 }
 
 void MediaKeys::OnSessionClosed(MediaKeySession* aSession) {
   nsAutoString id;
   aSession->GetSessionId(id);
   mKeySessions.Remove(id);
 }
 
--- a/dom/media/eme/MediaKeys.h
+++ b/dom/media/eme/MediaKeys.h
@@ -139,16 +139,19 @@ class MediaKeys final : public nsISuppor
 
   // JavaScript: MediaKeys.GetStatusForPolicy()
   already_AddRefed<Promise> GetStatusForPolicy(const MediaKeysPolicy& aPolicy,
                                                ErrorResult& aR);
   // Called by CDMProxy when CDM successfully GetStatusForPolicy.
   void ResolvePromiseWithKeyStatus(PromiseId aId,
                                    dom::MediaKeyStatus aMediaKeyStatus);
 
+  template <typename T>
+  void ResolvePromiseWithResult(PromiseId aId, const T& aResult);
+
  private:
   // Instantiate CDMProxy instance.
   // It could be MediaDrmCDMProxy (Widevine on Fennec) or ChromiumCDMProxy (the
   // rest).
   already_AddRefed<CDMProxy> CreateCDMProxy(nsIEventTarget* aMainThread);
 
   // Removes promise from mPromises, and returns it.
   already_AddRefed<DetailedPromise> RetrievePromise(PromiseId aId);
--- a/dom/media/eme/mediadrm/MediaDrmCDMProxy.cpp
+++ b/dom/media/eme/mediadrm/MediaDrmCDMProxy.cpp
@@ -96,20 +96,24 @@ void MediaDrmCDMProxy::LoadSession(Promi
   // TODO: Implement LoadSession.
   RejectPromise(
       aPromiseId, NS_ERROR_DOM_INVALID_STATE_ERR,
       NS_LITERAL_CSTRING("Currently Fennec does not support LoadSession"));
 }
 
 void MediaDrmCDMProxy::SetServerCertificate(PromiseId aPromiseId,
                                             nsTArray<uint8_t>& aCert) {
-  // TODO: Implement SetServerCertificate.
-  RejectPromise(aPromiseId, NS_ERROR_DOM_INVALID_STATE_ERR,
-                NS_LITERAL_CSTRING(
-                    "Currently Fennec does not support SetServerCertificate"));
+  MOZ_ASSERT(NS_IsMainThread());
+  MOZ_ASSERT(mOwnerThread);
+
+  mOwnerThread->Dispatch(NewRunnableMethod<PromiseId, const nsTArray<uint8_t>>(
+                             "MediaDrmCDMProxy::md_SetServerCertificate", this,
+                             &MediaDrmCDMProxy::md_SetServerCertificate,
+                             aPromiseId, std::move(aCert)),
+                         NS_DISPATCH_NORMAL);
 }
 
 void MediaDrmCDMProxy::UpdateSession(const nsAString& aSessionId,
                                      PromiseId aPromiseId,
                                      nsTArray<uint8_t>& aResponse) {
   MOZ_ASSERT(NS_IsMainThread());
   MOZ_ASSERT(mOwnerThread);
   NS_ENSURE_TRUE_VOID(!mKeys.IsNull());
@@ -280,16 +284,35 @@ void MediaDrmCDMProxy::ResolvePromise(Pr
     nsCOMPtr<nsIRunnable> task;
     task =
         NewRunnableMethod<PromiseId>("MediaDrmCDMProxy::ResolvePromise", this,
                                      &MediaDrmCDMProxy::ResolvePromise, aId);
     mMainThread->Dispatch(task.forget(), NS_DISPATCH_NORMAL);
   }
 }
 
+template <typename T>
+void MediaDrmCDMProxy::ResolvePromiseWithResult(PromiseId aId,
+                                                const T& aResult) {
+  if (NS_IsMainThread()) {
+    if (!mKeys.IsNull()) {
+      mKeys->ResolvePromiseWithResult(aId, aResult);
+    } else {
+      NS_WARNING("MediaDrmCDMProxy unable to resolve promise!");
+    }
+    return;
+  }
+
+  nsCOMPtr<nsIRunnable> task;
+  task = NewRunnableMethod<PromiseId, T>(
+      "MediaDrmCDMProxy::ResolvePromiseWithResult", this,
+      &MediaDrmCDMProxy::ResolvePromiseWithResult<T>, aId, aResult);
+  mMainThread->Dispatch(task.forget(), NS_DISPATCH_NORMAL);
+}
+
 const nsString& MediaDrmCDMProxy::KeySystem() const { return mKeySystem; }
 
 DataMutex<CDMCaps>& MediaDrmCDMProxy::Capabilites() { return mCapabilites; }
 
 void MediaDrmCDMProxy::OnKeyStatusesChange(const nsAString& aSessionId) {
   MOZ_ASSERT(NS_IsMainThread());
   if (mKeys.IsNull()) {
     return;
@@ -356,16 +379,35 @@ void MediaDrmCDMProxy::md_CreateSession(
     return;
   }
 
   mCDM->CreateSession(aData->mCreateSessionToken, aData->mPromiseId,
                       aData->mInitDataType, aData->mInitData,
                       ToMediaDrmSessionType(aData->mSessionType));
 }
 
+void MediaDrmCDMProxy::md_SetServerCertificate(PromiseId aPromiseId,
+                                               const nsTArray<uint8_t>& aCert) {
+  MOZ_ASSERT(IsOnOwnerThread());
+
+  if (!mCDM) {
+    RejectPromise(aPromiseId, NS_ERROR_DOM_INVALID_STATE_ERR,
+                  NS_LITERAL_CSTRING("Null CDM in md_SetServerCertificate"));
+    return;
+  }
+
+  if (mCDM->SetServerCertificate(aCert)) {
+    ResolvePromiseWithResult(aPromiseId, true);
+  } else {
+    RejectPromise(aPromiseId, NS_ERROR_DOM_INVALID_STATE_ERR,
+                  NS_LITERAL_CSTRING(
+                      "MediaDrmCDMProxy unable to set server certificate"));
+  }
+}
+
 void MediaDrmCDMProxy::md_UpdateSession(UniquePtr<UpdateSessionData>&& aData) {
   MOZ_ASSERT(IsOnOwnerThread());
 
   if (!mCDM) {
     RejectPromise(aData->mPromiseId, NS_ERROR_DOM_INVALID_STATE_ERR,
                   NS_LITERAL_CSTRING("Null CDM in md_UpdateSession"));
     return;
   }
--- a/dom/media/eme/mediadrm/MediaDrmCDMProxy.h
+++ b/dom/media/eme/mediadrm/MediaDrmCDMProxy.h
@@ -109,16 +109,19 @@ class MediaDrmCDMProxy : public CDMProxy
 
   const nsString& GetMediaDrmStubId() const;
 
  private:
   virtual ~MediaDrmCDMProxy();
 
   void OnCDMCreated(uint32_t aPromiseId);
 
+  template <typename T>
+  void ResolvePromiseWithResult(PromiseId aId, const T& aResult);
+
   struct CreateSessionData {
     MediaKeySessionType mSessionType;
     uint32_t mCreateSessionToken;
     PromiseId mPromiseId;
     nsCString mInitDataType;
     nsTArray<uint8_t> mInitData;
   };
 
@@ -158,16 +161,18 @@ class MediaDrmCDMProxy : public CDMProxy
   UniquePtr<MediaDrmProxySupport> mCDM;
   UniquePtr<MediaDrmCDMCallbackProxy> mCallback;
   bool mShutdownCalled;
 
   // =====================================================================
   // For MediaDrmProxySupport
   void md_Init(uint32_t aPromiseId);
   void md_CreateSession(UniquePtr<CreateSessionData>&& aData);
+  void md_SetServerCertificate(PromiseId aPromiseId,
+                               const nsTArray<uint8_t>& aCert);
   void md_UpdateSession(UniquePtr<UpdateSessionData>&& aData);
   void md_CloseSession(UniquePtr<SessionOpData>&& aData);
   void md_Shutdown();
   // =====================================================================
 };
 
 }  // namespace mozilla