am eced745a: Merge "Part of fix for TEE resource leaks when app crashes" into jb-mr2-dev

* commit 'eced745af7d2d27a8c45e04eb071d4fb30cd3e48':
  Part of fix for TEE resource leaks when app crashes
diff --git a/media/libmediaplayerservice/Drm.cpp b/media/libmediaplayerservice/Drm.cpp
index 1e6cd94..f00f488 100644
--- a/media/libmediaplayerservice/Drm.cpp
+++ b/media/libmediaplayerservice/Drm.cpp
@@ -71,6 +71,12 @@
 status_t Drm::setListener(const sp<IDrmClient>& listener)
 {
     Mutex::Autolock lock(mEventLock);
+    if (mListener != NULL){
+        mListener->asBinder()->unlinkToDeath(this);
+    }
+    if (listener != NULL) {
+        listener->asBinder()->linkToDeath(this);
+    }
     mListener = listener;
     return NO_ERROR;
 }
@@ -576,4 +582,12 @@
     return mPlugin->verify(sessionId, keyId, message, signature, match);
 }
 
+void Drm::binderDied(const wp<IBinder> &the_late_who)
+{
+    delete mPlugin;
+    mPlugin = NULL;
+    closeFactory();
+    mListener.clear();
+}
+
 }  // namespace android
diff --git a/media/libmediaplayerservice/Drm.h b/media/libmediaplayerservice/Drm.h
index 3da8ad4..3f460f1 100644
--- a/media/libmediaplayerservice/Drm.h
+++ b/media/libmediaplayerservice/Drm.h
@@ -29,7 +29,9 @@
 struct DrmFactory;
 struct DrmPlugin;
 
-struct Drm : public BnDrm, public DrmPluginListener {
+struct Drm : public BnDrm,
+             public IBinder::DeathRecipient,
+             public DrmPluginListener {
     Drm();
     virtual ~Drm();
 
@@ -115,6 +117,8 @@
                            Vector<uint8_t> const *sessionId,
                            Vector<uint8_t> const *data);
 
+    virtual void binderDied(const wp<IBinder> &the_late_who);
+
 private:
     mutable Mutex mLock;