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 std::string createDatabasePath(const DPL::String& tizenId)
81 using namespace DPL::DB::ORM;
82 using namespace WrtDB::WidgetConfig;
83 using namespace WrtDB::GlobalConfig;
85 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
87 std::stringstream filename;
88 filename << GetWidgetPersistentStoragePath(tizenId)
90 << SECURITY_ORIGIN_DB_NAME;
91 return filename.str();
93 SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to get database Path")
97 void checkDatabase(std::string databasePath)
99 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
101 if (databasePath.empty()) {
102 ThrowMsg(SecurityOriginDAO::Exception::DatabaseError,
103 "Wrong database Path is passed");
107 if (stat(databasePath.c_str(), &buffer) != 0) {
109 //Create fresh database
110 LogInfo("Creating database " << databasePath);
113 file.open(SECURITY_ORIGIN_DB_SQL_PATH, std::ios_base::in);
115 ThrowMsg(SecurityOriginDAO::Exception::DatabaseError,
116 "Fail to get database Path");
119 std::stringstream ssBuffer;
120 ssBuffer << file.rdbuf();
124 DPL::DB::SqlConnection con(databasePath,
125 SECURITY_ORIGIN_DB_TYPE,
126 SECURITY_ORIGIN_DB_OPTION);
127 con.ExecCommand(ssBuffer.str().c_str());
130 SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to get database Path")
134 SecurityOriginDAO::SecurityOriginDAO(int handle) :
135 m_securityOriginDBPath(createDatabasePath(handle)),
136 m_securityOriginDBInterface(m_securityOriginDBPath, SECURITY_ORIGIN_DB_TYPE)
138 checkDatabase(m_securityOriginDBPath);
139 m_securityOriginDBInterface.AttachToThread(SECURITY_ORIGIN_DB_OPTION);
142 SecurityOriginDAO::SecurityOriginDAO(const DPL::String& tizenId):
143 m_securityOriginDBPath(createDatabasePath(tizenId)),
144 m_securityOriginDBInterface(m_securityOriginDBPath, SECURITY_ORIGIN_DB_TYPE)
146 checkDatabase(m_securityOriginDBPath);
147 m_securityOriginDBInterface.AttachToThread(SECURITY_ORIGIN_DB_OPTION);
150 SecurityOriginDAO::~SecurityOriginDAO()
152 m_securityOriginDBInterface.DetachFromThread();
155 SecurityOriginDataList SecurityOriginDAO::getSecurityOriginDataList(void)
157 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
159 SecurityOriginDataList list;
160 SECURITY_ORIGIN_DB_SELECT(select, SecurityOriginInfo, &m_securityOriginDBInterface);
161 typedef std::list<SecurityOriginInfo::Row> RowList;
162 RowList rowList = select->GetRowList();
164 FOREACH(it, rowList) {
165 Origin origin(it->Get_scheme(), it->Get_host(), it->Get_port());
167 SecurityOriginDataPtr(
168 new SecurityOriginData(
169 static_cast<Feature>(it->Get_feature()), origin)));
173 SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to get data list")
176 Result SecurityOriginDAO::getResult(const SecurityOriginData &securityOriginData)
178 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
180 SECURITY_ORIGIN_DB_SELECT(select, SecurityOriginInfo, &m_securityOriginDBInterface);
182 And(And(And(Equals<SecurityOriginInfo::feature>(securityOriginData.feature),
183 Equals<SecurityOriginInfo::scheme>(securityOriginData.origin.scheme)),
184 Equals<SecurityOriginInfo::host>(securityOriginData.origin.host)),
185 Equals<SecurityOriginInfo::port>(securityOriginData.origin.port)));
186 SecurityOriginInfo::Select::RowList rows = select->GetRowList();
189 return RESULT_UNKNOWN;
191 SecurityOriginInfo::Row row = rows.front();
192 return static_cast<Result>(row.Get_result());
194 SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to get result for security origin")
197 void SecurityOriginDAO::setSecurityOriginData(const SecurityOriginData &securityOriginData,
200 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
202 ScopedTransaction transaction(&m_securityOriginDBInterface);
203 SecurityOriginInfo::Row row;
204 row.Set_feature(securityOriginData.feature);
205 row.Set_scheme(securityOriginData.origin.scheme);
206 row.Set_host(securityOriginData.origin.host);
207 row.Set_port(securityOriginData.origin.port);
208 row.Set_result(result);
210 if (true == hasResult(securityOriginData)) {
211 SECURITY_ORIGIN_DB_UPDATE(update,
213 &m_securityOriginDBInterface);
217 SECURITY_ORIGIN_DB_INSERT(
220 &m_securityOriginDBInterface);
224 transaction.Commit();
226 SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to set security origin data")
229 void SecurityOriginDAO::removeSecurityOriginData(
230 const SecurityOriginData &securityOriginData)
232 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
234 ScopedTransaction transaction(&m_securityOriginDBInterface);
236 if (true == hasResult(securityOriginData)) {
237 SECURITY_ORIGIN_DB_DELETE(del, SecurityOriginInfo, &m_securityOriginDBInterface)
239 And(And(And(Equals<SecurityOriginInfo::feature>(securityOriginData.feature),
240 Equals<SecurityOriginInfo::scheme>(securityOriginData.origin.scheme)),
241 Equals<SecurityOriginInfo::host>(securityOriginData.origin.host)),
242 Equals<SecurityOriginInfo::port>(securityOriginData.origin.port)));
244 transaction.Commit();
247 SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to set security origin data")
250 void SecurityOriginDAO::removeSecurityOriginData(const Result result)
252 SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
254 ScopedTransaction transaction(&m_securityOriginDBInterface);
255 SECURITY_ORIGIN_DB_DELETE(del, SecurityOriginInfo, &m_securityOriginDBInterface)
256 del->Where(Equals<SecurityOriginInfo::result>(result));
258 transaction.Commit();
260 SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to remove data by result")
263 bool SecurityOriginDAO::hasResult(const SecurityOriginData &securityOriginData)
265 Result res=getResult(securityOriginData);
266 return (res != RESULT_UNKNOWN);
269 #undef SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
270 #undef SQL_CONNECTION_EXCEPTION_HANDLER_END
272 } // namespace SecurityOriginDB