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