DrmManagerService.cpp revision 8635b7b095fbf7ffc63d3ce791891a9116ace1f6
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 "DrmManagerService(Native)"
19#include <utils/Log.h>
20
21#include <private/android_filesystem_config.h>
22#include <media/MemoryLeakTrackUtil.h>
23
24#include <errno.h>
25#include <utils/threads.h>
26#include <binder/IServiceManager.h>
27#include <binder/IPCThreadState.h>
28#include <sys/stat.h>
29#include "DrmManagerService.h"
30#include "DrmManager.h"
31
32using namespace android;
33
34static Vector<uid_t> trustedUids;
35
36static bool isProtectedCallAllowed() {
37    // TODO
38    // Following implementation is just for reference.
39    // Each OEM manufacturer should implement/replace with their own solutions.
40    bool result = false;
41
42    IPCThreadState* ipcState = IPCThreadState::self();
43    uid_t uid = ipcState->getCallingUid();
44
45    for (unsigned int i = 0; i < trustedUids.size(); ++i) {
46        if (trustedUids[i] == uid) {
47            result = true;
48            break;
49        }
50    }
51    return result;
52}
53
54void DrmManagerService::instantiate() {
55    LOGV("instantiate");
56    defaultServiceManager()->addService(String16("drm.drmManager"), new DrmManagerService());
57
58    if (0 >= trustedUids.size()) {
59        // TODO
60        // Following implementation is just for reference.
61        // Each OEM manufacturer should implement/replace with their own solutions.
62
63        // Add trusted uids here
64        trustedUids.push(AID_MEDIA);
65    }
66}
67
68DrmManagerService::DrmManagerService() :
69        mDrmManager(NULL) {
70    LOGV("created");
71    mDrmManager = new DrmManager();
72    mDrmManager->loadPlugIns();
73}
74
75DrmManagerService::~DrmManagerService() {
76    LOGV("Destroyed");
77    mDrmManager->unloadPlugIns();
78    delete mDrmManager; mDrmManager = NULL;
79}
80
81int DrmManagerService::addUniqueId(int uniqueId) {
82    return mDrmManager->addUniqueId(uniqueId);
83}
84
85void DrmManagerService::removeUniqueId(int uniqueId) {
86    mDrmManager->removeUniqueId(uniqueId);
87}
88
89void DrmManagerService::addClient(int uniqueId) {
90    mDrmManager->addClient(uniqueId);
91}
92
93void DrmManagerService::removeClient(int uniqueId) {
94    mDrmManager->removeClient(uniqueId);
95}
96
97status_t DrmManagerService::setDrmServiceListener(
98            int uniqueId, const sp<IDrmServiceListener>& drmServiceListener) {
99    LOGV("Entering setDrmServiceListener");
100    mDrmManager->setDrmServiceListener(uniqueId, drmServiceListener);
101    return DRM_NO_ERROR;
102}
103
104status_t DrmManagerService::installDrmEngine(int uniqueId, const String8& drmEngineFile) {
105    LOGV("Entering installDrmEngine");
106    return mDrmManager->installDrmEngine(uniqueId, drmEngineFile);
107}
108
109DrmConstraints* DrmManagerService::getConstraints(
110            int uniqueId, const String8* path, const int action) {
111    LOGV("Entering getConstraints from content");
112    return mDrmManager->getConstraints(uniqueId, path, action);
113}
114
115DrmMetadata* DrmManagerService::getMetadata(int uniqueId, const String8* path) {
116    LOGV("Entering getMetadata from content");
117    return mDrmManager->getMetadata(uniqueId, path);
118}
119
120bool DrmManagerService::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
121    LOGV("Entering canHandle");
122    return mDrmManager->canHandle(uniqueId, path, mimeType);
123}
124
125DrmInfoStatus* DrmManagerService::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
126    LOGV("Entering processDrmInfo");
127    return mDrmManager->processDrmInfo(uniqueId, drmInfo);
128}
129
130DrmInfo* DrmManagerService::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) {
131    LOGV("Entering acquireDrmInfo");
132    return mDrmManager->acquireDrmInfo(uniqueId, drmInfoRequest);
133}
134
135status_t DrmManagerService::saveRights(
136            int uniqueId, const DrmRights& drmRights,
137            const String8& rightsPath, const String8& contentPath) {
138    LOGV("Entering saveRights");
139    return mDrmManager->saveRights(uniqueId, drmRights, rightsPath, contentPath);
140}
141
142String8 DrmManagerService::getOriginalMimeType(int uniqueId, const String8& path) {
143    LOGV("Entering getOriginalMimeType");
144    return mDrmManager->getOriginalMimeType(uniqueId, path);
145}
146
147int DrmManagerService::getDrmObjectType(
148           int uniqueId, const String8& path, const String8& mimeType) {
149    LOGV("Entering getDrmObjectType");
150    return mDrmManager->getDrmObjectType(uniqueId, path, mimeType);
151}
152
153int DrmManagerService::checkRightsStatus(
154            int uniqueId, const String8& path, int action) {
155    LOGV("Entering checkRightsStatus");
156    return mDrmManager->checkRightsStatus(uniqueId, path, action);
157}
158
159status_t DrmManagerService::consumeRights(
160            int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
161    LOGV("Entering consumeRights");
162    return mDrmManager->consumeRights(uniqueId, decryptHandle, action, reserve);
163}
164
165status_t DrmManagerService::setPlaybackStatus(
166            int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int64_t position) {
167    LOGV("Entering setPlaybackStatus");
168    return mDrmManager->setPlaybackStatus(uniqueId, decryptHandle, playbackStatus, position);
169}
170
171bool DrmManagerService::validateAction(
172            int uniqueId, const String8& path,
173            int action, const ActionDescription& description) {
174    LOGV("Entering validateAction");
175    return mDrmManager->validateAction(uniqueId, path, action, description);
176}
177
178status_t DrmManagerService::removeRights(int uniqueId, const String8& path) {
179    LOGV("Entering removeRights");
180    return mDrmManager->removeRights(uniqueId, path);
181}
182
183status_t DrmManagerService::removeAllRights(int uniqueId) {
184    LOGV("Entering removeAllRights");
185    return mDrmManager->removeAllRights(uniqueId);
186}
187
188int DrmManagerService::openConvertSession(int uniqueId, const String8& mimeType) {
189    LOGV("Entering openConvertSession");
190    return mDrmManager->openConvertSession(uniqueId, mimeType);
191}
192
193DrmConvertedStatus* DrmManagerService::convertData(
194            int uniqueId, int convertId, const DrmBuffer* inputData) {
195    LOGV("Entering convertData");
196    return mDrmManager->convertData(uniqueId, convertId, inputData);
197}
198
199DrmConvertedStatus* DrmManagerService::closeConvertSession(int uniqueId, int convertId) {
200    LOGV("Entering closeConvertSession");
201    return mDrmManager->closeConvertSession(uniqueId, convertId);
202}
203
204status_t DrmManagerService::getAllSupportInfo(
205            int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
206    LOGV("Entering getAllSupportInfo");
207    return mDrmManager->getAllSupportInfo(uniqueId, length, drmSupportInfoArray);
208}
209
210DecryptHandle* DrmManagerService::openDecryptSession(
211            int uniqueId, int fd, off64_t offset, off64_t length) {
212    LOGV("Entering DrmManagerService::openDecryptSession");
213    if (isProtectedCallAllowed()) {
214        return mDrmManager->openDecryptSession(uniqueId, fd, offset, length);
215    }
216
217    return NULL;
218}
219
220DecryptHandle* DrmManagerService::openDecryptSession(
221            int uniqueId, const char* uri) {
222    LOGV("Entering DrmManagerService::openDecryptSession with uri");
223    if (isProtectedCallAllowed()) {
224        return mDrmManager->openDecryptSession(uniqueId, uri);
225    }
226
227    return NULL;
228}
229
230status_t DrmManagerService::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
231    LOGV("Entering closeDecryptSession");
232    return mDrmManager->closeDecryptSession(uniqueId, decryptHandle);
233}
234
235status_t DrmManagerService::initializeDecryptUnit(int uniqueId, DecryptHandle* decryptHandle,
236            int decryptUnitId, const DrmBuffer* headerInfo) {
237    LOGV("Entering initializeDecryptUnit");
238    return mDrmManager->initializeDecryptUnit(uniqueId,decryptHandle, decryptUnitId, headerInfo);
239}
240
241status_t DrmManagerService::decrypt(
242            int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId,
243            const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
244    LOGV("Entering decrypt");
245    return mDrmManager->decrypt(uniqueId, decryptHandle, decryptUnitId, encBuffer, decBuffer, IV);
246}
247
248status_t DrmManagerService::finalizeDecryptUnit(
249            int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) {
250    LOGV("Entering finalizeDecryptUnit");
251    return mDrmManager->finalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId);
252}
253
254ssize_t DrmManagerService::pread(int uniqueId, DecryptHandle* decryptHandle,
255            void* buffer, ssize_t numBytes, off64_t offset) {
256    LOGV("Entering pread");
257    return mDrmManager->pread(uniqueId, decryptHandle, buffer, numBytes, offset);
258}
259
260status_t DrmManagerService::dump(int fd, const Vector<String16>& args)
261{
262    const size_t SIZE = 256;
263    char buffer[SIZE];
264    String8 result;
265    if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
266        snprintf(buffer, SIZE, "Permission Denial: "
267                "can't dump DrmManagerService from pid=%d, uid=%d\n",
268                IPCThreadState::self()->getCallingPid(),
269                IPCThreadState::self()->getCallingUid());
270        result.append(buffer);
271    } else {
272#if DRM_MEMORY_LEAK_TRACK
273        bool dumpMem = false;
274        for (size_t i = 0; i < args.size(); i++) {
275            if (args[i] == String16("-m")) {
276                dumpMem = true;
277            }
278        }
279        if (dumpMem) {
280            dumpMemoryAddresses(fd);
281        }
282#endif
283    }
284    write(fd, result.string(), result.size());
285    return NO_ERROR;
286}
287
288