2 * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <sys/types.h>
25 #include "rua_internal.h"
26 #include "db-schema.h"
29 #include "rua_private.h"
31 static int __exec(sqlite3 *db, char *query)
39 r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
49 static int __create_table(sqlite3 *db)
53 r = __exec(db, CREATE_RUA_HISTORY_TABLE);
60 static sqlite3 *__db_init(uid_t uid)
65 r = _rua_util_open_db(&db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
70 r = __create_table(db);
79 static int __delete_history_with_pkg_name(sqlite3 *db, const char *pkg_name,
80 const char *instance_id)
82 static const char query[] =
83 "DELETE FROM rua_history WHERE pkg_name=? AND instance_id=?";
88 r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
90 LOGE("prepare failed: %s", sqlite3_errmsg(db));
94 __BIND_TEXT(db, stmt, idx++, pkg_name);
95 __BIND_TEXT(db, stmt, idx++, instance_id ? instance_id : "");
97 r = sqlite3_step(stmt);
98 if (r != SQLITE_DONE) {
99 LOGE("step failed: %s", sqlite3_errmsg(db));
100 sqlite3_finalize(stmt);
104 sqlite3_finalize(stmt);
109 static int __delete_history_with_app_path(sqlite3 *db, const char *app_path,
110 const char *instance_id)
112 static const char query[] =
113 "DELETE FROM rua_history WHERE app_path=? AND instance_id=?";
118 r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
119 if (r != SQLITE_OK) {
120 LOGE("prepare failed: %s", sqlite3_errmsg(db));
124 __BIND_TEXT(db, stmt, idx++, app_path);
125 __BIND_TEXT(db, stmt, idx++, instance_id ? instance_id : "");
127 r = sqlite3_step(stmt);
128 if (r != SQLITE_DONE) {
129 LOGE("step failed: %s", sqlite3_errmsg(db));
130 sqlite3_finalize(stmt);
134 sqlite3_finalize(stmt);
139 static int __clear_history(sqlite3 *db)
141 static const char query[] = "DELETE FROM rua_history";
145 r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
146 if (r != SQLITE_OK) {
147 LOGE("prepare failed: %s", sqlite3_errmsg(db));
151 r = sqlite3_step(stmt);
152 if (r != SQLITE_DONE) {
153 LOGE("step failed: %s", sqlite3_errmsg(db));
154 sqlite3_finalize(stmt);
158 sqlite3_finalize(stmt);
163 API int rua_usr_db_delete_history(bundle *b, uid_t uid)
167 char *pkg_name = NULL;
168 char *app_path = NULL;
169 char *instance_id = NULL;
173 LOGE("Error db 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);
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);
190 LOGI("rua clear history");
191 r = __clear_history(db);
194 sqlite3_close_v2(db);
197 LOGE("Failed to delete history");
201 r = rua_dbus_send_update_signal(DELETE);
203 LOGE("[RUA SEND SIGNAL ERROR] \n");
210 API int rua_db_delete_history(bundle *b)
212 return rua_usr_db_delete_history(b, getuid());
215 static int __insert_history(sqlite3 *db, struct rua_rec *rec)
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,"
222 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
227 r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
228 if (r != SQLITE_OK) {
229 LOGE("prepare failed: %s", sqlite3_errmsg(db));
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 : "");
245 r = sqlite3_step(stmt);
246 if (r != SQLITE_DONE) {
247 LOGE("step failed: %s", sqlite3_errmsg(db));
248 sqlite3_finalize(stmt);
252 sqlite3_finalize(stmt);
257 static int __update_history(sqlite3 *db, struct rua_rec *rec)
259 static const char query[] =
260 "UPDATE rua_history SET launch_time=? "
261 "WHERE pkg_name=? AND comp_id=? AND instance_id=?";
266 r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
267 if (r != SQLITE_OK) {
268 LOGE("prepare failed: %s", sqlite3_errmsg(db));
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);
277 r = sqlite3_step(stmt);
278 if (r != SQLITE_DONE) {
279 LOGE("step failed: %s", sqlite3_errmsg(db));
280 sqlite3_finalize(stmt);
284 sqlite3_finalize(stmt);
289 API int rua_usr_db_add_history(struct rua_rec *rec, uid_t uid)
294 if (rec == NULL || rec->pkg_name == NULL || rec->app_path == NULL) {
295 LOGE("Invalid parameter");
301 LOGE("Error db null");
305 if (rec->instance_id &&
306 (rec->instance_name == NULL || rec->icon == NULL ||
308 r = __update_history(db, rec);
310 r = __insert_history(db, rec);
312 LOGE("Failed insert or update history");
313 sqlite3_close_v2(db);
317 r = rua_dbus_send_update_signal(ADD);
319 LOGE("[RUA SEND SIGNAL ERROR] \n");
320 sqlite3_close_v2(db);
324 sqlite3_close_v2(db);
328 API int rua_db_add_history(struct rua_rec *rec)
330 return rua_usr_db_add_history(rec, getuid());
333 static int __update_image(sqlite3 *db, const char *pkg_name, const char *comp_id,
334 const char *instance_id, const char *image)
336 static const char query[] =
337 "UPDATE rua_history SET image=? "
338 "WHERE pkg_name=? AND comp_id=? AND instance_id=?";
343 r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
344 if (r != SQLITE_OK) {
345 LOGE("Prepare failed: %s", sqlite3_errmsg(db));
349 __BIND_TEXT(db, stmt, idx++, image);
350 __BIND_TEXT(db, stmt, idx++, pkg_name);
351 __BIND_TEXT(db, stmt, idx++, comp_id ? comp_id : "");
352 __BIND_TEXT(db, stmt, idx++, instance_id ? instance_id : "");
354 r = sqlite3_step(stmt);
355 if (r != SQLITE_DONE) {
356 LOGE("Step failed: %s", sqlite3_errmsg(db));
357 sqlite3_finalize(stmt);
360 sqlite3_finalize(stmt);
365 API int rua_usr_db_update_image(const char *pkg_name, const char *comp_id,
366 const char *instance_id, const char *image, uid_t uid)
371 if (pkg_name == NULL || image == NULL) {
372 LOGE("Invalid parameter");
378 LOGE("Error db null");
382 r = __update_image(db, pkg_name, comp_id, instance_id, image);
384 LOGE("Failed to update image - appid(%s)", pkg_name);
385 sqlite3_close_v2(db);
388 sqlite3_close_v2(db);
390 r = rua_dbus_send_update_signal(UPDATE);
392 LOGE("[RUA SEND SIGNAL ERROR]");
399 API int rua_db_update_image(const char *pkg_name, const char *comp_id,
400 const char *instance_id, const char *image)
402 return rua_usr_db_update_image(
403 pkg_name, comp_id, instance_id, image, getuid());