• 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 
21 #include <dlfcn.h>
22 
23 #include "nocopyable.h"
24 
25 #include "i_context.h"
26 #include "i_cooperate.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:
57     PluginManager() = default;
58     ~PluginManager() = default;
59     DISALLOW_COPY_AND_MOVE(PluginManager);
60 
61     void Init(IContext *context);
62 
63     ICooperate* LoadCooperate() override;
64     void UnloadCooperate() override;
65 
66 private:
67     template<typename IPlugin>
68     std::unique_ptr<Plugin<IPlugin>> LoadLibrary(IContext *context, const char *libPath);
69 
70 private:
71     IContext *context_ { nullptr };
72     std::unique_ptr<Plugin<ICooperate>> cooperate_ { nullptr };
73 };
74 
75 template<typename IPlugin>
Plugin(IContext * context,void * handle)76 PluginManager::Plugin<IPlugin>::Plugin(IContext *context, void *handle)
77     : context_(context), handle_(handle)
78 {}
79 
80 template<typename IPlugin>
~Plugin()81 PluginManager::Plugin<IPlugin>::~Plugin()
82 {
83     if (instance_ != nullptr) {
84         DestroyPlugin<IPlugin> destroy =
85             reinterpret_cast<DestroyPlugin<IPlugin>>(::dlsym(handle_, "DestroyInstance"));
86         if (destroy != nullptr) {
87             destroy(instance_);
88         }
89     }
90     ::dlclose(handle_);
91 }
92 
93 template<typename IPlugin>
GetInstance()94 IPlugin* PluginManager::Plugin<IPlugin>::GetInstance()
95 {
96     if (instance_ != nullptr) {
97         return instance_;
98     }
99     CreatePlugin<IPlugin> create = reinterpret_cast<CreatePlugin<IPlugin>>(::dlsym(handle_, "CreateInstance"));
100     if (create != nullptr) {
101         instance_ = create(context_);
102     }
103     return instance_;
104 }
105 
106 template<typename IPlugin>
LoadLibrary(IContext * context,const char * libPath)107 std::unique_ptr<PluginManager::Plugin<IPlugin>> PluginManager::LoadLibrary(IContext *context, const char *libPath)
108 {
109     void *handle = ::dlopen(libPath, RTLD_NOW);
110     return (handle != nullptr ? std::make_unique<Plugin<IPlugin>>(context, handle) : nullptr);
111 }
112 } // namespace DeviceStatus
113 } // namespace Msdp
114 } // namespace OHOS
115 #endif // PLUGIN_MANAGER_H