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 {
41 #define SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN Try
43 #define SQL_CONNECTION_EXCEPTION_HANDLER_END(message) \
44 Catch(DPL::DB::SqlConnection::Exception::Base) { \
46 ReThrowMsg(SecurityOriginDAO::Exception::DatabaseError, \
51 DPL::DB::SqlConnection::Flag::Option SECURITY_ORIGIN_DB_OPTION =
52 DPL::DB::SqlConnection::Flag::RW;
53 DPL::DB::SqlConnection::Flag::Type SECURITY_ORIGIN_DB_TYPE =
54 DPL::DB::SqlConnection::Flag::UseLucene;
55 const char* const SECURITY_ORIGIN_DB_NAME = ".security_origin.db";
56 const char* const SECURITY_ORIGIN_DB_SQL_PATH =
57 "/usr/share/wrt-engine/security_origin_db.sql";
59 std::string createDatabasePath(const WrtDB::WidgetPkgName &pkgName)
61 std::stringstream filename;
63 filename << WrtDB::WidgetConfig::GetWidgetPersistentStoragePath(pkgName)
65 << SECURITY_ORIGIN_DB_NAME;
66 return filename.str();
69 std::string createDatabasePath(int widgetHandle)
71 using namespace DPL::DB::ORM;
72 using namespace WrtDB::WidgetConfig;
73 using namespace WrtDB::GlobalConfig;
75 WrtDB::WidgetPkgName pkgname;
79 pkgname = WrtDB::WidgetDAOReadOnly::getPkgName(widgetHandle);
81 Catch(DPL::DB::SqlConnection::Exception::Base) {
82 LogError("Failed to get database Path");
84 return createDatabasePath(pkgname);
87 void checkDatabase(std::string databasePath)
89 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
91 if (databasePath.empty()) {
92 ThrowMsg(SecurityOriginDAO::Exception::DatabaseError,
93 "Wrong database Path is passed");
97 if (stat(databasePath.c_str(), &buffer) != 0) {
99 //Create fresh database
100 LogInfo("Creating database " << databasePath);
103 file.open(SECURITY_ORIGIN_DB_SQL_PATH, std::ios_base::in);
105 ThrowMsg(SecurityOriginDAO::Exception::DatabaseError,
106 "Fail to get database Path");
109 std::stringstream ssBuffer;
110 ssBuffer << file.rdbuf();
114 DPL::DB::SqlConnection con(databasePath,
115 SECURITY_ORIGIN_DB_TYPE,
116 SECURITY_ORIGIN_DB_OPTION);
117 con.ExecCommand(ssBuffer.str().c_str());
120 SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to get database Path")
124 SecurityOriginDAO::SecurityOriginDAO(int handle) :
125 m_securityOriginDBPath(createDatabasePath(handle)),
126 m_securityOriginDBInterface(m_securityOriginDBPath, SECURITY_ORIGIN_DB_TYPE)
128 checkDatabase(m_securityOriginDBPath);
129 m_securityOriginDBInterface.AttachToThread(SECURITY_ORIGIN_DB_OPTION);
132 SecurityOriginDAO::SecurityOriginDAO(const WrtDB::WidgetPkgName &pkgName) :
133 m_securityOriginDBPath(createDatabasePath(pkgName)),
134 m_securityOriginDBInterface(m_securityOriginDBPath, SECURITY_ORIGIN_DB_TYPE)
136 checkDatabase(m_securityOriginDBPath);
137 m_securityOriginDBInterface.AttachToThread(SECURITY_ORIGIN_DB_OPTION);
140 SecurityOriginDAO::~SecurityOriginDAO()
142 m_securityOriginDBInterface.DetachFromThread();
145 SecurityOriginDataList SecurityOriginDAO::getSecurityOriginDataList(void)
147 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
149 SecurityOriginDataList list;
150 SECURITY_ORIGIN_DB_SELECT(select, SecurityOriginInfo, &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(const SecurityOriginData &securityOriginData)
168 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
170 SECURITY_ORIGIN_DB_SELECT(select, SecurityOriginInfo, &m_securityOriginDBInterface);
172 And(And(And(Equals<SecurityOriginInfo::feature>(securityOriginData.feature),
173 Equals<SecurityOriginInfo::scheme>(securityOriginData.origin.scheme)),
174 Equals<SecurityOriginInfo::host>(securityOriginData.origin.host)),
175 Equals<SecurityOriginInfo::port>(securityOriginData.origin.port)));
176 SecurityOriginInfo::Select::RowList rows = select->GetRowList();
179 return RESULT_UNKNOWN;
181 SecurityOriginInfo::Row row = rows.front();
182 return static_cast<Result>(row.Get_result());
184 SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to get result for security origin")
187 void SecurityOriginDAO::setSecurityOriginData(const SecurityOriginData &securityOriginData,
190 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
192 ScopedTransaction transaction(&m_securityOriginDBInterface);
193 SecurityOriginInfo::Row row;
194 row.Set_feature(securityOriginData.feature);
195 row.Set_scheme(securityOriginData.origin.scheme);
196 row.Set_host(securityOriginData.origin.host);
197 row.Set_port(securityOriginData.origin.port);
198 row.Set_result(result);
200 if (true == hasResult(securityOriginData)) {
201 SECURITY_ORIGIN_DB_UPDATE(update,
203 &m_securityOriginDBInterface);
207 SECURITY_ORIGIN_DB_INSERT(
210 &m_securityOriginDBInterface);
214 transaction.Commit();
216 SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to set security origin data")
219 void SecurityOriginDAO::removeSecurityOriginData(
220 const SecurityOriginData &securityOriginData)
222 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
224 ScopedTransaction transaction(&m_securityOriginDBInterface);
226 if (true == hasResult(securityOriginData)) {
227 SECURITY_ORIGIN_DB_DELETE(del, SecurityOriginInfo, &m_securityOriginDBInterface)
229 And(And(And(Equals<SecurityOriginInfo::feature>(securityOriginData.feature),
230 Equals<SecurityOriginInfo::scheme>(securityOriginData.origin.scheme)),
231 Equals<SecurityOriginInfo::host>(securityOriginData.origin.host)),
232 Equals<SecurityOriginInfo::port>(securityOriginData.origin.port)));
234 transaction.Commit();
237 SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to set security origin data")
240 void SecurityOriginDAO::removeSecurityOriginData(const Result result)
242 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
244 ScopedTransaction transaction(&m_securityOriginDBInterface);
245 SECURITY_ORIGIN_DB_DELETE(del, SecurityOriginInfo, &m_securityOriginDBInterface)
246 del->Where(Equals<SecurityOriginInfo::result>(result));
248 transaction.Commit();
250 SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to remove data by result")
253 bool SecurityOriginDAO::hasResult(const SecurityOriginData &securityOriginData)
255 Result res=getResult(securityOriginData);
256 return (res != RESULT_UNKNOWN);
259 #undef SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
260 #undef SQL_CONNECTION_EXCEPTION_HANDLER_END
262 } // namespace SecurityOriginDB