Tizen 2.0 Release
[framework/web/wrt-commons.git] / modules / security_origin_dao / dao / security_origin_dao.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /*
17  * @file    security_origin_dao.cpp
18  * @author  Jihoon Chung (jihoon.chung@samsung.com)
19  * @version 1.0
20  * @brief    This file contains the definition of security origin dao class.
21  */
22
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>
33 #include <sys/stat.h>
34 #include <fstream>
35
36 using namespace DPL::DB::ORM;
37 using namespace DPL::DB::ORM::security_origin;
38
39 namespace SecurityOriginDB {
40
41 #define SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN          Try
42
43 #define SQL_CONNECTION_EXCEPTION_HANDLER_END(message)   \
44     Catch(DPL::DB::SqlConnection::Exception::Base) {       \
45         LogError(message);                              \
46         ReThrowMsg(SecurityOriginDAO::Exception::DatabaseError, \
47                    message);                            \
48     }
49
50 namespace {
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";
58
59 std::string createDatabasePath(const WrtDB::WidgetPkgName &pkgName)
60 {
61         std::stringstream filename;
62
63         filename << WrtDB::WidgetConfig::GetWidgetPersistentStoragePath(pkgName)
64                  << "/"
65                  << SECURITY_ORIGIN_DB_NAME;
66         return filename.str();
67 }
68
69 std::string createDatabasePath(int widgetHandle)
70 {
71     using namespace DPL::DB::ORM;
72     using namespace WrtDB::WidgetConfig;
73     using namespace WrtDB::GlobalConfig;
74
75     WrtDB::WidgetPkgName pkgname;
76
77     Try
78     {
79         pkgname = WrtDB::WidgetDAOReadOnly::getPkgName(widgetHandle);
80     }
81     Catch(DPL::DB::SqlConnection::Exception::Base) {
82         LogError("Failed to get database Path");
83     }
84     return createDatabasePath(pkgname);
85 }
86
87 void checkDatabase(std::string databasePath)
88 {
89     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
90     {
91         if (databasePath.empty()) {
92             ThrowMsg(SecurityOriginDAO::Exception::DatabaseError,
93                      "Wrong database Path is passed");
94         }
95
96         struct stat buffer;
97         if (stat(databasePath.c_str(), &buffer) != 0) {
98
99             //Create fresh database
100             LogInfo("Creating database " << databasePath);
101
102             std::fstream file;
103             file.open(SECURITY_ORIGIN_DB_SQL_PATH, std::ios_base::in);
104             if (!file) {
105                 ThrowMsg(SecurityOriginDAO::Exception::DatabaseError,
106                          "Fail to get database Path");
107             }
108
109             std::stringstream ssBuffer;
110             ssBuffer << file.rdbuf();
111
112             file.close();
113
114             DPL::DB::SqlConnection con(databasePath,
115                                        SECURITY_ORIGIN_DB_TYPE,
116                                        SECURITY_ORIGIN_DB_OPTION);
117             con.ExecCommand(ssBuffer.str().c_str());
118         }
119     }
120     SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to get database Path")
121 }
122 }
123
124 SecurityOriginDAO::SecurityOriginDAO(int handle) :
125     m_securityOriginDBPath(createDatabasePath(handle)),
126     m_securityOriginDBInterface(m_securityOriginDBPath, SECURITY_ORIGIN_DB_TYPE)
127 {
128     checkDatabase(m_securityOriginDBPath);
129     m_securityOriginDBInterface.AttachToThread(SECURITY_ORIGIN_DB_OPTION);
130 }
131
132 SecurityOriginDAO::SecurityOriginDAO(const WrtDB::WidgetPkgName &pkgName) :
133     m_securityOriginDBPath(createDatabasePath(pkgName)),
134     m_securityOriginDBInterface(m_securityOriginDBPath, SECURITY_ORIGIN_DB_TYPE)
135 {
136     checkDatabase(m_securityOriginDBPath);
137     m_securityOriginDBInterface.AttachToThread(SECURITY_ORIGIN_DB_OPTION);
138 }
139
140 SecurityOriginDAO::~SecurityOriginDAO()
141 {
142     m_securityOriginDBInterface.DetachFromThread();
143 }
144
145 SecurityOriginDataList SecurityOriginDAO::getSecurityOriginDataList(void)
146 {
147     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
148     {
149         SecurityOriginDataList list;
150         SECURITY_ORIGIN_DB_SELECT(select, SecurityOriginInfo, &m_securityOriginDBInterface);
151         typedef std::list<SecurityOriginInfo::Row> RowList;
152         RowList rowList = select->GetRowList();
153
154         FOREACH(it, rowList) {
155             Origin origin(it->Get_scheme(), it->Get_host(), it->Get_port());
156             list.push_back(
157                 SecurityOriginDataPtr(
158                     new SecurityOriginData(
159                         static_cast<Feature>(it->Get_feature()), origin)));
160         }
161         return list;
162     }
163     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to get data  list")
164 }
165
166 Result SecurityOriginDAO::getResult(const SecurityOriginData &securityOriginData)
167 {
168     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
169     {
170         SECURITY_ORIGIN_DB_SELECT(select, SecurityOriginInfo, &m_securityOriginDBInterface);
171         select->Where(
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();
177
178         if (rows.empty()) {
179             return RESULT_UNKNOWN;
180         }
181         SecurityOriginInfo::Row row = rows.front();
182         return static_cast<Result>(row.Get_result());
183     }
184     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to get result for security origin")
185 }
186
187 void SecurityOriginDAO::setSecurityOriginData(const SecurityOriginData &securityOriginData,
188                                               const Result result)
189 {
190     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
191     {
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);
199
200         if (true == hasResult(securityOriginData)) {
201             SECURITY_ORIGIN_DB_UPDATE(update,
202                                SecurityOriginInfo,
203                                &m_securityOriginDBInterface);
204             update->Values(row);
205             update->Execute();
206         } else {
207             SECURITY_ORIGIN_DB_INSERT(
208                 insert,
209                 SecurityOriginInfo,
210                 &m_securityOriginDBInterface);
211             insert->Values(row);
212             insert->Execute();
213         }
214         transaction.Commit();
215     }
216     SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to set security origin data")
217 }
218
219 void SecurityOriginDAO::removeSecurityOriginData(
220     const SecurityOriginData &securityOriginData)
221 {
222     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
223     {
224         ScopedTransaction transaction(&m_securityOriginDBInterface);
225
226         if (true == hasResult(securityOriginData)) {
227             SECURITY_ORIGIN_DB_DELETE(del, SecurityOriginInfo, &m_securityOriginDBInterface)
228             del->Where(
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)));
233             del->Execute();
234             transaction.Commit();
235         }
236     }
237     SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to set security origin data")
238 }
239
240 void SecurityOriginDAO::removeSecurityOriginData(const Result result)
241 {
242     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
243     {
244         ScopedTransaction transaction(&m_securityOriginDBInterface);
245         SECURITY_ORIGIN_DB_DELETE(del, SecurityOriginInfo, &m_securityOriginDBInterface)
246         del->Where(Equals<SecurityOriginInfo::result>(result));
247         del->Execute();
248         transaction.Commit();
249     }
250     SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to remove data by result")
251 }
252
253 bool SecurityOriginDAO::hasResult(const SecurityOriginData &securityOriginData)
254 {
255     Result res=getResult(securityOriginData);
256     return (res != RESULT_UNKNOWN);
257 }
258
259 #undef SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
260 #undef SQL_CONNECTION_EXCEPTION_HANDLER_END
261
262 } // namespace SecurityOriginDB