1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "drm_adapter_impl.h"
17
18 #include <atomic>
19 #include <clocale>
20 #include <cstddef>
21 #include <cstring.h>
22 #include <map>
23 #include <memory>
24 #include <sstream>
25 #include <typeinfo>
26 #include <unistd.h>
27
28 #include "native_mediakeysession.h"
29 #include "native_mediakeysystem.h"
30
31 namespace OHOS::NWeb {
32 std::unordered_map<MediaKeySystem*, std::shared_ptr<DrmCallbackImpl>> DrmAdapterImpl::mediaKeySystemCallbackMap_;
33 std::unordered_map<MediaKeySession*, std::shared_ptr<DrmCallbackImpl>> DrmAdapterImpl::mediaKeySessionCallbackMap_;
34 std::mutex DrmAdapterImpl::mediaKeySystemCallbackMapMutex_;
35 std::mutex DrmAdapterImpl::mediaKeySessionCallbackMutex_;
36
37 static std::unordered_map<std::string, uint32_t> KeyStatusMap {
38 { "USABLE", static_cast<uint32_t>(KeyStatus::KEY_STATUS_USABLE) },
39 { "EXPIRED", static_cast<uint32_t>(KeyStatus::KEY_STATUS_EXPIRED) },
40 { "OUTPUT_NOT_ALLOWED", static_cast<uint32_t>(KeyStatus::KEY_STATUS_OUTPUT_NOT_ALLOWED) },
41 { "PENDING", static_cast<uint32_t>(KeyStatus::KEY_STATUS_PENDING) },
42 { "INTERNAL_ERROR", static_cast<uint32_t>(KeyStatus::KEY_STATUS_INTERNAL_ERROR) },
43 { "USABLE_IN_FUTURE", static_cast<uint32_t>(KeyStatus::KEY_STATUS_USABLE_IN_FUTURE) },
44 };
45
GetContentProtectionLevelFromSecurityLevel(int32_t levelData)46 DRM_ContentProtectionLevel GetContentProtectionLevelFromSecurityLevel(int32_t levelData)
47 {
48 DRM_ContentProtectionLevel contentProtectionLevel = CONTENT_PROTECTION_LEVEL_UNKNOWN;
49 switch (levelData) {
50 case SECURITY_LEVEL_UNKNOWN:
51 contentProtectionLevel = CONTENT_PROTECTION_LEVEL_UNKNOWN;
52 break;
53 case SECURITY_LEVEL_1:
54 contentProtectionLevel = CONTENT_PROTECTION_LEVEL_HW_CRYPTO;
55 break;
56 case SECURITY_LEVEL_3:
57 contentProtectionLevel = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
58 break;
59 default:
60 break;
61 }
62 return contentProtectionLevel;
63 }
64
GetSecurityLevelFromContentProtectionLevel(int32_t levelData)65 int32_t GetSecurityLevelFromContentProtectionLevel(int32_t levelData)
66 {
67 int32_t securityLevel = SECURITY_LEVEL_3;
68 switch (levelData) {
69 case CONTENT_PROTECTION_LEVEL_UNKNOWN:
70 securityLevel = SECURITY_LEVEL_UNKNOWN;
71 break;
72 case CONTENT_PROTECTION_LEVEL_SW_CRYPTO:
73 securityLevel = SECURITY_LEVEL_3;
74 break;
75 case CONTENT_PROTECTION_LEVEL_HW_CRYPTO:
76 case CONTENT_PROTECTION_LEVEL_ENHANCED_HW_CRYPTO:
77 securityLevel = SECURITY_LEVEL_1;
78 break;
79 case CONTENT_PROTECTION_LEVEL_MAX:
80 default:
81 break;
82 }
83 return securityLevel;
84 }
85
toHexString(const uint8_t * data,size_t length)86 std::string toHexString(const uint8_t* data, size_t length)
87 {
88 static const char hexDigits[] = "0123456789ABCDEF";
89 std::string hexString;
90 hexString.reserve(length + length);
91 for (size_t i = 0; i < length; ++i) {
92 hexString.push_back(hexDigits[data[i] >> HEX_OFFSET]);
93 hexString.push_back(hexDigits[data[i] & 0x0F]);
94 }
95 return hexString;
96 }
97
EndsWithAndRemove(std::string & str,const std::string & suffix)98 bool EndsWithAndRemove(std::string& str, const std::string& suffix)
99 {
100 if (str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0) {
101 str.erase(str.size() - suffix.size(), suffix.size());
102 return true;
103 }
104 return false;
105 }
106
IsValidNumber(const std::string & str)107 bool IsValidNumber(const std::string& str)
108 {
109 for (char c : str) {
110 if (!std::isdigit(c)) {
111 return false;
112 }
113 }
114 return true;
115 }
116
DrmCallbackImpl(std::shared_ptr<DrmCallbackAdapter> callbackAdapter)117 DrmCallbackImpl::DrmCallbackImpl(std::shared_ptr<DrmCallbackAdapter> callbackAdapter)
118 : callbackAdapter_(callbackAdapter)
119 {}
120
OnSessionMessage(const std::string & sessionId,int32_t & type,const std::vector<uint8_t> & message)121 void DrmCallbackImpl::OnSessionMessage(const std::string& sessionId, int32_t& type, const std::vector<uint8_t>& message)
122 {
123 if (callbackAdapter_) {
124 callbackAdapter_->OnSessionMessage(sessionId, type, message);
125 }
126 }
127
OnProvisionRequest(const std::string & defaultUrl,const std::string & requestData)128 void DrmCallbackImpl::OnProvisionRequest(const std::string& defaultUrl, const std::string& requestData)
129 {
130 if (callbackAdapter_) {
131 callbackAdapter_->OnProvisionRequest(defaultUrl, requestData);
132 }
133 }
134
OnProvisioningComplete(bool success)135 void DrmCallbackImpl::OnProvisioningComplete(bool success)
136 {
137 if (callbackAdapter_) {
138 callbackAdapter_->OnProvisioningComplete(success);
139 }
140 }
141
OnMediaKeySessionReady(void * session)142 void DrmCallbackImpl::OnMediaKeySessionReady(void* session)
143 {
144 if (callbackAdapter_) {
145 callbackAdapter_->OnMediaKeySessionReady(session);
146 }
147 }
148
OnPromiseRejected(uint32_t promiseId,const std::string & errorMessage)149 void DrmCallbackImpl::OnPromiseRejected(uint32_t promiseId, const std::string& errorMessage)
150 {
151 if (callbackAdapter_) {
152 callbackAdapter_->OnPromiseRejected(promiseId, errorMessage);
153 }
154 }
155
OnPromiseResolved(uint32_t promiseId)156 void DrmCallbackImpl::OnPromiseResolved(uint32_t promiseId)
157 {
158 if (callbackAdapter_) {
159 callbackAdapter_->OnPromiseResolved(promiseId);
160 }
161 }
162
OnPromiseResolvedWithSession(uint32_t promiseId,const std::string & sessionId)163 void DrmCallbackImpl::OnPromiseResolvedWithSession(uint32_t promiseId, const std::string& sessionId)
164 {
165 if (callbackAdapter_) {
166 callbackAdapter_->OnPromiseResolvedWithSession(promiseId, sessionId);
167 }
168 }
169
OnSessionClosed(const std::string & sessionId)170 void DrmCallbackImpl::OnSessionClosed(const std::string& sessionId)
171 {
172 if (callbackAdapter_) {
173 callbackAdapter_->OnSessionClosed(sessionId);
174 }
175 }
176
OnSessionKeysChange(const std::string & sessionId,const std::vector<std::string> & keyIdArray,const std::vector<uint32_t> & statusArray,bool hasAdditionalUsableKey,bool isKeyRelease)177 void DrmCallbackImpl::OnSessionKeysChange(const std::string& sessionId, const std::vector<std::string>& keyIdArray,
178 const std::vector<uint32_t>& statusArray, bool hasAdditionalUsableKey, bool isKeyRelease)
179 {
180 if (callbackAdapter_) {
181 callbackAdapter_->OnSessionKeysChange(sessionId, keyIdArray, statusArray, hasAdditionalUsableKey, isKeyRelease);
182 }
183 }
184
OnSessionExpirationUpdate(const std::string & sessionId,uint64_t expirationTime)185 void DrmCallbackImpl::OnSessionExpirationUpdate(const std::string& sessionId, uint64_t expirationTime)
186 {
187 if (callbackAdapter_) {
188 callbackAdapter_->OnSessionExpirationUpdate(sessionId, expirationTime);
189 }
190 }
191
OnStorageProvisioned()192 void DrmCallbackImpl::OnStorageProvisioned()
193 {
194 if (callbackAdapter_) {
195 callbackAdapter_->OnStorageProvisioned();
196 }
197 }
198
OnStorageSaveInfo(const std::vector<uint8_t> & ketSetId,const std::string & mimeType,const std::string & sessionId,int32_t keyType)199 void DrmCallbackImpl::OnStorageSaveInfo(
200 const std::vector<uint8_t>& ketSetId, const std::string& mimeType, const std::string& sessionId, int32_t keyType)
201 {
202 if (callbackAdapter_) {
203 callbackAdapter_->OnStorageSaveInfo(ketSetId, mimeType, sessionId, keyType);
204 }
205 }
206
OnStorageLoadInfo(const std::string & sessionId)207 void DrmCallbackImpl::OnStorageLoadInfo(const std::string& sessionId)
208 {
209 if (callbackAdapter_) {
210 callbackAdapter_->OnStorageLoadInfo(sessionId);
211 }
212 }
213
OnStorageClearInfoForKeyRelease(const std::string & sessionId)214 void DrmCallbackImpl::OnStorageClearInfoForKeyRelease(const std::string& sessionId)
215 {
216 if (callbackAdapter_) {
217 callbackAdapter_->OnStorageClearInfoForKeyRelease(sessionId);
218 }
219 }
220
OnStorageClearInfoForLoadFail(const std::string & sessionId)221 void DrmCallbackImpl::OnStorageClearInfoForLoadFail(const std::string& sessionId)
222 {
223 if (callbackAdapter_) {
224 callbackAdapter_->OnStorageClearInfoForLoadFail(sessionId);
225 }
226 }
227
OnMediaLicenseReady(bool success)228 void DrmCallbackImpl::OnMediaLicenseReady(bool success)
229 {
230 if (callbackAdapter_) {
231 callbackAdapter_->OnMediaLicenseReady(success);
232 }
233 }
234
UpdateEmeId(const std::string & emeId,bool isRelease)235 void DrmCallbackImpl::UpdateEmeId(const std::string& emeId, bool isRelease)
236 {
237 WVLOG_I("[DRM]DrmAdapterImpl::UpdateEmeId emeId = %{public}s.", emeId.c_str());
238 emeId_ = emeId;
239 isRelease_ = isRelease;
240 }
241
UpdateMimeType(const std::string & mimeType,int32_t type)242 void DrmCallbackImpl::UpdateMimeType(const std::string& mimeType, int32_t type)
243 {
244 WVLOG_I("[DRM]DrmAdapterImpl::UpdateMimeType mimeType = %{public}s.", mimeType.c_str());
245 mimeType_ = mimeType;
246 type_ = type;
247 }
248
EmeId()249 std::string DrmCallbackImpl::EmeId()
250 {
251 return emeId_;
252 }
253
MimeType()254 std::string DrmCallbackImpl::MimeType()
255 {
256 return mimeType_;
257 }
258
IsRelease()259 bool DrmCallbackImpl::IsRelease()
260 {
261 return isRelease_;
262 }
263
Type()264 int32_t DrmCallbackImpl::Type()
265 {
266 return type_;
267 }
268
~DrmAdapterImpl()269 DrmAdapterImpl::~DrmAdapterImpl()
270 {
271 WVLOG_I("[DRM]DrmAdapterImpl::~DrmAdapterImpl enter.");
272 if (drmKeySessoin_ != nullptr) {
273 ReleaseMediaKeySession();
274 if (callback_) {
275 callback_->OnMediaKeySessionReady(nullptr);
276 }
277 }
278 if (drmKeySystem_ != nullptr) {
279 ReleaseMediaKeySystem();
280 }
281 }
282
IsSupported(const std::string & name)283 bool DrmAdapterImpl::IsSupported(const std::string& name)
284 {
285 WVLOG_I("[DRM]DrmAdapterImpl::IsSupported");
286 if (name.empty()) {
287 WVLOG_E("[DRM]name is empty!");
288 return false;
289 }
290
291 bool isSupported = OH_MediaKeySystem_IsSupported(name.c_str());
292 WVLOG_I("[DRM]DrmAdapterImpl::IsSupported: %{public}d", isSupported);
293 return isSupported;
294 }
295
IsSupported2(const std::string & name,const std::string & mimeType)296 bool DrmAdapterImpl::IsSupported2(const std::string& name, const std::string& mimeType)
297 {
298 WVLOG_I("[DRM]DrmAdapterImpl::IsSupported2 enter");
299 if (name.empty()) {
300 WVLOG_E("[DRM]name is empty!");
301 return false;
302 }
303 if (mimeType.empty()) {
304 WVLOG_E("[DRM]mimeType is empty!");
305 return false;
306 }
307
308 bool isSupported = OH_MediaKeySystem_IsSupported2(name.c_str(), mimeType.c_str());
309 WVLOG_I("[DRM]DrmAdapterImpl::IsSupported2: %{public}d", isSupported);
310 return isSupported;
311 }
312
IsSupported3(const std::string & name,const std::string & mimeType,int32_t level)313 bool DrmAdapterImpl::IsSupported3(const std::string& name, const std::string& mimeType, int32_t level)
314 {
315 WVLOG_I("[DRM]DrmAdapterImpl::IsSupported3 enter");
316 if (name.empty()) {
317 WVLOG_E("[DRM]name is empty!");
318 return false;
319 }
320 if (mimeType.empty()) {
321 WVLOG_E("[DRM]mimeType is empty!");
322 return false;
323 }
324 bool isSupported =
325 OH_MediaKeySystem_IsSupported3(name.c_str(), mimeType.c_str(), static_cast<DRM_ContentProtectionLevel>(level));
326 if (isSupported != true) {
327 WVLOG_E("[DRM]The device does not support the content protection level.");
328 }
329 return isSupported;
330 }
331
GetUUID(const std::string & name)332 std::vector<uint8_t> DrmAdapterImpl::GetUUID(const std::string& name)
333 {
334 WVLOG_I("[DRM]DrmAdapterImpl::GetUUID enter, name = %{public}s:", name.c_str());
335 std::vector<uint8_t> uuid;
336 uuid.clear();
337 uint32_t count = 10;
338 DRM_MediaKeySystemDescription infos[10];
339 (void)memset_s(infos, sizeof(infos), 0, sizeof(infos));
340 Drm_ErrCode errNo = OH_MediaKeySystem_GetMediaKeySystems(infos, &count);
341 if (errNo != DRM_ERR_OK) {
342 WVLOG_E("[DRM]DRMAdapterImpl::GetMediaKeySystems failed.");
343 return uuid;
344 }
345 WVLOG_I("[DRM]DrmAdapterImpl::GetUUID, name = %{public}s, count = %{public}d", name.c_str(), count);
346 for (uint32_t i = 0; i < count; i++) {
347 if (name == infos[i].name) {
348 uuid.insert(uuid.begin(), infos[i].uuid, infos[i].uuid + DRM_UUID_LEN);
349 break;
350 }
351 }
352 WVLOG_I("[DRM]DrmAdapterImpl::GetUUID, name = %{public}s", name.c_str());
353 return uuid;
354 }
355
ReleaseMediaKeySystem()356 int32_t DrmAdapterImpl::ReleaseMediaKeySystem()
357 {
358 WVLOG_I("[DRM]DrmAdapterImpl::ReleaseMediaKeySystem enter");
359 if (drmKeySystem_ == nullptr) {
360 WVLOG_E("[DRM]drmKeySystem_ is nullptr!");
361 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
362 }
363
364 {
365 std::lock_guard<std::mutex> lock(mediaKeySystemCallbackMapMutex_);
366 auto iter = mediaKeySystemCallbackMap_.find(drmKeySystem_);
367 if (iter != mediaKeySystemCallbackMap_.end()) {
368 mediaKeySystemCallbackMap_.erase(iter);
369 }
370 }
371
372 Drm_ErrCode ret = OH_MediaKeySystem_Destroy(drmKeySystem_);
373 drmKeySystem_ = nullptr;
374 if (ret != DRM_ERR_OK) {
375 WVLOG_E("[DRM]Failed to release MediaKeySystem.");
376 }
377 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
378 }
379
ReleaseMediaKeySession()380 int32_t DrmAdapterImpl::ReleaseMediaKeySession()
381 {
382 WVLOG_I("[DRM]DrmAdapterImpl::ReleaseMediaKeySession enter");
383 if (drmKeySessoin_ == nullptr) {
384 WVLOG_E("[DRM]drmKeySessoin_ is nullptr!");
385 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
386 }
387
388 {
389 std::lock_guard<std::mutex> lock(mediaKeySessionCallbackMutex_);
390 auto iter = mediaKeySessionCallbackMap_.find(drmKeySessoin_);
391 if (iter != mediaKeySessionCallbackMap_.end()) {
392 mediaKeySessionCallbackMap_.erase(iter);
393 }
394 }
395
396 Drm_ErrCode ret = OH_MediaKeySession_Destroy(drmKeySessoin_);
397 drmKeySessoin_ = nullptr;
398 if (ret != DRM_ERR_OK) {
399 WVLOG_E("[DRM]Failed to release MediaKeySessoin.");
400 }
401 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
402 }
403
SystemCallBackWithObj(MediaKeySystem * mediaKeySystem,DRM_EventType eventType,uint8_t * info,int32_t infoLen,char * extra)404 Drm_ErrCode DrmAdapterImpl::SystemCallBackWithObj(
405 MediaKeySystem* mediaKeySystem, DRM_EventType eventType, uint8_t* info, int32_t infoLen, char* extra)
406 {
407 WVLOG_I("[DRM]DrmAdapterImpl::SystemCallBackWithObj enter. eventType = %{public}d.", eventType);
408 if (mediaKeySystem == nullptr) {
409 WVLOG_E("[DRM]DrmAdapterImpl::SystemCallBackWithObj error, mediaKeySystem is nullptr.");
410 return DRM_ERR_INVALID_VAL;
411 }
412
413 std::shared_ptr<DrmCallbackImpl> callback = nullptr;
414 {
415 std::lock_guard<std::mutex> lock(mediaKeySystemCallbackMapMutex_);
416 auto iter = mediaKeySystemCallbackMap_.find(mediaKeySystem);
417 if (iter == mediaKeySystemCallbackMap_.end()) {
418 WVLOG_E("[DRM]DrmAdapterImpl::SystemCallBackWithObj error, mediaKeySystem not found.");
419 return DRM_ERR_INVALID_VAL;
420 }
421 callback = iter->second;
422 }
423
424 if (callback == nullptr) {
425 WVLOG_E("[DRM]DrmAdapterImpl::SystemCallBackWithObj error, callback is nullptr.");
426 return DRM_ERR_INVALID_VAL;
427 }
428
429 Drm_ErrCode ret = DRM_ERR_OK;
430 if (eventType == EVENT_PROVISION_REQUIRED) {
431 uint8_t request[MAX_REQUEST_LENGTH] = { 0x00 };
432 int32_t requestLen = MAX_REQUEST_LENGTH;
433 char defaultUrl[MAX_URL_LENGTH] = { 0x00 };
434 int32_t defaultUrlLen = MAX_URL_LENGTH;
435 ret =
436 OH_MediaKeySystem_GenerateKeySystemRequest(mediaKeySystem, request, &requestLen, defaultUrl, defaultUrlLen);
437 WVLOG_I("[DRM]DrmAdapterImpl::OH_MediaKeySystem_GenerateKeySystemRequest, ret = %{public}d.", ret);
438 if (ret == DRM_ERR_OK) {
439 if (requestLen > MAX_REQUEST_LENGTH) {
440 WVLOG_E("[DRM]OH_MediaKeySystem_GenerateKeySystemRequest error, invalid requestLen.");
441 return DRM_ERR_INVALID_VAL;
442 }
443 std::vector<uint8_t> requestData;
444 std::string requestString;
445 requestData.insert(requestData.begin(), request, request + requestLen);
446 requestString.assign(requestData.begin(), requestData.end());
447 callback->OnProvisionRequest(std::string(defaultUrl), requestString);
448 }
449 }
450 return ret;
451 }
452
OnSessionExpirationUpdate(MediaKeySession * drmKeySessoin,uint8_t * info,int32_t infoLen)453 void DrmAdapterImpl::OnSessionExpirationUpdate(MediaKeySession* drmKeySessoin, uint8_t* info, int32_t infoLen)
454 {
455 WVLOG_I("[DRM]DrmAdapterImpl::OnSessionExpirationUpdate enter. infoLen = %{public}d", infoLen);
456 std::shared_ptr<DrmCallbackImpl> callback = nullptr;
457 {
458 std::lock_guard<std::mutex> lock(mediaKeySessionCallbackMutex_);
459 auto iter = mediaKeySessionCallbackMap_.find(drmKeySessoin);
460 if (iter == mediaKeySessionCallbackMap_.end()) {
461 WVLOG_E("[DRM]DrmAdapterImpl::OnSessionExpirationUpdate error, mediaKeySessoin not found.");
462 return;
463 }
464 callback = iter->second;
465 }
466
467 if (callback == nullptr) {
468 WVLOG_E("[DRM]DrmAdapterImpl::OnSessionExpirationUpdate error, callback is nullptr.");
469 return;
470 }
471
472 uint64_t timeStamp = 0;
473 if (info != nullptr && infoLen > 0 && infoLen <= EXPIRATION_INFO_MAX_LEN) {
474 std::vector<uint8_t> infoData;
475 std::string infoString;
476 infoData.insert(infoData.begin(), info, info + infoLen);
477 infoString.assign(infoData.begin(), infoData.end());
478 WVLOG_I("[DRM]DrmAdapterImpl::OnSessionExpirationUpdate. infoString = %{public}s", infoString.c_str());
479 EndsWithAndRemove(infoString, "ms");
480 if (IsValidNumber(infoString)) {
481 if (infoString.size() > MILLISECOND_DIGITS) {
482 infoString.erase(infoString.size() - MILLISECOND_DIGITS, MILLISECOND_DIGITS);
483 }
484 timeStamp = std::strtoull(infoString.c_str(), nullptr, EXPIRATION_INFO_BASE);
485 }
486 }
487 std::string emeId = callback->EmeId();
488 WVLOG_I("[DRM]DrmAdapterImpl::SessoinKeyChangeCallBackWithObj emeId = %{public}s.", emeId.c_str());
489 callback->OnSessionExpirationUpdate(emeId, timeStamp * MILLISECOND_IN_SECOND);
490 }
491
GetKeyRequest(MediaKeySession * drmKeySessoin,uint8_t * info,int32_t infoLen)492 void DrmAdapterImpl::GetKeyRequest(MediaKeySession* drmKeySessoin, uint8_t* info, int32_t infoLen)
493 {
494 WVLOG_I("[DRM]DrmAdapterImpl::GetKeyRequest enter.");
495 if (drmKeySessoin == nullptr) {
496 WVLOG_E("[DRM]DrmAdapterImpl::GetKeyRequest error, drmKeySessoin is nullptr.");
497 return;
498 }
499 std::shared_ptr<DrmCallbackImpl> callback = nullptr;
500 {
501 std::lock_guard<std::mutex> lock(mediaKeySessionCallbackMutex_);
502 auto iter = mediaKeySessionCallbackMap_.find(drmKeySessoin);
503 if (iter == mediaKeySessionCallbackMap_.end()) {
504 WVLOG_E("[DRM]DrmAdapterImpl::GetKeyRequest error, mediaKeySessoin not found.");
505 return;
506 }
507 callback = iter->second;
508 }
509 if (callback == nullptr) {
510 WVLOG_E("[DRM]DrmAdapterImpl::GetKeyRequest error, callback is nupllptr.");
511 return;
512 }
513 DRM_MediaKeyRequestInfo reqInfo;
514 DRM_MediaKeyRequest mediaKeyRequest;
515 reqInfo.type = static_cast<DRM_MediaKeyType>(callback->Type());
516 reqInfo.initDataLen = infoLen;
517 reqInfo.optionsCount = 0;
518 errno_t retCopy =
519 memcpy_s(reqInfo.mimeType, MAX_MIMETYPE_LEN, callback->MimeType().c_str(), callback->MimeType().length());
520 if (retCopy != 0) {
521 WVLOG_E("[DRM]memcpy_s failed with error.");
522 return;
523 }
524 if (infoLen > 0) {
525 retCopy = memcpy_s(reqInfo.initData, MAX_INIT_DATA_LEN, info, infoLen);
526 if (retCopy != 0) {
527 WVLOG_E("[DRM]memcpy_s failed with error.");
528 return;
529 }
530 }
531
532 Drm_ErrCode ret = OH_MediaKeySession_GenerateMediaKeyRequest(drmKeySessoin, &reqInfo, &mediaKeyRequest);
533 WVLOG_I("[DRM]DrmAdapterImpl::OH_MediaKeySession_GenerateMediaKeyRequest, ret = %{public}d", ret);
534 if (ret != DRM_ERR_OK) {
535 return;
536 }
537 int32_t requestType = static_cast<int32_t>(mediaKeyRequest.type);
538 std::vector<uint8_t> requestData;
539 requestData.insert(requestData.begin(), mediaKeyRequest.data, mediaKeyRequest.data + mediaKeyRequest.dataLen);
540 callback->OnSessionMessage(callback->EmeId(), requestType, requestData);
541 }
542
SessoinEventCallBackWithObj(MediaKeySession * mediaKeySessoin,DRM_EventType eventType,uint8_t * info,int32_t infoLen,char * extra)543 Drm_ErrCode DrmAdapterImpl::SessoinEventCallBackWithObj(
544 MediaKeySession* mediaKeySessoin, DRM_EventType eventType, uint8_t* info, int32_t infoLen, char* extra)
545 {
546 WVLOG_I("[DRM]DrmAdapterImpl::SessoinEventCallBackWithObj: %{public}d, infoLen = %{public}d", (int32_t)eventType,
547 infoLen);
548 switch (eventType) {
549 case EVENT_KEY_REQUIRED:
550 GetKeyRequest(mediaKeySessoin, info, infoLen);
551 break;
552 case EVENT_EXPIRATION_UPDATE:
553 OnSessionExpirationUpdate(mediaKeySessoin, info, infoLen);
554 break;
555 case EVENT_DRM_BASE:
556 case EVENT_PROVISION_REQUIRED:
557 case EVENT_KEY_EXPIRED:
558 case EVENT_VENDOR_DEFINED:
559 default:
560 break;
561 }
562 return DRM_ERR_OK;
563 }
564
SessoinKeyChangeCallBackWithObj(MediaKeySession * mediaKeySessoin,DRM_KeysInfo * keysInfo,bool newKeysAvailable)565 Drm_ErrCode DrmAdapterImpl::SessoinKeyChangeCallBackWithObj(
566 MediaKeySession* mediaKeySessoin, DRM_KeysInfo* keysInfo, bool newKeysAvailable)
567 {
568 WVLOG_I("[DRM]DrmAdapterImpl::SessoinKeyChangeCallBackWithObj enter.");
569 if (keysInfo == nullptr) {
570 WVLOG_E("[DRM]DrmAdapterImpl::SessoinKeyChangeCallBackWithObj: keysInfo is nullptr.");
571 return DRM_ERR_INVALID_VAL;
572 }
573
574 std::shared_ptr<DrmCallbackImpl> callback = nullptr;
575 {
576 std::lock_guard<std::mutex> lock(mediaKeySessionCallbackMutex_);
577 auto sessionIter = mediaKeySessionCallbackMap_.find(mediaKeySessoin);
578 if (sessionIter == mediaKeySessionCallbackMap_.end()) {
579 WVLOG_E("[DRM]DrmAdapterImpl::SessoinKeyChangeCallBackWithObj: mediaKeySessoin is invalid.");
580 return DRM_ERR_INVALID_VAL;
581 }
582 callback = sessionIter->second;
583 }
584 std::vector<std::string> keyIdArray;
585 std::vector<uint32_t> statusArray;
586 for (uint32_t i = 0; i < keysInfo->keysInfoCount; i++) {
587 std::string statusStr = std::string(keysInfo->statusValue[i]);
588 uint32_t statusCode = static_cast<uint32_t>(KeyStatus::KEY_STATUS_INTERNAL_ERROR);
589
590 auto iter = KeyStatusMap.find(statusStr);
591 if (iter != KeyStatusMap.end()) {
592 statusCode = iter->second;
593 }
594
595 std::string keyIdStr = toHexString(keysInfo->keyId[i], MAX_KEY_ID_LEN);
596 keyIdArray.push_back(keyIdStr);
597 statusArray.push_back(statusCode);
598 }
599
600 if (callback) {
601 WVLOG_I("[DRM]DrmAdapterImpl::SessoinKeyChangeCallBackWithObj emeId = %{public}s.", callback->EmeId().c_str());
602 callback->OnSessionKeysChange(
603 callback->EmeId(), keyIdArray, statusArray, newKeysAvailable, callback->IsRelease());
604 } else {
605 WVLOG_E("[DRM]DrmAdapterImpl::SessoinKeyChangeCallBackWithObj, callback is nullptr.");
606 }
607 return DRM_ERR_OK;
608 }
609
CreateKeySystem(const std::string & name,const std::string & origin,int32_t securityLevel)610 int32_t DrmAdapterImpl::CreateKeySystem(const std::string& name, const std::string& origin, int32_t securityLevel)
611 {
612 WVLOG_I("[DRM]DrmAdapterImpl::CreateKeySystem enter.");
613 if (name.empty()) {
614 WVLOG_E("[DRM]name is empty!");
615 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
616 }
617
618 Drm_ErrCode ret = OH_MediaKeySystem_Create(name.c_str(), &drmKeySystem_);
619 WVLOG_I("[DRM]DrmAdapterImpl::OH_MediaKeySystem_Create name: %{public}s, ret: %{public}d.", name.c_str(), ret);
620 if (ret != DRM_ERR_OK) {
621 WVLOG_E("[DRM]DrmAdapterImpl::CreateKeySystem failed.");
622 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
623 }
624 {
625 std::lock_guard<std::mutex> lock(mediaKeySystemCallbackMapMutex_);
626 mediaKeySystemCallbackMap_[drmKeySystem_] = callback_;
627 }
628
629 if (name == WIDEVINE_NAME) {
630 SetConfigurationString(PRIVACY_MODE, ENABLE);
631 SetConfigurationString(SESSION_SHARING, ENABLE);
632 int32_t configRet = SetConfigurationString(ORIGIN, origin);
633 if (configRet != static_cast<int32_t>(DrmResult::DRM_RESULT_OK)) {
634 WVLOG_E("[DRM]DrmAdapterImpl::CreateKeySystem ORIGIN set failed.");
635 }
636 } else if (name == WISEPLAY_NAME) {
637 isWiseplay_ = true;
638 }
639
640 ret = OH_MediaKeySystem_SetCallback(drmKeySystem_, SystemCallBackWithObj);
641 if (ret != DRM_ERR_OK) {
642 WVLOG_E("[DRM]OH_MediaKeySystem_SetCallback failed.");
643 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
644 }
645 contentProtectionLevel_ = GetContentProtectionLevelFromSecurityLevel(securityLevel);
646 int32_t iRet = CreateMediaKeySession();
647 if (iRet != 0) {
648 WVLOG_E("[DRM]OH_MediaKeySystem_CreateMediaKeySession failed.");
649 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
650 }
651 WVLOG_I("[DRM]DrmAdapterImpl::CreateKeySystem exit.");
652 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
653 }
654
CreateMediaKeySession()655 int32_t DrmAdapterImpl::CreateMediaKeySession()
656 {
657 WVLOG_I("[DRM]DrmAdapterImpl::CreateMediaKeySession enter.");
658 if (drmKeySystem_ == nullptr) {
659 WVLOG_E("[DRM]drmKeySystem_ is nullptr!");
660 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
661 }
662 if (drmKeySessoin_ != nullptr) {
663 WVLOG_W("[DRM]DrmAdapterImpl::CreateMediaKeySession drmKeySessoin_ already exist.");
664 ReleaseMediaKeySession();
665 }
666 Drm_ErrCode ret = DRM_ERR_OK;
667 ret = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem_, &contentProtectionLevel_, &drmKeySessoin_);
668 WVLOG_I("[DRM]DrmAdapterImpl::OH_MediaKeySystem_CreateMediaKeySession ret: %{public}d.", ret);
669 if (ret != DRM_ERR_OK) {
670 WVLOG_E("[DRM]DrmAdapterImpl::CreateMediaKeySession failed.");
671 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
672 }
673 {
674 std::lock_guard<std::mutex> lock(mediaKeySessionCallbackMutex_);
675 mediaKeySessionCallbackMap_[drmKeySessoin_] = callback_;
676 }
677 OH_MediaKeySession_Callback sessionCallback = { SessoinEventCallBackWithObj, SessoinKeyChangeCallBackWithObj };
678 ret = OH_MediaKeySession_SetCallback(drmKeySessoin_, &sessionCallback);
679 if (ret != DRM_ERR_OK) {
680 WVLOG_E("[DRM]DrmAdapterImpl::CreateMediaKeySession failed.");
681 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
682 }
683 if (callback_) {
684 callback_->OnMediaKeySessionReady(reinterpret_cast<OHOSMediaKeySession>(drmKeySessoin_));
685 }
686 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
687 }
688
SetConfigurationString(const std::string & configName,const std::string & value)689 int32_t DrmAdapterImpl::SetConfigurationString(const std::string& configName, const std::string& value)
690 {
691 WVLOG_I("[DRM]DrmAdapterImpl::SetConfigurationString configName: %{public}s, vale: %{public}s", configName.c_str(),
692 value.c_str());
693 if (configName.empty()) {
694 WVLOG_E("[DRM]configName is empty!");
695 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
696 }
697 if (value.empty()) {
698 WVLOG_E("[DRM]value is empty!");
699 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
700 }
701 if (drmKeySystem_ == nullptr) {
702 WVLOG_E("[DRM]drmKeySystem_ is nullptr!");
703 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
704 }
705 Drm_ErrCode ret = OH_MediaKeySystem_SetConfigurationString(drmKeySystem_, configName.c_str(), value.c_str());
706 WVLOG_I("[DRM]DrmAdapterImpl::SetConfigurationString configName: %{public}s, vale: %{public}s, ret: %{public}d.",
707 configName.c_str(), value.c_str(), ret);
708 if (ret != DRM_ERR_OK) {
709 WVLOG_E("[DRM]DrmAdapterImpl::SetConfigurationString failed.");
710 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
711 }
712 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
713 }
714
GetConfigurationString(const std::string & configName,char * value,int32_t valueLen)715 int32_t DrmAdapterImpl::GetConfigurationString(const std::string& configName, char* value, int32_t valueLen)
716 {
717 WVLOG_I("[DRM]DrmAdapterImpl::GetConfigurationString");
718
719 if (configName.empty()) {
720 WVLOG_E("[DRM]configName is empty!");
721 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
722 }
723 if (value == nullptr) {
724 WVLOG_E("[DRM]value is nullptr!");
725 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
726 }
727 if (drmKeySystem_ == nullptr) {
728 WVLOG_E("[DRM]drmKeySystem_ is nullptr!");
729 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
730 }
731 Drm_ErrCode ret = OH_MediaKeySystem_GetConfigurationString(drmKeySystem_, configName.c_str(), value, valueLen);
732 if (ret != DRM_ERR_OK) {
733 WVLOG_E("[DRM]DrmAdapterImpl::GetConfigurationString failed.");
734 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
735 }
736 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
737 }
738
SetConfigurationByteArray(const std::string & configName,const uint8_t * value,int32_t valueLen)739 int32_t DrmAdapterImpl::SetConfigurationByteArray(const std::string& configName, const uint8_t* value, int32_t valueLen)
740 {
741 WVLOG_I("[DRM]DrmAdapterImpl::SetConfigurationByteArray");
742
743 if (configName.empty()) {
744 WVLOG_E("[DRM]configName is empty!");
745 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
746 }
747 if (value == nullptr) {
748 WVLOG_E("[DRM]value is nullptr!");
749 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
750 }
751 if (drmKeySystem_ == nullptr) {
752 WVLOG_E("[DRM]drmKeySystem_ is nullptr!");
753 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
754 }
755 uint8_t* nonConstPtr = const_cast<uint8_t*>(value);
756 Drm_ErrCode ret =
757 OH_MediaKeySystem_SetConfigurationByteArray(drmKeySystem_, configName.c_str(), nonConstPtr, valueLen);
758 if (ret != DRM_ERR_OK) {
759 WVLOG_E("[DRM]DrmAdapterImpl::SetConfigurationByteArray failed.");
760 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
761 }
762 return ret;
763 }
764
GetConfigurationByteArray(const std::string & configName,uint8_t * value,int32_t * valueLen)765 int32_t DrmAdapterImpl::GetConfigurationByteArray(const std::string& configName, uint8_t* value, int32_t* valueLen)
766 {
767 WVLOG_I("[DRM]DrmAdapterImpl::GetConfigurationByteArray");
768 if (configName.empty()) {
769 WVLOG_E("[DRM]configName is empty!");
770 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
771 }
772 if (value == nullptr) {
773 WVLOG_E("[DRM]value is nullptr!");
774 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
775 }
776 if (valueLen == nullptr) {
777 WVLOG_E("[DRM]valueLen is nullptr!");
778 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
779 }
780 if (drmKeySystem_ == nullptr) {
781 WVLOG_E("[DRM]drmKeySystem_ is nullptr!");
782 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
783 }
784 Drm_ErrCode ret = OH_MediaKeySystem_GetConfigurationByteArray(drmKeySystem_, configName.c_str(), value, valueLen);
785 if (ret != DRM_ERR_OK) {
786 WVLOG_E("[DRM]DrmAdapterImpl::GetConfigurationByteArray failed.");
787 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
788 }
789 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
790 }
791
GetMaxContentProtectionLevel(int32_t & level)792 int32_t DrmAdapterImpl::GetMaxContentProtectionLevel(int32_t& level)
793 {
794 WVLOG_I("[DRM]DrmAdapterImpl::GetMaxContentProtectionLevel enter.");
795 if (drmKeySystem_ == nullptr) {
796 WVLOG_E("[DRM]drmKeySystem_ is nullptr!");
797 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
798 }
799
800 DRM_ContentProtectionLevel contentProtectionLevel = CONTENT_PROTECTION_LEVEL_UNKNOWN;
801
802 Drm_ErrCode ret = OH_MediaKeySystem_GetMaxContentProtectionLevel(drmKeySystem_, &contentProtectionLevel);
803 level = contentProtectionLevel;
804 if (ret != DRM_ERR_OK) {
805 WVLOG_E("[DRM]DrmAdapterImpl::GetMaxContentProtectionLevel failed.");
806 }
807 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
808 }
809
StorageProvisionedResult(bool result)810 void DrmAdapterImpl::StorageProvisionedResult(bool result)
811 {
812 WVLOG_I("[DRM]DrmAdapterImpl::StorageProvisionedResult enter, result = %{public}d: ", result);
813 if (drmKeySessoin_ == nullptr) {
814 if (result) {
815 CreateMediaKeySession();
816 }
817 }
818 }
819
StorageSaveInfoResult(bool result,int32_t type)820 void DrmAdapterImpl::StorageSaveInfoResult(bool result, int32_t type)
821 {
822 WVLOG_I("[DRM]DrmAdapterImpl::StorageSaveInfoResult enter, result = %{public}d: ", result);
823 if (!result) {
824 if (callback_) {
825 callback_->OnPromiseRejected(removeSessionPromiseId_, "Fail to update persistent storage");
826 }
827 return;
828 }
829 if (type != static_cast<int32_t>(MediaKeyType::MEDIA_KEY_TYPE_RELEASE)) {
830 HandleKeyUpdatedCallback(updateSessionPromiseId_, result);
831 WVLOG_I("[DRM]DrmAdapterImpl::StorageSaveInfoResult result = %{public}d: ", result);
832 return;
833 }
834
835 if (!drmKeySystem_ || !drmKeySessoin_) {
836 return;
837 }
838
839 uint8_t releaseRequest[MAX_MEDIA_KEY_REQUEST_DATA_LEN];
840 int32_t releaseRequestLen = MAX_MEDIA_KEY_REQUEST_DATA_LEN;
841 std::shared_ptr<SessionId> sessionId = GetSessionIdByEmeId(releaseEmeId_);
842 if (sessionId == nullptr) {
843 if (callback_) {
844 callback_->OnPromiseRejected(removeSessionPromiseId_, "Session doesn't exist");
845 }
846 return;
847 }
848 Drm_ErrCode ret = OH_MediaKeySession_GenerateOfflineReleaseRequest(
849 drmKeySessoin_, sessionId->KeySetId(), sessionId->KeySetIdLen(), releaseRequest, &releaseRequestLen);
850 WVLOG_I("[DRM]DrmAdapterImpl::OH_MediaKeySession_GenerateOfflineReleaseRequest ret = %{public}d: ", ret);
851 if (ret != DRM_ERR_OK) {
852 if (callback_) {
853 callback_->OnPromiseRejected(removeSessionPromiseId_, "Fail to generate key release request");
854 }
855 WVLOG_I("[DRM]DrmAdapterImpl::StorageSaveInfoResult ret = %{public}d: ", ret);
856 return;
857 }
858 if (releaseRequestLen > MAX_MEDIA_KEY_REQUEST_DATA_LEN) {
859 WVLOG_E("[DRM]OH_MediaKeySession_GenerateOfflineReleaseRequest error, invalid releaseRequestLen.");
860 return;
861 }
862 std::vector<uint8_t> requestData;
863 requestData.insert(requestData.begin(), releaseRequest, releaseRequest + releaseRequestLen);
864 int32_t requestType = static_cast<int32_t>(MediaKeyType::MEDIA_KEY_TYPE_RELEASE);
865 if (callback_) {
866 callback_->OnPromiseResolved(removeSessionPromiseId_);
867 callback_->UpdateEmeId(sessionId->EmeId(), true);
868 callback_->OnSessionMessage(releaseEmeId_, requestType, requestData);
869 }
870 return;
871 }
872
StorageLoadInfoResult(const std::string & emeId,const std::vector<uint8_t> & keySetId,const std::string & mimeType,uint32_t keyType)873 void DrmAdapterImpl::StorageLoadInfoResult(
874 const std::string& emeId, const std::vector<uint8_t>& keySetId, const std::string& mimeType, uint32_t keyType)
875 {
876 WVLOG_I("[DRM]DrmAdapterImpl::StorageLoadInfoResult enter, emeId = %{public}s: ", emeId.c_str());
877 if (keySetId.size() == 0) {
878 WVLOG_I("[DRM]DrmAdapterImpl::StorageLoadInfoResult emeId = %{public}s: ", emeId.c_str());
879 if (callback_) {
880 callback_->OnPromiseResolvedWithSession(loadSessionPromiseId_, "");
881 }
882 return;
883 }
884
885 // Loading same persistent license into different sessions isn't
886 // supported.
887 if (GetSessionIdByEmeId(emeId) != nullptr) {
888 return;
889 }
890
891 std::shared_ptr<SessionId> sessionId = std::make_shared<SessionId>(emeId, keySetId.data(), keySetId.size());
892 PutSessionInfo(sessionId, mimeType, keyType);
893 LoadSessionWithLoadedStorage(sessionId, loadSessionPromiseId_);
894 }
895
StorageClearInfoResult(bool result,int32_t type)896 void DrmAdapterImpl::StorageClearInfoResult(bool result, int32_t type)
897 {
898 WVLOG_I("[DRM]DrmAdapterImpl::StorageClearInfoResult enter.");
899 if (type == static_cast<int32_t>(ClearInfoType::KEY_RELEASE)) {
900 HandleKeyUpdatedCallback(updateSessionPromiseId_, result);
901 } else if (type == static_cast<int32_t>(ClearInfoType::LOAD_FAIL)) {
902 if (!result) {
903 WVLOG_W("[DRM]Failed to clear persistent storage for non-exist license");
904 }
905 if (callback_) {
906 callback_->OnPromiseResolvedWithSession(loadSessionPromiseId_, "");
907 }
908 }
909 }
910
ProcessKeySystemResponse(const std::string & response,bool isResponseReceived)911 int32_t DrmAdapterImpl::ProcessKeySystemResponse(const std::string& response, bool isResponseReceived)
912 {
913 WVLOG_I("[DRM]DrmAdapterImpl::ProcessKeySystemResponse enter, isResponseReceived : %{public}d", isResponseReceived);
914 if (drmKeySystem_ == nullptr) {
915 WVLOG_E("[DRM]drmKeySystem_ is nullptr!");
916 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
917 }
918
919 bool success = true;
920 if (isResponseReceived) {
921 int32_t responseLen = static_cast<int32_t>(response.size());
922 std::vector<uint8_t> vec(responseLen);
923 errno_t retCopy = memcpy_s(vec.data(), responseLen, response.data(), response.size());
924 if (retCopy != 0) {
925 WVLOG_E("[DRM]memcpy_s failed with error.");
926 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
927 }
928 Drm_ErrCode ret = OH_MediaKeySystem_ProcessKeySystemResponse(drmKeySystem_, vec.data(), responseLen);
929 WVLOG_I("[DRM]DrmAdapterImpl::OH_MediaKeySystem_ProcessKeySystemResponse ret : %{public}d", ret);
930 if (ret != DRM_ERR_OK) {
931 WVLOG_E("[DRM]DrmAdapterImpl::ProcessKeySystemResponse failed.");
932 success = false;
933 }
934 } else {
935 success = false;
936 }
937
938 if (!success) {
939 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
940 }
941 if (callback_) {
942 callback_->OnStorageProvisioned();
943 }
944 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
945 }
946
GetCertificateStatus(int32_t & certStatus)947 int32_t DrmAdapterImpl::GetCertificateStatus(int32_t& certStatus)
948 {
949 WVLOG_I("[DRM]DrmAdapterImpl::GetCertificateStatus");
950 if (drmKeySystem_ == nullptr) {
951 WVLOG_E("[DRM]drmKeySystem_ is nullptr!");
952 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
953 }
954 DRM_CertificateStatus cert = CERT_STATUS_INVALID;
955 Drm_ErrCode ret = OH_MediaKeySystem_GetCertificateStatus(drmKeySystem_, &cert);
956 certStatus = cert;
957 if (ret != DRM_ERR_OK) {
958 WVLOG_E("[DRM]DrmAdapterImpl::GetCertificateStatus failed.");
959 }
960 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
961 }
962
RegistDrmCallback(std::shared_ptr<DrmCallbackAdapter> callbackAdapter)963 int32_t DrmAdapterImpl::RegistDrmCallback(std::shared_ptr<DrmCallbackAdapter> callbackAdapter)
964 {
965 callback_ = std::make_shared<DrmCallbackImpl>(callbackAdapter);
966 return DRM_ERR_OK;
967 }
968
UpdateSessionResult(bool isKeyRelease,std::shared_ptr<SessionId> sessionId,uint8_t * mediaKeyId,int32_t mediaKeyIdLen)969 void DrmAdapterImpl::UpdateSessionResult(
970 bool isKeyRelease, std::shared_ptr<SessionId> sessionId, uint8_t* mediaKeyId, int32_t mediaKeyIdLen)
971 {
972 WVLOG_I("[DRM]DrmAdapterImpl::UpdateSessionResult enter.");
973 if (sessionId == nullptr) {
974 return;
975 }
976
977 std::shared_ptr<SessionInfo> info = GetSessionInfo(sessionId);
978 if (info == nullptr) {
979 WVLOG_E(
980 "[DRM]DrmAdapterImpl::UpdateSessionResult, info is nullprt, emeId: %{public}s", sessionId->EmeId().c_str());
981 return;
982 }
983
984 if (isKeyRelease) {
985 WVLOG_I("[DRM]DrmAdapterImpl::UpdateSessionResult, emeId: %{public}s", sessionId->EmeId().c_str());
986 ClearPersistentSessionInfoFroKeyRelease(sessionId);
987 } else if (info->KeyType() == static_cast<int32_t>(MediaKeyType::MEDIA_KEY_TYPE_OFFLINE) && mediaKeyIdLen > 0) {
988 WVLOG_I("[DRM]DrmAdapterImpl::UpdateSessionResult, emeId: %{public}s", sessionId->EmeId().c_str());
989 SetKeySetId(sessionId, mediaKeyId, mediaKeyIdLen);
990 if (callback_) {
991 callback_->UpdateEmeId(sessionId->EmeId(), false);
992 }
993 } else {
994 WVLOG_I("[DRM]DrmAdapterImpl::UpdateSessionResult, emeId: %{public}s", sessionId->EmeId().c_str());
995 HandleKeyUpdatedCallback(updateSessionPromiseId_, true);
996 if (callback_) {
997 callback_->UpdateEmeId(sessionId->EmeId(), false);
998 }
999 }
1000 }
1001
UpdateSession(uint32_t promiseId,const std::string & emeId,std::vector<uint8_t> response)1002 int32_t DrmAdapterImpl::UpdateSession(uint32_t promiseId, const std::string& emeId, std::vector<uint8_t> response)
1003 {
1004 WVLOG_I("[DRM]DrmAdapterImpl::UpdateSession enter, emeId: %{public}s", emeId.c_str());
1005 if (drmKeySessoin_ == nullptr) {
1006 WVLOG_E("[DRM]drmKeySessoin_ is nullptr!");
1007 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1008 }
1009 updateSessionPromiseId_ = promiseId;
1010 std::shared_ptr<SessionId> sessionId = GetSessionIdByEmeId(emeId);
1011 if (sessionId == nullptr) {
1012 if (callback_) {
1013 callback_->OnPromiseRejected(promiseId, "Invalid session in updateSession: " + emeId);
1014 }
1015 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1016 }
1017 std::shared_ptr<SessionInfo> info = GetSessionInfo(sessionId);
1018 if (info == nullptr) {
1019 WVLOG_E("[DRM]DrmAdapterImpl::UpdateSession, info is nullprt, emeId: %{public}s", emeId.c_str());
1020 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1021 }
1022 bool isKeyRelease = info->KeyType() == static_cast<int32_t>(MediaKeyType::MEDIA_KEY_TYPE_RELEASE);
1023 int32_t mediaKeyIdLen = MAX_KEY_SET_ID_LEN;
1024 uint8_t mediaKeyId[MAX_KEY_SET_ID_LEN] = { 0x00 };
1025 if (isKeyRelease) {
1026 Drm_ErrCode ret = OH_MediaKeySession_ProcessOfflineReleaseResponse(
1027 drmKeySessoin_, sessionId->KeySetId(), sessionId->KeySetIdLen(), response.data(), response.size());
1028 WVLOG_I("[DRM]DrmAdapterImpl::OH_MediaKeySession_ProcessOfflineReleaseResponse, ret: %{public}d", ret);
1029 if (ret != DRM_ERR_OK) {
1030 if (callback_) {
1031 callback_->OnPromiseRejected(promiseId, "Update session failed.");
1032 }
1033 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1034 }
1035 } else {
1036 Drm_ErrCode ret = OH_MediaKeySession_ProcessMediaKeyResponse(
1037 drmKeySessoin_, response.data(), response.size(), mediaKeyId, &mediaKeyIdLen);
1038 WVLOG_I("[DRM]DrmAdapterImpl::OH_MediaKeySession_ProcessMediaKeyResponse result. ret: %{public}d", ret);
1039 if (ret != DRM_ERR_OK) {
1040 if (callback_) {
1041 callback_->OnPromiseRejected(promiseId, "Update session failed.");
1042 }
1043 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1044 }
1045 if (isWiseplay_ && callback_) {
1046 callback_->OnMediaLicenseReady(true);
1047 }
1048 }
1049 UpdateSessionResult(isKeyRelease, sessionId, mediaKeyId, mediaKeyIdLen);
1050 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
1051 }
1052
CloseSession(uint32_t promiseId,const std::string & emeId)1053 int32_t DrmAdapterImpl::CloseSession(uint32_t promiseId, const std::string& emeId)
1054 {
1055 WVLOG_I("[DRM]DrmAdapterImpl::CloseSession enter.");
1056 if (drmKeySystem_ == nullptr) {
1057 if (callback_) {
1058 callback_->OnPromiseRejected(promiseId, "closeSession() called when MediaDrm is null.");
1059 }
1060 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1061 }
1062
1063 std::shared_ptr<SessionId> sessionId = GetSessionIdByEmeId(emeId);
1064 if (sessionId == nullptr) {
1065 if (callback_) {
1066 callback_->OnPromiseRejected(promiseId, "Invalid sessionId in closeSession(): " + emeId);
1067 }
1068 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1069 }
1070
1071 RemoveSessionInfo(sessionId);
1072 if (callback_) {
1073 callback_->OnPromiseResolved(promiseId);
1074 callback_->OnSessionClosed(emeId);
1075 }
1076 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
1077 }
1078
RemoveSession(uint32_t promiseId,const std::string & emeId)1079 int32_t DrmAdapterImpl::RemoveSession(uint32_t promiseId, const std::string& emeId)
1080 {
1081 WVLOG_I("[DRM]DrmAdapterImpl::RemoveSession enter.");
1082 std::shared_ptr<SessionId> sessionId = GetSessionIdByEmeId(emeId);
1083 if (sessionId == nullptr) {
1084 if (callback_) {
1085 callback_->OnPromiseRejected(promiseId, "Session doesn't exist");
1086 }
1087 WVLOG_W("[DRM]DrmAdapterImpl::RemoveSession, Session doesn't exist.");
1088 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1089 }
1090
1091 std::shared_ptr<SessionInfo> sessionInfo = GetSessionInfo(sessionId);
1092 if (sessionInfo == nullptr) {
1093 callback_->OnPromiseRejected(promiseId, "SessionInfo doesn't exist");
1094 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1095 }
1096 if (sessionInfo->KeyType() == static_cast<int32_t>(MediaKeyType::MEDIA_KEY_TYPE_ONLINE)) {
1097 callback_->OnPromiseRejected(promiseId, "Removing temporary session isn't implemented");
1098 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1099 }
1100 removeSessionPromiseId_ = promiseId;
1101
1102 releaseEmeId_ = emeId;
1103 if (callback_) {
1104 callback_->UpdateEmeId(sessionId->EmeId(), true);
1105 }
1106 SetKeyType(sessionId, static_cast<int32_t>(MediaKeyType::MEDIA_KEY_TYPE_RELEASE));
1107 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
1108 }
1109
LoadSession(uint32_t promiseId,const std::string & sessionId)1110 int32_t DrmAdapterImpl::LoadSession(uint32_t promiseId, const std::string& sessionId)
1111 {
1112 WVLOG_I("[DRM]DrmAdapterImpl::LoadSession enter.");
1113 loadSessionPromiseId_ = promiseId;
1114 LoadSessionInfo(sessionId);
1115 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
1116 }
1117
ClearMediaKeys()1118 int32_t DrmAdapterImpl::ClearMediaKeys()
1119 {
1120 WVLOG_I("[DRM]DrmAdapterImpl::ClearMediaKeys enter.");
1121 if (drmKeySessoin_ == nullptr) {
1122 WVLOG_E("[DRM]drmKeySessoin_ is nullptr!");
1123 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1124 }
1125 Drm_ErrCode ret = OH_MediaKeySession_ClearMediaKeys(drmKeySessoin_);
1126 if (ret != DRM_ERR_OK) {
1127 WVLOG_E("[DRM]DrmAdapterImpl::ClearMediaKeys failed.");
1128 }
1129 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
1130 }
1131
GetSecurityLevel()1132 int32_t DrmAdapterImpl::GetSecurityLevel()
1133 {
1134 WVLOG_I("[DRM]DrmAdapterImpl::GetSecurityLevel enter.");
1135 if (drmKeySessoin_ == nullptr) {
1136 WVLOG_E("[DRM]drmKeySessoin_ is nullptr!");
1137 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1138 }
1139 DRM_ContentProtectionLevel levelData = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
1140 Drm_ErrCode ret = OH_MediaKeySession_GetContentProtectionLevel(drmKeySessoin_, &levelData);
1141 if (ret != DRM_ERR_OK) {
1142 WVLOG_E("[DRM]DrmAdapterImpl::GetSecurityLevel failed.");
1143 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1144 }
1145 int32_t securityLevel = GetSecurityLevelFromContentProtectionLevel(static_cast<int32_t>(levelData));
1146 return securityLevel;
1147 }
1148
RequireSecureDecoderModule(const std::string & mimeType,bool & status)1149 int32_t DrmAdapterImpl::RequireSecureDecoderModule(const std::string& mimeType, bool& status)
1150 {
1151 WVLOG_I("[DRM]DrmAdapterImpl::RequireSecureDecoderModule enter.");
1152 if (mimeType.empty()) {
1153 WVLOG_E("[DRM]mimeType is empty!");
1154 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1155 }
1156 if (drmKeySessoin_ == nullptr) {
1157 WVLOG_E("[DRM]drmKeySessoin_ is nullptr!");
1158 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1159 }
1160 bool stas = false;
1161 Drm_ErrCode ret = OH_MediaKeySession_RequireSecureDecoderModule(drmKeySessoin_, mimeType.c_str(), &stas);
1162 status = stas;
1163 if (ret != DRM_ERR_OK) {
1164 WVLOG_E("[DRM]DrmAdapterImpl::RequireSecureDecoderModule failed.");
1165 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1166 }
1167 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
1168 }
1169
GenerateMediaKeyRequest(const std::string & emeId,int32_t type,int32_t initDataLen,const std::vector<uint8_t> & initData,const std::string & mimeType,uint32_t promiseId)1170 int32_t DrmAdapterImpl::GenerateMediaKeyRequest(const std::string& emeId, int32_t type, int32_t initDataLen,
1171 const std::vector<uint8_t>& initData, const std::string& mimeType, uint32_t promiseId)
1172 {
1173 WVLOG_I("[DRM]DrmAdapterImpl::GenerateMediaKeyRequest enter, emeId = %{public}s", emeId.c_str());
1174 if (drmKeySystem_ == nullptr || drmKeySessoin_ == nullptr) {
1175 WVLOG_E("[DRM]GenerateMediaKeyRequest() called when drmKeySystem_ or drmKeySessoin_ is null.");
1176 if (callback_) {
1177 callback_->OnPromiseRejected(promiseId, "DrmKeySystem released previously.");
1178 }
1179 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1180 }
1181
1182 std::shared_ptr<SessionId> sessionId = SessionId::CreateSessionId(emeId);
1183 DRM_MediaKeyRequestInfo info;
1184 DRM_MediaKeyRequest mediaKeyRequest;
1185 info.type = static_cast<DRM_MediaKeyType>(type);
1186 info.initDataLen = initDataLen;
1187 info.optionsCount = 0;
1188 if (memcpy_s(info.mimeType, MAX_MIMETYPE_LEN, mimeType.c_str(), mimeType.length()) != 0) {
1189 WVLOG_E("[DRM]memcpy_s failed with error.");
1190 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1191 }
1192 if (memcpy_s(info.initData, MAX_INIT_DATA_LEN, initData.data(), initData.size()) != 0) {
1193 WVLOG_E("[DRM]memcpy_s failed with error.");
1194 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1195 }
1196 if (callback_) {
1197 callback_->UpdateMimeType(mimeType, type);
1198 }
1199
1200 int32_t iRet = CreateMediaKeySession();
1201 if (iRet != 0) {
1202 WVLOG_E("[DRM]OH_MediaKeySystem_CreateMediaKeySession failed.");
1203 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1204 }
1205 Drm_ErrCode ret = OH_MediaKeySession_GenerateMediaKeyRequest(drmKeySessoin_, &info, &mediaKeyRequest);
1206 WVLOG_I("[DRM]DrmAdapterImpl::OH_MediaKeySession_GenerateMediaKeyRequest, ret = %{public}d", ret);
1207 if (ret != DRM_ERR_OK) {
1208 if (callback_) {
1209 callback_->OnPromiseRejected(promiseId, "Generate request failed.");
1210 }
1211 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1212 }
1213 int32_t requestType = static_cast<int32_t>(mediaKeyRequest.type);
1214 std::vector<uint8_t> requestData;
1215 requestData.insert(requestData.begin(), mediaKeyRequest.data, mediaKeyRequest.data + mediaKeyRequest.dataLen);
1216 if (callback_) {
1217 callback_->UpdateEmeId(sessionId->EmeId(), false);
1218 callback_->OnPromiseResolvedWithSession(promiseId, emeId);
1219 callback_->OnSessionMessage(emeId, requestType, requestData);
1220 }
1221 PutSessionInfo(sessionId, mimeType, type);
1222 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
1223 }
1224
1225 // private
PutSessionInfo(std::shared_ptr<SessionId> sessionId,const std::string & mimeType,int32_t type)1226 void DrmAdapterImpl::PutSessionInfo(std::shared_ptr<SessionId> sessionId, const std::string& mimeType, int32_t type)
1227 {
1228 if (sessionId == nullptr) {
1229 return;
1230 }
1231 std::shared_ptr<SessionInfo> info = std::make_shared<SessionInfo>(sessionId, mimeType, type);
1232 WVLOG_I("[DRM]DrmAdapterImpl::PutSessionInfo, emeId = %{public}s", sessionId->EmeId().c_str());
1233 emeSessionInfoMap_[sessionId->EmeId()] = info;
1234 }
1235
GetSessionInfo(std::shared_ptr<SessionId> sessionId)1236 std::shared_ptr<SessionInfo> DrmAdapterImpl::GetSessionInfo(std::shared_ptr<SessionId> sessionId)
1237 {
1238 if (sessionId == nullptr) {
1239 WVLOG_I("[DRM]DrmAdapterImpl::GetSessionInfo, sessionId is nullptr.");
1240 return nullptr;
1241 }
1242 WVLOG_I("[DRM]DrmAdapterImpl::GetSessionInfo, emeId = %{public}s", sessionId->EmeId().c_str());
1243 auto iter = emeSessionInfoMap_.find(sessionId->EmeId());
1244 if (iter != emeSessionInfoMap_.end()) {
1245 WVLOG_I("[DRM]DrmAdapterImpl::GetSessionInfo, find.");
1246 return iter->second;
1247 }
1248 WVLOG_I("[DRM]DrmAdapterImpl::GetSessionInfo, ret is nullptr.");
1249 return nullptr;
1250 }
1251
GetSessionIdByEmeId(const std::string & emeId)1252 std::shared_ptr<SessionId> DrmAdapterImpl::GetSessionIdByEmeId(const std::string& emeId)
1253 {
1254 auto iter = emeSessionInfoMap_.find(emeId);
1255 if (iter != emeSessionInfoMap_.end()) {
1256 std::shared_ptr<SessionInfo> info = iter->second;
1257 if (info != nullptr) {
1258 return info->GetSessionId();
1259 }
1260 }
1261 return nullptr;
1262 }
1263
RemoveSessionInfo(std::shared_ptr<SessionId> sessionId)1264 void DrmAdapterImpl::RemoveSessionInfo(std::shared_ptr<SessionId> sessionId)
1265 {
1266 WVLOG_I("[DRM]DrmAdapterImpl::RemoveSessionInfo.");
1267 /**
1268 * Remove session and related infomration from memory, but doesn't touch
1269 * persistent storage.
1270 */
1271 if (sessionId == nullptr) {
1272 return;
1273 }
1274 std::shared_ptr<SessionInfo> info = GetSessionInfo(sessionId);
1275 if (info != nullptr) {
1276 emeSessionInfoMap_.erase(sessionId->EmeId());
1277 }
1278 }
1279
LoadSessionInfo(const std::string & emeId)1280 void DrmAdapterImpl::LoadSessionInfo(const std::string& emeId)
1281 {
1282 WVLOG_I("[DRM]DrmAdapterImpl::LoadSessionInfo.");
1283 if (callback_) {
1284 callback_->OnStorageLoadInfo(emeId);
1285 }
1286 }
1287
LoadSessionWithLoadedStorage(std::shared_ptr<SessionId> sessionId,uint32_t promiseId)1288 void DrmAdapterImpl::LoadSessionWithLoadedStorage(std::shared_ptr<SessionId> sessionId, uint32_t promiseId)
1289 {
1290 WVLOG_I("[DRM]DrmAdapterImpl::LoadSessionWithLoadedStorage.");
1291 if (sessionId == nullptr) {
1292 return;
1293 }
1294 std::shared_ptr<SessionInfo> info = GetSessionInfo(sessionId);
1295 if (info == nullptr) {
1296 WVLOG_I("[DRM]DrmAdapterImpl::LoadSessionWithLoadedStorage, info is null.");
1297 return;
1298 }
1299 if (info->KeyType() == static_cast<int32_t>(MediaKeyType::MEDIA_KEY_TYPE_RELEASE)) {
1300 if (callback_) {
1301 callback_->OnPromiseResolvedWithSession(promiseId, sessionId->EmeId());
1302 std::vector<std::string> dummyKeyId;
1303 std::vector<uint32_t> dummyStatus;
1304 dummyKeyId.push_back("");
1305 dummyStatus.push_back(static_cast<uint32_t>(KeyStatus::KEY_STATUS_INTERNAL_ERROR));
1306 callback_->OnSessionKeysChange(sessionId->EmeId(), dummyKeyId, dummyStatus, false, true);
1307 }
1308 return;
1309 }
1310
1311 if (info->KeyType() != static_cast<int32_t>(MediaKeyType::MEDIA_KEY_TYPE_OFFLINE)) {
1312 return;
1313 }
1314
1315 if (drmKeySessoin_ != nullptr) {
1316 Drm_ErrCode ret =
1317 OH_MediaKeySession_RestoreOfflineMediaKeys(drmKeySessoin_, sessionId->KeySetId(), sessionId->KeySetIdLen());
1318 WVLOG_I("[DRM]DrmAdapterImpl::OH_MediaKeySession_RestoreOfflineMediaKeys, ret = %{public}d", ret);
1319 if (ret != DRM_ERR_OK) {
1320 ClearPersistentSessionInfoForLoadFail(sessionId);
1321 return;
1322 }
1323 if (callback_) {
1324 if (isWiseplay_) {
1325 callback_->OnMediaLicenseReady(true);
1326 }
1327 callback_->OnPromiseResolvedWithSession(promiseId, sessionId->EmeId());
1328 }
1329 }
1330 WVLOG_I("[DRM]DrmAdapterImpl::LoadSessionWithLoadedStorage.");
1331 }
1332
1333 // remove && release
SetKeyType(std::shared_ptr<SessionId> sessionId,int32_t keyType)1334 void DrmAdapterImpl::SetKeyType(std::shared_ptr<SessionId> sessionId, int32_t keyType)
1335 {
1336 WVLOG_I("[DRM]DrmAdapterImpl::SetKeyType.");
1337 std::shared_ptr<SessionInfo> info = GetSessionInfo(sessionId);
1338 if (info == nullptr) {
1339 return;
1340 }
1341 info->SetKeyType(keyType);
1342
1343 if (info->GetSessionId() != nullptr && info->GetSessionId()->KeySetId() != nullptr) {
1344 std::vector<uint8_t> keySetIdVec;
1345 keySetIdVec.insert(keySetIdVec.begin(), info->GetSessionId()->KeySetId(),
1346 info->GetSessionId()->KeySetId() + info->GetSessionId()->KeySetIdLen());
1347 if (callback_) {
1348 WVLOG_I("[DRM]DrmAdapterImpl::OnStorageSaveInfo.");
1349 callback_->OnStorageSaveInfo(keySetIdVec, info->MimeType(), sessionId->EmeId(), keyType);
1350 }
1351 }
1352 }
1353
1354 // update
SetKeySetId(std::shared_ptr<SessionId> sessionId,uint8_t * mediaKeyId,int32_t mediaKeyIdLen)1355 void DrmAdapterImpl::SetKeySetId(std::shared_ptr<SessionId> sessionId, uint8_t* mediaKeyId, int32_t mediaKeyIdLen)
1356 {
1357 WVLOG_I("[DRM]DrmAdapterImpl::SetKeySetId.");
1358 if (sessionId == nullptr) {
1359 HandleKeyUpdatedCallback(updateSessionPromiseId_, false);
1360 return;
1361 }
1362 sessionId->SetKeySetId(mediaKeyId, mediaKeyIdLen);
1363 if (callback_) {
1364 std::shared_ptr<SessionInfo> info = GetSessionInfo(sessionId);
1365 if (info) {
1366 std::vector<uint8_t> keySetIdVec;
1367 keySetIdVec.insert(keySetIdVec.begin(), info->GetSessionId()->KeySetId(),
1368 info->GetSessionId()->KeySetId() + info->GetSessionId()->KeySetIdLen());
1369 callback_->OnStorageSaveInfo(keySetIdVec, info->MimeType(), sessionId->EmeId(), info->KeyType());
1370 }
1371 } else {
1372 HandleKeyUpdatedCallback(updateSessionPromiseId_, false);
1373 }
1374 }
1375
ClearPersistentSessionInfoFroKeyRelease(std::shared_ptr<SessionId> sessionId)1376 void DrmAdapterImpl::ClearPersistentSessionInfoFroKeyRelease(std::shared_ptr<SessionId> sessionId)
1377 {
1378 WVLOG_I("[DRM]DrmAdapterImpl::ClearPersistentSessionInfoFroKeyRelease.");
1379 if (sessionId != nullptr) {
1380 sessionId->SetKeySetId(nullptr, 0);
1381 if (callback_) {
1382 WVLOG_I("[DRM]OnStorageClearInfoForKeyRelease.");
1383 callback_->OnStorageClearInfoForKeyRelease(sessionId->EmeId());
1384 }
1385 }
1386 }
1387
ClearPersistentSessionInfoForLoadFail(std::shared_ptr<SessionId> sessionId)1388 void DrmAdapterImpl::ClearPersistentSessionInfoForLoadFail(std::shared_ptr<SessionId> sessionId)
1389 {
1390 WVLOG_I("[DRM]DrmAdapterImpl::ClearPersistentSessionInfoFroKeyRelease.");
1391 if (sessionId != nullptr) {
1392 sessionId->SetKeySetId(nullptr, 0);
1393 if (callback_) {
1394 WVLOG_I("[DRM]OnStorageClearInfoForLoadFail.");
1395 callback_->OnStorageClearInfoForLoadFail(sessionId->EmeId());
1396 }
1397 }
1398 }
1399
HandleKeyUpdatedCallback(uint32_t promiseId,bool result)1400 void DrmAdapterImpl::HandleKeyUpdatedCallback(uint32_t promiseId, bool result)
1401 {
1402 WVLOG_I("[DRM]DrmAdapterImpl::HandleKeyUpdatedCallback, result: %{public}d", result);
1403 if (callback_) {
1404 if (!result) {
1405 callback_->OnPromiseRejected(promiseId, "failed to update key after response accepted");
1406 return;
1407 }
1408 callback_->OnPromiseResolved(promiseId);
1409 }
1410 }
1411 } // namespace OHOS::NWeb