Seperate the pluginDir and extraDir from the path manager (#252)
[platform/core/dotnet/launcher.git] / NativeLauncher / util / path_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 <vector>
18 #include <iterator>
19 #include <sstream>
20 #include <vconf.h>
21 #include <fstream>
22
23 #include "utils.h"
24 #include "plugin_manager.h"
25 #include "log.h"
26
27 static const char* __TIZEN_API_PATH_KEY = "db/dotnet/tizen_api_path";
28
29 #define __XSTR(x) #x
30 #define __STR(x) __XSTR(x)
31 static const char* __DEVICE_API_DIR = __STR(DEVICE_API_DIR);
32 static const char* __RUNTIME_DIR = __STR(RUNTIME_DIR);
33 #undef __STR
34 #undef __XSTR
35
36 typedef struct DllPath {
37         std::string runtime_dir;
38         std::string tizenfx_dir;
39         std::string tizenfx_ref_dir;
40         std::vector<std::string> extra_dirs;
41         std::vector<std::string> plugin_dirs;
42 } DllPath;
43
44 static DllPath* __dllPath = nullptr;
45 static std::string __tpa;
46 bool initializedPathManager = false;
47
48 // on success, return 0. otherwise return -1.
49 int initializePathManager(const std::string& runtimeDir, const std::string& tizenFXDir, const std::string& extraDir)
50 {
51         if (initializedPathManager) {
52                 _INFO("Path manager already initialized");
53                 return 0;
54         }
55
56         __dllPath = new DllPath();
57         if (!__dllPath) {
58                 _ERR("fail to allocate memory for dll path structure\n");
59                 return -1;
60         }
61
62         if (!runtimeDir.empty()) {
63                 __dllPath->runtime_dir = getAbsolutePath(runtimeDir);
64         } else {
65                 __dllPath->runtime_dir = getAbsolutePath(__RUNTIME_DIR);
66         }
67
68         if (!tizenFXDir.empty()) {
69                 __dllPath->tizenfx_dir = getAbsolutePath(tizenFXDir);
70         } else {
71                 char* tmp = vconf_get_str(__TIZEN_API_PATH_KEY);
72                 if (tmp) {
73                         __dllPath->tizenfx_dir = std::string(tmp);
74                         _DBG("Device API Directory is set by vconf : %s", tmp);
75                         free(tmp);
76                 } else {
77                         __dllPath->tizenfx_dir = getAbsolutePath(__DEVICE_API_DIR);
78                 }
79         }
80
81         __dllPath->tizenfx_ref_dir = __dllPath->tizenfx_dir + "/ref";
82
83         // ":" seperated extra directories
84         if (!extraDir.empty()) {
85                 splitPath(extraDir, __dllPath->extra_dirs);
86         }
87
88         char* pluginPath = pluginGetDllPath();
89         if (pluginPath) {
90                 splitPath(pluginPath, __dllPath->plugin_dirs);
91         }
92
93         _INFO("Path manager initialize success");
94
95         initializedPathManager = true;
96         return 0;
97 }
98
99 void finalizePathManager()
100 {
101         _INFO("Path manager finalize called");
102         if (__dllPath) {
103                 delete __dllPath;
104                 __dllPath = NULL;
105         }
106
107         initializedPathManager = false;
108 }
109
110 std::string getRuntimeDir()
111 {
112         return __dllPath->runtime_dir;
113 }
114
115 std::string getTizenFXDir()
116 {
117         return __dllPath->tizenfx_dir;
118 }
119
120 std::string getTizenFXRefDir()
121 {
122         return __dllPath->tizenfx_ref_dir;
123 }
124
125 std::vector<std::string> getExtraDirs()
126 {
127         return __dllPath->extra_dirs;
128 }
129
130 std::vector<std::string> getPluginDirs()
131 {
132         return __dllPath->plugin_dirs;
133 }
134
135 static std::string getPlatformTPA()
136 {
137         std::string platform_tpa;
138
139         if (isFile(PLATFORM_TPA_CACHE)) {
140                 _INFO("platform tpa cache found.");
141                 std::ifstream cacheFile;
142                 cacheFile.open(PLATFORM_TPA_CACHE);
143                 std::getline(cacheFile, platform_tpa);
144                 cacheFile.close();
145         } else {
146                 std::vector<std::string> tpaDirs = { getRuntimeDir(), getTizenFXDir(), getTizenFXRefDir() };
147                 tpaDirs.insert(tpaDirs.end(), getExtraDirs().begin(), getExtraDirs().end());
148                 addAssembliesFromDirectories(tpaDirs, platform_tpa);
149         }
150
151         return platform_tpa;
152 }
153
154 static std::string getPluginTPA()
155 {
156         std::string plugin_tpa;
157         char* plugin_tpa_list = pluginGetTPA();
158         if (plugin_tpa_list) {
159                 _INFO("plugin TPA list found. use TPA list for plugin");
160                 plugin_tpa = plugin_tpa_list;
161         } else if (!getPluginDirs().empty()) {
162                 _INFO("Plugin directory found. use plugin directroy for TPA");
163                 addAssembliesFromDirectories(getPluginDirs(), plugin_tpa);
164         }
165
166         return plugin_tpa;
167 }
168
169 std::string getTPA()
170 {
171         if (!__tpa.empty()) {
172                 return __tpa;
173         }
174
175         if (__dllPath == NULL) {
176                 return std::string("");
177         }
178
179         return getPlatformTPA() + ":" + getPluginTPA();
180 }