Let getOriginalMimeType() take a fd passed from drm java applications

This patch enables the drm framework to avoid opening files directly.
As a result, the drm framework no longer needs the sdcard read permision.

o related-to-bug: 6426185

Change-Id: Ib176c35ef4b1a1a405e8e954f19a7985266f2510
diff --git a/drm/common/DrmEngineBase.cpp b/drm/common/DrmEngineBase.cpp
index a060f38..f734905 100644
--- a/drm/common/DrmEngineBase.cpp
+++ b/drm/common/DrmEngineBase.cpp
@@ -66,8 +66,8 @@
     return onAcquireDrmInfo(uniqueId, drmInfoRequest);
 }
 
-String8 DrmEngineBase::getOriginalMimeType(int uniqueId, const String8& path) {
-    return onGetOriginalMimeType(uniqueId, path);
+String8 DrmEngineBase::getOriginalMimeType(int uniqueId, const String8& path, int fd) {
+    return onGetOriginalMimeType(uniqueId, path, fd);
 }
 
 int DrmEngineBase::getDrmObjectType(int uniqueId, const String8& path, const String8& mimeType) {
diff --git a/drm/common/IDrmManagerService.cpp b/drm/common/IDrmManagerService.cpp
index ccff257..b76572c 100644
--- a/drm/common/IDrmManagerService.cpp
+++ b/drm/common/IDrmManagerService.cpp
@@ -368,13 +368,18 @@
     return reply.readInt32();
 }
 
-String8 BpDrmManagerService::getOriginalMimeType(int uniqueId, const String8& path) {
+String8 BpDrmManagerService::getOriginalMimeType(int uniqueId, const String8& path, int fd) {
     ALOGV("Get Original MimeType");
     Parcel data, reply;
 
     data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
     data.writeInt32(uniqueId);
     data.writeString8(path);
+    int32_t isFdValid = (fd >= 0);
+    data.writeInt32(isFdValid);
+    if (isFdValid) {
+        data.writeFileDescriptor(fd);
+    }
 
     remote()->transact(GET_ORIGINAL_MIMETYPE, data, &reply);
     return reply.readString8();
@@ -1067,7 +1072,12 @@
 
         const int uniqueId = data.readInt32();
         const String8 path = data.readString8();
-        const String8 originalMimeType = getOriginalMimeType(uniqueId, path);
+        const int32_t isFdValid = data.readInt32();
+        int fd = -1;
+        if (isFdValid) {
+            fd = data.readFileDescriptor();
+        }
+        const String8 originalMimeType = getOriginalMimeType(uniqueId, path, fd);
 
         reply->writeString8(originalMimeType);
         return DRM_NO_ERROR;
diff --git a/drm/drmserver/DrmManager.cpp b/drm/drmserver/DrmManager.cpp
index 737edab..e7b0e90 100644
--- a/drm/drmserver/DrmManager.cpp
+++ b/drm/drmserver/DrmManager.cpp
@@ -266,12 +266,12 @@
     return result;
 }
 
-String8 DrmManager::getOriginalMimeType(int uniqueId, const String8& path) {
+String8 DrmManager::getOriginalMimeType(int uniqueId, const String8& path, int fd) {
     Mutex::Autolock _l(mLock);
     const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
     if (EMPTY_STRING != plugInId) {
         IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
-        return rDrmEngine.getOriginalMimeType(uniqueId, path);
+        return rDrmEngine.getOriginalMimeType(uniqueId, path, fd);
     }
     return EMPTY_STRING;
 }
diff --git a/drm/drmserver/DrmManagerService.cpp b/drm/drmserver/DrmManagerService.cpp
index 25a4e7b..bbd3b7f 100644
--- a/drm/drmserver/DrmManagerService.cpp
+++ b/drm/drmserver/DrmManagerService.cpp
@@ -125,9 +125,9 @@
     return mDrmManager->saveRights(uniqueId, drmRights, rightsPath, contentPath);
 }
 
-String8 DrmManagerService::getOriginalMimeType(int uniqueId, const String8& path) {
+String8 DrmManagerService::getOriginalMimeType(int uniqueId, const String8& path, int fd) {
     ALOGV("Entering getOriginalMimeType");
-    return mDrmManager->getOriginalMimeType(uniqueId, path);
+    return mDrmManager->getOriginalMimeType(uniqueId, path, fd);
 }
 
 int DrmManagerService::getDrmObjectType(
diff --git a/drm/libdrmframework/DrmManagerClient.cpp b/drm/libdrmframework/DrmManagerClient.cpp
index d4db461..ea30d01 100644
--- a/drm/libdrmframework/DrmManagerClient.cpp
+++ b/drm/libdrmframework/DrmManagerClient.cpp
@@ -64,8 +64,8 @@
     return mDrmManagerClientImpl->saveRights(mUniqueId, drmRights, rightsPath, contentPath);
 }
 
-String8 DrmManagerClient::getOriginalMimeType(const String8& path) {
-    return mDrmManagerClientImpl->getOriginalMimeType(mUniqueId, path);
+String8 DrmManagerClient::getOriginalMimeType(const String8& path, int fd) {
+    return mDrmManagerClientImpl->getOriginalMimeType(mUniqueId, path, fd);
 }
 
 int DrmManagerClient::getDrmObjectType(const String8& path, const String8& mimeType) {
diff --git a/drm/libdrmframework/DrmManagerClientImpl.cpp b/drm/libdrmframework/DrmManagerClientImpl.cpp
index b1228d4..a970035 100644
--- a/drm/libdrmframework/DrmManagerClientImpl.cpp
+++ b/drm/libdrmframework/DrmManagerClientImpl.cpp
@@ -147,10 +147,10 @@
 }
 
 String8 DrmManagerClientImpl::getOriginalMimeType(
-        int uniqueId, const String8& path) {
+        int uniqueId, const String8& path, int fd) {
     String8 mimeType = EMPTY_STRING;
     if (EMPTY_STRING != path) {
-        mimeType = getDrmManagerService()->getOriginalMimeType(uniqueId, path);
+        mimeType = getDrmManagerService()->getOriginalMimeType(uniqueId, path, fd);
     }
     return mimeType;
 }
diff --git a/drm/libdrmframework/include/DrmManager.h b/drm/libdrmframework/include/DrmManager.h
index 3942efe..491e8f7 100644
--- a/drm/libdrmframework/include/DrmManager.h
+++ b/drm/libdrmframework/include/DrmManager.h
@@ -85,7 +85,7 @@
     status_t saveRights(int uniqueId, const DrmRights& drmRights,
             const String8& rightsPath, const String8& contentPath);
 
-    String8 getOriginalMimeType(int uniqueId, const String8& path);
+    String8 getOriginalMimeType(int uniqueId, const String8& path, int fd);
 
     int getDrmObjectType(int uniqueId, const String8& path, const String8& mimeType);
 
diff --git a/drm/libdrmframework/include/DrmManagerClientImpl.h b/drm/libdrmframework/include/DrmManagerClientImpl.h
index f3d9315..9b4c9ae 100644
--- a/drm/libdrmframework/include/DrmManagerClientImpl.h
+++ b/drm/libdrmframework/include/DrmManagerClientImpl.h
@@ -148,10 +148,11 @@
      *
      * @param[in] uniqueId Unique identifier for a session
      * @param[in] path the path of the protected content
+     * @param[in] fd the file descriptor of the protected content
      * @return String8
      *     Returns mime-type of the original content, such as "video/mpeg"
      */
-    String8 getOriginalMimeType(int uniqueId, const String8& path);
+    String8 getOriginalMimeType(int uniqueId, const String8& path, int fd);
 
     /**
      * Retrieves the type of the protected object (content, rights, etc..)
diff --git a/drm/libdrmframework/include/DrmManagerService.h b/drm/libdrmframework/include/DrmManagerService.h
index 066fe4a..0dfdca6 100644
--- a/drm/libdrmframework/include/DrmManagerService.h
+++ b/drm/libdrmframework/include/DrmManagerService.h
@@ -72,7 +72,7 @@
     status_t saveRights(int uniqueId, const DrmRights& drmRights,
             const String8& rightsPath, const String8& contentPath);
 
-    String8 getOriginalMimeType(int uniqueId, const String8& path);
+    String8 getOriginalMimeType(int uniqueId, const String8& path, int fd);
 
     int getDrmObjectType(int uniqueId, const String8& path, const String8& mimeType);
 
diff --git a/drm/libdrmframework/include/IDrmManagerService.h b/drm/libdrmframework/include/IDrmManagerService.h
index 5b89c91..5a4d70a 100644
--- a/drm/libdrmframework/include/IDrmManagerService.h
+++ b/drm/libdrmframework/include/IDrmManagerService.h
@@ -109,7 +109,7 @@
     virtual status_t saveRights(int uniqueId, const DrmRights& drmRights,
             const String8& rightsPath, const String8& contentPath) = 0;
 
-    virtual String8 getOriginalMimeType(int uniqueId, const String8& path) = 0;
+    virtual String8 getOriginalMimeType(int uniqueId, const String8& path, int fd) = 0;
 
     virtual int getDrmObjectType(
             int uniqueId, const String8& path, const String8& mimeType) = 0;
@@ -200,7 +200,7 @@
     virtual status_t saveRights(int uniqueId, const DrmRights& drmRights,
             const String8& rightsPath, const String8& contentPath);
 
-    virtual String8 getOriginalMimeType(int uniqueId, const String8& path);
+    virtual String8 getOriginalMimeType(int uniqueId, const String8& path, int fd);
 
     virtual int getDrmObjectType(int uniqueId, const String8& path, const String8& mimeType);
 
diff --git a/drm/libdrmframework/plugins/common/include/DrmEngineBase.h b/drm/libdrmframework/plugins/common/include/DrmEngineBase.h
index 6cebb97..fa51c13 100644
--- a/drm/libdrmframework/plugins/common/include/DrmEngineBase.h
+++ b/drm/libdrmframework/plugins/common/include/DrmEngineBase.h
@@ -53,7 +53,7 @@
 
     DrmInfo* acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest);
 
-    String8 getOriginalMimeType(int uniqueId, const String8& path);
+    String8 getOriginalMimeType(int uniqueId, const String8& path, int fd);
 
     int getDrmObjectType(int uniqueId, const String8& path, const String8& mimeType);
 
@@ -222,10 +222,11 @@
      *
      * @param[in] uniqueId Unique identifier for a session
      * @param[in] path Path of the protected content
+     * @param[in] fd descriptor of the protected content as a file source
      * @return String8
      *     Returns mime-type of the original content, such as "video/mpeg"
      */
-    virtual String8 onGetOriginalMimeType(int uniqueId, const String8& path) = 0;
+    virtual String8 onGetOriginalMimeType(int uniqueId, const String8& path, int fd) = 0;
 
     /**
      * Retrieves the type of the protected object (content, rights, etc..)
diff --git a/drm/libdrmframework/plugins/common/include/IDrmEngine.h b/drm/libdrmframework/plugins/common/include/IDrmEngine.h
index 60f4c1b..acc8ed9 100644
--- a/drm/libdrmframework/plugins/common/include/IDrmEngine.h
+++ b/drm/libdrmframework/plugins/common/include/IDrmEngine.h
@@ -165,11 +165,12 @@
      * Retrieves the mime type embedded inside the original content
      *
      * @param[in] uniqueId Unique identifier for a session
-     * @param[in] path Path of the protected content
+     * @param[in] path Path of the content or null.
+     * @param[in] fd File descriptor of the protected content
      * @return String8
      *     Returns mime-type of the original content, such as "video/mpeg"
      */
-    virtual String8 getOriginalMimeType(int uniqueId, const String8& path) = 0;
+    virtual String8 getOriginalMimeType(int uniqueId, const String8& path, int fd) = 0;
 
     /**
      * Retrieves the type of the protected object (content, rights, etc..)
diff --git a/drm/libdrmframework/plugins/forward-lock/FwdLockEngine/include/FwdLockEngine.h b/drm/libdrmframework/plugins/forward-lock/FwdLockEngine/include/FwdLockEngine.h
index c0e408e..a31b951 100644
--- a/drm/libdrmframework/plugins/forward-lock/FwdLockEngine/include/FwdLockEngine.h
+++ b/drm/libdrmframework/plugins/forward-lock/FwdLockEngine/include/FwdLockEngine.h
@@ -150,11 +150,12 @@
  * Retrieves the mime type embedded inside the original content.
  *
  * @param uniqueId Unique identifier for a session
- * @param path Path of the protected content
+ * @param path Path of the conent or null.
+ * @param fd descriptor of the protected content as a file source
  * @return String8
  *       Returns mime-type of the original content, such as "video/mpeg"
  */
-String8 onGetOriginalMimeType(int uniqueId, const String8& path);
+String8 onGetOriginalMimeType(int uniqueId, const String8& path, int fd);
 
 /**
  * Retrieves the type of the protected object (content, rights, etc..)
diff --git a/drm/libdrmframework/plugins/forward-lock/FwdLockEngine/src/FwdLockEngine.cpp b/drm/libdrmframework/plugins/forward-lock/FwdLockEngine/src/FwdLockEngine.cpp
index 4b1b40e..234aef2 100644
--- a/drm/libdrmframework/plugins/forward-lock/FwdLockEngine/src/FwdLockEngine.cpp
+++ b/drm/libdrmframework/plugins/forward-lock/FwdLockEngine/src/FwdLockEngine.cpp
@@ -309,12 +309,15 @@
     return (onCheckRightsStatus(uniqueId, path, action) == RightsStatus::RIGHTS_VALID);
 }
 
-String8 FwdLockEngine::onGetOriginalMimeType(int uniqueId, const String8& path) {
+String8 FwdLockEngine::onGetOriginalMimeType(int uniqueId, const String8& path, int fd) {
     LOG_VERBOSE("FwdLockEngine::onGetOriginalMimeType");
     String8 mimeString = String8("");
-    int fileDesc = FwdLockFile_open(path.string());
+    int fileDesc = dup(fd);
 
     if (-1 < fileDesc) {
+        if (FwdLockFile_attach(fileDesc) < 0) {
+            return mimeString;
+        }
         const char* pMimeType = FwdLockFile_GetContentType(fileDesc);
 
         if (NULL != pMimeType) {
diff --git a/drm/libdrmframework/plugins/forward-lock/internal-format/decoder/FwdLockFile.c b/drm/libdrmframework/plugins/forward-lock/internal-format/decoder/FwdLockFile.c
index 365bdec..7ff3c00 100644
--- a/drm/libdrmframework/plugins/forward-lock/internal-format/decoder/FwdLockFile.c
+++ b/drm/libdrmframework/plugins/forward-lock/internal-format/decoder/FwdLockFile.c
@@ -293,20 +293,6 @@
     return (sessionId >= 0) ? 0 : -1;
 }
 
-int FwdLockFile_open(const char *pFilename) {
-    int fileDesc = open(pFilename, O_RDONLY);
-    if (fileDesc < 0) {
-        ALOGE("failed to open file '%s': %s", pFilename, strerror(errno));
-        return fileDesc;
-    }
-
-    if (FwdLockFile_attach(fileDesc) < 0) {
-        (void)close(fileDesc);
-        fileDesc = -1;
-    }
-    return fileDesc;
-}
-
 ssize_t FwdLockFile_read(int fileDesc, void *pBuffer, size_t numBytes) {
     ssize_t numBytesRead;
     int sessionId = FwdLockFile_FindSession(fileDesc);
diff --git a/drm/libdrmframework/plugins/forward-lock/internal-format/decoder/FwdLockFile.h b/drm/libdrmframework/plugins/forward-lock/internal-format/decoder/FwdLockFile.h
index fc64050..187505b 100644
--- a/drm/libdrmframework/plugins/forward-lock/internal-format/decoder/FwdLockFile.h
+++ b/drm/libdrmframework/plugins/forward-lock/internal-format/decoder/FwdLockFile.h
@@ -36,16 +36,6 @@
 int FwdLockFile_attach(int fileDesc);
 
 /**
- * Opens a Forward Lock file for reading.
- *
- * @param[in] pFilename A reference to a filename.
- *
- * @return A file descriptor.
- * @retval -1 Failure.
- */
-int FwdLockFile_open(const char *pFilename);
-
-/**
  * Reads the specified number of bytes from an open Forward Lock file.
  *
  * @param[in] fileDesc The file descriptor of an open Forward Lock file.
diff --git a/drm/libdrmframework/plugins/passthru/include/DrmPassthruPlugIn.h b/drm/libdrmframework/plugins/passthru/include/DrmPassthruPlugIn.h
index f941f70..7b66dc7 100644
--- a/drm/libdrmframework/plugins/passthru/include/DrmPassthruPlugIn.h
+++ b/drm/libdrmframework/plugins/passthru/include/DrmPassthruPlugIn.h
@@ -47,7 +47,7 @@
 
     DrmInfo* onAcquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest);
 
-    String8 onGetOriginalMimeType(int uniqueId, const String8& path);
+    String8 onGetOriginalMimeType(int uniqueId, const String8& path, int fd);
 
     int onGetDrmObjectType(int uniqueId, const String8& path, const String8& mimeType);
 
diff --git a/drm/libdrmframework/plugins/passthru/src/DrmPassthruPlugIn.cpp b/drm/libdrmframework/plugins/passthru/src/DrmPassthruPlugIn.cpp
index a3eac3e..fa659fd 100644
--- a/drm/libdrmframework/plugins/passthru/src/DrmPassthruPlugIn.cpp
+++ b/drm/libdrmframework/plugins/passthru/src/DrmPassthruPlugIn.cpp
@@ -163,7 +163,7 @@
     return (String8(".passthru") == extension);
 }
 
-String8 DrmPassthruPlugIn::onGetOriginalMimeType(int uniqueId, const String8& path) {
+String8 DrmPassthruPlugIn::onGetOriginalMimeType(int uniqueId, const String8& path, int fd) {
     ALOGV("DrmPassthruPlugIn::onGetOriginalMimeType() : %d", uniqueId);
     return String8("video/passthru");
 }
diff --git a/include/drm/DrmManagerClient.h b/include/drm/DrmManagerClient.h
index a5c6992..866edac 100644
--- a/include/drm/DrmManagerClient.h
+++ b/include/drm/DrmManagerClient.h
@@ -279,10 +279,11 @@
      * Retrieves the mime type embedded inside the original content
      *
      * @param[in] path the path of the protected content
+     * @param[in] fd the file descriptor of the protected content
      * @return String8
      *     Returns mime-type of the original content, such as "video/mpeg"
      */
-    String8 getOriginalMimeType(const String8& path);
+    String8 getOriginalMimeType(const String8& path, int fd);
 
     /**
      * Retrieves the type of the protected object (content, rights, etc..)