IDrmManagerService.cpp revision 1da9aa606096e14985924e8433a087d04f68ea22
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "IDrmManagerService(Native)"
19#include <utils/Log.h>
20
21#include <stdint.h>
22#include <sys/types.h>
23#include <binder/IPCThreadState.h>
24
25#include <drm/DrmInfo.h>
26#include <drm/DrmConstraints.h>
27#include <drm/DrmMetadata.h>
28#include <drm/DrmRights.h>
29#include <drm/DrmInfoStatus.h>
30#include <drm/DrmConvertedStatus.h>
31#include <drm/DrmInfoRequest.h>
32#include <drm/DrmSupportInfo.h>
33
34#include "IDrmManagerService.h"
35
36#define INVALID_BUFFER_LENGTH -1
37
38using namespace android;
39
40static void writeDecrptHandleToParcelData(
41        const DecryptHandle* handle, Parcel* data) {
42    data->writeInt32(handle->decryptId);
43    data->writeString8(handle->mimeType);
44    data->writeInt32(handle->decryptApiType);
45    data->writeInt32(handle->status);
46
47    int size = handle->copyControlVector.size();
48    data->writeInt32(size);
49    for(int i = 0; i < size; i++) {
50        data->writeInt32(handle->copyControlVector.keyAt(i));
51        data->writeInt32(handle->copyControlVector.valueAt(i));
52    }
53
54    if (NULL != handle->decryptInfo) {
55        data->writeInt32(handle->decryptInfo->decryptBufferLength);
56    } else {
57        data->writeInt32(INVALID_BUFFER_LENGTH);
58    }
59}
60
61static void readDecryptHandleFromParcelData(
62        DecryptHandle* handle, const Parcel& data) {
63    if (0 == data.dataAvail()) {
64        return;
65    }
66
67    handle->decryptId = data.readInt32();
68    handle->mimeType = data.readString8();
69    handle->decryptApiType = data.readInt32();
70    handle->status = data.readInt32();
71
72    int size = data.readInt32();
73    for (int i = 0; i < size; i ++) {
74        handle->copyControlVector.add(
75                (DrmCopyControl)data.readInt32(), data.readInt32());
76    }
77
78    handle->decryptInfo = NULL;
79    const int bufferLen = data.readInt32();
80    if (INVALID_BUFFER_LENGTH != bufferLen) {
81        handle->decryptInfo = new DecryptInfo();
82        handle->decryptInfo->decryptBufferLength = bufferLen;
83    }
84}
85
86static void clearDecryptHandle(DecryptHandle* handle) {
87    if (handle == NULL) {
88        return;
89    }
90    if (handle->decryptInfo) {
91        delete handle->decryptInfo;
92        handle->decryptInfo = NULL;
93    }
94    handle->copyControlVector.clear();
95}
96
97int BpDrmManagerService::addUniqueId(int uniqueId) {
98    LOGV("add uniqueid");
99    Parcel data, reply;
100    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
101    data.writeInt32(uniqueId);
102    remote()->transact(ADD_UNIQUEID, data, &reply);
103    return reply.readInt32();
104}
105
106void BpDrmManagerService::removeUniqueId(int uniqueId) {
107    LOGV("remove uniqueid");
108    Parcel data, reply;
109    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
110    data.writeInt32(uniqueId);
111    remote()->transact(REMOVE_UNIQUEID, data, &reply);
112}
113
114void BpDrmManagerService::addClient(int uniqueId) {
115    Parcel data, reply;
116    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
117    data.writeInt32(uniqueId);
118    remote()->transact(ADD_CLIENT, data, &reply);
119}
120
121void BpDrmManagerService::removeClient(int uniqueId) {
122    Parcel data, reply;
123    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
124    data.writeInt32(uniqueId);
125    remote()->transact(REMOVE_CLIENT, data, &reply);
126}
127
128status_t BpDrmManagerService::setDrmServiceListener(
129            int uniqueId, const sp<IDrmServiceListener>& drmServiceListener) {
130    LOGV("setDrmServiceListener");
131    Parcel data, reply;
132
133    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
134    data.writeInt32(uniqueId);
135    data.writeStrongBinder(drmServiceListener->asBinder());
136    remote()->transact(SET_DRM_SERVICE_LISTENER, data, &reply);
137    return reply.readInt32();
138}
139
140status_t BpDrmManagerService::installDrmEngine(int uniqueId, const String8& drmEngineFile) {
141    LOGV("Install DRM Engine");
142    Parcel data, reply;
143
144    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
145    data.writeInt32(uniqueId);
146    data.writeString8(drmEngineFile);
147
148    remote()->transact(INSTALL_DRM_ENGINE, data, &reply);
149    return reply.readInt32();
150}
151
152DrmConstraints* BpDrmManagerService::getConstraints(
153            int uniqueId, const String8* path, const int action) {
154    LOGV("Get Constraints");
155    Parcel data, reply;
156
157    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
158    data.writeInt32(uniqueId);
159    data.writeString8(*path);
160    data.writeInt32(action);
161
162    remote()->transact(GET_CONSTRAINTS_FROM_CONTENT, data, &reply);
163
164    DrmConstraints* drmConstraints = NULL;
165    if (0 != reply.dataAvail()) {
166        //Filling Drm Constraints
167        drmConstraints = new DrmConstraints();
168
169        const int size = reply.readInt32();
170        for (int index = 0; index < size; ++index) {
171            const String8 key(reply.readString8());
172            const int bufferSize = reply.readInt32();
173            char* data = NULL;
174            if (0 < bufferSize) {
175                data = new char[bufferSize];
176                reply.read(data, bufferSize);
177            }
178            drmConstraints->put(&key, data);
179        }
180    }
181    return drmConstraints;
182}
183
184DrmMetadata* BpDrmManagerService::getMetadata(int uniqueId, const String8* path) {
185    LOGV("Get Metadata");
186    Parcel data, reply;
187    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
188    data.writeInt32(uniqueId);
189
190    DrmMetadata* drmMetadata = NULL;
191    data.writeString8(*path);
192    remote()->transact(GET_METADATA_FROM_CONTENT, data, &reply);
193
194    if (0 != reply.dataAvail()) {
195        //Filling Drm Metadata
196        drmMetadata = new DrmMetadata();
197
198        const int size = reply.readInt32();
199        for (int index = 0; index < size; ++index) {
200            const String8 key(reply.readString8());
201            const int bufferSize = reply.readInt32();
202            char* data = NULL;
203            if (0 < bufferSize) {
204                data = new char[bufferSize];
205                reply.read(data, bufferSize);
206            }
207            drmMetadata->put(&key, data);
208        }
209    }
210    return drmMetadata;
211}
212
213bool BpDrmManagerService::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
214    LOGV("Can Handle");
215    Parcel data, reply;
216
217    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
218    data.writeInt32(uniqueId);
219
220    data.writeString8(path);
221    data.writeString8(mimeType);
222
223    remote()->transact(CAN_HANDLE, data, &reply);
224
225    return static_cast<bool>(reply.readInt32());
226}
227
228DrmInfoStatus* BpDrmManagerService::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
229    LOGV("Process DRM Info");
230    Parcel data, reply;
231
232    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
233    data.writeInt32(uniqueId);
234
235    //Filling DRM info
236    data.writeInt32(drmInfo->getInfoType());
237    const DrmBuffer dataBuffer = drmInfo->getData();
238    const int dataBufferSize = dataBuffer.length;
239    data.writeInt32(dataBufferSize);
240    if (0 < dataBufferSize) {
241        data.write(dataBuffer.data, dataBufferSize);
242    }
243    data.writeString8(drmInfo->getMimeType());
244
245    data.writeInt32(drmInfo->getCount());
246    DrmInfo::KeyIterator keyIt = drmInfo->keyIterator();
247
248    while (keyIt.hasNext()) {
249        const String8 key = keyIt.next();
250        data.writeString8(key);
251        const String8 value = drmInfo->get(key);
252        data.writeString8((value == String8("")) ? String8("NULL") : value);
253    }
254
255    remote()->transact(PROCESS_DRM_INFO, data, &reply);
256
257    DrmInfoStatus* drmInfoStatus = NULL;
258    if (0 != reply.dataAvail()) {
259        //Filling DRM Info Status
260        const int statusCode = reply.readInt32();
261        const int infoType = reply.readInt32();
262        const String8 mimeType = reply.readString8();
263
264        DrmBuffer* drmBuffer = NULL;
265        if (0 != reply.dataAvail()) {
266            const int bufferSize = reply.readInt32();
267            char* data = NULL;
268            if (0 < bufferSize) {
269                data = new char[bufferSize];
270                reply.read(data, bufferSize);
271            }
272            drmBuffer = new DrmBuffer(data, bufferSize);
273        }
274        drmInfoStatus = new DrmInfoStatus(statusCode, infoType, drmBuffer, mimeType);
275    }
276    return drmInfoStatus;
277}
278
279DrmInfo* BpDrmManagerService::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInforequest) {
280    LOGV("Acquire DRM Info");
281    Parcel data, reply;
282
283    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
284    data.writeInt32(uniqueId);
285
286    //Filling DRM Info Request
287    data.writeInt32(drmInforequest->getInfoType());
288    data.writeString8(drmInforequest->getMimeType());
289
290    data.writeInt32(drmInforequest->getCount());
291    DrmInfoRequest::KeyIterator keyIt = drmInforequest->keyIterator();
292
293    while (keyIt.hasNext()) {
294        const String8 key = keyIt.next();
295        data.writeString8(key);
296        const String8 value = drmInforequest->get(key);
297        data.writeString8((value == String8("")) ? String8("NULL") : value);
298    }
299
300    remote()->transact(ACQUIRE_DRM_INFO, data, &reply);
301
302    DrmInfo* drmInfo = NULL;
303    if (0 != reply.dataAvail()) {
304        //Filling DRM Info
305        const int infoType = reply.readInt32();
306        const int bufferSize = reply.readInt32();
307        char* data = NULL;
308
309        if (0 < bufferSize) {
310            data = new char[bufferSize];
311            reply.read(data, bufferSize);
312        }
313        drmInfo = new DrmInfo(infoType, DrmBuffer(data, bufferSize), reply.readString8());
314
315        const int size = reply.readInt32();
316        for (int index = 0; index < size; ++index) {
317            const String8 key(reply.readString8());
318            const String8 value(reply.readString8());
319            drmInfo->put(key, (value == String8("NULL")) ? String8("") : value);
320        }
321    }
322    return drmInfo;
323}
324
325status_t BpDrmManagerService::saveRights(
326            int uniqueId, const DrmRights& drmRights,
327            const String8& rightsPath, const String8& contentPath) {
328    LOGV("Save Rights");
329    Parcel data, reply;
330
331    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
332    data.writeInt32(uniqueId);
333
334    //Filling Drm Rights
335    const DrmBuffer dataBuffer = drmRights.getData();
336    data.writeInt32(dataBuffer.length);
337    data.write(dataBuffer.data, dataBuffer.length);
338
339    const String8 mimeType = drmRights.getMimeType();
340    data.writeString8((mimeType == String8("")) ? String8("NULL") : mimeType);
341
342    const String8 accountId = drmRights.getAccountId();
343    data.writeString8((accountId == String8("")) ? String8("NULL") : accountId);
344
345    const String8 subscriptionId = drmRights.getSubscriptionId();
346    data.writeString8((subscriptionId == String8("")) ? String8("NULL") : subscriptionId);
347
348    data.writeString8((rightsPath == String8("")) ? String8("NULL") : rightsPath);
349    data.writeString8((contentPath == String8("")) ? String8("NULL") : contentPath);
350
351    remote()->transact(SAVE_RIGHTS, data, &reply);
352    return reply.readInt32();
353}
354
355String8 BpDrmManagerService::getOriginalMimeType(int uniqueId, const String8& path) {
356    LOGV("Get Original MimeType");
357    Parcel data, reply;
358
359    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
360    data.writeInt32(uniqueId);
361    data.writeString8(path);
362
363    remote()->transact(GET_ORIGINAL_MIMETYPE, data, &reply);
364    return reply.readString8();
365}
366
367int BpDrmManagerService::getDrmObjectType(
368            int uniqueId, const String8& path, const String8& mimeType) {
369    LOGV("Get Drm object type");
370    Parcel data, reply;
371
372    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
373    data.writeInt32(uniqueId);
374    data.writeString8(path);
375    data.writeString8(mimeType);
376
377    remote()->transact(GET_DRM_OBJECT_TYPE, data, &reply);
378
379    return reply.readInt32();
380}
381
382int BpDrmManagerService::checkRightsStatus(int uniqueId, const String8& path, int action) {
383    LOGV("checkRightsStatus");
384    Parcel data, reply;
385
386    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
387    data.writeInt32(uniqueId);
388    data.writeString8(path);
389    data.writeInt32(action);
390
391    remote()->transact(CHECK_RIGHTS_STATUS, data, &reply);
392
393    return reply.readInt32();
394}
395
396status_t BpDrmManagerService::consumeRights(
397            int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
398    LOGV("consumeRights");
399    Parcel data, reply;
400
401    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
402    data.writeInt32(uniqueId);
403
404    writeDecrptHandleToParcelData(decryptHandle, &data);
405
406    data.writeInt32(action);
407    data.writeInt32(static_cast< int>(reserve));
408
409    remote()->transact(CONSUME_RIGHTS, data, &reply);
410    return reply.readInt32();
411}
412
413status_t BpDrmManagerService::setPlaybackStatus(
414            int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int64_t position) {
415    LOGV("setPlaybackStatus");
416    Parcel data, reply;
417
418    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
419    data.writeInt32(uniqueId);
420
421    writeDecrptHandleToParcelData(decryptHandle, &data);
422
423    data.writeInt32(playbackStatus);
424    data.writeInt64(position);
425
426    remote()->transact(SET_PLAYBACK_STATUS, data, &reply);
427    return reply.readInt32();
428}
429
430bool BpDrmManagerService::validateAction(
431            int uniqueId, const String8& path,
432            int action, const ActionDescription& description) {
433    LOGV("validateAction");
434    Parcel data, reply;
435
436    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
437    data.writeInt32(uniqueId);
438    data.writeString8(path);
439    data.writeInt32(action);
440    data.writeInt32(description.outputType);
441    data.writeInt32(description.configuration);
442
443    remote()->transact(VALIDATE_ACTION, data, &reply);
444
445    return static_cast<bool>(reply.readInt32());
446}
447
448status_t BpDrmManagerService::removeRights(int uniqueId, const String8& path) {
449    LOGV("removeRights");
450    Parcel data, reply;
451
452    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
453    data.writeInt32(uniqueId);
454    data.writeString8(path);
455
456    remote()->transact(REMOVE_RIGHTS, data, &reply);
457    return reply.readInt32();
458}
459
460status_t BpDrmManagerService::removeAllRights(int uniqueId) {
461    LOGV("removeAllRights");
462    Parcel data, reply;
463
464    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
465    data.writeInt32(uniqueId);
466
467    remote()->transact(REMOVE_ALL_RIGHTS, data, &reply);
468    return reply.readInt32();
469}
470
471int BpDrmManagerService::openConvertSession(int uniqueId, const String8& mimeType) {
472    LOGV("openConvertSession");
473    Parcel data, reply;
474
475    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
476    data.writeInt32(uniqueId);
477    data.writeString8(mimeType);
478
479    remote()->transact(OPEN_CONVERT_SESSION, data, &reply);
480    return reply.readInt32();
481}
482
483DrmConvertedStatus* BpDrmManagerService::convertData(
484            int uniqueId, int convertId, const DrmBuffer* inputData) {
485    LOGV("convertData");
486    Parcel data, reply;
487
488    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
489    data.writeInt32(uniqueId);
490    data.writeInt32(convertId);
491    data.writeInt32(inputData->length);
492    data.write(inputData->data, inputData->length);
493
494    remote()->transact(CONVERT_DATA, data, &reply);
495
496    DrmConvertedStatus* drmConvertedStatus = NULL;
497
498    if (0 != reply.dataAvail()) {
499        //Filling DRM Converted Status
500        const int statusCode = reply.readInt32();
501        const off64_t offset = reply.readInt64();
502
503        DrmBuffer* convertedData = NULL;
504        if (0 != reply.dataAvail()) {
505            const int bufferSize = reply.readInt32();
506            char* data = NULL;
507            if (0 < bufferSize) {
508                data = new char[bufferSize];
509                reply.read(data, bufferSize);
510            }
511            convertedData = new DrmBuffer(data, bufferSize);
512        }
513        drmConvertedStatus = new DrmConvertedStatus(statusCode, convertedData, offset);
514    }
515    return drmConvertedStatus;
516}
517
518DrmConvertedStatus* BpDrmManagerService::closeConvertSession(int uniqueId, int convertId) {
519    LOGV("closeConvertSession");
520    Parcel data, reply;
521
522    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
523    data.writeInt32(uniqueId);
524    data.writeInt32(convertId);
525
526    remote()->transact(CLOSE_CONVERT_SESSION, data, &reply);
527
528    DrmConvertedStatus* drmConvertedStatus = NULL;
529
530    if (0 != reply.dataAvail()) {
531        //Filling DRM Converted Status
532        const int statusCode = reply.readInt32();
533        const off64_t offset = reply.readInt64();
534
535        DrmBuffer* convertedData = NULL;
536        if (0 != reply.dataAvail()) {
537            const int bufferSize = reply.readInt32();
538            char* data = NULL;
539            if (0 < bufferSize) {
540                data = new char[bufferSize];
541                reply.read(data, bufferSize);
542            }
543            convertedData = new DrmBuffer(data, bufferSize);
544        }
545        drmConvertedStatus = new DrmConvertedStatus(statusCode, convertedData, offset);
546    }
547    return drmConvertedStatus;
548}
549
550status_t BpDrmManagerService::getAllSupportInfo(
551            int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
552    LOGV("Get All Support Info");
553    Parcel data, reply;
554
555    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
556    data.writeInt32(uniqueId);
557
558    remote()->transact(GET_ALL_SUPPORT_INFO, data, &reply);
559
560    //Filling DRM Support Info
561    const int arraySize = reply.readInt32();
562    if (0 < arraySize) {
563        *drmSupportInfoArray = new DrmSupportInfo[arraySize];
564
565        for (int index = 0; index < arraySize; ++index) {
566            DrmSupportInfo drmSupportInfo;
567
568            const int fileSuffixVectorSize = reply.readInt32();
569            for (int i = 0; i < fileSuffixVectorSize; ++i) {
570                drmSupportInfo.addFileSuffix(reply.readString8());
571            }
572
573            const int mimeTypeVectorSize = reply.readInt32();
574            for (int i = 0; i < mimeTypeVectorSize; ++i) {
575                drmSupportInfo.addMimeType(reply.readString8());
576            }
577
578            drmSupportInfo.setDescription(reply.readString8());
579            (*drmSupportInfoArray)[index] = drmSupportInfo;
580        }
581    }
582    *length = arraySize;
583    return reply.readInt32();
584}
585
586DecryptHandle* BpDrmManagerService::openDecryptSession(
587            int uniqueId, int fd, off64_t offset, off64_t length) {
588    LOGV("Entering BpDrmManagerService::openDecryptSession");
589    Parcel data, reply;
590
591    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
592    data.writeInt32(uniqueId);
593    data.writeFileDescriptor(fd);
594    data.writeInt64(offset);
595    data.writeInt64(length);
596
597    remote()->transact(OPEN_DECRYPT_SESSION, data, &reply);
598
599    DecryptHandle* handle = NULL;
600    if (0 != reply.dataAvail()) {
601        handle = new DecryptHandle();
602        readDecryptHandleFromParcelData(handle, reply);
603    }
604    return handle;
605}
606
607DecryptHandle* BpDrmManagerService::openDecryptSession(int uniqueId, const char* uri) {
608    LOGV("Entering BpDrmManagerService::openDecryptSession");
609    Parcel data, reply;
610
611    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
612    data.writeInt32(uniqueId);
613    data.writeString8(String8(uri));
614
615    remote()->transact(OPEN_DECRYPT_SESSION_FROM_URI, data, &reply);
616
617    DecryptHandle* handle = NULL;
618    if (0 != reply.dataAvail()) {
619        handle = new DecryptHandle();
620        readDecryptHandleFromParcelData(handle, reply);
621    } else {
622        LOGV("no decryptHandle is generated in service side");
623    }
624    return handle;
625}
626
627status_t BpDrmManagerService::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
628    LOGV("closeDecryptSession");
629    Parcel data, reply;
630
631    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
632    data.writeInt32(uniqueId);
633
634    writeDecrptHandleToParcelData(decryptHandle, &data);
635
636    remote()->transact(CLOSE_DECRYPT_SESSION, data, &reply);
637
638    return reply.readInt32();
639}
640
641status_t BpDrmManagerService::initializeDecryptUnit(
642            int uniqueId, DecryptHandle* decryptHandle,
643            int decryptUnitId, const DrmBuffer* headerInfo) {
644    LOGV("initializeDecryptUnit");
645    Parcel data, reply;
646
647    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
648    data.writeInt32(uniqueId);
649
650    writeDecrptHandleToParcelData(decryptHandle, &data);
651
652    data.writeInt32(decryptUnitId);
653
654    data.writeInt32(headerInfo->length);
655    data.write(headerInfo->data, headerInfo->length);
656
657    remote()->transact(INITIALIZE_DECRYPT_UNIT, data, &reply);
658    return reply.readInt32();
659}
660
661status_t BpDrmManagerService::decrypt(
662            int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId,
663            const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
664    LOGV("decrypt");
665    Parcel data, reply;
666
667    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
668    data.writeInt32(uniqueId);
669
670    writeDecrptHandleToParcelData(decryptHandle, &data);
671
672    data.writeInt32(decryptUnitId);
673    data.writeInt32((*decBuffer)->length);
674
675    data.writeInt32(encBuffer->length);
676    data.write(encBuffer->data, encBuffer->length);
677
678    if (NULL != IV) {
679        data.writeInt32(IV->length);
680        data.write(IV->data, IV->length);
681    }
682
683    remote()->transact(DECRYPT, data, &reply);
684
685    const status_t status = reply.readInt32();
686    LOGV("Return value of decrypt() is %d", status);
687
688    const int size = reply.readInt32();
689    (*decBuffer)->length = size;
690    reply.read((void *)(*decBuffer)->data, size);
691
692    return status;
693}
694
695status_t BpDrmManagerService::finalizeDecryptUnit(
696            int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) {
697    LOGV("finalizeDecryptUnit");
698    Parcel data, reply;
699
700    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
701    data.writeInt32(uniqueId);
702
703    writeDecrptHandleToParcelData(decryptHandle, &data);
704
705    data.writeInt32(decryptUnitId);
706
707    remote()->transact(FINALIZE_DECRYPT_UNIT, data, &reply);
708    return reply.readInt32();
709}
710
711ssize_t BpDrmManagerService::pread(
712            int uniqueId, DecryptHandle* decryptHandle, void* buffer,
713            ssize_t numBytes, off64_t offset) {
714    LOGV("read");
715    Parcel data, reply;
716    int result;
717
718    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
719    data.writeInt32(uniqueId);
720
721    writeDecrptHandleToParcelData(decryptHandle, &data);
722
723    data.writeInt32(numBytes);
724    data.writeInt64(offset);
725
726    remote()->transact(PREAD, data, &reply);
727    result = reply.readInt32();
728    if (0 < result) {
729        reply.read(buffer, result);
730    }
731    return result;
732}
733
734IMPLEMENT_META_INTERFACE(DrmManagerService, "drm.IDrmManagerService");
735
736status_t BnDrmManagerService::onTransact(
737            uint32_t code, const Parcel& data,
738            Parcel* reply, uint32_t flags) {
739    LOGV("Entering BnDrmManagerService::onTransact with code %d", code);
740
741    switch (code) {
742    case ADD_UNIQUEID:
743    {
744        LOGV("BnDrmManagerService::onTransact :ADD_UNIQUEID");
745        CHECK_INTERFACE(IDrmManagerService, data, reply);
746        int uniqueId = addUniqueId(data.readInt32());
747        reply->writeInt32(uniqueId);
748        return DRM_NO_ERROR;
749    }
750
751    case REMOVE_UNIQUEID:
752    {
753        LOGV("BnDrmManagerService::onTransact :REMOVE_UNIQUEID");
754        CHECK_INTERFACE(IDrmManagerService, data, reply);
755        removeUniqueId(data.readInt32());
756        return DRM_NO_ERROR;
757    }
758
759    case ADD_CLIENT:
760    {
761        LOGV("BnDrmManagerService::onTransact :ADD_CLIENT");
762        CHECK_INTERFACE(IDrmManagerService, data, reply);
763        addClient(data.readInt32());
764        return DRM_NO_ERROR;
765    }
766
767    case REMOVE_CLIENT:
768    {
769        LOGV("BnDrmManagerService::onTransact :REMOVE_CLIENT");
770        CHECK_INTERFACE(IDrmManagerService, data, reply);
771        removeClient(data.readInt32());
772        return DRM_NO_ERROR;
773    }
774
775    case SET_DRM_SERVICE_LISTENER:
776    {
777        LOGV("BnDrmManagerService::onTransact :SET_DRM_SERVICE_LISTENER");
778        CHECK_INTERFACE(IDrmManagerService, data, reply);
779
780        const int uniqueId = data.readInt32();
781        const sp<IDrmServiceListener> drmServiceListener
782            = interface_cast<IDrmServiceListener> (data.readStrongBinder());
783
784        status_t status = setDrmServiceListener(uniqueId, drmServiceListener);
785
786        reply->writeInt32(status);
787        return DRM_NO_ERROR;
788    }
789
790    case INSTALL_DRM_ENGINE:
791    {
792        LOGV("BnDrmManagerService::onTransact :INSTALL_DRM_ENGINE");
793        CHECK_INTERFACE(IDrmManagerService, data, reply);
794
795        status_t status = installDrmEngine(data.readInt32(), data.readString8());
796
797        reply->writeInt32(status);
798        return DRM_NO_ERROR;
799    }
800
801    case GET_CONSTRAINTS_FROM_CONTENT:
802    {
803        LOGV("BnDrmManagerService::onTransact :GET_CONSTRAINTS_FROM_CONTENT");
804        CHECK_INTERFACE(IDrmManagerService, data, reply);
805
806        const int uniqueId = data.readInt32();
807        const String8 path = data.readString8();
808
809        DrmConstraints* drmConstraints = getConstraints(uniqueId, &path, data.readInt32());
810
811        if (NULL != drmConstraints) {
812            //Filling DRM Constraints contents
813            reply->writeInt32(drmConstraints->getCount());
814
815            DrmConstraints::KeyIterator keyIt = drmConstraints->keyIterator();
816            while (keyIt.hasNext()) {
817                const String8 key = keyIt.next();
818                reply->writeString8(key);
819                const char* value = drmConstraints->getAsByteArray(&key);
820                int bufferSize = 0;
821                if (NULL != value) {
822                    bufferSize = strlen(value);
823                }
824                reply->writeInt32(bufferSize + 1);
825                reply->write(value, bufferSize + 1);
826            }
827        }
828        delete drmConstraints; drmConstraints = NULL;
829        return DRM_NO_ERROR;
830    }
831
832    case GET_METADATA_FROM_CONTENT:
833    {
834        LOGV("BnDrmManagerService::onTransact :GET_METADATA_FROM_CONTENT");
835        CHECK_INTERFACE(IDrmManagerService, data, reply);
836
837        const int uniqueId = data.readInt32();
838        const String8 path = data.readString8();
839
840        DrmMetadata* drmMetadata = getMetadata(uniqueId, &path);
841        if (NULL != drmMetadata) {
842            //Filling DRM Metadata contents
843            reply->writeInt32(drmMetadata->getCount());
844
845            DrmMetadata::KeyIterator keyIt = drmMetadata->keyIterator();
846            while (keyIt.hasNext()) {
847                const String8 key = keyIt.next();
848                reply->writeString8(key);
849                const char* value = drmMetadata->getAsByteArray(&key);
850                int bufferSize = 0;
851                if (NULL != value) {
852                    bufferSize = strlen(value);
853                    reply->writeInt32(bufferSize + 1);
854                    reply->write(value, bufferSize + 1);
855                } else {
856                    reply->writeInt32(0);
857                }
858            }
859        }
860        delete drmMetadata; drmMetadata = NULL;
861        return NO_ERROR;
862    }
863
864    case CAN_HANDLE:
865    {
866        LOGV("BnDrmManagerService::onTransact :CAN_HANDLE");
867        CHECK_INTERFACE(IDrmManagerService, data, reply);
868
869        const int uniqueId = data.readInt32();
870        const String8 path = data.readString8();
871        const String8 mimeType = data.readString8();
872
873        bool result = canHandle(uniqueId, path, mimeType);
874
875        reply->writeInt32(result);
876        return DRM_NO_ERROR;
877    }
878
879    case PROCESS_DRM_INFO:
880    {
881        LOGV("BnDrmManagerService::onTransact :PROCESS_DRM_INFO");
882        CHECK_INTERFACE(IDrmManagerService, data, reply);
883
884        const int uniqueId = data.readInt32();
885
886        //Filling DRM info
887        const int infoType = data.readInt32();
888        const int bufferSize = data.readInt32();
889        char* buffer = NULL;
890        if (0 < bufferSize) {
891            buffer = (char *)data.readInplace(bufferSize);
892        }
893        const DrmBuffer drmBuffer(buffer, bufferSize);
894        DrmInfo* drmInfo = new DrmInfo(infoType, drmBuffer, data.readString8());
895
896        const int size = data.readInt32();
897        for (int index = 0; index < size; ++index) {
898            const String8 key(data.readString8());
899            const String8 value(data.readString8());
900            drmInfo->put(key, (value == String8("NULL")) ? String8("") : value);
901        }
902
903        DrmInfoStatus* drmInfoStatus = processDrmInfo(uniqueId, drmInfo);
904
905        if (NULL != drmInfoStatus) {
906            //Filling DRM Info Status contents
907            reply->writeInt32(drmInfoStatus->statusCode);
908            reply->writeInt32(drmInfoStatus->infoType);
909            reply->writeString8(drmInfoStatus->mimeType);
910
911            if (NULL != drmInfoStatus->drmBuffer) {
912                const DrmBuffer* drmBuffer = drmInfoStatus->drmBuffer;
913                const int bufferSize = drmBuffer->length;
914                reply->writeInt32(bufferSize);
915                if (0 < bufferSize) {
916                    reply->write(drmBuffer->data, bufferSize);
917                }
918                delete [] drmBuffer->data;
919                delete drmBuffer; drmBuffer = NULL;
920            }
921        }
922        delete drmInfo; drmInfo = NULL;
923        delete drmInfoStatus; drmInfoStatus = NULL;
924        return DRM_NO_ERROR;
925    }
926
927    case ACQUIRE_DRM_INFO:
928    {
929        LOGV("BnDrmManagerService::onTransact :ACQUIRE_DRM_INFO");
930        CHECK_INTERFACE(IDrmManagerService, data, reply);
931
932        const int uniqueId = data.readInt32();
933
934        //Filling DRM info Request
935        DrmInfoRequest* drmInfoRequest = new DrmInfoRequest(data.readInt32(), data.readString8());
936
937        const int size = data.readInt32();
938        for (int index = 0; index < size; ++index) {
939            const String8 key(data.readString8());
940            const String8 value(data.readString8());
941            drmInfoRequest->put(key, (value == String8("NULL")) ? String8("") : value);
942        }
943
944        DrmInfo* drmInfo = acquireDrmInfo(uniqueId, drmInfoRequest);
945
946        if (NULL != drmInfo) {
947            //Filling DRM Info
948            const DrmBuffer drmBuffer = drmInfo->getData();
949            reply->writeInt32(drmInfo->getInfoType());
950
951            const int bufferSize = drmBuffer.length;
952            reply->writeInt32(bufferSize);
953            if (0 < bufferSize) {
954                reply->write(drmBuffer.data, bufferSize);
955            }
956            reply->writeString8(drmInfo->getMimeType());
957            reply->writeInt32(drmInfo->getCount());
958
959            DrmInfo::KeyIterator keyIt = drmInfo->keyIterator();
960            while (keyIt.hasNext()) {
961                const String8 key = keyIt.next();
962                reply->writeString8(key);
963                const String8 value = drmInfo->get(key);
964                reply->writeString8((value == String8("")) ? String8("NULL") : value);
965            }
966            delete [] drmBuffer.data;
967        }
968        delete drmInfoRequest; drmInfoRequest = NULL;
969        delete drmInfo; drmInfo = NULL;
970        return DRM_NO_ERROR;
971    }
972
973    case SAVE_RIGHTS:
974    {
975        LOGV("BnDrmManagerService::onTransact :SAVE_RIGHTS");
976        CHECK_INTERFACE(IDrmManagerService, data, reply);
977
978        const int uniqueId = data.readInt32();
979
980        //Filling DRM Rights
981        const int bufferSize = data.readInt32();
982        const DrmBuffer drmBuffer((char *)data.readInplace(bufferSize), bufferSize);
983
984        const String8 mimeType(data.readString8());
985        const String8 accountId(data.readString8());
986        const String8 subscriptionId(data.readString8());
987        const String8 rightsPath(data.readString8());
988        const String8 contentPath(data.readString8());
989
990        DrmRights drmRights(drmBuffer,
991                            ((mimeType == String8("NULL")) ? String8("") : mimeType),
992                            ((accountId == String8("NULL")) ? String8("") : accountId),
993                            ((subscriptionId == String8("NULL")) ? String8("") : subscriptionId));
994
995        const status_t status = saveRights(uniqueId, drmRights,
996                            ((rightsPath == String8("NULL")) ? String8("") : rightsPath),
997                            ((contentPath == String8("NULL")) ? String8("") : contentPath));
998
999        reply->writeInt32(status);
1000        return DRM_NO_ERROR;
1001    }
1002
1003    case GET_ORIGINAL_MIMETYPE:
1004    {
1005        LOGV("BnDrmManagerService::onTransact :GET_ORIGINAL_MIMETYPE");
1006        CHECK_INTERFACE(IDrmManagerService, data, reply);
1007
1008        const String8 originalMimeType = getOriginalMimeType(data.readInt32(), data.readString8());
1009
1010        reply->writeString8(originalMimeType);
1011        return DRM_NO_ERROR;
1012    }
1013
1014    case GET_DRM_OBJECT_TYPE:
1015    {
1016        LOGV("BnDrmManagerService::onTransact :GET_DRM_OBJECT_TYPE");
1017        CHECK_INTERFACE(IDrmManagerService, data, reply);
1018
1019        const int drmObjectType
1020            = getDrmObjectType(data.readInt32(), data.readString8(), data.readString8());
1021
1022        reply->writeInt32(drmObjectType);
1023        return DRM_NO_ERROR;
1024    }
1025
1026    case CHECK_RIGHTS_STATUS:
1027    {
1028        LOGV("BnDrmManagerService::onTransact :CHECK_RIGHTS_STATUS");
1029        CHECK_INTERFACE(IDrmManagerService, data, reply);
1030
1031        const int result
1032            = checkRightsStatus(data.readInt32(), data.readString8(), data.readInt32());
1033
1034        reply->writeInt32(result);
1035        return DRM_NO_ERROR;
1036    }
1037
1038    case CONSUME_RIGHTS:
1039    {
1040        LOGV("BnDrmManagerService::onTransact :CONSUME_RIGHTS");
1041        CHECK_INTERFACE(IDrmManagerService, data, reply);
1042
1043        const int uniqueId = data.readInt32();
1044
1045        DecryptHandle handle;
1046        readDecryptHandleFromParcelData(&handle, data);
1047
1048        const status_t status
1049            = consumeRights(uniqueId, &handle, data.readInt32(),
1050                static_cast<bool>(data.readInt32()));
1051        reply->writeInt32(status);
1052
1053        clearDecryptHandle(&handle);
1054        return DRM_NO_ERROR;
1055    }
1056
1057    case SET_PLAYBACK_STATUS:
1058    {
1059        LOGV("BnDrmManagerService::onTransact :SET_PLAYBACK_STATUS");
1060        CHECK_INTERFACE(IDrmManagerService, data, reply);
1061
1062        const int uniqueId = data.readInt32();
1063
1064        DecryptHandle handle;
1065        readDecryptHandleFromParcelData(&handle, data);
1066
1067        const status_t status
1068            = setPlaybackStatus(uniqueId, &handle, data.readInt32(), data.readInt64());
1069        reply->writeInt32(status);
1070
1071        clearDecryptHandle(&handle);
1072        return DRM_NO_ERROR;
1073    }
1074
1075    case VALIDATE_ACTION:
1076    {
1077        LOGV("BnDrmManagerService::onTransact :VALIDATE_ACTION");
1078        CHECK_INTERFACE(IDrmManagerService, data, reply);
1079
1080        bool result = validateAction(
1081                                data.readInt32(),
1082                                data.readString8(),
1083                                data.readInt32(),
1084                                ActionDescription(data.readInt32(), data.readInt32()));
1085
1086        reply->writeInt32(result);
1087        return DRM_NO_ERROR;
1088    }
1089
1090    case REMOVE_RIGHTS:
1091    {
1092        LOGV("BnDrmManagerService::onTransact :REMOVE_RIGHTS");
1093        CHECK_INTERFACE(IDrmManagerService, data, reply);
1094
1095        const status_t status = removeRights(data.readInt32(), data.readString8());
1096        reply->writeInt32(status);
1097
1098        return DRM_NO_ERROR;
1099    }
1100
1101    case REMOVE_ALL_RIGHTS:
1102    {
1103        LOGV("BnDrmManagerService::onTransact :REMOVE_ALL_RIGHTS");
1104        CHECK_INTERFACE(IDrmManagerService, data, reply);
1105
1106        const status_t status = removeAllRights(data.readInt32());
1107        reply->writeInt32(status);
1108
1109        return DRM_NO_ERROR;
1110    }
1111
1112    case OPEN_CONVERT_SESSION:
1113    {
1114        LOGV("BnDrmManagerService::onTransact :OPEN_CONVERT_SESSION");
1115        CHECK_INTERFACE(IDrmManagerService, data, reply);
1116
1117        const int convertId = openConvertSession(data.readInt32(), data.readString8());
1118
1119        reply->writeInt32(convertId);
1120        return DRM_NO_ERROR;
1121    }
1122
1123    case CONVERT_DATA:
1124    {
1125        LOGV("BnDrmManagerService::onTransact :CONVERT_DATA");
1126        CHECK_INTERFACE(IDrmManagerService, data, reply);
1127
1128        const int uniqueId = data.readInt32();
1129        const int convertId = data.readInt32();
1130
1131        //Filling input data
1132        const int bufferSize = data.readInt32();
1133        DrmBuffer* inputData = new DrmBuffer((char *)data.readInplace(bufferSize), bufferSize);
1134
1135        DrmConvertedStatus*    drmConvertedStatus = convertData(uniqueId, convertId, inputData);
1136
1137        if (NULL != drmConvertedStatus) {
1138            //Filling Drm Converted Ststus
1139            reply->writeInt32(drmConvertedStatus->statusCode);
1140            reply->writeInt64(drmConvertedStatus->offset);
1141
1142            if (NULL != drmConvertedStatus->convertedData) {
1143                const DrmBuffer* convertedData = drmConvertedStatus->convertedData;
1144                const int bufferSize = convertedData->length;
1145                reply->writeInt32(bufferSize);
1146                if (0 < bufferSize) {
1147                    reply->write(convertedData->data, bufferSize);
1148                }
1149                delete [] convertedData->data;
1150                delete convertedData; convertedData = NULL;
1151            }
1152        }
1153        delete inputData; inputData = NULL;
1154        delete drmConvertedStatus; drmConvertedStatus = NULL;
1155        return DRM_NO_ERROR;
1156    }
1157
1158    case CLOSE_CONVERT_SESSION:
1159    {
1160        LOGV("BnDrmManagerService::onTransact :CLOSE_CONVERT_SESSION");
1161        CHECK_INTERFACE(IDrmManagerService, data, reply);
1162
1163        DrmConvertedStatus*    drmConvertedStatus
1164            = closeConvertSession(data.readInt32(), data.readInt32());
1165
1166        if (NULL != drmConvertedStatus) {
1167            //Filling Drm Converted Ststus
1168            reply->writeInt32(drmConvertedStatus->statusCode);
1169            reply->writeInt64(drmConvertedStatus->offset);
1170
1171            if (NULL != drmConvertedStatus->convertedData) {
1172                const DrmBuffer* convertedData = drmConvertedStatus->convertedData;
1173                const int bufferSize = convertedData->length;
1174                reply->writeInt32(bufferSize);
1175                if (0 < bufferSize) {
1176                    reply->write(convertedData->data, bufferSize);
1177                }
1178                delete [] convertedData->data;
1179                delete convertedData; convertedData = NULL;
1180            }
1181        }
1182        delete drmConvertedStatus; drmConvertedStatus = NULL;
1183        return DRM_NO_ERROR;
1184    }
1185
1186    case GET_ALL_SUPPORT_INFO:
1187    {
1188        LOGV("BnDrmManagerService::onTransact :GET_ALL_SUPPORT_INFO");
1189        CHECK_INTERFACE(IDrmManagerService, data, reply);
1190
1191        const int uniqueId = data.readInt32();
1192        int length = 0;
1193        DrmSupportInfo* drmSupportInfoArray = NULL;
1194
1195        status_t status = getAllSupportInfo(uniqueId, &length, &drmSupportInfoArray);
1196
1197        reply->writeInt32(length);
1198        for (int i = 0; i < length; ++i) {
1199            DrmSupportInfo drmSupportInfo = drmSupportInfoArray[i];
1200
1201            reply->writeInt32(drmSupportInfo.getFileSuffixCount());
1202            DrmSupportInfo::FileSuffixIterator fileSuffixIt
1203                = drmSupportInfo.getFileSuffixIterator();
1204            while (fileSuffixIt.hasNext()) {
1205                reply->writeString8(fileSuffixIt.next());
1206            }
1207
1208            reply->writeInt32(drmSupportInfo.getMimeTypeCount());
1209            DrmSupportInfo::MimeTypeIterator mimeTypeIt = drmSupportInfo.getMimeTypeIterator();
1210            while (mimeTypeIt.hasNext()) {
1211                reply->writeString8(mimeTypeIt.next());
1212            }
1213            reply->writeString8(drmSupportInfo.getDescription());
1214        }
1215        delete [] drmSupportInfoArray; drmSupportInfoArray = NULL;
1216        reply->writeInt32(status);
1217        return DRM_NO_ERROR;
1218    }
1219
1220    case OPEN_DECRYPT_SESSION:
1221    {
1222        LOGV("BnDrmManagerService::onTransact :OPEN_DECRYPT_SESSION");
1223        CHECK_INTERFACE(IDrmManagerService, data, reply);
1224
1225        const int uniqueId = data.readInt32();
1226        const int fd = data.readFileDescriptor();
1227
1228        DecryptHandle* handle
1229            = openDecryptSession(uniqueId, fd, data.readInt64(), data.readInt64());
1230
1231        if (NULL != handle) {
1232            writeDecrptHandleToParcelData(handle, reply);
1233            clearDecryptHandle(handle);
1234            delete handle; handle = NULL;
1235        }
1236        return DRM_NO_ERROR;
1237    }
1238
1239    case OPEN_DECRYPT_SESSION_FROM_URI:
1240    {
1241        LOGV("BnDrmManagerService::onTransact :OPEN_DECRYPT_SESSION_FROM_URI");
1242        CHECK_INTERFACE(IDrmManagerService, data, reply);
1243
1244        const int uniqueId = data.readInt32();
1245        const String8 uri = data.readString8();
1246
1247        DecryptHandle* handle = openDecryptSession(uniqueId, uri.string());
1248
1249        if (NULL != handle) {
1250            writeDecrptHandleToParcelData(handle, reply);
1251
1252            clearDecryptHandle(handle);
1253            delete handle; handle = NULL;
1254        } else {
1255            LOGV("NULL decryptHandle is returned");
1256        }
1257        return DRM_NO_ERROR;
1258    }
1259
1260    case CLOSE_DECRYPT_SESSION:
1261    {
1262        LOGV("BnDrmManagerService::onTransact :CLOSE_DECRYPT_SESSION");
1263        CHECK_INTERFACE(IDrmManagerService, data, reply);
1264
1265        const int uniqueId = data.readInt32();
1266
1267        DecryptHandle* handle = new DecryptHandle();
1268        readDecryptHandleFromParcelData(handle, data);
1269
1270        const status_t status = closeDecryptSession(uniqueId, handle);
1271        reply->writeInt32(status);
1272        return DRM_NO_ERROR;
1273    }
1274
1275    case INITIALIZE_DECRYPT_UNIT:
1276    {
1277        LOGV("BnDrmManagerService::onTransact :INITIALIZE_DECRYPT_UNIT");
1278        CHECK_INTERFACE(IDrmManagerService, data, reply);
1279
1280        const int uniqueId = data.readInt32();
1281
1282        DecryptHandle handle;
1283        readDecryptHandleFromParcelData(&handle, data);
1284
1285        const int decryptUnitId = data.readInt32();
1286
1287        //Filling Header info
1288        const int bufferSize = data.readInt32();
1289        DrmBuffer* headerInfo = NULL;
1290        headerInfo = new DrmBuffer((char *)data.readInplace(bufferSize), bufferSize);
1291
1292        const status_t status
1293            = initializeDecryptUnit(uniqueId, &handle, decryptUnitId, headerInfo);
1294        reply->writeInt32(status);
1295
1296        clearDecryptHandle(&handle);
1297        delete headerInfo; headerInfo = NULL;
1298        return DRM_NO_ERROR;
1299    }
1300
1301    case DECRYPT:
1302    {
1303        LOGV("BnDrmManagerService::onTransact :DECRYPT");
1304        CHECK_INTERFACE(IDrmManagerService, data, reply);
1305
1306        const int uniqueId = data.readInt32();
1307
1308        DecryptHandle handle;
1309        readDecryptHandleFromParcelData(&handle, data);
1310
1311        const int decryptUnitId = data.readInt32();
1312        const int decBufferSize = data.readInt32();
1313
1314        const int encBufferSize = data.readInt32();
1315        DrmBuffer* encBuffer
1316            = new DrmBuffer((char *)data.readInplace(encBufferSize), encBufferSize);
1317
1318        char* buffer = NULL;
1319        buffer = new char[decBufferSize];
1320        DrmBuffer* decBuffer = new DrmBuffer(buffer, decBufferSize);
1321
1322        DrmBuffer* IV = NULL;
1323        if (0 != data.dataAvail()) {
1324            const int ivBufferlength = data.readInt32();
1325            IV = new DrmBuffer((char *)data.readInplace(ivBufferlength), ivBufferlength);
1326        }
1327
1328        const status_t status
1329            = decrypt(uniqueId, &handle, decryptUnitId, encBuffer, &decBuffer, IV);
1330
1331        reply->writeInt32(status);
1332
1333        const int size = decBuffer->length;
1334        reply->writeInt32(size);
1335        reply->write(decBuffer->data, size);
1336
1337        clearDecryptHandle(&handle);
1338        delete encBuffer; encBuffer = NULL;
1339        delete decBuffer; decBuffer = NULL;
1340        delete [] buffer; buffer = NULL;
1341        delete IV; IV = NULL;
1342        return DRM_NO_ERROR;
1343    }
1344
1345    case FINALIZE_DECRYPT_UNIT:
1346    {
1347        LOGV("BnDrmManagerService::onTransact :FINALIZE_DECRYPT_UNIT");
1348        CHECK_INTERFACE(IDrmManagerService, data, reply);
1349
1350        const int uniqueId = data.readInt32();
1351
1352        DecryptHandle handle;
1353        readDecryptHandleFromParcelData(&handle, data);
1354
1355        const status_t status = finalizeDecryptUnit(uniqueId, &handle, data.readInt32());
1356        reply->writeInt32(status);
1357
1358        clearDecryptHandle(&handle);
1359        return DRM_NO_ERROR;
1360    }
1361
1362    case PREAD:
1363    {
1364        LOGV("BnDrmManagerService::onTransact :READ");
1365        CHECK_INTERFACE(IDrmManagerService, data, reply);
1366
1367        const int uniqueId = data.readInt32();
1368
1369        DecryptHandle handle;
1370        readDecryptHandleFromParcelData(&handle, data);
1371
1372        const int numBytes = data.readInt32();
1373        char* buffer = new char[numBytes];
1374
1375        const off64_t offset = data.readInt64();
1376
1377        ssize_t result = pread(uniqueId, &handle, buffer, numBytes, offset);
1378        reply->writeInt32(result);
1379        if (0 < result) {
1380            reply->write(buffer, result);
1381        }
1382
1383        clearDecryptHandle(&handle);
1384        delete [] buffer, buffer = NULL;
1385        return DRM_NO_ERROR;
1386    }
1387
1388    default:
1389        return BBinder::onTransact(code, data, reply, flags);
1390    }
1391}
1392
1393