Seperate the pluginDir and extraDir from the path manager (#252)
[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* mode)
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                                 return -1;
41                         }
42                         __pluginFunc->initialize = (plugin_initialize_ptr)dlsym(__pluginLib, "plugin_initialize");
43                         __pluginFunc->preload = (plugin_preload_ptr)dlsym(__pluginLib, "plugin_preload");
44                         __pluginFunc->has_log_control = (plugin_has_log_control_ptr)dlsym(__pluginLib, "plugin_has_log_control");
45                         __pluginFunc->set_app_info = (plugin_set_app_info_ptr)dlsym(__pluginLib, "plugin_set_app_info");
46                         __pluginFunc->set_coreclr_info = (plugin_set_coreclr_info_ptr)dlsym(__pluginLib, "plugin_set_coreclr_info");
47                         __pluginFunc->get_dll_path = (plugin_get_dll_path_ptr)dlsym(__pluginLib, "plugin_get_dll_path");
48                         __pluginFunc->get_tpa = (plugin_get_tpa_ptr)dlsym(__pluginLib, "plugin_get_tpa");
49                         __pluginFunc->before_execute = (plugin_before_execute_ptr)dlsym(__pluginLib, "plugin_before_execute");
50                         __pluginFunc->finalize  = (plugin_finalize_ptr)dlsym(__pluginLib, "plugin_finalize");
51                 }
52
53                 if (__pluginFunc->initialize)
54                         __pluginFunc->initialize(mode);
55         }
56
57         _INFO("Plugin manager initialize success");
58
59         initializedPluginManager = true;
60         return 0;
61 }
62
63 void finalizePluginManager()
64 {
65         _INFO("Plugin manager finalize called");
66         if (__pluginFunc) {
67                 free(__pluginFunc);
68                 __pluginFunc = NULL;
69         }
70
71         if (__pluginLib) {
72                 dlclose(__pluginLib);
73                 __pluginLib = NULL;
74         }
75
76         initializedPluginManager = false;
77 }
78
79 void pluginPreload()
80 {
81         if (__pluginFunc && __pluginFunc->preload) {
82                 __pluginFunc->preload();
83         }
84 }
85
86 bool pluginHasLogControl()
87 {
88         if (__pluginFunc && __pluginFunc->has_log_control) {
89                 return __pluginFunc->has_log_control();
90         } else {
91                 return false;
92         }
93 }
94
95 void pluginSetAppInfo(const char* appId, const char* managedAssemblyPath)
96 {
97         if (__pluginFunc && __pluginFunc->set_app_info) {
98                 __pluginFunc->set_app_info(appId, managedAssemblyPath);
99         }
100 }
101
102 void pluginSetCoreclrInfo(void* hostHandle, unsigned int domainId, coreclr_create_delegate_ptr delegateFunc)
103 {
104         if (__pluginFunc && __pluginFunc->set_coreclr_info) {
105                 __pluginFunc->set_coreclr_info(hostHandle, domainId, delegateFunc);
106         }
107 }
108
109 char* pluginGetDllPath()
110 {
111         if (__pluginFunc && __pluginFunc->get_dll_path) {
112                 return __pluginFunc->get_dll_path();
113         } else {
114                 return NULL;
115         }
116 }
117
118 char* pluginGetTPA()
119 {
120         if (__pluginFunc && __pluginFunc->get_tpa) {
121                 return __pluginFunc->get_tpa();
122         } else {
123                 return NULL;
124         }
125 }
126
127 void pluginBeforeExecute()
128 {
129         if (__pluginFunc && __pluginFunc->before_execute) {
130                 __pluginFunc->before_execute();
131         }
132 }
133
134 void pluginFinalize()
135 {
136         if (__pluginFunc && __pluginFunc->finalize) {
137                 __pluginFunc->finalize();
138         }
139 }