Support multiple instance launch
[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 #include "rua_util.h"
24 #include "rua_internal.h"
25 #include "rua.h"
26 #include "db-schema.h"
27
28 int rua_add_history_for_uid(char *pkg_name, char *app_path, char *arg, uid_t uid)
29 {
30         int r;
31         char time_str[32] = {0,};
32         bundle *b = NULL;
33
34         if (pkg_name == NULL || app_path == NULL) {
35                 LOGE("invalid param");
36                 return -1;
37         }
38
39         r = _rua_util_check_uid(uid);
40         if (r == -1)
41                 return r;
42
43         b = bundle_create();
44         if (b == NULL) {
45                 LOGE("bundle_create fail out of memory.");
46                 return -1;
47         }
48         snprintf(time_str, sizeof(time_str), "%d", (int)time(NULL));
49         bundle_add_str(b, AUL_K_RUA_PKGNAME, pkg_name);
50         bundle_add_str(b, AUL_K_RUA_APPPATH, app_path);
51         bundle_add_str(b, AUL_K_RUA_ARG, arg);
52         bundle_add_str(b, AUL_K_RUA_TIME, time_str);
53
54         r = aul_add_rua_history_for_uid(b, uid);
55         LOGI("rua_add_history_for_uid result : %d ", r);
56         bundle_free(b);
57         return r;
58 }
59
60 int rua_delete_history_with_pkgname(char *pkg_name)
61 {
62         return rua_delete_history_with_pkgname_for_uid(pkg_name, getuid());
63 }
64
65 int rua_delete_history_with_pkgname_for_uid(char *pkg_name, uid_t uid)
66 {
67         int r;
68         bundle *b;
69
70         r = _rua_util_check_uid(uid);
71         if (r == -1)
72                 return r;
73
74         b = bundle_create();
75         if (b == NULL) {
76                 LOGE("bundle_create fail out of memory.");
77                 return -1;
78         }
79         bundle_add_str(b, AUL_K_RUA_PKGNAME, pkg_name);
80         r = aul_delete_rua_history_for_uid(b, uid);
81         LOGI("rua_delete_history_with_pkgname result : %d ", r);
82         bundle_free(b);
83         return r;
84 }
85
86 int rua_delete_history_with_apppath(char *app_path)
87 {
88         return rua_delete_history_with_apppath_for_uid(app_path, getuid());
89 }
90
91 int rua_delete_history_with_apppath_for_uid(char *app_path, uid_t uid)
92 {
93         int r;
94         bundle *b;
95
96         r = _rua_util_check_uid(uid);
97         if (r == -1)
98                 return r;
99
100         b = bundle_create();
101         if (b == NULL) {
102                 LOGE("bundle_create fail out of memory.");
103                 return -1;
104         }
105         bundle_add_str(b, AUL_K_RUA_APPPATH, app_path);
106         r = aul_delete_rua_history_for_uid(b, uid);
107         LOGI("rua_delete_history_with_apppath result : %d ", r);
108         bundle_free(b);
109         return r;
110 }
111
112 int rua_clear_history(void)
113 {
114         return rua_clear_history_for_uid(getuid());
115 }
116
117 int rua_clear_history_for_uid(uid_t uid)
118 {
119         int r;
120
121         r = _rua_util_check_uid(uid);
122         if (r == -1)
123                 return r;
124
125         r = aul_delete_rua_history_for_uid(NULL, uid);
126         LOGI("rua_clear_history result : %d ", r);
127         return r;
128 }
129
130 int rua_history_load_db(char ***table, int *nrows, int *ncols)
131 {
132         return rua_history_load_db_for_uid(table, nrows, ncols, getuid());
133 }
134
135 int rua_history_load_db_for_uid(char ***table, int *nrows, int *ncols, uid_t uid)
136 {
137         int r;
138         char query[QUERY_MAXLEN];
139         char *db_err = NULL;
140         char **db_result = NULL;
141         sqlite3 *db = NULL;
142
143         if (table == NULL)
144                 return -1;
145         if (nrows == NULL)
146                 return -1;
147         if (ncols == NULL)
148                 return -1;
149
150         r = _rua_util_check_uid(uid);
151         if (r == -1)
152                 return r;
153
154         r = _rua_util_open_db(&db, SQLITE_OPEN_READONLY, uid, RUA_DB_NAME);
155         if (r != SQLITE_OK)
156                 return -1;
157
158         snprintf(query, sizeof(query),
159                  "SELECT pkg_name, app_path, arg, launch_time, instance_id, "
160                  "instance_name, icon, uri FROM %s ORDER BY launch_time DESC;",
161                  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         tmp = db_result[RUA_COL_INSTANCE_ID];
218         if (tmp && tmp[0] != '\0')
219                 rec->instance_id = tmp;
220         else
221                 rec->instance_id = NULL;
222
223         tmp = db_result[RUA_COL_INSTANCE_NAME];
224         if (tmp && tmp[0] != '\0')
225                 rec->instance_name = tmp;
226         else
227                 rec->instance_name = NULL;
228
229         tmp = db_result[RUA_COL_ICON];
230         if (tmp && tmp[0] != '\0')
231                 rec->icon = tmp;
232         else
233                 rec->icon = NULL;
234
235         tmp = db_result[RUA_COL_URI];
236         if (tmp && tmp[0] != '\0')
237                 rec->uri = tmp;
238         else
239                 rec->uri = NULL;
240
241         return 0;
242 }
243
244 int rua_is_latest_app(const char *pkg_name)
245 {
246         return rua_is_latest_app_for_uid(pkg_name, getuid());
247 }
248
249 int rua_is_latest_app_for_uid(const char *pkg_name, uid_t uid)
250 {
251         int r = -1;
252         sqlite3_stmt *stmt;
253         const unsigned char *ct;
254         sqlite3 *db = NULL;
255         char *query = "select pkg_name from rua_history order by launch_time desc limit 1;";
256
257         if (!pkg_name)
258                 return -1;
259
260         r = _rua_util_check_uid(uid);
261         if (r == -1)
262                 return r;
263
264         r = _rua_util_open_db(&db, SQLITE_OPEN_READONLY, uid, RUA_DB_NAME);
265         if (r != SQLITE_OK)
266                 return -1;
267
268         r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL);
269         if (r != SQLITE_OK) {
270                 db_util_close(db);
271                 return -1;
272         }
273
274         r = sqlite3_step(stmt);
275         if (r == SQLITE_ROW) {
276                 ct = sqlite3_column_text(stmt, 0);
277                 if (ct == NULL || ct[0] == '\0') {
278                         r = -1;
279                         goto out;
280                 }
281
282                 if (strncmp(pkg_name, (const char *)ct, strlen(pkg_name)) == 0) {
283                         r = 0;
284                         goto out;
285                 }
286         }
287
288 out:
289         if (stmt)
290                 sqlite3_finalize(stmt);
291         if (db)
292                 db_util_close(db);
293
294         return r;
295 }
296
297 int rua_init(void)
298 {
299         return 0;
300 }
301
302 int rua_fini(void)
303 {
304         return 0;
305 }
306
307 int rua_delete_history_with_instance_id(const char *app_id,
308                 const char *instance_id)
309 {
310         int ret;
311         bundle *b;
312
313         if (app_id == NULL) {
314                 LOGE("Invalid parameter");
315                 return -1;
316         }
317
318         b = bundle_create();
319         if (b == NULL) {
320                 LOGE("Out of memory");
321                 return -1;
322         }
323
324         bundle_add_str(b, AUL_K_RUA_PKGNAME, app_id);
325         if (instance_id)
326                 bundle_add_str(b, AUL_K_RUA_INSTANCE_ID, instance_id);
327
328         ret = aul_delete_rua_history_for_uid(b, getuid());
329         bundle_free(b);
330         if (ret < 0) {
331                 LOGE("Failed to delete rua history - result(%d)", ret);
332                 return -1;
333         }
334
335         return 0;
336 }