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 <system_info.h>
21 #include "PrivacyIdInfo.h"
22 #include "privacy_guard_client_types.h"
23 #include "PrivacyGuardTypes.h"
26 std::map< std::string, std::string > PrivacyIdInfo::m_privilegeToPrivacyMap;
27 bool PrivacyIdInfo:: m_isInitialized;
30 PrivacyIdInfo::initialize(void)
32 static const std::string sqlPrivilege("SELECT PRIVILEGE_ID, PRIVACY_ID from PrivilegeToPrivacyTable");
33 static const std::string sqlPrivacyInfo("SELECT FEATURE FROM PrivacyInfo where PRIVACY_ID=?");
35 openDb(PRIVACY_INFO_DB_PATH, pDbHandler, SQLITE_OPEN_READONLY);
36 prepareDb(pDbHandler, sqlPrivilege.c_str(), pStmtPrivilege);
39 while ((res = sqlite3_step(pStmtPrivilege.get())) == SQLITE_ROW)
41 const char* privilegeId = reinterpret_cast < const char* > (sqlite3_column_text(pStmtPrivilege.get(), 0));
42 const char* privacyId = reinterpret_cast < const char* > (sqlite3_column_text(pStmtPrivilege.get(), 1));
44 prepareDb(pDbHandler, sqlPrivacyInfo.c_str(), pStmtPrivacyInfo);
45 res = sqlite3_bind_text(pStmtPrivacyInfo.get(), 1, privacyId, -1, SQLITE_TRANSIENT);
46 TryReturn(res == SQLITE_OK, PRIV_FLTR_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
47 res = sqlite3_step(pStmtPrivacyInfo.get());
48 LOGD("privacy id : %s", privacyId);
49 TryReturn(res == SQLITE_DONE || res == SQLITE_ROW, PRIV_FLTR_ERROR_DB_ERROR, , "sqlite3_step : %d", res);
51 const char* feature = reinterpret_cast < const char* > (sqlite3_column_text(pStmtPrivacyInfo.get(), 0));
54 bool isSupported = false;
56 res = isFeatureEnabled(feature, isSupported);
57 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "isFeatureEnabled : %d", res);
65 m_privilegeToPrivacyMap.insert(std::map< std::string, std::string >::value_type(std::string(privilegeId), std::string(privacyId)));
68 m_isInitialized = true;
70 return PRIV_FLTR_ERROR_SUCCESS;
74 PrivacyIdInfo::getPrivacyIdFromPrivilege(const std::string privilege, std::string& privacyId)
81 std::map< std::string, std::string >::iterator iter = m_privilegeToPrivacyMap.find(privilege);
82 if (iter == m_privilegeToPrivacyMap.end())
84 return PRIV_FLTR_ERROR_NO_DATA;
86 privacyId = iter->second;
88 return PRIV_FLTR_ERROR_SUCCESS;
92 PrivacyIdInfo::getPrivilegeListFromPrivacyId(const std::string privacyId, std::list< std::string >& privilegeList)
99 privilegeList.clear();
100 for (std::map< std::string, std::string >::iterator iter = m_privilegeToPrivacyMap.begin(); iter != m_privilegeToPrivacyMap.end(); ++iter)
102 if (privacyId.compare((iter->second)) == 0)
104 privilegeList.push_back(iter->first);
108 if (privilegeList.size() == 0)
110 LOGE("PrivilegeList of %s privacy is empty!", privacyId.c_str());
111 return PRIV_FLTR_ERROR_NO_DATA;
114 return PRIV_FLTR_ERROR_SUCCESS;
118 PrivacyIdInfo::getPrivacyIdListFromPrivilegeList(const std::list< std::string > privilegeList, std::list< std::string >& privacyIdList)
120 if (!m_isInitialized)
125 privacyIdList.clear();
127 std::set< std::string > privacyIdSet;
129 for (std::list< std::string >::const_iterator iter = privilegeList.begin(); iter != privilegeList.end(); ++iter)
131 std::string privacyId;
132 int res = getPrivacyIdFromPrivilege(*iter, privacyId);
133 if (res == PRIV_FLTR_ERROR_SUCCESS)
135 privacyIdSet.insert(privacyId);
139 for (std::set< std::string >::iterator iter = privacyIdSet.begin(); iter != privacyIdSet.end(); ++iter)
141 privacyIdList.push_back(*iter);
144 return PRIV_FLTR_ERROR_SUCCESS;
148 PrivacyIdInfo::isValidPrivacyId(const std::string privacyId)
150 if (!m_isInitialized) {
154 for (std::map< std::string, std::string >::iterator iter = m_privilegeToPrivacyMap.begin(); iter != m_privilegeToPrivacyMap.end(); ++iter) {
155 if (privacyId.compare((iter->second)) == 0) {
164 PrivacyIdInfo::getAllPrivacyId(std::list< std::string >& privacyIdList)
166 static const std::string sql("SELECT PRIVACY_ID, FEATURE from PrivacyInfo");
168 if (!m_isInitialized)
173 openDb(PRIVACY_INFO_DB_PATH, pDbHandler, SQLITE_OPEN_READONLY);
174 prepareDb(pDbHandler, sql.c_str(), pStmt);
177 while ((res = sqlite3_step(pStmt.get())) == SQLITE_ROW)
179 const char* privacyId = reinterpret_cast < const char* > (sqlite3_column_text(pStmt.get(), 0));
180 const char* feature = reinterpret_cast < const char* > (sqlite3_column_text(pStmt.get(), 1));
181 LOGD("privacy: %s, feature: %s", privacyId, feature);
185 bool isSupported = false;
186 res = isFeatureEnabled(feature, isSupported);
187 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, res, , "isFeatureEnabled : %d", res);
194 privacyIdList.push_back(std::string(privacyId));
195 SECURE_LOGD(" privacy Id : %s", privacyId);
198 return PRIV_FLTR_ERROR_SUCCESS;
202 PrivacyIdInfo::getPrivaycDisplayName(const std::string privacyId, std::string& displayName)
204 if (!m_isInitialized)
209 std::string sql = std::string("SELECT STR_MODULE_ID, STR_NAME_ID from PrivacyInfo where PRIVACY_ID=?");
211 openDb(PRIVACY_INFO_DB_PATH, pDbHandler, SQLITE_OPEN_READONLY);
212 prepareDb(pDbHandler, sql.c_str(), pStmt);
214 int res = sqlite3_bind_text(pStmt.get(), 1, privacyId.c_str(), -1, SQLITE_TRANSIENT);
215 TryReturn(res == SQLITE_OK, PRIV_FLTR_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
217 if (sqlite3_step(pStmt.get()) == SQLITE_ROW)
219 const char* pModuleId = reinterpret_cast < const char* > (sqlite3_column_text(pStmt.get(), 0));
220 const char* pNameId = reinterpret_cast < const char* > (sqlite3_column_text(pStmt.get(), 1));
224 displayName = privacyId;
228 displayName = std::string(dgettext(pModuleId, pNameId));
233 LOGI("Cannot find privacy string %s ", privacyId.c_str());
234 return PRIV_FLTR_ERROR_NO_DATA;
237 return PRIV_FLTR_ERROR_SUCCESS;
241 PrivacyIdInfo::getPrivaycDescription(const std::string privacyId, std::string& displayName)
243 if (!m_isInitialized)
248 std::string sql = std::string("SELECT STR_MODULE_ID, STR_NAME_ID from PrivacyInfo where PRIVACY_ID=?");
250 openDb(PRIVACY_INFO_DB_PATH, pDbHandler, SQLITE_OPEN_READONLY);
251 prepareDb(pDbHandler, sql.c_str(), pStmt);
253 int res = sqlite3_bind_text(pStmt.get(), 1, privacyId.c_str(), -1, SQLITE_TRANSIENT);
254 TryReturn(res == SQLITE_OK, PRIV_FLTR_ERROR_DB_ERROR, , "sqlite3_bind_text : %d", res);
256 if (sqlite3_step(pStmt.get()) == SQLITE_ROW)
258 const char* pModuleId = reinterpret_cast < const char* > (sqlite3_column_text(pStmt.get(), 0));
259 const char* pNameId = reinterpret_cast < const char* > (sqlite3_column_text(pStmt.get(), 1));
261 displayName = std::string(dgettext(pModuleId, pNameId));
265 LOGI("Cannot find privacy string %s ", privacyId.c_str());
266 return PRIV_FLTR_ERROR_NO_DATA;
269 return PRIV_FLTR_ERROR_SUCCESS;
273 PrivacyIdInfo::isFeatureEnabled(const char* feature, bool& enabled)
275 int res = PRIV_FLTR_ERROR_SUCCESS;
283 res = system_info_get_platform_bool(feature, &enabled);
284 TryReturn(res == PRIV_FLTR_ERROR_SUCCESS, PRIV_FLTR_ERROR_SYSTEM_ERROR, , "system_info_get_platform_bool : %d", res);
286 return PRIV_FLTR_ERROR_SUCCESS;