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