Deprecate shared directory related API
[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         case APP_MANAGER_ERROR_NOT_SUPPORTED:
54                 return "Not supported";
55         default:
56                 return "Unknown";
57         }
58 }
59
60 int app_manager_error(app_manager_error_e error, const char* function, const char *description)
61 {
62         if (description)
63                 LOGE("[%s] %s(0x%08x) : %s", function, app_manager_error_to_string(error), error, description);
64         else
65                 LOGE("[%s] %s(0x%08x)", function, app_manager_error_to_string(error), error);
66
67         return error;
68 }
69
70 API int app_manager_set_app_context_event_cb(app_manager_app_context_event_cb callback, void *user_data)
71 {
72         int retval = app_context_set_event_cb(callback, user_data);
73
74         if (retval != APP_MANAGER_ERROR_NONE)
75                 return app_manager_error(retval, __FUNCTION__, NULL);
76         else
77                 return APP_MANAGER_ERROR_NONE;
78 }
79
80 API void app_manager_unset_app_context_event_cb(void)
81 {
82         app_context_unset_event_cb();
83 }
84
85 API int app_manager_foreach_app_context(app_manager_app_context_cb callback, void *user_data)
86 {
87         int retval = app_context_foreach_app_context(callback, user_data);
88
89         if (retval != APP_MANAGER_ERROR_NONE)
90                 return app_manager_error(retval, __FUNCTION__, NULL);
91         else
92                 return APP_MANAGER_ERROR_NONE;
93 }
94
95 API int app_manager_get_app_context(const char *app_id, app_context_h *app_context)
96 {
97         int retval = app_context_get_app_context(app_id, app_context);
98
99         if (retval != APP_MANAGER_ERROR_NONE)
100                 return app_manager_error(retval, __FUNCTION__, NULL);
101         else
102                 return APP_MANAGER_ERROR_NONE;
103 }
104
105 API int app_manager_resume_app(app_context_h app_context)
106 {
107         char *app_id;
108         int retval = APP_MANAGER_ERROR_NONE;
109
110         if (app_context == NULL)
111                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
112
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");
115
116         if (aul_app_is_running(app_id) == 0) {
117                 if (app_id) {
118                         free(app_id);
119                         app_id = NULL;
120                 }
121                 return app_manager_error(APP_MANAGER_ERROR_APP_NO_RUNNING, __FUNCTION__, NULL);
122         }
123
124         retval = aul_resume_app(app_id);
125
126         if (app_id)
127                 free(app_id);
128
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);
133         else if (retval < 0)
134                 return app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
135
136         return APP_MANAGER_ERROR_NONE;
137 }
138
139 API int app_manager_foreach_app_info(app_manager_app_info_cb callback, void *user_data)
140 {
141         int retval;
142
143         retval = app_info_foreach_app_info(callback, user_data);
144
145         if (retval != APP_MANAGER_ERROR_NONE)
146                 return app_manager_error(retval, __FUNCTION__, NULL);
147         else
148                 return APP_MANAGER_ERROR_NONE;
149 }
150
151 API int app_manager_get_app_info(const char *app_id, app_info_h *app_info)
152 {
153         int retval;
154
155         retval = app_info_create(app_id, app_info);
156
157         if (retval != APP_MANAGER_ERROR_NONE)
158                 return app_manager_error(retval, __FUNCTION__, NULL);
159         else
160                 return APP_MANAGER_ERROR_NONE;
161 }
162
163 API int app_manager_get_app_id(pid_t pid, char **app_id)
164 {
165         char buffer[256] = {0, };
166         char *app_id_dup = NULL;
167
168         if (app_id == NULL)
169                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
170
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");
173
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);
177
178         *app_id = app_id_dup;
179
180         return APP_MANAGER_ERROR_NONE;
181 }
182
183 API int app_manager_terminate_app(app_context_h app_context)
184 {
185         int retval;
186         pid_t pid = 0;
187
188         if (app_context == NULL)
189                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
190
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");
193
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;
203         }
204
205         return APP_MANAGER_ERROR_NONE;
206 }
207
208 API int app_manager_request_terminate_bg_app(app_context_h app_context)
209 {
210         int retval = APP_MANAGER_ERROR_NONE;
211         pid_t pid = 0;
212
213         if (app_context == NULL)
214                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
215
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");
218
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;
228         }
229
230         return APP_MANAGER_ERROR_NONE;
231 }
232
233 API int app_manager_is_running(const char *app_id, bool *running)
234 {
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;
238         }
239
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;
243         }
244
245         *running = aul_app_is_running(app_id);
246
247         return APP_MANAGER_ERROR_NONE;
248 }
249
250 API int app_manager_get_shared_data_path(const char *app_id, char **path)
251 {
252         int r;
253         int retval = aul_get_app_shared_data_path_by_appid(app_id, path);
254
255         switch (retval) {
256         case AUL_R_OK:
257                 r = APP_MANAGER_ERROR_NONE;
258                 break;
259         case AUL_R_ENOAPP:
260                 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
261                 break;
262         case AUL_R_EINVAL:
263                 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
264                 break;
265         case AUL_R_ERROR:
266                 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
267                 break;
268         case AUL_R_EREJECTED:
269                 r = app_manager_error(APP_MANAGER_ERROR_NOT_SUPPORTED, __FUNCTION__, NULL);
270                 break;
271         default:
272                 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
273                 break;
274         }
275
276         return r;
277 }
278
279 API int app_manager_get_shared_resource_path(const char *app_id, char **path)
280 {
281         int r;
282         int retval = aul_get_app_shared_resource_path_by_appid(app_id, path);
283
284         switch (retval) {
285         case AUL_R_OK:
286                 r = APP_MANAGER_ERROR_NONE;
287                 break;
288         case AUL_R_ENOAPP:
289                 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
290                 break;
291         case AUL_R_EINVAL:
292                 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
293                 break;
294         case AUL_R_ERROR:
295                 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
296                 break;
297         default:
298                 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
299                 break;
300         }
301
302         return r;
303 }
304
305 API int app_manager_get_shared_trusted_path(const char *app_id, char **path)
306 {
307         int r;
308         int retval = aul_get_app_shared_trusted_path_by_appid(app_id, path);
309
310         switch (retval) {
311         case AUL_R_OK:
312                 r = APP_MANAGER_ERROR_NONE;
313                 break;
314         case AUL_R_ENOAPP:
315                 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
316                 break;
317         case AUL_R_EINVAL:
318                 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
319                 break;
320         case AUL_R_ERROR:
321                 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
322                 break;
323         default:
324                 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
325                 break;
326         }
327
328         return r;
329 }
330
331 API int app_manager_get_external_shared_data_path(const char *app_id, char **path)
332 {
333         int r;
334         int retval = aul_get_app_external_shared_data_path_by_appid(app_id, path);
335
336         switch (retval) {
337         case AUL_R_OK:
338                 r = APP_MANAGER_ERROR_NONE;
339                 break;
340         case AUL_R_ENOAPP:
341                 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
342                 break;
343         case AUL_R_EINVAL:
344                 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
345                 break;
346         case AUL_R_ERROR:
347                 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
348                 break;
349         default:
350                 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
351                 break;
352         }
353
354         return r;
355 }