From 8e2d0770059f1512876b6f73ed790e47051138e6 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Mon, 13 Jul 2020 14:04:13 +0900 Subject: [PATCH] Refactor app_context_set_status_cb function - Uses aul_app_event_create() instead of dbus signals Requires: - https://review.tizen.org/gerrit/#/c/platform/core/appfw/aul-1/+/238356/ - https://review.tizen.org/gerrit/#/c/platform/core/appfw/amd/+/238355/ Change-Id: Ic471c2ff64a54132f2d5eaf159b89d3f150f511b Signed-off-by: Hwankyu Jhun --- src/app_context.c | 84 ++++++++++++++++++++++--------------------------------- 1 file changed, 33 insertions(+), 51 deletions(-) diff --git a/src/app_context.c b/src/app_context.c index e2fd286..c93bd66 100644 --- a/src/app_context.c +++ b/src/app_context.c @@ -320,6 +320,7 @@ API int app_context_clone(app_context_h *clone, app_context_h app_context) } typedef struct _event_cb_context_ { + aul_app_event_h handle; GHashTable *pid_table; app_manager_app_context_event_cb callback; void *user_data; @@ -341,16 +342,8 @@ static void app_context_unlock_event_cb_context() static bool __load_all_app_context_cb_locked(app_context_h app_context, void *user_data) { - app_context_h app_context_cloned; char *app_id = NULL; int pid; - int ret; - - ret = app_context_clone(&app_context_cloned, app_context); - if (ret != APP_MANAGER_ERROR_NONE) { - _E("Failed to clone app context"); - return false; - } app_context_get_app_id(app_context, &app_id); app_context_get_pid(app_context, &pid); @@ -358,56 +351,39 @@ static bool __load_all_app_context_cb_locked(app_context_h app_context, free(app_id); g_hash_table_insert(event_cb_context->pid_table, GINT_TO_POINTER(pid), - app_context_cloned); + GINT_TO_POINTER(pid)); return true; } -static int app_context_launched_event_cb(pid_t pid, const char *app_id, - void *data) +static void __app_context_launched_event_cb(aul_app_context_h app_context, + void *user_data) { - app_context_h app_context; - int ret; - - if (pid < 0 || app_id == NULL) { - _E("Invalid parameter"); - return -1; - } - - ret = app_context_get_app_context_by_pid(pid, &app_context); - if (ret != APP_MANAGER_ERROR_NONE) { - _E("Failed to create app context. pid(%d), error(%d)", - pid, ret); - return ret; - } + int pid; + aul_app_context_get_pid(app_context, &pid); app_context_lock_event_cb_context(); - if (event_cb_context && event_cb_context->pid_table) { g_hash_table_insert(event_cb_context->pid_table, - GINT_TO_POINTER(pid), app_context); - event_cb_context->callback(app_context, + GINT_TO_POINTER(pid), GINT_TO_POINTER(pid)); + event_cb_context->callback((app_context_h)app_context, APP_CONTEXT_EVENT_LAUNCHED, event_cb_context->user_data); } else { - _E("Invalid context"); - app_context_destroy(app_context); + _E("Invalid context. pid(%d)", pid); } - app_context_unlock_event_cb_context(); - - return 0; } -static int app_context_terminated_event_cb(pid_t pid, void *data) +static void __app_context_terminated_event_cb(aul_app_context_h app_context, + void *user_data) { - app_context_h app_context; + int pid; + aul_app_context_get_pid(app_context, &pid); app_context_lock_event_cb_context(); - if (event_cb_context && event_cb_context->pid_table) { - app_context = g_hash_table_lookup(event_cb_context->pid_table, - GINT_TO_POINTER(pid)); - if (app_context) { + if (g_hash_table_contains(event_cb_context->pid_table, + GINT_TO_POINTER(pid))) { event_cb_context->callback(app_context, APP_CONTEXT_EVENT_TERMINATED, event_cb_context->user_data); @@ -415,12 +391,9 @@ static int app_context_terminated_event_cb(pid_t pid, void *data) GINT_TO_POINTER(pid)); } } else { - _E("Invalid context"); + _E("Invalid context. pid(%d)", pid); } - app_context_unlock_event_cb_context(); - - return 0; } static void __event_cb_context_fini(void) @@ -428,8 +401,8 @@ static void __event_cb_context_fini(void) if (!event_cb_context) return; - aul_listen_app_dead_signal(NULL, NULL); - aul_listen_app_launch_signal_v2(NULL, NULL); + if (event_cb_context->handle) + aul_app_event_destroy(event_cb_context->handle); if (event_cb_context->pid_table) g_hash_table_destroy(event_cb_context->pid_table); @@ -440,6 +413,8 @@ static void __event_cb_context_fini(void) static int __event_cb_context_init(void) { + int ret; + if (event_cb_context) return APP_MANAGER_ERROR_NONE; @@ -449,9 +424,8 @@ static int __event_cb_context_init(void) return APP_MANAGER_ERROR_OUT_OF_MEMORY; } - event_cb_context->pid_table = g_hash_table_new_full(g_direct_hash, - g_direct_equal, NULL, - (GDestroyNotify)app_context_destroy); + event_cb_context->pid_table = g_hash_table_new(g_direct_hash, + g_direct_equal); if (event_cb_context->pid_table == NULL) { _E("Failed to create hash table"); __event_cb_context_fini(); @@ -459,8 +433,16 @@ static int __event_cb_context_init(void) } app_context_foreach_app_context(__load_all_app_context_cb_locked, NULL); - aul_listen_app_dead_signal(app_context_terminated_event_cb, NULL); - aul_listen_app_launch_signal_v2(app_context_launched_event_cb, NULL); + + ret = aul_app_event_create(__app_context_launched_event_cb, + __app_context_terminated_event_cb, + NULL, + &event_cb_context->handle); + if (ret != AUL_R_OK) { + _E("aul_app_context_create() is failed. error(%d)", ret); + __event_cb_context_fini(); + return APP_MANAGER_ERROR_OUT_OF_MEMORY; + } return APP_MANAGER_ERROR_NONE; } @@ -624,7 +606,7 @@ int app_context_set_status_cb(app_manager_app_context_status_cb callback, if (!info) return APP_MANAGER_ERROR_OUT_OF_MEMORY; - ret = aul_app_event_create(appid, + ret = aul_app_event_create_with_appid(appid, __aul_app_event_launched_cb, __aul_app_event_terminated_cb, info, -- 2.7.4