472415d1acbd1a6eb9d461730a54dff3348cd44b
[platform/core/dotnet/launcher.git] / NativeLauncher / util / plugin_manager.cc
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <dlfcn.h>
18
19 #include "plugin_manager.h"
20 #include "utils.h"
21 #include "log.h"
22
23 static PluginFunc* __pluginFunc = NULL;
24 static void* __pluginLib;
25 static bool initializedPluginManager = false;
26
27 int initializePluginManager(const char* appType)
28 {
29         if (initializedPluginManager) {
30                 _INFO("Plugin manager already initialized");
31                 return 0;
32         }
33
34         if (isFile(PLUGIN_PATH)) {
35                 __pluginLib = dlopen(PLUGIN_PATH, RTLD_NOW | RTLD_LOCAL | RTLD_NODELETE);
36                 if (!__pluginLib) {
37                         _ERR("fail to dlopen plugin library");
38                         return -1;
39                 }
40
41                 __pluginFunc = (PluginFunc*)calloc(sizeof(PluginFunc), 1);
42                 if (!__pluginFunc) {
43                         _ERR("fail to allocate memory for plugin function structure");
44                         dlclose(__pluginLib);
45                         __pluginLib = NULL;
46                         return -1;
47                 }
48                 __pluginFunc->initialize = (plugin_initialize_ptr)dlsym(__pluginLib, "plugin_initialize");
49                 __pluginFunc->preload = (plugin_preload_ptr)dlsym(__pluginLib, "plugin_preload");
50                 __pluginFunc->has_log_control = (plugin_has_log_control_ptr)dlsym(__pluginLib, "plugin_has_log_control");
51                 __pluginFunc->set_app_info = (plugin_set_app_info_ptr)dlsym(__pluginLib, "plugin_set_app_info");
52                 __pluginFunc->set_coreclr_info = (plugin_set_coreclr_info_ptr)dlsym(__pluginLib, "plugin_set_coreclr_info");
53                 __pluginFunc->get_dll_path = (plugin_get_dll_path_ptr)dlsym(__pluginLib, "plugin_get_dll_path");
54                 __pluginFunc->get_extra_dll_path = (plugin_get_extra_dll_path_ptr)dlsym(__pluginLib, "plugin_get_extra_dll_path");
55                 __pluginFunc->get_native_dll_searching_path = (plugin_get_native_dll_searching_path_ptr)dlsym(__pluginLib, "plugin_get_native_dll_searching_path");
56                 __pluginFunc->get_tpa = (plugin_get_tpa_ptr)dlsym(__pluginLib, "plugin_get_tpa");
57                 __pluginFunc->before_execute = (plugin_before_execute_ptr)dlsym(__pluginLib, "plugin_before_execute");
58                 __pluginFunc->finalize  = (plugin_finalize_ptr)dlsym(__pluginLib, "plugin_finalize");
59         }
60
61         if (__pluginFunc && __pluginFunc->initialize)
62                 __pluginFunc->initialize(appType);
63
64         initializedPluginManager = true;
65
66         _INFO("Plugin manager initialize success : appType(%s)", appType);
67
68         return 0;
69 }
70
71 void finalizePluginManager()
72 {
73         if (!initializedPluginManager)
74                 return;
75
76         _INFO("Plugin manager finalize called");
77
78         if (__pluginFunc) {
79                 free(__pluginFunc);
80                 __pluginFunc = NULL;
81         }
82
83         if (__pluginLib) {
84                 dlclose(__pluginLib);
85                 __pluginLib = NULL;
86         }
87
88         initializedPluginManager = false;
89 }
90
91 void pluginPreload()
92 {
93         if (__pluginFunc && __pluginFunc->preload) {
94                 __pluginFunc->preload();
95         }
96 }
97
98 bool pluginHasLogControl()
99 {
100         if (__pluginFunc && __pluginFunc->has_log_control) {
101                 return __pluginFunc->has_log_control();
102         } else {
103                 return false;
104         }
105 }
106
107 void pluginSetAppInfo(const char* appId, const char* managedAssemblyPath)
108 {
109         if (__pluginFunc && __pluginFunc->set_app_info) {
110                 __pluginFunc->set_app_info(appId, managedAssemblyPath);
111         }
112 }
113
114 void pluginSetCoreclrInfo(void* hostHandle, unsigned int domainId, coreclr_create_delegate_ptr delegateFunc)
115 {
116         if (__pluginFunc && __pluginFunc->set_coreclr_info) {
117                 __pluginFunc->set_coreclr_info(hostHandle, domainId, delegateFunc);
118         }
119 }
120
121 char* pluginGetDllPath()
122 {
123         if (__pluginFunc && __pluginFunc->get_dll_path) {
124                 return __pluginFunc->get_dll_path();
125         } else {
126                 return NULL;
127         }
128 }
129
130 char* pluginGetExtraDllPath()
131 {
132         if (__pluginFunc && __pluginFunc->get_extra_dll_path) {
133                 return __pluginFunc->get_extra_dll_path();
134         } else {
135                 return NULL;
136         }
137 }
138
139 char* pluginGetNativeDllSearchingPath()
140 {
141         if (__pluginFunc && __pluginFunc->get_native_dll_searching_path) {
142                 return __pluginFunc->get_native_dll_searching_path();
143         } else {
144                 return NULL;
145         }
146 }
147
148 char* pluginGetTPA()
149 {
150         if (__pluginFunc && __pluginFunc->get_tpa) {
151                 return __pluginFunc->get_tpa();
152         } else {
153                 return NULL;
154         }
155 }
156
157 void pluginBeforeExecute()
158 {
159         if (__pluginFunc && __pluginFunc->before_execute) {
160                 __pluginFunc->before_execute();
161         }
162 }
163
164 void pluginFinalize()
165 {
166         if (__pluginFunc && __pluginFunc->finalize) {
167                 __pluginFunc->finalize();
168         }
169 }