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 IMotionDrag* LoadMotionDrag() override;
65 void UnloadMotionDrag() override;
66
67 private:
68 template<typename IPlugin>
69 std::unique_ptr<Plugin<IPlugin>> LoadLibrary(IContext *context, const char *libPath);
70
71 private:
72 std::mutex lock_;
73 IContext *context_ { nullptr };
74 std::unique_ptr<Plugin<ICooperate>> cooperate_ { nullptr };
75 std::unique_ptr<Plugin<IMotionDrag>> motionDrag_ { nullptr };
76 };
77
78 template<typename IPlugin>
Plugin(IContext * context,void * handle)79 PluginManager::Plugin<IPlugin>::Plugin(IContext *context, void *handle)
80 : context_(context), handle_(handle)
81 {}
82
83 template<typename IPlugin>
~Plugin()84 PluginManager::Plugin<IPlugin>::~Plugin()
85 {
86 if (instance_ != nullptr) {
87 DestroyPlugin<IPlugin> destroy =
88 reinterpret_cast<DestroyPlugin<IPlugin>>(::dlsym(handle_, "DestroyInstance"));
89 if (destroy != nullptr) {
90 destroy(instance_);
91 }
92 }
93 ::dlclose(handle_);
94 }
95
96 template<typename IPlugin>
GetInstance()97 IPlugin* PluginManager::Plugin<IPlugin>::GetInstance()
98 {
99 if (instance_ != nullptr) {
100 return instance_;
101 }
102 CreatePlugin<IPlugin> create = reinterpret_cast<CreatePlugin<IPlugin>>(::dlsym(handle_, "CreateInstance"));
103 if (create != nullptr) {
104 instance_ = create(context_);
105 }
106 return instance_;
107 }
108
109 template<typename IPlugin>
LoadLibrary(IContext * context,const char * libPath)110 std::unique_ptr<PluginManager::Plugin<IPlugin>> PluginManager::LoadLibrary(IContext *context, const char *libPath)
111 {
112 char realPath[PATH_MAX] = { 0 };
113 if (realpath(libPath, realPath) == nullptr) {
114 FI_HILOGE("Path is error, path is %{private}s", libPath);
115 return nullptr;
116 }
117 void *handle = ::dlopen(libPath, RTLD_NOW);
118 return (handle != nullptr ? std::make_unique<Plugin<IPlugin>>(context, handle) : nullptr);
119 }
120 } // namespace DeviceStatus
121 } // namespace Msdp
122 } // namespace OHOS
123 #endif // PLUGIN_MANAGER_H