Support multiple instance launch
[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 <db-util.h>
18 #include <aul.h>
19
20 #include "rua_internal.h"
21 #include "db-schema.h"
22 #include "rua_util.h"
23
24 static int __exec(sqlite3 *db, char *query)
25 {
26         int r;
27         char *errmsg = NULL;
28
29         if (db == NULL)
30                 return -1;
31
32         r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
33
34         if (r != SQLITE_OK) {
35                 sqlite3_free(errmsg);
36                 return -1;
37         }
38
39         return 0;
40 }
41
42 static int __create_table(sqlite3 *db)
43 {
44         int r;
45
46         r = __exec(db, CREATE_RUA_HISTORY_TABLE);
47         if (r == -1)
48                 return -1;
49
50         return 0;
51 }
52
53 static sqlite3 *__db_init(uid_t uid)
54 {
55         int r;
56         sqlite3 *db = NULL;
57
58         r = _rua_util_open_db(&db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
59                         uid, RUA_DB_NAME);
60         if (r != SQLITE_OK)
61                 return NULL;
62
63         r = __create_table(db);
64         if (r) {
65                 db_util_close(db);
66                 return NULL;
67         }
68
69         return db;
70 }
71
72 int rua_db_delete_history(bundle *b)
73 {
74         return rua_usr_db_delete_history(b, getuid());
75 }
76
77 int rua_usr_db_delete_history(bundle *b, uid_t uid)
78 {
79         int r;
80         sqlite3 *db = NULL;
81         char query[QUERY_MAXLEN];
82         char *pkg_name = NULL;
83         char *app_path = NULL;
84         char *instance_id = NULL;
85         char *errmsg = NULL;
86         int result = 0;
87
88         db = __db_init(uid);
89         if (db == NULL) {
90                 LOGE("Error db null");
91                 return -1;
92         }
93
94         if (b != NULL) {
95                 bundle_get_str(b, AUL_K_RUA_PKGNAME, &pkg_name);
96                 bundle_get_str(b, AUL_K_RUA_APPPATH, &app_path);
97                 bundle_get_str(b, AUL_K_RUA_INSTANCE_ID, &instance_id);
98         }
99
100         if (pkg_name) {
101                 snprintf(query, sizeof(query),
102                                 "DELETE FROM rua_history WHERE pkg_name = '%s' "
103                                 "AND instance_id = '%s';",
104                                 pkg_name, instance_id ? instance_id : "");
105         } else if (app_path) {
106                 snprintf(query, sizeof(query),
107                                 "DELETE FROM rua_history WHERE app_path = '%s' "
108                                 "AND instance_id = '%s';",
109                                 app_path, instance_id ? instance_id : "");
110         } else {
111                 snprintf(query, sizeof(query), "DELETE FROM rua_history;");
112         }
113
114         LOGI("rua_delete_history_from_db : %s", query);
115         r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
116
117         if (r != SQLITE_OK) {
118                 LOGE("fail to exec delete query %s : %s", query, errmsg);
119                 sqlite3_free(errmsg);
120                 result = -1;
121         }
122
123         if (db != NULL)
124                 db_util_close(db);
125
126         return result;
127 }
128
129 int rua_db_add_history(struct rua_rec *rec)
130 {
131         return rua_usr_db_add_history(rec, getuid());
132 }
133
134 int rua_usr_db_add_history(struct rua_rec *rec, uid_t uid)
135 {
136         int r;
137         char query[QUERY_MAXLEN];
138         sqlite3 *db;
139
140         if (rec == NULL || rec->pkg_name == NULL || rec->app_path == NULL) {
141                 LOGE("Invalid parameter");
142                 return -1;
143         }
144
145         db = __db_init(uid);
146         if (db == NULL) {
147                 LOGE("Error db null");
148                 return -1;
149         }
150
151         if (rec->instance_id &&
152                         (rec->instance_name == NULL || rec->icon == NULL ||
153                          rec->uri == NULL)) {
154                 snprintf(query, sizeof(query),
155                                 "UPDATE %s SET launch_time = %d "
156                                 "WHERE pkg_name = %s AND instance_id = %s;",
157                                 RUA_HISTORY,
158                                 (int)rec->launch_time,
159                                 rec->pkg_name,
160                                 rec->instance_id);
161         } else {
162                 snprintf(query, sizeof(query),
163                                 "INSERT OR REPLACE INTO %s "
164                                 "(pkg_name, app_path, arg, launch_time, "
165                                 "instance_id, instance_name, icon, uri) "
166                                 "VALUES (\"%s\", \"%s\", \"%s\", %d, "
167                                 "\"%s\", \"%s\", \"%s\", \"%s\");",
168                                 RUA_HISTORY,
169                                 rec->pkg_name,
170                                 rec->app_path,
171                                 rec->arg ? rec->arg : "",
172                                 (int)rec->launch_time,
173                                 rec->instance_id ? rec->instance_id : "",
174                                 rec->instance_name ? rec->instance_name : "",
175                                 rec->icon ? rec->icon : "",
176                                 rec->uri ? rec->uri : "");
177         }
178
179         r = __exec(db, query);
180         if (r == -1) {
181                 LOGE("[RUA ADD HISTORY ERROR] %s\n", query);
182                 db_util_close(db);
183                 return -1;
184         }
185
186         db_util_close(db);
187         return r;
188 }