Add multi-user feature
[platform/core/appfw/librua.git] / src / rua.c
1 /*
2  * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <db-util.h>
21 #include <aul.h>
22
23 /* For multi-user support */
24 #include <tzplatform_config.h>
25 #include "rua_util.h"
26 #include "rua_internal.h"
27 #include "rua.h"
28 #include "db-schema.h"
29
30 int rua_add_history_for_uid(char *pkg_name, char *app_path, char *arg, uid_t uid)
31 {
32         int r;
33         char time_str[32] = {0,};
34         bundle *b = NULL;
35
36         if (pkg_name == NULL || app_path == NULL) {
37                 LOGE("invalid param");
38                 return -1;
39         }
40
41         r = _rua_util_check_uid(uid);
42         if (r == -1)
43                 return r;
44
45         b = bundle_create();
46         if (b == NULL) {
47                 LOGE("bundle_create fail out of memory.");
48                 return -1;
49         }
50         snprintf(time_str, sizeof(time_str), "%d", (int)time(NULL));
51         bundle_add_str(b, AUL_K_RUA_PKGNAME, pkg_name);
52         bundle_add_str(b, AUL_K_RUA_APPPATH, app_path);
53         bundle_add_str(b, AUL_K_RUA_ARG, arg);
54         bundle_add_str(b, AUL_K_RUA_TIME, time_str);
55
56         r = aul_add_rua_history_for_uid(b, uid);
57         LOGI("rua_add_history_for_uid result : %d ", r);
58         bundle_free(b);
59         return r;
60 }
61
62 int rua_delete_history_with_pkgname(char *pkg_name)
63 {
64         return rua_delete_history_with_pkgname_for_uid(pkg_name, getuid());
65 }
66
67 int rua_delete_history_with_pkgname_for_uid(char *pkg_name, uid_t uid)
68 {
69         int r;
70         bundle *b;
71
72         r = _rua_util_check_uid(uid);
73         if (r == -1)
74                 return r;
75
76         b = bundle_create();
77         if (b == NULL) {
78                 LOGE("bundle_create fail out of memory.");
79                 return -1;
80         }
81         bundle_add_str(b, AUL_K_RUA_PKGNAME, pkg_name);
82         r = aul_delete_rua_history_for_uid(b, uid);
83         LOGI("rua_delete_history_with_pkgname result : %d ", r);
84         bundle_free(b);
85         return r;
86 }
87
88 int rua_delete_history_with_apppath(char *app_path)
89 {
90         return rua_delete_history_with_apppath_for_uid(app_path, getuid());
91 }
92
93 int rua_delete_history_with_apppath_for_uid(char *app_path, uid_t uid)
94 {
95         int r;
96         bundle *b;
97
98         r = _rua_util_check_uid(uid);
99         if (r == -1)
100                 return r;
101
102         b = bundle_create();
103         if (b == NULL) {
104                 LOGE("bundle_create fail out of memory.");
105                 return -1;
106         }
107         bundle_add_str(b, AUL_K_RUA_APPPATH, app_path);
108         r = aul_delete_rua_history_for_uid(b, uid);
109         LOGI("rua_delete_history_with_apppath result : %d ", r);
110         bundle_free(b);
111         return r;
112 }
113
114 int rua_clear_history(void)
115 {
116         return rua_clear_history_for_uid(getuid());
117 }
118
119 int rua_clear_history_for_uid(uid_t uid)
120 {
121         int r;
122
123         r = _rua_util_check_uid(uid);
124         if (r == -1)
125                 return r;
126
127         r = aul_delete_rua_history_for_uid(NULL, uid);
128         LOGI("rua_clear_history result : %d ", r);
129         return r;
130 }
131
132 int rua_history_load_db(char ***table, int *nrows, int *ncols)
133 {
134         return rua_history_load_db_for_uid(table, nrows, ncols, getuid());
135 }
136
137 int rua_history_load_db_for_uid(char ***table, int *nrows, int *ncols, uid_t uid)
138 {
139         int r;
140         char query[QUERY_MAXLEN];
141         char *db_err = NULL;
142         char **db_result = NULL;
143         sqlite3 *db = NULL;
144
145         if (table == NULL)
146                 return -1;
147         if (nrows == NULL)
148                 return -1;
149         if (ncols == NULL)
150                 return -1;
151
152         r = _rua_util_check_uid(uid);
153         if (r == -1)
154                 return r;
155
156         r = _rua_util_open_db(&db, SQLITE_OPEN_READONLY, uid, RUA_DB_NAME);
157         if (r != SQLITE_OK)
158                 return -1;
159
160         snprintf(query, QUERY_MAXLEN,
161                  "select pkg_name, app_path, arg, launch_time from %s order by launch_time desc;", RUA_HISTORY);
162
163         r = sqlite3_get_table(db, query, &db_result, nrows, ncols, &db_err);
164
165         if (r == SQLITE_OK)
166                 *table = db_result;
167         else
168                 sqlite3_free_table(db_result);
169
170         db_util_close(db);
171
172         return r;
173 }
174
175 int rua_history_unload_db(char ***table)
176 {
177         if (*table) {
178                 sqlite3_free_table(*table);
179                 *table = NULL;
180                 return 0;
181         }
182         return -1;
183 }
184
185 int rua_history_get_rec(struct rua_rec *rec, char **table, int nrows, int ncols,
186                         int row)
187 {
188         char **db_result = NULL;
189         char *tmp = NULL;
190
191         if (rec == NULL)
192                 return -1;
193         if (table == NULL)
194                 return -1;
195         if (row >= nrows)
196                 return -1;
197
198         db_result = table + ((row + 1) * ncols);
199
200         tmp = db_result[RUA_COL_PKGNAME];
201         if (tmp)
202                 rec->pkg_name = tmp;
203
204         LOGI("get rec pkg_name %s", rec->pkg_name);
205         tmp = db_result[RUA_COL_APPPATH];
206         if (tmp)
207                 rec->app_path = tmp;
208
209         tmp = db_result[RUA_COL_ARG];
210         if (tmp)
211                 rec->arg = tmp;
212
213         tmp = db_result[RUA_COL_LAUNCHTIME];
214         if (tmp)
215                 rec->launch_time = atoi(tmp);
216
217         return 0;
218 }
219
220 int rua_is_latest_app(const char *pkg_name)
221 {
222         return rua_is_latest_app_for_uid(pkg_name, getuid());
223 }
224
225 int rua_is_latest_app_for_uid(const char *pkg_name, uid_t uid)
226 {
227         int r = -1;
228         sqlite3_stmt *stmt;
229         const unsigned char *ct;
230         sqlite3 *db = NULL;
231         char *query = "select pkg_name from rua_history order by launch_time desc limit 1;";
232
233         if (!pkg_name)
234                 return -1;
235
236         r = _rua_util_check_uid(uid);
237         if (r == -1)
238                 return r;
239
240         r = _rua_util_open_db(&db, SQLITE_OPEN_READONLY, uid, RUA_DB_NAME);
241         if (r != SQLITE_OK)
242                 return -1;
243
244         r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL);
245         if (r != SQLITE_OK) {
246                 db_util_close(db);
247                 return -1;
248         }
249
250         r = sqlite3_step(stmt);
251         if (r == SQLITE_ROW) {
252                 ct = sqlite3_column_text(stmt, 0);
253                 if (ct == NULL || ct[0] == '\0') {
254                         r = -1;
255                         goto out;
256                 }
257
258                 if (strncmp(pkg_name, (const char *)ct, strlen(pkg_name)) == 0) {
259                         r = 0;
260                         goto out;
261                 }
262         }
263
264 out:
265         if (stmt)
266                 sqlite3_finalize(stmt);
267         if (db)
268                 db_util_close(db);
269
270         return r;
271 }
272
273 int rua_init(void)
274 {
275         return 0;
276 }
277
278 int rua_fini(void)
279 {
280         return 0;
281 }