Change the brace policy to K&R.
[platform/core/api/app-manager.git] / src / app_manager.c
1 /*
2  * Copyright (c) 2011 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
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 #include <aul.h>
24 #include <dlog.h>
25
26 #include "app_manager.h"
27 #include "app_manager_internal.h"
28
29 #ifdef LOG_TAG
30 #undef LOG_TAG
31 #endif
32
33 #define LOG_TAG "CAPI_APPFW_APP_MANAGER"
34
35
36 static const char* app_manager_error_to_string(app_manager_error_e error)
37 {
38         switch (error) {
39         case APP_MANAGER_ERROR_NONE:
40                 return "Successful";
41         case APP_MANAGER_ERROR_INVALID_PARAMETER:
42                 return "Invalid parameter";
43         case APP_MANAGER_ERROR_OUT_OF_MEMORY:
44                 return "Out of memory";
45         case APP_MANAGER_ERROR_IO_ERROR:
46                 return "IO error";
47         case APP_MANAGER_ERROR_NO_SUCH_APP:
48                 return "No such application";
49         case APP_MANAGER_ERROR_DB_FAILED:
50                 return "DB error";
51         case APP_MANAGER_ERROR_INVALID_PACKAGE:
52                 return "Invalid package";
53         default:
54                 return "Unknown";
55         }
56 }
57
58 int app_manager_error(app_manager_error_e error, const char* function, const char *description)
59 {
60         if (description)
61                 LOGE("[%s] %s(0x%08x) : %s", function, app_manager_error_to_string(error), error, description);
62         else
63                 LOGE("[%s] %s(0x%08x)", function, app_manager_error_to_string(error), error);
64
65         return error;
66 }
67
68 API int app_manager_set_app_context_event_cb(app_manager_app_context_event_cb callback, void *user_data)
69 {
70         int retval = app_context_set_event_cb(callback, user_data);
71
72         if (retval != APP_MANAGER_ERROR_NONE)
73                 return app_manager_error(retval, __FUNCTION__, NULL);
74         else
75                 return APP_MANAGER_ERROR_NONE;
76 }
77
78 API void app_manager_unset_app_context_event_cb(void)
79 {
80         app_context_unset_event_cb();
81 }
82
83 API int app_manager_foreach_app_context(app_manager_app_context_cb callback, void *user_data)
84 {
85         int retval = app_context_foreach_app_context(callback, user_data);
86
87         if (retval != APP_MANAGER_ERROR_NONE)
88                 return app_manager_error(retval, __FUNCTION__, NULL);
89         else
90                 return APP_MANAGER_ERROR_NONE;
91 }
92
93 API int app_manager_get_app_context(const char *app_id, app_context_h *app_context)
94 {
95         int retval = app_context_get_app_context(app_id, app_context);
96
97         if (retval != APP_MANAGER_ERROR_NONE)
98                 return app_manager_error(retval, __FUNCTION__, NULL);
99         else
100                 return APP_MANAGER_ERROR_NONE;
101 }
102
103 API int app_manager_resume_app(app_context_h app_context)
104 {
105         char *app_id;
106         int retval;
107
108         if (app_context == NULL)
109                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
110
111         if (app_context_get_app_id(app_context, &app_id) != APP_MANAGER_ERROR_NONE)
112                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, "failed to get the application ID");
113
114         if (aul_app_is_running(app_id) == 0) {
115                 free(app_id);
116                 return app_manager_error(APP_MANAGER_ERROR_APP_NO_RUNNING, __FUNCTION__, NULL);
117         }
118
119         retval = aul_resume_app(app_id);
120
121         free(app_id);
122
123         if (retval == AUL_R_EINVAL)
124                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
125         else if (retval == AUL_R_EILLACC)
126                 return app_manager_error(APP_MANAGER_ERROR_PERMISSION_DENIED, __FUNCTION__, NULL);
127         else if (retval < 0)
128                 return app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
129
130         return APP_MANAGER_ERROR_NONE;
131 }
132
133 API int app_manager_foreach_app_info(app_manager_app_info_cb callback, void *user_data)
134 {
135         int retval;
136
137         retval = app_info_foreach_app_info(callback, user_data);
138
139         if (retval != APP_MANAGER_ERROR_NONE)
140                 return app_manager_error(retval, __FUNCTION__, NULL);
141         else
142                 return APP_MANAGER_ERROR_NONE;
143 }
144
145 API int app_manager_get_app_info(const char *app_id, app_info_h *app_info)
146 {
147         int retval;
148
149         retval = app_info_create(app_id, app_info);
150
151         if (retval != APP_MANAGER_ERROR_NONE)
152                 return app_manager_error(retval, __FUNCTION__, NULL);
153         else
154                 return APP_MANAGER_ERROR_NONE;
155 }
156
157 API int app_manager_get_app_id(pid_t pid, char **app_id)
158 {
159         char buffer[256] = {0, };
160         char *app_id_dup = NULL;
161
162         if (app_id == NULL)
163                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
164
165         if (aul_app_get_appid_bypid(pid, buffer, sizeof(buffer)) != AUL_R_OK)
166                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, "Invalid process ID");
167
168         app_id_dup = strdup(buffer);
169         if (app_id_dup == NULL)
170                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
171
172         *app_id = app_id_dup;
173
174         return APP_MANAGER_ERROR_NONE;
175 }
176
177 API int app_manager_terminate_app(app_context_h app_context)
178 {
179         int retval;
180         pid_t pid = 0;
181
182         if (app_context == NULL)
183                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
184
185         if (app_context_get_pid(app_context, &pid) != APP_MANAGER_ERROR_NONE)
186                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, "failed to get the process ID");
187
188         retval = aul_terminate_pid(pid);
189         if (retval == AUL_R_EINVAL) {
190                 LOGE("[%s] APP_MANAGER_ERROR_INVALID_PARAMETER(0x%08x) : Invalid param", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
191                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
192         } else if (retval == AUL_R_EILLACC) {
193                 LOGE("[%s] APP_MANAGER_ERROR_PERMISSION_DENIED(0x%08x) : Permission denied", __FUNCTION__, APP_MANAGER_ERROR_PERMISSION_DENIED);
194                 return APP_MANAGER_ERROR_PERMISSION_DENIED;
195         } else if (retval < 0) {
196                 return APP_MANAGER_ERROR_REQUEST_FAILED;
197         }
198
199         return APP_MANAGER_ERROR_NONE;
200 }
201
202 API int app_manager_request_terminate_bg_app(app_context_h app_context)
203 {
204         int retval = APP_MANAGER_ERROR_NONE;
205         pid_t pid = 0;
206
207         if (app_context == NULL)
208                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
209
210         if (app_context_get_pid(app_context, &pid) != APP_MANAGER_ERROR_NONE)
211                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, "failed to get the process ID");
212
213         retval = aul_terminate_bgapp_pid(pid);
214         if (retval == AUL_R_EINVAL) {
215                 LOGE("[%s] APP_MANAGER_ERROR_INVALID_PARAMETER(0x%08x) : Invalid param", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
216                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
217         } else if (retval == AUL_R_EILLACC) {
218                 LOGE("[%s] APP_MANAGER_ERROR_PERMISSION_DENIED(0x%08x) : Permission denied", __FUNCTION__, APP_MANAGER_ERROR_PERMISSION_DENIED);
219                 return APP_MANAGER_ERROR_PERMISSION_DENIED;
220         } else if (retval < 0) {
221                 return APP_MANAGER_ERROR_REQUEST_FAILED;
222         }
223
224         return APP_MANAGER_ERROR_NONE;
225 }
226
227 API int app_manager_is_running(const char *app_id, bool *running)
228 {
229         if (app_id == NULL) {
230                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid package", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
231                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
232         }
233
234         if (running == NULL) {
235                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid output param", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
236                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
237         }
238
239         *running = aul_app_is_running(app_id);
240
241         return APP_MANAGER_ERROR_NONE;
242 }
243
244 API int app_manager_open_app(const char *app_id)
245 {
246         int retval;
247
248         retval = aul_open_app(app_id);
249         if (retval == AUL_R_ERROR) {
250                 LOGE("[%s] APP_MANAGER_ERROR_NO_SUCH_APP(0x%08x) : No such application", __FUNCTION__, APP_MANAGER_ERROR_NO_SUCH_APP);
251                 return APP_MANAGER_ERROR_NO_SUCH_APP;
252         } else if (retval == AUL_R_EINVAL) {
253                 LOGE("[%s] APP_MANAGER_ERROR_INVALID_PARAMETER(0x%08x) : Invalid param", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
254                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
255         } else if (retval == AUL_R_EILLACC) {
256                 LOGE("[%s] APP_MANAGER_ERROR_PERMISSION_DENIED(0x%08x) : Permission denied", __FUNCTION__, APP_MANAGER_ERROR_PERMISSION_DENIED);
257                 return APP_MANAGER_ERROR_PERMISSION_DENIED;
258         } else if (retval < 0) {
259                 return APP_MANAGER_ERROR_REQUEST_FAILED;
260         }
261
262         return APP_MANAGER_ERROR_NONE;
263 }
264
265 API int app_manager_get_shared_data_path(const char *app_id, char **path)
266 {
267         int r;
268         int retval = aul_get_app_shared_data_path_by_appid(app_id, path);
269
270         switch (retval) {
271         case AUL_R_OK:
272                 r = APP_MANAGER_ERROR_NONE;
273                 break;
274         case AUL_R_ENOAPP:
275                 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
276                 break;
277         case AUL_R_EINVAL:
278                 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
279                 break;
280         case AUL_R_ERROR:
281                 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
282                 break;
283         default:
284                 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
285                 break;
286         }
287
288         return r;
289 }
290
291 API int app_manager_get_shared_resource_path(const char *app_id, char **path)
292 {
293         int r;
294         int retval = aul_get_app_shared_resource_path_by_appid(app_id, path);
295
296         switch (retval) {
297         case AUL_R_OK:
298                 r = APP_MANAGER_ERROR_NONE;
299                 break;
300         case AUL_R_ENOAPP:
301                 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
302                 break;
303         case AUL_R_EINVAL:
304                 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
305                 break;
306         case AUL_R_ERROR:
307                 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
308                 break;
309         default:
310                 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
311                 break;
312         }
313
314         return r;
315 }
316
317 API int app_manager_get_shared_trusted_path(const char *app_id, char **path)
318 {
319         int r;
320         int retval = aul_get_app_shared_trusted_path_by_appid(app_id, path);
321
322         switch (retval) {
323         case AUL_R_OK:
324                 r = APP_MANAGER_ERROR_NONE;
325                 break;
326         case AUL_R_ENOAPP:
327                 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
328                 break;
329         case AUL_R_EINVAL:
330                 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
331                 break;
332         case AUL_R_ERROR:
333                 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
334                 break;
335         default:
336                 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
337                 break;
338         }
339
340         return r;
341 }
342
343 API int app_manager_get_external_shared_data_path(const char *app_id, char **path)
344 {
345         int r;
346         int retval = aul_get_app_external_shared_data_path_by_appid(app_id, path);
347
348         switch (retval) {
349         case AUL_R_OK:
350                 r = APP_MANAGER_ERROR_NONE;
351                 break;
352         case AUL_R_ENOAPP:
353                 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
354                 break;
355         case AUL_R_EINVAL:
356                 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
357                 break;
358         case AUL_R_ERROR:
359                 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
360                 break;
361         default:
362                 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
363                 break;
364         }
365
366         return r;
367 }