Fix to apply whitespace coding rules
[platform/core/security/krate.git] / server / app-proxy.cpp
1 /*
2  *  Copyright (c) 2015 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 <sys/stat.h>
18 #include <sys/types.h>
19
20 #include <unordered_map>
21
22 #include <system_settings.h>
23 #include <klay/auth/user.h>
24 #include <klay/audit/logger.h>
25
26 #include "packman.h"
27 #include "launchpad.h"
28
29 #include "rmi/app-proxy.h"
30
31 namespace Krate {
32
33 namespace {
34
35 struct IteratorData {
36         std::string krate;
37         std::vector<ApplicationInfo> list;
38         unsigned int current;
39 };
40
41 std::unordered_map<int, IteratorData> iteratorMap;
42 int newIteratorId = 0;
43
44 } // namespace
45
46 AppProxy::AppProxy(KrateControlContext& ctx) :
47         context(ctx)
48 {
49         context.registerParametricMethod(this, "", (AppProxy::AppInfo)(AppProxy::getAppInfo)(std::string, std::string));
50
51         context.registerParametricMethod(this, "", (int)(AppProxy::createIterator)(std::string));
52         context.registerParametricMethod(this, "", (AppProxy::AppInfo)(AppProxy::getIteratorValue)(int));
53         context.registerParametricMethod(this, "", (bool)(AppProxy::nextIterator)(int));
54         context.registerParametricMethod(this, "", (int)(AppProxy::destroyIterator)(int));
55
56         context.registerParametricMethod(this, "", (int)(AppProxy::launch)(std::string, AppProxy::Bundle));
57         context.registerParametricMethod(this, "", (int)(AppProxy::resume)(std::string, std::string));
58         context.registerParametricMethod(this, "", (int)(AppProxy::terminate)(std::string, std::string));
59         context.registerParametricMethod(this, "", (int)(AppProxy::isRunning)(std::string, std::string));
60 }
61
62 AppProxy::~AppProxy()
63 {
64 }
65
66 AppProxy::AppInfo AppProxy::getAppInfo(const std::string& name, const std::string& appid)
67 {
68         AppInfo appInfo;
69
70         try {
71                 runtime::User user(name);
72                 ApplicationInfo appinfo(appid, user.getUid());
73                 char* locale;
74
75                 system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &locale);
76                 if (locale == NULL) {
77                         appInfo.locale = "No locale";
78                 } else {
79                         appInfo.locale = locale;
80                         free(locale);
81                 }
82
83                 appInfo.krate = name;
84                 appInfo.id = appid;
85                 appInfo.package = appinfo.getPackage();
86                 appInfo.type = appinfo.getType();
87                 appInfo.icon = appinfo.getIcon();
88                 appInfo.label = appinfo.getLabel();
89                 appInfo.componentType = appinfo.getComponentType();
90                 appInfo.isNoDisplayed = appinfo.isNoDisplayed();
91                 appInfo.isTaskManaged = appinfo.isTaskManaged();
92         } catch (runtime::Exception& e) {
93                 ERROR("Failed to retrieve application info installed in the krate: " + appid);
94         }
95
96         return appInfo;
97 }
98
99 int AppProxy::createIterator(const std::string& name)
100 {
101         int iteratorId = -1;
102         try {
103                 PackageManager& packman = PackageManager::instance();
104                 runtime::User user(name);
105                 IteratorData data;
106
107                 iteratorId = newIteratorId;
108
109                 data.krate = name;
110                 data.list = packman.getAppList(user.getUid());
111                 data.current = 0;
112
113                 iteratorMap.insert(std::make_pair(iteratorId, data));
114
115                 if (++newIteratorId < 0) {
116                         newIteratorId = 0;
117                 }
118         } catch (runtime::Exception& e) {
119                 ERROR("Failed to retrieve package info installed in the krate");
120         }
121         return iteratorId;
122 }
123
124 AppProxy::AppInfo AppProxy::getIteratorValue(int iterator)
125 {
126         AppInfo appInfo;
127
128         auto it = iteratorMap.find(iterator);
129         if (it == iteratorMap.end()) {
130                 return appInfo;
131         }
132         if (it->second.current >= it->second.list.size()) {
133                 return appInfo;
134         }
135
136         const ApplicationInfo& appinfo = it->second.list.at(it->second.current);
137         char* locale;
138
139         system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &locale);
140         if (locale == NULL) {
141                 appInfo.locale = "No locale";
142         } else {
143                 appInfo.locale = locale;
144                 free(locale);
145         }
146
147         appInfo.krate = it->second.krate;
148         appInfo.id = appinfo.getId();
149         appInfo.package = appinfo.getPackage();
150         appInfo.type = appinfo.getType();
151         appInfo.icon = appinfo.getIcon();
152         appInfo.label = appinfo.getLabel();
153         appInfo.componentType = appinfo.getComponentType();
154         appInfo.isNoDisplayed = appinfo.isNoDisplayed();
155         appInfo.isTaskManaged = appinfo.isTaskManaged();
156
157         return appInfo;
158 }
159
160 bool AppProxy::nextIterator(int iterator)
161 {
162         auto it = iteratorMap.find(iterator);
163         if (it != iteratorMap.end()) {
164                 if (++it->second.current < it->second.list.size()) {
165                         return true;
166                 }
167         }
168         return false;
169 }
170
171 int AppProxy::destroyIterator(int iterator)
172 {
173         auto it = iteratorMap.find(iterator);
174         if (it != iteratorMap.end()) {
175                 iteratorMap.erase(it);
176                 return 0;
177         }
178         return -1;
179 }
180
181 int AppProxy::launch(const std::string& name, const AppProxy::Bundle& bundle)
182 {
183         try {
184                 runtime::User user(name);
185                 ::Bundle b;
186
187                 if (!bundle.operation.empty()) {
188                         b.add("__APP_SVC_OP_TYPE__", bundle.operation);
189                 }
190                 if (!bundle.uri.empty()) {
191                         b.add("__APP_SVC_URI__", bundle.uri);
192                 }
193                 if (!bundle.mime.empty()) {
194                         b.add("__APP_SVC_MIME__", bundle.mime);
195                 }
196                 if (!bundle.category.empty()) {
197                         b.add("__APP_SVC_CATEGORY__", bundle.category);
198                 }
199
200                 for (Bundle::Extra extra : bundle.extraData) {
201                         if (extra.value.size() > 1) {
202                                 b.add(extra.key, extra.value);
203                         } else if (extra.value.size() == 1) {
204                                 b.add(extra.key, extra.value.at(0));
205                         }
206                 }
207
208                 Launchpad launchpad(user.getUid());
209                 launchpad.launch(bundle.appId, b);
210         } catch (runtime::Exception& e) {
211                 ERROR("Failed to launch app in the krate");
212                 return -1;
213         }
214         return 0;
215 }
216
217 int AppProxy::resume(const std::string& name, const std::string& appid)
218 {
219         try {
220                 runtime::User user(name);
221                 Launchpad launchpad(user.getUid());
222                 launchpad.resume(appid);
223         } catch (runtime::Exception& e) {
224                 ERROR("Failed to terminate app in the krate");
225                 return -1;
226         }
227         return 0;
228 }
229
230 int AppProxy::terminate(const std::string& name, const std::string& appid)
231 {
232         try {
233                 runtime::User user(name);
234                 Launchpad launchpad(user.getUid());
235                 launchpad.terminate(appid);
236         } catch (runtime::Exception& e) {
237                 ERROR("Failed to terminate app in the krate");
238                 return -1;
239         }
240         return 0;
241 }
242
243 bool AppProxy::isRunning(const std::string& name, const std::string& appid)
244 {
245         try {
246                 runtime::User user(name);
247                 Launchpad launchpad(user.getUid());
248                 return launchpad.isRunning(appid);
249         } catch (runtime::Exception& e) {
250                 ERROR("Failed to get app running state in the krate");
251         }
252         return false;
253 }
254
255 } // namespace Krate