2d7fc9bb03b7a6f5002e9513d6dec8f0bc2eb10f
[platform/core/security/privacy-guard.git] / server / src / PrivacyGuardDb.cpp
1 /*
2  * Copyright (c) 2013 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 <sstream>
18 #include <fstream>
19 #include <sqlite3.h>
20 #include <pkgmgr-info.h>
21 #include <time.h>
22 #include "Utils.h"
23 #include "PrivacyGuardDb.h"
24 #include "PrivacyIdInfo.h"
25 #if 0
26 // [CYNARA]
27 #include "CynaraService.h"
28
29 //static cynara_monitor_configuration *p_conf;
30 static cynara_monitor *p_cynara_monitor;
31 static cynara_monitor_entry **monitor_entries;
32 #endif
33
34 std::mutex PrivacyGuardDb::m_singletonMutex;
35 PrivacyGuardDb* PrivacyGuardDb::m_pInstance = NULL;
36
37 static const char* privacy_list[5] = { "http://tizen.org/privacy/location",
38                                                                  "http://tizen.org/privacy/contact",
39                                                                  "http://tizen.org/privacy/calendar",
40                                                                  "http://tizen.org/privacy/messaging",
41                                                                  "http://tizen.org/privacy/callhistory" };
42
43 #ifdef __FILTER_LISTED_PKG
44 const std::string PrivacyGuardDb::PRIVACY_FILTER_LIST_FILE = std::string("/usr/share/privacy-guard/privacy-guard-list.ini");
45 const std::string PrivacyGuardDb::FILTER_KEY = std::string("package_id");
46 std::map < std::string, bool > PrivacyGuardDb::m_filteredPkgList;
47 #endif
48
49 void
50 PrivacyGuardDb::createDB(void)
51 {
52
53 }
54
55 void
56 PrivacyGuardDb::openSqliteDB(void)
57 {
58         int res = -1;
59         res = sqlite3_open_v2(PRIVACY_DB_PATH, &m_sqlHandler, SQLITE_OPEN_READWRITE, NULL);
60         if(res == SQLITE_OK)    {
61                 PG_LOGI("monitor db is opened successfully");
62 //              sqlite3_wal_autocheckpoint(m_sqlHandler, 1);
63                 m_bDBOpen = true;
64         }
65         else {
66                 PG_LOGE("fail : monitor db open(%d)", res);
67         }
68 }
69
70 int
71 PrivacyGuardDb::PgAddPrivacyAccessLog(const int userId, std::list < std::pair < std::string, std::string > > logInfoList)
72 {
73         time_t current_date;
74         struct tm tm;
75         current_date = time(NULL);
76         localtime_r(&current_date, &tm);
77
78         if(current_date <= 0) {
79                 return PRIV_GUARD_ERROR_INVALID_PARAMETER;
80         }
81
82         int res = -1;
83
84         static const std::string QUERY_INSERT = std::string("INSERT INTO StatisticsMonitorInfo(USER_ID, PKG_ID, PRIVACY_ID, USE_DATE) VALUES(?, ?, ?, ?)");
85
86         m_dbMutex.lock();
87         // open db
88         if(m_bDBOpen == false) {
89                 openSqliteDB();
90         }
91         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
92
93         PG_LOGD("addlogToDb m_sqlHandler : %p", m_sqlHandler);
94
95         // prepare
96         res = sqlite3_prepare_v2(m_sqlHandler, QUERY_INSERT.c_str(), -1, &m_stmt, NULL);
97         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
98
99         for (std::list <std::pair <std::string, std::string>>::iterator iter = logInfoList.begin(); iter != logInfoList.end(); ++iter) {
100                 PG_LOGD("packageID : %s, PrivacyID : %s", iter->first.c_str(), iter->second.c_str());
101
102                 // bind
103                 res = sqlite3_bind_int(m_stmt, 1, userId);
104                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
105
106                 res = sqlite3_bind_text(m_stmt, 2, iter->first.c_str(), -1, SQLITE_TRANSIENT);
107                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
108
109                 res = sqlite3_bind_text(m_stmt, 3, iter->second.c_str(), -1, SQLITE_TRANSIENT);
110                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
111
112                 res = sqlite3_bind_int(m_stmt, 4, current_date);
113                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
114
115                 res = sqlite3_step(m_stmt);
116                 TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res);
117
118                 sqlite3_reset(m_stmt);
119         }
120         m_dbMutex.unlock();
121
122         return PRIV_GUARD_ERROR_SUCCESS;
123 }
124
125 int
126 PrivacyGuardDb::PgAddPrivacyAccessLogForCynara(const int userId, const std::string packageId, const std::string privilege, const timespec* timestamp)
127 {
128         if(timestamp->tv_sec <= 0) {
129                 return PRIV_GUARD_ERROR_INVALID_PARAMETER;
130         }
131
132         int res = SQLITE_OK;
133
134         // change from privilege to privacy
135         std::string privacyId;
136         res = PrivacyIdInfo::getPrivacyIdFromPrivilege(privilege, privacyId);
137         if (res == PRIV_GUARD_ERROR_NO_DATA) {
138                 return PRIV_GUARD_ERROR_SUCCESS;
139         }
140         TryReturn( res == PRIV_GUARD_ERROR_SUCCESS, res, , "getPrivacyIdFromPrivilege : %d", res);
141
142         // change from timespec to time_t
143         time_t logging_date;
144         logging_date = timestamp->tv_sec;
145
146         static const std::string QUERY_INSERT = std::string("INSERT INTO StatisticsMonitorInfo(USER_ID, PKG_ID, PRIVACY_ID, USE_DATE) VALUES(?, ?, ?, ?)");
147
148         m_dbMutex.lock();
149         // open db
150         if(m_bDBOpen == false) {
151                 openSqliteDB();
152         }
153         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
154
155         PG_LOGD("addlogToDb m_sqlHandler : %p", m_sqlHandler);
156
157         // prepare
158         res = sqlite3_prepare_v2(m_sqlHandler, QUERY_INSERT.c_str(), -1, &m_stmt, NULL);
159         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
160
161         // bind
162         res = sqlite3_bind_int(m_stmt, 1, userId);
163         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
164
165         res = sqlite3_bind_text(m_stmt, 2, packageId.c_str(), -1, SQLITE_TRANSIENT);
166         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
167
168         res = sqlite3_bind_text(m_stmt, 3, privacyId.c_str(), -1, SQLITE_TRANSIENT);
169         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
170
171         res = sqlite3_bind_int(m_stmt, 4, logging_date);
172         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
173
174         res = sqlite3_step(m_stmt);
175         TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res);
176
177         sqlite3_reset(m_stmt);
178
179         m_dbMutex.unlock();
180
181         return PRIV_GUARD_ERROR_SUCCESS;
182 }
183
184 int
185 PrivacyGuardDb::PgAddPrivacyAccessLogTest(const int userId, const std::string packageId, const std::string privacyId)
186 {
187         time_t current_date;
188         struct tm tm;
189         current_date = time(NULL);
190         localtime_r(&current_date, &tm);
191
192         if(current_date <= 0) {
193                 return PRIV_GUARD_ERROR_INVALID_PARAMETER;
194         }
195
196         int res = SQLITE_OK;
197
198         static const std::string QUERY_INSERT = std::string("INSERT INTO StatisticsMonitorInfo(USER_ID, PKG_ID, PRIVACY_ID, USE_DATE) VALUES(?, ?, ?, ?)");
199
200         m_dbMutex.lock();
201         // open db
202         if(m_bDBOpen == false) {
203                 openSqliteDB();
204         }
205         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
206
207         PG_LOGD("addlogToDb m_sqlHandler : %p", m_sqlHandler);
208
209         // prepare
210         res = sqlite3_prepare_v2(m_sqlHandler, QUERY_INSERT.c_str(), -1, &m_stmt, NULL);
211         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
212
213         // bind
214         res = sqlite3_bind_int(m_stmt, 1, userId);
215         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
216
217         res = sqlite3_bind_text(m_stmt, 2, packageId.c_str(), -1, SQLITE_TRANSIENT);
218         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
219
220         res = sqlite3_bind_text(m_stmt, 3, privacyId.c_str(), -1, SQLITE_TRANSIENT);
221         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
222
223         res = sqlite3_bind_int(m_stmt, 4, current_date);
224         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
225
226         res = sqlite3_step(m_stmt);
227         TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res);
228
229         sqlite3_reset(m_stmt);
230
231         m_dbMutex.unlock();
232
233         return PRIV_GUARD_ERROR_SUCCESS;
234 }
235
236
237 int
238 PrivacyGuardDb::PgAddMonitorPolicy(const int userId, const std::string packageId, const std::list < std::string > privacyList, bool monitorPolicy)
239 {
240         PG_LOGD("PrivacyGuardDb::PgAddMonitorPolicy");
241
242         int res = -1;
243
244         static const std::string QUERY_INSERT = std::string("INSERT INTO MonitorPolicy(USER_ID, PKG_ID, PRIVACY_ID, MONITOR_POLICY) VALUES(?, ?, ?, ?)");
245
246         m_dbMutex.lock();
247         // open db
248         if(m_bDBOpen == false) {
249                 openSqliteDB();
250         }
251         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
252
253         PG_LOGD("addlogToDb m_sqlHandler : %p", m_sqlHandler);
254
255         // prepare
256         res = sqlite3_prepare_v2(m_sqlHandler, QUERY_INSERT.c_str(), -1, &m_stmt, NULL);
257         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
258
259         for (std::list <std::string>::const_iterator iter = privacyList.begin(); iter != privacyList.end(); ++iter) {
260                 PG_LOGD("PrivacyID : %s", iter->c_str());
261
262                 // bind
263                 res = sqlite3_bind_int(m_stmt, 1, userId);
264                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
265
266                 res = sqlite3_bind_text(m_stmt, 2, packageId.c_str(), -1, SQLITE_TRANSIENT);
267                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
268
269                 res = sqlite3_bind_text(m_stmt, 3, iter->c_str(), -1, SQLITE_TRANSIENT);
270                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
271
272                 res = sqlite3_bind_int(m_stmt, 4, monitorPolicy);
273                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
274
275                 res = sqlite3_step(m_stmt);
276                 TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res);
277
278                 sqlite3_reset(m_stmt);
279         }
280         m_dbMutex.unlock();
281
282         return PRIV_GUARD_ERROR_SUCCESS;
283 }
284
285 int
286 PrivacyGuardDb::PgCheckPrivacyPackage(const int userId, const std::string packageId, bool &isPrivacyPackage)
287 {
288         int res = -1;
289         static const std::string query = std::string("SELECT COUNT(*) FROM MonitorPolicy WHERE USER_ID=? AND PKG_ID=?");
290
291         m_dbMutex.lock();
292         // open db
293         if(m_bDBOpen == false) {
294                 openSqliteDB();
295         }
296         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
297
298         // prepare
299         res = sqlite3_prepare_v2(m_sqlHandler, query.c_str(), -1, &m_stmt, NULL);
300         TryCatchResLogReturn( res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
301
302         // bind
303         res = sqlite3_bind_int(m_stmt, 1, userId);
304         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
305
306         res = sqlite3_bind_text(m_stmt, 2, packageId.c_str(), -1, SQLITE_TRANSIENT);
307         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
308
309         int count = -1;
310
311         // step
312         if ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) {
313                 count = sqlite3_column_int(m_stmt, 0);
314         }
315         m_dbMutex.unlock();
316
317         if (count > 0) {
318                 isPrivacyPackage = true;
319         }
320         else {
321                 isPrivacyPackage = false;
322         }
323
324         return PRIV_GUARD_ERROR_SUCCESS;
325 }
326
327 int
328 PrivacyGuardDb::PgDeleteAllLogsAndMonitorPolicy(void)
329 {
330         PG_LOGD("PgDeleteAllLogsAndMonitorPolicy");
331
332         int res = -1;
333
334         static const std::string LOG_DELETE = std::string("DELETE FROM StatisticsMonitorInfo");
335         static const std::string POLICY_DELETE = std::string("DELETE FROM MonitorPolicy");
336         static const std::string MAIN_POLICY_DELETE = std::string("DELETE FROM MainMonitorPolicy");
337
338         m_dbMutex.lock();
339         // open db
340         if(m_bDBOpen == false) {
341                 openSqliteDB();
342         }
343         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
344
345         // prepare
346         res = sqlite3_prepare_v2(m_sqlHandler, LOG_DELETE.c_str(), -1, &m_stmt, NULL);
347         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
348
349         res = sqlite3_step(m_stmt);
350         TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res);
351
352         res = sqlite3_prepare_v2(m_sqlHandler, POLICY_DELETE.c_str(), -1, &m_stmt, NULL);
353         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
354
355         res = sqlite3_step(m_stmt);
356         TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res);
357
358         res = sqlite3_prepare_v2(m_sqlHandler, MAIN_POLICY_DELETE.c_str(), -1, &m_stmt, NULL);
359         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
360
361         res = sqlite3_step(m_stmt);
362         TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res);
363
364         m_dbMutex.unlock();
365
366         return PRIV_GUARD_ERROR_SUCCESS;
367 }
368
369
370 int
371 PrivacyGuardDb::PgDeleteLogsByPackageId(const std::string packageId)
372 {
373         PG_LOGD("PrivacyGuardDb::PgDeleteLogsByPackageId packageid : %s", packageId.c_str());
374
375         int res = -1;
376
377         static const std::string QUERY_DELETE = std::string("DELETE FROM StatisticsMonitorInfo WHERE PKG_ID=?");
378
379         m_dbMutex.lock();
380         // open db
381         if(m_bDBOpen == false) {
382                 openSqliteDB();
383         }
384         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
385
386         // prepare
387         res = sqlite3_prepare_v2(m_sqlHandler, QUERY_DELETE.c_str(), -1, &m_stmt, NULL);
388         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
389
390         // bind
391         res = sqlite3_bind_text(m_stmt, 1, packageId.c_str(), -1, SQLITE_TRANSIENT);
392         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
393
394         // step
395         res = sqlite3_step(m_stmt);
396         TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res);
397
398         m_dbMutex.unlock();
399
400         return PRIV_GUARD_ERROR_SUCCESS;
401 }
402
403 int
404 PrivacyGuardDb::PgDeleteMonitorPolicyByPackageId(const std::string packageId)
405 {
406         PG_LOGD("PrivacyGuardDb::PgDeleteMonitorPolicyByPackageId packageid : %s", packageId.c_str());
407
408         int res = -1;
409
410         static const std::string QUERY_DELETE = std::string("DELETE FROM MonitorPolicy WHERE PKG_ID=?");
411
412         m_dbMutex.lock();
413         // open db
414         if(m_bDBOpen == false) {
415                 openSqliteDB();
416         }
417         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
418
419         // prepare
420         res = sqlite3_prepare_v2(m_sqlHandler, QUERY_DELETE.c_str(), -1, &m_stmt, NULL);
421         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
422
423         // bind
424         res = sqlite3_bind_text(m_stmt, 1, packageId.c_str(), -1, SQLITE_TRANSIENT);
425         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
426
427         // step
428         res = sqlite3_step(m_stmt);
429         TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res);
430
431         m_dbMutex.unlock();
432
433         return PRIV_GUARD_ERROR_SUCCESS;
434 }
435
436 int
437 PrivacyGuardDb::PgForeachTotalPrivacyCountOfPackage(const int userId, const int startDate, const int endDate,
438                 std::list < std::pair < std::string, int > >& packageInfoList)
439 {
440         int res = -1;
441
442 #if 0
443         // [CYNARA] Fluch Entries
444         int ret= cynara_monitor_entries_flush(p_cynara_monitor);
445         if(ret != CYNARA_API_SUCCESS){
446                 PG_LOGE("cynara_monitor_entries_flush FAIL");
447                 return PRIV_GUARD_ERROR_SYSTEM_ERROR;
448         }
449
450         // [CYNARA] Get Entries
451         ret = cynara_monitor_entries_get(p_cynara_monitor, &monitor_entries);
452         if(ret != CYNARA_API_SUCCESS){
453                 PG_LOGE("cynara_monitor_entries_get FAIL");
454                 return PRIV_GUARD_ERROR_SYSTEM_ERROR;
455         }
456
457         // [CYNARA] Update DB
458         ret = CynaraService::updateDb(monitor_entries);
459         if(ret != PRIV_GUARD_ERROR_SUCCESS){
460                 PG_LOGE("updateDb FAIL");
461                 return ret;
462         }
463 #endif
464
465         static const std::string PKGID_SELECT = std::string("SELECT DISTINCT PKG_ID FROM StatisticsMonitorInfo WHERE USER_ID=? AND USE_DATE>=? AND USE_DATE<=?");
466         static const std::string PKGINFO_SELECT = std::string("SELECT COUNT(*) FROM StatisticsMonitorInfo WHERE USER_ID=? AND PKG_ID=? AND USE_DATE>=? AND USE_DATE<=?");
467         sqlite3_stmt* infoStmt;
468
469         m_dbMutex.lock();
470         // open db
471         if(m_bDBOpen == false) {
472                 openSqliteDB();
473         }
474         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
475
476         // prepare
477         res = sqlite3_prepare_v2(m_sqlHandler, PKGID_SELECT.c_str(), -1, &m_stmt, NULL);
478         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
479
480         res = sqlite3_bind_int(m_stmt, 1, userId);
481         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
482
483         res = sqlite3_bind_int(m_stmt, 2, startDate);
484         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
485
486         res = sqlite3_bind_int(m_stmt, 3, endDate);
487         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
488
489         while ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) {
490                 const char* packageId = reinterpret_cast < const char* > (sqlite3_column_text(m_stmt, 0));
491                 if(packageId == NULL) { continue; }
492
493                 // prepare
494                 res = sqlite3_prepare_v2(m_sqlHandler, PKGINFO_SELECT.c_str(), -1, &infoStmt, NULL);
495                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
496
497                 res = sqlite3_bind_int(infoStmt, 1, userId);
498                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
499
500                 res = sqlite3_bind_text(infoStmt, 2, packageId, -1, SQLITE_TRANSIENT);
501                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
502
503                 res = sqlite3_bind_int(infoStmt, 3, startDate);
504                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
505
506                 res = sqlite3_bind_int(infoStmt, 4, endDate);
507                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
508
509                 while ((res = sqlite3_step(infoStmt)) == SQLITE_ROW) {
510                         int count = sqlite3_column_int(infoStmt, 0);
511                         if (count == 0) {
512                                 continue;
513                         }
514                         packageInfoList.push_back(std::pair <std::string, int> (std::string(packageId), count));
515                 }
516                 sqlite3_reset(infoStmt);
517         }
518         m_dbMutex.unlock();
519
520         return PRIV_GUARD_ERROR_SUCCESS;
521 }
522
523 int
524 PrivacyGuardDb::PgForeachTotalPrivacyCountOfPrivacy(const int userId, const int startDate, const int endDate,
525                 std::list < std::pair < std::string, int > >& privacyInfoList)
526 {
527         int res = -1;
528
529 #if 0
530         // [CYNARA] Fluch Entries
531         int ret= cynara_monitor_entries_flush(p_cynara_monitor);
532         if(ret != CYNARA_API_SUCCESS){
533                 PG_LOGE("cynara_monitor_entries_flush FAIL");
534                 return PRIV_GUARD_ERROR_SYSTEM_ERROR;
535         }
536
537         // [CYNARA] Get Entries
538         ret = cynara_monitor_entries_get(p_cynara_monitor, &monitor_entries);
539         if(ret != CYNARA_API_SUCCESS){
540                 PG_LOGE("cynara_monitor_entries_get FAIL");
541                 return PRIV_GUARD_ERROR_SYSTEM_ERROR;
542         }
543
544         // [CYNARA] Update DB
545         ret = CynaraService::updateDb(monitor_entries);
546         if(ret != PRIV_GUARD_ERROR_SUCCESS){
547                 PG_LOGE("updateDb FAIL");
548                 return ret;
549         }
550 #endif
551
552         static const std::string PRIVACY_SELECT = std::string("SELECT COUNT(*) FROM StatisticsMonitorInfo WHERE USER_ID=? AND PRIVACY_ID=? AND USE_DATE>=? AND USE_DATE<=?");
553
554         m_dbMutex.lock();
555         // open db
556         if(m_bDBOpen == false) {
557                 openSqliteDB();
558         }
559         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
560
561         int i;
562         int cnt_privacy = sizeof(privacy_list) / sizeof(privacy_list[0]);
563
564         for (i = 0; i < cnt_privacy; i++) {
565                 // prepare
566                 res = sqlite3_prepare_v2(m_sqlHandler, PRIVACY_SELECT.c_str(), -1, &m_stmt, NULL);
567                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
568
569                 // bind
570                 res = sqlite3_bind_int(m_stmt, 1, userId);
571                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
572
573                 res = sqlite3_bind_text(m_stmt, 2, privacy_list[i], -1, SQLITE_TRANSIENT);
574                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
575
576                 res = sqlite3_bind_int(m_stmt, 3, startDate);
577                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
578
579                 res = sqlite3_bind_int(m_stmt, 4, endDate);
580                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
581
582                 // step
583                 if ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) {
584                         int count = sqlite3_column_int(m_stmt, 0);
585                         if (count == 0) {
586                                 continue;
587                         }
588                         const char* privacyId = privacy_list[i];
589                         privacyInfoList.push_back(std::pair <std::string, int> (std::string(privacyId), count));
590                 }
591                 sqlite3_reset(m_stmt);
592         }
593
594         m_dbMutex.unlock();
595
596         return PRIV_GUARD_ERROR_SUCCESS;
597 }
598
599 int
600 PrivacyGuardDb::PgForeachPrivacyCountByPrivacyId(const int userId, const int startDate, const int endDate,
601                 const std::string privacyId, std::list < std::pair < std::string, int > >& packageInfoList)
602 {
603         int res = -1;
604
605 #if 0
606         // [CYNARA] Fluch Entries
607         int ret= cynara_monitor_entries_flush(p_cynara_monitor);
608         if(ret != CYNARA_API_SUCCESS){
609                 PG_LOGE("cynara_monitor_entries_flush FAIL");
610                 return PRIV_GUARD_ERROR_SYSTEM_ERROR;
611         }
612
613         // [CYNARA] Get Entries
614         ret = cynara_monitor_entries_get(p_cynara_monitor, &monitor_entries);
615         if(ret != CYNARA_API_SUCCESS){
616                 PG_LOGE("cynara_monitor_entries_get FAIL");
617                 return PRIV_GUARD_ERROR_SYSTEM_ERROR;
618         }
619
620         // [CYNARA] Update DB
621         ret = CynaraService::updateDb(monitor_entries);
622         if(ret != PRIV_GUARD_ERROR_SUCCESS){
623                 PG_LOGE("updateDb FAIL");
624                 return ret;
625         }
626 #endif
627
628         static const std::string PKGID_SELECT = std::string("SELECT DISTINCT PKG_ID FROM StatisticsMonitorInfo WHERE USER_ID=? AND PRIVACY_ID=? AND USE_DATE>=? AND USE_DATE<=?");
629         static const std::string PKGINFO_SELECT = std::string("SELECT COUNT(*) FROM StatisticsMonitorInfo WHERE USER_ID=? AND PKG_ID=? AND PRIVACY_ID=? AND USE_DATE>=? AND USE_DATE<=?");
630         sqlite3_stmt* infoStmt;
631
632         m_dbMutex.lock();
633         // open db
634         if(m_bDBOpen == false) {
635                 openSqliteDB();
636         }
637         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
638
639         // prepare
640         res = sqlite3_prepare_v2(m_sqlHandler, PKGID_SELECT.c_str(), -1, &m_stmt, NULL);
641         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
642
643         // bind
644         res = sqlite3_bind_int(m_stmt, 1, userId);
645         TryReturn(res == SQLITE_OK, PRIV_GUARD_ERROR_DB_ERROR, , "sqlite3_bind_int : %d", res);
646
647         res = sqlite3_bind_text(m_stmt, 2, privacyId.c_str(), -1, SQLITE_TRANSIENT);
648         TryReturn(res == SQLITE_OK, PRIV_GUARD_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
649
650         res = sqlite3_bind_int(m_stmt, 3, startDate);
651         TryReturn(res == SQLITE_OK, PRIV_GUARD_ERROR_DB_ERROR, , "sqlite3_bind_int : %d", res);
652
653         res = sqlite3_bind_int(m_stmt, 4, endDate);
654         TryReturn(res == SQLITE_OK, PRIV_GUARD_ERROR_DB_ERROR, , "sqlite3_bind_int : %d", res);
655
656         while ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) {
657                 const char* packageId =  reinterpret_cast < const char* > (sqlite3_column_text(m_stmt, 0));
658                 if(packageId == NULL) { continue; }
659
660                 // prepare
661                 res = sqlite3_prepare_v2(m_sqlHandler, PKGINFO_SELECT.c_str(), -1, &infoStmt, NULL);
662                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
663
664                 // bind
665                 res = sqlite3_bind_int(infoStmt, 1, userId);
666                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
667
668                 res = sqlite3_bind_text(infoStmt, 2, packageId, -1, SQLITE_TRANSIENT);
669                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
670
671                 res = sqlite3_bind_text(infoStmt, 3, privacyId.c_str(), -1, SQLITE_TRANSIENT);
672                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
673
674                 res = sqlite3_bind_int(infoStmt, 4, startDate);
675                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
676
677                 res = sqlite3_bind_int(infoStmt, 5, endDate);
678                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
679
680                 if ((res = sqlite3_step(infoStmt)) == SQLITE_ROW) {
681                         int count = sqlite3_column_int(infoStmt, 0);
682                         if (count == 0) {
683                                 continue;
684                         }
685                         packageInfoList.push_back(std::pair <std::string, int> (std::string(packageId), count));
686                 }
687                 sqlite3_reset(infoStmt);
688         }
689
690         m_dbMutex.unlock();
691
692         return PRIV_GUARD_ERROR_SUCCESS;
693 }
694
695 int
696 PrivacyGuardDb::PgForeachPrivacyCountByPackageId(const int userId, const int startDate, const int endDate,
697                 const std::string packageId, std::list < std::pair < std::string, int > >& privacyInfoList)
698 {
699         int res = -1;
700
701 #if 0
702         // [CYNARA] Fluch Entries
703         int ret= cynara_monitor_entries_flush(p_cynara_monitor);
704         if(ret != CYNARA_API_SUCCESS){
705                 PG_LOGE("cynara_monitor_entries_flush FAIL");
706                 return PRIV_GUARD_ERROR_SYSTEM_ERROR;
707         }
708
709         // [CYNARA] Get Entries
710         ret = cynara_monitor_entries_get(p_cynara_monitor, &monitor_entries);
711         if(ret != CYNARA_API_SUCCESS){
712                 PG_LOGE("cynara_monitor_entries_get FAIL");
713                 return PRIV_GUARD_ERROR_SYSTEM_ERROR;
714         }
715
716         // [CYNARA] Update DB
717         ret = CynaraService::updateDb(monitor_entries);
718         if(ret != PRIV_GUARD_ERROR_SUCCESS){
719                 PG_LOGE("updateDb FAIL");
720                 return ret;
721         }
722 #endif
723
724         static const std::string PRIVACY_SELECT = std::string("SELECT COUNT(*) FROM StatisticsMonitorInfo WHERE USER_ID=? AND PKG_ID=? AND PRIVACY_ID=? AND USE_DATE>=? AND USE_DATE<=?");
725
726         m_dbMutex.lock();
727         // open db
728         if(m_bDBOpen == false) {
729                 openSqliteDB();
730         }
731         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
732
733         int i;
734         int cnt_privacy = sizeof(privacy_list) / sizeof(privacy_list[0]);
735
736         for (i = 0; i < cnt_privacy; i++) {
737                 // prepare
738                 res = sqlite3_prepare_v2(m_sqlHandler, PRIVACY_SELECT.c_str(), -1, &m_stmt, NULL);
739                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
740
741                 //bind
742                 res = sqlite3_bind_int(m_stmt, 1, userId);
743                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
744
745                 res = sqlite3_bind_text(m_stmt, 2, packageId.c_str(), -1, SQLITE_TRANSIENT);
746                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
747
748                 res = sqlite3_bind_text(m_stmt, 3, privacy_list[i], -1, SQLITE_TRANSIENT);
749                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
750
751                 res = sqlite3_bind_int(m_stmt, 4, startDate);
752                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
753
754                 res = sqlite3_bind_int(m_stmt, 5, endDate);
755                 TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
756
757                 if ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) {
758                         int count = sqlite3_column_int(m_stmt, 0);
759                         if (count == 0) {
760                                 continue;
761                         }
762                         const char* privacyId = privacy_list[i];
763                         privacyInfoList.push_back(std::pair <std::string, int> (std::string(privacyId), count));
764                 }
765                 sqlite3_reset(m_stmt);
766         }
767
768         m_dbMutex.unlock();
769
770         return PRIV_GUARD_ERROR_SUCCESS;
771 }
772
773 int
774 PrivacyGuardDb::PgGetMonitorPolicy(const int userId, const std::string packageId, const std::string privacyId, int& monitorPolicy)
775 {
776
777         int res = -1;
778         static const std::string query = std::string("SELECT MONITOR_POLICY FROM MonitorPolicy WHERE USER_ID=? AND PKG_ID=? AND PRIVACY_ID=?");
779
780         m_dbMutex.lock();
781         // open db
782         if(m_bDBOpen == false) {
783                 openSqliteDB();
784         }
785         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
786
787         // prepare
788         res = sqlite3_prepare_v2(m_sqlHandler, query.c_str(), -1, &m_stmt, NULL);
789         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
790
791         // bind
792         res = sqlite3_bind_int(m_stmt, 1, userId);
793         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
794
795         res = sqlite3_bind_text(m_stmt, 2, packageId.c_str(), -1, SQLITE_TRANSIENT);
796         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
797
798         res = sqlite3_bind_text(m_stmt, 3, privacyId.c_str(), -1, SQLITE_TRANSIENT);
799         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
800
801         // step
802         monitorPolicy = 0;
803         if ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) {
804                 monitorPolicy = sqlite3_column_int(m_stmt, 0);
805         }
806         m_dbMutex.unlock();
807
808         return PRIV_GUARD_ERROR_SUCCESS;
809 }
810
811 int
812 PrivacyGuardDb::PgGetAllMonitorPolicy(std::list < std::pair < std::string, int > >& monitorPolicyList)
813 {
814         int res = -1;
815
816         static const std::string MONITOR_POLICY_SELECT = std::string("SELECT * FROM MonitorPolicy");
817
818         m_dbMutex.lock();
819         // open db
820         if(m_bDBOpen == false) {
821                 openSqliteDB();
822         }
823         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
824
825         // prepare
826         res = sqlite3_prepare_v2(m_sqlHandler, MONITOR_POLICY_SELECT.c_str(), -1, &m_stmt, NULL);
827         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
828
829         // step
830         int monitorPolicy = 0;
831         while ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) {
832                 int userId = sqlite3_column_int(m_stmt, 0);
833                 char* tmpPkgId = (char*)sqlite3_column_text(m_stmt, 1);
834                 char* tmpPrivacyId = (char*)sqlite3_column_text(m_stmt, 2);
835                 if(tmpPkgId == NULL || tmpPrivacyId == NULL) {
836                         continue;
837                 }
838                 std::string userPkgIdPrivacyId = std::to_string(userId);
839                 userPkgIdPrivacyId.append("|").append(std::string(tmpPkgId));
840                 userPkgIdPrivacyId.append("|").append(std::string(tmpPrivacyId));
841                 monitorPolicy = sqlite3_column_int(m_stmt, 3);
842                 monitorPolicyList.push_back(std::pair < std::string, int > (userPkgIdPrivacyId, monitorPolicy));
843         }
844
845         m_dbMutex.unlock();
846         if(monitorPolicyList.size() > 0) {
847                 res = PRIV_GUARD_ERROR_SUCCESS;
848         }
849         else {
850                 res = PRIV_GUARD_ERROR_NO_DATA;
851         }
852
853         return res;
854 }
855
856 int
857 PrivacyGuardDb::PgForeachMonitorPolicyByPackageId(const int userId, const std::string packageId, std::list <privacy_data_s>& privacyInfoList)
858 {
859         int res = -1;
860         static const std::string query = std::string("SELECT DISTINCT PRIVACY_ID, MONITOR_POLICY FROM MonitorPolicy WHERE USER_ID=? AND PKG_ID=?");
861
862         m_dbMutex.lock();
863         // open db
864         if(m_bDBOpen == false) {
865                 openSqliteDB();
866         }
867         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
868
869         // prepare
870         res = sqlite3_prepare_v2(m_sqlHandler, query.c_str(), -1, &m_stmt, NULL);
871         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock();, PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
872
873         // bind
874         res = sqlite3_bind_int(m_stmt, 1, userId);
875         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
876
877         res = sqlite3_bind_text(m_stmt, 2, packageId.c_str(), -1, SQLITE_TRANSIENT);
878         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
879
880         // step
881         while ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) {
882
883                 char* tmp_data = (char*)sqlite3_column_text(m_stmt, 0);
884                 if(tmp_data == NULL) {
885                         continue;
886                 }
887                 privacy_data_s p_data;
888                 p_data.privacy_id = strdup(tmp_data);
889                 p_data.monitor_policy= sqlite3_column_int(m_stmt, 1);
890
891                 privacyInfoList.push_back(p_data);
892         }
893         m_dbMutex.unlock();
894
895         return PRIV_GUARD_ERROR_SUCCESS;
896 }
897
898 int
899 PrivacyGuardDb::PgForeachPrivacyPackageId(const int userId, std::list < std::string > &packageList)
900 {
901         int res = -1;
902         static const std::string query = std::string("SELECT DISTINCT PKG_ID FROM MonitorPolicy WHERE USER_ID=?");
903
904         m_dbMutex.lock();
905         // open db
906         if(m_bDBOpen == false) {
907                 openSqliteDB();
908         }
909         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
910
911         // prepare
912         res = sqlite3_prepare_v2(m_sqlHandler, query.c_str(), -1, &m_stmt, NULL);
913         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock();, PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
914
915         // bind
916         res = sqlite3_bind_int(m_stmt, 1, userId);
917         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
918
919         // step
920         while ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) {
921                 char* p_data = (char*)sqlite3_column_text(m_stmt, 0);
922                 if(p_data == NULL) {
923                         continue;
924                 }
925                 packageList.push_back(std::string(p_data));
926         }
927         m_dbMutex.unlock();
928
929         return PRIV_GUARD_ERROR_SUCCESS;
930 }
931
932 int
933 PrivacyGuardDb::PgForeachPackageByPrivacyId(const int userId, const std::string privacyId, std::list < std::string > &packageList)
934 {
935         int res = -1;
936         static const std::string query = std::string("SELECT DISTINCT PKG_ID FROM MonitorPolicy WHERE USER_ID=? AND PRIVACY_ID=?");
937
938         m_dbMutex.lock();
939         // open db
940         if(m_bDBOpen == false) {
941                 openSqliteDB();
942         }
943         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
944
945         // prepare
946         res = sqlite3_prepare_v2(m_sqlHandler, query.c_str(), -1, &m_stmt, NULL);
947         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock();, PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
948
949         // bind
950         res = sqlite3_bind_int(m_stmt, 1, userId);
951         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
952
953         res = sqlite3_bind_text(m_stmt, 2, privacyId.c_str(), -1, SQLITE_TRANSIENT);
954         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
955
956         // step
957         while ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) {
958                 char* p_data = (char*)sqlite3_column_text(m_stmt, 0);
959                 if(p_data == NULL) {
960                         continue;
961                 }
962                 packageList.push_back(std::string(p_data));
963         }
964         m_dbMutex.unlock();
965
966         return PRIV_GUARD_ERROR_SUCCESS;
967 }
968
969 int
970 PrivacyGuardDb::PgUpdateMonitorPolicy(const int userId, const std::string packageId, const std::string privacyId, const int monitorPolicy)
971 {
972         int res = -1;
973         static const std::string query = std::string("UPDATE MonitorPolicy SET MONITOR_POLICY=? WHERE USER_ID=? AND PKG_ID=? AND PRIVACY_ID=?");
974
975         m_dbMutex.lock();
976         // open db
977         if(m_bDBOpen == false) {
978                 openSqliteDB();
979         }
980         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
981
982         // prepare
983         res = sqlite3_prepare_v2(m_sqlHandler, query.c_str(), -1, &m_stmt, NULL);
984         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
985
986         // bind
987         res = sqlite3_bind_int(m_stmt, 1, monitorPolicy);
988         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
989
990         res = sqlite3_bind_int(m_stmt, 2, userId);
991         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
992
993         res = sqlite3_bind_text(m_stmt, 3, packageId.c_str(), -1, SQLITE_TRANSIENT);
994         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
995
996         res = sqlite3_bind_text(m_stmt, 4, privacyId.c_str(), -1, SQLITE_TRANSIENT);
997         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_text : %d", res);
998
999         // step
1000         res = sqlite3_step(m_stmt);
1001         TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res);
1002
1003         m_dbMutex.unlock();
1004
1005         return PRIV_GUARD_ERROR_SUCCESS;
1006 }
1007
1008 int
1009 PrivacyGuardDb::PgAddMainMonitorPolicy(const int userId)
1010 {
1011         int res = -1;
1012
1013         static const std::string QUERY_INSERT = std::string("INSERT INTO MainMonitorPolicy(USER_ID) VALUES(?)");
1014
1015         m_dbMutex.lock();
1016         // open db
1017         if(m_bDBOpen == false) {
1018                 openSqliteDB();
1019         }
1020         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
1021
1022         PG_LOGD("addlogToDb m_sqlHandler : %p", m_sqlHandler);
1023
1024         // prepare
1025         res = sqlite3_prepare_v2(m_sqlHandler, QUERY_INSERT.c_str(), -1, &m_stmt, NULL);
1026         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
1027
1028         //bind
1029         res = sqlite3_bind_int(m_stmt, 1, userId);
1030         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
1031
1032         //step
1033         res = sqlite3_step(m_stmt);
1034         TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res);
1035
1036         m_dbMutex.unlock();
1037
1038         return PRIV_GUARD_ERROR_SUCCESS;
1039 }
1040
1041 int
1042 PrivacyGuardDb::PgUpdateMainMonitorPolicy(const int userId, const bool mainMonitorPolicy)
1043 {
1044         int res = -1;
1045         static const std::string query = std::string("UPDATE MainMonitorPolicy SET MAIN_MONITOR_POLICY=? WHERE USER_ID=?");
1046
1047         m_dbMutex.lock();
1048         // open db
1049         if(m_bDBOpen == false) {
1050                 openSqliteDB();
1051         }
1052         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
1053
1054         // prepare
1055         res = sqlite3_prepare_v2(m_sqlHandler, query.c_str(), -1, &m_stmt, NULL);
1056         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
1057
1058         // bind
1059         res = sqlite3_bind_int(m_stmt, 1, mainMonitorPolicy);
1060         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
1061
1062         res = sqlite3_bind_int(m_stmt, 2, userId);
1063         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
1064
1065         // step
1066         res = sqlite3_step(m_stmt);
1067         TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res);
1068
1069         m_dbMutex.unlock();
1070
1071 #if 0
1072         // [CYNARA] Set Filter
1073         cynara_monitor_configuration_set_filter();
1074 #endif
1075
1076         return PRIV_GUARD_ERROR_SUCCESS;
1077 }
1078
1079 int
1080 PrivacyGuardDb::PgGetMainMonitorPolicy(const int userId, bool &mainMonitorPolicy)
1081 {
1082
1083         int res = -1;
1084         static const std::string query = std::string("SELECT MAIN_MONITOR_POLICY FROM MainMonitorPolicy WHERE USER_ID=?");
1085
1086         m_dbMutex.lock();
1087         // open db
1088         if(m_bDBOpen == false) {
1089                 openSqliteDB();
1090         }
1091         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
1092
1093         // prepare
1094         res = sqlite3_prepare_v2(m_sqlHandler, query.c_str(), -1, &m_stmt, NULL);
1095         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
1096
1097         // bind
1098         res = sqlite3_bind_int(m_stmt, 1, userId);
1099         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
1100
1101         // step
1102         mainMonitorPolicy = false;
1103         if ((res = sqlite3_step(m_stmt)) == SQLITE_ROW) {
1104                 mainMonitorPolicy = sqlite3_column_int(m_stmt, 0);
1105                 m_dbMutex.unlock();
1106         }
1107         else {
1108                 m_dbMutex.unlock();
1109                 res = PgAddMainMonitorPolicy(userId);
1110                 TryReturn(res == PRIV_GUARD_ERROR_SUCCESS, res, , "PgAddMainMonitorPolicy failed : %d", res);
1111         }
1112
1113         return PRIV_GUARD_ERROR_SUCCESS;
1114 }
1115
1116 int
1117 PrivacyGuardDb::PgDeleteMainMonitorPolicyByUserId(const int userId)
1118 {
1119         int res = -1;
1120
1121         static const std::string QUERY_DELETE = std::string("DELETE FROM MainMonitorPolicy WHERE USER_ID=?");
1122
1123         m_dbMutex.lock();
1124         // open db
1125         if(m_bDBOpen == false) {
1126                 openSqliteDB();
1127         }
1128         TryCatchResLogReturn(m_bDBOpen == true, m_dbMutex.unlock(), PRIV_GUARD_ERROR_IO_ERROR, "openSqliteDB : %d", res);
1129
1130         // prepare
1131         res = sqlite3_prepare_v2(m_sqlHandler, QUERY_DELETE.c_str(), -1, &m_stmt, NULL);
1132         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_prepare_v2 : %d", res);
1133
1134         // bind
1135         res = sqlite3_bind_int(m_stmt, 1, userId);
1136         TryCatchResLogReturn(res == SQLITE_OK, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_bind_int : %d", res);
1137
1138         // step
1139         res = sqlite3_step(m_stmt);
1140         TryCatchResLogReturn(res == SQLITE_DONE, m_dbMutex.unlock(), PRIV_GUARD_ERROR_DB_ERROR, "sqlite3_step : %d", res);
1141
1142         m_dbMutex.unlock();
1143
1144         return PRIV_GUARD_ERROR_SUCCESS;
1145 }
1146
1147 PrivacyGuardDb::PrivacyGuardDb(void)
1148 {
1149
1150         // open DB
1151         m_bDBOpen = false;
1152         m_sqlHandler = NULL;
1153         m_dbMutex.lock();
1154         openSqliteDB();
1155         m_dbMutex.unlock();
1156         m_stmt = NULL;
1157 }
1158
1159 PrivacyGuardDb::~PrivacyGuardDb(void)
1160 {
1161         // close DB
1162         if(m_bDBOpen == true) {
1163                 m_dbMutex.lock();
1164                 sqlite3_finalize(m_stmt);
1165                 sqlite3_close(m_sqlHandler);
1166                 m_bDBOpen = false;
1167                 m_dbMutex.unlock();
1168         }
1169 }
1170
1171 PrivacyGuardDb*
1172 PrivacyGuardDb::getInstance(void)
1173 {
1174         std::lock_guard < std::mutex > guard(m_singletonMutex);
1175
1176         if (m_pInstance == NULL)
1177         {       
1178                 m_pInstance = new PrivacyGuardDb();
1179         }
1180
1181         return m_pInstance;
1182 }