2 * Copyright (c) 2019 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.
24 #include <pkgmgr-info.h>
26 #include <aul_comp_info.h>
29 #include "rua_private.h"
31 #include "rua_info_internal.h"
33 #define RUA_INFO_START 0
36 RUA_INFO_APP_ID = RUA_INFO_START,
41 RUA_INFO_INSTANCE_NAME,
45 RUA_INFO_COMPONENT_ID,
52 char *value[RUA_INFO_MAX];
55 typedef int (*rua_info_add_cb)(struct rua_rec *rec,
56 struct rua_info_s *info);
57 typedef void (*rua_info_remove_cb)(void *data);
58 typedef int (*rua_info_clone_cb)(struct rua_info_s *info,
59 struct rua_info_s *clone);
61 typedef struct _rua_info_vft {
63 rua_info_remove_cb dtor;
64 rua_info_clone_cb clnr;
67 static int __rua_info_add_app_id(struct rua_rec *rec,
68 struct rua_info_s *info)
70 info->value[RUA_INFO_APP_ID] = strdup(rec->pkg_name);
71 if (!info->value[RUA_INFO_APP_ID]) {
73 return RUA_ERROR_OUT_OF_MEMORY;
76 return RUA_ERROR_NONE;
79 static int __rua_info_add_app_path(struct rua_rec *rec,
80 struct rua_info_s *info)
82 info->value[RUA_INFO_APP_PATH] = strdup(rec->app_path);
83 if (!info->value[RUA_INFO_APP_PATH]) {
85 return RUA_ERROR_OUT_OF_MEMORY;
88 return RUA_ERROR_NONE;
91 static int __rua_info_add_args(struct rua_rec *rec,
92 struct rua_info_s *info)
94 info->value[RUA_INFO_ARGS] = strdup(rec->arg);
95 if (!info->value[RUA_INFO_ARGS]) {
97 return RUA_ERROR_OUT_OF_MEMORY;
100 return RUA_ERROR_NONE;
103 static int __rua_info_add_launch_time(struct rua_rec *rec,
104 struct rua_info_s *info)
106 info->value[RUA_INFO_LAUNCH_TIME] = GINT_TO_POINTER(rec->launch_time);
108 return RUA_ERROR_NONE;
111 static int __rua_info_add_instance_id(struct rua_rec *rec,
112 struct rua_info_s *info)
114 if (!rec->instance_id)
115 return RUA_ERROR_NONE;
117 info->value[RUA_INFO_INSTANCE_ID] = strdup(rec->instance_id);
118 if (!info->value[RUA_INFO_INSTANCE_ID]) {
120 return RUA_ERROR_OUT_OF_MEMORY;
123 return RUA_ERROR_NONE;
126 static int __rua_info_add_instance_name(struct rua_rec *rec,
127 struct rua_info_s *info)
129 if (!rec->instance_name)
130 return RUA_ERROR_NONE;
132 info->value[RUA_INFO_INSTANCE_NAME] = strdup(rec->instance_name);
133 if (!info->value[RUA_INFO_INSTANCE_NAME]) {
135 return RUA_ERROR_OUT_OF_MEMORY;
138 return RUA_ERROR_NONE;
141 static int __rua_info_add_icon(struct rua_rec *rec, struct rua_info_s *info)
144 return RUA_ERROR_NONE;
146 info->value[RUA_INFO_ICON] = strdup(rec->icon);
147 if (!info->value[RUA_INFO_ICON]) {
149 return RUA_ERROR_OUT_OF_MEMORY;
152 return RUA_ERROR_NONE;
155 static int __rua_info_add_uri(struct rua_rec *rec, struct rua_info_s *info)
158 return RUA_ERROR_NONE;
160 info->value[RUA_INFO_URI] = strdup(rec->uri);
161 if (!info->value[RUA_INFO_URI]) {
163 return RUA_ERROR_OUT_OF_MEMORY;
166 return RUA_ERROR_NONE;
169 static int __rua_info_add_image(struct rua_rec *rec, struct rua_info_s *info)
172 return RUA_ERROR_NONE;
174 info->value[RUA_INFO_IMAGE] = strdup(rec->image);
175 if (!info->value[RUA_INFO_IMAGE]) {
177 return RUA_ERROR_OUT_OF_MEMORY;
180 return RUA_ERROR_NONE;
183 static int __rua_info_add_component_id(struct rua_rec *rec,
184 struct rua_info_s *info)
187 return RUA_ERROR_NONE;
189 info->value[RUA_INFO_COMPONENT_ID] = strdup(rec->comp_id);
190 if (!info->value[RUA_INFO_COMPONENT_ID]) {
192 return RUA_ERROR_OUT_OF_MEMORY;
195 return RUA_ERROR_NONE;
198 static int __rua_info_add_app_info(struct rua_rec *rec,
199 struct rua_info_s *info)
201 pkgmgrinfo_appinfo_h app_info;
204 ret = pkgmgrinfo_appinfo_get_appinfo(rec->pkg_name, &app_info);
205 if (ret != PMINFO_R_OK) {
206 _E("Failed to get appinfo");
207 return RUA_ERROR_IO_ERROR;
210 info->value[RUA_INFO_APP_INFO] = app_info;
212 return RUA_ERROR_NONE;
215 static int __rua_info_add_comp_info(struct rua_rec *rec,
216 struct rua_info_s *info)
218 aul_comp_info_h comp_info;
222 return RUA_ERROR_NONE;
224 ret = aul_comp_info_create(rec->comp_id, &comp_info);
225 if (ret != AUL_R_OK) {
226 _E("Failed to create comp info");
227 return RUA_ERROR_IO_ERROR;
230 info->value[RUA_INFO_COMP_INFO] = comp_info;
232 return RUA_ERROR_NONE;
235 static void __rua_info_remove_app_info(void *data)
237 pkgmgrinfo_appinfo_h app_info;
239 app_info = (pkgmgrinfo_appinfo_h)data;
240 pkgmgrinfo_appinfo_destroy_appinfo(app_info);
243 static void __rua_info_remove_comp_info(void *data)
245 aul_comp_info_h comp_info;
247 comp_info = (aul_comp_info_h)data;
248 aul_comp_info_destroy(comp_info);
251 static int __rua_info_clone_app_id(struct rua_info_s *info,
252 struct rua_info_s *clone)
254 clone->value[RUA_INFO_APP_ID] = strdup(info->value[RUA_INFO_APP_ID]);
255 if (!clone->value[RUA_INFO_APP_ID]) {
257 return RUA_ERROR_OUT_OF_MEMORY;
260 return RUA_ERROR_NONE;
263 static int __rua_info_clone_app_path(struct rua_info_s *info,
264 struct rua_info_s *clone)
266 clone->value[RUA_INFO_APP_PATH] =
267 strdup(info->value[RUA_INFO_APP_PATH]);
268 if (!clone->value[RUA_INFO_APP_PATH]) {
270 return RUA_ERROR_OUT_OF_MEMORY;
273 return RUA_ERROR_NONE;
276 static int __rua_info_clone_args(struct rua_info_s *info,
277 struct rua_info_s *clone)
279 clone->value[RUA_INFO_ARGS] = strdup(info->value[RUA_INFO_ARGS]);
280 if (!clone->value[RUA_INFO_ARGS]) {
282 return RUA_ERROR_OUT_OF_MEMORY;
285 return RUA_ERROR_NONE;
288 static int __rua_info_clone_launch_time(struct rua_info_s *info,
289 struct rua_info_s *clone)
291 clone->value[RUA_INFO_LAUNCH_TIME] = info->value[RUA_INFO_LAUNCH_TIME];
293 return RUA_ERROR_NONE;
296 static int __rua_info_clone_instance_id(struct rua_info_s *info,
297 struct rua_info_s *clone)
299 if (!info->value[RUA_INFO_INSTANCE_ID])
300 return RUA_ERROR_NONE;
302 clone->value[RUA_INFO_INSTANCE_ID] =
303 strdup(info->value[RUA_INFO_INSTANCE_ID]);
304 if (!clone->value[RUA_INFO_INSTANCE_ID]) {
306 return RUA_ERROR_OUT_OF_MEMORY;
309 return RUA_ERROR_NONE;
312 static int __rua_info_clone_instance_name(struct rua_info_s *info,
313 struct rua_info_s *clone)
315 if (!info->value[RUA_INFO_INSTANCE_NAME])
316 return RUA_ERROR_NONE;
318 clone->value[RUA_INFO_INSTANCE_NAME] =
319 strdup(info->value[RUA_INFO_INSTANCE_NAME]);
320 if (!clone->value[RUA_INFO_INSTANCE_NAME]) {
322 return RUA_ERROR_OUT_OF_MEMORY;
325 return RUA_ERROR_NONE;
328 static int __rua_info_clone_icon(struct rua_info_s *info,
329 struct rua_info_s *clone)
331 if (!info->value[RUA_INFO_ICON])
332 return RUA_ERROR_NONE;
334 clone->value[RUA_INFO_ICON] = strdup(info->value[RUA_INFO_ICON]);
335 if (!clone->value[RUA_INFO_ICON]) {
337 return RUA_ERROR_OUT_OF_MEMORY;
340 return RUA_ERROR_NONE;
343 static int __rua_info_clone_uri(struct rua_info_s *info,
344 struct rua_info_s *clone)
346 if (!info->value[RUA_INFO_URI])
347 return RUA_ERROR_NONE;
349 clone->value[RUA_INFO_URI] = strdup(info->value[RUA_INFO_URI]);
350 if (!clone->value[RUA_INFO_URI]) {
352 return RUA_ERROR_OUT_OF_MEMORY;
355 return RUA_ERROR_NONE;
358 static int __rua_info_clone_image(struct rua_info_s *info,
359 struct rua_info_s *clone)
361 if (!info->value[RUA_INFO_IMAGE])
362 return RUA_ERROR_NONE;
364 clone->value[RUA_INFO_IMAGE] = strdup(info->value[RUA_INFO_IMAGE]);
365 if (!clone->value[RUA_INFO_IMAGE]) {
367 return RUA_ERROR_OUT_OF_MEMORY;
370 return RUA_ERROR_NONE;
373 static int __rua_info_clone_component_id(struct rua_info_s *info,
374 struct rua_info_s *clone)
376 if (!info->value[RUA_INFO_COMPONENT_ID])
377 return RUA_ERROR_NONE;
379 clone->value[RUA_INFO_COMPONENT_ID] =
380 strdup(info->value[RUA_INFO_COMPONENT_ID]);
381 if (!clone->value[RUA_INFO_COMPONENT_ID]) {
383 return RUA_ERROR_OUT_OF_MEMORY;
386 return RUA_ERROR_NONE;
389 static int __rua_info_clone_app_info(struct rua_info_s *info,
390 struct rua_info_s *clone)
392 pkgmgrinfo_appinfo_h app_info;
395 ret = pkgmgrinfo_appinfo_clone_appinfo(
396 info->value[RUA_INFO_APP_INFO],
398 if (ret != RUA_ERROR_NONE) {
399 _E("Failed to clone app info");
400 return RUA_ERROR_OUT_OF_MEMORY;
403 clone->value[RUA_INFO_APP_INFO] = app_info;
405 return RUA_ERROR_NONE;
408 static int __rua_info_clone_comp_info(struct rua_info_s *info,
409 struct rua_info_s *clone)
411 aul_comp_info_h comp_info;
414 if (!info->value[RUA_INFO_COMP_INFO])
415 return RUA_ERROR_NONE;
417 ret = aul_comp_info_clone(info->value[RUA_INFO_COMP_INFO],
419 if (ret != RUA_ERROR_NONE) {
420 _E("Failed to clone comp info");
421 return RUA_ERROR_OUT_OF_MEMORY;
424 clone->value[RUA_INFO_COMP_INFO] = comp_info;
426 return RUA_ERROR_NONE;
429 static rua_info_vft __rua_table[] = {
430 [RUA_INFO_APP_ID] = {
431 .ctor = __rua_info_add_app_id,
433 .clnr = __rua_info_clone_app_id
435 [RUA_INFO_APP_PATH] = {
436 .ctor = __rua_info_add_app_path,
438 .clnr = __rua_info_clone_app_path
441 .ctor = __rua_info_add_args,
443 .clnr = __rua_info_clone_args
445 [RUA_INFO_LAUNCH_TIME] = {
446 .ctor = __rua_info_add_launch_time,
448 .clnr = __rua_info_clone_launch_time
450 [RUA_INFO_INSTANCE_ID] = {
451 .ctor = __rua_info_add_instance_id,
453 .clnr = __rua_info_clone_instance_id
455 [RUA_INFO_INSTANCE_NAME] = {
456 .ctor = __rua_info_add_instance_name,
458 .clnr = __rua_info_clone_instance_name
461 .ctor = __rua_info_add_icon,
463 .clnr = __rua_info_clone_icon
466 .ctor = __rua_info_add_uri,
468 .clnr = __rua_info_clone_uri
471 .ctor = __rua_info_add_image,
473 .clnr = __rua_info_clone_image
475 [RUA_INFO_COMPONENT_ID] = {
476 .ctor = __rua_info_add_component_id,
478 .clnr = __rua_info_clone_component_id
480 [RUA_INFO_APP_INFO] = {
481 .ctor = __rua_info_add_app_info,
482 .dtor = __rua_info_remove_app_info,
483 .clnr = __rua_info_clone_app_info
485 [RUA_INFO_COMP_INFO] = {
486 .ctor = __rua_info_add_comp_info,
487 .dtor = __rua_info_remove_comp_info,
488 .clnr = __rua_info_clone_comp_info
492 static void __destroy_rua_info(gpointer data)
494 struct rua_info_s *info = (struct rua_info_s *)data;
500 for (i = RUA_INFO_START; i < RUA_INFO_MAX; ++i) {
501 if (!__rua_table[i].dtor)
504 __rua_table[i].dtor(info->value[i]);
510 static struct rua_info_s *__create_rua_info(struct rua_rec *rec)
512 struct rua_info_s *info;
516 info = calloc(1, sizeof(struct rua_info_s));
522 for (i = RUA_INFO_START; i < RUA_INFO_MAX; ++i) {
523 if (!__rua_table[i].ctor)
526 ret = __rua_table[i].ctor(rec, info);
527 if (ret != RUA_ERROR_NONE) {
528 __destroy_rua_info(info);
536 static struct rua_info_s *__clone_rua_info(struct rua_info_s *info)
538 struct rua_info_s *clone;
542 clone = calloc(1, sizeof(struct rua_info_s));
548 for (i = RUA_INFO_START; i < RUA_INFO_MAX; ++i) {
549 if (!__rua_table[i].clnr)
552 ret = __rua_table[i].clnr(info, clone);
553 if (ret != RUA_ERROR_NONE) {
554 __destroy_rua_info(clone);
562 int rua_info_foreach(rua_manager_rua_info_cb callback, void *user_data)
564 return rua_info_usr_foreach(getuid(), callback, user_data);
567 int rua_info_usr_foreach(uid_t uid, rua_manager_rua_info_cb callback,
570 struct rua_info_s *info;
581 _E("Invalid parameter");
582 return RUA_ERROR_INVALID_PARAMETER;
585 ret = rua_history_load_db_for_uid(&table, &nrows, &ncols, uid);
587 _E("Failed to load rua history");
588 return RUA_ERROR_IO_ERROR;
591 for (row = 0; row < nrows; ++row) {
592 rua_history_get_rec(&rec, table, nrows, ncols, row);
593 info = __create_rua_info(&rec);
595 list = g_list_append(list, info);
598 rua_history_unload_db(&table);
602 info = (struct rua_info_s *)iter->data;
603 if (!callback(info, user_data))
606 iter = g_list_next(iter);
609 g_list_free_full(list, __destroy_rua_info);
611 return RUA_ERROR_NONE;
614 API int rua_info_get_app_id(rua_info_h info, char **app_id)
616 if (!info || !app_id) {
617 _E("Invalid parameter");
618 return RUA_ERROR_INVALID_PARAMETER;
621 *app_id = strdup(info->value[RUA_INFO_APP_ID]);
622 if (*app_id == NULL) {
624 return RUA_ERROR_OUT_OF_MEMORY;
627 return RUA_ERROR_NONE;
630 API int rua_info_get_app_path(rua_info_h info, char **app_path)
632 if (!info || !app_path) {
633 _E("Invalid parameter");
634 return RUA_ERROR_INVALID_PARAMETER;
637 *app_path = strdup(info->value[RUA_INFO_APP_PATH]);
638 if (*app_path == NULL) {
640 return RUA_ERROR_OUT_OF_MEMORY;
643 return RUA_ERROR_NONE;
646 API int rua_info_get_args(rua_info_h info, char **args)
648 if (!info || !args) {
649 _E("Invalid parameter");
650 return RUA_ERROR_INVALID_PARAMETER;
653 *args = strdup(info->value[RUA_INFO_ARGS]);
656 return RUA_ERROR_OUT_OF_MEMORY;
659 return RUA_ERROR_NONE;
662 API int rua_info_get_launch_time(rua_info_h info, time_t *launch_time)
664 if (!info || !launch_time) {
665 _E("Invalid parameter");
666 return RUA_ERROR_INVALID_PARAMETER;
669 *launch_time = GPOINTER_TO_INT(info->value[RUA_INFO_LAUNCH_TIME]);
671 return RUA_ERROR_NONE;
674 API int rua_info_get_instance_id(rua_info_h info, char **instance_id)
676 if (!info || !instance_id) {
677 _E("Invalid parameter");
678 return RUA_ERROR_INVALID_PARAMETER;
681 if (info->value[RUA_INFO_INSTANCE_ID]) {
682 *instance_id = strdup(info->value[RUA_INFO_INSTANCE_ID]);
683 if (*instance_id == NULL) {
685 return RUA_ERROR_OUT_OF_MEMORY;
691 return RUA_ERROR_NONE;
694 API int rua_info_get_instance_name(rua_info_h info, char **instance_name)
696 if (!info || !instance_name) {
697 _E("Invalid parameter");
698 return RUA_ERROR_INVALID_PARAMETER;
701 if (info->value[RUA_INFO_INSTANCE_NAME]) {
702 *instance_name = strdup(info->value[RUA_INFO_INSTANCE_NAME]);
703 if (*instance_name == NULL) {
705 return RUA_ERROR_OUT_OF_MEMORY;
708 *instance_name = NULL;
711 return RUA_ERROR_NONE;
714 static char *__get_icon(rua_info_h info)
716 pkgmgrinfo_appinfo_h app_info;
717 aul_comp_info_h comp_info;
721 if (info->value[RUA_INFO_COMP_INFO]) {
722 comp_info = (aul_comp_info_h)info->value[RUA_INFO_COMP_INFO];
723 ret = aul_comp_info_get_icon(comp_info, (const char **)&icon);
724 if (ret != AUL_R_OK) {
725 _E("Failed to get icon");
729 app_info = (pkgmgrinfo_appinfo_h)info->value[RUA_INFO_APP_INFO];
730 ret = pkgmgrinfo_appinfo_get_icon(app_info, &icon);
731 if (ret != PMINFO_R_OK) {
732 _E("Failed to get icon");
737 if (icon && icon[0] != '\0')
743 API int rua_info_get_icon(rua_info_h info, char **icon)
745 if (!info || !icon) {
746 _E("Invalid parameter");
747 return RUA_ERROR_INVALID_PARAMETER;
750 if (info->value[RUA_INFO_ICON]) {
751 *icon = strdup(info->value[RUA_INFO_ICON]);
754 return RUA_ERROR_OUT_OF_MEMORY;
757 *icon = __get_icon(info);
760 return RUA_ERROR_NONE;
763 API int rua_info_get_uri(rua_info_h info, char **uri)
766 _E("Invalid parameter");
767 return RUA_ERROR_INVALID_PARAMETER;
770 if (info->value[RUA_INFO_URI]) {
771 *uri = strdup(info->value[RUA_INFO_URI]);
774 return RUA_ERROR_OUT_OF_MEMORY;
780 return RUA_ERROR_NONE;
783 API int rua_info_get_image(rua_info_h info, char **image)
785 if (!info || !image) {
786 _E("Invalid parameter");
787 return RUA_ERROR_INVALID_PARAMETER;
790 if (info->value[RUA_INFO_IMAGE]) {
791 *image = strdup(info->value[RUA_INFO_IMAGE]);
792 if (*image == NULL) {
794 return RUA_ERROR_OUT_OF_MEMORY;
800 return RUA_ERROR_NONE;
803 API int rua_info_get_component_id(rua_info_h info, char **component_id)
805 if (!info || !component_id) {
806 _E("Invalid parameter");
807 return RUA_ERROR_INVALID_PARAMETER;
810 if (info->value[RUA_INFO_COMPONENT_ID]) {
811 *component_id = strdup(info->value[RUA_INFO_COMPONENT_ID]);
812 if (*component_id == NULL) {
814 return RUA_ERROR_OUT_OF_MEMORY;
817 *component_id = NULL;
820 return RUA_ERROR_NONE;
824 API int rua_info_is_managed_by_task_manager(rua_info_h info, bool *managed)
826 pkgmgrinfo_appinfo_h app_info;
827 aul_comp_info_h comp_info;
830 if (!info || !managed) {
831 _E("Invalid parameter");
832 return RUA_ERROR_INVALID_PARAMETER;
835 if (info->value[RUA_INFO_COMP_INFO]) {
836 comp_info = (aul_comp_info_h)info->value[RUA_INFO_COMP_INFO];
837 ret = aul_comp_info_is_taskmanage(comp_info, managed);
838 if (ret != AUL_R_OK) {
839 _E("Failed to check taskmanage");
840 return RUA_ERROR_INVALID_PARAMETER;
843 app_info = (pkgmgrinfo_appinfo_h)info->value[RUA_INFO_APP_INFO];
844 ret = pkgmgrinfo_appinfo_is_taskmanage(app_info, managed);
845 if (ret != PMINFO_R_OK) {
846 _E("Failed to check taskmanage");
847 return RUA_ERROR_INVALID_PARAMETER;
851 return RUA_ERROR_NONE;
854 static char *__get_label(rua_info_h info)
856 pkgmgrinfo_appinfo_h app_info;
857 aul_comp_info_h comp_info;
861 if (info->value[RUA_INFO_COMP_INFO]) {
862 comp_info = (aul_comp_info_h)info->value[RUA_INFO_COMP_INFO];
863 ret = aul_comp_info_get_label(comp_info, (const char **)&label);
864 if (ret != AUL_R_OK) {
865 _E("Failed to get label");
869 app_info = (pkgmgrinfo_appinfo_h)info->value[RUA_INFO_APP_INFO];
870 ret = pkgmgrinfo_appinfo_get_label(app_info, &label);
871 if (ret != PMINFO_R_OK) {
872 _E("Failed to get label");
877 if (label && label[0] != '\0')
878 return strdup(label);
883 API int rua_info_get_label(rua_info_h info, char **label)
885 if (!info || !label) {
886 _E("Invalid parameter");
887 return RUA_ERROR_INVALID_PARAMETER;
890 *label = __get_label(info);
892 return RUA_ERROR_NONE;
895 API int rua_info_destroy(rua_info_h info)
898 _E("Invalid parameter");
899 return RUA_ERROR_INVALID_PARAMETER;
902 __destroy_rua_info(info);
904 return RUA_ERROR_NONE;
907 API int rua_info_clone(rua_info_h info, rua_info_h *clone)
909 if (!info || !clone) {
910 _E("Invalid parameter");
911 return RUA_ERROR_INVALID_PARAMETER;
914 *clone = __clone_rua_info(info);
915 if (*clone == NULL) {
916 _E("Failed to clone rua info");
917 return RUA_ERROR_OUT_OF_MEMORY;
920 return RUA_ERROR_NONE;