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