From d52748cde9a086e0ff30d32c95da5baee3e625bb Mon Sep 17 00:00:00 2001 From: "mk5004.lee" Date: Fri, 5 Jan 2018 14:09:14 +0900 Subject: [PATCH] Add func to check limitation of notification - Limit the number of each app. - case of TYPE_NOTI and no tag Change-Id: I35d95f0dbcb5b7bcacb00af2dd046767858b0da5 Signed-off-by: mk5004.lee --- include/notification_noti.h | 2 + src/notification_noti.c | 104 ++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) diff --git a/include/notification_noti.h b/include/notification_noti.h index 77a81ea1..47886ce7 100644 --- a/include/notification_noti.h +++ b/include/notification_noti.h @@ -18,6 +18,7 @@ #define __NOTIFICATION_NOTI_H__ #include +#include #include #include @@ -74,6 +75,7 @@ int notification_noti_get_package_template(notification_h noti, char *app_id, ch int notification_noti_delete_template(const char *pkg_id); void notification_noti_init_data(void); +int notification_noti_check_limit(notification_h noti, uid_t uid, GList **list); #endif /* __NOTIFICATION_NOTI_H__ */ diff --git a/src/notification_noti.c b/src/notification_noti.c index 4a3b4734..37a4ab43 100755 --- a/src/notification_noti.c +++ b/src/notification_noti.c @@ -39,6 +39,7 @@ #define NOTI_BURST_DELETE_UNIT 10 #define ERR_BUFFER_SIZE 1024 +#define NOTI_LIMIT 100 static void __free_and_set(void **target_ptr, void *new_ptr) { @@ -2054,3 +2055,106 @@ EXPORT_API void notification_noti_init_data(void) sqlite3_free(query); notification_db_close(&db); } + +EXPORT_API int notification_noti_check_limit(notification_h noti, uid_t uid, GList **list) +{ + int ret; + int priv_id; + int count = 0; + char *query = NULL; + char *list_query = NULL; + sqlite3 *db = NULL; + sqlite3_stmt *stmt; + + if (noti == NULL) + return NOTIFICATION_ERROR_INVALID_PARAMETER; + + db = notification_db_open(); + if (!db) + return get_last_result(); + + query = sqlite3_mprintf("SELECT COUNT(*) FROM noti_list " + "WHERE caller_app_id = %Q AND uid = %d ", + noti->caller_app_id, uid); + if (query == NULL) { + /* LCOV_EXCL_START */ + ERR("sqlite3_mprintf Failed"); + ret = NOTIFICATION_ERROR_OUT_OF_MEMORY; + goto err; + /* LCOV_EXCL_STOP */ + } + + ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL); + if (ret != SQLITE_OK) { + /* LCOV_EXCL_START */ + ERR("sqlite3_prepare_v2 Failed [%d][%s]", ret, + sqlite3_errmsg(db)); + ret = NOTIFICATION_ERROR_FROM_DB; + goto err; + /* LCOV_EXCL_STOP */ + } + + ret = sqlite3_step(stmt); + if (ret == SQLITE_ROW) { + count = sqlite3_column_int(stmt, 0); + ret = NOTIFICATION_ERROR_NONE; + } else { + ret = NOTIFICATION_ERROR_FROM_DB; + goto err; + } + + if (count > NOTI_LIMIT) { + count -= NOTI_LIMIT; + + ret = sqlite3_reset(stmt); + if (ret != SQLITE_OK) { + /* LCOV_EXCL_START */ + ret = NOTIFICATION_ERROR_FROM_DB; + goto err; + /* LCOV_EXCL_STOP */ + } + + list_query = sqlite3_mprintf("SELECT priv_id FROM noti_list " + "WHERE caller_app_id = %Q AND uid = %d " + "AND type = %d ORDER BY insert_time ASC, priv_id ASC", + noti->caller_app_id, uid, NOTIFICATION_TYPE_NOTI); + if (list_query == NULL) { + /* LCOV_EXCL_START */ + ERR("sqlite3_mprintf Failed"); + ret = NOTIFICATION_ERROR_OUT_OF_MEMORY; + goto err; + /* LCOV_EXCL_STOP */ + } + + ret = sqlite3_prepare_v2(db, list_query, -1, &stmt, NULL); + if (ret != SQLITE_OK) { + /* LCOV_EXCL_START */ + ERR("sqlite3_prepare_v2 Failed [%d][%s]", ret, + sqlite3_errmsg(db)); + ret = NOTIFICATION_ERROR_FROM_DB; + goto err; + /* LCOV_EXCL_STOP */ + } + + while (sqlite3_step(stmt) == SQLITE_ROW && count > 0) { + priv_id = sqlite3_column_int(stmt, 0); + *list = g_list_append(*list, GINT_TO_POINTER(priv_id)); + count--; + } + } + +err: + if (stmt) + sqlite3_finalize(stmt); + + if (query) + sqlite3_free(query); + + if (list_query) + sqlite3_free(list_query); + + if (db) + notification_db_close(&db); + + return ret; +} -- 2.34.1