Reduce duplicate code related with ui dbus 43/277343/5
authorInHong Han <inhong1.han@samsung.com>
Tue, 5 Jul 2022 09:01:43 +0000 (18:01 +0900)
committerInHong Han <inhong1.han@samsung.com>
Mon, 11 Jul 2022 08:21:13 +0000 (17:21 +0900)
Change-Id: I5af023c6363a0f55a546825e8f5ff514f494eda1

client/ma_ui_dbus.c

index 668af388ba3cffc9175ccbb164ba9d4b3dca3794..5b49e101217f891a42b3f36493a06cc366ff0fb6 100644 (file)
@@ -454,6 +454,61 @@ int ma_ui_dbus_reconnect()
        return 0;
 }
 
+static int __send_sync_dbus_message(DBusMessage* msg, bool need_add_rule)
+{
+       DBusError err;
+       dbus_error_init(&err);
+
+       DBusMessage* result_msg;
+       int result = MA_ERROR_OPERATION_FAILED;
+
+       result_msg = dbus_connection_send_with_reply_and_block(g_ui_conn_sender, msg, g_ui_waiting_time, &err);
+       dbus_message_unref(msg);
+
+       if (dbus_error_is_set(&err)) {
+               MAUI_SLOGE("[ERROR] Dbus Error (%s)", err.message);
+               dbus_error_free(&err);
+       }
+
+       if (NULL != result_msg) {
+               dbus_message_get_args(result_msg, &err,
+                       DBUS_TYPE_INT32, &result,
+                       DBUS_TYPE_INVALID);
+
+               if (dbus_error_is_set(&err)) {
+                       MAUI_SLOGE("[ERROR] Get arguments error (%s)", err.message);
+                       dbus_error_free(&err);
+                       result = MA_ERROR_OPERATION_FAILED;
+               }
+
+               dbus_message_unref(result_msg);
+
+               if (0 == result) {
+                       if (need_add_rule) {
+                               /* add a rule for daemon error */
+                               char rule_err[256] = {0, };
+                               snprintf(rule_err, 256, "sender='org.freedesktop.DBus',path='/org/freedesktop/DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',type='signal',arg0='%s'", MA_SERVER_SERVICE_INTERFACE);
+                               dbus_bus_add_match(g_ui_conn_listener, rule_err, &err);
+
+                               if (dbus_error_is_set(&err)) {
+                                       MAUI_SLOGE("Match Error (%s)", err.message);
+                                       dbus_error_free(&err);
+                               }
+                       }
+
+                       MAUI_SLOGD("[SUCCESS] Success to send meesage : result = %d", result);
+               } else {
+                       MAUI_SLOGE("[ERROR] Fail to send message : result = %d", result);
+               }
+       } else {
+               MAUI_SLOGE("[ERROR] Result message is NULL ");
+               ma_ui_dbus_reconnect();
+               result = MA_ERROR_TIMED_OUT;
+       }
+
+       return result;
+}
+
 int ma_ui_dbus_request_hello()
 {
        DBusMessage* msg;
@@ -520,7 +575,7 @@ static int __dbus_restore_daemon()
        return ret;
 }
 
-int ma_ui_dbus_request_initialize(int pid)
+static int __check_damon_exists()
 {
        DBusError err;
        dbus_error_init(&err);
@@ -541,6 +596,15 @@ int ma_ui_dbus_request_initialize(int pid)
                return MA_ERROR_OPERATION_FAILED;
        }
 
+       return MA_ERROR_NONE;
+}
+
+int ma_ui_dbus_request_initialize(int pid)
+{
+       int ret = __check_damon_exists();
+       if (MA_ERROR_NONE != ret)
+               return ret;
+
        DBusMessage* msg;
 
        msg = dbus_message_new_method_call(
@@ -560,74 +624,18 @@ int ma_ui_dbus_request_initialize(int pid)
                DBUS_TYPE_INT32, &pid,
                DBUS_TYPE_INVALID);
 
-       DBusMessage* result_msg;
-       int result = MA_ERROR_OPERATION_FAILED;
-
-       result_msg = dbus_connection_send_with_reply_and_block(g_ui_conn_sender, msg, g_ui_waiting_time, &err);
-       dbus_message_unref(msg);
-
-       if (dbus_error_is_set(&err)) {
-               MAUI_SLOGE("[ERROR] Dbus Error (%s)", err.message);
-               dbus_error_free(&err);
-       }
-
-       if (NULL != result_msg) {
-               dbus_message_get_args(result_msg, &err,
-                       DBUS_TYPE_INT32, &result,
-                       DBUS_TYPE_INVALID);
-
-               if (dbus_error_is_set(&err)) {
-                       MAUI_SLOGE("[ERROR] Get arguments error (%s)", err.message);
-                       dbus_error_free(&err);
-                       result = MA_ERROR_OPERATION_FAILED;
-               }
-
-               dbus_message_unref(result_msg);
-
-               if (0 == result) {
-                       /* add a rule for daemon error */
-                       char rule_err[256] = {0, };
-                       snprintf(rule_err, 256, "sender='org.freedesktop.DBus',path='/org/freedesktop/DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',type='signal',arg0='%s'", MA_SERVER_SERVICE_INTERFACE);
-                       dbus_bus_add_match(g_ui_conn_listener, rule_err, &err);
-
-                       if (dbus_error_is_set(&err)) {
-                               MAUI_SLOGE("Match Error (%s)", err.message);
-                               dbus_error_free(&err);
-                       }
-
-                       MAUI_SLOGD("[ERROR] multi-assistant ui initialize : result = %d", result);
-               } else {
-                       MAUI_SLOGE("[ERROR] multi-assistant ui initialize : result = %d", result);
-               }
-       } else {
-               MAUI_SLOGE("[ERROR] Result message is NULL ");
-               ma_ui_dbus_reconnect();
-               result = MA_ERROR_TIMED_OUT;
-       }
-
-       return result;}
+       return __send_sync_dbus_message(msg, true);
+}
 
 int ma_ui_dbus_request_deinitialize(int pid)
 {
+       int ret = __check_damon_exists();
+       if (MA_ERROR_NONE != ret)
+               return ret;
+
        DBusError err;
        dbus_error_init(&err);
 
-       bool exist = dbus_bus_name_has_owner(g_ui_conn_sender, MA_SERVER_SERVICE_NAME, &err);
-       if (dbus_error_is_set(&err)) {
-               MAUI_SLOGE("[ERROR] Dbus Error (%s)", err.message);
-               dbus_error_free(&err);
-       }
-
-       int ret;
-       if (false == exist) {
-               ret = __dbus_restore_daemon();
-               if (MA_ERROR_NONE != ret) {
-                       MAUI_SLOGE("[ERROR] Fail to restore daemon");
-                       return MA_ERROR_TIMED_OUT;
-               }
-               return MA_ERROR_OPERATION_FAILED;
-       }
-
        /* remove a rule for daemon error */
        char rule_err[256] = {0, };
        snprintf(rule_err, 256, "sender='org.freedesktop.DBus',path='/org/freedesktop/DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',type='signal',arg0='%s'", MA_SERVER_SERVICE_INTERFACE);
@@ -657,42 +665,7 @@ int ma_ui_dbus_request_deinitialize(int pid)
 
        dbus_message_append_args(msg, DBUS_TYPE_INT32, &pid, DBUS_TYPE_INVALID);
 
-       DBusMessage* result_msg;
-       int result = MA_ERROR_OPERATION_FAILED;
-
-       result_msg = dbus_connection_send_with_reply_and_block(g_ui_conn_sender, msg, g_ui_waiting_time, &err);
-       dbus_message_unref(msg);
-
-       if (dbus_error_is_set(&err)) {
-               MAUI_SLOGE("[ERROR] Dbus Error (%s)", err.message);
-               dbus_error_free(&err);
-       }
-
-       if (NULL != result_msg) {
-               dbus_message_get_args(result_msg, &err,
-                       DBUS_TYPE_INT32, &result,
-                       DBUS_TYPE_INVALID);
-
-               if (dbus_error_is_set(&err)) {
-                       MAUI_SLOGE("[ERROR] Get arguments error (%s)", err.message);
-                       dbus_error_free(&err);
-                       result = MA_ERROR_OPERATION_FAILED;
-               }
-
-               dbus_message_unref(result_msg);
-
-               if (0 == result) {
-                       MAUI_SLOGD("[DEBUG] multi-assistant ui finalize : result = %d", result);
-               } else {
-                       MAUI_SLOGE("[ERROR] multi-assistant ui finalize : result = %d", result);
-               }
-       } else {
-               MAUI_SLOGE("[ERROR] Result message is NULL ");
-               ma_ui_dbus_reconnect();
-               result = MA_ERROR_TIMED_OUT;
-       }
-
-       return result;
+       return __send_sync_dbus_message(msg, false);
 }
 
 int ma_ui_dbus_request_change_assistant(const char *appid)
@@ -730,41 +703,7 @@ int ma_ui_dbus_request_change_assistant(const char *appid)
        }
 
        dbus_message_append_args(msg, DBUS_TYPE_STRING, &appid, DBUS_TYPE_INVALID);
-       DBusMessage* result_msg;
-       int result = MA_ERROR_OPERATION_FAILED;
-
-       result_msg = dbus_connection_send_with_reply_and_block(g_ui_conn_sender, msg, g_ui_waiting_time, &err);
-       dbus_message_unref(msg);
 
-       if (dbus_error_is_set(&err)) {
-               MAUI_SLOGE("[ERROR] Dbus Error (%s)", err.message);
-               dbus_error_free(&err);
-       }
-
-       if (NULL != result_msg) {
-               dbus_message_get_args(result_msg, &err,
-                       DBUS_TYPE_INT32, &result,
-                       DBUS_TYPE_INVALID);
-
-               if (dbus_error_is_set(&err)) {
-                       MAUI_SLOGE("[ERROR] Get arguments error (%s)", err.message);
-                       dbus_error_free(&err);
-                       result = MA_ERROR_OPERATION_FAILED;
-               }
-
-               dbus_message_unref(result_msg);
-
-               if (0 == result) {
-                       MAUI_SLOGD("[DEBUG] multi-assistant ui change assistant : result = %d", result);
-               } else {
-                       MAUI_SLOGE("[ERROR] multi-assistant ui change assistant : result = %d", result);
-               }
-       } else {
-               MAUI_SLOGE("[ERROR] Result message is NULL ");
-               ma_ui_dbus_reconnect();
-               result = MA_ERROR_TIMED_OUT;
-       }
-
-       return result;
+       return __send_sync_dbus_message(msg, false);
 }