Merge 2.4 patch.
[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 = APP_MANAGER_ERROR_NONE;
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                 if (app_id) {
116                         free(app_id);
117                         app_id = NULL;
118                 }
119                 return app_manager_error(APP_MANAGER_ERROR_APP_NO_RUNNING, __FUNCTION__, NULL);
120         }
121
122         retval = aul_resume_app(app_id);
123
124         if (app_id)
125                 free(app_id);
126
127         if (retval == AUL_R_EINVAL)
128                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
129         else if (retval == AUL_R_EILLACC)
130                 return app_manager_error(APP_MANAGER_ERROR_PERMISSION_DENIED, __FUNCTION__, NULL);
131         else if (retval < 0)
132                 return app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
133
134         return APP_MANAGER_ERROR_NONE;
135 }
136
137 API int app_manager_foreach_app_info(app_manager_app_info_cb callback, void *user_data)
138 {
139         int retval;
140
141         retval = app_info_foreach_app_info(callback, user_data);
142
143         if (retval != APP_MANAGER_ERROR_NONE)
144                 return app_manager_error(retval, __FUNCTION__, NULL);
145         else
146                 return APP_MANAGER_ERROR_NONE;
147 }
148
149 API int app_manager_get_app_info(const char *app_id, app_info_h *app_info)
150 {
151         int retval;
152
153         retval = app_info_create(app_id, app_info);
154
155         if (retval != APP_MANAGER_ERROR_NONE)
156                 return app_manager_error(retval, __FUNCTION__, NULL);
157         else
158                 return APP_MANAGER_ERROR_NONE;
159 }
160
161 API int app_manager_get_app_id(pid_t pid, char **app_id)
162 {
163         char buffer[256] = {0, };
164         char *app_id_dup = NULL;
165
166         if (app_id == NULL)
167                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
168
169         if (aul_app_get_appid_bypid(pid, buffer, sizeof(buffer)) != AUL_R_OK)
170                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, "Invalid process ID");
171
172         app_id_dup = strdup(buffer);
173         if (app_id_dup == NULL)
174                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
175
176         *app_id = app_id_dup;
177
178         return APP_MANAGER_ERROR_NONE;
179 }
180
181 API int app_manager_terminate_app(app_context_h app_context)
182 {
183         int retval;
184         pid_t pid = 0;
185
186         if (app_context == NULL)
187                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
188
189         if (app_context_get_pid(app_context, &pid) != APP_MANAGER_ERROR_NONE)
190                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, "failed to get the process ID");
191
192         retval = aul_terminate_pid(pid);
193         if (retval == AUL_R_EINVAL) {
194                 LOGE("[%s] APP_MANAGER_ERROR_INVALID_PARAMETER(0x%08x) : Invalid param", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
195                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
196         } else if (retval == AUL_R_EILLACC) {
197                 LOGE("[%s] APP_MANAGER_ERROR_PERMISSION_DENIED(0x%08x) : Permission denied", __FUNCTION__, APP_MANAGER_ERROR_PERMISSION_DENIED);
198                 return APP_MANAGER_ERROR_PERMISSION_DENIED;
199         } else if (retval < 0) {
200                 return APP_MANAGER_ERROR_REQUEST_FAILED;
201         }
202
203         return APP_MANAGER_ERROR_NONE;
204 }
205
206 API int app_manager_request_terminate_bg_app(app_context_h app_context)
207 {
208         int retval = APP_MANAGER_ERROR_NONE;
209         pid_t pid = 0;
210
211         if (app_context == NULL)
212                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
213
214         if (app_context_get_pid(app_context, &pid) != APP_MANAGER_ERROR_NONE)
215                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, "failed to get the process ID");
216
217         retval = aul_terminate_bgapp_pid(pid);
218         if (retval == AUL_R_EINVAL) {
219                 LOGE("[%s] APP_MANAGER_ERROR_INVALID_PARAMETER(0x%08x) : Invalid param", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
220                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
221         } else if (retval == AUL_R_EILLACC) {
222                 LOGE("[%s] APP_MANAGER_ERROR_PERMISSION_DENIED(0x%08x) : Permission denied", __FUNCTION__, APP_MANAGER_ERROR_PERMISSION_DENIED);
223                 return APP_MANAGER_ERROR_PERMISSION_DENIED;
224         } else if (retval < 0) {
225                 return APP_MANAGER_ERROR_REQUEST_FAILED;
226         }
227
228         return APP_MANAGER_ERROR_NONE;
229 }
230
231 API int app_manager_is_running(const char *app_id, bool *running)
232 {
233         if (app_id == NULL) {
234                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid package", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
235                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
236         }
237
238         if (running == NULL) {
239                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid output param", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
240                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
241         }
242
243         *running = aul_app_is_running(app_id);
244
245         return APP_MANAGER_ERROR_NONE;
246 }
247
248 API int app_manager_get_shared_data_path(const char *app_id, char **path)
249 {
250         int r;
251         int retval = aul_get_app_shared_data_path_by_appid(app_id, path);
252
253         switch (retval) {
254         case AUL_R_OK:
255                 r = APP_MANAGER_ERROR_NONE;
256                 break;
257         case AUL_R_ENOAPP:
258                 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
259                 break;
260         case AUL_R_EINVAL:
261                 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
262                 break;
263         case AUL_R_ERROR:
264                 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
265                 break;
266         default:
267                 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
268                 break;
269         }
270
271         return r;
272 }
273
274 API int app_manager_get_shared_resource_path(const char *app_id, char **path)
275 {
276         int r;
277         int retval = aul_get_app_shared_resource_path_by_appid(app_id, path);
278
279         switch (retval) {
280         case AUL_R_OK:
281                 r = APP_MANAGER_ERROR_NONE;
282                 break;
283         case AUL_R_ENOAPP:
284                 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
285                 break;
286         case AUL_R_EINVAL:
287                 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
288                 break;
289         case AUL_R_ERROR:
290                 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
291                 break;
292         default:
293                 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
294                 break;
295         }
296
297         return r;
298 }
299
300 API int app_manager_get_shared_trusted_path(const char *app_id, char **path)
301 {
302         int r;
303         int retval = aul_get_app_shared_trusted_path_by_appid(app_id, path);
304
305         switch (retval) {
306         case AUL_R_OK:
307                 r = APP_MANAGER_ERROR_NONE;
308                 break;
309         case AUL_R_ENOAPP:
310                 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
311                 break;
312         case AUL_R_EINVAL:
313                 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
314                 break;
315         case AUL_R_ERROR:
316                 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
317                 break;
318         default:
319                 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
320                 break;
321         }
322
323         return r;
324 }
325
326 API int app_manager_get_external_shared_data_path(const char *app_id, char **path)
327 {
328         int r;
329         int retval = aul_get_app_external_shared_data_path_by_appid(app_id, path);
330
331         switch (retval) {
332         case AUL_R_OK:
333                 r = APP_MANAGER_ERROR_NONE;
334                 break;
335         case AUL_R_ENOAPP:
336                 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
337                 break;
338         case AUL_R_EINVAL:
339                 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
340                 break;
341         case AUL_R_ERROR:
342                 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
343                 break;
344         default:
345                 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
346                 break;
347         }
348
349         return r;
350 }