227e0a2fa21fabaa7a8062606885b000c6bc1545
[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
25 #define PLUGIN_PATH "/usr/share/dotnet.tizen/lib/libdotnet_plugin.so"
26
27 int initializePluginManager(const char* mode)
28 {
29         if (isFileExist(PLUGIN_PATH)) {
30                 __pluginLib = dlopen(PLUGIN_PATH, RTLD_NOW | RTLD_LOCAL);
31                 if (__pluginLib) {
32                         __pluginFunc = (PluginFunc*)calloc(sizeof(PluginFunc), 1);
33                         if (!__pluginFunc) {
34                                 _ERR("fail to allocate memory for plugin function structure");
35                                 return -1;
36                         }
37                         __pluginFunc->initialize = (plugin_initialize_ptr)dlsym(__pluginLib, "plugin_initialize");
38                         __pluginFunc->preload = (plugin_preload_ptr)dlsym(__pluginLib, "plugin_preload");
39                         __pluginFunc->has_log_control = (plugin_has_log_control_ptr)dlsym(__pluginLib, "plugin_has_log_control");
40                         __pluginFunc->set_app_info = (plugin_set_app_info_ptr)dlsym(__pluginLib, "plugin_set_app_info");
41                         __pluginFunc->set_coreclr_info = (plugin_set_coreclr_info_ptr)dlsym(__pluginLib, "plugin_set_coreclr_info");
42                         __pluginFunc->get_dll_path = (plugin_get_dll_path_ptr)dlsym(__pluginLib, "plugin_get_dll_path");
43                         __pluginFunc->before_execute = (plugin_before_execute_ptr)dlsym(__pluginLib, "plugin_before_execute");
44                         __pluginFunc->finalize  = (plugin_finalize_ptr)dlsym(__pluginLib, "plugin_finalize");
45                 }
46
47                 if (__pluginFunc->initialize)
48                         __pluginFunc->initialize(mode);
49         }
50
51         _INFO("Plugin manager initialize success");
52
53         return 0;
54 }
55
56 void finalizePluginManager()
57 {
58         if (__pluginFunc) {
59                 if (__pluginFunc->finalize) {
60                         __pluginFunc->finalize();
61                 }
62                 free(__pluginFunc);
63                 __pluginFunc = NULL;
64         }
65
66         if (__pluginLib) {
67                 dlclose(__pluginLib);
68                 __pluginLib = NULL;
69         }
70 }
71
72 void pluginPreload()
73 {
74         if (__pluginFunc && __pluginFunc->preload) {
75                 __pluginFunc->preload();
76         }
77 }
78
79 bool pluginHasLogControl()
80 {
81         if (__pluginFunc && __pluginFunc->has_log_control) {
82                 return __pluginFunc->has_log_control();
83         } else {
84                 return false;
85         }
86 }
87
88 void pluginSetAppInfo(const char* appId, const char* managedAssemblyPath)
89 {
90         if (__pluginFunc && __pluginFunc->set_app_info) {
91                 __pluginFunc->set_app_info(appId, managedAssemblyPath);
92         }
93 }
94
95 void pluginSetCoreclrInfo(void* hostHandle, unsigned int domainId, coreclr_create_delegate_ptr delegateFunc)
96 {
97         if (__pluginFunc && __pluginFunc->set_coreclr_info) {
98                 __pluginFunc->set_coreclr_info(hostHandle, domainId, delegateFunc);
99         }
100 }
101
102 char* pluginGetDllPath()
103 {
104         if (__pluginFunc && __pluginFunc->get_dll_path) {
105                 return __pluginFunc->get_dll_path();
106         } else {
107                 return NULL;
108         }
109 }
110
111 void pluginBeforeExecute()
112 {
113         if (__pluginFunc && __pluginFunc->before_execute) {
114                 __pluginFunc->before_execute();
115         }
116 }
117