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_DRM_HOST_MANAGER_H
17 #define OHOS_DRM_DRM_HOST_MANAGER_H
18 
19 #include <refbase.h>
20 #include <iostream>
21 #include <memory>
22 #include <mutex>
23 #include <shared_mutex>
24 #include <string>
25 #include <vector>
26 #include <queue>
27 #include <condition_variable>
28 #include <thread>
29 #include "iservmgr_hdi.h"
30 #include "servmgr_hdi.h"
31 #include "idevmgr_hdi.h"
32 #include "hdf_device_class.h"
33 #include "i_mediakeysystem_service.h"
34 #include "iservstat_listener_hdi.h"
35 #include "v1_0/media_key_system_factory_proxy.h"
36 #include "v1_0/media_key_system_proxy.h"
37 
38 namespace OHOS {
39 namespace DrmStandard {
40 using namespace OHOS::HDI::Drm::V1_0;
41 using namespace OHOS::HDI;
42 using OHOS::HDI::DeviceManager::V1_0::IDeviceManager;
43 using OHOS::HDI::ServiceManager::V1_0::IServiceManager;
44 
45 enum ExtraInfo {
46     OEM_CERT_PROVISIONED_DONE = 0,
47     MEDIA_KEY_SYSTEM_ERROR,
48     GET_OEM_CERTIFICATE_ERROR,
49     GET_OEM_PROVISION_REQUEST_ERROR,
50     OEM_CERT_PROVISION_SERVER_ERROR,
51     OEM_CERT_PROVISIONED_ERROR,
52     EXIT_FOR_UNKNOWN_CAUSE,
53 };
54 
55 struct Message {
56     enum Type {
57         UnLoadOEMCertifaicateService
58     };
59     Type type;
60     std::string name;
61     ExtraInfo extraInfo;
MessageMessage62     Message(Type t, std::string pluginName, ExtraInfo info) : type(t), name(pluginName), extraInfo(info) {}
63 };
64 
65 #define OEM_CERTIFICATE_PATH "/system/lib64/oem_certificate_service/"
66 #define PLUGIN_LAZYLOAD_CONFIG_PATH "/etc/drm/drm_plugin_lazyloding.cfg"
67 
68 typedef void (*MediaKeySystemCallBack)(std::string &, ExtraInfo);
69 typedef int32_t (*QueryMediaKeySystemNameFuncType)(std::string &);
70 typedef int32_t (*SetMediaKeySystemFuncType)(sptr<OHOS::HDI::Drm::V1_0::IMediaKeySystem> &);
71 typedef bool (*IsProvisionRequiredFuncType)();
72 typedef int32_t (*ThreadExitNotifyFuncType)(MediaKeySystemCallBack);
73 typedef int32_t (*StartThreadFuncType)();
74 typedef void (*StopThreadFuncType)();
75 
76 class DrmHostManager : public virtual RefBase, public HDI::ServiceManager::V1_0::ServStatListenerStub {
77 public:
78     class StatusCallback {
79     public:
80         virtual ~StatusCallback() = default;
81         virtual void OnDrmPluginDied(std::string &name) = 0;
82     };
83     class DrmHostDeathRecipient : public IRemoteObject::DeathRecipient {
84     public:
85         explicit DrmHostDeathRecipient(const sptr<DrmHostManager>& drmHostManager,
86             std::string &name);
87         virtual ~DrmHostDeathRecipient();
88         void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
89     private:
90         wptr<DrmHostManager> drmHostManager_;
91         std::string name_;
92     };
93 
94     explicit DrmHostManager(StatusCallback *statusCallback);
95     ~DrmHostManager() override;
96 
97     int32_t Init(void);
98     void DeInit(void);
99     void OnReceive(const HDI::ServiceManager::V1_0::ServiceStatus &status) override;
100     int32_t IsMediaKeySystemSupported(std::string &name, bool *isSurpported);
101     int32_t IsMediaKeySystemSupported(std::string &name, std::string &mimeType, bool *isSurpported);
102     int32_t IsMediaKeySystemSupported(std::string &name, std::string &mimeType, int32_t securityLevel,
103         bool *isSurpported);
104     int32_t CreateMediaKeySystem(std::string &name, sptr<IMediaKeySystem> &hdiMediaKeySystem);
105     int32_t GetMediaKeySystems(std::map<std::string, std::string> &mediaKeySystemDescription);
106     int32_t GetMediaKeySystemUuid(std::string &name, std::string &uuid);
107     void ReleaseMediaKeySystem(sptr<IMediaKeySystem> &hdiMediaKeySystem);
108     void ClearDeathService(std::string &name);
109     void OnDrmPluginDied(std::string &name);
110 private:
111     static void UnLoadOEMCertifaicateService(std::string &name, ExtraInfo info);
112     int32_t InitGetMediaKeySystems();
113     void StopServiceThread();
114     void DelayedLazyUnLoad();
115     void ProcessMessage();
116     void ServiceThreadMain();
117     void GetOemLibraryPath(std::vector<std::string> &libsToLoad);
118     void OemCertificateManager();
119     int32_t ProcessLazyLoadInfomation(std::string &name, sptr<IMediaKeySystemFactory> &drmHostServieProxy);
120     int32_t ProcessLazyLoadPlugin(std::string &name, std::vector<std::string> &serviceName,
121         sptr<IDeviceManager> &deviceMgr, sptr<IServiceManager> &servmgr);
122     int32_t GetServices(std::string &name, bool *isSurpported, sptr<IMediaKeySystemFactory> &drmHostServieProxys);
123     void ReleaseHandleAndKeySystemMap(void *handle);
124     std::string StringTrim(const std::string& str);
125     int32_t LazyLoadPlugin(std::string &name, std::vector<std::string> &serviceName,
126     sptr<IDeviceManager> &deviceMgr, sptr<IServiceManager> &servmgr);
127     void parseLazyLoadService(std::ifstream& file, std::map<std::string, std::string>& lazyLoadPluginInfoMap);
128     int32_t LoadPluginInfo(const std::string& filePath);
129     void ReleaseSevices(sptr<IMediaKeySystemFactory> drmHostServieProxy);
130     void UnloadAllServices();
131 
132     StatusCallback *statusCallback_;
133     std::map<void *, sptr<IMediaKeySystem>> handleAndKeySystemMap;
134     std::thread serviceThread;
135     std::thread messageQueueThread;
136     bool serviceThreadRunning = false;
137     std::vector<void *> loadedLibs;
138     std::recursive_mutex drmHostMapMutex;
139     static std::mutex queueMutex;
140     static std::queue<Message> messageQueue;
141     static std::condition_variable cv;
142     std::map<std::string, void *> pluginNameAndHandleMap;
143     std::map<std::string, std::string> lazyLoadPluginInfoMap;
144     std::map<std::string, int32_t> lazyLoadPluginCountMap;
145     std::map<std::string, int32_t> lazyLoadPluginTimeoutMap;
146     std::map<std::string, std::string> mediaKeySystemDescription_;
147     std::map<sptr<IMediaKeySystem>, sptr<IMediaKeySystemFactory>> hdiMediaKeySystemAndFactoryMap;
148     std::map<sptr<IMediaKeySystemFactory>, std::string> hdiMediaKeySystemFactoryAndPluginNameMap;
149     std::map<sptr<IMediaKeySystemFactory>, sptr<DrmHostDeathRecipient>> drmHostDeathRecipientMap;
150 };
151 } // DrmStandard
152 } // OHOS
153 
154 #endif // OHOS_DRM_DRM_HOST_MANAGER_H