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 "DrmManagerClientImpl(Native)"
19 #include <utils/Log.h>
20
21 #include <utils/String8.h>
22 #include <utils/Vector.h>
23 #include <binder/IServiceManager.h>
24 #include <cutils/properties.h>
25
26 #include "DrmManagerClientImpl.h"
27 #include "NoOpDrmManagerClientImpl.h"
28
29 using namespace android;
30
31 #define INVALID_VALUE (-1)
32
33 Mutex DrmManagerClientImpl::sMutex;
34 sp<IDrmManagerService> DrmManagerClientImpl::sDrmManagerService;
35 sp<DrmManagerClientImpl::DeathNotifier> DrmManagerClientImpl::sDeathNotifier;
36 const String8 DrmManagerClientImpl::EMPTY_STRING("");
37
create(int * pUniqueId,bool isNative)38 DrmManagerClientImpl* DrmManagerClientImpl::create(
39 int* pUniqueId, bool isNative) {
40 sp<IDrmManagerService> service = getDrmManagerService();
41 if (service != NULL) {
42 *pUniqueId = getDrmManagerService()->addUniqueId(isNative);
43 return new DrmManagerClientImpl();
44 }
45 return new NoOpDrmManagerClientImpl();
46 }
47
remove(int uniqueId)48 void DrmManagerClientImpl::remove(int uniqueId) {
49 getDrmManagerService()->removeUniqueId(uniqueId);
50 }
51
getDrmManagerService()52 const sp<IDrmManagerService>& DrmManagerClientImpl::getDrmManagerService() {
53 Mutex::Autolock lock(sMutex);
54 if (NULL == sDrmManagerService.get()) {
55 sp<IServiceManager> sm = defaultServiceManager();
56 sp<IBinder> binder = sm->checkService(String16("drm.drmManager"));
57 if (binder == NULL) {
58 return sDrmManagerService;
59 }
60 if (NULL == sDeathNotifier.get()) {
61 sDeathNotifier = new DeathNotifier();
62 }
63 binder->linkToDeath(sDeathNotifier);
64 sDrmManagerService = interface_cast<IDrmManagerService>(binder);
65 }
66 return sDrmManagerService;
67 }
68
addClient(int uniqueId)69 void DrmManagerClientImpl::addClient(int uniqueId) {
70 getDrmManagerService()->addClient(uniqueId);
71 }
72
removeClient(int uniqueId)73 void DrmManagerClientImpl::removeClient(int uniqueId) {
74 getDrmManagerService()->removeClient(uniqueId);
75 }
76
setOnInfoListener(int uniqueId,const sp<DrmManagerClient::OnInfoListener> & infoListener)77 status_t DrmManagerClientImpl::setOnInfoListener(
78 int uniqueId,
79 const sp<DrmManagerClient::OnInfoListener>& infoListener) {
80 Mutex::Autolock _l(mLock);
81 mOnInfoListener = infoListener;
82 return getDrmManagerService()->setDrmServiceListener(uniqueId,
83 (NULL != infoListener.get()) ? this : NULL);
84 }
85
getConstraints(int uniqueId,const String8 * path,const int action)86 DrmConstraints* DrmManagerClientImpl::getConstraints(
87 int uniqueId, const String8* path, const int action) {
88 DrmConstraints *drmConstraints = NULL;
89 if ((NULL != path) && (EMPTY_STRING != *path)) {
90 drmConstraints =
91 getDrmManagerService()->getConstraints(uniqueId, path, action);
92 }
93 return drmConstraints;
94 }
95
getMetadata(int uniqueId,const String8 * path)96 DrmMetadata* DrmManagerClientImpl::getMetadata(int uniqueId, const String8* path) {
97 DrmMetadata *drmMetadata = NULL;
98 if ((NULL != path) && (EMPTY_STRING != *path)) {
99 drmMetadata = getDrmManagerService()->getMetadata(uniqueId, path);
100 }
101 return drmMetadata;
102 }
103
canHandle(int uniqueId,const String8 & path,const String8 & mimeType)104 bool DrmManagerClientImpl::canHandle(
105 int uniqueId, const String8& path, const String8& mimeType) {
106 bool retCode = false;
107 if ((EMPTY_STRING != path) || (EMPTY_STRING != mimeType)) {
108 retCode = getDrmManagerService()->canHandle(uniqueId, path, mimeType);
109 }
110 return retCode;
111 }
112
processDrmInfo(int uniqueId,const DrmInfo * drmInfo)113 DrmInfoStatus* DrmManagerClientImpl::processDrmInfo(
114 int uniqueId, const DrmInfo* drmInfo) {
115 DrmInfoStatus *drmInfoStatus = NULL;
116 if (NULL != drmInfo) {
117 drmInfoStatus = getDrmManagerService()->processDrmInfo(uniqueId, drmInfo);
118 }
119 return drmInfoStatus;
120 }
121
acquireDrmInfo(int uniqueId,const DrmInfoRequest * drmInfoRequest)122 DrmInfo* DrmManagerClientImpl::acquireDrmInfo(
123 int uniqueId, const DrmInfoRequest* drmInfoRequest) {
124 DrmInfo* drmInfo = NULL;
125 if (NULL != drmInfoRequest) {
126 drmInfo = getDrmManagerService()->acquireDrmInfo(uniqueId, drmInfoRequest);
127 }
128 return drmInfo;
129 }
130
saveRights(int uniqueId,const DrmRights & drmRights,const String8 & rightsPath,const String8 & contentPath)131 status_t DrmManagerClientImpl::saveRights(int uniqueId, const DrmRights& drmRights,
132 const String8& rightsPath, const String8& contentPath) {
133 return getDrmManagerService()->saveRights(
134 uniqueId, drmRights, rightsPath, contentPath);
135 }
136
getOriginalMimeType(int uniqueId,const String8 & path,int fd)137 String8 DrmManagerClientImpl::getOriginalMimeType(
138 int uniqueId, const String8& path, int fd) {
139 String8 mimeType = EMPTY_STRING;
140 if (EMPTY_STRING != path) {
141 mimeType = getDrmManagerService()->getOriginalMimeType(uniqueId, path, fd);
142 }
143 return mimeType;
144 }
145
getDrmObjectType(int uniqueId,const String8 & path,const String8 & mimeType)146 int DrmManagerClientImpl::getDrmObjectType(
147 int uniqueId, const String8& path, const String8& mimeType) {
148 int drmOjectType = DrmObjectType::UNKNOWN;
149 if ((EMPTY_STRING != path) || (EMPTY_STRING != mimeType)) {
150 drmOjectType =
151 getDrmManagerService()->getDrmObjectType(uniqueId, path, mimeType);
152 }
153 return drmOjectType;
154 }
155
checkRightsStatus(int uniqueId,const String8 & path,int action)156 int DrmManagerClientImpl::checkRightsStatus(
157 int uniqueId, const String8& path, int action) {
158 int rightsStatus = RightsStatus::RIGHTS_INVALID;
159 if (EMPTY_STRING != path) {
160 rightsStatus =
161 getDrmManagerService()->checkRightsStatus(uniqueId, path, action);
162 }
163 return rightsStatus;
164 }
165
consumeRights(int uniqueId,sp<DecryptHandle> & decryptHandle,int action,bool reserve)166 status_t DrmManagerClientImpl::consumeRights(
167 int uniqueId, sp<DecryptHandle> &decryptHandle,
168 int action, bool reserve) {
169 status_t status = DRM_ERROR_UNKNOWN;
170 if (NULL != decryptHandle.get()) {
171 status = getDrmManagerService()->consumeRights(
172 uniqueId, decryptHandle, action, reserve);
173 }
174 return status;
175 }
176
setPlaybackStatus(int uniqueId,sp<DecryptHandle> & decryptHandle,int playbackStatus,int64_t position)177 status_t DrmManagerClientImpl::setPlaybackStatus(
178 int uniqueId, sp<DecryptHandle> &decryptHandle,
179 int playbackStatus, int64_t position) {
180 status_t status = DRM_ERROR_UNKNOWN;
181 if (NULL != decryptHandle.get()) {
182 status = getDrmManagerService()->setPlaybackStatus(
183 uniqueId, decryptHandle, playbackStatus, position);
184 }
185 return status;
186 }
187
validateAction(int uniqueId,const String8 & path,int action,const ActionDescription & description)188 bool DrmManagerClientImpl::validateAction(
189 int uniqueId, const String8& path,
190 int action, const ActionDescription& description) {
191 bool retCode = false;
192 if (EMPTY_STRING != path) {
193 retCode = getDrmManagerService()->validateAction(
194 uniqueId, path, action, description);
195 }
196 return retCode;
197 }
198
removeRights(int uniqueId,const String8 & path)199 status_t DrmManagerClientImpl::removeRights(int uniqueId, const String8& path) {
200 status_t status = DRM_ERROR_UNKNOWN;
201 if (EMPTY_STRING != path) {
202 status = getDrmManagerService()->removeRights(uniqueId, path);
203 }
204 return status;
205 }
206
removeAllRights(int uniqueId)207 status_t DrmManagerClientImpl::removeAllRights(int uniqueId) {
208 return getDrmManagerService()->removeAllRights(uniqueId);
209 }
210
openConvertSession(int uniqueId,const String8 & mimeType)211 int DrmManagerClientImpl::openConvertSession(
212 int uniqueId, const String8& mimeType) {
213 int retCode = INVALID_VALUE;
214 if (EMPTY_STRING != mimeType) {
215 retCode = getDrmManagerService()->openConvertSession(uniqueId, mimeType);
216 }
217 return retCode;
218 }
219
convertData(int uniqueId,int convertId,const DrmBuffer * inputData)220 DrmConvertedStatus* DrmManagerClientImpl::convertData(
221 int uniqueId, int convertId, const DrmBuffer* inputData) {
222 DrmConvertedStatus* drmConvertedStatus = NULL;
223 if (NULL != inputData) {
224 drmConvertedStatus =
225 getDrmManagerService()->convertData(uniqueId, convertId, inputData);
226 }
227 return drmConvertedStatus;
228 }
229
closeConvertSession(int uniqueId,int convertId)230 DrmConvertedStatus* DrmManagerClientImpl::closeConvertSession(
231 int uniqueId, int convertId) {
232 return getDrmManagerService()->closeConvertSession(uniqueId, convertId);
233 }
234
getAllSupportInfo(int uniqueId,int * length,DrmSupportInfo ** drmSupportInfoArray)235 status_t DrmManagerClientImpl::getAllSupportInfo(
236 int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
237 status_t status = DRM_ERROR_UNKNOWN;
238 if ((NULL != drmSupportInfoArray) && (NULL != length)) {
239 status = getDrmManagerService()->getAllSupportInfo(
240 uniqueId, length, drmSupportInfoArray);
241 }
242 return status;
243 }
244
openDecryptSession(int uniqueId,int fd,off64_t offset,off64_t length,const char * mime)245 sp<DecryptHandle> DrmManagerClientImpl::openDecryptSession(
246 int uniqueId, int fd, off64_t offset,
247 off64_t length, const char* mime) {
248
249 return getDrmManagerService()->openDecryptSession(
250 uniqueId, fd, offset, length, mime);
251 }
252
openDecryptSession(int uniqueId,const char * uri,const char * mime)253 sp<DecryptHandle> DrmManagerClientImpl::openDecryptSession(
254 int uniqueId, const char* uri, const char* mime) {
255
256 sp<DecryptHandle> handle;
257 if (NULL != uri) {
258 handle = getDrmManagerService()->openDecryptSession(uniqueId, uri, mime);
259 }
260 return handle;
261 }
262
openDecryptSession(int uniqueId,const DrmBuffer & buf,const String8 & mimeType)263 sp<DecryptHandle> DrmManagerClientImpl::openDecryptSession(
264 int uniqueId, const DrmBuffer& buf, const String8& mimeType) {
265 return getDrmManagerService()->openDecryptSession(uniqueId, buf, mimeType);
266 }
267
closeDecryptSession(int uniqueId,sp<DecryptHandle> & decryptHandle)268 status_t DrmManagerClientImpl::closeDecryptSession(
269 int uniqueId, sp<DecryptHandle> &decryptHandle) {
270 status_t status = DRM_ERROR_UNKNOWN;
271 if (NULL != decryptHandle.get()) {
272 status = getDrmManagerService()->closeDecryptSession(
273 uniqueId, decryptHandle);
274 }
275 return status;
276 }
277
initializeDecryptUnit(int uniqueId,sp<DecryptHandle> & decryptHandle,int decryptUnitId,const DrmBuffer * headerInfo)278 status_t DrmManagerClientImpl::initializeDecryptUnit(
279 int uniqueId, sp<DecryptHandle> &decryptHandle,
280 int decryptUnitId, const DrmBuffer* headerInfo) {
281 status_t status = DRM_ERROR_UNKNOWN;
282 if ((NULL != decryptHandle.get()) && (NULL != headerInfo)) {
283 status = getDrmManagerService()->initializeDecryptUnit(
284 uniqueId, decryptHandle, decryptUnitId, headerInfo);
285 }
286 return status;
287 }
288
decrypt(int uniqueId,sp<DecryptHandle> & decryptHandle,int decryptUnitId,const DrmBuffer * encBuffer,DrmBuffer ** decBuffer,DrmBuffer * IV)289 status_t DrmManagerClientImpl::decrypt(
290 int uniqueId, sp<DecryptHandle> &decryptHandle,
291 int decryptUnitId, const DrmBuffer* encBuffer,
292 DrmBuffer** decBuffer, DrmBuffer* IV) {
293 status_t status = DRM_ERROR_UNKNOWN;
294 if ((NULL != decryptHandle.get()) && (NULL != encBuffer)
295 && (NULL != decBuffer) && (NULL != *decBuffer)) {
296 status = getDrmManagerService()->decrypt(
297 uniqueId, decryptHandle, decryptUnitId,
298 encBuffer, decBuffer, IV);
299 }
300 return status;
301 }
302
finalizeDecryptUnit(int uniqueId,sp<DecryptHandle> & decryptHandle,int decryptUnitId)303 status_t DrmManagerClientImpl::finalizeDecryptUnit(
304 int uniqueId, sp<DecryptHandle> &decryptHandle, int decryptUnitId) {
305 status_t status = DRM_ERROR_UNKNOWN;
306 if (NULL != decryptHandle.get()) {
307 status = getDrmManagerService()->finalizeDecryptUnit(
308 uniqueId, decryptHandle, decryptUnitId);
309 }
310 return status;
311 }
312
pread(int uniqueId,sp<DecryptHandle> & decryptHandle,void * buffer,ssize_t numBytes,off64_t offset)313 ssize_t DrmManagerClientImpl::pread(int uniqueId, sp<DecryptHandle> &decryptHandle,
314 void* buffer, ssize_t numBytes, off64_t offset) {
315 ssize_t retCode = INVALID_VALUE;
316 if ((NULL != decryptHandle.get()) && (NULL != buffer) && (0 < numBytes)) {
317 retCode = getDrmManagerService()->pread(
318 uniqueId, decryptHandle, buffer, numBytes, offset);
319 }
320 return retCode;
321 }
322
notify(const DrmInfoEvent & event)323 status_t DrmManagerClientImpl::notify(const DrmInfoEvent& event) {
324 if (NULL != mOnInfoListener.get()) {
325 Mutex::Autolock _l(mLock);
326 sp<DrmManagerClient::OnInfoListener> listener = mOnInfoListener;
327 listener->onInfo(event);
328 }
329 return DRM_NO_ERROR;
330 }
331
~DeathNotifier()332 DrmManagerClientImpl::DeathNotifier::~DeathNotifier() {
333 Mutex::Autolock lock(sMutex);
334 if (NULL != sDrmManagerService.get()) {
335 IInterface::asBinder(sDrmManagerService)->unlinkToDeath(this);
336 }
337 }
338
binderDied(const wp<IBinder> &)339 void DrmManagerClientImpl::DeathNotifier::binderDied(
340 const wp<IBinder>& /* who */) {
341 Mutex::Autolock lock(sMutex);
342 DrmManagerClientImpl::sDrmManagerService.clear();
343 ALOGW("DrmManager server died!");
344 }
345
346