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 PLUGIN_MANAGER_H
17 #define PLUGIN_MANAGER_H
18 
19 #include <memory>
20 #include <mutex>
21 
22 #include <dlfcn.h>
23 
24 #include "nocopyable.h"
25 
26 #include "i_context.h"
27 #include "i_plugin_manager.h"
28 
29 namespace OHOS {
30 namespace Msdp {
31 namespace DeviceStatus {
32 
33 // Loading、unloading and bookkeeping of modules.
34 class PluginManager final : public IPluginManager {
35     template<typename IPlugin>
36     class Plugin final {
37     public:
38         Plugin(IContext *context, void *handle);
39         ~Plugin();
40         DISALLOW_COPY_AND_MOVE(Plugin);
41 
42         IPlugin* GetInstance();
43 
44     private:
45         IContext *context_ { nullptr };
46         void *handle_ { nullptr };
47         IPlugin *instance_ { nullptr };
48     };
49 
50     template<typename IPlugin>
51     using CreatePlugin = IPlugin* (*)(IContext *context);
52 
53     template<typename IPlugin>
54     using DestroyPlugin = void (*)(IPlugin *);
55 
56 public:
PluginManager(IContext * context)57     PluginManager(IContext *context) : context_(context) {}
58     ~PluginManager() = default;
59     DISALLOW_COPY_AND_MOVE(PluginManager);
60 
61     ICooperate* LoadCooperate() override;
62     void UnloadCooperate() override;
63 
64 private:
65     template<typename IPlugin>
66     std::unique_ptr<Plugin<IPlugin>> LoadLibrary(IContext *context, const char *libPath);
67 
68 private:
69     std::mutex lock_;
70     IContext *context_ { nullptr };
71     std::unique_ptr<Plugin<ICooperate>> cooperate_ { nullptr };
72 };
73 
74 template<typename IPlugin>
Plugin(IContext * context,void * handle)75 PluginManager::Plugin<IPlugin>::Plugin(IContext *context, void *handle)
76     : context_(context), handle_(handle)
77 {}
78 
79 template<typename IPlugin>
~Plugin()80 PluginManager::Plugin<IPlugin>::~Plugin()
81 {
82     if (instance_ != nullptr) {
83         DestroyPlugin<IPlugin> destroy =
84             reinterpret_cast<DestroyPlugin<IPlugin>>(::dlsym(handle_, "DestroyInstance"));
85         if (destroy != nullptr) {
86             destroy(instance_);
87         }
88     }
89     ::dlclose(handle_);
90 }
91 
92 template<typename IPlugin>
GetInstance()93 IPlugin* PluginManager::Plugin<IPlugin>::GetInstance()
94 {
95     if (instance_ != nullptr) {
96         return instance_;
97     }
98     CreatePlugin<IPlugin> create = reinterpret_cast<CreatePlugin<IPlugin>>(::dlsym(handle_, "CreateInstance"));
99     if (create != nullptr) {
100         instance_ = create(context_);
101     }
102     return instance_;
103 }
104 
105 template<typename IPlugin>
LoadLibrary(IContext * context,const char * libPath)106 std::unique_ptr<PluginManager::Plugin<IPlugin>> PluginManager::LoadLibrary(IContext *context, const char *libPath)
107 {
108     char realPath[PATH_MAX] = { 0 };
109     if (realpath(libPath, realPath) == nullptr) {
110         FI_HILOGE("Path is error, path is %{public}s", libPath);
111         return nullptr;
112     }
113     void *handle = ::dlopen(libPath, RTLD_NOW);
114     return (handle != nullptr ? std::make_unique<Plugin<IPlugin>>(context, handle) : nullptr);
115 }
116 } // namespace DeviceStatus
117 } // namespace Msdp
118 } // namespace OHOS
119 #endif // PLUGIN_MANAGER_H