• 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 #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 void (*GetHttpProxyParameterCallback)(std::string &, int32_t &, std::list<std::string> &);
70 typedef int32_t (*QueryMediaKeySystemNameFuncType)(std::string &);
71 typedef int32_t (*SetMediaKeySystemFuncType)(sptr<OHOS::HDI::Drm::V1_0::IMediaKeySystem> &);
72 typedef bool (*IsProvisionRequiredFuncType)();
73 typedef int32_t (*ThreadGetHttpProxyParameterFuncType)(GetHttpProxyParameterCallback);
74 typedef int32_t (*ThreadExitNotifyFuncType)(MediaKeySystemCallBack);
75 typedef int32_t (*StartThreadFuncType)();
76 typedef void (*StopThreadFuncType)();
77 
78 class DrmHostManager : public virtual RefBase, public HDI::ServiceManager::V1_0::ServStatListenerStub {
79 public:
80     class StatusCallback {
81     public:
82         virtual ~StatusCallback() = default;
83         virtual void OnDrmPluginDied(std::string &name) = 0;
84     };
85     class DrmHostDeathRecipient : public IRemoteObject::DeathRecipient {
86     public:
87         explicit DrmHostDeathRecipient(const sptr<DrmHostManager>& drmHostManager,
88             std::string &name);
89         virtual ~DrmHostDeathRecipient();
90         void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
91     private:
92         wptr<DrmHostManager> drmHostManager_;
93         std::string name_;
94     };
95 
96     explicit DrmHostManager(StatusCallback *statusCallback);
97     ~DrmHostManager() override;
98 
99     int32_t Init(void);
100     void DeInit(void);
101     void OnReceive(const HDI::ServiceManager::V1_0::ServiceStatus &status) override;
102     int32_t IsMediaKeySystemSupported(std::string &name, bool *isSupported);
103     int32_t IsMediaKeySystemSupported(std::string &name, std::string &mimeType, bool *isSupported);
104     int32_t IsMediaKeySystemSupported(std::string &name, std::string &mimeType, int32_t securityLevel,
105         bool *isSupported);
106     int32_t CreateMediaKeySystem(std::string &name, sptr<IMediaKeySystem> &hdiMediaKeySystem);
107     int32_t GetMediaKeySystems(std::map<std::string, std::string> &mediaKeySystemDescription);
108     int32_t GetMediaKeySystemUuid(std::string &name, std::string &uuid);
109     void ReleaseMediaKeySystem(sptr<IMediaKeySystem> &hdiMediaKeySystem);
110     void ClearDeathService(std::string &name);
111     void OnDrmPluginDied(std::string &name);
112 private:
113     static void UnLoadOEMCertifaicateService(std::string &name, ExtraInfo info);
114     static void GetHttpProxyParameter(std::string &host, int32_t &port, std::list<std::string> &exclusionList);
115     int32_t InitGetMediaKeySystems();
116     void StopServiceThread();
117     void DelayedLazyUnLoad();
118     void ProcessMessage();
119     void ServiceThreadMain();
120     void GetOemLibraryPath(std::vector<std::string> &libsToLoad);
121     void OemCertificateManager();
122     int32_t ProcessLazyLoadInfomation(std::string &name, sptr<IMediaKeySystemFactory> &drmHostServieProxy);
123     int32_t ProcessLazyLoadPlugin(std::string &name, std::vector<std::string> &serviceName,
124         sptr<IDeviceManager> &deviceMgr, sptr<IServiceManager> &servmgr);
125     int32_t GetServices(std::string &name, bool *isSupported, sptr<IMediaKeySystemFactory> &drmHostServieProxys);
126     void ReleaseHandleAndKeySystemMap(void *handle);
127     std::string StringTrim(const std::string& str);
128     int32_t LazyLoadPlugin(std::string &name, std::vector<std::string> &serviceName,
129     sptr<IDeviceManager> &deviceMgr, sptr<IServiceManager> &servmgr);
130     void parseLazyLoadService(std::ifstream& file, std::map<std::string, std::string>& lazyLoadPluginInfoMap);
131     int32_t LoadPluginInfo(const std::string& filePath);
132     void ReleaseSevices(sptr<IMediaKeySystemFactory> drmHostServieProxy);
133     void UnloadAllServices();
134 
135     StatusCallback *statusCallback_;
136     std::map<void *, sptr<IMediaKeySystem>> handleAndKeySystemMap;
137     std::thread serviceThread;
138     std::thread messageQueueThread;
139     bool serviceThreadRunning = false;
140     std::vector<void *> loadedLibs;
141     std::recursive_mutex drmHostMapMutex;
142     static std::mutex queueMutex;
143     static std::queue<Message> messageQueue;
144     static std::condition_variable cv;
145     std::map<std::string, void *> pluginNameAndHandleMap;
146     std::map<std::string, std::string> lazyLoadPluginInfoMap;
147     std::map<std::string, int32_t> lazyLoadPluginCountMap;
148     std::map<std::string, int32_t> lazyLoadPluginTimeoutMap;
149     std::map<std::string, std::string> mediaKeySystemDescription_;
150     std::map<sptr<IMediaKeySystem>, sptr<IMediaKeySystemFactory>> hdiMediaKeySystemAndFactoryMap;
151     std::map<sptr<IMediaKeySystemFactory>, std::string> hdiMediaKeySystemFactoryAndPluginNameMap;
152     std::map<sptr<IMediaKeySystemFactory>, sptr<DrmHostDeathRecipient>> drmHostDeathRecipientMap;
153 };
154 } // DrmStandard
155 } // OHOS
156 
157 #endif // OHOS_DRM_DRM_HOST_MANAGER_H