2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include "app_manager.h"
27 #include "app_manager_internal.h"
33 #define LOG_TAG "CAPI_APPFW_APP_MANAGER"
36 static const char* app_manager_error_to_string(app_manager_error_e error)
39 case APP_MANAGER_ERROR_NONE:
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:
47 case APP_MANAGER_ERROR_NO_SUCH_APP:
48 return "No such application";
49 case APP_MANAGER_ERROR_DB_FAILED:
51 case APP_MANAGER_ERROR_INVALID_PACKAGE:
52 return "Invalid package";
58 int app_manager_error(app_manager_error_e error, const char* function, const char *description)
61 LOGE("[%s] %s(0x%08x) : %s", function, app_manager_error_to_string(error), error, description);
63 LOGE("[%s] %s(0x%08x)", function, app_manager_error_to_string(error), error);
68 API int app_manager_set_app_context_event_cb(app_manager_app_context_event_cb callback, void *user_data)
70 int retval = app_context_set_event_cb(callback, user_data);
72 if (retval != APP_MANAGER_ERROR_NONE)
73 return app_manager_error(retval, __FUNCTION__, NULL);
75 return APP_MANAGER_ERROR_NONE;
78 API void app_manager_unset_app_context_event_cb(void)
80 app_context_unset_event_cb();
83 API int app_manager_foreach_app_context(app_manager_app_context_cb callback, void *user_data)
85 int retval = app_context_foreach_app_context(callback, user_data);
87 if (retval != APP_MANAGER_ERROR_NONE)
88 return app_manager_error(retval, __FUNCTION__, NULL);
90 return APP_MANAGER_ERROR_NONE;
93 API int app_manager_get_app_context(const char *app_id, app_context_h *app_context)
95 int retval = app_context_get_app_context(app_id, app_context);
97 if (retval != APP_MANAGER_ERROR_NONE)
98 return app_manager_error(retval, __FUNCTION__, NULL);
100 return APP_MANAGER_ERROR_NONE;
103 API int app_manager_resume_app(app_context_h app_context)
106 int retval = APP_MANAGER_ERROR_NONE;
108 if (app_context == NULL)
109 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
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");
114 if (aul_app_is_running(app_id) == 0) {
119 return app_manager_error(APP_MANAGER_ERROR_APP_NO_RUNNING, __FUNCTION__, NULL);
122 retval = aul_resume_app(app_id);
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);
132 return app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
134 return APP_MANAGER_ERROR_NONE;
137 API int app_manager_foreach_app_info(app_manager_app_info_cb callback, void *user_data)
141 retval = app_info_foreach_app_info(callback, user_data);
143 if (retval != APP_MANAGER_ERROR_NONE)
144 return app_manager_error(retval, __FUNCTION__, NULL);
146 return APP_MANAGER_ERROR_NONE;
149 API int app_manager_get_app_info(const char *app_id, app_info_h *app_info)
153 retval = app_info_create(app_id, app_info);
155 if (retval != APP_MANAGER_ERROR_NONE)
156 return app_manager_error(retval, __FUNCTION__, NULL);
158 return APP_MANAGER_ERROR_NONE;
161 API int app_manager_get_app_id(pid_t pid, char **app_id)
163 char buffer[256] = {0, };
164 char *app_id_dup = NULL;
167 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
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");
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);
176 *app_id = app_id_dup;
178 return APP_MANAGER_ERROR_NONE;
181 API int app_manager_terminate_app(app_context_h app_context)
186 if (app_context == NULL)
187 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
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");
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;
203 return APP_MANAGER_ERROR_NONE;
206 API int app_manager_request_terminate_bg_app(app_context_h app_context)
208 int retval = APP_MANAGER_ERROR_NONE;
211 if (app_context == NULL)
212 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
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");
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;
228 return APP_MANAGER_ERROR_NONE;
231 API int app_manager_is_running(const char *app_id, bool *running)
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;
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;
243 *running = aul_app_is_running(app_id);
245 return APP_MANAGER_ERROR_NONE;
248 API int app_manager_get_shared_data_path(const char *app_id, char **path)
251 int retval = aul_get_app_shared_data_path_by_appid(app_id, path);
255 r = APP_MANAGER_ERROR_NONE;
258 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
261 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
264 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
267 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
274 API int app_manager_get_shared_resource_path(const char *app_id, char **path)
277 int retval = aul_get_app_shared_resource_path_by_appid(app_id, path);
281 r = APP_MANAGER_ERROR_NONE;
284 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
287 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
290 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
293 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
300 API int app_manager_get_shared_trusted_path(const char *app_id, char **path)
303 int retval = aul_get_app_shared_trusted_path_by_appid(app_id, path);
307 r = APP_MANAGER_ERROR_NONE;
310 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
313 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
316 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
319 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
326 API int app_manager_get_external_shared_data_path(const char *app_id, char **path)
329 int retval = aul_get_app_external_shared_data_path_by_appid(app_id, path);
333 r = APP_MANAGER_ERROR_NONE;
336 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
339 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
342 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
345 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);