Remove duplicated logics 93/242393/4
authorYunjin Lee <yunjin-.lee@samsung.com>
Wed, 26 Aug 2020 08:33:55 +0000 (17:33 +0900)
committerYunjin Lee <yunjin-.lee@samsung.com>
Thu, 3 Sep 2020 08:25:01 +0000 (17:25 +0900)
- Remove duplicated code with privilege-checker and call
privilege-checker APIs

Change-Id: Ic01498578c9932da4d546025313e7cd027a85c12
Signed-off-by: Yunjin Lee <yunjin-.lee@samsung.com>
src/privilege_information.c

index 1f58050..095bdc8 100755 (executable)
                return returnValue; \
        }
 
+#define SAFE_FREE(var) { if (var) { free(var); var = NULL; } }
+
 #ifndef PI_API
 #define PI_API __attribute__((visibility("default")))
 #endif
 
-int privilege_info_get_string_id(const char *package_type_string, int display, const char *api_version, const char *privilege, char **string_id)
+static int __convert_package_type(const char* package_type_string)
 {
-       char* temp = NULL;
-       int ret;
-       privilege_manager_package_type_e package_type;
-
-       if (package_type_string != NULL)
-               goto get_string_id_with_package_type;
-
-       /* Check NATIVE */
-       if (display)
-               ret = privilege_db_manager_get_privilege_display(PRVMGR_PACKAGE_TYPE_CORE, privilege, api_version, &temp);
-       else
-               ret = privilege_db_manager_get_privilege_description(PRVMGR_PACKAGE_TYPE_CORE, privilege, api_version, &temp);
-
-       if (ret == PRIVILEGE_DB_MANAGER_ERR_NONE && temp != NULL)
-               goto string_id_found;
-       else if (ret != PRIVILEGE_DB_MANAGER_ERR_NO_EXIST_RESULT)
-               goto err;
+       if (strcmp(package_type_string, "PRVINFO_PACKAGE_TYPE_WEB") == 0)
+               return PRVMGR_PACKAGE_TYPE_WRT;
+       if (strcmp(package_type_string, "PRVINFO_PACKAGE_TYPE_NATIVE") == 0)
+               return PRVMGR_PACKAGE_TYPE_CORE;
 
-       /* Check WEB */
+       return PRVMGR_PACKAGE_TYPE_NONE;
+}
 
-get_string_id_with_package_type:
-       if (package_type_string == NULL || strcmp(package_type_string, "PRVINFO_PACKAGE_TYPE_WEB") == 0)
-               package_type = PRVMGR_PACKAGE_TYPE_WRT;
-       else if (strcmp(package_type_string, "PRVINFO_PACKAGE_TYPE_NATIVE") == 0)
-               package_type = PRVMGR_PACKAGE_TYPE_CORE;
-       else
+static int __convert_return_value(int ret, const char* result)
+{
+       switch (ret) {
+       case PRVMGR_ERR_NONE:
+               if (result == NULL)
+                       return PRVINFO_ERROR_NO_MATCHING_PRIVILEGE;
+               return PRVINFO_ERROR_NONE;
+       case PRVMGR_ERR_INVALID_PARAMETER:
                return PRVINFO_ERROR_INVALID_PARAMETER;
-
-       if (display)
-               ret = privilege_db_manager_get_privilege_display(package_type, privilege, api_version, &temp);
-       else
-               ret = privilege_db_manager_get_privilege_description(package_type, privilege, api_version, &temp);
-
-       if (ret == PRIVILEGE_DB_MANAGER_ERR_NONE && temp != NULL)
-               goto string_id_found;
-       else
-               goto err;
-
-string_id_found:
-       *string_id = strdup(temp);
-       TryReturn(*string_id != NULL, free(temp), PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation is failed.");
-       free(temp);
-       return PRVINFO_ERROR_NONE;
-
-err:
-       *string_id = NULL;
-       if (temp != NULL)
-               free(temp);
-       if (ret == PRIVILEGE_DB_MANAGER_ERR_NO_EXIST_RESULT)
+       case PRVMGR_ERR_OUT_OF_MEMORY:
+               return PRVINFO_ERROR_OUT_OF_MEMORY;
+       case PRVMGR_ERR_NO_EXIST_PRIVILEGE:
                return PRVINFO_ERROR_NO_MATCHING_PRIVILEGE;
-       else
+       case PRVMGR_ERR_INTERNAL_ERROR:
+       default:
                return PRVINFO_ERROR_INTERNAL_ERROR;
+       }
 }
 
-int privilege_info_get_string_by_string_id(const char *string_id, char **string)
+static int __convert_result_code(int result)
 {
-       TryReturn(string_id != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] string_id is NULL");
-       char* temp = NULL;
-       temp = dgettext("privilege", string_id);
-
-       *string = strdup(temp);
-       TryReturn(*string != NULL, , PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] strdup of string failed.");
-
-       return PRVINFO_ERROR_NONE;
+       switch (result) {
+       case PRIVILEGE_NOTI_INFO_RESULT_CODE_SUCCESS:
+               return PRIVILEGE_CONSUMER_RETURN_CODE_SUCCESS;
+       case PRIVILEGE_NOTI_INFO_RESULT_CODE_UNKNOWN_LOCALE_CODE:
+               return PRIVILEGE_CONSUMER_RETURN_CODE_UNKNOWN_LOCALE_CODE;
+       case PRIVILEGE_NOTI_INFO_RESULT_CODE_INVALID_PARAMETER:
+       default:
+               return PRIVILEGE_CONSUMER_RETURN_CODE_INVALID_PARAMETER;
+       }
 }
 
 PI_API
@@ -132,16 +108,7 @@ int privilege_info_get_display_name(const char *api_version, const char *privile
        TryReturn(api_version != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] api_version is NULL");
        TryReturn(privilege != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege is NULL");
 
-       int ret = 0;
-       char* string_id = NULL;
-
-       ret = privilege_info_get_string_id(NULL, 1, api_version, privilege, &string_id);
-
-       if (ret == PRVINFO_ERROR_NONE)
-               ret = privilege_info_get_string_by_string_id(string_id, display_name);
-       if (string_id != NULL)
-               free(string_id);
-       return ret;
+       return __convert_return_value(privilege_info_get_privilege_display_name(privilege, display_name), *display_name);
 }
 
 PI_API
@@ -150,17 +117,7 @@ int privilege_info_get_description(const char *api_version, const char *privileg
        TryReturn(api_version != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] api_version is NULL");
        TryReturn(privilege != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege is NULL");
 
-       int ret = 0;
-       char* string_id = NULL;
-
-       ret = privilege_info_get_string_id(NULL, 0, api_version, privilege, &string_id);
-
-       if (ret == PRVINFO_ERROR_NONE)
-               ret = privilege_info_get_string_by_string_id(string_id, description);
-       if (string_id != NULL)
-               free(string_id);
-
-       return ret;
+       return __convert_return_value(privilege_info_get_privilege_description(privilege, description), *description);
 }
 
 PI_API
@@ -170,18 +127,7 @@ int privilege_info_get_display_name_by_pkgtype(const char *package_type, const c
        TryReturn(api_version != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] api_version is NULL");
        TryReturn(privilege != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege is NULL");
 
-       int ret = 0;
-       char* string_id = NULL;
-
-       ret = privilege_info_get_string_id(package_type, 1, api_version, privilege, &string_id);
-       TryReturn(ret != PRVINFO_ERROR_INVALID_PARAMETER, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] invalid package_type : %s", package_type);
-
-       if (ret == PRVINFO_ERROR_NONE)
-               ret = privilege_info_get_string_by_string_id(string_id, display_name);
-       if (string_id != NULL)
-               free(string_id);
-
-       return ret;
+       return __convert_return_value(privilege_info_get_privilege_display_name_by_package_type(privilege, api_version, __convert_package_type(package_type), display_name), *display_name);
 }
 
 PI_API
@@ -191,18 +137,7 @@ int privilege_info_get_description_by_pkgtype(const char *package_type, const ch
        TryReturn(api_version != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] api_version is NULL");
        TryReturn(privilege != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege is NULL");
 
-       int ret = 0;
-       char* string_id = NULL;
-
-       ret = privilege_info_get_string_id(package_type, 0, api_version, privilege, &string_id);
-       TryReturn(ret != PRVINFO_ERROR_INVALID_PARAMETER, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] invalid package_type : %s", package_type);
-
-       if (ret == PRVINFO_ERROR_NONE)
-               ret = privilege_info_get_string_by_string_id(string_id, description);
-       if (string_id != NULL)
-               free(string_id);
-
-       return ret;
+       return __convert_return_value(privilege_info_get_privilege_description_by_package_type(privilege, api_version, __convert_package_type(package_type), description), *description);
 }
 
 PI_API
@@ -210,188 +145,33 @@ int privilege_info_get_privacy_display_name(const char *privilege, char **privac
 {
        CHECK_FEATURE_SUPPORTED(PRIVACY_FEATURE);
        TryReturn(privilege != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege is NULL");
-       TryReturn(privilege_db_manager_is('p', privilege) == 1, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege does not exist or is not a privacy related");
-
-       char* privacy_id = NULL;
-       char* privacy_display_string_id = NULL;
+       TryReturn(privilege_info_is_privacy(privilege) == 1, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege does not exist or is not a privacy related");
 
-       TryReturn(privilege_db_manager_get_privacy_by_privilege(privilege, &privacy_id) == PRIVILEGE_DB_MANAGER_ERR_NONE && privacy_id != NULL, , PRVINFO_ERROR_INTERNAL_ERROR, "[PRVINFO_ERROR_INTERNAL_ERROR] privilege_db_manager_get_privacy_by_privilege failed");
+       char* privacy_name = NULL;
+       TryReturn(privilege_info_get_privacy_by_privilege(privilege, &privacy_name) == PRVMGR_ERR_NONE && privacy_name != NULL, SAFE_FREE(privacy_name), PRVINFO_ERROR_INTERNAL_ERROR, "[PRVINFO_ERROR_INTERNAL_ERROR] privilege_info_get_privacy_by_privilege() failed. privilege = %s", privilege);
 
-       TryReturn(privilege_db_manager_get_privacy_display(privacy_id, &privacy_display_string_id) == PRIVILEGE_DB_MANAGER_ERR_NONE && privacy_display_string_id != NULL, free(privacy_id), PRVINFO_ERROR_INTERNAL_ERROR, "[PRVINFO_ERROR_INTERNAL_ERROR] privilege_db_manager_get_privacy_display failed");
+       int ret = privilege_info_get_privacy_display(privacy_name, privacy_display_name);
+       SAFE_FREE(privacy_name);
 
-       TryReturn(privilege_info_get_string_by_string_id(privacy_display_string_id, privacy_display_name) == PRVINFO_ERROR_NONE && *privacy_display_name != NULL, free(privacy_id); free(privacy_display_string_id), PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
-
-       free(privacy_id);
-       free(privacy_display_string_id);
-       return PRVINFO_ERROR_NONE;
+       return __convert_return_value(ret, *privacy_display_name);
 }
 
 PI_API
 int privilege_info_get_privilege_info_list(const char* locale, GList* privilege_name_list, GList** privilege_info_list, privilege_consumer_return_code_e* return_result)
 {
-       GList *l;
-       GList *temp_privilege_info_list = NULL;
-       char* privilege_display = NULL;
-       char* privilege_description = NULL;
-       int is_invaild_parameter_count = 0;
-       int privilege_name_list_size = 0;
-       privilege_consumer_return_code_e consumer_return_code = PRIVILEGE_CONSUMER_RETURN_CODE_SUCCESS;
-       int ret = PRVINFO_ERROR_NONE;
-
-       if (privilege_name_list == NULL) {
-               LOGE("[PRVINFO_ERROR_INVALID_PARAMETER] privilege_list is NULL");
-               return PRVINFO_ERROR_INVALID_PARAMETER;
-       }
-       char *orig_locale = NULL;
-       char *result = (char *)setlocale(LC_ALL, NULL);
-       if (!result) {
-               LOGE("failed to get original locale. errno = %d", errno);
-       } else {
-               orig_locale = strdup(result);
-               TryReturn(orig_locale != NULL, , PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] strdup of orig_locale failed");
-               LOGD("orig_locale = %s", orig_locale);
-       }
-       result = (char *)setlocale(LC_ALL, locale);
-       if (result) {
-               LOGI("succeeded in setting locale = %s", result);
-       } else {
-               LOGE("failed to set locale. set locale to en_US.UTF8. errno = %d", errno);
-               setlocale(LC_ALL, "en_US.UTF8");
-
-               consumer_return_code = PRIVILEGE_CONSUMER_RETURN_CODE_UNKNOWN_LOCALE_CODE;
-       }
-
-       for (l = privilege_name_list; l != NULL; l = l->next) {
-               char* privilege_name = (char*)l->data;
-
-               LOGI("privilege_name = %s", privilege_name);
-
-               ret = privilege_info_get_privilege_display_name(privilege_name, &privilege_display);
-               if (ret == PRVMGR_ERR_NONE) {
-                       if (privilege_display != NULL) {
-                               LOGI("display : %s", privilege_display);
-                       } else {
-                               privilege_display = strdup("");
-                               if (privilege_display == NULL) {
-                                       LOGE("[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
-                                       ret = PRVINFO_ERROR_OUT_OF_MEMORY;
-                                       goto FINISH;
-                               }
-                               is_invaild_parameter_count++;
-                       }
-               } else {
-                       LOGE("failed to call privilege_info_get_privilege_display_name.");
-                       ret = PRVINFO_ERROR_INTERNAL_ERROR;
-                       goto FINISH;
-               }
-
-               LOGI("privilege_display = %s", privilege_display);
+       TryReturn(privilege_name_list != NULL, , PRVINFO_ERROR_INVALID_PARAMETER, "[PRVINFO_ERROR_INVALID_PARAMETER] privilege_name_list is NULL.");
 
-               ret = privilege_info_get_privilege_description(privilege_name, &privilege_description);
-               if (ret == PRVMGR_ERR_NONE) {
-                       if (privilege_description != NULL) {
-                               LOGI("description : %s", privilege_description);
-                       } else {
-                               privilege_description = strdup("");
-                               if (privilege_description == NULL) {
-                                       LOGE("[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
-                                       ret = PRVINFO_ERROR_OUT_OF_MEMORY;
-                                       goto FINISH;
-                               }
-                       }
-               } else {
-                       LOGE("failed to call privilege_info_get_privilege_description.");
-                       ret = PRVINFO_ERROR_INTERNAL_ERROR;
-                       goto FINISH;
-               }
+       privilege_noti_info_result_e result;
+       int ret = privilege_info_get_privilege_noti_info_list(locale, privilege_name_list, privilege_info_list, &result);
+       *return_result = __convert_result_code(result);
 
-               LOGD("privilege_description = %s", privilege_description);
-
-               privilege_info_s* privilege_info = (privilege_info_s*)malloc(sizeof(privilege_info_s));
-               memset(privilege_info, 0, sizeof(privilege_info_s));
-
-               privilege_info->privilege_name = strdup(privilege_name);
-               if (privilege_info->privilege_name == NULL) {
-                       LOGE("[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
-                       ret = PRVINFO_ERROR_OUT_OF_MEMORY;
-                       free(privilege_info);
-                       goto FINISH;
-               }
-               privilege_info->display_name = strdup(privilege_display);
-               if (privilege_info->display_name == NULL) {
-                       LOGE("[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
-                       ret = PRVINFO_ERROR_OUT_OF_MEMORY;
-                       free(privilege_info->privilege_name);
-                       free(privilege_info);
-                       goto FINISH;
-               }
-               privilege_info->description = strdup(privilege_description);
-               if (privilege_info->description == NULL) {
-                       LOGE("[PRVINFO_ERROR_OUT_OF_MEMORY] Memory allocation failed.");
-                       ret = PRVINFO_ERROR_OUT_OF_MEMORY;
-                       free(privilege_info->privilege_name);
-                       free(privilege_info->display_name);
-                       free(privilege_info);
-                       goto FINISH;
-               }
-               LOGI("privilege_info->privilege_name = %s", privilege_info->privilege_name);
-               LOGI("privilege_info->display_name = %s", privilege_info->display_name);
-               LOGI("privilege_info->description = %s", privilege_info->description);
-
-               temp_privilege_info_list = g_list_append(temp_privilege_info_list, privilege_info);
-
-               privilege_name_list_size++;
-
-               if (privilege_display != NULL) {
-                       free(privilege_display);
-                       privilege_display = NULL;
-               }
-
-               if (privilege_description != NULL) {
-                       free(privilege_description);
-                       privilege_description = NULL;
-               }
-       }
-
-       if (is_invaild_parameter_count == privilege_name_list_size)
-               consumer_return_code = PRIVILEGE_CONSUMER_RETURN_CODE_INVALID_PARAMETER;
-
-FINISH:
-
-       *privilege_info_list = temp_privilege_info_list;
-       *return_result = consumer_return_code;
-
-       if (privilege_display != NULL) {
-               free(privilege_display);
-               privilege_display = NULL;
-       }
-
-       if (privilege_description != NULL) {
-               free(privilege_description);
-               privilege_description = NULL;
-       }
-       if (orig_locale != NULL) {
-               setlocale(LC_ALL, orig_locale);
-               free(orig_locale);
-               orig_locale = NULL;
-       }
-
-       return ret;
+       return __convert_return_value(ret, "");
 }
 
 PI_API
 int privilege_info_free_privilege_info_list(GList* privilege_info_list)
 {
-       if (privilege_info_list == NULL)
-               return PRVINFO_ERROR_INVALID_PARAMETER;
-
-       GList* l = NULL;
-       for (l = privilege_info_list; l != NULL; l = l->next) {
-               privilege_info_s* privilege_info = (privilege_info_s*)l->data;
-               free(privilege_info->privilege_name);
-               free(privilege_info->display_name);
-               free(privilege_info->description);
-       }
-       g_list_free(privilege_info_list);
-       return PRVINFO_ERROR_NONE;
+       return __convert_return_value(privilege_info_free_privilege_noti_info_list(privilege_info_list), "");
 }
+
+