Release version 0.13.7
[platform/core/api/app-manager.git] / src / app_info.c
1 /*
2  * Copyright (c) 2011 - 2016 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 #include <fcntl.h>
23 #include <pthread.h>
24
25 #include <pkgmgr-info.h>
26 #include <package-manager.h>
27 #include <dlog.h>
28 #include <cynara-client.h>
29 #include <aul.h>
30 #include <aul_svc.h>
31
32 #include "app_info.h"
33 #include "app_manager.h"
34 #include "app_manager_internal.h"
35
36 #ifdef LOG_TAG
37 #undef LOG_TAG
38 #endif
39
40 #define LOG_TAG "TIZEN_N_APP_MANAGER"
41
42 #define SMACK_LABEL_LEN 255
43
44 #undef REGULAR_UID_MIN
45 #define REGULAR_UID_MIN 5000
46
47 struct app_info_s {
48         char *app_id;
49         pkgmgrinfo_appinfo_h pkg_app_info;
50 };
51
52 struct app_info_filter_s {
53         pkgmgrinfo_appinfo_filter_h pkg_app_info_filter;
54 };
55
56 struct app_info_metadata_filter_s {
57         pkgmgrinfo_appinfo_metadata_filter_h pkg_app_info_metadata_filter;
58 };
59
60 typedef struct _foreach_context_ {
61         app_manager_app_info_cb callback;
62         void *user_data;
63 } foreach_context_s;
64
65 typedef struct _foreach_metada_context_ {
66         app_info_metadata_cb callback;
67         void *user_data;
68 } foreach_metadata_context_s;
69
70 typedef struct _foreach_category_ {
71         app_info_category_cb callback;
72         void *user_data;
73 } foreach_category_context_s;
74
75 typedef struct _foreach_res_control_ {
76         app_info_res_control_cb callback;
77         void *user_data;
78 } foreach_res_control_context_s;
79
80 typedef struct app_context_s {
81         char appid[256];
82         bool initialized;
83 } app_context_t;
84
85 static app_context_t __context;
86 static pthread_mutex_t __mutex = PTHREAD_MUTEX_INITIALIZER;
87
88 static int app_info_convert_str_property(const char *property, char **converted_property)
89 {
90         if (property == NULL)
91                 return -1;
92
93         if (strcmp(property, PACKAGE_INFO_PROP_APP_ID) == 0)
94                 *converted_property = PMINFO_APPINFO_PROP_APP_ID;
95         else if (strcmp(property, PACKAGE_INFO_PROP_APP_TYPE) == 0)
96                 *converted_property = PMINFO_APPINFO_PROP_APP_TYPE;
97         else if (strcmp(property, PACKAGE_INFO_PROP_APP_CATEGORY) == 0)
98                 *converted_property = PMINFO_APPINFO_PROP_APP_CATEGORY;
99         else if (strcmp(property, PACKAGE_INFO_PROP_APP_INSTALLED_STORAGE) == 0)
100                 *converted_property = PMINFO_APPINFO_PROP_APP_INSTALLED_STORAGE;
101         else if (strcmp(property, PACKAGE_INFO_PROP_APP_COMPONENT_TYPE) == 0)
102                 *converted_property = PMINFO_APPINFO_PROP_APP_COMPONENT;
103         else
104                 return -1;
105
106         return 0;
107 }
108
109 static int app_info_convert_bool_property(const char *property, char **converted_property)
110 {
111         if (property == NULL)
112                 return -1;
113
114         if (strcmp(property, PACKAGE_INFO_PROP_APP_NODISPLAY) == 0)
115                 *converted_property = PMINFO_APPINFO_PROP_APP_NODISPLAY;
116         else if (strcmp(property, PACKAGE_INFO_PROP_APP_TASKMANAGE) == 0)
117                 *converted_property = PMINFO_APPINFO_PROP_APP_TASKMANAGE;
118         else if (strcmp(property, PACKAGE_INFO_PROP_APP_DISABLED) == 0)
119                 *converted_property = PMINFO_APPINFO_PROP_APP_DISABLE;
120         else
121                 return -1;
122
123         return 0;
124 }
125
126 static int app_info_convert_app_component(pkgmgrinfo_app_component component, app_info_app_component_type_e *converted_component)
127 {
128         if (component == PMINFO_UI_APP)
129                 *converted_component = APP_INFO_APP_COMPONENT_TYPE_UI_APP;
130         else if (component == PMINFO_SVC_APP)
131                 *converted_component = APP_INFO_APP_COMPONENT_TYPE_SERVICE_APP;
132         else if (component == PMINFO_WIDGET_APP)
133                 *converted_component = APP_INFO_APP_COMPONENT_TYPE_WIDGET_APP;
134         else if (component == PMINFO_WATCH_APP)
135                 *converted_component = APP_INFO_APP_COMPONENT_TYPE_WATCH_APP;
136         else if (component == PMINFO_COMPONENT_BASED_APP)
137                 *converted_component = APP_INFO_APP_COMPONENT_TYPE_COMPONENT_BASED_APP;
138         else
139                 return -1;
140
141         return 0;
142 }
143
144 static int app_info_foreach_app_filter_cb(pkgmgrinfo_appinfo_h handle, void *user_data)
145 {
146         int retval = 0;
147         char *appid = NULL;
148         app_info_h info = NULL;
149         bool iteration_next = true;
150
151         info = calloc(1, sizeof(struct app_info_s));
152         if (info == NULL)
153                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
154
155         foreach_context_s *foreach_context = user_data;
156         if (handle == NULL || foreach_context == NULL) {
157                 /* LCOV_EXCL_START */
158                 free(info);
159                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
160                 /* LCOV_EXCL_STOP */
161         }
162
163         retval = pkgmgrinfo_appinfo_get_appid(handle, &appid);
164         if (retval < 0) {
165                 /* LCOV_EXCL_START */
166                 free(info);
167                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
168                 /* LCOV_EXCL_STOP */
169         }
170
171         info->app_id = strdup(appid);
172         if (info->app_id == NULL) {
173                 /* LCOV_EXCL_START */
174                 if (info) {
175                         free(info);
176                         info = NULL;
177                 }
178                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
179                 /* LCOV_EXCL_STOP */
180         }
181         info->pkg_app_info = handle;
182
183         iteration_next = foreach_context->callback(info, foreach_context->user_data);
184
185         free(info->app_id);
186         free(info);
187
188         if (iteration_next == true)
189                 return PMINFO_R_OK;
190
191         return PMINFO_R_ERROR;
192 }
193
194 static int app_info_foreach_app_metadata_cb(const char *metadata_key, const char *metadata_value, void *user_data)
195 {
196         foreach_metadata_context_s *foreach_context = user_data;
197         bool iteration_next = true;
198
199         if (metadata_value == NULL || foreach_context == NULL)
200                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
201
202         iteration_next = foreach_context->callback(metadata_key, metadata_value, foreach_context->user_data);
203         if (iteration_next == true)
204                 return PMINFO_R_OK;
205
206         return PMINFO_R_ERROR;
207 }
208
209 static int app_info_foreach_category_cb(const char *category_name, void *user_data)
210 {
211         foreach_category_context_s *foreach_category = user_data;
212         bool iteration_next = true;
213
214         if (category_name == NULL || foreach_category == NULL)
215                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
216
217         iteration_next = foreach_category->callback(category_name, foreach_category->user_data);
218         if (iteration_next == true)
219                 return PMINFO_R_OK;
220
221         return PMINFO_R_ERROR;
222 }
223
224 static int app_info_foreach_app_info_cb(pkgmgrinfo_appinfo_h handle, void *cb_data)
225 {
226         foreach_context_s *foreach_context = cb_data;
227         app_info_h app_info = NULL;
228         char *appid = NULL;
229         int ret = 0;
230         bool iteration_next = true;
231
232         if (handle == NULL || foreach_context == NULL) {
233                 app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
234                 return PMINFO_R_ERROR;
235         }
236
237         ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
238         if (ret != PMINFO_R_OK) {
239                 /* LCOV_EXCL_START */
240                 app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
241                 return PMINFO_R_ERROR;
242                 /* LCOV_EXCL_STOP */
243         }
244
245         app_info = calloc(1, sizeof(struct app_info_s));
246         if (app_info == NULL) {
247                 /* LCOV_EXCL_START */
248                 app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
249                 return PMINFO_R_ERROR;
250                 /* LCOV_EXCL_STOP */
251         }
252
253         app_info->app_id = strdup(appid);
254         app_info->pkg_app_info = handle;
255         iteration_next = foreach_context->callback(app_info, foreach_context->user_data);
256
257         free(app_info->app_id);
258         free(app_info);
259
260         if (iteration_next == true)
261                 return PMINFO_R_OK;
262
263         return PMINFO_R_ERROR;
264 }
265
266 static int app_info_foreach_res_control_cb(const char *res_type,
267                 const char *min_res_version, const char *max_res_version,
268                 const char *auto_close, void *user_data)
269 {
270         foreach_res_control_context_s *foreach_context = user_data;
271         bool iteration_next = true;
272
273         if (res_type == NULL)
274                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
275
276         iteration_next = foreach_context->callback(res_type, min_res_version,
277                         max_res_version, auto_close,
278                         foreach_context->user_data);
279         if (iteration_next == true)
280                 return PMINFO_R_OK;
281
282         return PMINFO_R_ERROR;
283 }
284
285 int app_info_foreach_app_info(app_manager_app_info_cb callback, void *user_data)
286 {
287         foreach_context_s foreach_context = {
288                 .callback = callback,
289                 .user_data = user_data,
290         };
291
292         if (callback == NULL)
293                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
294
295         pkgmgrinfo_appinfo_get_usr_installed_list(app_info_foreach_app_info_cb, getuid(), &foreach_context);
296
297         return APP_MANAGER_ERROR_NONE;
298 }
299
300 static int _check_privilege(char *privilege)
301 {
302         cynara *p_cynara;
303         int fd;
304         int ret;
305         char client[SMACK_LABEL_LEN + 1] = {0,};
306         char uid[10] = {0,};
307         char *client_session = "";
308
309         if (privilege == NULL) {
310                 LOGE("invalid parameter");
311                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
312         }
313
314         ret = cynara_initialize(&p_cynara, NULL);
315         if (ret != CYNARA_API_SUCCESS) {
316                 /* LCOV_EXCL_START */
317                 LOGE("cynara_initialize [%d] failed!", ret);
318                 return APP_MANAGER_ERROR_IO_ERROR;
319                 /* LCOV_EXCL_STOP */
320         }
321
322         fd = open("/proc/self/attr/current", O_RDONLY);
323         if (fd < 0) {
324                 /* LCOV_EXCL_START */
325                 LOGE("open [%d] failed!", errno);
326                 ret = APP_MANAGER_ERROR_IO_ERROR;
327                 goto out;
328                 /* LCOV_EXCL_STOP */
329         }
330
331         ret = read(fd, client, SMACK_LABEL_LEN);
332         if (ret < 0) {
333                 /* LCOV_EXCL_START */
334                 LOGE("read [%d] failed!", errno);
335                 close(fd);
336                 ret = APP_MANAGER_ERROR_IO_ERROR;
337                 goto out;
338                 /* LCOV_EXCL_STOP */
339         }
340         close(fd);
341         snprintf(uid, 10, "%d", getuid());
342
343         ret = cynara_check(p_cynara, client, client_session, uid, privilege);
344         if (ret != CYNARA_API_ACCESS_ALLOWED) {
345                 LOGE("cynara access check [%d] failed!", ret);
346
347                 if (ret == CYNARA_API_ACCESS_DENIED)
348                         ret = APP_MANAGER_ERROR_PERMISSION_DENIED;
349                 else
350                         ret = APP_MANAGER_ERROR_IO_ERROR;
351
352                 goto out;
353         }
354         ret = APP_MANAGER_ERROR_NONE;
355
356 out:
357         if (p_cynara)
358                 cynara_finish(p_cynara);
359
360         return ret;
361 }
362
363 static void __app_context_initialize(void)
364 {
365         pthread_mutex_lock(&__mutex);
366         if (__context.initialized) {
367                 pthread_mutex_unlock(&__mutex);
368                 return;
369         }
370
371         if (getuid() < REGULAR_UID_MIN) {
372                 __context.initialized = true;
373                 pthread_mutex_unlock(&__mutex);
374                 return;
375         }
376
377         aul_app_get_appid_bypid(getpid(), __context.appid,
378                         sizeof(__context.appid));
379         __context.initialized = true;
380         pthread_mutex_unlock(&__mutex);
381 }
382
383 static int __app_info_create_by_alias_appid(const char *app_id,
384                 app_info_h app_info)
385 {
386         pkgmgrinfo_appinfo_h appinfo;
387         char *real_appid = NULL;
388         int ret;
389
390         __app_context_initialize();
391         if (__context.appid[0] != '\0' && !strcmp(__context.appid, app_id))
392                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
393
394         ret = aul_svc_get_appid_by_alias_appid(app_id, &real_appid);
395         if (ret != AUL_SVC_RET_OK || real_appid == NULL)
396                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
397
398         /* LCOV_EXCL_START */
399         ret = pkgmgrinfo_appinfo_get_usr_appinfo(real_appid, getuid(),
400                         &appinfo);
401         free(real_appid);
402         if (ret != PMINFO_R_OK)
403                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
404
405         app_info->app_id = strdup(app_id);
406         app_info->pkg_app_info = appinfo;
407         /* LCOV_EXCL_STOP */
408
409         return APP_MANAGER_ERROR_NONE;
410 }
411
412 API int app_info_create(const char *app_id, app_info_h *app_info)
413 {
414         pkgmgrinfo_pkginfo_h pkginfo = NULL;
415         pkgmgrinfo_appinfo_h appinfo = NULL;
416         app_info_h info = NULL;
417         int retval = 0;
418         char *main_appid = NULL;
419
420         if (app_id == NULL || app_info == NULL)
421                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
422
423         info = calloc(1, sizeof(struct app_info_s));
424         if (info == NULL)
425                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
426
427         retval = __app_info_create_by_alias_appid(app_id, info);
428         if (retval == APP_MANAGER_ERROR_NONE) {
429                 *app_info = info;
430                 return APP_MANAGER_ERROR_NONE;
431         }
432
433         retval = pkgmgrinfo_appinfo_get_usr_appinfo(app_id, getuid(), &appinfo);
434         if (!retval) {
435                 info->app_id = strdup(app_id);
436                 info->pkg_app_info = appinfo;
437                 *app_info = info;
438                 return APP_MANAGER_ERROR_NONE;
439         }
440
441         retval = pkgmgrinfo_pkginfo_get_usr_pkginfo(app_id, getuid(), &pkginfo);
442         if (retval < 0) {
443                 free(info);
444                 return app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
445         }
446
447         retval = pkgmgrinfo_pkginfo_get_mainappid(pkginfo, &main_appid);
448         if (retval < 0) {
449                 free(info);
450                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
451                 return app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
452         }
453         if (pkgmgrinfo_appinfo_get_usr_appinfo(main_appid, getuid(), &appinfo)) {
454                 free(info);
455                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
456                 return app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
457         }
458
459         info->app_id = strdup(main_appid);
460         info->pkg_app_info = appinfo;
461         *app_info = info;
462
463         pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
464
465         return APP_MANAGER_ERROR_NONE;
466 }
467
468 API int app_info_destroy(app_info_h app_info)
469 {
470         if (app_info == NULL)
471                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
472
473         if (app_info->app_id)
474                 free(app_info->app_id);
475
476         pkgmgrinfo_appinfo_destroy_appinfo(app_info->pkg_app_info);
477         free(app_info);
478         return APP_MANAGER_ERROR_NONE;
479 }
480
481 API int app_info_get_app_id(app_info_h app_info, char **app_id)
482 {
483         char *app_id_dup = NULL;
484
485         if (app_info == NULL || app_id == NULL)
486                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
487
488         if (app_info->app_id == NULL)
489                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
490
491         app_id_dup = strdup(app_info->app_id);
492         if (app_id_dup == NULL)
493                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
494
495         *app_id = app_id_dup;
496
497         return APP_MANAGER_ERROR_NONE;
498 }
499
500 API int app_info_get_exec(app_info_h app_info, char **exec)
501 {
502         char *val = NULL;
503         char *app_exec_dup = NULL;
504         int ret = -1;
505
506         if (app_info == NULL || exec == NULL)
507                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
508
509         ret = pkgmgrinfo_appinfo_get_exec(app_info->pkg_app_info, &val);
510         if (ret != PMINFO_R_OK || val == NULL)
511                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
512
513         app_exec_dup = strdup(val);
514         if (app_exec_dup == NULL)
515                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
516
517         *exec = app_exec_dup;
518
519         return APP_MANAGER_ERROR_NONE;
520 }
521
522 API int app_info_get_label(app_info_h app_info, char **label)
523 {
524         char *val = NULL;
525         char *app_label_dup = NULL;
526         int ret = 0;
527
528         if (app_info == NULL || label == NULL)
529                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
530
531         ret = pkgmgrinfo_appinfo_get_label(app_info->pkg_app_info, &val);
532         if (ret < 0 || val == NULL)
533                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
534
535         app_label_dup = strdup(val);
536         if (app_label_dup == NULL)
537                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
538
539         *label = app_label_dup;
540
541         return APP_MANAGER_ERROR_NONE;
542 }
543
544 API int app_info_get_localed_label(const char *app_id, const char *locale, char **label)
545 {
546         char *val = NULL;
547
548         if (app_id == NULL || locale == NULL || label == NULL)
549                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
550
551         if (pkgmgrinfo_appinfo_usr_get_localed_label(app_id, locale, getuid(), &val))
552                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
553
554         *label = val;
555
556         return APP_MANAGER_ERROR_NONE;
557 }
558
559 API int app_info_get_icon(app_info_h app_info, char **path)
560 {
561         char *val = NULL;
562         char *app_icon_dup = NULL;
563         int ret = -1;
564
565         if (app_info == NULL || path == NULL)
566                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
567
568         ret = pkgmgrinfo_appinfo_get_icon(app_info->pkg_app_info, &val);
569         if (ret != PMINFO_R_OK || val == NULL)
570                 return APP_MANAGER_ERROR_INVALID_PARAMETER;
571
572         app_icon_dup = strdup(val);
573         if (app_icon_dup == NULL)
574                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
575
576         *path = app_icon_dup;
577
578         return APP_MANAGER_ERROR_NONE;
579 }
580
581 API int app_info_get_package(app_info_h app_info, char **package)
582 {
583         char *val = NULL;
584         char *app_package_dup = NULL;
585         int ret = 0;
586
587         if (app_info == NULL || package == NULL)
588                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
589
590         ret = pkgmgrinfo_appinfo_get_pkgname(app_info->pkg_app_info, &val);
591         if (ret < 0)
592                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
593         if (val == NULL)
594                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
595
596         app_package_dup = strdup(val);
597         if (app_package_dup == NULL)
598                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
599
600         *package = app_package_dup;
601
602         return APP_MANAGER_ERROR_NONE;
603 }
604
605 API int app_info_get_type(app_info_h app_info, char **type)
606 {
607         char *val = NULL;
608         char *app_type_dup = NULL;
609         int ret = 0;
610
611         if (app_info == NULL || type == NULL)
612                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
613
614         ret = pkgmgrinfo_appinfo_get_apptype(app_info->pkg_app_info, &val);
615         if (ret < 0)
616                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
617         if (val == NULL)
618                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
619
620         app_type_dup = strdup(val);
621         if (app_type_dup == NULL)
622                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
623
624         *type = app_type_dup;
625
626         return APP_MANAGER_ERROR_NONE;
627 }
628
629 API int app_info_get_app_component_type(app_info_h app_info, app_info_app_component_type_e *type)
630 {
631         pkgmgrinfo_app_component comp_val;
632         app_info_app_component_type_e converted_comp_val;
633         int ret = 0;
634
635         if (app_info == NULL || type == NULL)
636                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
637
638         ret = pkgmgrinfo_appinfo_get_component(app_info->pkg_app_info, &comp_val);
639         if (ret < 0)
640                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
641
642         ret = app_info_convert_app_component(comp_val, &converted_comp_val);
643         if (ret < 0)
644                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
645
646         *type = converted_comp_val;
647
648         return APP_MANAGER_ERROR_NONE;
649 }
650
651 API int app_info_foreach_metadata(app_info_h app_info, app_info_metadata_cb callback, void *user_data)
652 {
653         int retval = 0;
654
655         if (app_info == NULL || callback == NULL)
656                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
657
658         foreach_metadata_context_s foreach_context = {
659                 .callback = callback,
660                 .user_data = user_data,
661         };
662
663         retval = pkgmgrinfo_appinfo_foreach_metadata(app_info->pkg_app_info, app_info_foreach_app_metadata_cb, &foreach_context);
664         if (retval < 0)
665                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
666
667         return APP_MANAGER_ERROR_NONE;
668 }
669
670 API int app_info_is_nodisplay(app_info_h app_info, bool *nodisplay)
671 {
672         bool val;
673
674         if (app_info == NULL || nodisplay == NULL)
675                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
676
677         if (pkgmgrinfo_appinfo_is_nodisplay(app_info->pkg_app_info, &val) < 0)
678                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
679
680         *nodisplay = val;
681         return APP_MANAGER_ERROR_NONE;
682 }
683
684 API int app_info_is_enabled(app_info_h app_info, bool *enabled)
685 {
686         bool val;
687
688         if (app_info == NULL || enabled == NULL)
689                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
690
691         if (pkgmgrinfo_appinfo_is_enabled(app_info->pkg_app_info, &val) < 0)
692                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
693
694         *enabled = val;
695         return APP_MANAGER_ERROR_NONE;
696
697 }
698
699 API int app_info_is_equal(app_info_h lhs, app_info_h rhs, bool *equal)
700 {
701         if (lhs == NULL || rhs == NULL || equal == NULL)
702                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
703
704         if (!strcmp(lhs->app_id, rhs->app_id))
705                 *equal = true;
706         else
707                 *equal = false;
708
709         return APP_MANAGER_ERROR_NONE;
710 }
711
712 API int app_info_is_onboot(app_info_h app_info, bool *onboot)
713 {
714         bool val;
715
716         if (app_info == NULL || onboot == NULL)
717                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
718
719         if (pkgmgrinfo_appinfo_is_onboot(app_info->pkg_app_info, &val) < 0)
720                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
721
722         *onboot = val;
723         return APP_MANAGER_ERROR_NONE;
724 }
725
726 API int app_info_is_preload(app_info_h app_info, bool *preload)
727 {
728         bool val;
729
730         if (app_info == NULL || preload == NULL)
731                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
732
733         if (pkgmgrinfo_appinfo_is_preload(app_info->pkg_app_info, &val) < 0)
734                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
735
736         *preload = val;
737         return APP_MANAGER_ERROR_NONE;
738 }
739
740 API int app_info_is_support_ambient(app_info_h app_info, bool *support_ambient)
741 {
742         bool val;
743
744         if (app_info == NULL || support_ambient == NULL)
745                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
746
747         if (pkgmgrinfo_appinfo_is_support_ambient(app_info->pkg_app_info, &val) < 0)
748                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
749
750         *support_ambient = val;
751         return APP_MANAGER_ERROR_NONE;
752 }
753
754 API int app_info_clone(app_info_h *clone, app_info_h app_info)
755 {
756         app_info_h info;
757
758         if (clone == NULL || app_info == NULL)
759                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
760
761         info = calloc(1, sizeof(struct app_info_s));
762         if (info == NULL)
763                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
764
765         info->app_id = strdup(app_info->app_id);
766         if (info->app_id == NULL) {
767                 /* LCOV_EXCL_START */
768                 free(info);
769                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
770                 /* LCOV_EXCL_STOP */
771         }
772
773         if (pkgmgrinfo_appinfo_clone_appinfo(app_info->pkg_app_info, &(info->pkg_app_info)) < 0) {
774                 /* LCOV_EXCL_START */
775                 free(info->app_id);
776                 free(info);
777                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
778                 /* LCOV_EXCL_STOP */
779         }
780
781         *clone = info;
782
783         return APP_MANAGER_ERROR_NONE;
784 }
785
786 API int app_info_foreach_category(app_info_h app_info, app_info_category_cb callback, void *user_data)
787 {
788         int retval;
789         if (app_info == NULL || callback == NULL)
790                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
791
792         retval = _check_privilege(PRIVILEGE_PACKAGE_MANAGER_ADMIN);
793         if (retval != APP_MANAGER_ERROR_NONE)
794                 return app_manager_error(APP_MANAGER_ERROR_PERMISSION_DENIED, __FUNCTION__, NULL);
795
796         foreach_category_context_s foreach_category = {
797                 .callback = callback,
798                 .user_data = user_data,
799         };
800
801         retval = pkgmgrinfo_appinfo_foreach_category(app_info->pkg_app_info, app_info_foreach_category_cb, &foreach_category);
802         if (retval != PMINFO_R_OK)
803                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
804
805         return APP_MANAGER_ERROR_NONE;
806 }
807
808 API int app_info_filter_create(app_info_filter_h *handle)
809 {
810         int retval = 0;
811         app_info_filter_h filter_created = NULL;
812         pkgmgrinfo_appinfo_filter_h filter_h = NULL;
813
814         if (handle == NULL)
815                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
816
817         retval = pkgmgrinfo_appinfo_filter_create(&filter_h);
818         if (retval < 0)
819                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
820
821         filter_created = calloc(1, sizeof(struct app_info_filter_s));
822         if (filter_created == NULL) {
823                 /* LCOV_EXCL_START */
824                 free(filter_h);
825                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
826                 /* LCOV_EXCL_STOP */
827         }
828
829         filter_created->pkg_app_info_filter = filter_h;
830
831         *handle = filter_created;
832
833         return APP_MANAGER_ERROR_NONE;
834 }
835
836 API int app_info_filter_destroy(app_info_filter_h handle)
837 {
838         int retval = 0;
839
840         if (handle == NULL)
841                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
842
843         retval = pkgmgrinfo_appinfo_filter_destroy(handle->pkg_app_info_filter);
844         if (retval < 0)
845                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
846
847         free(handle);
848         return APP_MANAGER_ERROR_NONE;
849 }
850
851 API int app_info_filter_add_bool(app_info_filter_h handle, const char *property, const bool value)
852 {
853         int retval = 0;
854         char *converted_property = NULL;
855
856         if ((handle == NULL) || (property == NULL))
857                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
858
859         retval = app_info_convert_bool_property(property, &converted_property);
860         if (retval < 0)
861                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
862
863         retval = pkgmgrinfo_appinfo_filter_add_bool(handle->pkg_app_info_filter, converted_property, value);
864         if (retval < 0)
865                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
866
867         return APP_MANAGER_ERROR_NONE;
868 }
869
870 API int app_info_filter_add_string(app_info_filter_h handle, const char *property, const char *value)
871 {
872         int retval = 0;
873         char *converted_property = NULL;
874
875         if ((handle == NULL) || (property == NULL))
876                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
877
878         retval = app_info_convert_str_property(property, &converted_property);
879         if (retval < 0)
880                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
881
882         retval = pkgmgrinfo_appinfo_filter_add_string(handle->pkg_app_info_filter, converted_property, value);
883         if (retval < 0)
884                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
885
886         return APP_MANAGER_ERROR_NONE;
887 }
888
889 API int app_info_filter_count_appinfo(app_info_filter_h handle, int *count)
890 {
891         int retval = 0;
892
893         if ((handle == NULL) || (count == NULL))
894                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
895
896         retval = pkgmgrinfo_appinfo_filter_count(handle->pkg_app_info_filter, count);
897         if (retval < 0)
898                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
899
900         return APP_MANAGER_ERROR_NONE;
901 }
902
903 API int app_info_filter_foreach_appinfo(app_info_filter_h handle, app_info_filter_cb callback, void *user_data)
904 {
905         int retval = 0;
906
907         foreach_context_s foreach_context = {
908                 .callback = callback,
909                 .user_data = user_data,
910         };
911
912         if ((handle == NULL) || (callback == NULL))
913                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
914
915         retval = pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(handle->pkg_app_info_filter, app_info_foreach_app_filter_cb, &foreach_context, getuid());
916         if (retval < 0)
917                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
918
919         return APP_MANAGER_ERROR_NONE;
920 }
921
922 API int app_info_metadata_filter_create(app_info_metadata_filter_h *handle)
923 {
924         int retval = 0;
925         app_info_metadata_filter_h filter_created = NULL;
926         pkgmgrinfo_appinfo_metadata_filter_h filter_h = NULL;
927
928         if (handle == NULL)
929                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
930
931         filter_created = calloc(1, sizeof(struct app_info_metadata_filter_s));
932         if (filter_created == NULL)
933                 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
934
935         retval = pkgmgrinfo_appinfo_metadata_filter_create(&filter_h);
936         if (retval < 0) {
937                 /* LCOV_EXCL_START */
938                 free(filter_created);
939                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
940                 /* LCOV_EXCL_STOP */
941         }
942
943         filter_created->pkg_app_info_metadata_filter = filter_h;
944
945         *handle = filter_created;
946
947         return APP_MANAGER_ERROR_NONE;
948 }
949
950 API int app_info_metadata_filter_destroy(app_info_metadata_filter_h handle)
951 {
952         int retval = 0;
953
954         if (handle == NULL)
955                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
956
957         retval = pkgmgrinfo_appinfo_metadata_filter_destroy(handle->pkg_app_info_metadata_filter);
958         if (retval < 0)
959                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
960
961         free(handle);
962         return APP_MANAGER_ERROR_NONE;
963 }
964
965 API int app_info_metadata_filter_add(app_info_metadata_filter_h handle, const char *key, const char *value)
966 {
967         int retval = 0;
968
969         if ((handle == NULL) || (key == NULL))
970                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
971
972         retval = pkgmgrinfo_appinfo_metadata_filter_add(handle->pkg_app_info_metadata_filter, key, value);
973         if (retval < 0)
974                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
975
976         return APP_MANAGER_ERROR_NONE;
977 }
978
979 API int app_info_metadata_filter_foreach(app_info_metadata_filter_h handle, app_info_filter_cb callback, void *user_data)
980 {
981         int retval = 0;
982
983         foreach_context_s foreach_context = {
984                 .callback = callback,
985                 .user_data = user_data,
986         };
987
988         if (handle == NULL)
989                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
990
991         if (callback == NULL)
992                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
993
994         retval = pkgmgrinfo_appinfo_usr_metadata_filter_foreach(handle->pkg_app_info_metadata_filter, app_info_foreach_app_filter_cb, &foreach_context, getuid());
995         if (retval < 0)
996                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
997
998         return APP_MANAGER_ERROR_NONE;
999 }
1000
1001 API int app_info_foreach_res_control(app_info_h app_info, app_info_res_control_cb callback, void *user_data)
1002 {
1003         int retval = 0;
1004
1005         if (app_info == NULL || callback == NULL)
1006                 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1007
1008         foreach_res_control_context_s foreach_context = {
1009                 .callback = callback,
1010                 .user_data = user_data,
1011         };
1012
1013         retval = pkgmgrinfo_appinfo_foreach_res_control(app_info->pkg_app_info, app_info_foreach_res_control_cb, &foreach_context);
1014         if (retval < 0)
1015                 return app_manager_error(APP_MANAGER_ERROR_IO_ERROR, __FUNCTION__, NULL);
1016
1017         return APP_MANAGER_ERROR_NONE;
1018 }