0e38f4e96d22e8d6abe614bcef60ae99a86d2e9f
[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 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                         __pluginFunc = (PluginFunc*)calloc(sizeof(PluginFunc), 1);
38                         if (!__pluginFunc) {
39                                 _ERR("fail to allocate memory for plugin function structure");
40                                 dlclose(__pluginLib);
41                                 __pluginLib = NULL;
42                                 return -1;
43                         }
44                         __pluginFunc->initialize = (plugin_initialize_ptr)dlsym(__pluginLib, "plugin_initialize");
45                         __pluginFunc->preload = (plugin_preload_ptr)dlsym(__pluginLib, "plugin_preload");
46                         __pluginFunc->has_log_control = (plugin_has_log_control_ptr)dlsym(__pluginLib, "plugin_has_log_control");
47                         __pluginFunc->set_app_info = (plugin_set_app_info_ptr)dlsym(__pluginLib, "plugin_set_app_info");
48                         __pluginFunc->set_coreclr_info = (plugin_set_coreclr_info_ptr)dlsym(__pluginLib, "plugin_set_coreclr_info");
49                         __pluginFunc->get_dll_path = (plugin_get_dll_path_ptr)dlsym(__pluginLib, "plugin_get_dll_path");
50                         __pluginFunc->get_extra_dll_path = (plugin_get_extra_dll_path_ptr)dlsym(__pluginLib, "plugin_get_extra_dll_path");
51                         __pluginFunc->get_native_dll_searching_path = (plugin_get_native_dll_searching_path_ptr)dlsym(__pluginLib, "plugin_get_native_dll_searching_path");
52                         __pluginFunc->get_tpa = (plugin_get_tpa_ptr)dlsym(__pluginLib, "plugin_get_tpa");
53                         __pluginFunc->before_execute = (plugin_before_execute_ptr)dlsym(__pluginLib, "plugin_before_execute");
54                         __pluginFunc->finalize  = (plugin_finalize_ptr)dlsym(__pluginLib, "plugin_finalize");
55                 }
56
57                 if (__pluginFunc->initialize)
58                         __pluginFunc->initialize(appType);
59         }
60
61         _INFO("Plugin manager initialize success : appType(%s)", appType);
62
63         initializedPluginManager = true;
64         return 0;
65 }
66
67 void finalizePluginManager()
68 {
69         if (!initializedPluginManager)
70                 return;
71
72         _INFO("Plugin manager finalize called");
73
74         if (__pluginFunc) {
75                 free(__pluginFunc);
76                 __pluginFunc = NULL;
77         }
78
79         if (__pluginLib) {
80                 dlclose(__pluginLib);
81                 __pluginLib = NULL;
82         }
83
84         initializedPluginManager = false;
85 }
86
87 void pluginPreload()
88 {
89         if (__pluginFunc && __pluginFunc->preload) {
90                 __pluginFunc->preload();
91         }
92 }
93
94 bool pluginHasLogControl()
95 {
96         if (__pluginFunc && __pluginFunc->has_log_control) {
97                 return __pluginFunc->has_log_control();
98         } else {
99                 return false;
100         }
101 }
102
103 void pluginSetAppInfo(const char* appId, const char* managedAssemblyPath)
104 {
105         if (__pluginFunc && __pluginFunc->set_app_info) {
106                 __pluginFunc->set_app_info(appId, managedAssemblyPath);
107         }
108 }
109
110 void pluginSetCoreclrInfo(void* hostHandle, unsigned int domainId, coreclr_create_delegate_ptr delegateFunc)
111 {
112         if (__pluginFunc && __pluginFunc->set_coreclr_info) {
113                 __pluginFunc->set_coreclr_info(hostHandle, domainId, delegateFunc);
114         }
115 }
116
117 char* pluginGetDllPath()
118 {
119         if (__pluginFunc && __pluginFunc->get_dll_path) {
120                 return __pluginFunc->get_dll_path();
121         } else {
122                 return NULL;
123         }
124 }
125
126 char* pluginGetExtraDllPath()
127 {
128         if (__pluginFunc && __pluginFunc->get_extra_dll_path) {
129                 return __pluginFunc->get_extra_dll_path();
130         } else {
131                 return NULL;
132         }
133 }
134
135 char* pluginGetNativeDllSearchingPath()
136 {
137         if (__pluginFunc && __pluginFunc->get_native_dll_searching_path) {
138                 return __pluginFunc->get_native_dll_searching_path();
139         } else {
140                 return NULL;
141         }
142 }
143
144 char* pluginGetTPA()
145 {
146         if (__pluginFunc && __pluginFunc->get_tpa) {
147                 return __pluginFunc->get_tpa();
148         } else {
149                 return NULL;
150         }
151 }
152
153 void pluginBeforeExecute()
154 {
155         if (__pluginFunc && __pluginFunc->before_execute) {
156                 __pluginFunc->before_execute();
157         }
158 }
159
160 void pluginFinalize()
161 {
162         if (__pluginFunc && __pluginFunc->finalize) {
163                 __pluginFunc->finalize();
164         }
165 }