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 #ifndef OHOS_DRM_MEDIA_KEY_SYSTEMP_IMPL_H
17 #define OHOS_DRM_MEDIA_KEY_SYSTEMP_IMPL_H
18 
19 #include "ipc_skeleton.h"
20 #include "iservice_registry.h"
21 #include "nocopyable.h"
22 #include "system_ability_definition.h"
23 #include "drm_log.h"
24 #include "drm_death_recipient.h"
25 #include "key_session_impl.h"
26 #include "drm_listener_stub.h"
27 #include "i_mediakeysystem_service.h"
28 #include "mediakeysystem_service_callback_stub.h"
29 
30 namespace OHOS {
31 namespace DrmStandard {
32 namespace MediaKeySystemEvent {
33 const std::string EVENT_STR_PROVISION_REQUIRED = "keySystemRequired";
34 }
35 class MediaKeySystemImplCallback : public RefBase {
36 public:
37     MediaKeySystemImplCallback() = default;
38     virtual ~MediaKeySystemImplCallback() = default;
39     virtual void SendEvent(const std::string &event, int32_t extra, const std::vector<uint8_t> &data) = 0;
40 };
41 
42 class MediaKeySystemImpl : public RefBase {
43 public:
44     explicit MediaKeySystemImpl(sptr<IMediaKeySystemService> &mediaKeysystem);
45     ~MediaKeySystemImpl();
46 
47     int32_t SetConfigurationString(std::string &configName, std::string &value);
48     int32_t GetConfigurationString(std::string &configName, std::string &value);
49     int32_t SetConfigurationByteArray(std::string &configName, std::vector<uint8_t> &value);
50     int32_t GetConfigurationByteArray(std::string &configName, std::vector<uint8_t> &value);
51     int32_t CreateMediaKeySession(IMediaKeySessionService::ContentProtectionLevel securityLevel,
52         sptr<MediaKeySessionImpl> *keySessionImpl);
53     int32_t GetStatistics(std::vector<IMediaKeySystemService::MetircKeyValue> &metrics);
54     int32_t GetMaxContentProtectionLevel(IMediaKeySessionService::ContentProtectionLevel *securityLevel);
55 
56     int32_t GenerateKeySystemRequest(std::vector<uint8_t> &request, std::string &defaultUrl);
57     int32_t ProcessKeySystemResponse(const std::vector<uint8_t> &response);
58 
59     int32_t GetOfflineMediaKeyIds(std::vector<std::vector<uint8_t>> &licenseIds);
60     int32_t GetOfflineMediaKeyStatus(std::vector<uint8_t> &licenseId,
61         IMediaKeySessionService::OfflineMediaKeyStatus &status);
62     int32_t ClearOfflineMediaKeys(std::vector<uint8_t> &licenseId);
63 
64     int32_t GetCertificateStatus(IMediaKeySystemService::CertificateStatus *certStatus);
65     sptr<MediaKeySystemImplCallback> GetApplicationCallback();
66     int32_t SetCallback(const sptr<MediaKeySystemImplCallback> &callback);
67     int32_t Release();
68 
69 private:
70     void MediaKeySystemServerDied(pid_t pid);
71     std::recursive_mutex mutex_;
72     sptr<IMediaKeySystemService> serviceProxy_;
73     sptr<MediaKeySystemImplCallback> mediaKeySystemApplicationCallback_;
74     sptr<IMediaKeySystemServiceCallback> serviceCallback_;
75     sptr<DrmDeathRecipient> deathRecipient_ = nullptr;
76 };
77 
78 class MediaKeySystemCallback : public MediaKeySystemServiceCallbackStub {
79 public:
MediaKeySystemCallback()80     MediaKeySystemCallback() : systemImpl_(nullptr)
81     {
82         InitEventMap();
83     };
MediaKeySystemCallback(MediaKeySystemImpl * systemImpl)84     explicit MediaKeySystemCallback(MediaKeySystemImpl *systemImpl) : systemImpl_(systemImpl)
85     {
86         InitEventMap();
87     };
88     ~MediaKeySystemCallback();
89     void InitEventMap();
90     std::string GetEventName(DrmEventType event);
91     int32_t SendEvent(DrmEventType event, int32_t extra, const std::vector<uint8_t> &data) override;
92 
93 private:
94     std::recursive_mutex mutex_;
95     MediaKeySystemImpl *systemImpl_;
96     std::unordered_map<int32_t, std::string> eventMap_;
97 };
98 } // DrmStandard
99 } // OHOS
100 #endif // OHOS_DRM_MEDIA_KEY_SYSTEMP_IMPL_H
101