Fix client releasing code simpler 76/289376/4
authorSuyeon Hwang <stom.hwang@samsung.com>
Wed, 22 Feb 2023 04:27:06 +0000 (13:27 +0900)
committerSuyeon Hwang <stom.hwang@samsung.com>
Tue, 7 Mar 2023 01:14:24 +0000 (10:14 +0900)
- Issue:
tts_config_mgr_unset_callback should be invoked for all clients when
they are released.

- Solution:
This patch fixes the __tts_config_release_client()'s behavior. Through
this patch, __tts_config_release_client() will invoke
tts_config_mgr_unset_callback() to reset all callbacks in client
structure.
And plus, this patch changes the name and type of these functions. These
two functions are only invoked in this file. This means that those
functions are private. So, this patch makes these functions as static
and changes the name of these functions which avoids the reserved
identifiers.

Change-Id: I2d20ca73595eacca5a37d8e84822a8d4e0602df7
Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
common/tts_config_mgr.c
common/tts_config_mgr.h

index c39e521..a61f13a 100644 (file)
@@ -758,33 +758,46 @@ void __tts_config_screen_reader_changed_cb(keynode_t *key, void *data)
        }
 }
 
-int __tts_config_release_client(unsigned int uid)
+static inline void free_config_client(tts_config_client_s* client)
 {
-       GSList *iter = NULL;
-       tts_config_client_s* temp_client = NULL;
+       if (NULL != client) {
+               client->uid = TTS_INVALID_UID;
+               client->engine_cb = NULL;
+               client->voice_cb = NULL;
+               client->speech_cb = NULL;
+               client->screen_cb = NULL;
+               client->pitch_cb = NULL;
+               client->bg_volume_ratio_cb = NULL;
+               client->user_data = NULL;
+               client->screen_user_data = NULL;
+
+               free(client);
+       }
+}
 
-       if (0 < g_slist_length(g_config_client_list)) {
-               /* Check uid */
-               iter = g_slist_nth(g_config_client_list, 0);
+static gint compare_config_client_and_uid(gconstpointer a, gconstpointer b)
+{
+       const tts_config_client_s *client = (const tts_config_client_s *)a;
+       const unsigned int *uid = (const unsigned int *)b;
 
-               while (NULL != iter) {
-                       temp_client = iter->data;
-
-                       if (NULL != temp_client) {
-                               if (uid == temp_client->uid) {
-                                       g_config_client_list = g_slist_remove(g_config_client_list, temp_client);
-                                       free(temp_client);
-                                       temp_client = NULL;
-                                       break;
-                               }
-                       }
+       if (NULL != client && NULL != uid && client->uid == *uid) {
+               return 0;
+       }
 
-                       iter = g_slist_next(iter);
-               }
+       return -1;
+}
+
+static int release_config_client(unsigned int uid)
+{
+       GSList *iter = g_slist_find_custom(g_config_client_list, &uid, compare_config_client_and_uid);
+       if (NULL != iter) {
+               tts_config_client_s *client = (tts_config_client_s *)iter->data;
+               g_config_client_list = g_slist_delete_link(g_config_client_list, iter);
+
+               free_config_client(client);
        }
 
        SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Client count (%d)", g_slist_length(g_config_client_list));
-
        return g_slist_length(g_config_client_list);
 }
 
@@ -1068,17 +1081,8 @@ static int __add_new_config_client(unsigned int uid)
                return TTS_CONFIG_ERROR_OUT_OF_MEMORY;
        }
        new_client->uid = uid;
-       new_client->engine_cb = NULL;
-       new_client->voice_cb = NULL;
-       new_client->speech_cb = NULL;
-       new_client->pitch_cb = NULL;
-       new_client->screen_cb = NULL;
-       new_client->bg_volume_ratio_cb = NULL;
-       new_client->user_data = NULL;
-       new_client->screen_user_data = NULL;
 
        g_config_client_list = g_slist_append(g_config_client_list, new_client);
-
        return TTS_CONFIG_ERROR_NONE;
 }
 
@@ -1120,7 +1124,7 @@ int tts_config_mgr_initialize(unsigned int uid, tts_config_client_type_e client_
        if (0 != access(TTS_CONFIG_BASE, F_OK)) {
                if (0 != mkdir(TTS_CONFIG_BASE, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
                        SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to make directory : %s", TTS_CONFIG_BASE);
-                       __tts_config_release_client(uid);
+                       release_config_client(uid);
                        return TTS_CONFIG_ERROR_OPERATION_FAILED;
                } else {
                        SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Success to make directory : %s", TTS_CONFIG_BASE);
@@ -1130,7 +1134,7 @@ int tts_config_mgr_initialize(unsigned int uid, tts_config_client_type_e client_
        if (0 != access(TTS_HOME, F_OK)) {
                if (0 != mkdir(TTS_HOME, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
                        SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to make directory : %s", TTS_HOME);
-                       __tts_config_release_client(uid);
+                       release_config_client(uid);
                        return TTS_CONFIG_ERROR_OPERATION_FAILED;
                } else {
                        SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Success to make directory : %s", TTS_HOME);
@@ -1140,7 +1144,7 @@ int tts_config_mgr_initialize(unsigned int uid, tts_config_client_type_e client_
        if (0 != access(TTS_DOWNLOAD_BASE, F_OK)) {
                if (0 != mkdir(TTS_DOWNLOAD_BASE, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
                        SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to make directory : %s", TTS_DOWNLOAD_BASE);
-                       __tts_config_release_client(uid);
+                       release_config_client(uid);
                        return TTS_CONFIG_ERROR_OPERATION_FAILED;
                } else {
                        SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Success to make directory : %s", TTS_DOWNLOAD_BASE);
@@ -1150,7 +1154,7 @@ int tts_config_mgr_initialize(unsigned int uid, tts_config_client_type_e client_
        if (0 != access(TTS_DOWNLOAD_ENGINE_INFO, F_OK)) {
                if (0 != mkdir(TTS_DOWNLOAD_ENGINE_INFO, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
                        SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to make directory : %s", TTS_DOWNLOAD_ENGINE_INFO);
-                       __tts_config_release_client(uid);
+                       release_config_client(uid);
                        return TTS_CONFIG_ERROR_OPERATION_FAILED;
                } else {
                        SLOG(LOG_DEBUG, TAG_TTSCONFIG, "Success to make directory : %s", TTS_DOWNLOAD_ENGINE_INFO);
@@ -1162,7 +1166,7 @@ int tts_config_mgr_initialize(unsigned int uid, tts_config_client_type_e client_
 
        if (0 != __tts_config_mgr_get_engine_info()) {
                SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to get engine info");
-               __tts_config_release_client(uid);
+               release_config_client(uid);
                __tts_config_release_engine();
                return TTS_CONFIG_ERROR_ENGINE_NOT_FOUND;
        }
@@ -1171,14 +1175,14 @@ int tts_config_mgr_initialize(unsigned int uid, tts_config_client_type_e client_
 
        if (0 != tts_parser_load_config()) {
                SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to parse configure information");
-               __tts_config_release_client(uid);
+               release_config_client(uid);
                __tts_config_release_engine();
                return TTS_CONFIG_ERROR_OPERATION_FAILED;
        }
        tts_config_s config_info;
        if (0 != tts_parser_get_config_info(&config_info)) {
                SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to get configure information");
-               __tts_config_release_client(uid);
+               release_config_client(uid);
                __tts_config_release_engine();
                return TTS_CONFIG_ERROR_OPERATION_FAILED;
        }
@@ -1186,7 +1190,7 @@ int tts_config_mgr_initialize(unsigned int uid, tts_config_client_type_e client_
        /* Check whether engine id is valid */
        if (0 != __tts_config_mgr_check_engine_is_valid(config_info.engine_id)) {
                SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to get default engine");
-               __tts_config_release_client(uid);
+               release_config_client(uid);
                __tts_config_release_engine();
                tts_parser_unload_config();
                return TTS_CONFIG_ERROR_ENGINE_NOT_FOUND;
@@ -1212,7 +1216,7 @@ int tts_config_mgr_initialize(unsigned int uid, tts_config_client_type_e client_
                        int tmp_type = -1;
                        if (0 != __tts_config_mgr_select_lang(config_info.engine_id, &tmp_language, &tmp_type)) {
                                SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to select language");
-                               __tts_config_release_client(uid);
+                               release_config_client(uid);
                                __tts_config_release_engine();
                                tts_parser_unload_config();
                                return TTS_CONFIG_ERROR_OPERATION_FAILED;
@@ -1233,7 +1237,7 @@ int tts_config_mgr_initialize(unsigned int uid, tts_config_client_type_e client_
                                if (__is_client_type(TTS_CONFIG_CLIENT_TYPE_SETTING | TTS_CONFIG_CLIENT_TYPE_SERVER | TTS_CONFIG_CLIENT_TYPE_INTERRUPT)) {
                                        if (0 != tts_parser_set_voice(config_info.language, config_info.type)) {
                                                SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to save config");
-                                               __tts_config_release_client(uid);
+                                               release_config_client(uid);
                                                __tts_config_release_engine();
                                                tts_parser_unload_config();
                                                return TTS_CONFIG_ERROR_OPERATION_FAILED;
@@ -1259,7 +1263,7 @@ int tts_config_mgr_initialize(unsigned int uid, tts_config_client_type_e client_
 
        if (0 != __tts_config_mgr_register_config_event()) {
                SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to register config event");
-               __tts_config_release_client(uid);
+               release_config_client(uid);
                __tts_config_release_engine();
                tts_parser_unload_config();
                return TTS_CONFIG_ERROR_OPERATION_FAILED;
@@ -1272,7 +1276,7 @@ int tts_config_mgr_initialize(unsigned int uid, tts_config_client_type_e client_
        /* For engine directory monitoring */
        //if (0 != __tts_config_mgr_register_engine_config_updated_event()) {
        //      SLOG(LOG_ERROR, TAG_TTSCONFIG, "[ERROR] Fail to register engine config updated event");
-       //      __tts_config_release_client(uid);
+       //      release_config_client(uid);
        //      __tts_config_release_engine();
        //      tts_parser_unload_config();
        //      __tts_config_mgr_unregister_config_event();
@@ -1284,7 +1288,7 @@ int tts_config_mgr_initialize(unsigned int uid, tts_config_client_type_e client_
 
 int tts_config_mgr_finalize(unsigned int uid, tts_config_client_type_e client_type)
 {
-       if (0 < __tts_config_release_client(uid)) {
+       if (0 < release_config_client(uid)) {
                g_client_type &= ~client_type;
                return TTS_CONFIG_ERROR_NONE;
        }
@@ -1292,8 +1296,6 @@ int tts_config_mgr_finalize(unsigned int uid, tts_config_client_type_e client_ty
        SLOG(LOG_INFO, TAG_TTSCONFIG, "Client type : %d", client_type);
        g_client_type &= ~client_type;
 
-       tts_config_mgr_unset_callback(uid);
-
        __tts_config_release_engine();
 
        tts_parser_unload_config();
@@ -1341,34 +1343,6 @@ int tts_config_mgr_set_callback(unsigned int uid,
        return TTS_CONFIG_ERROR_NONE;
 }
 
-int tts_config_mgr_unset_callback(unsigned int uid)
-{
-       GSList *iter = NULL;
-       tts_config_client_s* temp_client = NULL;
-
-       /* Call all callbacks of client*/
-       iter = g_slist_nth(g_config_client_list, 0);
-
-       while (NULL != iter) {
-               temp_client = iter->data;
-
-               if (NULL != temp_client) {
-                       if (uid == temp_client->uid) {
-                               temp_client->engine_cb = NULL;
-                               temp_client->voice_cb = NULL;
-                               temp_client->speech_cb = NULL;
-                               temp_client->pitch_cb = NULL;
-                               temp_client->bg_volume_ratio_cb = NULL;
-                               temp_client->user_data = NULL;
-                       }
-               }
-
-               iter = g_slist_next(iter);
-       }
-
-       return TTS_CONFIG_ERROR_NONE;
-}
-
 int tts_config_mgr_set_screen_reader_callback(unsigned int uid, tts_config_screen_reader_changed_cb callback, void* user_data)
 {
        if (NULL == callback) {
index 2200457..a387d48 100644 (file)
@@ -86,8 +86,6 @@ int tts_config_mgr_set_callback(unsigned int uid,
                                tts_config_bg_volume_ratio_changed_cb bg_volume_ratio_cb,
                                void* user_data);
 
-int tts_config_mgr_unset_callback(unsigned int uid);
-
 /* Only for screen reader option */
 int tts_config_mgr_set_screen_reader_callback(unsigned int uid, tts_config_screen_reader_changed_cb callback, void* user_data);