blob: 35e62f3bc4ee86a6f432b1b487c41cd4d24ee186 [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>
26#include <drm/DrmInfoStatus.h>
27#include <drm/DrmInfoRequest.h>
28#include <drm/DrmSupportInfo.h>
29#include <drm/DrmConvertedStatus.h>
30#include <IDrmEngine.h>
31
32#include "DrmManager.h"
33#include "ReadWriteUtils.h"
34
35#define DECRYPT_FILE_ERROR -1
36
37using namespace android;
38
Takeshi Aimidc549d62010-09-20 23:40:41 +090039Vector<int> DrmManager::mUniqueIdVector;
aimitakeshid074e302010-07-29 10:12:27 +090040const String8 DrmManager::EMPTY_STRING("");
41
42DrmManager::DrmManager() :
43 mDecryptSessionId(0),
44 mConvertId(0) {
45
46}
47
48DrmManager::~DrmManager() {
49
50}
51
Takeshi Aimidc549d62010-09-20 23:40:41 +090052int DrmManager::addUniqueId(int uniqueId) {
53 if (0 == uniqueId) {
54 int temp = 0;
55 bool foundUniqueId = false;
56 srand(time(NULL));
57
58 while (!foundUniqueId) {
59 const int size = mUniqueIdVector.size();
60 temp = rand() % 100;
61
62 int index = 0;
63 for (; index < size; ++index) {
64 if (mUniqueIdVector.itemAt(index) == temp) {
65 foundUniqueId = false;
66 break;
67 }
68 }
69 if (index == size) {
70 foundUniqueId = true;
71 }
72 }
73 uniqueId = temp;
74 }
75 mUniqueIdVector.push(uniqueId);
76 return uniqueId;
77}
78
79void DrmManager::removeUniqueId(int uniqueId) {
80 for (unsigned int i = 0; i < mUniqueIdVector.size(); i++) {
81 if (uniqueId == mUniqueIdVector.itemAt(i)) {
82 mUniqueIdVector.removeAt(i);
83 break;
84 }
85 }
86}
87
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +090088status_t DrmManager::loadPlugIns() {
aimitakeshid074e302010-07-29 10:12:27 +090089 String8 pluginDirPath("/system/lib/drm/plugins/native");
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +090090 return loadPlugIns(pluginDirPath);
aimitakeshid074e302010-07-29 10:12:27 +090091}
92
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +090093status_t DrmManager::loadPlugIns(const String8& plugInDirPath) {
aimitakeshid074e302010-07-29 10:12:27 +090094 if (mSupportInfoToPlugInIdMap.isEmpty()) {
95 mPlugInManager.loadPlugIns(plugInDirPath);
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +090096 Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
97 for (unsigned int i = 0; i < plugInPathList.size(); ++i) {
98 String8 plugInPath = plugInPathList[i];
99 DrmSupportInfo* info = mPlugInManager.getPlugIn(plugInPath).getSupportInfo(0);
100 if (NULL != info) {
101 mSupportInfoToPlugInIdMap.add(*info, plugInPath);
102 }
103 }
aimitakeshid074e302010-07-29 10:12:27 +0900104 }
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900105 return DRM_NO_ERROR;
106}
aimitakeshid074e302010-07-29 10:12:27 +0900107
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900108status_t DrmManager::unloadPlugIns() {
109 mConvertSessionMap.clear();
110 mDecryptSessionMap.clear();
111 mPlugInManager.unloadPlugIns();
112 mSupportInfoToPlugInIdMap.clear();
aimitakeshid074e302010-07-29 10:12:27 +0900113 return DRM_NO_ERROR;
114}
115
116status_t DrmManager::setDrmServiceListener(
117 int uniqueId, const sp<IDrmServiceListener>& drmServiceListener) {
118 Mutex::Autolock _l(mLock);
119 mServiceListeners.add(uniqueId, drmServiceListener);
120 return DRM_NO_ERROR;
121}
122
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900123void DrmManager::addClient(int uniqueId) {
124 if (!mSupportInfoToPlugInIdMap.isEmpty()) {
125 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
126 for (unsigned int index = 0; index < plugInIdList.size(); index++) {
127 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
128 rDrmEngine.initialize(uniqueId);
129 rDrmEngine.setOnInfoListener(uniqueId, this);
130 }
131 }
132}
aimitakeshid074e302010-07-29 10:12:27 +0900133
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900134void DrmManager::removeClient(int uniqueId) {
135 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
aimitakeshid074e302010-07-29 10:12:27 +0900136 for (unsigned int index = 0; index < plugInIdList.size(); index++) {
137 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
138 rDrmEngine.terminate(uniqueId);
139 }
aimitakeshid074e302010-07-29 10:12:27 +0900140}
141
142DrmConstraints* DrmManager::getConstraints(int uniqueId, const String8* path, const int action) {
143 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, *path);
144 if (EMPTY_STRING != plugInId) {
145 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
146 return rDrmEngine.getConstraints(uniqueId, path, action);
147 }
148 return NULL;
149}
150
151status_t DrmManager::installDrmEngine(int uniqueId, const String8& absolutePath) {
152 mPlugInManager.loadPlugIn(absolutePath);
153
154 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(absolutePath);
155 rDrmEngine.initialize(uniqueId);
156 rDrmEngine.setOnInfoListener(uniqueId, this);
157
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900158 DrmSupportInfo* info = rDrmEngine.getSupportInfo(0);
aimitakeshid074e302010-07-29 10:12:27 +0900159 mSupportInfoToPlugInIdMap.add(*info, absolutePath);
160
161 return DRM_NO_ERROR;
162}
163
164bool DrmManager::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
165 const String8 plugInId = getSupportedPlugInId(mimeType);
166 bool result = (EMPTY_STRING != plugInId) ? true : false;
167
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900168 if (0 < path.length()) {
aimitakeshid074e302010-07-29 10:12:27 +0900169 if (result) {
170 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
171 result = rDrmEngine.canHandle(uniqueId, path);
172 } else {
173 result = canHandle(uniqueId, path);
174 }
175 }
176 return result;
177}
178
179DrmInfoStatus* DrmManager::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
180 const String8 plugInId = getSupportedPlugInId(drmInfo->getMimeType());
181 if (EMPTY_STRING != plugInId) {
182 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
183 return rDrmEngine.processDrmInfo(uniqueId, drmInfo);
184 }
185 return NULL;
186}
187
188bool DrmManager::canHandle(int uniqueId, const String8& path) {
189 bool result = false;
190 Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
191
192 for (unsigned int i = 0; i < plugInPathList.size(); ++i) {
193 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInPathList[i]);
194 result = rDrmEngine.canHandle(uniqueId, path);
195
196 if (result) {
197 break;
198 }
199 }
200 return result;
201}
202
203DrmInfo* DrmManager::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) {
204 const String8 plugInId = getSupportedPlugInId(drmInfoRequest->getMimeType());
205 if (EMPTY_STRING != plugInId) {
206 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
207 return rDrmEngine.acquireDrmInfo(uniqueId, drmInfoRequest);
208 }
209 return NULL;
210}
211
Takeshi Aimidc549d62010-09-20 23:40:41 +0900212status_t DrmManager::saveRights(int uniqueId, const DrmRights& drmRights,
aimitakeshid074e302010-07-29 10:12:27 +0900213 const String8& rightsPath, const String8& contentPath) {
214 const String8 plugInId = getSupportedPlugInId(drmRights.getMimeType());
Takeshi Aimidc549d62010-09-20 23:40:41 +0900215 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshid074e302010-07-29 10:12:27 +0900216 if (EMPTY_STRING != plugInId) {
217 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900218 result = rDrmEngine.saveRights(uniqueId, drmRights, rightsPath, contentPath);
aimitakeshid074e302010-07-29 10:12:27 +0900219 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900220 return result;
aimitakeshid074e302010-07-29 10:12:27 +0900221}
222
223String8 DrmManager::getOriginalMimeType(int uniqueId, const String8& path) {
224 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
225 if (EMPTY_STRING != plugInId) {
226 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
227 return rDrmEngine.getOriginalMimeType(uniqueId, path);
228 }
229 return EMPTY_STRING;
230}
231
232int DrmManager::getDrmObjectType(int uniqueId, const String8& path, const String8& mimeType) {
233 const String8 plugInId = getSupportedPlugInId(uniqueId, path, mimeType);
234 if (EMPTY_STRING != plugInId) {
235 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
236 return rDrmEngine.getDrmObjectType(uniqueId, path, mimeType);
237 }
238 return DrmObjectType::UNKNOWN;
239}
240
241int DrmManager::checkRightsStatus(int uniqueId, const String8& path, int action) {
242 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
243 if (EMPTY_STRING != plugInId) {
244 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
245 return rDrmEngine.checkRightsStatus(uniqueId, path, action);
246 }
247 return RightsStatus::RIGHTS_INVALID;
248}
249
Takeshi Aimidc549d62010-09-20 23:40:41 +0900250status_t DrmManager::consumeRights(
aimitakeshid074e302010-07-29 10:12:27 +0900251 int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
Takeshi Aimidc549d62010-09-20 23:40:41 +0900252 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshid074e302010-07-29 10:12:27 +0900253 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
254 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900255 result = drmEngine->consumeRights(uniqueId, decryptHandle, action, reserve);
aimitakeshid074e302010-07-29 10:12:27 +0900256 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900257 return result;
aimitakeshid074e302010-07-29 10:12:27 +0900258}
259
Takeshi Aimidc549d62010-09-20 23:40:41 +0900260status_t DrmManager::setPlaybackStatus(
aimitakeshid074e302010-07-29 10:12:27 +0900261 int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int position) {
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->setPlaybackStatus(uniqueId, decryptHandle, playbackStatus, position);
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
270bool DrmManager::validateAction(
271 int uniqueId, const String8& path, int action, const ActionDescription& description) {
272 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
273 if (EMPTY_STRING != plugInId) {
274 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
275 return rDrmEngine.validateAction(uniqueId, path, action, description);
276 }
277 return false;
278}
279
Takeshi Aimidc549d62010-09-20 23:40:41 +0900280status_t DrmManager::removeRights(int uniqueId, const String8& path) {
aimitakeshid074e302010-07-29 10:12:27 +0900281 const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900282 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshid074e302010-07-29 10:12:27 +0900283 if (EMPTY_STRING != plugInId) {
284 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900285 result = rDrmEngine.removeRights(uniqueId, path);
aimitakeshid074e302010-07-29 10:12:27 +0900286 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900287 return result;
aimitakeshid074e302010-07-29 10:12:27 +0900288}
289
Takeshi Aimidc549d62010-09-20 23:40:41 +0900290status_t DrmManager::removeAllRights(int uniqueId) {
aimitakeshid074e302010-07-29 10:12:27 +0900291 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
Takeshi Aimidc549d62010-09-20 23:40:41 +0900292 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshid074e302010-07-29 10:12:27 +0900293 for (unsigned int index = 0; index < plugInIdList.size(); index++) {
294 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index));
Takeshi Aimidc549d62010-09-20 23:40:41 +0900295 result = rDrmEngine.removeAllRights(uniqueId);
296 if (DRM_NO_ERROR != result) {
297 break;
298 }
aimitakeshid074e302010-07-29 10:12:27 +0900299 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900300 return result;
aimitakeshid074e302010-07-29 10:12:27 +0900301}
302
303int DrmManager::openConvertSession(int uniqueId, const String8& mimeType) {
304 int convertId = -1;
305
306 const String8 plugInId = getSupportedPlugInId(mimeType);
307 if (EMPTY_STRING != plugInId) {
308 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
309
Takeshi Aimidc549d62010-09-20 23:40:41 +0900310 if (DRM_NO_ERROR == rDrmEngine.openConvertSession(uniqueId, mConvertId + 1)) {
311 Mutex::Autolock _l(mConvertLock);
312 ++mConvertId;
313 convertId = mConvertId;
314 mConvertSessionMap.add(convertId, &rDrmEngine);
315 }
aimitakeshid074e302010-07-29 10:12:27 +0900316 }
317 return convertId;
318}
319
320DrmConvertedStatus* DrmManager::convertData(
321 int uniqueId, int convertId, const DrmBuffer* inputData) {
322 DrmConvertedStatus *drmConvertedStatus = NULL;
323
324 if (mConvertSessionMap.indexOfKey(convertId) != NAME_NOT_FOUND) {
325 IDrmEngine* drmEngine = mConvertSessionMap.valueFor(convertId);
326 drmConvertedStatus = drmEngine->convertData(uniqueId, convertId, inputData);
327 }
328 return drmConvertedStatus;
329}
330
331DrmConvertedStatus* DrmManager::closeConvertSession(int uniqueId, int convertId) {
332 DrmConvertedStatus *drmConvertedStatus = NULL;
333
334 if (mConvertSessionMap.indexOfKey(convertId) != NAME_NOT_FOUND) {
335 IDrmEngine* drmEngine = mConvertSessionMap.valueFor(convertId);
336 drmConvertedStatus = drmEngine->closeConvertSession(uniqueId, convertId);
337 mConvertSessionMap.removeItem(convertId);
338 }
339 return drmConvertedStatus;
340}
341
342status_t DrmManager::getAllSupportInfo(
343 int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
344 Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList();
345 int size = plugInPathList.size();
346 int validPlugins = 0;
347
348 if (0 < size) {
349 Vector<DrmSupportInfo> drmSupportInfoList;
350
351 for (int i = 0; i < size; ++i) {
352 String8 plugInPath = plugInPathList[i];
353 DrmSupportInfo* drmSupportInfo
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900354 = mPlugInManager.getPlugIn(plugInPath).getSupportInfo(0);
aimitakeshid074e302010-07-29 10:12:27 +0900355 if (NULL != drmSupportInfo) {
356 drmSupportInfoList.add(*drmSupportInfo);
357 delete drmSupportInfo; drmSupportInfo = NULL;
358 }
359 }
360
361 validPlugins = drmSupportInfoList.size();
362 if (0 < validPlugins) {
363 *drmSupportInfoArray = new DrmSupportInfo[validPlugins];
364 for (int i = 0; i < validPlugins; ++i) {
365 (*drmSupportInfoArray)[i] = drmSupportInfoList[i];
366 }
367 }
368 }
369 *length = validPlugins;
370 return DRM_NO_ERROR;
371}
372
373DecryptHandle* DrmManager::openDecryptSession(int uniqueId, int fd, int offset, int length) {
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900374 Mutex::Autolock _l(mDecryptLock);
aimitakeshid074e302010-07-29 10:12:27 +0900375 status_t result = DRM_ERROR_CANNOT_HANDLE;
376 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
377
378 DecryptHandle* handle = new DecryptHandle();
379 if (NULL != handle) {
aimitakeshid074e302010-07-29 10:12:27 +0900380 handle->decryptId = mDecryptSessionId + 1;
381
382 for (unsigned int index = 0; index < plugInIdList.size(); index++) {
383 String8 plugInId = plugInIdList.itemAt(index);
384 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
385 result = rDrmEngine.openDecryptSession(uniqueId, handle, fd, offset, length);
386
aimitakeshid074e302010-07-29 10:12:27 +0900387 if (DRM_NO_ERROR == result) {
388 ++mDecryptSessionId;
389 mDecryptSessionMap.add(mDecryptSessionId, &rDrmEngine);
aimitakeshid074e302010-07-29 10:12:27 +0900390 break;
391 }
392 }
393 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900394 if (DRM_NO_ERROR != result) {
aimitakeshid074e302010-07-29 10:12:27 +0900395 delete handle; handle = NULL;
aimitakeshid074e302010-07-29 10:12:27 +0900396 }
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900397 return handle;
398}
aimitakeshid074e302010-07-29 10:12:27 +0900399
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900400DecryptHandle* DrmManager::openDecryptSession(int uniqueId, const char* uri) {
401 Mutex::Autolock _l(mDecryptLock);
402 status_t result = DRM_ERROR_CANNOT_HANDLE;
403 Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList();
404
405 DecryptHandle* handle = new DecryptHandle();
406 if (NULL != handle) {
407 handle->decryptId = mDecryptSessionId + 1;
408
409 for (unsigned int index = 0; index < plugInIdList.size(); index++) {
410 String8 plugInId = plugInIdList.itemAt(index);
411 IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId);
412 result = rDrmEngine.openDecryptSession(uniqueId, handle, uri);
413
414 if (DRM_NO_ERROR == result) {
415 ++mDecryptSessionId;
416 mDecryptSessionMap.add(mDecryptSessionId, &rDrmEngine);
417 break;
418 }
419 }
420 }
421 if (DRM_NO_ERROR != result) {
422 delete handle; handle = NULL;
423 LOGE("DrmManager::openDecryptSession: no capable plug-in found");
424 }
aimitakeshid074e302010-07-29 10:12:27 +0900425 return handle;
426}
427
Takeshi Aimidc549d62010-09-20 23:40:41 +0900428status_t DrmManager::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
Takeshi Aimic7b3ccc2010-10-08 23:05:49 +0900429 Mutex::Autolock _l(mDecryptLock);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900430 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshid074e302010-07-29 10:12:27 +0900431 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
432 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900433 result = drmEngine->closeDecryptSession(uniqueId, decryptHandle);
434 if (DRM_NO_ERROR == result) {
435 mDecryptSessionMap.removeItem(decryptHandle->decryptId);
436 }
aimitakeshid074e302010-07-29 10:12:27 +0900437 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900438 return result;
aimitakeshid074e302010-07-29 10:12:27 +0900439}
440
Takeshi Aimidc549d62010-09-20 23:40:41 +0900441status_t DrmManager::initializeDecryptUnit(
aimitakeshid074e302010-07-29 10:12:27 +0900442 int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId, const DrmBuffer* headerInfo) {
Takeshi Aimidc549d62010-09-20 23:40:41 +0900443 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshid074e302010-07-29 10:12:27 +0900444 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
445 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900446 result = drmEngine->initializeDecryptUnit(uniqueId, decryptHandle, decryptUnitId, headerInfo);
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::decrypt(int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId,
452 const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
453 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->decrypt(
457 uniqueId, decryptHandle, decryptUnitId, encBuffer, decBuffer, IV);
aimitakeshid074e302010-07-29 10:12:27 +0900458 }
Takeshi Aimidc549d62010-09-20 23:40:41 +0900459 return result;
aimitakeshid074e302010-07-29 10:12:27 +0900460}
461
Takeshi Aimidc549d62010-09-20 23:40:41 +0900462status_t DrmManager::finalizeDecryptUnit(
aimitakeshid074e302010-07-29 10:12:27 +0900463 int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) {
Takeshi Aimidc549d62010-09-20 23:40:41 +0900464 status_t result = DRM_ERROR_UNKNOWN;
aimitakeshid074e302010-07-29 10:12:27 +0900465 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
466 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
Takeshi Aimidc549d62010-09-20 23:40:41 +0900467 result = drmEngine->finalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId);
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
472ssize_t DrmManager::pread(int uniqueId, DecryptHandle* decryptHandle,
473 void* buffer, ssize_t numBytes, off_t offset) {
474 ssize_t result = DECRYPT_FILE_ERROR;
475
476 if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) {
477 IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId);
478 result = drmEngine->pread(uniqueId, decryptHandle, buffer, numBytes, offset);
479 }
480 return result;
481}
482
aimitakeshid074e302010-07-29 10:12:27 +0900483String8 DrmManager::getSupportedPlugInId(
484 int uniqueId, const String8& path, const String8& mimeType) {
485 String8 plugInId("");
486
487 if (EMPTY_STRING != mimeType) {
488 plugInId = getSupportedPlugInId(mimeType);
489 } else {
490 plugInId = getSupportedPlugInIdFromPath(uniqueId, path);
491 }
492 return plugInId;
493}
494
495String8 DrmManager::getSupportedPlugInId(const String8& mimeType) {
496 String8 plugInId("");
497
498 if (EMPTY_STRING != mimeType) {
499 for (unsigned int index = 0; index < mSupportInfoToPlugInIdMap.size(); index++) {
500 const DrmSupportInfo& drmSupportInfo = mSupportInfoToPlugInIdMap.keyAt(index);
501
502 if (drmSupportInfo.isSupportedMimeType(mimeType)) {
503 plugInId = mSupportInfoToPlugInIdMap.valueFor(drmSupportInfo);
504 break;
505 }
506 }
507 }
508 return plugInId;
509}
510
511String8 DrmManager::getSupportedPlugInIdFromPath(int uniqueId, const String8& path) {
512 String8 plugInId("");
513 const String8 fileSuffix = path.getPathExtension();
514
515 for (unsigned int index = 0; index < mSupportInfoToPlugInIdMap.size(); index++) {
516 const DrmSupportInfo& drmSupportInfo = mSupportInfoToPlugInIdMap.keyAt(index);
517
518 if (drmSupportInfo.isSupportedFileSuffix(fileSuffix)) {
519 String8 key = mSupportInfoToPlugInIdMap.valueFor(drmSupportInfo);
520 IDrmEngine& drmEngine = mPlugInManager.getPlugIn(key);
521
522 if (drmEngine.canHandle(uniqueId, path)) {
523 plugInId = key;
524 break;
525 }
526 }
527 }
528 return plugInId;
529}
530
531void DrmManager::onInfo(const DrmInfoEvent& event) {
532 Mutex::Autolock _l(mLock);
533 for (unsigned int index = 0; index < mServiceListeners.size(); index++) {
534 int uniqueId = mServiceListeners.keyAt(index);
535
536 if (uniqueId == event.getUniqueId()) {
537 sp<IDrmServiceListener> serviceListener = mServiceListeners.valueFor(uniqueId);
538 serviceListener->notify(event);
539 }
540 }
541}
542