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