[Non-ACR][Preventing SQL Injection Attack]
[platform/core/context/app-history.git] / src / server / usage-stats / InstallMonitor.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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 <sstream>
18 #include <AppHistoryTypes.h>
19 #include <AppHistoryTypesPrivate.h>
20 #include "../DatabaseManager.h"
21 #include "AppStatisticsTypes.h"
22 #include "InstallMonitor.h"
23
24 static package_manager_event_type_e __lastEventType;
25
26 ctx::AppInstallMonitor::AppInstallMonitor() :
27         __pkgmgrHandle(NULL)
28 {
29         __startMonitoring();
30 }
31
32 ctx::AppInstallMonitor::~AppInstallMonitor()
33 {
34         __stopMonitoring();
35 }
36
37 bool ctx::AppInstallMonitor::__startMonitoring()
38 {
39         int err = package_manager_create(&__pkgmgrHandle);
40         IF_FAIL_RETURN_TAG(err == PACKAGE_MANAGER_ERROR_NONE, false, _E, "package_manager_create() failed");
41
42         err = package_manager_set_event_cb(__pkgmgrHandle, __packageEventCb, this);
43         IF_FAIL_RETURN_TAG(err == PACKAGE_MANAGER_ERROR_NONE, false, _E, "package_manager_set_event_cb() failed");
44
45         return true;
46 }
47
48 void ctx::AppInstallMonitor::__stopMonitoring()
49 {
50         if(__pkgmgrHandle) {
51                 package_manager_unset_event_cb(__pkgmgrHandle);
52                 package_manager_destroy(__pkgmgrHandle);
53                 __pkgmgrHandle = NULL;
54         }
55 }
56
57 void ctx::AppInstallMonitor::__packageEventCb(const char *type, const char *package, package_manager_event_type_e eventType, package_manager_event_state_e eventState, int progress, package_manager_error_e error, void *userData)
58 {
59         IF_FAIL_VOID_TAG(error == PACKAGE_MANAGER_ERROR_NONE, _E, "package_manager error: %d", error);
60
61         if (!(eventType == PACKAGE_MANAGER_EVENT_TYPE_INSTALL && eventState == PACKAGE_MANAGER_EVENT_STATE_COMPLETED) &&
62                 !(eventType == PACKAGE_MANAGER_EVENT_TYPE_UNINSTALL && eventState == PACKAGE_MANAGER_EVENT_STATE_STARTED)) {
63                 _D("Skipping event type-%d / state-%d", eventType, eventState);
64                 return;
65         }
66
67         package_info_h pkgInfo;
68         int err = package_manager_get_package_info(package, &pkgInfo);
69         IF_FAIL_VOID_TAG(err == PACKAGE_MANAGER_ERROR_NONE, _E, "package_manager_get_package_info() failed");
70
71         __lastEventType = eventType;
72
73         err = package_info_foreach_app_from_package(pkgInfo, PACKAGE_INFO_UIAPP, __appInfoCb, userData);
74         if (err != PACKAGE_MANAGER_ERROR_NONE)
75                 _E("package_info_foreach_app_from_package() failed");
76
77         package_info_destroy(pkgInfo);
78 }
79
80 bool ctx::AppInstallMonitor::__appInfoCb(package_info_app_component_type_e compType, const char *appId, void *userData)
81 {
82         Database* database = DatabaseManager::getInstance();
83         int count = 1;
84
85         if (__lastEventType == PACKAGE_MANAGER_EVENT_TYPE_INSTALL) {
86                 apphistory_stmt hstmt = NULL;
87                 char query[APPHISTORY_SQL_LEN_MAX] = {0, };
88
89                 APPHISTORY_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId) VALUES " "(?) ", APP_TABLE_REMOVABLE_APP);
90
91                 hstmt = database->prepare_query(query);
92                 database->query_bind_text(hstmt, count, appId);
93                 database->execute(hstmt);
94
95         } else if (__lastEventType == PACKAGE_MANAGER_EVENT_TYPE_UNINSTALL) {
96                 database->execute(__createDeletionQuery(APP_TABLE_REMOVABLE_APP, appId));
97                 database->execute(__createDeletionQuery(APP_TABLE_USAGE_LOG, appId));
98         }
99
100         return true;
101 }
102
103 apphistory_stmt ctx::AppInstallMonitor::__createDeletionQuery(const char* tableName, const char* appId)
104 {
105         Database *database = DatabaseManager::getInstance();
106         int count = 1;
107
108         apphistory_stmt hstmt = NULL;
109         char query[APPHISTORY_SQL_LEN_MAX] = {0, };
110
111         APPHISTORY_SNPRINTF(query, sizeof(query), "DELETE FROM %s(AppId) WHERE KEY_APP_ID = ? ", tableName);
112
113         hstmt = database->prepare_query(query);
114         database->query_bind_text(hstmt, count, appId);
115         return hstmt;
116 }
117