Bug 1128379 - improve error handling in MediaKeys::CreateSession. r=bz a=lmandel
authorJW Wang <jwwang@mozilla.com>
Sun, 01 Feb 2015 19:12:00 +0100
changeset 250195 88ab5bafc85a
parent 250194 c197f7371955
child 250196 336529d8cd1a
push id4521
push usercpearce@mozilla.com
push date2015-03-04 01:22 +0000
treeherdermozilla-beta@8abdbdecd2d6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz, lmandel
bugs1128379
milestone37.0
Bug 1128379 - improve error handling in MediaKeys::CreateSession. r=bz a=lmandel
dom/bindings/Bindings.conf
dom/media/eme/MediaKeySession.cpp
dom/media/eme/MediaKeySession.h
dom/media/eme/MediaKeyStatusMap.cpp
dom/media/eme/MediaKeyStatusMap.h
dom/media/eme/MediaKeys.cpp
dom/media/eme/MediaKeys.h
--- a/dom/bindings/Bindings.conf
+++ b/dom/bindings/Bindings.conf
@@ -668,16 +668,20 @@ DOMInterfaces = {
 'MediaSource': [{
 },
 {
     'nativeType': 'JSObject',
     'workers': True,
     'skipGen': True
 }],
 
+'MediaKeys' : {
+    'implicitJSContext': [ 'createSession']
+},
+
 'MediaKeyStatusMap' : {
     'implicitJSContext': [ 'size', 'get', 'has' ]
 },
 
 'MediaStream': [{
     'headerFile': 'DOMMediaStream.h',
     'nativeType': 'mozilla::DOMMediaStream'
 },
--- a/dom/media/eme/MediaKeySession.cpp
+++ b/dom/media/eme/MediaKeySession.cpp
@@ -31,31 +31,35 @@ NS_INTERFACE_MAP_END_INHERITING(DOMEvent
 
 NS_IMPL_ADDREF_INHERITED(MediaKeySession, DOMEventTargetHelper)
 NS_IMPL_RELEASE_INHERITED(MediaKeySession, DOMEventTargetHelper)
 
 // Count of number of instances. Used to give each instance a
 // unique token.
 static uint32_t sMediaKeySessionNum = 0;
 
-MediaKeySession::MediaKeySession(nsPIDOMWindow* aParent,
+MediaKeySession::MediaKeySession(JSContext* aCx,
+                                 nsPIDOMWindow* aParent,
                                  MediaKeys* aKeys,
                                  const nsAString& aKeySystem,
                                  SessionType aSessionType,
                                  ErrorResult& aRv)
   : DOMEventTargetHelper(aParent)
   , mKeys(aKeys)
   , mKeySystem(aKeySystem)
   , mSessionType(aSessionType)
   , mToken(sMediaKeySessionNum++)
   , mIsClosed(false)
   , mUninitialized(true)
-  , mKeyStatusMap(new MediaKeyStatusMap(aParent))
+  , mKeyStatusMap(new MediaKeyStatusMap(aCx, aParent, aRv))
 {
   MOZ_ASSERT(aParent);
+  if (aRv.Failed()) {
+    return;
+  }
   mClosed = mKeys->MakePromise(aRv);
 }
 
 void MediaKeySession::SetSessionId(const nsAString& aSessionId)
 {
   if (NS_WARN_IF(!mSessionId.IsEmpty())) {
     return;
   }
--- a/dom/media/eme/MediaKeySession.h
+++ b/dom/media/eme/MediaKeySession.h
@@ -34,17 +34,18 @@ class MediaKeyStatusMap;
 
 class MediaKeySession MOZ_FINAL : public DOMEventTargetHelper
 {
 public:
   NS_DECL_ISUPPORTS_INHERITED
   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(MediaKeySession,
                                            DOMEventTargetHelper)
 public:
-  MediaKeySession(nsPIDOMWindow* aParent,
+  MediaKeySession(JSContext* aCx,
+                  nsPIDOMWindow* aParent,
                   MediaKeys* aKeys,
                   const nsAString& aKeySystem,
                   SessionType aSessionType,
                   ErrorResult& aRv);
 
   void SetSessionId(const nsAString& aSessionId);
 
   virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
--- a/dom/media/eme/MediaKeyStatusMap.cpp
+++ b/dom/media/eme/MediaKeyStatusMap.cpp
@@ -32,30 +32,25 @@ NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
 
 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(MediaKeyStatusMap)
   NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
   NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mMap)
 NS_IMPL_CYCLE_COLLECTION_TRACE_END
 
-MediaKeyStatusMap::MediaKeyStatusMap(nsPIDOMWindow* aParent)
+MediaKeyStatusMap::MediaKeyStatusMap(JSContext* aCx,
+                                     nsPIDOMWindow* aParent,
+                                     ErrorResult& aRv)
   : mParent(aParent)
   , mUpdateError(NS_OK)
 {
-  AutoJSAPI jsapi;
-  if (NS_WARN_IF(!jsapi.Init(aParent))) {
-    mUpdateError = NS_ERROR_FAILURE;
-    return;
-  }
-
-  JSContext* cx = jsapi.cx();
-  mMap = JS::NewMapObject(cx);
+  mMap = JS::NewMapObject(aCx);
   if (NS_WARN_IF(!mMap)) {
-    mUpdateError = NS_ERROR_FAILURE;
+    aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
   }
 }
 
 MediaKeyStatusMap::~MediaKeyStatusMap()
 {
 }
 
 JSObject*
@@ -204,17 +199,17 @@ ToJSString(JSContext* aCx, GMPMediaKeySt
   aResult.setString(str);
   return true;
 }
 
 nsresult
 MediaKeyStatusMap::UpdateInternal(const nsTArray<CDMCaps::KeyStatus>& keys)
 {
   AutoJSAPI jsapi;
-  if (!mMap || NS_WARN_IF(!jsapi.Init(mParent))) {
+  if (NS_WARN_IF(!jsapi.Init(mParent))) {
     return NS_ERROR_FAILURE;
   }
 
   jsapi.TakeOwnershipOfErrorReporting();
   JSContext* cx = jsapi.cx();
   JS::Rooted<JSObject*> map(cx, mMap);
   if (!JS::MapClear(cx, map)) {
     return NS_ERROR_FAILURE;
--- a/dom/media/eme/MediaKeyStatusMap.h
+++ b/dom/media/eme/MediaKeyStatusMap.h
@@ -25,17 +25,19 @@ namespace dom {
 class MediaKeyStatusMap MOZ_FINAL : public nsISupports,
                                     public nsWrapperCache
 {
 public:
   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(MediaKeyStatusMap)
 
 public:
-  explicit MediaKeyStatusMap(nsPIDOMWindow* aParent);
+  explicit MediaKeyStatusMap(JSContext* aCx,
+                             nsPIDOMWindow* aParent,
+                             ErrorResult& aRv);
 
 protected:
   ~MediaKeyStatusMap();
 
 public:
   nsPIDOMWindow* GetParentObject() const;
 
   virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
--- a/dom/media/eme/MediaKeys.cpp
+++ b/dom/media/eme/MediaKeys.cpp
@@ -354,25 +354,31 @@ MediaKeys::OnCDMCreated(PromiseId aId, c
   nsRefPtr<MediaKeys> keys(this);
   promise->MaybeResolve(keys);
   if (mCreatePromiseId == aId) {
     Release();
   }
 }
 
 already_AddRefed<MediaKeySession>
-MediaKeys::CreateSession(SessionType aSessionType,
+MediaKeys::CreateSession(JSContext* aCx,
+                         SessionType aSessionType,
                          ErrorResult& aRv)
 {
-  nsRefPtr<MediaKeySession> session = new MediaKeySession(GetParentObject(),
+  nsRefPtr<MediaKeySession> session = new MediaKeySession(aCx,
+                                                          GetParentObject(),
                                                           this,
                                                           mKeySystem,
                                                           aSessionType,
                                                           aRv);
 
+  if (aRv.Failed()) {
+    return nullptr;
+  }
+
   // 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)
--- a/dom/media/eme/MediaKeys.h
+++ b/dom/media/eme/MediaKeys.h
@@ -60,17 +60,18 @@ public:
   virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
 
   nsresult Bind(HTMLMediaElement* aElement);
 
   // Javascript: readonly attribute DOMString keySystem;
   void GetKeySystem(nsString& retval) const;
 
   // JavaScript: MediaKeys.createSession()
-  already_AddRefed<MediaKeySession> CreateSession(SessionType aSessionType,
+  already_AddRefed<MediaKeySession> CreateSession(JSContext* aCx,
+                                                  SessionType aSessionType,
                                                   ErrorResult& aRv);
 
   // JavaScript: MediaKeys.SetServerCertificate()
   already_AddRefed<Promise> SetServerCertificate(const ArrayBufferViewOrArrayBuffer& aServerCertificate,
                                                  ErrorResult& aRv);
 
   already_AddRefed<MediaKeySession> GetSession(const nsAString& aSessionId);