1 /*
2  * Copyright (c) 2023 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 <unordered_set>
17 #include <securec.h>
18 #include "system_ability_definition.h"
19 #include "ipc_skeleton.h"
20 #include "iservice_registry.h"
21 #include "drm_dfx_utils.h"
22 #include "drm_host_manager.h"
23 #include "drm_log.h"
24 #include "drm_trace.h"
25 #include "drm_error_code.h"
26 #include "key_session_service.h"
27 #include "mediakeysystem_service.h"
28 #include "hitrace/tracechain.h"
29 
30 namespace OHOS {
31 namespace DrmStandard {
32 using namespace OHOS::HiviewDFX;
MediaKeySystemService(sptr<OHOS::HDI::Drm::V1_0::IMediaKeySystem> hdiKeySystem)33 MediaKeySystemService::MediaKeySystemService(sptr<OHOS::HDI::Drm::V1_0::IMediaKeySystem> hdiKeySystem)
34 {
35     DRM_INFO_LOG("MediaKeySystemService 0x%{public}06" PRIXPTR " Instances create.", FAKE_POINTER(this));
36     keySystemOperatoersCallback_ = nullptr;
37     hdiKeySystem_ = hdiKeySystem;
38 }
39 
MediaKeySystemService(sptr<OHOS::HDI::Drm::V1_0::IMediaKeySystem> hdiKeySystem,StatisticsInfo statisticsInfo)40 MediaKeySystemService::MediaKeySystemService(sptr<OHOS::HDI::Drm::V1_0::IMediaKeySystem> hdiKeySystem,
41     StatisticsInfo statisticsInfo)
42 {
43     DRM_INFO_LOG("MediaKeySystemService 0x%{public}06" PRIXPTR " Instances create.", FAKE_POINTER(this));
44     keySystemOperatoersCallback_ = nullptr;
45     hdiKeySystem_ = hdiKeySystem;
46     statisticsInfo_ = statisticsInfo;
47 }
48 
~MediaKeySystemService()49 MediaKeySystemService::~MediaKeySystemService()
50 {
51     DRM_INFO_LOG("~MediaKeySystemService 0x%{public}06" PRIXPTR " Instances destroy.", FAKE_POINTER(this));
52     {
53         std::lock_guard<std::recursive_mutex> lock(callbackMutex_);
54         keySystemOperatoersCallback_ = nullptr;
55     }
56     std::lock_guard<std::recursive_mutex> lock(mutex_);
57     if (hdiKeySystem_ != nullptr) {
58         DRM_ERR_LOG("hdiKeySystem != nullptr");
59     }
60 }
61 
OnDrmPluginDied(std::string & name)62 void MediaKeySystemService::OnDrmPluginDied(std::string &name)
63 {}
64 
CloseMediaKeySystemServiceByCallback()65 int32_t MediaKeySystemService::CloseMediaKeySystemServiceByCallback()
66 {
67     DRM_INFO_LOG("CloseMediaKeySystemServiceByCallback enter.");
68     int32_t currentPid = IPCSkeleton::GetCallingPid();
69     DRM_DEBUG_LOG("MediaKeySystemService GetCallingPID: %{public}d", currentPid);
70     {
71         std::lock_guard<std::mutex> lock(sessionsSetMutex_);
72         for (auto it = sessionsSet_.begin(); it != sessionsSet_.end();) {
73             if ((*it) != nullptr) {
74                 (*it)->CloseMediaKeySessionServiceByCallback();
75             }
76             it = sessionsSet_.erase(it);
77         }
78         sessionsSet_.clear();
79     }
80     {
81         std::lock_guard<std::recursive_mutex> lock(callbackMutex_);
82         keySystemOperatoersCallback_ = nullptr;
83     }
84     std::lock_guard<std::recursive_mutex> lock(mutex_);
85     // release itself
86     if (hdiKeySystem_ != nullptr) {
87         DRM_ERR_LOG("hdiKeySystem_ CloseHdiMediaKeySession");
88         hdiKeySystem_->Destroy();
89         hdiKeySystem_ = nullptr;
90     }
91     return DRM_OK;
92 }
93 
Release()94 int32_t MediaKeySystemService::Release()
95 {
96     DRM_INFO_LOG("Release enter.");
97     int32_t currentPid = IPCSkeleton::GetCallingPid();
98     DRM_DEBUG_LOG("MediaKeySystemService GetCallingPID: %{public}d", currentPid);
99     std::lock_guard<std::recursive_mutex> lock(callbackMutex_);
100     if (keySystemOperatoersCallback_ != nullptr) {
101         keySystemOperatoersCallback_->CloseMediaKeySystemService(this);
102     }
103     return DRM_OK;
104 }
105 
SetMediaKeySystemServiceOperatorsCallback(wptr<IMediaKeySystemServiceOperatorsCallback> callback)106 int32_t MediaKeySystemService::SetMediaKeySystemServiceOperatorsCallback(
107     wptr<IMediaKeySystemServiceOperatorsCallback> callback)
108 {
109     std::lock_guard<std::recursive_mutex> lock(callbackMutex_);
110     if (callback.promote() == nullptr) {
111         DRM_ERR_LOG("SetMediaKeySystemServiceOperatorsCallback callback is null");
112         return DRM_INVALID_ARG;
113     }
114     keySystemOperatoersCallback_ = callback;
115     return DRM_OK;
116 }
117 
GenerateKeySystemRequest(std::vector<uint8_t> & request,std::string & defaultUrl)118 int32_t MediaKeySystemService::GenerateKeySystemRequest(std::vector<uint8_t> &request, std::string &defaultUrl)
119 {
120     DrmTrace trace("MediaKeySystemService::GenerateKeySystemRequest");
121     DRM_INFO_LOG("GenerateKeySystemRequest enter.");
122     int32_t ret = DRM_OK;
123     std::lock_guard<std::recursive_mutex> lock(mutex_);
124     DRM_CHECK_AND_RETURN_RET_LOG(hdiKeySystem_ != nullptr, DRM_SERVICE_FATAL_ERROR,
125         "hdiKeySystem_ is nullptr!");
126     auto timeBefore = std::chrono::system_clock::now();
127     ret = hdiKeySystem_->GenerateKeySystemRequest(defaultUrl, request);
128     generationDuration_ = CalculateTimeDiff(timeBefore, std::chrono::system_clock::now());
129     if (ret != DRM_OK) {
130         DRM_ERR_LOG("GenerateKeySystemRequest failed.");
131         ReportFaultEvent(ret, "GenerateKeySystemRequest failed", "");
132         generationResult_ = "failed";
133         return ret;
134     }
135     generationResult_ = "success";
136     return ret;
137 }
138 
ProcessKeySystemResponse(const std::vector<uint8_t> & response)139 int32_t MediaKeySystemService::ProcessKeySystemResponse(const std::vector<uint8_t> &response)
140 {
141     DrmTrace trace("MediaKeySystemService::ProcessKeySystemResponse");
142     DRM_INFO_LOG("ProcessKeySystemResponse enter.");
143     int32_t ret = DRM_OK;
144     std::lock_guard<std::recursive_mutex> lock(mutex_);
145     DRM_CHECK_AND_RETURN_RET_LOG(hdiKeySystem_ != nullptr, DRM_SERVICE_FATAL_ERROR,
146         "hdiKeySystem_ is nullptr!");
147     auto timeBefore = std::chrono::system_clock::now();
148     ret = hdiKeySystem_->ProcessKeySystemResponse(response);
149     uint32_t processDuration = CalculateTimeDiff(timeBefore, std::chrono::system_clock::now());
150     if (ret != DRM_OK) {
151         DRM_ERR_LOG("ProcessKeySystemResponse failed.");
152         std::string responseString = std::string(reinterpret_cast<const char*>(response.data()), response.size());
153         ReportFaultEvent(ret, "ProcessKeySystemResponse failed", responseString);
154         return ret;
155     }
156     struct DownLoadInfo downLoadInfo = InitDownLoadInfo(generationDuration_, generationResult_, processDuration,
157         "success");
158     ReportCertificateBehaviorEvent(statisticsInfo_, downLoadInfo, 0, 0, "");
159     return ret;
160 }
161 
SetConfigurationString(std::string & configName,std::string & value)162 int32_t MediaKeySystemService::SetConfigurationString(std::string &configName, std::string &value)
163 {
164     DRM_INFO_LOG("SetConfiguration enter, configName:%{public}s, value:%{public}s.",
165         configName.c_str(), value.c_str());
166     DRM_CHECK_AND_RETURN_RET_LOG(configName != "bundleName", DRM_INVALID_ARG,
167         "configuration name is not support");
168     int32_t ret = DRM_OK;
169     std::lock_guard<std::recursive_mutex> lock(mutex_);
170     DRM_CHECK_AND_RETURN_RET_LOG(hdiKeySystem_ != nullptr, DRM_SERVICE_FATAL_ERROR,
171         "hdiKeySystem_ is nullptr!");
172     ret = hdiKeySystem_->SetConfigurationString(configName, value);
173     if (ret != DRM_OK) {
174         DRM_ERR_LOG("SetConfiguration failed.");
175         return ret;
176     }
177     return ret;
178 }
179 
GetConfigurationString(std::string & configName,std::string & value)180 int32_t MediaKeySystemService::GetConfigurationString(std::string &configName, std::string &value)
181 {
182     DRM_INFO_LOG("GetConfiguration enter, configName:%{public}s.", configName.c_str());
183     DRM_CHECK_AND_RETURN_RET_LOG(configName != "bundleName", DRM_INVALID_ARG,
184         "configuration name is not support");
185     int32_t ret = DRM_OK;
186     std::lock_guard<std::recursive_mutex> lock(mutex_);
187     DRM_CHECK_AND_RETURN_RET_LOG(hdiKeySystem_ != nullptr, DRM_SERVICE_FATAL_ERROR,
188         "hdiKeySystem_ is nullptr!");
189 
190     ret = hdiKeySystem_->GetConfigurationString(configName, value);
191     if (ret != DRM_OK) {
192         DRM_ERR_LOG("GetConfiguration failed.");
193         return ret;
194     }
195     return ret;
196 }
197 
SetConfigurationByteArray(std::string & configName,std::vector<uint8_t> & value)198 int32_t MediaKeySystemService::SetConfigurationByteArray(std::string &configName, std::vector<uint8_t> &value)
199 {
200     DRM_INFO_LOG("SetConfiguration enter, configName:%{public}s.", configName.c_str());
201     DRM_CHECK_AND_RETURN_RET_LOG(configName != "bundleName", DRM_INVALID_ARG,
202         "configuration name is not support");
203     int32_t ret = DRM_OK;
204     std::lock_guard<std::recursive_mutex> lock(mutex_);
205     DRM_CHECK_AND_RETURN_RET_LOG(hdiKeySystem_ != nullptr, DRM_SERVICE_FATAL_ERROR,
206         "hdiKeySystem_ is nullptr!");
207     std::vector<uint8_t> valueVec;
208     valueVec.assign(value.begin(), value.end());
209     ret = hdiKeySystem_->SetConfigurationByteArray(configName, valueVec);
210     if (ret != DRM_OK) {
211         DRM_ERR_LOG("SetConfiguration failed.");
212         return ret;
213     }
214     return ret;
215 }
216 
GetConfigurationByteArray(std::string & configName,std::vector<uint8_t> & value)217 int32_t MediaKeySystemService::GetConfigurationByteArray(std::string &configName, std::vector<uint8_t> &value)
218 {
219     DRM_INFO_LOG("GetConfiguration enter, configName:%{public}s.", configName.c_str());
220     DRM_CHECK_AND_RETURN_RET_LOG(configName != "bundleName", DRM_INVALID_ARG,
221         "configuration name is not support");
222     int32_t ret = DRM_OK;
223     std::lock_guard<std::recursive_mutex> lock(mutex_);
224     DRM_CHECK_AND_RETURN_RET_LOG(hdiKeySystem_ != nullptr, DRM_SERVICE_FATAL_ERROR,
225         "hdiKeySystem_ is nullptr!");
226     std::vector<uint8_t> valueVec;
227     ret = hdiKeySystem_->GetConfigurationByteArray(configName, valueVec);
228     if (ret != DRM_OK) {
229         DRM_ERR_LOG("GetConfiguration failed.");
230         return ret;
231     }
232     value.assign(valueVec.begin(), valueVec.end());
233     return ret;
234 }
235 
CreateMediaKeySession(IMediaKeySessionService::ContentProtectionLevel securityLevel,sptr<IMediaKeySessionService> & keySessionProxy)236 int32_t MediaKeySystemService::CreateMediaKeySession(IMediaKeySessionService::ContentProtectionLevel securityLevel,
237     sptr<IMediaKeySessionService> &keySessionProxy)
238 {
239     DrmTrace trace("MediaKeySystemService::CreateMediaKeySession");
240     DRM_INFO_LOG("CreateMediaKeySession enter, securityLevel:%{public}d.", securityLevel);
241     int32_t ret = DRM_OK;
242     std::lock_guard<std::recursive_mutex> lock(mutex_);
243     DRM_CHECK_AND_RETURN_RET_LOG(hdiKeySystem_ != nullptr, DRM_SERVICE_FATAL_ERROR,
244         "hdiKeySystem_ is nullptr!");
245     sptr<MediaKeySessionService> keySessionService = nullptr;
246     sptr<OHOS::HDI::Drm::V1_0::IMediaKeySession> hdiMediaKeySession = nullptr;
247     if (currentKeySessionNumber >= KEY_SESSION_MAX_NUMBER) {
248         DRM_ERR_LOG("The number of MediaKeySession is greater than 64");
249         return DRM_MAX_SESSION_NUM_REACHED;
250     }
251     ret = hdiKeySystem_->CreateMediaKeySession((OHOS::HDI::Drm::V1_0::ContentProtectionLevel)securityLevel,
252         hdiMediaKeySession);
253     if (hdiMediaKeySession == nullptr) {
254         DRM_ERR_LOG("hdiKeySystem_ CreateMediaKeySession failed.");
255         ReportFaultEvent(DRM_SERVICE_ERROR, "CreateMediaKeySession failed", "");
256         return DRM_SERVICE_ERROR;
257     }
258     keySessionService = new (std::nothrow) MediaKeySessionService(hdiMediaKeySession, statisticsInfo_);
259     if (keySessionService == nullptr) {
260         DRM_ERR_LOG("CreateMediaKeySession allocation failed.");
261         ReportFaultEvent(DRM_ALLOC_ERROR, "CreateMediaKeySession failed", "");
262         return DRM_ALLOC_ERROR;
263     }
264     keySessionService->SetMediaKeySessionServiceOperatorsCallback(this);
265 
266     int32_t pid = IPCSkeleton::GetCallingPid();
267     DRM_DEBUG_LOG("MediaKeySystemService CreateMediaKeySession GetCallingPID: %{public}d", pid);
268     {
269         std::lock_guard<std::mutex> lock(sessionsSetMutex_);
270         sessionsSet_.insert(keySessionService);
271     }
272 
273     DRM_DEBUG_LOG("0x%{public}06" PRIXPTR " is Current keySessionService", FAKE_POINTER(keySessionService.GetRefPtr()));
274     keySessionProxy = keySessionService;
275     currentKeySessionNumber++;
276     return ret;
277 }
278 
SetBundleName()279 int32_t MediaKeySystemService::SetBundleName()
280 {
281     DRM_INFO_LOG("SetBundleName");
282     int32_t ret = DRM_OK;
283     std::lock_guard<std::recursive_mutex> lock(mutex_);
284     DRM_CHECK_AND_RETURN_RET_LOG(hdiKeySystem_ != nullptr, DRM_SERVICE_FATAL_ERROR,
285         "hdiKeySystem_ is nullptr!");
286     DRM_CHECK_AND_RETURN_RET_LOG(!statisticsInfo_.bundleName.empty(), DRM_UNKNOWN_ERROR,
287         "bundle name is empty!");
288     ret = hdiKeySystem_->SetConfigurationString("bundleName", statisticsInfo_.bundleName);
289     DRM_CHECK_AND_RETURN_RET_LOG(ret == DRM_OK, ret, "SetBundleName failed.");
290     return ret;
291 }
292 
CloseMediaKeySessionService(sptr<MediaKeySessionService> sessionService)293 int32_t MediaKeySystemService::CloseMediaKeySessionService(sptr<MediaKeySessionService> sessionService)
294 {
295     DRM_INFO_LOG("CloseMediaKeySessionService enter.");
296     int32_t ret = DRM_OK;
297     int32_t currentPid = IPCSkeleton::GetCallingPid();
298     DRM_DEBUG_LOG("MediaKeySystemService GetCallingPID: %{public}d", currentPid);
299     std::lock_guard<std::recursive_mutex> lock(mutex_);
300 
301     if (sessionService != nullptr) {
302         DRM_INFO_LOG("MediaKeySystemService call CloseMediaKeySessionServiceByCallback ");
303         ret = sessionService->CloseMediaKeySessionServiceByCallback();
304     }
305     {
306         std::lock_guard<std::mutex> lock(sessionsSetMutex_);
307         sessionsSet_.erase(sessionService);
308         if (currentKeySessionNumber > 0) {
309             currentKeySessionNumber--;
310         }
311     }
312     sessionService = nullptr;
313     return ret;
314 }
315 
GetStatistics(std::vector<IMediaKeySystemService::MetircKeyValue> & metrics)316 int32_t MediaKeySystemService::GetStatistics(std::vector<IMediaKeySystemService::MetircKeyValue> &metrics)
317 {
318     DRM_INFO_LOG("GetStatistics enter");
319     int32_t ret = DRM_OK;
320     std::lock_guard<std::recursive_mutex> lock(mutex_);
321     DRM_CHECK_AND_RETURN_RET_LOG(hdiKeySystem_ != nullptr, DRM_SERVICE_FATAL_ERROR,
322         "hdiKeySystem_ is nullptr!");
323     std::map<std::string, std::string> tmpStatistics;
324     ret = hdiKeySystem_->GetStatistics(tmpStatistics);
325     if (ret != DRM_OK) {
326         DRM_ERR_LOG("GetStatistics failed.");
327         return ret;
328     }
329     for (auto it = tmpStatistics.begin(); it != tmpStatistics.end(); it++) {
330         IMediaKeySystemService::MetircKeyValue keyValue;
331         keyValue.name = it->first;
332         keyValue.value = it->second;
333         metrics.push_back(keyValue);
334     }
335     if (metrics.size() == 0) {
336         DRM_ERR_LOG("GetStatistics failed.");
337         return DRM_ERROR;
338     }
339     return ret;
340 }
341 
GetMaxContentProtectionLevel(IMediaKeySessionService::ContentProtectionLevel * securityLevel)342 int32_t MediaKeySystemService::GetMaxContentProtectionLevel(
343     IMediaKeySessionService::ContentProtectionLevel *securityLevel)
344 {
345     DRM_INFO_LOG("GetMaxContentProtectionLevel enter.");
346     int32_t ret = DRM_OK;
347     OHOS::HDI::Drm::V1_0::ContentProtectionLevel level;
348     std::lock_guard<std::recursive_mutex> lock(mutex_);
349     DRM_CHECK_AND_RETURN_RET_LOG(hdiKeySystem_ != nullptr, DRM_SERVICE_FATAL_ERROR,
350         "hdiKeySystem_ is nullptr!");
351 
352     ret = hdiKeySystem_->GetMaxContentProtectionLevel(level);
353     if (ret != DRM_OK) {
354         DRM_ERR_LOG("GetMaxContentProtectionLevel failed.");
355         return ret;
356     }
357     *securityLevel = (IMediaKeySessionService::ContentProtectionLevel)level;
358     return ret;
359 }
360 
GetCertificateStatus(IMediaKeySystemService::CertificateStatus * certStatus)361 int32_t MediaKeySystemService::GetCertificateStatus(IMediaKeySystemService::CertificateStatus *certStatus)
362 {
363     DRM_INFO_LOG("GetCertificateStatus enter.");
364     int32_t ret = DRM_OK;
365     std::lock_guard<std::recursive_mutex> lock(mutex_);
366     DRM_CHECK_AND_RETURN_RET_LOG(hdiKeySystem_ != nullptr, DRM_SERVICE_FATAL_ERROR,
367         "hdiKeySystem_ is nullptr!");
368 
369     OHOS::HDI::Drm::V1_0::CertificateStatus tmpStatus;
370     ret = hdiKeySystem_->GetOemCertificateStatus(tmpStatus);
371     if (ret != DRM_OK) {
372         DRM_ERR_LOG("GetCertificateStatus failed.");
373         return ret;
374     }
375     *certStatus = (IMediaKeySystemService::CertificateStatus)tmpStatus;
376 
377     return ret;
378 }
379 
GetOfflineMediaKeyIds(std::vector<std::vector<uint8_t>> & licenseIds)380 int32_t MediaKeySystemService::GetOfflineMediaKeyIds(std::vector<std::vector<uint8_t>> &licenseIds)
381 {
382     DRM_INFO_LOG("GetOfflineMediaKeyIds enter.");
383     int32_t ret = DRM_OK;
384     std::lock_guard<std::recursive_mutex> lock(mutex_);
385     DRM_CHECK_AND_RETURN_RET_LOG(hdiKeySystem_ != nullptr, DRM_SERVICE_FATAL_ERROR,
386         "hdiKeySystem_ is nullptr!");
387 
388     ret = hdiKeySystem_->GetOfflineMediaKeyIds(licenseIds);
389     if (ret != DRM_OK) {
390         DRM_ERR_LOG("GetOfflineMediaKeyIds failed.");
391         return ret;
392     }
393 
394     return ret;
395 }
396 
GetOfflineMediaKeyStatus(std::vector<uint8_t> & licenseId,IMediaKeySessionService::OfflineMediaKeyStatus & status)397 int32_t MediaKeySystemService::GetOfflineMediaKeyStatus(std::vector<uint8_t> &licenseId,
398     IMediaKeySessionService::OfflineMediaKeyStatus &status)
399 {
400     DRM_INFO_LOG("GetOfflineMediaKeyStatus enter.");
401     int32_t ret = DRM_OK;
402     std::lock_guard<std::recursive_mutex> lock(mutex_);
403     DRM_CHECK_AND_RETURN_RET_LOG(hdiKeySystem_ != nullptr, DRM_SERVICE_FATAL_ERROR,
404         "hdiKeySystem_ is nullptr!");
405 
406     OHOS::HDI::Drm::V1_0::OfflineMediaKeyStatus offlineMediaKeyStatus;
407     ret = hdiKeySystem_->GetOfflineMediaKeyStatus(licenseId, offlineMediaKeyStatus);
408     if (ret != DRM_OK) {
409         DRM_ERR_LOG("GetOfflineMediaKeyStatus failed.");
410         return ret;
411     }
412     status = (IMediaKeySessionService::OfflineMediaKeyStatus)offlineMediaKeyStatus;
413 
414     return ret;
415 }
416 
ClearOfflineMediaKeys(std::vector<uint8_t> & licenseId)417 int32_t MediaKeySystemService::ClearOfflineMediaKeys(std::vector<uint8_t> &licenseId)
418 {
419     DRM_INFO_LOG("ClearOfflineMediaKeys enter.");
420     int32_t ret = DRM_OK;
421     std::lock_guard<std::recursive_mutex> lock(mutex_);
422     DRM_CHECK_AND_RETURN_RET_LOG(hdiKeySystem_ != nullptr, DRM_SERVICE_FATAL_ERROR,
423         "hdiKeySystem_ is nullptr!");
424 
425     ret = hdiKeySystem_->ClearOfflineMediaKeys(licenseId);
426     if (ret != DRM_OK) {
427         DRM_ERR_LOG("ClearOfflineMediaKeys failed.");
428         return ret;
429     }
430 
431     return ret;
432 }
433 
SetCallback(sptr<IMediaKeySystemServiceCallback> & callback)434 int32_t MediaKeySystemService::SetCallback(sptr<IMediaKeySystemServiceCallback> &callback)
435 {
436     DRM_INFO_LOG("SetCallback enter");
437     int32_t ret = DRM_ERROR;
438     std::lock_guard<std::recursive_mutex> lock(mutex_);
439     DRM_CHECK_AND_RETURN_RET_LOG(hdiKeySystem_ != nullptr, DRM_SERVICE_FATAL_ERROR,
440         "hdiKeySystem_ is nullptr!");
441     if (callback == nullptr) {
442         DRM_ERR_LOG("SetCallback callback is nullptr.");
443         return ret;
444     }
445     callback_ = callback;
446 
447     if (hdiKeySystem_ != nullptr) {
448         return hdiKeySystem_->SetCallback(this);
449     }
450     DRM_ERR_LOG("SetCallback hdiKeySystem_ is nullptr.");
451     return ret;
452 }
453 
SendEvent(OHOS::HDI::Drm::V1_0::EventType eventType,int32_t extra,const std::vector<uint8_t> & data)454 int32_t MediaKeySystemService::SendEvent(OHOS::HDI::Drm::V1_0::EventType eventType, int32_t extra,
455     const std::vector<uint8_t> &data)
456 {
457     DRM_INFO_LOG("SendEvent enter.");
458     DrmEventType event = static_cast<DrmEventType>(eventType);
459     std::lock_guard<std::recursive_mutex> lock(mutex_);
460     if (callback_ != nullptr) {
461         return callback_->SendEvent(event, extra, data);
462     }
463     DRM_INFO_LOG("SendEvent failed because callback is nullptr");
464     return DRM_OPERATION_NOT_ALLOWED;
465 }
466 
GetPluginName()467 std::string MediaKeySystemService::GetPluginName()
468 {
469     std::lock_guard<std::recursive_mutex> lock(mutex_);
470     return statisticsInfo_.pluginName;
471 }
472 
GetSessionsDumpInfo()473 std::string MediaKeySystemService::GetSessionsDumpInfo()
474 {
475     DRM_INFO_LOG("GetSessionsDumpInfo enter");
476     std::string dumpInfo;
477     std::lock_guard<std::mutex> lock(sessionsSetMutex_);
478     dumpInfo += "Total MediaKeySession Num: " + std::to_string(sessionsSet_.size()) + "\n";
479     uint32_t sessionNum = 0;
480     for (auto &session : sessionsSet_) {
481         sessionNum++;
482         dumpInfo += "#### MediaKeySession " + std::to_string(sessionNum) + " ####\n";
483         if (session != nullptr) {
484             dumpInfo += session->GetDecryptModuleDumpInfo();
485         }
486     }
487     return dumpInfo;
488 }
489 
getMediaKeySystem()490 sptr<OHOS::HDI::Drm::V1_0::IMediaKeySystem> MediaKeySystemService::getMediaKeySystem()
491 {
492     std::lock_guard<std::recursive_mutex> lock(mutex_);
493     return hdiKeySystem_;
494 }
495 
496 } // DrmStandard
497 } // OHOS