2 * Copyright (c) 2013 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.
20 #include <PrivacyDb.h>
21 #include <PrivacyManagerTypes.h>
23 #include <pkgmgr-info.h>
25 std::mutex PrivacyDb::m_singletonMutex;
26 PrivacyDb* PrivacyDb::m_pInstance = NULL;
29 PrivacyDb::createDB(void)
34 bool PrivacyDb::isFilteredPackage(const std::string pkgId) const
36 pkgmgrinfo_pkginfo_h handle;
38 int res = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &handle);
40 if (res != PMINFO_R_OK)
43 bool preloaded = false;
44 res = pkgmgrinfo_pkginfo_is_preload(handle, &preloaded);
45 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
51 PrivacyDb::setPrivacySetting(const std::string pkgId, const std::string privacyId, bool enabled)
55 static const std::string query = std::string("UPDATE PrivacyInfo set IS_ENABLED =? where PKG_ID=? and PRIVACY_ID=?");
57 openDb(PRIVACY_DB_PATH.c_str(), pDbHandler, SQLITE_OPEN_READWRITE);
58 prepareDb(pDbHandler, query.c_str(), pStmt);
60 int res = sqlite3_bind_int(pStmt.get(), 1, enabled);
61 TryReturn(res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_int : %d", res);
63 res = sqlite3_bind_text(pStmt.get(), 2, pkgId.c_str(), -1, SQLITE_TRANSIENT);
64 TryReturn(res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_int : %d", res);
66 res = sqlite3_bind_text(pStmt.get(), 3, privacyId.c_str(), -1, SQLITE_TRANSIENT);
67 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
69 res = sqlite3_step(pStmt.get());
70 TryReturn( res == SQLITE_DONE, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_step : %d", res);
78 PrivacyDb::getPrivacyAppPackages(std::list <std::string>& list) const
82 std::string query = "SELECT PKG_ID from PackageInfo";
84 openDb(PRIVACY_DB_PATH.c_str(), pDbHandler, SQLITE_OPEN_READONLY);
85 prepareDb(pDbHandler, query.c_str(), pStmt);
87 while ( sqlite3_step(pStmt.get()) == SQLITE_ROW )
89 const char* pValue = reinterpret_cast < const char* > (sqlite3_column_text(pStmt.get(), 0));
91 LOGD("PkgId found : %s ", pValue);
92 std::string pkgId = std::string(pValue);
94 if (isFilteredPackage(pkgId))
96 LOGD("%s is Filtered", pValue);
99 list.push_back(std::string(pValue));
108 PrivacyDb::getAppPackagePrivacyInfo(const std::string pkgId, std::list < std::pair < std::string, bool > >& privacyInfoList) const
112 static const std::string query = "SELECT PRIVACY_ID, IS_ENABLED from PrivacyInfo where PKG_ID=?";
114 openDb(PRIVACY_DB_PATH.c_str(), pDbHandler, SQLITE_OPEN_READONLY);
115 prepareDb(pDbHandler, query.c_str(), pStmt);
117 int res = sqlite3_bind_text(pStmt.get(), 1, pkgId.c_str(), -1, SQLITE_TRANSIENT);
118 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_int : %d", res);
120 while ( (res= sqlite3_step(pStmt.get())) == SQLITE_ROW )
122 const char* privacyId = reinterpret_cast < const char* > (sqlite3_column_text(pStmt.get(), 0));
123 bool privacyEnabled = sqlite3_column_int(pStmt.get(), 1) > 0 ? true : false;
125 privacyInfoList.push_back( std::pair <std::string, bool> (std::string(privacyId), privacyEnabled) );
127 LOGD("Privacy found : %s %d", privacyId, privacyEnabled);
137 PrivacyDb::addAppPackagePrivacyInfo(const std::string pkgId, const std::list < std::string > privilegeList)
141 static const std::string pkgInfoQuery("INSERT INTO PackageInfo(PKG_ID, IS_SET) VALUES(?, ?)");
142 static const std::string privacyQuery("INSERT INTO PrivacyInfo(PKG_ID, PRIVACY_ID, IS_ENABLED) VALUES(?, ?, ?)");
144 openDb(PRIVACY_DB_PATH.c_str(), pDbHandler, SQLITE_OPEN_READWRITE);
145 prepareDb(pDbHandler, pkgInfoQuery.c_str(), pPkgInfoStmt);
147 int res = sqlite3_bind_text(pPkgInfoStmt.get(), 1, pkgId.c_str(), -1, SQLITE_TRANSIENT);
148 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
150 res = sqlite3_bind_int(pPkgInfoStmt.get(), 2, 0);
151 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_int : %d", res);
153 res = sqlite3_step(pPkgInfoStmt.get());
154 TryReturn( res == SQLITE_DONE, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_step : %d", res);
156 for ( std::list <std::string>::const_iterator iter = privilegeList.begin(); iter != privilegeList.end(); ++iter)
158 LOGD(" install privacy: %s", iter->c_str());
159 prepareDb(pDbHandler, privacyQuery.c_str(), pPrivacyStmt);
161 res = sqlite3_bind_text(pPrivacyStmt.get(), 1, pkgId.c_str(), -1, SQLITE_TRANSIENT);
162 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_int : %d", res);
164 res = sqlite3_bind_text(pPrivacyStmt.get(), 2, iter->c_str(), -1, SQLITE_TRANSIENT);
165 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
167 // Before setting app and popup is ready, default value is true
168 res = sqlite3_bind_int(pPrivacyStmt.get(), 3, 1);
169 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_int : %d", res);
171 res = sqlite3_step(pPrivacyStmt.get());
172 TryReturn( res == SQLITE_DONE || res == SQLITE_CONSTRAINT, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_step : %d", res);
174 sqlite3_reset(pPrivacyStmt.get());
181 PrivacyDb::removeAppPackagePrivacyInfo(const std::string pkgId)
185 static const std::string pkgInfoQuery("DELETE FROM PackageInfo WHERE PKG_ID=?");
186 static const std::string privacyQuery("DELETE FROM PrivacyInfo WHERE PKG_ID=?");
190 openDb(PRIVACY_DB_PATH.c_str(), pDbHandler, SQLITE_OPEN_READWRITE);
191 prepareDb(pDbHandler, pkgInfoQuery.c_str(), pPkgInfoStmt);
193 res = sqlite3_bind_text(pPkgInfoStmt.get(), 1, pkgId.c_str(), -1, SQLITE_TRANSIENT);
194 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
196 res = sqlite3_step(pPkgInfoStmt.get());
197 TryReturn( res == SQLITE_DONE, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_step : %d", res);
199 prepareDb(pDbHandler, privacyQuery.c_str(), pPrivacyStmt);
201 res = sqlite3_bind_text(pPrivacyStmt.get(), 1, pkgId.c_str(), -1, SQLITE_TRANSIENT);
202 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
204 res = sqlite3_step(pPrivacyStmt.get());
205 TryReturn( res == SQLITE_DONE, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_step : %d", res);
211 PrivacyDb::isUserPrompted(const std::string pkgId, bool& isPrompted) const
215 static const std::string query = "SELECT IS_SET from PackageInfo where PKG_ID=?";
219 if (isFilteredPackage(pkgId))
221 LOGD("%s is Filtered", pkgId.c_str());
225 openDb(PRIVACY_DB_PATH.c_str(), pDbHandler, SQLITE_OPEN_READONLY);
226 prepareDb(pDbHandler, query.c_str(), pStmt);
228 int res = sqlite3_bind_text(pStmt.get(), 1, pkgId.c_str(), -1, SQLITE_TRANSIENT);
229 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
231 std::unique_ptr < std::list < std::pair < std:: string, bool > > > pList ( new std::list < std::pair < std:: string, bool > >);
233 if ((res = sqlite3_step(pStmt.get())) == SQLITE_ROW)
235 isPrompted = sqlite3_column_int(pStmt.get(), 0) > 0 ? true : false;
239 LOGE("The package[%s] can not access privacy", pkgId.c_str());
240 return PRIV_MGR_ERROR_SUCCESS;
247 PrivacyDb::setUserPrompted(const std::string pkgId, bool prompted)
251 std::string query = std::string("UPDATE PackageInfo set IS_SET =? where PKG_ID=?");
255 openDb(PRIVACY_DB_PATH.c_str(), pDbHandler, SQLITE_OPEN_READWRITE);
256 prepareDb(pDbHandler, query.c_str(), pStmt);
258 res = sqlite3_bind_int(pStmt.get(), 1, prompted? 1 : 0);
259 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_int : %d", res);
261 res = sqlite3_bind_text(pStmt.get(), 2, pkgId.c_str(), -1, SQLITE_TRANSIENT);
262 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
264 res = sqlite3_step(pStmt.get());
265 TryReturn( res == SQLITE_DONE, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_step : %d", res);
273 PrivacyDb::getAppPackagesbyPrivacyId(std::string privacyId, std::list < std::pair < std::string, bool > >& list) const
277 std::string sql = std::string("SELECT PKG_ID, IS_ENABLED from PrivacyInfo where PRIVACY_ID=?");
279 openDb(PRIVACY_DB_PATH.c_str(), pDbHandler, SQLITE_OPEN_READWRITE);
280 prepareDb(pDbHandler, sql.c_str(), pStmt);
282 LOGD("privacy id : %s", privacyId.c_str());
283 int res = sqlite3_bind_text(pStmt.get(), 1, privacyId.c_str(), -1, SQLITE_TRANSIENT);
284 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
286 while ( sqlite3_step(pStmt.get()) == SQLITE_ROW )
289 const char* pPkgId = reinterpret_cast < const char* > (sqlite3_column_text(pStmt.get(), 0));
290 bool isEnabled = sqlite3_column_int(pStmt.get(), 1) > 0 ? true : false;
291 std::string pkgId = std::string(pPkgId);
292 if (isFilteredPackage(pkgId))
294 LOGD("%s is Filtered", pPkgId);
298 list.push_back( std::pair <std::string, bool >(pkgId, isEnabled) );
303 return PRIV_MGR_ERROR_SUCCESS;
306 PrivacyDb::PrivacyDb(void)
311 PrivacyDb::~PrivacyDb(void)
317 PrivacyDb::getInstance(void)
320 std::lock_guard < std::mutex > guard(m_singletonMutex);
322 if (m_pInstance == NULL)
324 m_pInstance = new PrivacyDb();