aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 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 | |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 18 | #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 | |
| 37 | using namespace android; |
| 38 | |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 39 | Vector<int> DrmManager::mUniqueIdVector; |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 40 | const String8 DrmManager::EMPTY_STRING(""); |
| 41 | |
| 42 | DrmManager::DrmManager() : |
| 43 | mDecryptSessionId(0), |
| 44 | mConvertId(0) { |
| 45 | |
| 46 | } |
| 47 | |
| 48 | DrmManager::~DrmManager() { |
| 49 | |
| 50 | } |
| 51 | |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 52 | int 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 | |
| 79 | void 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 | |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 88 | status_t DrmManager::loadPlugIns(int uniqueId) { |
| 89 | String8 pluginDirPath("/system/lib/drm/plugins/native"); |
| 90 | return loadPlugIns(uniqueId, pluginDirPath); |
| 91 | } |
| 92 | |
| 93 | status_t DrmManager::loadPlugIns(int uniqueId, const String8& plugInDirPath) { |
| 94 | if (mSupportInfoToPlugInIdMap.isEmpty()) { |
| 95 | mPlugInManager.loadPlugIns(plugInDirPath); |
| 96 | |
| 97 | initializePlugIns(uniqueId); |
| 98 | |
| 99 | populate(uniqueId); |
| 100 | } else { |
| 101 | initializePlugIns(uniqueId); |
| 102 | } |
| 103 | |
| 104 | return DRM_NO_ERROR; |
| 105 | } |
| 106 | |
| 107 | status_t DrmManager::setDrmServiceListener( |
| 108 | int uniqueId, const sp<IDrmServiceListener>& drmServiceListener) { |
| 109 | Mutex::Autolock _l(mLock); |
| 110 | mServiceListeners.add(uniqueId, drmServiceListener); |
| 111 | return DRM_NO_ERROR; |
| 112 | } |
| 113 | |
| 114 | status_t DrmManager::unloadPlugIns(int uniqueId) { |
| 115 | Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList(); |
| 116 | |
| 117 | for (unsigned int index = 0; index < plugInIdList.size(); index++) { |
| 118 | IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index)); |
| 119 | rDrmEngine.terminate(uniqueId); |
| 120 | } |
| 121 | |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 122 | if (0 >= mUniqueIdVector.size()) { |
| 123 | mConvertSessionMap.clear(); |
| 124 | mDecryptSessionMap.clear(); |
| 125 | mSupportInfoToPlugInIdMap.clear(); |
| 126 | mPlugInManager.unloadPlugIns(); |
| 127 | } |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 128 | return DRM_NO_ERROR; |
| 129 | } |
| 130 | |
| 131 | DrmConstraints* DrmManager::getConstraints(int uniqueId, const String8* path, const int action) { |
| 132 | const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, *path); |
| 133 | if (EMPTY_STRING != plugInId) { |
| 134 | IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId); |
| 135 | return rDrmEngine.getConstraints(uniqueId, path, action); |
| 136 | } |
| 137 | return NULL; |
| 138 | } |
| 139 | |
| 140 | status_t DrmManager::installDrmEngine(int uniqueId, const String8& absolutePath) { |
| 141 | mPlugInManager.loadPlugIn(absolutePath); |
| 142 | |
| 143 | IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(absolutePath); |
| 144 | rDrmEngine.initialize(uniqueId); |
| 145 | rDrmEngine.setOnInfoListener(uniqueId, this); |
| 146 | |
| 147 | DrmSupportInfo* info = rDrmEngine.getSupportInfo(uniqueId); |
| 148 | mSupportInfoToPlugInIdMap.add(*info, absolutePath); |
| 149 | |
| 150 | return DRM_NO_ERROR; |
| 151 | } |
| 152 | |
| 153 | bool DrmManager::canHandle(int uniqueId, const String8& path, const String8& mimeType) { |
| 154 | const String8 plugInId = getSupportedPlugInId(mimeType); |
| 155 | bool result = (EMPTY_STRING != plugInId) ? true : false; |
| 156 | |
| 157 | if (NULL != path) { |
| 158 | if (result) { |
| 159 | IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId); |
| 160 | result = rDrmEngine.canHandle(uniqueId, path); |
| 161 | } else { |
| 162 | result = canHandle(uniqueId, path); |
| 163 | } |
| 164 | } |
| 165 | return result; |
| 166 | } |
| 167 | |
| 168 | DrmInfoStatus* DrmManager::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) { |
| 169 | const String8 plugInId = getSupportedPlugInId(drmInfo->getMimeType()); |
| 170 | if (EMPTY_STRING != plugInId) { |
| 171 | IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId); |
| 172 | return rDrmEngine.processDrmInfo(uniqueId, drmInfo); |
| 173 | } |
| 174 | return NULL; |
| 175 | } |
| 176 | |
| 177 | bool DrmManager::canHandle(int uniqueId, const String8& path) { |
| 178 | bool result = false; |
| 179 | Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList(); |
| 180 | |
| 181 | for (unsigned int i = 0; i < plugInPathList.size(); ++i) { |
| 182 | IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInPathList[i]); |
| 183 | result = rDrmEngine.canHandle(uniqueId, path); |
| 184 | |
| 185 | if (result) { |
| 186 | break; |
| 187 | } |
| 188 | } |
| 189 | return result; |
| 190 | } |
| 191 | |
| 192 | DrmInfo* DrmManager::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) { |
| 193 | const String8 plugInId = getSupportedPlugInId(drmInfoRequest->getMimeType()); |
| 194 | if (EMPTY_STRING != plugInId) { |
| 195 | IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId); |
| 196 | return rDrmEngine.acquireDrmInfo(uniqueId, drmInfoRequest); |
| 197 | } |
| 198 | return NULL; |
| 199 | } |
| 200 | |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 201 | status_t DrmManager::saveRights(int uniqueId, const DrmRights& drmRights, |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 202 | const String8& rightsPath, const String8& contentPath) { |
| 203 | const String8 plugInId = getSupportedPlugInId(drmRights.getMimeType()); |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 204 | status_t result = DRM_ERROR_UNKNOWN; |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 205 | if (EMPTY_STRING != plugInId) { |
| 206 | IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId); |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 207 | result = rDrmEngine.saveRights(uniqueId, drmRights, rightsPath, contentPath); |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 208 | } |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 209 | return result; |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | String8 DrmManager::getOriginalMimeType(int uniqueId, const String8& path) { |
| 213 | const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path); |
| 214 | if (EMPTY_STRING != plugInId) { |
| 215 | IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId); |
| 216 | return rDrmEngine.getOriginalMimeType(uniqueId, path); |
| 217 | } |
| 218 | return EMPTY_STRING; |
| 219 | } |
| 220 | |
| 221 | int DrmManager::getDrmObjectType(int uniqueId, const String8& path, const String8& mimeType) { |
| 222 | const String8 plugInId = getSupportedPlugInId(uniqueId, path, mimeType); |
| 223 | if (EMPTY_STRING != plugInId) { |
| 224 | IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId); |
| 225 | return rDrmEngine.getDrmObjectType(uniqueId, path, mimeType); |
| 226 | } |
| 227 | return DrmObjectType::UNKNOWN; |
| 228 | } |
| 229 | |
| 230 | int DrmManager::checkRightsStatus(int uniqueId, const String8& path, int action) { |
| 231 | const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path); |
| 232 | if (EMPTY_STRING != plugInId) { |
| 233 | IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId); |
| 234 | return rDrmEngine.checkRightsStatus(uniqueId, path, action); |
| 235 | } |
| 236 | return RightsStatus::RIGHTS_INVALID; |
| 237 | } |
| 238 | |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 239 | status_t DrmManager::consumeRights( |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 240 | int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) { |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 241 | status_t result = DRM_ERROR_UNKNOWN; |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 242 | if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) { |
| 243 | IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId); |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 244 | result = drmEngine->consumeRights(uniqueId, decryptHandle, action, reserve); |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 245 | } |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 246 | return result; |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 247 | } |
| 248 | |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 249 | status_t DrmManager::setPlaybackStatus( |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 250 | int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int position) { |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 251 | status_t result = DRM_ERROR_UNKNOWN; |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 252 | if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) { |
| 253 | IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId); |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 254 | result = drmEngine->setPlaybackStatus(uniqueId, decryptHandle, playbackStatus, position); |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 255 | } |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 256 | return result; |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | bool DrmManager::validateAction( |
| 260 | int uniqueId, const String8& path, int action, const ActionDescription& description) { |
| 261 | const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path); |
| 262 | if (EMPTY_STRING != plugInId) { |
| 263 | IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId); |
| 264 | return rDrmEngine.validateAction(uniqueId, path, action, description); |
| 265 | } |
| 266 | return false; |
| 267 | } |
| 268 | |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 269 | status_t DrmManager::removeRights(int uniqueId, const String8& path) { |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 270 | const String8 plugInId = getSupportedPlugInIdFromPath(uniqueId, path); |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 271 | status_t result = DRM_ERROR_UNKNOWN; |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 272 | if (EMPTY_STRING != plugInId) { |
| 273 | IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId); |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 274 | result = rDrmEngine.removeRights(uniqueId, path); |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 275 | } |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 276 | return result; |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 277 | } |
| 278 | |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 279 | status_t DrmManager::removeAllRights(int uniqueId) { |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 280 | Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList(); |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 281 | status_t result = DRM_ERROR_UNKNOWN; |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 282 | for (unsigned int index = 0; index < plugInIdList.size(); index++) { |
| 283 | IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index)); |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 284 | result = rDrmEngine.removeAllRights(uniqueId); |
| 285 | if (DRM_NO_ERROR != result) { |
| 286 | break; |
| 287 | } |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 288 | } |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 289 | return result; |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | int DrmManager::openConvertSession(int uniqueId, const String8& mimeType) { |
| 293 | int convertId = -1; |
| 294 | |
| 295 | const String8 plugInId = getSupportedPlugInId(mimeType); |
| 296 | if (EMPTY_STRING != plugInId) { |
| 297 | IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId); |
| 298 | |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 299 | if (DRM_NO_ERROR == rDrmEngine.openConvertSession(uniqueId, mConvertId + 1)) { |
| 300 | Mutex::Autolock _l(mConvertLock); |
| 301 | ++mConvertId; |
| 302 | convertId = mConvertId; |
| 303 | mConvertSessionMap.add(convertId, &rDrmEngine); |
| 304 | } |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 305 | } |
| 306 | return convertId; |
| 307 | } |
| 308 | |
| 309 | DrmConvertedStatus* DrmManager::convertData( |
| 310 | int uniqueId, int convertId, const DrmBuffer* inputData) { |
| 311 | DrmConvertedStatus *drmConvertedStatus = NULL; |
| 312 | |
| 313 | if (mConvertSessionMap.indexOfKey(convertId) != NAME_NOT_FOUND) { |
| 314 | IDrmEngine* drmEngine = mConvertSessionMap.valueFor(convertId); |
| 315 | drmConvertedStatus = drmEngine->convertData(uniqueId, convertId, inputData); |
| 316 | } |
| 317 | return drmConvertedStatus; |
| 318 | } |
| 319 | |
| 320 | DrmConvertedStatus* DrmManager::closeConvertSession(int uniqueId, int convertId) { |
| 321 | DrmConvertedStatus *drmConvertedStatus = NULL; |
| 322 | |
| 323 | if (mConvertSessionMap.indexOfKey(convertId) != NAME_NOT_FOUND) { |
| 324 | IDrmEngine* drmEngine = mConvertSessionMap.valueFor(convertId); |
| 325 | drmConvertedStatus = drmEngine->closeConvertSession(uniqueId, convertId); |
| 326 | mConvertSessionMap.removeItem(convertId); |
| 327 | } |
| 328 | return drmConvertedStatus; |
| 329 | } |
| 330 | |
| 331 | status_t DrmManager::getAllSupportInfo( |
| 332 | int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) { |
| 333 | Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList(); |
| 334 | int size = plugInPathList.size(); |
| 335 | int validPlugins = 0; |
| 336 | |
| 337 | if (0 < size) { |
| 338 | Vector<DrmSupportInfo> drmSupportInfoList; |
| 339 | |
| 340 | for (int i = 0; i < size; ++i) { |
| 341 | String8 plugInPath = plugInPathList[i]; |
| 342 | DrmSupportInfo* drmSupportInfo |
| 343 | = mPlugInManager.getPlugIn(plugInPath).getSupportInfo(uniqueId); |
| 344 | if (NULL != drmSupportInfo) { |
| 345 | drmSupportInfoList.add(*drmSupportInfo); |
| 346 | delete drmSupportInfo; drmSupportInfo = NULL; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | validPlugins = drmSupportInfoList.size(); |
| 351 | if (0 < validPlugins) { |
| 352 | *drmSupportInfoArray = new DrmSupportInfo[validPlugins]; |
| 353 | for (int i = 0; i < validPlugins; ++i) { |
| 354 | (*drmSupportInfoArray)[i] = drmSupportInfoList[i]; |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | *length = validPlugins; |
| 359 | return DRM_NO_ERROR; |
| 360 | } |
| 361 | |
| 362 | DecryptHandle* DrmManager::openDecryptSession(int uniqueId, int fd, int offset, int length) { |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 363 | status_t result = DRM_ERROR_CANNOT_HANDLE; |
| 364 | Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList(); |
| 365 | |
| 366 | DecryptHandle* handle = new DecryptHandle(); |
| 367 | if (NULL != handle) { |
| 368 | Mutex::Autolock _l(mDecryptLock); |
| 369 | handle->decryptId = mDecryptSessionId + 1; |
| 370 | |
| 371 | for (unsigned int index = 0; index < plugInIdList.size(); index++) { |
| 372 | String8 plugInId = plugInIdList.itemAt(index); |
| 373 | IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInId); |
| 374 | result = rDrmEngine.openDecryptSession(uniqueId, handle, fd, offset, length); |
| 375 | |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 376 | if (DRM_NO_ERROR == result) { |
| 377 | ++mDecryptSessionId; |
| 378 | mDecryptSessionMap.add(mDecryptSessionId, &rDrmEngine); |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 379 | break; |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 384 | if (DRM_NO_ERROR != result) { |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 385 | delete handle; handle = NULL; |
| 386 | LOGE("DrmManager::openDecryptSession: no capable plug-in found"); |
| 387 | } |
| 388 | |
| 389 | return handle; |
| 390 | } |
| 391 | |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 392 | status_t DrmManager::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) { |
| 393 | status_t result = DRM_ERROR_UNKNOWN; |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 394 | if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) { |
| 395 | IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId); |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 396 | result = drmEngine->closeDecryptSession(uniqueId, decryptHandle); |
| 397 | if (DRM_NO_ERROR == result) { |
| 398 | mDecryptSessionMap.removeItem(decryptHandle->decryptId); |
| 399 | } |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 400 | } |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 401 | return result; |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 402 | } |
| 403 | |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 404 | status_t DrmManager::initializeDecryptUnit( |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 405 | int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId, const DrmBuffer* headerInfo) { |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 406 | status_t result = DRM_ERROR_UNKNOWN; |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 407 | if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) { |
| 408 | IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId); |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 409 | result = drmEngine->initializeDecryptUnit(uniqueId, decryptHandle, decryptUnitId, headerInfo); |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 410 | } |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 411 | return result; |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 412 | } |
| 413 | |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 414 | status_t DrmManager::decrypt(int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId, |
| 415 | const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) { |
| 416 | status_t result = DRM_ERROR_UNKNOWN; |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 417 | if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) { |
| 418 | IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId); |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 419 | result = drmEngine->decrypt( |
| 420 | uniqueId, decryptHandle, decryptUnitId, encBuffer, decBuffer, IV); |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 421 | } |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 422 | return result; |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 423 | } |
| 424 | |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 425 | status_t DrmManager::finalizeDecryptUnit( |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 426 | int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) { |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 427 | status_t result = DRM_ERROR_UNKNOWN; |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 428 | if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) { |
| 429 | IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId); |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 430 | result = drmEngine->finalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId); |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 431 | } |
Takeshi Aimi | dc549d6 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 432 | return result; |
aimitakeshi | d074e30 | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | ssize_t DrmManager::pread(int uniqueId, DecryptHandle* decryptHandle, |
| 436 | void* buffer, ssize_t numBytes, off_t offset) { |
| 437 | ssize_t result = DECRYPT_FILE_ERROR; |
| 438 | |
| 439 | if (mDecryptSessionMap.indexOfKey(decryptHandle->decryptId) != NAME_NOT_FOUND) { |
| 440 | IDrmEngine* drmEngine = mDecryptSessionMap.valueFor(decryptHandle->decryptId); |
| 441 | result = drmEngine->pread(uniqueId, decryptHandle, buffer, numBytes, offset); |
| 442 | } |
| 443 | return result; |
| 444 | } |
| 445 | |
| 446 | void DrmManager::initializePlugIns(int uniqueId) { |
| 447 | Vector<String8> plugInIdList = mPlugInManager.getPlugInIdList(); |
| 448 | |
| 449 | for (unsigned int index = 0; index < plugInIdList.size(); index++) { |
| 450 | IDrmEngine& rDrmEngine = mPlugInManager.getPlugIn(plugInIdList.itemAt(index)); |
| 451 | rDrmEngine.initialize(uniqueId); |
| 452 | rDrmEngine.setOnInfoListener(uniqueId, this); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | void DrmManager::populate(int uniqueId) { |
| 457 | Vector<String8> plugInPathList = mPlugInManager.getPlugInIdList(); |
| 458 | |
| 459 | for (unsigned int i = 0; i < plugInPathList.size(); ++i) { |
| 460 | String8 plugInPath = plugInPathList[i]; |
| 461 | DrmSupportInfo* info = mPlugInManager.getPlugIn(plugInPath).getSupportInfo(uniqueId); |
| 462 | if (NULL != info) { |
| 463 | mSupportInfoToPlugInIdMap.add(*info, plugInPath); |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | String8 DrmManager::getSupportedPlugInId( |
| 469 | int uniqueId, const String8& path, const String8& mimeType) { |
| 470 | String8 plugInId(""); |
| 471 | |
| 472 | if (EMPTY_STRING != mimeType) { |
| 473 | plugInId = getSupportedPlugInId(mimeType); |
| 474 | } else { |
| 475 | plugInId = getSupportedPlugInIdFromPath(uniqueId, path); |
| 476 | } |
| 477 | return plugInId; |
| 478 | } |
| 479 | |
| 480 | String8 DrmManager::getSupportedPlugInId(const String8& mimeType) { |
| 481 | String8 plugInId(""); |
| 482 | |
| 483 | if (EMPTY_STRING != mimeType) { |
| 484 | for (unsigned int index = 0; index < mSupportInfoToPlugInIdMap.size(); index++) { |
| 485 | const DrmSupportInfo& drmSupportInfo = mSupportInfoToPlugInIdMap.keyAt(index); |
| 486 | |
| 487 | if (drmSupportInfo.isSupportedMimeType(mimeType)) { |
| 488 | plugInId = mSupportInfoToPlugInIdMap.valueFor(drmSupportInfo); |
| 489 | break; |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | return plugInId; |
| 494 | } |
| 495 | |
| 496 | String8 DrmManager::getSupportedPlugInIdFromPath(int uniqueId, const String8& path) { |
| 497 | String8 plugInId(""); |
| 498 | const String8 fileSuffix = path.getPathExtension(); |
| 499 | |
| 500 | for (unsigned int index = 0; index < mSupportInfoToPlugInIdMap.size(); index++) { |
| 501 | const DrmSupportInfo& drmSupportInfo = mSupportInfoToPlugInIdMap.keyAt(index); |
| 502 | |
| 503 | if (drmSupportInfo.isSupportedFileSuffix(fileSuffix)) { |
| 504 | String8 key = mSupportInfoToPlugInIdMap.valueFor(drmSupportInfo); |
| 505 | IDrmEngine& drmEngine = mPlugInManager.getPlugIn(key); |
| 506 | |
| 507 | if (drmEngine.canHandle(uniqueId, path)) { |
| 508 | plugInId = key; |
| 509 | break; |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | return plugInId; |
| 514 | } |
| 515 | |
| 516 | void DrmManager::onInfo(const DrmInfoEvent& event) { |
| 517 | Mutex::Autolock _l(mLock); |
| 518 | for (unsigned int index = 0; index < mServiceListeners.size(); index++) { |
| 519 | int uniqueId = mServiceListeners.keyAt(index); |
| 520 | |
| 521 | if (uniqueId == event.getUniqueId()) { |
| 522 | sp<IDrmServiceListener> serviceListener = mServiceListeners.valueFor(uniqueId); |
| 523 | serviceListener->notify(event); |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | |