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>
35 using namespace DPL::DB::ORM;
36 using namespace DPL::DB::ORM::security_origin;
38 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(int widgetHandle)
60 using namespace DPL::DB::ORM;
61 using namespace WrtDB::WidgetConfig;
62 using namespace WrtDB::GlobalConfig;
64 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
66 std::stringstream filename;
67 WrtDB::WidgetDAOReadOnly widgetDAO(widgetHandle);
68 DPL::Optional<DPL::String> pkgname = widgetDAO.getPkgname();
70 filename << GetWidgetPersistentStoragePath(*pkgname)
72 << SECURITY_ORIGIN_DB_NAME;
73 return filename.str();
75 SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to get database Path")
79 void checkDatabase(std::string databasePath)
81 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
83 if (databasePath.empty()) {
84 ThrowMsg(SecurityOriginDAO::Exception::DatabaseError,
85 "Wrong database Path is passed");
89 if (stat(databasePath.c_str(), &buffer) != 0) {
91 //Create fresh database
92 LogInfo("Creating database " << databasePath);
95 file.open(SECURITY_ORIGIN_DB_SQL_PATH, std::ios_base::in);
97 ThrowMsg(SecurityOriginDAO::Exception::DatabaseError,
98 "Fail to get database Path");
101 std::stringstream ssBuffer;
102 ssBuffer << file.rdbuf();
106 DPL::DB::SqlConnection con(databasePath,
107 SECURITY_ORIGIN_DB_TYPE,
108 SECURITY_ORIGIN_DB_OPTION);
109 con.ExecCommand(ssBuffer.str().c_str());
112 SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to get database Path")
116 SecurityOriginDAO::SecurityOriginDAO(int handle) :
117 m_securityOriginDBPath(createDatabasePath(handle)),
118 m_securityOriginDBInterface(m_securityOriginDBPath, SECURITY_ORIGIN_DB_TYPE)
120 checkDatabase(m_securityOriginDBPath);
121 m_securityOriginDBInterface.AttachToThread(SECURITY_ORIGIN_DB_OPTION);
124 SecurityOriginDAO::~SecurityOriginDAO()
126 m_securityOriginDBInterface.DetachFromThread();
129 SecurityOriginDataList SecurityOriginDAO::getSecurityOriginDataList(void)
131 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
133 SecurityOriginDataList list;
134 SECURITY_ORIGIN_DB_SELECT(select, SecurityOriginInfo, &m_securityOriginDBInterface);
135 typedef std::list<SecurityOriginInfo::Row> RowList;
136 RowList rowList = select->GetRowList();
138 FOREACH(it, rowList) {
139 Origin origin(it->Get_scheme(), it->Get_host(), it->Get_port());
141 SecurityOriginDataPtr(
142 new SecurityOriginData(
143 static_cast<Feature>(it->Get_feature()), origin)));
147 SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to get data list")
150 Result SecurityOriginDAO::getResult(const SecurityOriginData &securityOriginData)
152 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
154 SECURITY_ORIGIN_DB_SELECT(select, SecurityOriginInfo, &m_securityOriginDBInterface);
156 And(And(And(Equals<SecurityOriginInfo::feature>(securityOriginData.feature),
157 Equals<SecurityOriginInfo::scheme>(securityOriginData.origin.scheme)),
158 Equals<SecurityOriginInfo::host>(securityOriginData.origin.host)),
159 Equals<SecurityOriginInfo::port>(securityOriginData.origin.port)));
160 SecurityOriginInfo::Select::RowList rows = select->GetRowList();
163 return RESULT_UNKNOWN;
165 SecurityOriginInfo::Row row = rows.front();
166 return static_cast<Result>(row.Get_result());
168 SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to get result for security origin")
171 void SecurityOriginDAO::setSecurityOriginData(const SecurityOriginData &securityOriginData,
174 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
176 ScopedTransaction transaction(&m_securityOriginDBInterface);
177 SecurityOriginInfo::Row row;
178 row.Set_feature(securityOriginData.feature);
179 row.Set_scheme(securityOriginData.origin.scheme);
180 row.Set_host(securityOriginData.origin.host);
181 row.Set_port(securityOriginData.origin.port);
182 row.Set_result(result);
184 if (true == hasResult(securityOriginData)) {
185 SECURITY_ORIGIN_DB_UPDATE(update,
187 &m_securityOriginDBInterface);
191 SECURITY_ORIGIN_DB_INSERT(
194 &m_securityOriginDBInterface);
198 transaction.Commit();
200 SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to set security origin data")
203 void SecurityOriginDAO::removeSecurityOriginData(
204 const SecurityOriginData &securityOriginData)
206 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
208 ScopedTransaction transaction(&m_securityOriginDBInterface);
210 if (true == hasResult(securityOriginData)) {
211 SECURITY_ORIGIN_DB_DELETE(del, SecurityOriginInfo, &m_securityOriginDBInterface)
213 And(And(And(Equals<SecurityOriginInfo::feature>(securityOriginData.feature),
214 Equals<SecurityOriginInfo::scheme>(securityOriginData.origin.scheme)),
215 Equals<SecurityOriginInfo::host>(securityOriginData.origin.host)),
216 Equals<SecurityOriginInfo::port>(securityOriginData.origin.port)));
218 transaction.Commit();
221 SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to set security origin data")
224 void SecurityOriginDAO::removeSecurityOriginData(const Result result)
226 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
228 ScopedTransaction transaction(&m_securityOriginDBInterface);
229 SECURITY_ORIGIN_DB_DELETE(del, SecurityOriginInfo, &m_securityOriginDBInterface)
230 del->Where(Equals<SecurityOriginInfo::result>(result));
232 transaction.Commit();
234 SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to remove data by result")
237 bool SecurityOriginDAO::hasResult(const SecurityOriginData &securityOriginData)
239 Result res=getResult(securityOriginData);
240 return (res != RESULT_UNKNOWN);
243 #undef SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
244 #undef SQL_CONNECTION_EXCEPTION_HANDLER_END
246 } // namespace SecurityOriginDB