2 * Copyright (c) 2011 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.
17 * @file security_origin_dao.cpp
18 * @author Jihoon Chung (jihoon.chung@samsung.com)
20 * @brief This file contains the definition of security origin dao class.
23 #include <wrt-commons/security-origin-dao/security_origin_database.h>
24 #include <wrt-commons/security-origin-dao/security_origin_dao.h>
25 #include <wrt-commons/security-origin-dao/security_origin_dao_types.h>
26 #include <orm_generator_security_origin.h>
27 #include <dpl/foreach.h>
28 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
29 #include <dpl/wrt-dao-ro/WrtDatabase.h>
30 #include <dpl/wrt-dao-ro/widget_config.h>
31 #include <dpl/wrt-dao-ro/global_config.h>
32 #include <dpl/wrt-dao-ro/common_dao_types.h>
36 using namespace DPL::DB::ORM;
37 using namespace DPL::DB::ORM::security_origin;
39 namespace SecurityOriginDB {
40 #define SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN Try
42 #define SQL_CONNECTION_EXCEPTION_HANDLER_END(message) \
43 Catch(DPL::DB::SqlConnection::Exception::Base) { \
45 ReThrowMsg(SecurityOriginDAO::Exception::DatabaseError, \
50 DPL::DB::SqlConnection::Flag::Option SECURITY_ORIGIN_DB_OPTION =
51 DPL::DB::SqlConnection::Flag::RW;
52 DPL::DB::SqlConnection::Flag::Type SECURITY_ORIGIN_DB_TYPE =
53 DPL::DB::SqlConnection::Flag::UseLucene;
54 const char* const SECURITY_ORIGIN_DB_NAME = ".security_origin.db";
55 const char* const SECURITY_ORIGIN_DB_SQL_PATH =
56 "/usr/share/wrt-engine/security_origin_db.sql";
58 std::string createDatabasePath(const WrtDB::WidgetPkgName &pkgName)
60 std::stringstream filename;
62 filename << WrtDB::WidgetConfig::GetWidgetPersistentStoragePath(pkgName)
64 << SECURITY_ORIGIN_DB_NAME;
65 return filename.str();
68 std::string createDatabasePath(int widgetHandle)
70 using namespace DPL::DB::ORM;
71 using namespace WrtDB::WidgetConfig;
72 using namespace WrtDB::GlobalConfig;
74 WrtDB::TizenAppId appid;
78 appid = WrtDB::WidgetDAOReadOnly::getTzAppId(widgetHandle);
80 Catch(DPL::DB::SqlConnection::Exception::Base) {
81 LogError("Failed to get database Path");
83 return createDatabasePath(appid);
86 void checkDatabase(std::string databasePath)
88 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
90 if (databasePath.empty()) {
91 ThrowMsg(SecurityOriginDAO::Exception::DatabaseError,
92 "Wrong database Path is passed");
96 if (stat(databasePath.c_str(), &buffer) != 0) {
97 //Create fresh database
98 LogInfo("Creating database " << databasePath);
101 file.open(SECURITY_ORIGIN_DB_SQL_PATH, std::ios_base::in);
103 ThrowMsg(SecurityOriginDAO::Exception::DatabaseError,
104 "Fail to get database Path");
107 std::stringstream ssBuffer;
108 ssBuffer << file.rdbuf();
112 DPL::DB::SqlConnection con(databasePath,
113 SECURITY_ORIGIN_DB_TYPE,
114 SECURITY_ORIGIN_DB_OPTION);
115 con.ExecCommand(ssBuffer.str().c_str());
118 SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to get database Path")
122 SecurityOriginDAO::SecurityOriginDAO(int handle) :
123 m_securityOriginDBPath(createDatabasePath(handle)),
124 m_securityOriginDBInterface(m_securityOriginDBPath, SECURITY_ORIGIN_DB_TYPE)
126 checkDatabase(m_securityOriginDBPath);
127 m_securityOriginDBInterface.AttachToThread(SECURITY_ORIGIN_DB_OPTION);
130 SecurityOriginDAO::SecurityOriginDAO(const WrtDB::WidgetPkgName &pkgName) :
131 m_securityOriginDBPath(createDatabasePath(pkgName)),
132 m_securityOriginDBInterface(m_securityOriginDBPath, SECURITY_ORIGIN_DB_TYPE)
134 checkDatabase(m_securityOriginDBPath);
135 m_securityOriginDBInterface.AttachToThread(SECURITY_ORIGIN_DB_OPTION);
138 SecurityOriginDAO::~SecurityOriginDAO()
140 m_securityOriginDBInterface.DetachFromThread();
143 SecurityOriginDataList SecurityOriginDAO::getSecurityOriginDataList(void)
145 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
147 SecurityOriginDataList list;
148 SECURITY_ORIGIN_DB_SELECT(select,
150 &m_securityOriginDBInterface);
151 typedef std::list<SecurityOriginInfo::Row> RowList;
152 RowList rowList = select->GetRowList();
154 FOREACH(it, rowList) {
155 Origin origin(it->Get_scheme(), it->Get_host(), it->Get_port());
157 SecurityOriginDataPtr(
158 new SecurityOriginData(
159 static_cast<Feature>(it->Get_feature()), origin)));
163 SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to get data list")
166 Result SecurityOriginDAO::getResult(
167 const SecurityOriginData &securityOriginData)
169 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
171 SECURITY_ORIGIN_DB_SELECT(select,
173 &m_securityOriginDBInterface);
175 And(And(And(Equals<SecurityOriginInfo::feature>(securityOriginData.
177 Equals<SecurityOriginInfo::scheme>(securityOriginData.
179 Equals<SecurityOriginInfo::host>(securityOriginData.origin.
181 Equals<SecurityOriginInfo::port>(securityOriginData.origin.port)));
182 SecurityOriginInfo::Select::RowList rows = select->GetRowList();
185 return RESULT_UNKNOWN;
187 SecurityOriginInfo::Row row = rows.front();
188 return static_cast<Result>(row.Get_result());
190 SQL_CONNECTION_EXCEPTION_HANDLER_END(
191 "Failed to get result for security origin")
194 void SecurityOriginDAO::setSecurityOriginData(
195 const SecurityOriginData &securityOriginData,
198 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
200 ScopedTransaction transaction(&m_securityOriginDBInterface);
201 SecurityOriginInfo::Row row;
202 row.Set_feature(securityOriginData.feature);
203 row.Set_scheme(securityOriginData.origin.scheme);
204 row.Set_host(securityOriginData.origin.host);
205 row.Set_port(securityOriginData.origin.port);
206 row.Set_result(result);
208 if (true == hasResult(securityOriginData)) {
209 SECURITY_ORIGIN_DB_UPDATE(update,
211 &m_securityOriginDBInterface);
215 SECURITY_ORIGIN_DB_INSERT(
218 &m_securityOriginDBInterface);
222 transaction.Commit();
224 SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to set security origin data")
227 void SecurityOriginDAO::removeSecurityOriginData(
228 const SecurityOriginData &securityOriginData)
230 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
232 ScopedTransaction transaction(&m_securityOriginDBInterface);
234 if (true == hasResult(securityOriginData)) {
235 SECURITY_ORIGIN_DB_DELETE(del,
237 &m_securityOriginDBInterface)
239 And(And(And(Equals<SecurityOriginInfo::feature>(
240 securityOriginData.feature),
241 Equals<SecurityOriginInfo::scheme>(
242 securityOriginData.origin.scheme)),
243 Equals<SecurityOriginInfo::host>(securityOriginData.
245 Equals<SecurityOriginInfo::port>(securityOriginData.origin.
248 transaction.Commit();
251 SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to set security origin data")
254 void SecurityOriginDAO::removeSecurityOriginData(const Result result)
256 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
258 ScopedTransaction transaction(&m_securityOriginDBInterface);
259 SECURITY_ORIGIN_DB_DELETE(del,
261 &m_securityOriginDBInterface)
262 del->Where(Equals<SecurityOriginInfo::result>(result));
264 transaction.Commit();
266 SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to remove data by result")
269 bool SecurityOriginDAO::hasResult(const SecurityOriginData &securityOriginData)
271 Result res = getResult(securityOriginData);
272 return (res != RESULT_UNKNOWN);
275 #undef SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
276 #undef SQL_CONNECTION_EXCEPTION_HANDLER_END
277 } // namespace SecurityOriginDB