Use smart pointer using g_autoptr
[platform/core/api/wav-player.git] / src / wav_player_private.c
index 7b8e048..f06b8f8 100644 (file)
 #define PA_SOUND_PLAYER_METHOD_NAME_SOUND_STOP         "SoundStop"
 #define PA_SOUND_PLAYER_SIGNAL_EOS                     "EOS"
 
-struct dbus_cb_data {
-       GDBusConnection *conn;
+typedef struct dbus_cb_data {
        int handle;
        guint subs_id;
        wav_player_playback_completed_cb cb;
        void *user_data;
-};
+} dbus_cb_data_s;
 
 static int __convert_dbus_error(const char *error_msg)
 {
@@ -55,17 +54,25 @@ static int __convert_dbus_error(const char *error_msg)
        return WAV_PLAYER_ERROR_INVALID_OPERATION;
 }
 
+static void __weak_notify_cb(gpointer data, GObject *where_the_object_was)
+{
+       LOGD("object(%p) destroyed, data(%p)", where_the_object_was, data);
+}
+
 static GDBusConnection *__get_dbus_connection(void)
 {
        GDBusConnection *conn = NULL;
-       GError *err = NULL;
+       g_autoptr(GError) err = NULL;
 
        conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
        if (!conn) {
                LOGE("g_bus_get_sync() error (%s)", err->message);
-               g_error_free(err);
+               return NULL;
        }
 
+       /* For Debugging Purpose */
+       g_object_weak_ref(G_OBJECT(conn), __weak_notify_cb, NULL);
+
        return conn;
 }
 
@@ -79,7 +86,7 @@ static void __internal_complete_cb(GDBusConnection *connection,
 {
        gint handle = -1;
        gboolean stopped_by_user = FALSE;
-       struct dbus_cb_data *dbus_data = (struct dbus_cb_data *)userdata;
+       dbus_cb_data_s *dbus_data = (dbus_cb_data_s *)userdata;
 
        if (!dbus_data) {
                LOGE("dbus data is null");
@@ -87,20 +94,22 @@ static void __internal_complete_cb(GDBusConnection *connection,
        }
 
        g_variant_get(params, "(ib)", &handle, &stopped_by_user);
-       LOGI("expected handle(%d) callback(%p), incoming handle(%d), stopped_by_user(%d)",
-               dbus_data->handle, dbus_data->cb, handle, stopped_by_user);
+       LOGI("Incoming/expected handle(%d/%d), id(%u), callback(%p), stopped_by_user(%d)",
+               handle, dbus_data->handle, dbus_data->subs_id, dbus_data->cb, stopped_by_user);
 
        if (handle != dbus_data->handle)
                return;
 
-       g_dbus_connection_signal_unsubscribe(dbus_data->conn, dbus_data->subs_id);
-       g_object_unref(dbus_data->conn);
-
        if (!stopped_by_user) {
-               LOGI("Invoking user callback for handle(%d) cb(%p)", handle, dbus_data->cb);
+               LOGI("Invoking user callback(%p) for handle(%d)", dbus_data->cb, handle);
                dbus_data->cb(handle, dbus_data->user_data);
        }
 
+       LOGI("user callback returned");
+
+       g_dbus_connection_signal_unsubscribe(connection, dbus_data->subs_id);
+       g_object_unref(connection);
+
        g_free(dbus_data);
 }
 
@@ -114,10 +123,10 @@ int _wav_play_sound(const char *path, sound_stream_info_h stream_info, unsigned
        bool result = false;
 
        int handle;
-       GError *err = NULL;
-       GVariant *reply = NULL;
-       GDBusConnection *conn = NULL;
-       struct dbus_cb_data *dbus_cb_data = NULL;
+       g_autoptr(GDBusConnection) conn = NULL;
+       g_autoptr(GError) err = NULL;
+       g_autoptr(GVariant) reply = NULL;
+       dbus_cb_data_s *dbus_cb_data = NULL;
 
        if (path == NULL || stream_info == NULL) {
                LOGE("invalid params");
@@ -169,37 +178,33 @@ int _wav_play_sound(const char *path, sound_stream_info_h stream_info, unsigned
        if (!reply) {
                LOGE("g_dbus_connection_call_sync error (%s)", err->message);
                ret = __convert_dbus_error(err->message);
-               g_error_free(err);
-               g_object_unref(conn);
                return ret;
        }
 
        g_variant_get(reply, "(i)", &handle);
-       g_variant_unref(reply);
        if (id)
                *id = handle;
 
        LOGI("handle : %d", handle);
 
        if (callback) {
-               dbus_cb_data = g_new0(struct dbus_cb_data, 1);
-               dbus_cb_data->conn = conn;
+               dbus_cb_data = g_new0(dbus_cb_data_s, 1);
                dbus_cb_data->handle = handle;
                dbus_cb_data->cb = callback;
                dbus_cb_data->user_data = user_data;
-               dbus_cb_data->subs_id = g_dbus_connection_signal_subscribe(conn, NULL,
-                                PA_SOUND_PLAYER_INTERFACE,
-                                PA_SOUND_PLAYER_SIGNAL_EOS,
-                                PA_SOUND_PLAYER_OBJECT_PATH, NULL, G_DBUS_SIGNAL_FLAGS_NONE,
-                                __internal_complete_cb, dbus_cb_data, NULL);
-               if (!dbus_cb_data->subs_id) {
+               dbus_cb_data->subs_id = g_dbus_connection_signal_subscribe(
+                               g_object_ref(conn), NULL,
+                               PA_SOUND_PLAYER_INTERFACE,
+                               PA_SOUND_PLAYER_SIGNAL_EOS,
+                               PA_SOUND_PLAYER_OBJECT_PATH,
+                               NULL, G_DBUS_SIGNAL_FLAGS_NONE,
+                               __internal_complete_cb, dbus_cb_data, NULL);
+               if (dbus_cb_data->subs_id == 0) {
                        g_object_unref(conn);
                        g_free(dbus_cb_data);
                        LOGE("g_dbus_connection_signal_subscribe() failed");
                        return WAV_PLAYER_ERROR_INVALID_OPERATION;
                }
-       } else {
-               g_object_unref(conn);
        }
 
        return WAV_PLAYER_ERROR_NONE;
@@ -208,12 +213,11 @@ int _wav_play_sound(const char *path, sound_stream_info_h stream_info, unsigned
 //LCOV_EXCL_START
 int _wav_play_sound_simple(const char *path, const char *stream_role)
 {
-       int ret = WAV_PLAYER_ERROR_NONE;
        char m_path[PATH_MAX];
        int handle;
-       GError *err = NULL;
-       GVariant *reply = NULL;
-       GDBusConnection *conn = NULL;
+       g_autoptr(GDBusConnection) conn = NULL;
+       g_autoptr(GError) err = NULL;
+       g_autoptr(GVariant) reply = NULL;
 
        if (!path || !stream_role) {
                LOGE("invalid params path(%p), stream_role(%p)", path, stream_role);
@@ -248,31 +252,24 @@ int _wav_play_sound_simple(const char *path, const char *stream_role)
        if (!reply) {
                if (err) {
                        LOGE("g_dbus_connection_call_sync error (%s)", err->message);
-                       ret = __convert_dbus_error(err->message);
-                       g_error_free(err);
-               } else {
-                       ret = WAV_PLAYER_ERROR_INVALID_OPERATION;
+                       return __convert_dbus_error(err->message);
                }
-               goto finish;
+               return WAV_PLAYER_ERROR_INVALID_OPERATION;
        }
 
        g_variant_get(reply, "(i)", &handle);
-       g_variant_unref(reply);
 
        LOGI("handle : %d", handle);
 
-finish:
-       g_object_unref(conn);
-
-       return ret;
+       return WAV_PLAYER_ERROR_NONE;
 }
 //LCOV_EXCL_STOP
 
 int _wav_stop_sound(int id)
 {
-       GDBusConnection *conn = NULL;
-       GError *err = NULL;
-       GVariant *reply = NULL;
+       g_autoptr(GDBusConnection) conn = NULL;
+       g_autoptr(GError) err = NULL;
+       g_autoptr(GVariant) reply = NULL;
 
        LOGI("handle(%d)", id);
 
@@ -286,20 +283,11 @@ int _wav_stop_sound(int id)
                                PA_SOUND_PLAYER_METHOD_NAME_SOUND_STOP,
                                g_variant_new("(i)", id),
                                NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
-
-       g_object_unref(conn);
-
        if (!reply) {
-               int ret;
-
                LOGE("g_dbus_connection_call_sync error (%s)", err->message);
-               ret = __convert_dbus_error(err->message);
-               g_error_free(err);
-               return ret;
+               return __convert_dbus_error(err->message);
        }
 
-       g_variant_unref(reply);
-
        LOGI("stop sound. handle(%d)", id);
 
        return WAV_PLAYER_ERROR_NONE;