Source code formating unification
[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 #define SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN          Try
41
42 #define SQL_CONNECTION_EXCEPTION_HANDLER_END(message)   \
43     Catch(DPL::DB::SqlConnection::Exception::Base) {       \
44         LogError(message);                              \
45         ReThrowMsg(SecurityOriginDAO::Exception::DatabaseError, \
46                    message);                            \
47     }
48
49 namespace {
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";
57
58 std::string createDatabasePath(const WrtDB::WidgetPkgName &pkgName)
59 {
60     std::stringstream filename;
61
62     filename << WrtDB::WidgetConfig::GetWidgetPersistentStoragePath(pkgName)
63              << "/"
64              << SECURITY_ORIGIN_DB_NAME;
65     return filename.str();
66 }
67
68 std::string createDatabasePath(int widgetHandle)
69 {
70     using namespace DPL::DB::ORM;
71     using namespace WrtDB::WidgetConfig;
72     using namespace WrtDB::GlobalConfig;
73
74     WrtDB::TizenAppId appid;
75
76     Try
77     {
78         appid = WrtDB::WidgetDAOReadOnly::getTzAppId(widgetHandle);
79     }
80     Catch(DPL::DB::SqlConnection::Exception::Base) {
81         LogError("Failed to get database Path");
82     }
83     return createDatabasePath(appid);
84 }
85
86 void checkDatabase(std::string databasePath)
87 {
88     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
89     {
90         if (databasePath.empty()) {
91             ThrowMsg(SecurityOriginDAO::Exception::DatabaseError,
92                      "Wrong database Path is passed");
93         }
94
95         struct stat buffer;
96         if (stat(databasePath.c_str(), &buffer) != 0) {
97             //Create fresh database
98             LogInfo("Creating database " << databasePath);
99
100             std::fstream file;
101             file.open(SECURITY_ORIGIN_DB_SQL_PATH, std::ios_base::in);
102             if (!file) {
103                 ThrowMsg(SecurityOriginDAO::Exception::DatabaseError,
104                          "Fail to get database Path");
105             }
106
107             std::stringstream ssBuffer;
108             ssBuffer << file.rdbuf();
109
110             file.close();
111
112             DPL::DB::SqlConnection con(databasePath,
113                                        SECURITY_ORIGIN_DB_TYPE,
114                                        SECURITY_ORIGIN_DB_OPTION);
115             con.ExecCommand(ssBuffer.str().c_str());
116         }
117     }
118     SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to get database Path")
119 }
120 }
121
122 SecurityOriginDAO::SecurityOriginDAO(int handle) :
123     m_securityOriginDBPath(createDatabasePath(handle)),
124     m_securityOriginDBInterface(m_securityOriginDBPath, SECURITY_ORIGIN_DB_TYPE)
125 {
126     checkDatabase(m_securityOriginDBPath);
127     m_securityOriginDBInterface.AttachToThread(SECURITY_ORIGIN_DB_OPTION);
128 }
129
130 SecurityOriginDAO::SecurityOriginDAO(const WrtDB::WidgetPkgName &pkgName) :
131     m_securityOriginDBPath(createDatabasePath(pkgName)),
132     m_securityOriginDBInterface(m_securityOriginDBPath, SECURITY_ORIGIN_DB_TYPE)
133 {
134     checkDatabase(m_securityOriginDBPath);
135     m_securityOriginDBInterface.AttachToThread(SECURITY_ORIGIN_DB_OPTION);
136 }
137
138 SecurityOriginDAO::~SecurityOriginDAO()
139 {
140     m_securityOriginDBInterface.DetachFromThread();
141 }
142
143 SecurityOriginDataList SecurityOriginDAO::getSecurityOriginDataList(void)
144 {
145     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
146     {
147         SecurityOriginDataList list;
148         SECURITY_ORIGIN_DB_SELECT(select,
149                                   SecurityOriginInfo,
150                                   &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(
167     const SecurityOriginData &securityOriginData)
168 {
169     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
170     {
171         SECURITY_ORIGIN_DB_SELECT(select,
172                                   SecurityOriginInfo,
173                                   &m_securityOriginDBInterface);
174         select->Where(
175             And(And(And(Equals<SecurityOriginInfo::feature>(securityOriginData.
176                                                                 feature),
177                         Equals<SecurityOriginInfo::scheme>(securityOriginData.
178                                                                origin.scheme)),
179                     Equals<SecurityOriginInfo::host>(securityOriginData.origin.
180                                                          host)),
181                 Equals<SecurityOriginInfo::port>(securityOriginData.origin.port)));
182         SecurityOriginInfo::Select::RowList rows = select->GetRowList();
183
184         if (rows.empty()) {
185             return RESULT_UNKNOWN;
186         }
187         SecurityOriginInfo::Row row = rows.front();
188         return static_cast<Result>(row.Get_result());
189     }
190     SQL_CONNECTION_EXCEPTION_HANDLER_END(
191         "Failed to get result for security origin")
192 }
193
194 void SecurityOriginDAO::setSecurityOriginData(
195     const SecurityOriginData &securityOriginData,
196     const Result result)
197 {
198     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
199     {
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);
207
208         if (true == hasResult(securityOriginData)) {
209             SECURITY_ORIGIN_DB_UPDATE(update,
210                                       SecurityOriginInfo,
211                                       &m_securityOriginDBInterface);
212             update->Values(row);
213             update->Execute();
214         } else {
215             SECURITY_ORIGIN_DB_INSERT(
216                 insert,
217                 SecurityOriginInfo,
218                 &m_securityOriginDBInterface);
219             insert->Values(row);
220             insert->Execute();
221         }
222         transaction.Commit();
223     }
224     SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to set security origin data")
225 }
226
227 void SecurityOriginDAO::removeSecurityOriginData(
228     const SecurityOriginData &securityOriginData)
229 {
230     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
231     {
232         ScopedTransaction transaction(&m_securityOriginDBInterface);
233
234         if (true == hasResult(securityOriginData)) {
235             SECURITY_ORIGIN_DB_DELETE(del,
236                                       SecurityOriginInfo,
237                                       &m_securityOriginDBInterface)
238             del->Where(
239                 And(And(And(Equals<SecurityOriginInfo::feature>(
240                                 securityOriginData.feature),
241                             Equals<SecurityOriginInfo::scheme>(
242                                 securityOriginData.origin.scheme)),
243                         Equals<SecurityOriginInfo::host>(securityOriginData.
244                                                              origin.host)),
245                     Equals<SecurityOriginInfo::port>(securityOriginData.origin.
246                                                          port)));
247             del->Execute();
248             transaction.Commit();
249         }
250     }
251     SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to set security origin data")
252 }
253
254 void SecurityOriginDAO::removeSecurityOriginData(const Result result)
255 {
256     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
257     {
258         ScopedTransaction transaction(&m_securityOriginDBInterface);
259         SECURITY_ORIGIN_DB_DELETE(del,
260                                   SecurityOriginInfo,
261                                   &m_securityOriginDBInterface)
262         del->Where(Equals<SecurityOriginInfo::result>(result));
263         del->Execute();
264         transaction.Commit();
265     }
266     SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to remove data by result")
267 }
268
269 bool SecurityOriginDAO::hasResult(const SecurityOriginData &securityOriginData)
270 {
271     Result res = getResult(securityOriginData);
272     return (res != RESULT_UNKNOWN);
273 }
274
275 #undef SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
276 #undef SQL_CONNECTION_EXCEPTION_HANDLER_END
277 } // namespace SecurityOriginDB