b207209bc940702c071ff7d81cee36ed6cf9f8f0
[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 } DllPath;
42
43 static DllPath* __dllPath = nullptr;
44 static std::string __tpa;
45 bool initializedPathManager = false;
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         if (initializedPathManager) {
51                 _INFO("Path manager already initialized");
52                 return 0;
53         }
54
55         __dllPath = new DllPath();
56         if (!__dllPath) {
57                 _ERR("fail to allocate memory for dll path structure\n");
58                 return -1;
59         }
60
61         if (!runtimeDir.empty()) {
62                 __dllPath->runtime_dir = absolutePath(runtimeDir);
63         } else {
64                 __dllPath->runtime_dir = absolutePath(__RUNTIME_DIR);
65         }
66
67         if (!tizenFXDir.empty()) {
68                 __dllPath->tizenfx_dir = absolutePath(tizenFXDir);
69         } else {
70                 char* tmp = vconf_get_str(__TIZEN_API_PATH_KEY);
71                 if (tmp) {
72                         __dllPath->tizenfx_dir = std::string(tmp);
73                         _DBG("Device API Directory is set by vconf : %s", tmp);
74                 } else {
75                         __dllPath->tizenfx_dir = absolutePath(__DEVICE_API_DIR);
76                 }
77         }
78
79         __dllPath->tizenfx_ref_dir = __dllPath->tizenfx_dir + "/ref";
80
81         // ":" seperated extra directories
82         if (!extraDir.empty()) {
83                 splitPath(extraDir, __dllPath->extra_dirs);
84         } else {
85                 char* extraPath = pluginGetDllPath();
86                 if (extraPath) {
87                         splitPath(extraPath, __dllPath->extra_dirs);
88                 }
89         }
90
91         _INFO("Path manager initialize success");
92
93         initializedPathManager = true;
94         return 0;
95 }
96
97 void finalizePathManager()
98 {
99         if (__dllPath) {
100                 delete __dllPath;
101                 __dllPath = NULL;
102         }
103
104         initializedPathManager = false;
105 }
106
107 std::string getRuntimeDir()
108 {
109         return __dllPath->runtime_dir;
110 }
111
112 std::string getTizenFXDir()
113 {
114         return __dllPath->tizenfx_dir;
115 }
116
117 std::string getTizenFXRefDir()
118 {
119         return __dllPath->tizenfx_ref_dir;
120 }
121
122 std::vector <std::string> getExtraDirs()
123 {
124         return __dllPath->extra_dirs;
125 }
126
127 static std::string getPlatformTPA()
128 {
129         std::string platform_tpa;
130
131         if (isFileExist(PLATFORM_TPA_CACHE)) {
132                 _INFO("platform tpa cache found.");
133                 std::ifstream cacheFile;
134                 cacheFile.open(PLATFORM_TPA_CACHE);
135                 std::getline(cacheFile, platform_tpa);
136                 cacheFile.close();
137         } else {
138                 std::vector<std::string> tpaDir;
139                 tpaDir.push_back(getRuntimeDir());
140                 tpaDir.push_back(getTizenFXDir());
141                 tpaDir.push_back(getTizenFXRefDir());
142                 assembliesInDirectory(tpaDir, platform_tpa);
143         }
144
145         return platform_tpa;
146 }
147
148 static std::string getPluginTPA()
149 {
150         std::string plugin_tpa;
151
152         char* plugin_tpa_list = pluginGetTPA();
153         if (plugin_tpa_list) {
154                 _INFO("plugin TPA list found. use TPA list for plugin");
155                 plugin_tpa = plugin_tpa_list;
156         } else if (!__dllPath->extra_dirs.empty()){
157                 _INFO("plugin extra directory found. use plugin extra directroy for TPA");
158                 assembliesInDirectory(__dllPath->extra_dirs, plugin_tpa);
159         }
160
161         return plugin_tpa;
162 }
163
164 std::string getTPA()
165 {
166         if (!__tpa.empty()) {
167                 return __tpa;
168         }
169
170         if (__dllPath == NULL) {
171                 return std::string("");
172         }
173
174         return getPlatformTPA() + ":" + getPluginTPA();
175 }
176