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