Minor readability fix (#244)
[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 #include <dlfcn.h>
17
18 #include "plugin_manager.h"
19 #include "utils.h"
20 #include "log.h"
21
22 static PluginFunc* __pluginFunc = NULL;
23 static void* __pluginLib;
24 bool initializedPluginManager = false;
25
26 int initializePluginManager(const char* mode)
27 {
28         if (initializedPluginManager) {
29                 _INFO("Plugin manager already initialized");
30                 return 0;
31         }
32
33         if (isFile(PLUGIN_PATH)) {
34                 __pluginLib = dlopen(PLUGIN_PATH, RTLD_NOW | RTLD_LOCAL | RTLD_NODELETE);
35                 if (__pluginLib) {
36                         __pluginFunc = (PluginFunc*)calloc(sizeof(PluginFunc), 1);
37                         if (!__pluginFunc) {
38                                 _ERR("fail to allocate memory for plugin function structure");
39                                 return -1;
40                         }
41                         __pluginFunc->initialize = (plugin_initialize_ptr)dlsym(__pluginLib, "plugin_initialize");
42                         __pluginFunc->preload = (plugin_preload_ptr)dlsym(__pluginLib, "plugin_preload");
43                         __pluginFunc->has_log_control = (plugin_has_log_control_ptr)dlsym(__pluginLib, "plugin_has_log_control");
44                         __pluginFunc->set_app_info = (plugin_set_app_info_ptr)dlsym(__pluginLib, "plugin_set_app_info");
45                         __pluginFunc->set_coreclr_info = (plugin_set_coreclr_info_ptr)dlsym(__pluginLib, "plugin_set_coreclr_info");
46                         __pluginFunc->get_dll_path = (plugin_get_dll_path_ptr)dlsym(__pluginLib, "plugin_get_dll_path");
47                         __pluginFunc->get_tpa = (plugin_get_tpa_ptr)dlsym(__pluginLib, "plugin_get_tpa");
48                         __pluginFunc->before_execute = (plugin_before_execute_ptr)dlsym(__pluginLib, "plugin_before_execute");
49                         __pluginFunc->finalize  = (plugin_finalize_ptr)dlsym(__pluginLib, "plugin_finalize");
50                 }
51
52                 if (__pluginFunc->initialize)
53                         __pluginFunc->initialize(mode);
54         }
55
56         _INFO("Plugin manager initialize success");
57
58         initializedPluginManager = true;
59         return 0;
60 }
61
62 void finalizePluginManager()
63 {
64         _INFO("Plugin manager finalize called");
65         if (__pluginFunc) {
66                 free(__pluginFunc);
67                 __pluginFunc = NULL;
68         }
69
70         if (__pluginLib) {
71                 dlclose(__pluginLib);
72                 __pluginLib = NULL;
73         }
74
75         initializedPluginManager = false;
76 }
77
78 void pluginPreload()
79 {
80         if (__pluginFunc && __pluginFunc->preload) {
81                 __pluginFunc->preload();
82         }
83 }
84
85 bool pluginHasLogControl()
86 {
87         if (__pluginFunc && __pluginFunc->has_log_control) {
88                 return __pluginFunc->has_log_control();
89         } else {
90                 return false;
91         }
92 }
93
94 void pluginSetAppInfo(const char* appId, const char* managedAssemblyPath)
95 {
96         if (__pluginFunc && __pluginFunc->set_app_info) {
97                 __pluginFunc->set_app_info(appId, managedAssemblyPath);
98         }
99 }
100
101 void pluginSetCoreclrInfo(void* hostHandle, unsigned int domainId, coreclr_create_delegate_ptr delegateFunc)
102 {
103         if (__pluginFunc && __pluginFunc->set_coreclr_info) {
104                 __pluginFunc->set_coreclr_info(hostHandle, domainId, delegateFunc);
105         }
106 }
107
108 char* pluginGetDllPath()
109 {
110         if (__pluginFunc && __pluginFunc->get_dll_path) {
111                 return __pluginFunc->get_dll_path();
112         } else {
113                 return NULL;
114         }
115 }
116
117 char* pluginGetTPA()
118 {
119         if (__pluginFunc && __pluginFunc->get_tpa) {
120                 return __pluginFunc->get_tpa();
121         } else {
122                 return NULL;
123         }
124 }
125
126 void pluginBeforeExecute()
127 {
128         if (__pluginFunc && __pluginFunc->before_execute) {
129                 __pluginFunc->before_execute();
130         }
131 }
132
133 void pluginFinalize()
134 {
135         if (__pluginFunc && __pluginFunc->finalize) {
136                 __pluginFunc->finalize();
137         }
138 }
139