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