• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2021 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 #define HST_LOG_TAG "PluginLoader"
17 
18 #include "plugin_loader.h"
19 
20 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
21 #include <Windows.h>
22 #include <iostream>
23 #include <utility>
24 #else
25 #include <dlfcn.h>
26 #endif
27 
28 #include "foundation/log.h"
29 
30 using namespace OHOS::Media::Plugin;
31 
32 // NOLINTNEXTLINE: void*
PluginLoader(void * handler,std::string name)33 PluginLoader::PluginLoader(void* handler, std::string name) : handler_(handler), name_(std::move(name))
34 {
35 }
36 
~PluginLoader()37 PluginLoader::~PluginLoader()
38 {
39     UnLoadPluginFile();
40 }
41 
Create(const std::string & name,const std::string & path)42 std::shared_ptr<PluginLoader> PluginLoader::Create(const std::string& name, const std::string& path)
43 {
44     if (name.empty() || path.empty()) {
45         return {};
46     }
47     return CheckSymbol(LoadPluginFile(path), name);
48 }
49 
FetchRegisterFunction()50 RegisterFunc PluginLoader::FetchRegisterFunction()
51 {
52     return registerFunc_;
53 }
54 
FetchUnregisterFunction()55 UnregisterFunc PluginLoader::FetchUnregisterFunction()
56 {
57     return unregisterFunc_;
58 }
59 
LoadPluginFile(const std::string & path)60 void* PluginLoader::LoadPluginFile(const std::string& path)
61 {
62 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
63     return ::LoadLibraryA(path.c_str());
64 #else
65     auto pathStr = path.c_str();
66     if (pathStr) {
67         auto ptr = ::dlopen(pathStr, RTLD_NOW);
68         if (ptr == nullptr) {
69             MEDIA_LOG_E("dlopen failed due to " PUBLIC_LOG_S, ::dlerror());
70         }
71         return ptr;
72     }
73     return nullptr;
74 #endif
75 }
76 
CheckSymbol(void * handler,const std::string & name)77 std::shared_ptr<PluginLoader> PluginLoader::CheckSymbol(void* handler, const std::string& name)
78 {
79     if (handler) {
80         std::string registerFuncName = "register_" + name;
81         std::string unregisterFuncName = "unregister_" + name;
82         RegisterFunc registerFunc = nullptr;
83         UnregisterFunc unregisterFunc = nullptr;
84 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
85         registerFunc = (RegisterFunc)(::GetProcAddress((HMODULE)handler, registerFuncName.c_str()));
86         unregisterFunc = (UnregisterFunc)(::GetProcAddress((HMODULE)handler, unregisterFuncName.c_str()));
87 #else
88         registerFunc = (RegisterFunc)(::dlsym(handler, registerFuncName.c_str()));
89         unregisterFunc = (UnregisterFunc)(::dlsym(handler, unregisterFuncName.c_str()));
90 #endif
91         if (registerFunc && unregisterFunc) {
92             std::shared_ptr<PluginLoader> loader(new PluginLoader(handler, name));
93             loader->registerFunc_ = registerFunc;
94             loader->unregisterFunc_ = unregisterFunc;
95             return loader;
96         } else {
97             MEDIA_LOG_W("register or unregister found is not found in " PUBLIC_LOG_S, name.c_str());
98         }
99     }
100     return {};
101 }
102 
UnLoadPluginFile()103 void PluginLoader::UnLoadPluginFile()
104 {
105     if (handler_) {
106 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
107         ::FreeLibrary((HMODULE)handler_);
108 #else
109         ::dlclose(const_cast<void*>(handler_)); // NOLINT: const_cast
110 #endif
111     }
112 }
113