Release version 0.5.14
[platform/core/appfw/librua.git] / src / rua_internal.c
1 /*
2  * Copyright (c) 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 <sys/types.h>
19 #include <unistd.h>
20
21 #include <aul.h>
22 #include <dlog.h>
23 #include <sqlite3.h>
24
25 #include "rua_internal.h"
26 #include "db-schema.h"
27 #include "rua_util.h"
28 #include "rua_dbus.h"
29 #include "rua_private.h"
30
31 static int __exec(sqlite3 *db, char *query)
32 {
33         int r;
34         char *errmsg = NULL;
35
36         if (db == NULL)
37                 return -1;
38
39         r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
40
41         if (r != SQLITE_OK) {
42                 sqlite3_free(errmsg);
43                 return -1;
44         }
45
46         return 0;
47 }
48
49 static int __create_table(sqlite3 *db)
50 {
51         int r;
52
53         r = __exec(db, CREATE_RUA_HISTORY_TABLE);
54         if (r == -1)
55                 return -1;
56
57         return 0;
58 }
59
60 static sqlite3 *__db_init(uid_t uid)
61 {
62         int r;
63         sqlite3 *db = NULL;
64
65         r = _rua_util_open_db(&db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
66                         uid, RUA_DB_NAME);
67         if (r != SQLITE_OK)
68                 return NULL;
69
70         r = __create_table(db);
71         if (r) {
72                 sqlite3_close_v2(db);
73                 return NULL;
74         }
75
76         return db;
77 }
78
79 static int __delete_history_with_pkg_name(sqlite3 *db, const char *pkg_name,
80                 const char *instance_id)
81 {
82         static const char query[] =
83                 "DELETE FROM rua_history WHERE pkg_name=? AND instance_id=?";
84         sqlite3_stmt *stmt;
85         int r;
86         int idx = 1;
87
88         r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
89         if (r != SQLITE_OK) {
90                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
91                 return -1;
92         }
93
94         __BIND_TEXT(db, stmt, idx++, pkg_name);
95         __BIND_TEXT(db, stmt, idx++, instance_id ? instance_id : "");
96
97         r = sqlite3_step(stmt);
98         if (r != SQLITE_DONE) {
99                 LOGE("step failed: %s", sqlite3_errmsg(db));
100                 sqlite3_finalize(stmt);
101                 return -1;
102         }
103
104         sqlite3_finalize(stmt);
105
106         return 0;
107 }
108
109 static int __delete_history_with_app_path(sqlite3 *db, const char *app_path,
110                 const char *instance_id)
111 {
112         static const char query[] =
113                 "DELETE FROM rua_history WHERE app_path=? AND instance_id=?";
114         sqlite3_stmt *stmt;
115         int r;
116         int idx = 1;
117
118         r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
119         if (r != SQLITE_OK) {
120                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
121                 return -1;
122         }
123
124         __BIND_TEXT(db, stmt, idx++, app_path);
125         __BIND_TEXT(db, stmt, idx++, instance_id ? instance_id : "");
126
127         r = sqlite3_step(stmt);
128         if (r != SQLITE_DONE) {
129                 LOGE("step failed: %s", sqlite3_errmsg(db));
130                 sqlite3_finalize(stmt);
131                 return -1;
132         }
133
134         sqlite3_finalize(stmt);
135
136         return 0;
137 }
138
139 static int __clear_history(sqlite3 *db)
140 {
141         static const char query[] = "DELETE FROM rua_history";
142         sqlite3_stmt *stmt;
143         int r;
144
145         r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
146         if (r != SQLITE_OK) {
147                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
148                 return -1;
149         }
150
151         r = sqlite3_step(stmt);
152         if (r != SQLITE_DONE) {
153                 LOGE("step failed: %s", sqlite3_errmsg(db));
154                 sqlite3_finalize(stmt);
155                 return -1;
156         }
157
158         sqlite3_finalize(stmt);
159
160         return 0;
161 }
162
163 API int rua_usr_db_delete_history(bundle *b, uid_t uid)
164 {
165         int r;
166         sqlite3 *db;
167         char *pkg_name = NULL;
168         char *app_path = NULL;
169         char *instance_id = NULL;
170
171         db = __db_init(uid);
172         if (db == NULL) {
173                 LOGE("Error db null");
174                 return -1;
175         }
176
177         if (b != NULL) {
178                 bundle_get_str(b, AUL_K_RUA_PKGNAME, &pkg_name);
179                 bundle_get_str(b, AUL_K_RUA_APPPATH, &app_path);
180                 bundle_get_str(b, AUL_K_RUA_INSTANCE_ID, &instance_id);
181         }
182
183         if (pkg_name) {
184                 LOGI("rua_delete_history_from_db : %s", pkg_name);
185                 r = __delete_history_with_pkg_name(db, pkg_name, instance_id);
186         } else if (app_path) {
187                 LOGI("rua_delete_history_from_db : %s", app_path);
188                 r = __delete_history_with_app_path(db, app_path, instance_id);
189         } else {
190                 LOGI("rua clear history");
191                 r = __clear_history(db);
192         }
193
194         sqlite3_close_v2(db);
195
196         if (r == -1) {
197                 LOGE("Failed to delete history");
198                 return -1;
199         }
200
201         r = rua_dbus_send_update_signal(DELETE);
202         if (r == -1) {
203                 LOGE("[RUA SEND SIGNAL ERROR] \n");
204                 return -1;
205         }
206
207         return 0;
208 }
209
210 API int rua_db_delete_history(bundle *b)
211 {
212         return rua_usr_db_delete_history(b, getuid());
213 }
214
215 static int __insert_history(sqlite3 *db, struct rua_rec *rec)
216 {
217         static const char query[] =
218                 "INSERT OR REPLACE INTO rua_history ("
219                 "  pkg_name, app_path, arg, launch_time,"
220                 "  instance_id, instance_name, icon, uri,"
221                 "  image, comp_id) "
222                 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
223         int r;
224         sqlite3_stmt *stmt;
225         int idx = 1;
226
227         r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
228         if (r != SQLITE_OK) {
229                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
230                 return -1;
231         }
232
233         __BIND_TEXT(db, stmt, idx++, rec->pkg_name);
234         __BIND_TEXT(db, stmt, idx++, rec->app_path);
235         __BIND_TEXT(db, stmt, idx++, rec->arg ? rec->arg : "");
236         __BIND_INT(db, stmt, idx++, (int)rec->launch_time);
237         __BIND_TEXT(db, stmt, idx++, rec->instance_id ? rec->instance_id : "");
238         __BIND_TEXT(db, stmt, idx++,
239                         rec->instance_name ? rec->instance_name : "");
240         __BIND_TEXT(db, stmt, idx++, rec->icon ? rec->icon : "");
241         __BIND_TEXT(db, stmt, idx++, rec->uri ? rec->uri : "");
242         __BIND_TEXT(db, stmt, idx++, rec->image ? rec->image : "");
243         __BIND_TEXT(db, stmt, idx++, rec->comp_id ? rec->comp_id : "");
244
245         r = sqlite3_step(stmt);
246         if (r != SQLITE_DONE) {
247                 LOGE("step failed: %s", sqlite3_errmsg(db));
248                 sqlite3_finalize(stmt);
249                 return -1;
250         }
251
252         sqlite3_finalize(stmt);
253
254         return 0;
255 }
256
257 static int __update_history(sqlite3 *db, struct rua_rec *rec)
258 {
259         static const char query[] =
260                 "UPDATE rua_history SET launch_time=? "
261                 "WHERE pkg_name=? AND comp_id=? AND instance_id=?";
262         int r;
263         sqlite3_stmt *stmt;
264         int idx = 1;
265
266         r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
267         if (r != SQLITE_OK) {
268                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
269                 return -1;
270         }
271
272         __BIND_INT(db, stmt, idx++, (int)rec->launch_time);
273         __BIND_TEXT(db, stmt, idx++, rec->pkg_name);
274         __BIND_TEXT(db, stmt, idx++, rec->comp_id);
275         __BIND_TEXT(db, stmt, idx++, rec->instance_id);
276
277         r = sqlite3_step(stmt);
278         if (r != SQLITE_DONE) {
279                 LOGE("step failed: %s", sqlite3_errmsg(db));
280                 sqlite3_finalize(stmt);
281                 return -1;
282         }
283
284         sqlite3_finalize(stmt);
285
286         return 0;
287 }
288
289 API int rua_usr_db_update_history(const char *pkg_name,
290                 const char *comp_id, int launch_time, uid_t uid)
291 {
292         static const char query[] =
293                 "UPDATE rua_history SET launch_time=? "
294                 "WHERE pkg_name=? AND comp_id=?";
295         int r;
296         sqlite3_stmt *stmt;
297         int idx = 1;
298         sqlite3 *db;
299
300         db = __db_init(uid);
301         if (db == NULL) {
302                 LOGE("Error db null");
303                 return -1;
304         }
305
306         r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
307         if (r != SQLITE_OK) {
308                 LOGE("prepare failed: %s", sqlite3_errmsg(db));
309                 sqlite3_close_v2(db);
310                 return -1;
311         }
312
313         __BIND_INT(db, stmt, idx++, launch_time);
314         __BIND_TEXT(db, stmt, idx++, pkg_name);
315         __BIND_TEXT(db, stmt, idx++, comp_id ? comp_id : "");
316
317         r = sqlite3_step(stmt);
318         if (r != SQLITE_DONE) {
319                 LOGE("step failed: %s", sqlite3_errmsg(db));
320                 sqlite3_finalize(stmt);
321                 sqlite3_close_v2(db);
322                 return -1;
323         }
324
325         sqlite3_finalize(stmt);
326         sqlite3_close_v2(db);
327         return r;
328 }
329
330 API int rua_db_update_history(const char *pkg_name,
331                 const char *comp_id, int launch_time)
332 {
333         return rua_usr_db_update_history(
334                 pkg_name, comp_id, launch_time, getuid());
335 }
336
337 API int rua_usr_db_add_history(struct rua_rec *rec, uid_t uid)
338 {
339         int r;
340         sqlite3 *db;
341
342         if (rec == NULL || rec->pkg_name == NULL || rec->app_path == NULL) {
343                 LOGE("Invalid parameter");
344                 return -1;
345         }
346
347         db = __db_init(uid);
348         if (db == NULL) {
349                 LOGE("Error db null");
350                 return -1;
351         }
352
353         if (rec->instance_id &&
354                         (rec->instance_name == NULL || rec->icon == NULL ||
355                          rec->uri == NULL))
356                 r = __update_history(db, rec);
357         else
358                 r = __insert_history(db, rec);
359         if (r == -1) {
360                 LOGE("Failed insert or update history");
361                 sqlite3_close_v2(db);
362                 return -1;
363         }
364
365         r = rua_dbus_send_update_signal(ADD);
366         if (r == -1) {
367                 LOGE("[RUA SEND SIGNAL ERROR] \n");
368                 sqlite3_close_v2(db);
369                 return -1;
370         }
371
372         sqlite3_close_v2(db);
373         return r;
374 }
375
376 API int rua_db_add_history(struct rua_rec *rec)
377 {
378         return rua_usr_db_add_history(rec, getuid());
379 }
380
381 static int __update_image(sqlite3 *db, const char *pkg_name, const char *comp_id,
382                 const char *instance_id, const char *image)
383 {
384         static const char query[] =
385                 "UPDATE rua_history SET image=? "
386                 "WHERE pkg_name=? AND comp_id=? AND instance_id=?";
387         sqlite3_stmt *stmt;
388         int idx = 1;
389         int r;
390
391         r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
392         if (r != SQLITE_OK) {
393                 LOGE("Prepare failed: %s", sqlite3_errmsg(db));
394                 return -1;
395         }
396
397         __BIND_TEXT(db, stmt, idx++, image);
398         __BIND_TEXT(db, stmt, idx++, pkg_name);
399         __BIND_TEXT(db, stmt, idx++, comp_id ? comp_id : "");
400         __BIND_TEXT(db, stmt, idx++, instance_id ? instance_id : "");
401
402         r = sqlite3_step(stmt);
403         if (r != SQLITE_DONE) {
404                 LOGE("Step failed: %s", sqlite3_errmsg(db));
405                 sqlite3_finalize(stmt);
406                 return -1;
407         }
408         sqlite3_finalize(stmt);
409
410         return 0;
411 }
412
413 API int rua_usr_db_update_image(const char *pkg_name, const char *comp_id,
414                 const char *instance_id, const char *image, uid_t uid)
415 {
416         int r;
417         sqlite3 *db;
418
419         if (pkg_name == NULL || image == NULL) {
420                 LOGE("Invalid parameter");
421                 return -1;
422         }
423
424         db = __db_init(uid);
425         if (db == NULL) {
426                 LOGE("Error db null");
427                 return -1;
428         }
429
430         r = __update_image(db, pkg_name, comp_id, instance_id, image);
431         if (r < 0) {
432                 LOGE("Failed to update image - appid(%s)", pkg_name);
433                 sqlite3_close_v2(db);
434                 return -1;
435         }
436         sqlite3_close_v2(db);
437
438         r = rua_dbus_send_update_signal(UPDATE);
439         if (r < 0) {
440                 LOGE("[RUA SEND SIGNAL ERROR]");
441                 return -1;
442         }
443
444         return r;
445 }
446
447 API int rua_db_update_image(const char *pkg_name, const char *comp_id,
448                 const char *instance_id, const char *image)
449 {
450         return rua_usr_db_update_image(
451                         pkg_name, comp_id, instance_id, image, getuid());
452 }