cc42e88a8a5a2460da3642f9ee913c80f2323f13
[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
22 #include "utils.h"
23 #include "plugin_manager.h"
24 #include "log.h"
25
26 static const char* __TIZEN_API_PATH_KEY = "db/dotnet/tizen_api_path";
27
28 #define __XSTR(x) #x
29 #define __STR(x) __XSTR(x)
30 static const char* __DEVICE_API_DIR = __STR(DEVICE_API_DIR);
31 static const char* __RUNTIME_DIR = __STR(RUNTIME_DIR);
32 #undef __STR
33 #undef __XSTR
34
35 typedef struct DllPath {
36         std::string runtime_dir;
37         std::string tizenfx_dir;
38         std::string tizenfx_ref_dir;
39         std::vector <std::string>extra_dirs;
40 } DllPath;
41
42 static DllPath* __dllPath = nullptr;
43 static std::string __tpa;
44
45 // on success, return 0. otherwise return -1.
46 int initializePathManager(const std::string& runtimeDir, const std::string& tizenFXDir, const std::string& extraDir)
47 {
48         __dllPath = new DllPath();
49         if (!__dllPath) {
50                 fprintf(stderr, "fail to allocate memory for dll path structure\n");
51                 return -1;
52         }
53
54         if (!runtimeDir.empty()) {
55                 __dllPath->runtime_dir = absolutePath(runtimeDir);
56         } else {
57                 __dllPath->runtime_dir = absolutePath(__RUNTIME_DIR);
58         }
59
60         if (!tizenFXDir.empty()) {
61                 __dllPath->tizenfx_dir = absolutePath(tizenFXDir);
62         } else {
63                 char* tmp = vconf_get_str(__TIZEN_API_PATH_KEY);
64                 if (tmp) {
65                         __dllPath->tizenfx_dir = std::string(tmp);
66                         _DBG("Device API Directory is set by vconf : %s", tmp);
67                 } else {
68                         __dllPath->tizenfx_dir = absolutePath(__DEVICE_API_DIR);
69                 }
70         }
71
72         __dllPath->tizenfx_ref_dir = __dllPath->tizenfx_dir + "/ref";
73
74         // ":" seperated extra directories
75         if (!extraDir.empty()) {
76                 splitPath(extraDir, __dllPath->extra_dirs);
77         } else {
78                 if (pluginGetDllPath()) {
79                         std::string pluginPath(pluginGetDllPath());
80                         if (!pluginPath.empty()) {
81                                 splitPath(pluginPath, __dllPath->extra_dirs);
82                         }
83                 }
84         }
85
86         return 0;
87 }
88
89 void finalizePathManager()
90 {
91         if (__dllPath) {
92                 delete __dllPath;
93                 __dllPath = NULL;
94         }
95 }
96
97 std::string getRuntimeDir()
98 {
99         return __dllPath->runtime_dir;
100 }
101
102 std::string getTizenFXDir()
103 {
104         return __dllPath->tizenfx_dir;
105 }
106
107 std::string getTizenFXRefDir()
108 {
109         return __dllPath->tizenfx_ref_dir;
110 }
111
112 std::vector <std::string> getExtraDirs()
113 {
114         return __dllPath->extra_dirs;
115 }
116
117 std::string getTPA()
118 {
119         if (!__tpa.empty()) {
120                 return __tpa;
121         }
122
123         std::vector<std::string> tpaDir;
124
125         if (__dllPath == NULL) {
126                 return std::string("");
127         }
128
129         tpaDir.push_back(getRuntimeDir());
130         tpaDir.push_back(getTizenFXDir());
131         tpaDir.push_back(getTizenFXRefDir());
132         tpaDir.insert(tpaDir.end(), getExtraDirs().begin(), getExtraDirs().end());
133         assembliesInDirectory(tpaDir, __tpa);
134
135         return __tpa;
136 }
137