blob: 9a6f7877cdeef9110a61c454f7898b79fca2c1c3 [file] [log] [blame]
aimitakeshid074e302010-07-29 10:12:27 +09001/*
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
Takeshi Aimidc549d62010-09-20 23:40:41 +090017//#define LOG_NDEBUG 0
aimitakeshid074e302010-07-29 10:12:27 +090018#define LOG_TAG "DrmManager(Native)"
19#include "utils/Log.h"
20
21#include <utils/String8.h>
22#include <drm/DrmInfo.h>
23#include <drm/DrmInfoEvent.h>
24#include <drm/DrmRights.h>
25#include <drm/DrmConstraints.h>
Takeshi Aimidc9186562010-11-16 13:56:11 +090026#include <drm/DrmMetadata.h>
aimitakeshid074e302010-07-29 10:12:27 +090027#include <drm/DrmInfoStatus.h>
28#include <drm/DrmInfoRequest.h>
29#include <drm/DrmSupportInfo.h>
30#include <drm/DrmConvertedStatus.h>
31#include <IDrmEngine.h>
32
33#include "DrmManager.h"
34#include "ReadWriteUtils.h"
35
36#define DECRYPT_FILE_ERROR -1
37
38using namespace android;
39
Takeshi Aimidc549d62010-09-20 23:40:41 +090040Vector<int> DrmManager::mUniqueIdVector;
aimitakeshid074e302010-07-29 10:12:27 +090041const String8 DrmManager::EMPTY_STRING("");
42
43DrmManager::DrmManager() :
44 mDecryptSessionId(0),
45 mConvertId(0) {
46
47}
48
49DrmManager::~DrmManager() {
50
51}
52
Takeshi Aimidc549d62010-09-20 23:40:41 +090053int DrmManager::addUniqueId(int uniqueId) {
54 if (0 == uniqueId) {
55 int temp = 0;
56 bool foundUniqueId = false;
57 srand(time(NULL));
58
59 while (!foundUniqueId) {
60 const int size = mUniqueIdVector.size();
61 temp = rand() % 100;
62
63 int index = 0;
64 for (; index < size; ++index) {
65 if (mUniqueIdVector.itemAt(index) == temp) {
66 foundUniqueId = false;
67 break;
68 }
69 }
70 if (index == size) {
71 foundUniqueId = true;
72 }
73 }
74 uniqueId = temp;
75 }
76 mUniqueIdVector.push(uniqueId);
77 return uniqueId;
78}
79
80void DrmManager::removeUniqueId(int uniqueId) {
81 for (unsigned int i = 0; i < mUniqueIdVector.size(); i++) {
82 if (uniqueId == mUniqueIdVector.itemAt(i)) {
83 mUniqueIdVector.removeAt(i);
84 break;
85 }
86 }
87}
88
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +090089status_t DrmManager::loadPlugIns() {
aimitakeshid074e302010-07-29 10:12:27 +090090 String8 pluginDirPath("/system/lib/drm/plugins/native");
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +090091 return loadPlugIns(pluginDirPath);
aimitakeshid074e302010-07-29 10:12:27 +090092}
93
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +090094status_t DrmManager::loadPlugIns(const String8& plugInDirPath) {
aimitakeshid074e302010-07-29 10:12:27 +090095 if (mSupportInfoToPlugInIdMap.isEmpty()) {
96 mPlugInManager.loadPlugIns(plugInDirPath);
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +090097 Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
98 for (unsigned int i = 0; i < plugInPathList.size(); ++i) {
99 String8 plugInPath = plugInPathList[i];
100 DrmSupportInfo* info = mPlugInManager.getPlugIn(plugInPath).getSupportInfo(0);
101 if (NULL != info) {
102 mSupportInfoToPlugInIdMap.add(*info, plugInPath);
103 }
104 }
aimitakeshid074e302010-07-29 10:12:27 +0900105 }
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900106 return DRM_NO_ERROR;
107}
aimitakeshid074e302010-07-29 10:12:27 +0900108
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900109status_t DrmManager::unloadPlugIns() {
110 mConvertSessionMap.clear();
111 mDecryptSessionMap.clear();
112 mPlugInManager.unloadPlugIns();
113 mSupportInfoToPlugInIdMap.clear();
aimitakeshid074e302010-07-29 10:12:27 +0900114 return DRM_NO_ERROR;
115}
116
117status_t DrmManager::setDrmServiceListener(
118 int uniqueId, const sp<IDrmServiceListener>& drmServiceListener) {
119 Mutex::Autolock _l(mLock);
120 mServiceListeners.add(uniqueId, drmServiceListener);
121 return DRM_NO_ERROR;
122}
123
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900124void DrmManager::addClient(int uniqueId) {
125 if (!mSupportInfoToPlugInIdMap.isEmpty()) {
126 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
127 for (unsigned int index = 0; index < plugInIdList.size(); index++) {
128 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
129 rDrmEngine.initialize(uniqueId);
130 rDrmEngine.setOnInfoListener(uniqueId, this);
131 }
132 }
133}
aimitakeshid074e302010-07-29 10:12:27 +0900134
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900135void DrmManager::removeClient(int uniqueId) {
136 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
aimitakeshid074e302010-07-29 10:12:27 +0900137 for (unsigned int index = 0; index < plugInIdList.size(); index++) {
138 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
139 rDrmEngine.terminate(uniqueId);
140 }
aimitakeshid074e302010-07-29 10:12:27 +0900141}
142
143DrmConstraints* DrmManager::getConstraints(int uniqueId, const String8* path, const int action) {
144 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, *path);
145 if (EMPTY_STRING != plugInId) {
146 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
147 return rDrmEngine.getConstraints(uniqueId, path, action);
148 }
149 return NULL;
150}
151
Takeshi Aimidc9186562010-11-16 13:56:11 +0900152DrmMetadata* DrmManager::getMetadata(int uniqueId, const String8* path) {
153 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, *path);
154 if (EMPTY_STRING != plugInId) {
155 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
156 return rDrmEngine.getMetadata(uniqueId, path);
157 }
158 return NULL;
159}
160
aimitakeshid074e302010-07-29 10:12:27 +0900161status_t DrmManager::installDrmEngine(int uniqueId, const String8& absolutePath) {
162 mPlugInManager.loadPlugIn(absolutePath);
163
164 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(absolutePath);
165 rDrmEngine.initialize(uniqueId);
166 rDrmEngine.setOnInfoListener(uniqueId, this);
167
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900168 DrmSupportInfo* info = rDrmEngine.getSupportInfo(0);
aimitakeshid074e302010-07-29 10:12:27 +0900169 mSupportInfoToPlugInIdMap.add(*info, absolutePath);
170
171 return DRM_NO_ERROR;
172}
173
174bool DrmManager::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
175 const String8 plugInId = getSupportedPlugInId(mimeType);
176 bool result = (EMPTY_STRING != plugInId) ? true : false;
177
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900178 if (0 < path.length()) {
aimitakeshid074e302010-07-29 10:12:27 +0900179 if (result) {
180 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
181 result = rDrmEngine.canHandle(uniqueId, path);
182 } else {
183 result = canHandle(uniqueId, path);
184 }
185 }
186 return result;
187}
188
189DrmInfoStatus* DrmManager::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
190 const String8 plugInId = getSupportedPlugInId(drmInfo->getMimeType());
191 if (EMPTY_STRING != plugInId) {
192 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
193 return rDrmEngine.processDrmInfo(uniqueId, drmInfo);
194 }
195 return NULL;
196}
197
198bool DrmManager::canHandle(int uniqueId, const String8& path) {
199 bool result = false;
200 Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
201
202 for (unsigned int i = 0; i < plugInPathList.size(); ++i) {
203 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInPathList[i]);
204 result = rDrmEngine.canHandle(uniqueId, path);
205
206 if (result) {
207 break;
208 }
209 }
210 return result;
211}
212
213DrmInfo* DrmManager::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) {
214 const String8 plugInId = getSupportedPlugInId(drmInfoRequest->getMimeType());
215 if (EMPTY_STRING != plugInId) {
216 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
217 return rDrmEngine.acquireDrmInfo(uniqueId, drmInfoRequest);
218 }
219 return NULL;
220}
221
Takeshi Aimidc549d62010-09-20 23:40:41 +0900222status_t DrmManager::saveRights(int uniqueId, const DrmRights& drmRights,
aimitakeshid074e302010-07-29 10:12:27 +0900223 const String8& rightsPath, const String8& contentPath) {
224 const String8 plugInId = getSupportedPlugInId(drmRights.getMimeType());
Takeshi Aimidc549d62010-09-20 23:40:41 +0900225 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshid074e302010-07-29 10:12:27 +0900226 if (EMPTY_STRING != plugInId) {
227 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900228 result = rDrmEngine.saveRights(uniqueId, drmRights, rightsPath, contentPath);
aimitakeshid074e302010-07-29 10:12:27 +0900229 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900230 return result;
aimitakeshid074e302010-07-29 10:12:27 +0900231}
232
233String8 DrmManager::getOriginalMimeType(int uniqueId, const String8& path) {
234 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
235 if (EMPTY_STRING != plugInId) {
236 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
237 return rDrmEngine.getOriginalMimeType(uniqueId, path);
238 }
239 return EMPTY_STRING;
240}
241
242int DrmManager::getDrmObjectType(int uniqueId, const String8& path, const String8& mimeType) {
243 const String8 plugInId = getSupportedPlugInId(uniqueId, path, mimeType);
244 if (EMPTY_STRING != plugInId) {
245 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
246 return rDrmEngine.getDrmObjectType(uniqueId, path, mimeType);
247 }
248 return DrmObjectType::UNKNOWN;
249}
250
251int DrmManager::checkRightsStatus(int uniqueId, const String8& path, int action) {
252 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
253 if (EMPTY_STRING != plugInId) {
254 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
255 return rDrmEngine.checkRightsStatus(uniqueId, path, action);
256 }
257 return RightsStatus::RIGHTS_INVALID;
258}
259
Takeshi Aimidc549d62010-09-20 23:40:41 +0900260status_t DrmManager::consumeRights(
aimitakeshid074e302010-07-29 10:12:27 +0900261 int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
Takeshi Aimidc549d62010-09-20 23:40:41 +0900262 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshid074e302010-07-29 10:12:27 +0900263 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
264 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900265 result = drmEngine->consumeRights(uniqueId, decryptHandle, action, reserve);
aimitakeshid074e302010-07-29 10:12:27 +0900266 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900267 return result;
aimitakeshid074e302010-07-29 10:12:27 +0900268}
269
Takeshi Aimidc549d62010-09-20 23:40:41 +0900270status_t DrmManager::setPlaybackStatus(
Gloria Wang5fc3edb2010-11-19 15:19:36 -0800271 int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int64_t position) {
Takeshi Aimidc549d62010-09-20 23:40:41 +0900272 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshid074e302010-07-29 10:12:27 +0900273 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
274 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900275 result = drmEngine->setPlaybackStatus(uniqueId, decryptHandle, playbackStatus, position);
aimitakeshid074e302010-07-29 10:12:27 +0900276 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900277 return result;
aimitakeshid074e302010-07-29 10:12:27 +0900278}
279
280bool DrmManager::validateAction(
281 int uniqueId, const String8& path, int action, const ActionDescription& description) {
282 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
283 if (EMPTY_STRING != plugInId) {
284 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
285 return rDrmEngine.validateAction(uniqueId, path, action, description);
286 }
287 return false;
288}
289
Takeshi Aimidc549d62010-09-20 23:40:41 +0900290status_t DrmManager::removeRights(int uniqueId, const String8& path) {
aimitakeshid074e302010-07-29 10:12:27 +0900291 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900292 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshid074e302010-07-29 10:12:27 +0900293 if (EMPTY_STRING != plugInId) {
294 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900295 result = rDrmEngine.removeRights(uniqueId, path);
aimitakeshid074e302010-07-29 10:12:27 +0900296 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900297 return result;
aimitakeshid074e302010-07-29 10:12:27 +0900298}
299
Takeshi Aimidc549d62010-09-20 23:40:41 +0900300status_t DrmManager::removeAllRights(int uniqueId) {
aimitakeshid074e302010-07-29 10:12:27 +0900301 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
Takeshi Aimidc549d62010-09-20 23:40:41 +0900302 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshid074e302010-07-29 10:12:27 +0900303 for (unsigned int index = 0; index < plugInIdList.size(); index++) {
304 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
Takeshi Aimidc549d62010-09-20 23:40:41 +0900305 result = rDrmEngine.removeAllRights(uniqueId);
306 if (DRM_NO_ERROR != result) {
307 break;
308 }
aimitakeshid074e302010-07-29 10:12:27 +0900309 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900310 return result;
aimitakeshid074e302010-07-29 10:12:27 +0900311}
312
313int DrmManager::openConvertSession(int uniqueId, const String8& mimeType) {
314 int convertId = -1;
315
316 const String8 plugInId = getSupportedPlugInId(mimeType);
317 if (EMPTY_STRING != plugInId) {
318 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
319
Takeshi Aimidc549d62010-09-20 23:40:41 +0900320 if (DRM_NO_ERROR == rDrmEngine.openConvertSession(uniqueId, mConvertId + 1)) {
321 Mutex::Autolock _l(mConvertLock);
322 ++mConvertId;
323 convertId = mConvertId;
324 mConvertSessionMap.add(convertId, &rDrmEngine);
325 }
aimitakeshid074e302010-07-29 10:12:27 +0900326 }
327 return convertId;
328}
329
330DrmConvertedStatus* DrmManager::convertData(
331 int uniqueId, int convertId, const DrmBuffer* inputData) {
332 DrmConvertedStatus *drmConvertedStatus = NULL;
333
334 if (mConvertSessionMap.indexOfKey(convertId) != NAME_NOT_FOUND) {
335 IDrmEngine* drmEngine = mConvertSessionMap.valueFor(convertId);
336 drmConvertedStatus = drmEngine->convertData(uniqueId, convertId, inputData);
337 }
338 return drmConvertedStatus;
339}
340
341DrmConvertedStatus* DrmManager::closeConvertSession(int uniqueId, int convertId) {
342 DrmConvertedStatus *drmConvertedStatus = NULL;
343
344 if (mConvertSessionMap.indexOfKey(convertId) != NAME_NOT_FOUND) {
345 IDrmEngine* drmEngine = mConvertSessionMap.valueFor(convertId);
346 drmConvertedStatus = drmEngine->closeConvertSession(uniqueId, convertId);
347 mConvertSessionMap.removeItem(convertId);
348 }
349 return drmConvertedStatus;
350}
351
352status_t DrmManager::getAllSupportInfo(
353 int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
354 Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
355 int size = plugInPathList.size();
356 int validPlugins = 0;
357
358 if (0 < size) {
359 Vector<DrmSupportInfo> drmSupportInfoList;
360
361 for (int i = 0; i < size; ++i) {
362 String8 plugInPath = plugInPathList[i];
363 DrmSupportInfo* drmSupportInfo
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900364 = mPlugInManager.getPlugIn(plugInPath).getSupportInfo(0);
aimitakeshid074e302010-07-29 10:12:27 +0900365 if (NULL != drmSupportInfo) {
366 drmSupportInfoList.add(*drmSupportInfo);
367 delete drmSupportInfo; drmSupportInfo = NULL;
368 }
369 }
370
371 validPlugins = drmSupportInfoList.size();
372 if (0 < validPlugins) {
373 *drmSupportInfoArray = new DrmSupportInfo[validPlugins];
374 for (int i = 0; i < validPlugins; ++i) {
375 (*drmSupportInfoArray)[i] = drmSupportInfoList[i];
376 }
377 }
378 }
379 *length = validPlugins;
380 return DRM_NO_ERROR;
381}
382
Gloria Wang5fc3edb2010-11-19 15:19:36 -0800383DecryptHandle* DrmManager::openDecryptSession(int uniqueId, int fd, off64_t offset, off64_t length) {
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900384 Mutex::Autolock _l(mDecryptLock);
aimitakeshid074e302010-07-29 10:12:27 +0900385 status_t result = DRM_ERROR_CANNOT_HANDLE;
386 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
387
388 DecryptHandle* handle = new DecryptHandle();
389 if (NULL != handle) {
aimitakeshid074e302010-07-29 10:12:27 +0900390 handle->decryptId = mDecryptSessionId + 1;
391
392 for (unsigned int index = 0; index < plugInIdList.size(); index++) {
393 String8 plugInId = plugInIdList.itemAt(index);
394 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
395 result = rDrmEngine.openDecryptSession(uniqueId, handle, fd, offset, length);
396
aimitakeshid074e302010-07-29 10:12:27 +0900397 if (DRM_NO_ERROR == result) {
398 ++mDecryptSessionId;
399 mDecryptSessionMap.add(mDecryptSessionId, &rDrmEngine);
aimitakeshid074e302010-07-29 10:12:27 +0900400 break;
401 }
402 }
403 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900404 if (DRM_NO_ERROR != result) {
aimitakeshid074e302010-07-29 10:12:27 +0900405 delete handle; handle = NULL;
aimitakeshid074e302010-07-29 10:12:27 +0900406 }
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900407 return handle;
408}
aimitakeshid074e302010-07-29 10:12:27 +0900409
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900410DecryptHandle* DrmManager::openDecryptSession(int uniqueId, const char* uri) {
411 Mutex::Autolock _l(mDecryptLock);
412 status_t result = DRM_ERROR_CANNOT_HANDLE;
413 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
414
415 DecryptHandle* handle = new DecryptHandle();
416 if (NULL != handle) {
417 handle->decryptId = mDecryptSessionId + 1;
418
419 for (unsigned int index = 0; index < plugInIdList.size(); index++) {
420 String8 plugInId = plugInIdList.itemAt(index);
421 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
422 result = rDrmEngine.openDecryptSession(uniqueId, handle, uri);
423
424 if (DRM_NO_ERROR == result) {
425 ++mDecryptSessionId;
426 mDecryptSessionMap.add(mDecryptSessionId, &rDrmEngine);
427 break;
428 }
429 }
430 }
431 if (DRM_NO_ERROR != result) {
432 delete handle; handle = NULL;
433 LOGE("DrmManager::openDecryptSession: no capable plug-in found");
434 }
aimitakeshid074e302010-07-29 10:12:27 +0900435 return handle;
436}
437
Takeshi Aimidc549d62010-09-20 23:40:41 +0900438status_t DrmManager::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900439 Mutex::Autolock _l(mDecryptLock);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900440 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshid074e302010-07-29 10:12:27 +0900441 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
442 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900443 result = drmEngine->closeDecryptSession(uniqueId, decryptHandle);
444 if (DRM_NO_ERROR == result) {
445 mDecryptSessionMap.removeItem(decryptHandle->decryptId);
446 }
aimitakeshid074e302010-07-29 10:12:27 +0900447 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900448 return result;
aimitakeshid074e302010-07-29 10:12:27 +0900449}
450
Takeshi Aimidc549d62010-09-20 23:40:41 +0900451status_t DrmManager::initializeDecryptUnit(
aimitakeshid074e302010-07-29 10:12:27 +0900452 int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId, const DrmBuffer* headerInfo) {
Takeshi Aimidc549d62010-09-20 23:40:41 +0900453 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshid074e302010-07-29 10:12:27 +0900454 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
455 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900456 result = drmEngine->initializeDecryptUnit(uniqueId, decryptHandle, decryptUnitId, headerInfo);
aimitakeshid074e302010-07-29 10:12:27 +0900457 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900458 return result;
aimitakeshid074e302010-07-29 10:12:27 +0900459}
460
Takeshi Aimidc549d62010-09-20 23:40:41 +0900461status_t DrmManager::decrypt(int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId,
462 const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
463 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshid074e302010-07-29 10:12:27 +0900464 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
465 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900466 result = drmEngine->decrypt(
467 uniqueId, decryptHandle, decryptUnitId, encBuffer, decBuffer, IV);
aimitakeshid074e302010-07-29 10:12:27 +0900468 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900469 return result;
aimitakeshid074e302010-07-29 10:12:27 +0900470}
471
Takeshi Aimidc549d62010-09-20 23:40:41 +0900472status_t DrmManager::finalizeDecryptUnit(
aimitakeshid074e302010-07-29 10:12:27 +0900473 int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) {
Takeshi Aimidc549d62010-09-20 23:40:41 +0900474 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshid074e302010-07-29 10:12:27 +0900475 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
476 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900477 result = drmEngine->finalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId);
aimitakeshid074e302010-07-29 10:12:27 +0900478 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900479 return result;
aimitakeshid074e302010-07-29 10:12:27 +0900480}
481
482ssize_t DrmManager::pread(int uniqueId, DecryptHandle* decryptHandle,
Gloria Wang5fc3edb2010-11-19 15:19:36 -0800483 void* buffer, ssize_t numBytes, off64_t offset) {
aimitakeshid074e302010-07-29 10:12:27 +0900484 ssize_t result = DECRYPT_FILE_ERROR;
485
486 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
487 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
488 result = drmEngine->pread(uniqueId, decryptHandle, buffer, numBytes, offset);
489 }
490 return result;
491}
492
aimitakeshid074e302010-07-29 10:12:27 +0900493String8 DrmManager::getSupportedPlugInId(
494 int uniqueId, const String8& path, const String8& mimeType) {
495 String8 plugInId("");
496
497 if (EMPTY_STRING != mimeType) {
498 plugInId = getSupportedPlugInId(mimeType);
499 } else {
500 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
501 }
502 return plugInId;
503}
504
505String8 DrmManager::getSupportedPlugInId(const String8& mimeType) {
506 String8 plugInId("");
507
508 if (EMPTY_STRING != mimeType) {
509 for (unsigned int index = 0; index < mSupportInfoToPlugInIdMap.size(); index++) {
510 const DrmSupportInfo& drmSupportInfo = mSupportInfoToPlugInIdMap.keyAt(index);
511
512 if (drmSupportInfo.isSupportedMimeType(mimeType)) {
513 plugInId = mSupportInfoToPlugInIdMap.valueFor(drmSupportInfo);
514 break;
515 }
516 }
517 }
518 return plugInId;
519}
520
521String8 DrmManager::getSupportedPlugInIdFromPath(int uniqueId, const String8& path) {
522 String8 plugInId("");
523 const String8 fileSuffix = path.getPathExtension();
524
525 for (unsigned int index = 0; index < mSupportInfoToPlugInIdMap.size(); index++) {
526 const DrmSupportInfo& drmSupportInfo = mSupportInfoToPlugInIdMap.keyAt(index);
527
528 if (drmSupportInfo.isSupportedFileSuffix(fileSuffix)) {
529 String8 key = mSupportInfoToPlugInIdMap.valueFor(drmSupportInfo);
530 IDrmEngine& drmEngine = mPlugInManager.getPlugIn(key);
531
532 if (drmEngine.canHandle(uniqueId, path)) {
533 plugInId = key;
534 break;
535 }
536 }
537 }
538 return plugInId;
539}
540
541void DrmManager::onInfo(const DrmInfoEvent& event) {
542 Mutex::Autolock _l(mLock);
543 for (unsigned int index = 0; index < mServiceListeners.size(); index++) {
544 int uniqueId = mServiceListeners.keyAt(index);
545
546 if (uniqueId == event.getUniqueId()) {
547 sp<IDrmServiceListener> serviceListener = mServiceListeners.valueFor(uniqueId);
548 serviceListener->notify(event);
549 }
550 }
551}
552