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";
53 case APP_MANAGER_ERROR_NOT_SUPPORTED:
54 return "Not supported";
60 int app_manager_error(app_manager_error_e error, const char* function, const char *description)
63 LOGE("[%s] %s(0x%08x) : %s", function, app_manager_error_to_string(error), error, description);
65 LOGE("[%s] %s(0x%08x)", function, app_manager_error_to_string(error), error);
70 API int app_manager_set_app_context_event_cb(app_manager_app_context_event_cb callback, void *user_data)
72 int retval = app_context_set_event_cb(callback, user_data);
74 if (retval != APP_MANAGER_ERROR_NONE)
75 return app_manager_error(retval, __FUNCTION__, NULL);
77 return APP_MANAGER_ERROR_NONE;
80 API void app_manager_unset_app_context_event_cb(void)
82 app_context_unset_event_cb();
85 API int app_manager_foreach_app_context(app_manager_app_context_cb callback, void *user_data)
87 int retval = app_context_foreach_app_context(callback, user_data);
89 if (retval != APP_MANAGER_ERROR_NONE)
90 return app_manager_error(retval, __FUNCTION__, NULL);
92 return APP_MANAGER_ERROR_NONE;
95 API int app_manager_get_app_context(const char *app_id, app_context_h *app_context)
97 int retval = app_context_get_app_context(app_id, app_context);
99 if (retval != APP_MANAGER_ERROR_NONE)
100 return app_manager_error(retval, __FUNCTION__, NULL);
102 return APP_MANAGER_ERROR_NONE;
105 API int app_manager_resume_app(app_context_h app_context)
108 int retval = APP_MANAGER_ERROR_NONE;
110 if (app_context == NULL)
111 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
113 if (app_context_get_app_id(app_context, &app_id) != APP_MANAGER_ERROR_NONE)
114 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, "failed to get the application ID");
116 if (aul_app_is_running(app_id) == 0) {
121 return app_manager_error(APP_MANAGER_ERROR_APP_NO_RUNNING, __FUNCTION__, NULL);
124 retval = aul_resume_app(app_id);
129 if (retval == AUL_R_EINVAL)
130 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
131 else if (retval == AUL_R_EILLACC)
132 return app_manager_error(APP_MANAGER_ERROR_PERMISSION_DENIED, __FUNCTION__, NULL);
134 return app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
136 return APP_MANAGER_ERROR_NONE;
139 API int app_manager_foreach_app_info(app_manager_app_info_cb callback, void *user_data)
143 retval = app_info_foreach_app_info(callback, user_data);
145 if (retval != APP_MANAGER_ERROR_NONE)
146 return app_manager_error(retval, __FUNCTION__, NULL);
148 return APP_MANAGER_ERROR_NONE;
151 API int app_manager_get_app_info(const char *app_id, app_info_h *app_info)
155 retval = app_info_create(app_id, app_info);
157 if (retval != APP_MANAGER_ERROR_NONE)
158 return app_manager_error(retval, __FUNCTION__, NULL);
160 return APP_MANAGER_ERROR_NONE;
163 API int app_manager_get_app_id(pid_t pid, char **app_id)
165 char buffer[256] = {0, };
166 char *app_id_dup = NULL;
169 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
171 if (aul_app_get_appid_bypid(pid, buffer, sizeof(buffer)) != AUL_R_OK)
172 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, "Invalid process ID");
174 app_id_dup = strdup(buffer);
175 if (app_id_dup == NULL)
176 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
178 *app_id = app_id_dup;
180 return APP_MANAGER_ERROR_NONE;
183 API int app_manager_terminate_app(app_context_h app_context)
188 if (app_context == NULL)
189 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
191 if (app_context_get_pid(app_context, &pid) != APP_MANAGER_ERROR_NONE)
192 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, "failed to get the process ID");
194 retval = aul_terminate_pid(pid);
195 if (retval == AUL_R_EINVAL) {
196 LOGE("[%s] APP_MANAGER_ERROR_INVALID_PARAMETER(0x%08x) : Invalid param", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
197 return APP_MANAGER_ERROR_INVALID_PARAMETER;
198 } else if (retval == AUL_R_EILLACC) {
199 LOGE("[%s] APP_MANAGER_ERROR_PERMISSION_DENIED(0x%08x) : Permission denied", __FUNCTION__, APP_MANAGER_ERROR_PERMISSION_DENIED);
200 return APP_MANAGER_ERROR_PERMISSION_DENIED;
201 } else if (retval < 0) {
202 return APP_MANAGER_ERROR_REQUEST_FAILED;
205 return APP_MANAGER_ERROR_NONE;
208 API int app_manager_request_terminate_bg_app(app_context_h app_context)
210 int retval = APP_MANAGER_ERROR_NONE;
213 if (app_context == NULL)
214 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
216 if (app_context_get_pid(app_context, &pid) != APP_MANAGER_ERROR_NONE)
217 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, "failed to get the process ID");
219 retval = aul_terminate_bgapp_pid(pid);
220 if (retval == AUL_R_EINVAL) {
221 LOGE("[%s] APP_MANAGER_ERROR_INVALID_PARAMETER(0x%08x) : Invalid param", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
222 return APP_MANAGER_ERROR_INVALID_PARAMETER;
223 } else if (retval == AUL_R_EILLACC) {
224 LOGE("[%s] APP_MANAGER_ERROR_PERMISSION_DENIED(0x%08x) : Permission denied", __FUNCTION__, APP_MANAGER_ERROR_PERMISSION_DENIED);
225 return APP_MANAGER_ERROR_PERMISSION_DENIED;
226 } else if (retval < 0) {
227 return APP_MANAGER_ERROR_REQUEST_FAILED;
230 return APP_MANAGER_ERROR_NONE;
233 API int app_manager_is_running(const char *app_id, bool *running)
235 if (app_id == NULL) {
236 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid package", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
237 return APP_MANAGER_ERROR_INVALID_PARAMETER;
240 if (running == NULL) {
241 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid output param", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
242 return APP_MANAGER_ERROR_INVALID_PARAMETER;
245 *running = aul_app_is_running(app_id);
247 return APP_MANAGER_ERROR_NONE;
250 API int app_manager_get_shared_data_path(const char *app_id, char **path)
253 int retval = aul_get_app_shared_data_path_by_appid(app_id, path);
257 r = APP_MANAGER_ERROR_NONE;
260 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
263 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
266 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
268 case AUL_R_EREJECTED:
269 r = app_manager_error(APP_MANAGER_ERROR_NOT_SUPPORTED, __FUNCTION__, NULL);
272 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
279 API int app_manager_get_shared_resource_path(const char *app_id, char **path)
282 int retval = aul_get_app_shared_resource_path_by_appid(app_id, path);
286 r = APP_MANAGER_ERROR_NONE;
289 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
292 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
295 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
298 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
305 API int app_manager_get_shared_trusted_path(const char *app_id, char **path)
308 int retval = aul_get_app_shared_trusted_path_by_appid(app_id, path);
312 r = APP_MANAGER_ERROR_NONE;
315 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
318 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
321 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
324 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
331 API int app_manager_get_external_shared_data_path(const char *app_id, char **path)
334 int retval = aul_get_app_external_shared_data_path_by_appid(app_id, path);
338 r = APP_MANAGER_ERROR_NONE;
341 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
344 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
347 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
350 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);