127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi/*
227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi * Copyright (C) 2010 The Android Open Source Project
327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi *
427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi * Licensed under the Apache License, Version 2.0 (the "License");
527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi * you may not use this file except in compliance with the License.
627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi * You may obtain a copy of the License at
727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi *
827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi *      http://www.apache.org/licenses/LICENSE-2.0
927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi *
1027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi * Unless required by applicable law or agreed to in writing, software
1127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi * distributed under the License is distributed on an "AS IS" BASIS,
1227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi * See the License for the specific language governing permissions and
1427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi * limitations under the License.
1527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi */
1627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
172272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi//#define LOG_NDEBUG 0
1827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi#define LOG_TAG "DrmManager(Native)"
1927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi#include "utils/Log.h"
2027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
2127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi#include <utils/String8.h>
2227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi#include <drm/DrmInfo.h>
2327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi#include <drm/DrmInfoEvent.h>
2427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi#include <drm/DrmRights.h>
2527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi#include <drm/DrmConstraints.h>
263473846f64f5b28e1cbeb70ef5867073fc93159eTakeshi Aimi#include <drm/DrmMetadata.h>
2727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi#include <drm/DrmInfoStatus.h>
2827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi#include <drm/DrmInfoRequest.h>
2927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi#include <drm/DrmSupportInfo.h>
3027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi#include <drm/DrmConvertedStatus.h>
3127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi#include <IDrmEngine.h>
3227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
3327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi#include "DrmManager.h"
3427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi#include "ReadWriteUtils.h"
3527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
3627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi#define DECRYPT_FILE_ERROR -1
3727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
3827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshiusing namespace android;
3927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
4027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshiconst String8 DrmManager::EMPTY_STRING("");
4127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
4227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshiDrmManager::DrmManager() :
4327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    mDecryptSessionId(0),
4427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    mConvertId(0) {
4527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
4627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
4727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
4827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshiDrmManager::~DrmManager() {
4927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
5027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
5127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
528f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wangint DrmManager::addUniqueId(bool isNative) {
536b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang    Mutex::Autolock _l(mLock);
548f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang
558f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang    int temp = 0;
568f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang    bool foundUniqueId = false;
578f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang    const int size = mUniqueIdVector.size();
588f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang    const int uniqueIdRange = 0xfff;
598f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang    int maxLoopTimes = (uniqueIdRange - 1) / 2;
608f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang    srand(time(NULL));
618f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang
628f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang    while (!foundUniqueId) {
638f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang        temp = rand() & uniqueIdRange;
648f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang
658f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang        if (isNative) {
668f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang            // set a flag to differentiate DrmManagerClient
678f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang            // created from native side and java side
688f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang            temp |= 0x1000;
698f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang        }
708f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang
718f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang        int index = 0;
728f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang        for (; index < size; ++index) {
738f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang            if (mUniqueIdVector.itemAt(index) == temp) {
748f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang                foundUniqueId = false;
758f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang                break;
762272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi            }
772272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi        }
788f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang        if (index == size) {
798f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang            foundUniqueId = true;
808f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang        }
818f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang
828f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang        maxLoopTimes --;
838f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang        LOG_FATAL_IF(maxLoopTimes <= 0, "cannot find an unique ID for this session");
842272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    }
858f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang
868f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang    mUniqueIdVector.push(temp);
878f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang    return temp;
882272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi}
892272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi
902272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimivoid DrmManager::removeUniqueId(int uniqueId) {
916b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang    Mutex::Autolock _l(mLock);
922272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    for (unsigned int i = 0; i < mUniqueIdVector.size(); i++) {
932272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi        if (uniqueId == mUniqueIdVector.itemAt(i)) {
942272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi            mUniqueIdVector.removeAt(i);
952272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi            break;
962272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi        }
972272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    }
982272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi}
992272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi
100e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimistatus_t DrmManager::loadPlugIns() {
1015f6f4e4e89bc9b234d89276aea3eae7f677e2ff7Edwin Wong
1025f6f4e4e89bc9b234d89276aea3eae7f677e2ff7Edwin Wong    String8 vendorPluginDirPath("/vendor/lib/drm");
1035f6f4e4e89bc9b234d89276aea3eae7f677e2ff7Edwin Wong    loadPlugIns(vendorPluginDirPath);
1045f6f4e4e89bc9b234d89276aea3eae7f677e2ff7Edwin Wong
105785ee06d106cd7958e0c151ebc6b7174d9ba861eJames Dong    String8 pluginDirPath("/system/lib/drm");
106785ee06d106cd7958e0c151ebc6b7174d9ba861eJames Dong    loadPlugIns(pluginDirPath);
1075f6f4e4e89bc9b234d89276aea3eae7f677e2ff7Edwin Wong    return DRM_NO_ERROR;
1085f6f4e4e89bc9b234d89276aea3eae7f677e2ff7Edwin Wong
10927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
11027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
111e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimistatus_t DrmManager::loadPlugIns(const String8& plugInDirPath) {
1125f6f4e4e89bc9b234d89276aea3eae7f677e2ff7Edwin Wong    mPlugInManager.loadPlugIns(plugInDirPath);
1135f6f4e4e89bc9b234d89276aea3eae7f677e2ff7Edwin Wong    Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
1145f6f4e4e89bc9b234d89276aea3eae7f677e2ff7Edwin Wong    for (unsigned int i = 0; i < plugInPathList.size(); ++i) {
1155f6f4e4e89bc9b234d89276aea3eae7f677e2ff7Edwin Wong        String8 plugInPath = plugInPathList[i];
1165f6f4e4e89bc9b234d89276aea3eae7f677e2ff7Edwin Wong        DrmSupportInfo* info = mPlugInManager.getPlugIn(plugInPath).getSupportInfo(0);
1175f6f4e4e89bc9b234d89276aea3eae7f677e2ff7Edwin Wong        if (NULL != info) {
1185f6f4e4e89bc9b234d89276aea3eae7f677e2ff7Edwin Wong            if (mSupportInfoToPlugInIdMap.indexOfKey(*info) < 0) {
119e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi                mSupportInfoToPlugInIdMap.add(*info, plugInPath);
120e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi            }
1215f6f4e4e89bc9b234d89276aea3eae7f677e2ff7Edwin Wong            delete info;
122e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi        }
12327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
124e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi    return DRM_NO_ERROR;
125e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi}
12627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
127e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimistatus_t DrmManager::unloadPlugIns() {
1286b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang    Mutex::Autolock _l(mLock);
129e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi    mConvertSessionMap.clear();
130e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi    mDecryptSessionMap.clear();
131e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi    mPlugInManager.unloadPlugIns();
132e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi    mSupportInfoToPlugInIdMap.clear();
13327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return DRM_NO_ERROR;
13427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
13527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
13627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshistatus_t DrmManager::setDrmServiceListener(
13727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            int uniqueId, const sp<IDrmServiceListener>& drmServiceListener) {
1380e0a5f9e0b7a7ca618e8a1f7ccecc57a17e0d1e2Gloria Wang    Mutex::Autolock _l(mListenerLock);
139c618b5af98dec06d49374a61a5a94016f9fec2d3Takeshi Aimi    if (NULL != drmServiceListener.get()) {
140c618b5af98dec06d49374a61a5a94016f9fec2d3Takeshi Aimi        mServiceListeners.add(uniqueId, drmServiceListener);
141c618b5af98dec06d49374a61a5a94016f9fec2d3Takeshi Aimi    } else {
142c618b5af98dec06d49374a61a5a94016f9fec2d3Takeshi Aimi        mServiceListeners.removeItem(uniqueId);
143c618b5af98dec06d49374a61a5a94016f9fec2d3Takeshi Aimi    }
14427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return DRM_NO_ERROR;
14527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
14627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
147e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimivoid DrmManager::addClient(int uniqueId) {
1486b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang    Mutex::Autolock _l(mLock);
149e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi    if (!mSupportInfoToPlugInIdMap.isEmpty()) {
150e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi        Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
151e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi        for (unsigned int index = 0; index < plugInIdList.size(); index++) {
152e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi            IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
153e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi            rDrmEngine.initialize(uniqueId);
154e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi            rDrmEngine.setOnInfoListener(uniqueId, this);
155e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi        }
156e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi    }
157e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi}
15827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
159e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimivoid DrmManager::removeClient(int uniqueId) {
1606b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang    Mutex::Autolock _l(mLock);
161e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi    Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
16227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    for (unsigned int index = 0; index < plugInIdList.size(); index++) {
16327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
16427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        rDrmEngine.terminate(uniqueId);
16527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
16627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
16727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
16827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshiDrmConstraints* DrmManager::getConstraints(int uniqueId, const String8* path, const int action) {
1696b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang    Mutex::Autolock _l(mLock);
17027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, *path);
17127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (EMPTY_STRING != plugInId) {
17227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
17327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        return rDrmEngine.getConstraints(uniqueId, path, action);
17427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
17527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return NULL;
17627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
17727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
1783473846f64f5b28e1cbeb70ef5867073fc93159eTakeshi AimiDrmMetadata* DrmManager::getMetadata(int uniqueId, const String8* path) {
1796b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang    Mutex::Autolock _l(mLock);
1803473846f64f5b28e1cbeb70ef5867073fc93159eTakeshi Aimi    const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, *path);
1813473846f64f5b28e1cbeb70ef5867073fc93159eTakeshi Aimi    if (EMPTY_STRING != plugInId) {
1823473846f64f5b28e1cbeb70ef5867073fc93159eTakeshi Aimi        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
1833473846f64f5b28e1cbeb70ef5867073fc93159eTakeshi Aimi        return rDrmEngine.getMetadata(uniqueId, path);
1843473846f64f5b28e1cbeb70ef5867073fc93159eTakeshi Aimi    }
1853473846f64f5b28e1cbeb70ef5867073fc93159eTakeshi Aimi    return NULL;
1863473846f64f5b28e1cbeb70ef5867073fc93159eTakeshi Aimi}
1873473846f64f5b28e1cbeb70ef5867073fc93159eTakeshi Aimi
18827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshistatus_t DrmManager::installDrmEngine(int uniqueId, const String8& absolutePath) {
1896b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang    Mutex::Autolock _l(mLock);
19027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    mPlugInManager.loadPlugIn(absolutePath);
19127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
19227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(absolutePath);
19327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    rDrmEngine.initialize(uniqueId);
19427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    rDrmEngine.setOnInfoListener(uniqueId, this);
19527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
196e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi    DrmSupportInfo* info = rDrmEngine.getSupportInfo(0);
19727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    mSupportInfoToPlugInIdMap.add(*info, absolutePath);
198adfb5592622305090ad39dcc2b7df477736da2c6James Dong    delete info;
19927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
20027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return DRM_NO_ERROR;
20127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
20227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
20327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshibool DrmManager::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
2046b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang    Mutex::Autolock _l(mLock);
20527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    const String8 plugInId = getSupportedPlugInId(mimeType);
20627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    bool result = (EMPTY_STRING != plugInId) ? true : false;
20727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
208e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi    if (0 < path.length()) {
20927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        if (result) {
21027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
21127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            result = rDrmEngine.canHandle(uniqueId, path);
21227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        } else {
2137f89d09c0f4a47119834ba15789260b933123ea5Gloria Wang            String8 extension = path.getPathExtension();
2147f89d09c0f4a47119834ba15789260b933123ea5Gloria Wang            if (String8("") != extension) {
2157f89d09c0f4a47119834ba15789260b933123ea5Gloria Wang                result = canHandle(uniqueId, path);
2167f89d09c0f4a47119834ba15789260b933123ea5Gloria Wang            }
21727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        }
21827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
21927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return result;
22027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
22127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
22227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshiDrmInfoStatus* DrmManager::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
2236b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang    Mutex::Autolock _l(mLock);
22427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    const String8 plugInId = getSupportedPlugInId(drmInfo->getMimeType());
22527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (EMPTY_STRING != plugInId) {
22627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
22727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        return rDrmEngine.processDrmInfo(uniqueId, drmInfo);
22827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
22927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return NULL;
23027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
23127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
23227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshibool DrmManager::canHandle(int uniqueId, const String8& path) {
23327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    bool result = false;
23427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
23527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
23627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    for (unsigned int i = 0; i < plugInPathList.size(); ++i) {
23727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInPathList[i]);
23827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        result = rDrmEngine.canHandle(uniqueId, path);
23927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
24027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        if (result) {
24127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            break;
24227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        }
24327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
24427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return result;
24527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
24627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
24727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshiDrmInfo* DrmManager::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) {
2486b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang    Mutex::Autolock _l(mLock);
24927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    const String8 plugInId = getSupportedPlugInId(drmInfoRequest->getMimeType());
25027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (EMPTY_STRING != plugInId) {
25127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
25227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        return rDrmEngine.acquireDrmInfo(uniqueId, drmInfoRequest);
25327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
25427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return NULL;
25527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
25627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
2572272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimistatus_t DrmManager::saveRights(int uniqueId, const DrmRights& drmRights,
25827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            const String8& rightsPath, const String8& contentPath) {
2596b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang    Mutex::Autolock _l(mLock);
26027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    const String8 plugInId = getSupportedPlugInId(drmRights.getMimeType());
2612272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    status_t result = DRM_ERROR_UNKNOWN;
26227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (EMPTY_STRING != plugInId) {
26327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
2642272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi        result = rDrmEngine.saveRights(uniqueId, drmRights, rightsPath, contentPath);
26527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
2662272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    return result;
26727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
26827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
269bf5b3b29e31b293313788d7464cfb258ac0da803James DongString8 DrmManager::getOriginalMimeType(int uniqueId, const String8& path, int fd) {
2706b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang    Mutex::Autolock _l(mLock);
27127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
27227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (EMPTY_STRING != plugInId) {
27327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
274bf5b3b29e31b293313788d7464cfb258ac0da803James Dong        return rDrmEngine.getOriginalMimeType(uniqueId, path, fd);
27527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
27627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return EMPTY_STRING;
27727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
27827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
27927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshiint DrmManager::getDrmObjectType(int uniqueId, const String8& path, const String8& mimeType) {
2806b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang    Mutex::Autolock _l(mLock);
28127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    const String8 plugInId = getSupportedPlugInId(uniqueId, path, mimeType);
28227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (EMPTY_STRING != plugInId) {
28327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
28427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        return rDrmEngine.getDrmObjectType(uniqueId, path, mimeType);
28527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
28627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return DrmObjectType::UNKNOWN;
28727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
28827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
28927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshiint DrmManager::checkRightsStatus(int uniqueId, const String8& path, int action) {
2906b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang    Mutex::Autolock _l(mLock);
29127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
29227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (EMPTY_STRING != plugInId) {
29327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
29427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        return rDrmEngine.checkRightsStatus(uniqueId, path, action);
29527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
29627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return RightsStatus::RIGHTS_INVALID;
29727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
29827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
2992272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimistatus_t DrmManager::consumeRights(
30027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
3012272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    status_t result = DRM_ERROR_UNKNOWN;
3026b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang    Mutex::Autolock _l(mDecryptLock);
30327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
30427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
3052272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi        result = drmEngine->consumeRights(uniqueId, decryptHandle, action, reserve);
30627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
3072272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    return result;
30827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
30927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
3102272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimistatus_t DrmManager::setPlaybackStatus(
311a2cd44cb5067b4fe98794860690394254d3ac73cGloria Wang    int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int64_t position) {
3122272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    status_t result = DRM_ERROR_UNKNOWN;
3136b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang    Mutex::Autolock _l(mDecryptLock);
31427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
31527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
3162272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi        result = drmEngine->setPlaybackStatus(uniqueId, decryptHandle, playbackStatus, position);
31727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
3182272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    return result;
31927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
32027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
32127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshibool DrmManager::validateAction(
32227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    int uniqueId, const String8& path, int action, const ActionDescription& description) {
3236b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang    Mutex::Autolock _l(mLock);
32427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
32527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (EMPTY_STRING != plugInId) {
32627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
32727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        return rDrmEngine.validateAction(uniqueId, path, action, description);
32827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
32927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return false;
33027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
33127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
3322272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimistatus_t DrmManager::removeRights(int uniqueId, const String8& path) {
3336b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang    Mutex::Autolock _l(mLock);
33427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
3352272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    status_t result = DRM_ERROR_UNKNOWN;
33627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (EMPTY_STRING != plugInId) {
33727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
3382272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi        result = rDrmEngine.removeRights(uniqueId, path);
33927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
3402272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    return result;
34127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
34227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
3432272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimistatus_t DrmManager::removeAllRights(int uniqueId) {
34427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
3452272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    status_t result = DRM_ERROR_UNKNOWN;
34627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    for (unsigned int index = 0; index < plugInIdList.size(); index++) {
34727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
3482272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi        result = rDrmEngine.removeAllRights(uniqueId);
3492272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi        if (DRM_NO_ERROR != result) {
3502272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi            break;
3512272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi        }
35227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
3532272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    return result;
35427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
35527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
35627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshiint DrmManager::openConvertSession(int uniqueId, const String8& mimeType) {
3576b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang    Mutex::Autolock _l(mConvertLock);
35827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    int convertId = -1;
35927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
36027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    const String8 plugInId = getSupportedPlugInId(mimeType);
36127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (EMPTY_STRING != plugInId) {
36227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
36327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
3642272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi        if (DRM_NO_ERROR == rDrmEngine.openConvertSession(uniqueId, mConvertId + 1)) {
3652272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi            ++mConvertId;
3662272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi            convertId = mConvertId;
3672272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi            mConvertSessionMap.add(convertId, &rDrmEngine);
3682272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi        }
36927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
37027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return convertId;
37127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
37227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
37327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshiDrmConvertedStatus* DrmManager::convertData(
37427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            int uniqueId, int convertId, const DrmBuffer* inputData) {
37527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    DrmConvertedStatus *drmConvertedStatus = NULL;
37627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
3776b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang    Mutex::Autolock _l(mConvertLock);
37827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (mConvertSessionMap.indexOfKey(convertId) != NAME_NOT_FOUND) {
37927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        IDrmEngine* drmEngine = mConvertSessionMap.valueFor(convertId);
38027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        drmConvertedStatus = drmEngine->convertData(uniqueId, convertId, inputData);
38127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
38227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return drmConvertedStatus;
38327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
38427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
38527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshiDrmConvertedStatus* DrmManager::closeConvertSession(int uniqueId, int convertId) {
3866b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang    Mutex::Autolock _l(mConvertLock);
38727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    DrmConvertedStatus *drmConvertedStatus = NULL;
38827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
38927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (mConvertSessionMap.indexOfKey(convertId) != NAME_NOT_FOUND) {
39027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        IDrmEngine* drmEngine = mConvertSessionMap.valueFor(convertId);
39127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        drmConvertedStatus = drmEngine->closeConvertSession(uniqueId, convertId);
39227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        mConvertSessionMap.removeItem(convertId);
39327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
39427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return drmConvertedStatus;
39527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
39627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
39727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshistatus_t DrmManager::getAllSupportInfo(
39827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi                    int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
3996b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang    Mutex::Autolock _l(mLock);
40027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
40127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    int size = plugInPathList.size();
40227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    int validPlugins = 0;
40327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
40427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (0 < size) {
40527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        Vector<DrmSupportInfo> drmSupportInfoList;
40627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
40727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        for (int i = 0; i < size; ++i) {
40827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            String8 plugInPath = plugInPathList[i];
40927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            DrmSupportInfo* drmSupportInfo
410e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi                = mPlugInManager.getPlugIn(plugInPath).getSupportInfo(0);
41127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            if (NULL != drmSupportInfo) {
41227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi                drmSupportInfoList.add(*drmSupportInfo);
41327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi                delete drmSupportInfo; drmSupportInfo = NULL;
41427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            }
41527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        }
41627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
41727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        validPlugins = drmSupportInfoList.size();
41827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        if (0 < validPlugins) {
41927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            *drmSupportInfoArray = new DrmSupportInfo[validPlugins];
42027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            for (int i = 0; i < validPlugins; ++i) {
42127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi                (*drmSupportInfoArray)[i] = drmSupportInfoList[i];
42227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            }
42327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        }
42427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
42527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    *length = validPlugins;
42627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return DRM_NO_ERROR;
42727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
42827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
4299d2f386dd2885eaffa11fd494ae258bb09fe6397James DongDecryptHandle* DrmManager::openDecryptSession(
4309d2f386dd2885eaffa11fd494ae258bb09fe6397James Dong        int uniqueId, int fd, off64_t offset, off64_t length, const char* mime) {
4319d2f386dd2885eaffa11fd494ae258bb09fe6397James Dong
432e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi    Mutex::Autolock _l(mDecryptLock);
43327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    status_t result = DRM_ERROR_CANNOT_HANDLE;
43427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
43527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
43627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    DecryptHandle* handle = new DecryptHandle();
43727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (NULL != handle) {
43827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        handle->decryptId = mDecryptSessionId + 1;
43927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
44027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        for (unsigned int index = 0; index < plugInIdList.size(); index++) {
44127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            String8 plugInId = plugInIdList.itemAt(index);
44227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
4439d2f386dd2885eaffa11fd494ae258bb09fe6397James Dong            result = rDrmEngine.openDecryptSession(uniqueId, handle, fd, offset, length, mime);
44427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
44527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            if (DRM_NO_ERROR == result) {
44627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi                ++mDecryptSessionId;
44727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi                mDecryptSessionMap.add(mDecryptSessionId, &rDrmEngine);
44827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi                break;
44927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            }
45027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        }
45127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
4522272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    if (DRM_NO_ERROR != result) {
45327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        delete handle; handle = NULL;
45427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
455e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi    return handle;
456e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi}
457e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi
4589d2f386dd2885eaffa11fd494ae258bb09fe6397James DongDecryptHandle* DrmManager::openDecryptSession(
4599d2f386dd2885eaffa11fd494ae258bb09fe6397James Dong        int uniqueId, const char* uri, const char* mime) {
460e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi    Mutex::Autolock _l(mDecryptLock);
461e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi    status_t result = DRM_ERROR_CANNOT_HANDLE;
462e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi    Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
46327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
464e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi    DecryptHandle* handle = new DecryptHandle();
465e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi    if (NULL != handle) {
466e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi        handle->decryptId = mDecryptSessionId + 1;
467e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi
468e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi        for (unsigned int index = 0; index < plugInIdList.size(); index++) {
469e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi            String8 plugInId = plugInIdList.itemAt(index);
470e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi            IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
4719d2f386dd2885eaffa11fd494ae258bb09fe6397James Dong            result = rDrmEngine.openDecryptSession(uniqueId, handle, uri, mime);
47227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
473e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi            if (DRM_NO_ERROR == result) {
474e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi                ++mDecryptSessionId;
475e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi                mDecryptSessionMap.add(mDecryptSessionId, &rDrmEngine);
476e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi                break;
477e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi            }
478e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi        }
479e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi    }
480e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi    if (DRM_NO_ERROR != result) {
481e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi        delete handle; handle = NULL;
4823856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("DrmManager::openDecryptSession: no capable plug-in found");
483e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi    }
48427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return handle;
48527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
48627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
487cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei TakahashiDecryptHandle* DrmManager::openDecryptSession(
488cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi        int uniqueId, const DrmBuffer& buf, const String8& mimeType) {
489cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi    Mutex::Autolock _l(mDecryptLock);
490cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi    status_t result = DRM_ERROR_CANNOT_HANDLE;
491cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi    Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
492cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi
493cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi    DecryptHandle* handle = new DecryptHandle();
494cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi    if (NULL != handle) {
495cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi        handle->decryptId = mDecryptSessionId + 1;
496cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi
497cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi        for (size_t index = 0; index < plugInIdList.size(); index++) {
498cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi            String8 plugInId = plugInIdList.itemAt(index);
499cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi            IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
500cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi            result = rDrmEngine.openDecryptSession(uniqueId, handle, buf, mimeType);
501cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi
502cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi            if (DRM_NO_ERROR == result) {
503cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi                ++mDecryptSessionId;
504cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi                mDecryptSessionMap.add(mDecryptSessionId, &rDrmEngine);
505cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi                break;
506cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi            }
507cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi        }
508cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi    }
509cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi    if (DRM_NO_ERROR != result) {
510cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi        delete handle;
511cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi        handle = NULL;
512cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi        ALOGV("DrmManager::openDecryptSession: no capable plug-in found");
513cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi    }
514cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi    return handle;
515cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi}
516cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi
5172272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimistatus_t DrmManager::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
518e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi    Mutex::Autolock _l(mDecryptLock);
5192272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    status_t result = DRM_ERROR_UNKNOWN;
52027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
52127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
5222272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi        result = drmEngine->closeDecryptSession(uniqueId, decryptHandle);
5232272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi        if (DRM_NO_ERROR == result) {
5242272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi            mDecryptSessionMap.removeItem(decryptHandle->decryptId);
5252272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi        }
52627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
5272272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    return result;
52827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
52927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
5302272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimistatus_t DrmManager::initializeDecryptUnit(
53127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId, const DrmBuffer* headerInfo) {
5322272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    status_t result = DRM_ERROR_UNKNOWN;
5336b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang    Mutex::Autolock _l(mDecryptLock);
53427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
53527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
5362272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi        result = drmEngine->initializeDecryptUnit(uniqueId, decryptHandle, decryptUnitId, headerInfo);
53727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
5382272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    return result;
53927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
54027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
5412272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimistatus_t DrmManager::decrypt(int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId,
5422272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi            const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
5432272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    status_t result = DRM_ERROR_UNKNOWN;
5446b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang
5456b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang    Mutex::Autolock _l(mDecryptLock);
54627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
54727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
5482272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi        result = drmEngine->decrypt(
5492272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi                uniqueId, decryptHandle, decryptUnitId, encBuffer, decBuffer, IV);
55027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
5512272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    return result;
55227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
55327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
5542272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimistatus_t DrmManager::finalizeDecryptUnit(
55527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) {
5562272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    status_t result = DRM_ERROR_UNKNOWN;
5576b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang    Mutex::Autolock _l(mDecryptLock);
55827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
55927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
5602272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi        result = drmEngine->finalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId);
56127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
5622272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    return result;
56327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
56427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
56527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshissize_t DrmManager::pread(int uniqueId, DecryptHandle* decryptHandle,
566a2cd44cb5067b4fe98794860690394254d3ac73cGloria Wang            void* buffer, ssize_t numBytes, off64_t offset) {
56727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    ssize_t result = DECRYPT_FILE_ERROR;
56827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
5696b610a3d31a225adfc5867223df25dba16637bd9Gloria Wang    Mutex::Autolock _l(mDecryptLock);
57027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
57127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
572