Fix lcov option for lcov 2.0
[platform/core/appfw/librua.git] / src / rua.c
index 8ba783e..527c66a 100644 (file)
--- a/src/rua.c
+++ b/src/rua.c
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <db-util.h>
-#include <aul.h>
+#include <unistd.h>
 
-/* For multi-user support */
-#include <tzplatform_config.h>
+#include <sqlite3.h>
+#include <aul.h>
 #include <dlog.h>
 
-#ifdef LOG_TAG
-#undef LOG_TAG
-#endif
-
-#define LOG_TAG "RUA"
-
+#include "rua_util.h"
 #include "rua.h"
 #include "db-schema.h"
-#include "perf-measure.h"
-
-#define RUA_DB_NAME    ".rua.db"
-#define RUA_HISTORY    "rua_history"
-#define QUERY_MAXLEN   4096
-#define Q_LATEST \
-       "select pkg_name from rua_history " \
-       "order by launch_time desc limit 1 "
-
-static int __exec(sqlite3 *db, char *query);
-static int __create_table(sqlite3 *db);
-static sqlite3 *__db_init();
+#include "rua_dbus.h"
+#include "rua_private.h"
 
-int rua_delete_history_from_db(bundle *b)
+API int rua_add_history_for_uid(char *pkg_name, char *app_path, char *arg, uid_t uid)
 {
        int r;
-       sqlite3 *db = NULL;
-       char query[QUERY_MAXLEN];
-
-       char *pkg_name = NULL;
-       char *app_path = NULL;
-       char *errmsg = NULL;
-       int result = 0;
+       char time_str[32] = {0,};
+       bundle *b = NULL;
 
-       db = __db_init();
-       if (db == NULL) {
-               LOGE("Error db null");
+       if (pkg_name == NULL || app_path == NULL) {
+               LOGE("invalid param");
                return -1;
        }
 
-       if (b != NULL) {
-               bundle_get_str(b, AUL_K_RUA_PKGNAME, &pkg_name);
-               bundle_get_str(b, AUL_K_RUA_APPPATH, &app_path);
-       }
-
-       if (pkg_name != NULL)
-               snprintf(query, QUERY_MAXLEN, "delete from rua_history where pkg_name = '%s';", pkg_name);
-       else if (app_path != NULL)
-               snprintf(query, QUERY_MAXLEN, "delete from rua_history where app_path = '%s';", app_path);
-       else
-               snprintf(query, QUERY_MAXLEN, "delete from rua_history;");
-
-       LOGI("rua_delete_history_from_db : %s", query);
-       r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
+       r = _rua_util_check_uid(uid);
+       if (r == -1)
+               return r;
 
-       if (r != SQLITE_OK) {
-               LOGE("fail to exec delete query %s : %s", query, errmsg);
-               sqlite3_free(errmsg);
-               result = -1;
+       b = bundle_create();
+       if (b == NULL) {
+               LOGE("bundle_create fail out of memory.");
+               return -1;
        }
+       snprintf(time_str, sizeof(time_str), "%d", (int)time(NULL));
+       bundle_add_str(b, AUL_K_RUA_PKGNAME, pkg_name);
+       bundle_add_str(b, AUL_K_RUA_APPPATH, app_path);
+       bundle_add_str(b, AUL_K_RUA_ARG, arg);
+       bundle_add_str(b, AUL_K_RUA_TIME, time_str);
 
-       if (db != NULL)
-               db_util_close(db);
-
-       return result;
-
-}
-
-int rua_clear_history(void)
-{
-       int r;
-       r = aul_delete_rua_history(NULL);
-       LOGI("rua_clear_history result : %d ", r);
+       r = aul_add_rua_history_for_uid(b, uid);
+       LOGI("rua_add_history_for_uid result : %d ", r);
+       bundle_free(b);
        return r;
 }
 
-int rua_delete_history_with_pkgname(char *pkg_name)
+API int rua_delete_history_with_pkgname_for_uid(char *pkg_name, uid_t uid)
 {
        int r;
-       bundle *b = bundle_create();
+       bundle *b;
+
+       r = _rua_util_check_uid(uid);
+       if (r == -1)
+               return r;
+
+       b = bundle_create();
        if (b == NULL) {
                LOGE("bundle_create fail out of memory.");
                return -1;
        }
-
        bundle_add_str(b, AUL_K_RUA_PKGNAME, pkg_name);
-       r = aul_delete_rua_history(b);
+       r = aul_delete_rua_history_for_uid(b, uid);
        LOGI("rua_delete_history_with_pkgname result : %d ", r);
        bundle_free(b);
        return r;
 }
 
-int rua_delete_history_with_apppath(char *app_path)
+API int rua_delete_history_with_pkgname(char *pkg_name)
+{
+       return rua_delete_history_with_pkgname_for_uid(pkg_name, getuid());
+}
+
+API int rua_delete_history_with_apppath_for_uid(char *app_path, uid_t uid)
 {
        int r;
-       bundle *b = bundle_create();
+       bundle *b;
+
+       r = _rua_util_check_uid(uid);
+       if (r == -1)
+               return r;
+
+       b = bundle_create();
        if (b == NULL) {
                LOGE("bundle_create fail out of memory.");
                return -1;
        }
-
        bundle_add_str(b, AUL_K_RUA_APPPATH, app_path);
-       r = aul_delete_rua_history(b);
+       r = aul_delete_rua_history_for_uid(b, uid);
        LOGI("rua_delete_history_with_apppath result : %d ", r);
        bundle_free(b);
+       return r;
+}
 
+API int rua_delete_history_with_apppath(char *app_path)
+{
+       return rua_delete_history_with_apppath_for_uid(app_path, getuid());
+}
+
+API int rua_clear_history_for_uid(uid_t uid)
+{
+       int r;
+
+       r = _rua_util_check_uid(uid);
+       if (r == -1)
+               return r;
+
+       r = aul_delete_rua_history_for_uid(NULL, uid);
+       LOGI("rua_clear_history result : %d ", r);
        return r;
 }
 
-int rua_add_history(struct rua_rec *rec)
+API int rua_clear_history(void)
+{
+       return rua_clear_history_for_uid(getuid());
+}
+
+API int rua_history_load_db_for_uid(char ***table, int *nrows, int *ncols, uid_t uid)
 {
+       static const char query[] = "SELECT "
+               "pkg_name, app_path, arg, launch_time, instance_id, "
+               "instance_name, icon, uri, image, comp_id "
+               "FROM rua_history ORDER BY launch_time DESC";
        int r;
-       int cnt = 0;
-       char query[QUERY_MAXLEN];
-       sqlite3_stmt *stmt;
+       char *db_err = NULL;
+       char **db_result = NULL;
        sqlite3 *db = NULL;
 
-       db = __db_init();
-       if (db == NULL) {
-               LOGE("Error db null");
+       if (table == NULL || nrows == NULL || ncols == NULL) {
+               LOGE("invalid parameter");
                return -1;
        }
 
-       if (rec == NULL) {
-               LOGE("Error rec null");
-               db_util_close(db);
-               return -1;
-       }
+       r = _rua_util_check_uid(uid);
+       if (r == -1)
+               return r;
 
-       snprintf(query, QUERY_MAXLEN,
-               "select count(*) from %s where pkg_name = '%s';", RUA_HISTORY,
-               rec->pkg_name);
+       r = _rua_util_open_db(&db, SQLITE_OPEN_READONLY, uid, RUA_DB_NAME);
+       if (r != SQLITE_OK)
+               return -1;
 
-       r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL);
+       r = sqlite3_get_table(db, query, &db_result, nrows, ncols, &db_err);
        if (r != SQLITE_OK) {
-               LOGE("Error sqlite3_prepare fail");
-               db_util_close(db);
+               LOGE("get table failed: %s", db_err);
+               sqlite3_free(db_err);
+               sqlite3_close_v2(db);
                return -1;
        }
 
-       r = sqlite3_step(stmt);
-       if (r == SQLITE_ROW)
-               cnt = sqlite3_column_int(stmt, 0);
-
-       sqlite3_finalize(stmt);
+       *table = db_result;
 
-       if (cnt == 0)
-               /* insert */
-               snprintf(query, QUERY_MAXLEN,
-                       "insert into %s ( pkg_name, app_path, arg, launch_time ) "
-                       " values ( \"%s\", \"%s\", \"%s\", %d ) ",
-                       RUA_HISTORY,
-                       rec->pkg_name ? rec->pkg_name : "",
-                       rec->app_path ? rec->app_path : "",
-                       rec->arg ? rec->arg : "", (int)time(NULL));
-       else
-               /* update */
-               snprintf(query, QUERY_MAXLEN,
-                       "update %s set arg='%s', launch_time='%d' where pkg_name = '%s';",
-                       RUA_HISTORY,
-                       rec->arg ? rec->arg : "", (int)time(NULL), rec->pkg_name);
-
-       r = __exec(db, query);
-       if (r == -1) {
-               LOGE("[RUA ADD HISTORY ERROR] %s\n", query);
-               db_util_close(db);
-               return -1;
-       }
+       sqlite3_close_v2(db);
 
-       db_util_close(db);
        return r;
 }
 
-int rua_history_load_db(char ***table, int *nrows, int *ncols)
+API int rua_history_load_db(char ***table, int *nrows, int *ncols)
+{
+       return rua_history_load_db_for_uid(table, nrows, ncols, getuid());
+}
+
+API int rua_register_update_cb_for_uid(rua_history_update_cb callback, void *user_data, int *callback_id, uid_t uid)
 {
        int r;
-       char query[QUERY_MAXLEN];
-       char *db_err = NULL;
-       char **db_result = NULL;
-       sqlite3 *db = NULL;
 
-       char defname[FILENAME_MAX];
-       const char *rua_db_path = tzplatform_getenv(TZ_USER_DB);
-       if (rua_db_path == NULL) {
-               LOGE("fail to get rua_db_path");
+       if (callback == NULL)
                return -1;
-       }
-       snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME);
 
-       if (table == NULL)
-               return -1;
-       if (nrows == NULL)
-               return -1;
-       if (ncols == NULL)
-               return -1;
+       r = _rua_util_check_uid(uid);
+       if (r == -1)
+               return r;
 
-       r = db_util_open_with_options(defname, &db, SQLITE_OPEN_READONLY, NULL);
-       if (r) {
-               db_util_close(db);
-               return -1;
-       }
+       r = rua_dbus_signal_subscribe(callback, user_data, callback_id);
+
+       return r;
+}
 
-       snprintf(query, QUERY_MAXLEN,
-                "select * from %s order by launch_time desc;", RUA_HISTORY);
+API int rua_register_update_cb(rua_history_update_cb callback, void *user_data, int *callback_id)
+{
+       return rua_register_update_cb_for_uid(callback, user_data, callback_id, getuid());
+}
 
-       r = sqlite3_get_table(db, query, &db_result, nrows, ncols, &db_err);
+API int rua_unregister_update_cb_for_uid(int callback_id, uid_t uid)
+{
+       int r;
 
-       if (r == SQLITE_OK)
-               *table = db_result;
-       else
-               sqlite3_free_table(db_result);
+       r = _rua_util_check_uid(uid);
+       if (r == -1)
+               return r;
 
-       db_util_close(db);
+       r = rua_dbus_signal_unsubscribe(callback_id);
 
        return r;
 }
 
-int rua_history_unload_db(char ***table)
+API int rua_unregister_update_cb(int callback_id)
 {
-       if (*table) {
-               sqlite3_free_table(*table);
-               *table = NULL;
-               return 0;
-       }
-       return -1;
+       return rua_unregister_update_cb_for_uid(callback_id, getuid());
 }
 
-int rua_history_get_rec(struct rua_rec *rec, char **table, int nrows, int ncols,
+API int rua_history_get_rec(struct rua_rec *rec, char **table, int nrows, int ncols,
                        int row)
 {
        char **db_result = NULL;
@@ -264,13 +229,11 @@ int rua_history_get_rec(struct rua_rec *rec, char **table, int nrows, int ncols,
 
        db_result = table + ((row + 1) * ncols);
 
-       tmp = db_result[RUA_COL_ID];
-       if (tmp)
-               rec->id = atoi(tmp);
-
        tmp = db_result[RUA_COL_PKGNAME];
-       if (tmp)
+       if (tmp) {
                rec->pkg_name = tmp;
+               LOGI("get rec pkg_name %s", rec->pkg_name);
+       }
 
        tmp = db_result[RUA_COL_APPPATH];
        if (tmp)
@@ -284,125 +247,153 @@ int rua_history_get_rec(struct rua_rec *rec, char **table, int nrows, int ncols,
        if (tmp)
                rec->launch_time = atoi(tmp);
 
+       tmp = db_result[RUA_COL_COMP_ID];
+       if (tmp && tmp[0] != '\0')
+               rec->comp_id = tmp;
+       else
+               rec->comp_id = NULL;
+
+       tmp = db_result[RUA_COL_INSTANCE_ID];
+       if (tmp && tmp[0] != '\0')
+               rec->instance_id = tmp;
+       else
+               rec->instance_id = NULL;
+
+       tmp = db_result[RUA_COL_INSTANCE_NAME];
+       if (tmp && tmp[0] != '\0')
+               rec->instance_name = tmp;
+       else
+               rec->instance_name = NULL;
+
+       tmp = db_result[RUA_COL_ICON];
+       if (tmp && tmp[0] != '\0')
+               rec->icon = tmp;
+       else
+               rec->icon = NULL;
+
+       tmp = db_result[RUA_COL_URI];
+       if (tmp && tmp[0] != '\0')
+               rec->uri = tmp;
+       else
+               rec->uri = NULL;
+
+       tmp = db_result[RUA_COL_IMAGE];
+       if (tmp && tmp[0] != '\0')
+               rec->image = tmp;
+       else
+               rec->image = NULL;
+
+       tmp = db_result[RUA_COL_COMP_ID];
+       if (tmp && tmp[0] != '\0')
+               rec->comp_id = tmp;
+       else
+               rec->comp_id = NULL;
+
        return 0;
 }
 
-int rua_is_latest_app(const char *pkg_name)
+API int rua_history_unload_db(char ***table)
 {
+       if (*table) {
+               sqlite3_free_table(*table);
+               *table = NULL;
+               return 0;
+       }
+       return -1;
+}
+
+API int rua_is_latest_app_for_uid(const char *pkg_name, uid_t uid)
+{
+       static const char query[] =
+               "SELECT pkg_name FROM rua_history "
+               "ORDER BY launch_time DESC LIMIT 1";
        int r = -1;
        sqlite3_stmt *stmt;
        const unsigned char *ct;
-       sqlite3 *db;
-
-       char defname[FILENAME_MAX];
-       const char *rua_db_path = tzplatform_getenv(TZ_USER_DB);
-       if (rua_db_path == NULL) {
-               LOGE("fail to get rua_db_path");
-               return -1;
-       }
-       snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME);
+       sqlite3 *db = NULL;
 
        if (!pkg_name)
                return -1;
 
-       r = db_util_open_with_options(defname, &db, SQLITE_OPEN_READONLY, NULL);
-       if (r) {
-               db_util_close(db);
+       r = _rua_util_check_uid(uid);
+       if (r == -1)
+               return r;
+
+       r = _rua_util_open_db(&db, SQLITE_OPEN_READONLY, uid, RUA_DB_NAME);
+       if (r != SQLITE_OK)
                return -1;
-       }
 
-       r = sqlite3_prepare(db, Q_LATEST, sizeof(Q_LATEST), &stmt, NULL);
+       r = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
        if (r != SQLITE_OK) {
-               db_util_close(db);
+               sqlite3_close_v2(db);
                return -1;
        }
 
        r = sqlite3_step(stmt);
-       if (r == SQLITE_ROW) {
-               ct = sqlite3_column_text(stmt, 0);
-               if (ct == NULL || ct[0] == '\0') {
-                       r = -1;
-                       goto out;
-               }
-
-               if (strncmp(pkg_name, (const char *)ct, strlen(pkg_name)) == 0) {
-                       r = 0;
-                       goto out;
-               }
+       if (r != SQLITE_ROW) {
+               if (r != SQLITE_DONE)
+                       LOGE("step failed: %s", sqlite3_errmsg(db));
+               sqlite3_finalize(stmt);
+               sqlite3_close_v2(db);
+               return -1;
        }
 
-out:
-       if (stmt)
-               sqlite3_finalize(stmt);
-       if (db)
-               db_util_close(db);
+       ct = sqlite3_column_text(stmt, 0);
+       if (ct == NULL || ct[0] == '\0')
+               r = -1;
+       else if (strncmp(pkg_name, (const char *)ct, strlen(pkg_name)) == 0)
+               r = 0;
+       else
+               r = -1;
+
+       sqlite3_finalize(stmt);
+       sqlite3_close_v2(db);
 
        return r;
 }
 
-int rua_init(void)
+API int rua_is_latest_app(const char *pkg_name)
 {
-       return 0;
+       return rua_is_latest_app_for_uid(pkg_name, getuid());
 }
 
-int rua_fini(void)
+API int rua_init(void)
 {
        return 0;
 }
 
-static int __exec(sqlite3 *db, char *query)
+API int rua_fini(void)
 {
-       int r;
-       char *errmsg = NULL;
-
-       if (db == NULL)
-               return -1;
-
-       r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
-
-       if (r != SQLITE_OK) {
-               sqlite3_free(errmsg);
-               return -1;
-       }
-
        return 0;
 }
 
-static int __create_table(sqlite3 *db)
+API int rua_delete_history_with_instance_id(const char *app_id,
+               const char *instance_id)
 {
-       int r;
+       int ret;
+       bundle *b;
 
-       r = __exec(db, CREATE_RUA_HISTORY_TABLE);
-       if (r == -1)
+       if (app_id == NULL) {
+               LOGE("Invalid parameter");
                return -1;
-
-       return 0;
-}
-
-static sqlite3 *__db_init()
-{
-       int r;
-       sqlite3 *db = NULL;
-
-       char defname[FILENAME_MAX];
-       const char *rua_db_path = tzplatform_getenv(TZ_USER_DB);
-       if (rua_db_path == NULL) {
-               LOGE("fail to get rua_db_path");
-               return NULL;
        }
-       snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME);
 
-       r = db_util_open_with_options(defname, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
-       if (r) {
-               db_util_close(db);
-               return NULL;
+       b = bundle_create();
+       if (b == NULL) {
+               LOGE("Out of memory");
+               return -1;
        }
 
-       r = __create_table(db);
-       if (r) {
-               db_util_close(db);
-               return NULL;
+       bundle_add_str(b, AUL_K_RUA_PKGNAME, app_id);
+       if (instance_id)
+               bundle_add_str(b, AUL_K_RUA_INSTANCE_ID, instance_id);
+
+       ret = aul_delete_rua_history_for_uid(b, getuid());
+       bundle_free(b);
+       if (ret < 0) {
+               LOGE("Failed to delete rua history - result(%d)", ret);
+               return -1;
        }
 
-       return db;
+       return 0;
 }