Bug-fix : plugin extra directory should be added for TPA searching directory.
[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 static const char* PLATFORM_TPA_CACHE = "/usr/share/dotnet.tizen/lib/platform_tpa_cache";
29
30 #define __XSTR(x) #x
31 #define __STR(x) __XSTR(x)
32 static const char* __DEVICE_API_DIR = __STR(DEVICE_API_DIR);
33 static const char* __RUNTIME_DIR = __STR(RUNTIME_DIR);
34 #undef __STR
35 #undef __XSTR
36
37 typedef struct DllPath {
38         std::string runtime_dir;
39         std::string tizenfx_dir;
40         std::string tizenfx_ref_dir;
41         std::vector <std::string>extra_dirs;
42 } DllPath;
43
44 static DllPath* __dllPath = nullptr;
45 static std::string __tpa;
46
47 // on success, return 0. otherwise return -1.
48 int initializePathManager(const std::string& runtimeDir, const std::string& tizenFXDir, const std::string& extraDir)
49 {
50         __dllPath = new DllPath();
51         if (!__dllPath) {
52                 _ERR("fail to allocate memory for dll path structure\n");
53                 return -1;
54         }
55
56         if (!runtimeDir.empty()) {
57                 __dllPath->runtime_dir = absolutePath(runtimeDir);
58         } else {
59                 __dllPath->runtime_dir = absolutePath(__RUNTIME_DIR);
60         }
61
62         if (!tizenFXDir.empty()) {
63                 __dllPath->tizenfx_dir = absolutePath(tizenFXDir);
64         } else {
65                 char* tmp = vconf_get_str(__TIZEN_API_PATH_KEY);
66                 if (tmp) {
67                         __dllPath->tizenfx_dir = std::string(tmp);
68                         _DBG("Device API Directory is set by vconf : %s", tmp);
69                 } else {
70                         __dllPath->tizenfx_dir = absolutePath(__DEVICE_API_DIR);
71                 }
72         }
73
74         __dllPath->tizenfx_ref_dir = __dllPath->tizenfx_dir + "/ref";
75
76         // ":" seperated extra directories
77         if (!extraDir.empty()) {
78                 splitPath(extraDir, __dllPath->extra_dirs);
79         } else {
80                 char* extraPath = pluginGetDllPath();
81                 if (extraPath) {
82                         splitPath(extraPath, __dllPath->extra_dirs);
83                 }
84         }
85
86         _INFO("Path manager initialize success");
87
88         return 0;
89 }
90
91 void finalizePathManager()
92 {
93         if (__dllPath) {
94                 delete __dllPath;
95                 __dllPath = NULL;
96         }
97 }
98
99 std::string getRuntimeDir()
100 {
101         return __dllPath->runtime_dir;
102 }
103
104 std::string getTizenFXDir()
105 {
106         return __dllPath->tizenfx_dir;
107 }
108
109 std::string getTizenFXRefDir()
110 {
111         return __dllPath->tizenfx_ref_dir;
112 }
113
114 std::vector <std::string> getExtraDirs()
115 {
116         return __dllPath->extra_dirs;
117 }
118
119 static std::string getPlatformTPA()
120 {
121         std::string platform_tpa;
122
123         if (isFileExist(PLATFORM_TPA_CACHE)) {
124                 _INFO("platform tpa cache found.\n");
125                 std::ifstream cacheFile;
126                 cacheFile.open(PLATFORM_TPA_CACHE);
127                 std::getline(cacheFile, platform_tpa);
128                 cacheFile.close();
129         } else {
130                 std::vector<std::string> tpaDir;
131                 tpaDir.push_back(getRuntimeDir());
132                 tpaDir.push_back(getTizenFXDir());
133                 tpaDir.push_back(getTizenFXRefDir());
134                 assembliesInDirectory(tpaDir, platform_tpa);
135         }
136
137         return platform_tpa;
138 }
139
140 static std::string getPluginTPA()
141 {
142         std::string plugin_tpa;
143
144         char* plugin_tpa_list = pluginGetTPA();
145         if (plugin_tpa_list) {
146                 _INFO("plugin TPA list found. use TPA list for plugin");
147                 plugin_tpa = plugin_tpa_list;
148         } else if (!__dllPath->extra_dirs.empty()){
149                 _INFO("plugin extra directory found. use plugin extra directroy for TPA");
150                 assembliesInDirectory(__dllPath->extra_dirs, plugin_tpa);
151         }
152
153         return plugin_tpa;
154 }
155
156 std::string getTPA()
157 {
158         if (!__tpa.empty()) {
159                 return __tpa;
160         }
161
162         if (__dllPath == NULL) {
163                 return std::string("");
164         }
165
166         return getPlatformTPA() + ":" + getPluginTPA();
167 }
168