proc-monitor: Remove duplicate code as separate function 79/315179/2
authorChanwoo Choi <cw00.choi@samsung.com>
Fri, 26 Jul 2024 06:01:07 +0000 (15:01 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Fri, 26 Jul 2024 07:24:03 +0000 (16:24 +0900)
proc-monitor module have the duplicate code to parse the app
information. In order to remove duplicate code as separate function
and also it make easy to add newly app type.

Change-Id: If8b894e156e561ab231fd2b7a8cd02f9cf6ad3ec
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
src/process/proc-monitor.c

index 67bc98d..bd6df78 100644 (file)
 
 #define APP_WATCHDOG_TIMER_INTERVAL    10
 
+#define APP_TYPE_SERVICE               "svcapp"
+#define APP_TYPE_UI                    "uiapp"
+#define APP_TYPE_WIDGET                        "widgetapp"
+#define APP_TYPE_WATCH                 "watchapp"
+
 static int current_lcd_state;
 
 static GSource *app_watchdog_check_timer;
@@ -1113,6 +1118,29 @@ static void systemtime_changed_signal_handler(GVariant *params)
        resourced_notify(RESOURCED_NOTIFIER_SYSTEMTIME_CHANGED, NULL);
 }
 
+static int get_app_type(char *pkgtype, int *apptype)
+{
+       if (!pkgtype || !apptype) {
+               _E("Invalid parameter of pkgtype");
+               return -EINVAL;
+       }
+
+       if (!strncmp(pkgtype, APP_TYPE_SERVICE, sizeof(APP_TYPE_SERVICE))) {
+               *apptype = PROC_TYPE_SERVICE;
+       } else if (!strncmp(pkgtype, APP_TYPE_WIDGET, sizeof(APP_TYPE_WIDGET))) {
+               *apptype = PROC_TYPE_WIDGET;
+       } else if (!strncmp(pkgtype, APP_TYPE_WATCH, sizeof(APP_TYPE_WATCH))) {
+               *apptype = PROC_TYPE_WATCH;
+       } else if (!strncmp(pkgtype, APP_TYPE_UI, sizeof(APP_TYPE_UI))) {
+               *apptype = PROC_TYPE_GUI;
+       } else {
+               _E("Unknown pkgtype is not able to get app type");
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
 EXPORT_TEST void proc_dbus_aul_launch(GVariant *params)
 {
        pid_t pid = 0;
@@ -1120,6 +1148,7 @@ EXPORT_TEST void proc_dbus_aul_launch(GVariant *params)
        char *appid = NULL;
        char *pkgid = NULL;
        char *pkgtype = NULL;
+       int ret;
 
        do_expr_unless_g_variant_get_typechecked(return, params, "(i&s&s&s)", &pid, &appid, &pkgid, &pkgtype);
        if (pid == 0 || !appid || !pkgid || !pkgtype) {
@@ -1132,20 +1161,14 @@ EXPORT_TEST void proc_dbus_aul_launch(GVariant *params)
                        appid, pkgid, pid, pkgtype);
 #endif
 
-       if (!strncmp(pkgtype, "svc", 3)) {
-               apptype = PROC_TYPE_SERVICE;
+       ret = get_app_type(pkgtype, &apptype);
+       if (ret < 0)
+               return;
+
+       if (apptype == PROC_TYPE_SERVICE)
                status = PROC_CGROUP_SET_SERVICE_REQUEST;
-       } else if (!strncmp(pkgtype, "ui", 2)) {
-               apptype = PROC_TYPE_GUI;
-               status = PROC_CGROUP_SET_LAUNCH_REQUEST;
-       } else if (!strncmp(pkgtype, "widget", 6)) {
-               apptype = PROC_TYPE_WIDGET;
-               status = PROC_CGROUP_SET_LAUNCH_REQUEST;
-       } else if (!strncmp(pkgtype, "watch", 5)) {
-               apptype = PROC_TYPE_WATCH;
+       else
                status = PROC_CGROUP_SET_LAUNCH_REQUEST;
-       } else
-               return;
 
        resourced_proc_status_change(status, pid, appid, pkgid, apptype);
 }
@@ -1157,6 +1180,7 @@ EXPORT_TEST void proc_dbus_aul_resume(GVariant *params)
        char *appid = NULL;
        char *pkgid = NULL;
        char *pkgtype = NULL;
+       int ret;
 
        do_expr_unless_g_variant_get_typechecked(return, params, "(i&s&s&s)", &pid, &appid, &pkgid, &pkgtype);
        if (pid == 0 || !appid || !pkgid || !pkgtype) {
@@ -1164,14 +1188,9 @@ EXPORT_TEST void proc_dbus_aul_resume(GVariant *params)
                return;
        }
 
-       if (!strncmp(pkgtype, "svc", 3))
-               apptype = PROC_TYPE_SERVICE;
-       else if (!strncmp(pkgtype, "widget", 6))
-               apptype = PROC_TYPE_WIDGET;
-       else if (!strncmp(pkgtype, "watch", 5))
-               apptype = PROC_TYPE_WATCH;
-       else
-               apptype = PROC_TYPE_GUI;
+       ret = get_app_type(pkgtype, &apptype);
+       if (ret < 0)
+               return;
 
        resourced_proc_status_change(status, pid, appid, pkgid, apptype);
 }
@@ -1201,6 +1220,7 @@ EXPORT_TEST void proc_dbus_aul_changestate(GVariant *params)
        char *pkgid = NULL;
        char *statstr = NULL;
        char *pkgtype = NULL;
+       int ret;
 
        do_expr_unless_g_variant_get_typechecked(return, params, "(i&s&s&s&s)", &pid, &appid, &pkgid, &statstr, &pkgtype);
        if (pid == 0 || !appid || !pkgid || !statstr || !pkgtype) {
@@ -1215,14 +1235,9 @@ EXPORT_TEST void proc_dbus_aul_changestate(GVariant *params)
        else
                return;
 
-       if (!strncmp(pkgtype, "svc", 3))
-               apptype = PROC_TYPE_SERVICE;
-       else if (!strncmp(pkgtype, "widget", 6))
-               apptype = PROC_TYPE_WIDGET;
-       else if (!strncmp(pkgtype, "watch", 5))
-               apptype = PROC_TYPE_WATCH;
-       else
-               apptype = PROC_TYPE_GUI;
+       ret = get_app_type(pkgtype, &apptype);
+       if (ret < 0)
+               return;
 
        resourced_proc_status_change(status, pid, appid, pkgid, apptype);
 }