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