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 "DrmManagerClientImpl(Native)"
1927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi#include <utils/Log.h>
2027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
2127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi#include <utils/String8.h>
2227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi#include <utils/Vector.h>
2327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi#include <binder/IServiceManager.h>
2427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
2527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi#include "DrmManagerClientImpl.h"
2627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
2727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshiusing namespace android;
2827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
2927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi#define INVALID_VALUE -1
3027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
318d2577b9ac2f95f218db59a78447efd3c6a742ddGloria WangMutex DrmManagerClientImpl::sMutex;
328d2577b9ac2f95f218db59a78447efd3c6a742ddGloria Wangsp<IDrmManagerService> DrmManagerClientImpl::sDrmManagerService;
338d2577b9ac2f95f218db59a78447efd3c6a742ddGloria Wangsp<DrmManagerClientImpl::DeathNotifier> DrmManagerClientImpl::sDeathNotifier;
3427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshiconst String8 DrmManagerClientImpl::EMPTY_STRING("");
3527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
368f00151cbe693d52f3e233757c57fab3b6396d21Gloria WangDrmManagerClientImpl* DrmManagerClientImpl::create(
378f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang        int* pUniqueId, bool isNative) {
388f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang    *pUniqueId = getDrmManagerService()->addUniqueId(isNative);
398f00151cbe693d52f3e233757c57fab3b6396d21Gloria Wang
4027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return new DrmManagerClientImpl();
4127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
4227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
4327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshivoid DrmManagerClientImpl::remove(int uniqueId) {
442272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    getDrmManagerService()->removeUniqueId(uniqueId);
4527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
4627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
4727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshiconst sp<IDrmManagerService>& DrmManagerClientImpl::getDrmManagerService() {
488d2577b9ac2f95f218db59a78447efd3c6a742ddGloria Wang    Mutex::Autolock lock(sMutex);
498d2577b9ac2f95f218db59a78447efd3c6a742ddGloria Wang    if (NULL == sDrmManagerService.get()) {
5027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        sp<IServiceManager> sm = defaultServiceManager();
5127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        sp<IBinder> binder;
5227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        do {
5327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            binder = sm->getService(String16("drm.drmManager"));
5427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            if (binder != 0) {
5527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi                break;
5627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            }
575ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block            ALOGW("DrmManagerService not published, waiting...");
5827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            struct timespec reqt;
5927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            reqt.tv_sec  = 0;
6027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            reqt.tv_nsec = 500000000; //0.5 sec
6127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            nanosleep(&reqt, NULL);
6227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        } while (true);
638d2577b9ac2f95f218db59a78447efd3c6a742ddGloria Wang        if (NULL == sDeathNotifier.get()) {
648d2577b9ac2f95f218db59a78447efd3c6a742ddGloria Wang            sDeathNotifier = new DeathNotifier();
658d2577b9ac2f95f218db59a78447efd3c6a742ddGloria Wang        }
668d2577b9ac2f95f218db59a78447efd3c6a742ddGloria Wang        binder->linkToDeath(sDeathNotifier);
678d2577b9ac2f95f218db59a78447efd3c6a742ddGloria Wang        sDrmManagerService = interface_cast<IDrmManagerService>(binder);
6827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
698d2577b9ac2f95f218db59a78447efd3c6a742ddGloria Wang    return sDrmManagerService;
7027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
7127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
72e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimivoid DrmManagerClientImpl::addClient(int uniqueId) {
73e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi    getDrmManagerService()->addClient(uniqueId);
7427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
7527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
76e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimivoid DrmManagerClientImpl::removeClient(int uniqueId) {
77e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi    getDrmManagerService()->removeClient(uniqueId);
7827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
7927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
8027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshistatus_t DrmManagerClientImpl::setOnInfoListener(
81b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang            int uniqueId,
82b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang            const sp<DrmManagerClient::OnInfoListener>& infoListener) {
8327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    Mutex::Autolock _l(mLock);
8427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    mOnInfoListener = infoListener;
85c618b5af98dec06d49374a61a5a94016f9fec2d3Takeshi Aimi    return getDrmManagerService()->setDrmServiceListener(uniqueId,
86c618b5af98dec06d49374a61a5a94016f9fec2d3Takeshi Aimi            (NULL != infoListener.get()) ? this : NULL);
8727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
8827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
89b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wangstatus_t DrmManagerClientImpl::installDrmEngine(
90b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang        int uniqueId, const String8& drmEngineFile) {
9127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    status_t status = DRM_ERROR_UNKNOWN;
9227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (EMPTY_STRING != drmEngineFile) {
9327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        status = getDrmManagerService()->installDrmEngine(uniqueId, drmEngineFile);
9427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
9527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return status;
9627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
9727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
9827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshiDrmConstraints* DrmManagerClientImpl::getConstraints(
9927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        int uniqueId, const String8* path, const int action) {
10027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    DrmConstraints *drmConstraints = NULL;
10127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if ((NULL != path) && (EMPTY_STRING != *path)) {
102b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang        drmConstraints =
103b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang            getDrmManagerService()->getConstraints(uniqueId, path, action);
10427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
10527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return drmConstraints;
10627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
10727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
1083473846f64f5b28e1cbeb70ef5867073fc93159eTakeshi AimiDrmMetadata* DrmManagerClientImpl::getMetadata(int uniqueId, const String8* path) {
1093473846f64f5b28e1cbeb70ef5867073fc93159eTakeshi Aimi    DrmMetadata *drmMetadata = NULL;
1103473846f64f5b28e1cbeb70ef5867073fc93159eTakeshi Aimi    if ((NULL != path) && (EMPTY_STRING != *path)) {
1113473846f64f5b28e1cbeb70ef5867073fc93159eTakeshi Aimi        drmMetadata = getDrmManagerService()->getMetadata(uniqueId, path);
1123473846f64f5b28e1cbeb70ef5867073fc93159eTakeshi Aimi    }
1133473846f64f5b28e1cbeb70ef5867073fc93159eTakeshi Aimi    return drmMetadata;
1143473846f64f5b28e1cbeb70ef5867073fc93159eTakeshi Aimi}
1153473846f64f5b28e1cbeb70ef5867073fc93159eTakeshi Aimi
116b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wangbool DrmManagerClientImpl::canHandle(
117b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang        int uniqueId, const String8& path, const String8& mimeType) {
11827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    bool retCode = false;
11927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if ((EMPTY_STRING != path) || (EMPTY_STRING != mimeType)) {
12027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        retCode = getDrmManagerService()->canHandle(uniqueId, path, mimeType);
12127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
12227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return retCode;
12327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
12427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
125b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria WangDrmInfoStatus* DrmManagerClientImpl::processDrmInfo(
126b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang        int uniqueId, const DrmInfo* drmInfo) {
12727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    DrmInfoStatus *drmInfoStatus = NULL;
12827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (NULL != drmInfo) {
12927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        drmInfoStatus = getDrmManagerService()->processDrmInfo(uniqueId, drmInfo);
13027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
13127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return drmInfoStatus;
13227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
13327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
134b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria WangDrmInfo* DrmManagerClientImpl::acquireDrmInfo(
135b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang        int uniqueId, const DrmInfoRequest* drmInfoRequest) {
13627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    DrmInfo* drmInfo = NULL;
13727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (NULL != drmInfoRequest) {
13827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        drmInfo = getDrmManagerService()->acquireDrmInfo(uniqueId, drmInfoRequest);
13927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
14027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return drmInfo;
14127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
14227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
1432272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimistatus_t DrmManagerClientImpl::saveRights(int uniqueId, const DrmRights& drmRights,
14427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            const String8& rightsPath, const String8& contentPath) {
1458d2577b9ac2f95f218db59a78447efd3c6a742ddGloria Wang    return getDrmManagerService()->saveRights(
146b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang                uniqueId, drmRights, rightsPath, contentPath);
14727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
14827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
149b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria WangString8 DrmManagerClientImpl::getOriginalMimeType(
150bf5b3b29e31b293313788d7464cfb258ac0da803James Dong        int uniqueId, const String8& path, int fd) {
15127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    String8 mimeType = EMPTY_STRING;
15227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (EMPTY_STRING != path) {
153bf5b3b29e31b293313788d7464cfb258ac0da803James Dong        mimeType = getDrmManagerService()->getOriginalMimeType(uniqueId, path, fd);
15427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
15527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return mimeType;
15627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
15727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
15827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshiint DrmManagerClientImpl::getDrmObjectType(
15927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            int uniqueId, const String8& path, const String8& mimeType) {
16027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    int drmOjectType = DrmObjectType::UNKNOWN;
16127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if ((EMPTY_STRING != path) || (EMPTY_STRING != mimeType)) {
162b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang         drmOjectType =
163b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang             getDrmManagerService()->getDrmObjectType(uniqueId, path, mimeType);
16427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
16527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return drmOjectType;
16627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
16727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
16827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshiint DrmManagerClientImpl::checkRightsStatus(
16927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            int uniqueId, const String8& path, int action) {
17027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    int rightsStatus = RightsStatus::RIGHTS_INVALID;
17127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (EMPTY_STRING != path) {
172b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang        rightsStatus =
173b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang            getDrmManagerService()->checkRightsStatus(uniqueId, path, action);
17427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
17527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return rightsStatus;
17627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
17727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
1782272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimistatus_t DrmManagerClientImpl::consumeRights(
179b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang            int uniqueId, sp<DecryptHandle> &decryptHandle,
180b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang            int action, bool reserve) {
1812272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    status_t status = DRM_ERROR_UNKNOWN;
182b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang    if (NULL != decryptHandle.get()) {
183b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang        status = getDrmManagerService()->consumeRights(
184b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang                uniqueId, decryptHandle.get(), action, reserve);
18527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
1862272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    return status;
18727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
18827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
1892272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimistatus_t DrmManagerClientImpl::setPlaybackStatus(
190b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang            int uniqueId, sp<DecryptHandle> &decryptHandle,
191b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang            int playbackStatus, int64_t position) {
1922272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    status_t status = DRM_ERROR_UNKNOWN;
193b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang    if (NULL != decryptHandle.get()) {
1942272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi        status = getDrmManagerService()->setPlaybackStatus(
195b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang                uniqueId, decryptHandle.get(), playbackStatus, position);
19627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
1972272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    return status;
19827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
19927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
20027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshibool DrmManagerClientImpl::validateAction(
201b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang            int uniqueId, const String8& path,
202b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang            int action, const ActionDescription& description) {
20327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    bool retCode = false;
20427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (EMPTY_STRING != path) {
205b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang        retCode = getDrmManagerService()->validateAction(
206b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang                uniqueId, path, action, description);
20727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
20827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return retCode;
20927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
21027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
2112272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimistatus_t DrmManagerClientImpl::removeRights(int uniqueId, const String8& path) {
2122272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    status_t status = DRM_ERROR_UNKNOWN;
21327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (EMPTY_STRING != path) {
2142272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi        status = getDrmManagerService()->removeRights(uniqueId, path);
21527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
2162272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    return status;
21727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
21827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
2192272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimistatus_t DrmManagerClientImpl::removeAllRights(int uniqueId) {
2202272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    return getDrmManagerService()->removeAllRights(uniqueId);
22127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
22227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
223b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wangint DrmManagerClientImpl::openConvertSession(
224b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang        int uniqueId, const String8& mimeType) {
22527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    int retCode = INVALID_VALUE;
22627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (EMPTY_STRING != mimeType) {
22727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        retCode = getDrmManagerService()->openConvertSession(uniqueId, mimeType);
22827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
22927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return retCode;
23027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
23127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
23227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshiDrmConvertedStatus* DrmManagerClientImpl::convertData(
23327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            int uniqueId, int convertId, const DrmBuffer* inputData) {
23427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    DrmConvertedStatus* drmConvertedStatus = NULL;
23527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (NULL != inputData) {
236b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang         drmConvertedStatus =
237b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang             getDrmManagerService()->convertData(uniqueId, convertId, inputData);
23827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
23927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return drmConvertedStatus;
24027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
24127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
242b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria WangDrmConvertedStatus* DrmManagerClientImpl::closeConvertSession(
243b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang        int uniqueId, int convertId) {
24427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return getDrmManagerService()->closeConvertSession(uniqueId, convertId);
24527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
24627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
24727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshistatus_t DrmManagerClientImpl::getAllSupportInfo(
24827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi            int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
24927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    status_t status = DRM_ERROR_UNKNOWN;
25027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if ((NULL != drmSupportInfoArray) && (NULL != length)) {
251b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang        status = getDrmManagerService()->getAllSupportInfo(
252b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang                uniqueId, length, drmSupportInfoArray);
25327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
25427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return status;
25527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
25627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
257b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wangsp<DecryptHandle> DrmManagerClientImpl::openDecryptSession(
2589d2f386dd2885eaffa11fd494ae258bb09fe6397James Dong            int uniqueId, int fd, off64_t offset,
2599d2f386dd2885eaffa11fd494ae258bb09fe6397James Dong            off64_t length, const char* mime) {
2609d2f386dd2885eaffa11fd494ae258bb09fe6397James Dong
2619d2f386dd2885eaffa11fd494ae258bb09fe6397James Dong    return getDrmManagerService()->openDecryptSession(
2629d2f386dd2885eaffa11fd494ae258bb09fe6397James Dong                uniqueId, fd, offset, length, mime);
26327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
26427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
265b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wangsp<DecryptHandle> DrmManagerClientImpl::openDecryptSession(
2669d2f386dd2885eaffa11fd494ae258bb09fe6397James Dong        int uniqueId, const char* uri, const char* mime) {
2679d2f386dd2885eaffa11fd494ae258bb09fe6397James Dong
268e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi    DecryptHandle* handle = NULL;
269e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi    if (NULL != uri) {
2709d2f386dd2885eaffa11fd494ae258bb09fe6397James Dong        handle = getDrmManagerService()->openDecryptSession(uniqueId, uri, mime);
271e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi    }
272e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi    return handle;
273e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi}
274e943f84129326ab885cc7a69dcfa17f766b72b89Takeshi Aimi
275cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashisp<DecryptHandle> DrmManagerClientImpl::openDecryptSession(
276cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi            int uniqueId, const DrmBuffer& buf, const String8& mimeType) {
277cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi    return getDrmManagerService()->openDecryptSession(uniqueId, buf, mimeType);
278cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi}
279cba7b32d8f2c47632313f54118ed3733b4b02cc8Kei Takahashi
280b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wangstatus_t DrmManagerClientImpl::closeDecryptSession(
281b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang        int uniqueId, sp<DecryptHandle> &decryptHandle) {
2822272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    status_t status = DRM_ERROR_UNKNOWN;
283b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang    if (NULL != decryptHandle.get()) {
284b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang        status = getDrmManagerService()->closeDecryptSession(
285b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang                uniqueId, decryptHandle.get());
28627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
2872272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    return status;
28827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
28927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
290b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wangstatus_t DrmManagerClientImpl::initializeDecryptUnit(
291b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang        int uniqueId, sp<DecryptHandle> &decryptHandle,
292b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang        int decryptUnitId, const DrmBuffer* headerInfo) {
2932272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    status_t status = DRM_ERROR_UNKNOWN;
294b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang    if ((NULL != decryptHandle.get()) && (NULL != headerInfo)) {
2952272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi        status = getDrmManagerService()->initializeDecryptUnit(
296b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang                uniqueId, decryptHandle.get(), decryptUnitId, headerInfo);
29727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
2982272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    return status;
29927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
30027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
301b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wangstatus_t DrmManagerClientImpl::decrypt(
302b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang        int uniqueId, sp<DecryptHandle> &decryptHandle,
303b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang        int decryptUnitId, const DrmBuffer* encBuffer,
304b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang        DrmBuffer** decBuffer, DrmBuffer* IV) {
30527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    status_t status = DRM_ERROR_UNKNOWN;
306b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang    if ((NULL != decryptHandle.get()) && (NULL != encBuffer)
30727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        && (NULL != decBuffer) && (NULL != *decBuffer)) {
30827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        status = getDrmManagerService()->decrypt(
309b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang                uniqueId, decryptHandle.get(), decryptUnitId,
310b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang                encBuffer, decBuffer, IV);
31127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
31227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return status;
31327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
31427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
3152272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimistatus_t DrmManagerClientImpl::finalizeDecryptUnit(
316b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang            int uniqueId, sp<DecryptHandle> &decryptHandle, int decryptUnitId) {
3172272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    status_t status = DRM_ERROR_UNKNOWN;
318b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang    if (NULL != decryptHandle.get()) {
319b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang        status = getDrmManagerService()->finalizeDecryptUnit(
320b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang                    uniqueId, decryptHandle.get(), decryptUnitId);
32127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
3222272ee27d9022d173b6eab45c409b3c3f57f30ecTakeshi Aimi    return status;
32327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
32427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
325b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wangssize_t DrmManagerClientImpl::pread(int uniqueId, sp<DecryptHandle> &decryptHandle,
326a2cd44cb5067b4fe98794860690394254d3ac73cGloria Wang            void* buffer, ssize_t numBytes, off64_t offset) {
32727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    ssize_t retCode = INVALID_VALUE;
328b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang    if ((NULL != decryptHandle.get()) && (NULL != buffer) && (0 < numBytes)) {
329b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang        retCode = getDrmManagerService()->pread(
330b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang                uniqueId, decryptHandle.get(), buffer, numBytes, offset);
33127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
33227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return retCode;
33327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
33427ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
33527ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshistatus_t DrmManagerClientImpl::notify(const DrmInfoEvent& event) {
33627ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    if (NULL != mOnInfoListener.get()) {
33727ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        Mutex::Autolock _l(mLock);
33827ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        sp<DrmManagerClient::OnInfoListener> listener = mOnInfoListener;
33927ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi        listener->onInfo(event);
34027ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    }
34127ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi    return DRM_NO_ERROR;
34227ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi}
34327ed8ad2db653f6ac07dcf8bcc05e2409c8bb024aimitakeshi
3448d2577b9ac2f95f218db59a78447efd3c6a742ddGloria WangDrmManagerClientImpl::DeathNotifier::~DeathNotifier() {
3458d2577b9ac2f95f218db59a78447efd3c6a742ddGloria Wang    Mutex::Autolock lock(sMutex);
3468d2577b9ac2f95f218db59a78447efd3c6a742ddGloria Wang    if (NULL != sDrmManagerService.get()) {
3478d2577b9ac2f95f218db59a78447efd3c6a742ddGloria Wang        sDrmManagerService->asBinder()->unlinkToDeath(this);
3488d2577b9ac2f95f218db59a78447efd3c6a742ddGloria Wang    }
3498d2577b9ac2f95f218db59a78447efd3c6a742ddGloria Wang}
3508d2577b9ac2f95f218db59a78447efd3c6a742ddGloria Wang
3518d2577b9ac2f95f218db59a78447efd3c6a742ddGloria Wangvoid DrmManagerClientImpl::DeathNotifier::binderDied(const wp<IBinder>& who) {
3528d2577b9ac2f95f218db59a78447efd3c6a742ddGloria Wang    Mutex::Autolock lock(sMutex);
3538d2577b9ac2f95f218db59a78447efd3c6a742ddGloria Wang    DrmManagerClientImpl::sDrmManagerService.clear();
3545ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block    ALOGW("DrmManager server died!");
3558d2577b9ac2f95f218db59a78447efd3c6a742ddGloria Wang}
3568d2577b9ac2f95f218db59a78447efd3c6a742ddGloria Wang
357