2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
18 #include <PrivacyManagerServer.h>
\r
21 #include <PrivacyManagerTypes.h>
\r
22 #include <sqlite3.h>
\r
24 std::mutex PrivacyManagerServer::m_singletonMutex;
\r
25 PrivacyManagerServer* PrivacyManagerServer::m_pInstance = NULL;
\r
28 PrivacyManagerServer::createDB(void)
\r
34 PrivacyManagerServer::setPrivacySetting(const std::string pkgId, const std::string privacyId, bool enabled)
\r
38 static const std::string query = std::string("UPDATE PrivacyInfo set IS_ENABLED =? where PKG_ID=? and PRIVACY_ID=?");
\r
40 openDb(PRIVACY_DB_PATH.c_str(), pDbHandler, SQLITE_OPEN_READWRITE);
\r
41 prepareDb(pDbHandler, query.c_str(), pStmt);
\r
43 int res = sqlite3_bind_int(pStmt.get(), 1, enabled);
\r
44 TryReturn(res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_int : %d", res);
\r
46 res = sqlite3_bind_text(pStmt.get(), 2, pkgId.c_str(), -1, SQLITE_TRANSIENT);
\r
47 TryReturn(res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_int : %d", res);
\r
49 res = sqlite3_bind_text(pStmt.get(), 3, privacyId.c_str(), -1, SQLITE_TRANSIENT);
\r
50 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
\r
52 res = sqlite3_step(pStmt.get());
\r
53 TryReturn( res == SQLITE_DONE, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_step : %d", res);
\r
55 res = m_notificationServer.notify(pkgId, privacyId);
\r
56 TryReturn( res == 0, res, , "m_notificationServer.notify : %d", res);
\r
64 PrivacyManagerServer::getPrivacyAppPackages(std::list <std::string>& list)
\r
68 std::string query = "SELECT PKG_ID from PackageInfo";
\r
70 openDb(PRIVACY_DB_PATH.c_str(), pDbHandler, SQLITE_OPEN_READWRITE);
\r
71 prepareDb(pDbHandler, query.c_str(), pStmt);
\r
73 while ( sqlite3_step(pStmt.get()) == SQLITE_ROW )
\r
75 const char* pValue = reinterpret_cast < const char* > (sqlite3_column_text(pStmt.get(), 0));
\r
77 LOGD("AppId found : %s ", pValue);
\r
78 list.push_back(std::string(pValue));
\r
87 PrivacyManagerServer::getAppPackagePrivacyInfo(const std::string pkgId, std::list < std::pair < std::string, bool > >& privacyInfoList)
\r
91 static const std::string query = "SELECT PRIVACY_ID, IS_ENABLED from PrivacyInfo where PKG_ID=?";
\r
93 openDb(PRIVACY_DB_PATH.c_str(), pDbHandler, SQLITE_OPEN_READWRITE);
\r
94 prepareDb(pDbHandler, query.c_str(), pStmt);
\r
96 int res = sqlite3_bind_text(pStmt.get(), 1, pkgId.c_str(), -1, SQLITE_TRANSIENT);
\r
97 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_int : %d", res);
\r
101 while ( (res= sqlite3_step(pStmt.get())) == SQLITE_ROW )
\r
103 const char* privacyId = reinterpret_cast < const char* > (sqlite3_column_text(pStmt.get(), 0));
\r
104 bool privacyEnabled = sqlite3_column_int(pStmt.get(), 1) > 0 ? true : false;
\r
106 privacyInfoList.push_back( std::pair <std::string, bool> (std::string(privacyId), privacyEnabled) );
\r
108 LOGD("Privacy found : %s %d", privacyId, privacyEnabled);
\r
118 PrivacyManagerServer::addAppPackagePrivacyInfo(const std::string pkgId, const std::list < std::string > privilegeList)
\r
122 static const std::string pkgInfoQuery("INSERT INTO PackageInfo(PKG_ID, IS_SET) VALUES(?, ?)");
\r
123 static const std::string privacyQuery("INSERT INTO PrivacyInfo(PKG_ID, PRIVACY_ID, IS_ENABLED) VALUES(?, ?, ?)");
\r
125 openDb(PRIVACY_DB_PATH.c_str(), pDbHandler, SQLITE_OPEN_READWRITE);
\r
126 prepareDb(pDbHandler, pkgInfoQuery.c_str(), pPkgInfoStmt);
\r
128 int res = sqlite3_bind_text(pPkgInfoStmt.get(), 1, pkgId.c_str(), -1, SQLITE_TRANSIENT);
\r
129 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
\r
131 res = sqlite3_bind_int(pPkgInfoStmt.get(), 2, 0);
\r
132 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_int : %d", res);
\r
134 res = sqlite3_step(pPkgInfoStmt.get());
\r
135 TryReturn( res == SQLITE_DONE || res == SQLITE_CONSTRAINT, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_step : %d", res);
\r
137 for ( std::list <std::string>::const_iterator iter = privilegeList.begin(); iter != privilegeList.end(); ++iter)
\r
139 LOGD(" install privacy: %s", iter->c_str());
\r
140 prepareDb(pDbHandler, privacyQuery.c_str(), pPrivacyStmt);
\r
142 res = sqlite3_bind_text(pPrivacyStmt.get(), 1, pkgId.c_str(), -1, SQLITE_TRANSIENT);
\r
143 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_int : %d", res);
\r
145 res = sqlite3_bind_text(pPrivacyStmt.get(), 2, iter->c_str(), -1, SQLITE_TRANSIENT);
\r
146 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
\r
148 // Before setting app and popup is ready, default value is true
\r
149 res = sqlite3_bind_int(pPrivacyStmt.get(), 3, 1);
\r
150 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_int : %d", res);
\r
152 res = sqlite3_step(pPrivacyStmt.get());
\r
153 TryReturn( res == SQLITE_DONE || res == SQLITE_CONSTRAINT, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_step : %d", res);
\r
155 sqlite3_reset(pPrivacyStmt.get());
\r
162 PrivacyManagerServer::removeAppPackagePrivacyInfo(const std::string pkgId)
\r
166 static const std::string pkgInfoQuery("DELETE FROM PackageInfo WHERE PKG_ID=?");
\r
167 static const std::string privacyQuery("DELETE FROM PrivacyInfo WHERE PKG_ID=?");
\r
171 openDb(PRIVACY_DB_PATH.c_str(), pDbHandler, SQLITE_OPEN_READWRITE);
\r
172 prepareDb(pDbHandler, pkgInfoQuery.c_str(), pPkgInfoStmt);
\r
174 res = sqlite3_bind_text(pPkgInfoStmt.get(), 1, pkgId.c_str(), -1, SQLITE_TRANSIENT);
\r
175 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
\r
177 res = sqlite3_step(pPkgInfoStmt.get());
\r
178 TryReturn( res == SQLITE_DONE, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_step : %d", res);
\r
180 prepareDb(pDbHandler, privacyQuery.c_str(), pPrivacyStmt);
\r
182 res = sqlite3_bind_text(pPrivacyStmt.get(), 1, pkgId.c_str(), -1, SQLITE_TRANSIENT);
\r
183 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
\r
185 res = sqlite3_step(pPrivacyStmt.get());
\r
186 TryReturn( res == SQLITE_DONE, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_step : %d", res);
\r
192 PrivacyManagerServer::isUserPrompted(const std::string pkgId, bool& isPrompted)
\r
196 static const std::string query = "SELECT IS_SET from PackageInfo where PKG_ID=?";
\r
198 openDb(PRIVACY_DB_PATH.c_str(), pDbHandler, SQLITE_OPEN_READWRITE);
\r
199 prepareDb(pDbHandler, query.c_str(), pStmt);
\r
201 int res = sqlite3_bind_text(pStmt.get(), 1, pkgId.c_str(), -1, SQLITE_TRANSIENT);
\r
202 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
\r
204 std::unique_ptr < std::list < std::pair < std:: string, bool > > > pList ( new std::list < std::pair < std:: string, bool > >);
\r
206 if ((res = sqlite3_step(pStmt.get())) == SQLITE_ROW)
\r
208 isPrompted = sqlite3_column_int(pStmt.get(), 0) > 0 ? true : false;
\r
212 LOGE("Fail to get data : %d", res);
\r
213 return PRIV_MGR_ERROR_DB_ERROR;
\r
222 PrivacyManagerServer::setUserPrompted(const std::string pkgId, bool prompted)
\r
224 LOGI("enter :%s - %d", pkgId.c_str(), prompted);
\r
226 std::string query = std::string("UPDATE PackageInfo set IS_SET =? where PKG_ID=?");
\r
230 openDb(PRIVACY_DB_PATH.c_str(), pDbHandler, SQLITE_OPEN_READWRITE);
\r
231 prepareDb(pDbHandler, query.c_str(), pStmt);
\r
233 res = sqlite3_bind_int(pStmt.get(), 1, prompted? 1 : 0);
\r
234 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_int : %d", res);
\r
236 res = sqlite3_bind_text(pStmt.get(), 2, pkgId.c_str(), -1, SQLITE_TRANSIENT);
\r
237 TryReturn( res == SQLITE_OK, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
\r
239 res = sqlite3_step(pStmt.get());
\r
240 TryReturn( res == SQLITE_DONE, PRIV_MGR_ERROR_DB_ERROR, , "sqlite3_step : %d", res);
\r
245 PrivacyManagerServer::PrivacyManagerServer(void)
\r
250 PrivacyManagerServer*
\r
251 PrivacyManagerServer::getInstance(void)
\r
254 std::lock_guard < std::mutex > guard(m_singletonMutex);
\r
256 if (m_pInstance == NULL)
\r
258 m_pInstance = new PrivacyManagerServer();
\r
260 m_pInstance->m_notificationServer.initialize();
\r
263 return m_pInstance;
\r