Overload methods to run with tizen id instead of widgetHandle - fixed
[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 <sys/stat.h>
33 #include <fstream>
34
35 using namespace DPL::DB::ORM;
36 using namespace DPL::DB::ORM::security_origin;
37
38 namespace SecurityOriginDB {
39
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(int widgetHandle)
59 {
60     using namespace DPL::DB::ORM;
61     using namespace WrtDB::WidgetConfig;
62     using namespace WrtDB::GlobalConfig;
63
64     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
65     {
66         std::stringstream filename;
67         WrtDB::WidgetDAOReadOnly widgetDAO(widgetHandle);
68         DPL::Optional<DPL::String> pkgname = widgetDAO.getPkgname();
69
70         filename << GetWidgetPersistentStoragePath(*pkgname)
71                  << "/"
72                  << SECURITY_ORIGIN_DB_NAME;
73         return filename.str();
74     }
75     SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to get database Path")
76
77 }
78
79 std::string createDatabasePath(const DPL::String& tizenId)
80 {
81     using namespace DPL::DB::ORM;
82     using namespace WrtDB::WidgetConfig;
83     using namespace WrtDB::GlobalConfig;
84
85     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
86     {
87         std::stringstream filename;
88         filename << GetWidgetPersistentStoragePath(tizenId)
89                  << "/"
90                  << SECURITY_ORIGIN_DB_NAME;
91         return filename.str();
92     }
93     SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to get database Path")
94
95 }
96
97 void checkDatabase(std::string databasePath)
98 {
99     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
100     {
101         if (databasePath.empty()) {
102             ThrowMsg(SecurityOriginDAO::Exception::DatabaseError,
103                      "Wrong database Path is passed");
104         }
105
106         struct stat buffer;
107         if (stat(databasePath.c_str(), &buffer) != 0) {
108
109             //Create fresh database
110             LogInfo("Creating database " << databasePath);
111
112             std::fstream file;
113             file.open(SECURITY_ORIGIN_DB_SQL_PATH, std::ios_base::in);
114             if (!file) {
115                 ThrowMsg(SecurityOriginDAO::Exception::DatabaseError,
116                          "Fail to get database Path");
117             }
118
119             std::stringstream ssBuffer;
120             ssBuffer << file.rdbuf();
121
122             file.close();
123
124             DPL::DB::SqlConnection con(databasePath,
125                                        SECURITY_ORIGIN_DB_TYPE,
126                                        SECURITY_ORIGIN_DB_OPTION);
127             con.ExecCommand(ssBuffer.str().c_str());
128         }
129     }
130     SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to get database Path")
131 }
132 }
133
134 SecurityOriginDAO::SecurityOriginDAO(int handle) :
135     m_securityOriginDBPath(createDatabasePath(handle)),
136     m_securityOriginDBInterface(m_securityOriginDBPath, SECURITY_ORIGIN_DB_TYPE)
137 {
138     checkDatabase(m_securityOriginDBPath);
139     m_securityOriginDBInterface.AttachToThread(SECURITY_ORIGIN_DB_OPTION);
140 }
141
142 SecurityOriginDAO::SecurityOriginDAO(const DPL::String& tizenId):
143     m_securityOriginDBPath(createDatabasePath(tizenId)),
144     m_securityOriginDBInterface(m_securityOriginDBPath, SECURITY_ORIGIN_DB_TYPE)
145 {
146     checkDatabase(m_securityOriginDBPath);
147     m_securityOriginDBInterface.AttachToThread(SECURITY_ORIGIN_DB_OPTION);
148 }
149
150 SecurityOriginDAO::~SecurityOriginDAO()
151 {
152     m_securityOriginDBInterface.DetachFromThread();
153 }
154
155 SecurityOriginDataList SecurityOriginDAO::getSecurityOriginDataList(void)
156 {
157     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
158     {
159         SecurityOriginDataList list;
160         SECURITY_ORIGIN_DB_SELECT(select, SecurityOriginInfo, &m_securityOriginDBInterface);
161         typedef std::list<SecurityOriginInfo::Row> RowList;
162         RowList rowList = select->GetRowList();
163
164         FOREACH(it, rowList) {
165             Origin origin(it->Get_scheme(), it->Get_host(), it->Get_port());
166             list.push_back(
167                 SecurityOriginDataPtr(
168                     new SecurityOriginData(
169                         static_cast<Feature>(it->Get_feature()), origin)));
170         }
171         return list;
172     }
173     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to get data  list")
174 }
175
176 Result SecurityOriginDAO::getResult(const SecurityOriginData &securityOriginData)
177 {
178     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
179     {
180         SECURITY_ORIGIN_DB_SELECT(select, SecurityOriginInfo, &m_securityOriginDBInterface);
181         select->Where(
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();
187
188         if (rows.empty()) {
189             return RESULT_UNKNOWN;
190         }
191         SecurityOriginInfo::Row row = rows.front();
192         return static_cast<Result>(row.Get_result());
193     }
194     SQL_CONNECTION_EXCEPTION_HANDLER_END("Failed to get result for security origin")
195 }
196
197 void SecurityOriginDAO::setSecurityOriginData(const SecurityOriginData &securityOriginData,
198                                               const Result result)
199 {
200     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
201     {
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);
209
210         if (true == hasResult(securityOriginData)) {
211             SECURITY_ORIGIN_DB_UPDATE(update,
212                                SecurityOriginInfo,
213                                &m_securityOriginDBInterface);
214             update->Values(row);
215             update->Execute();
216         } else {
217             SECURITY_ORIGIN_DB_INSERT(
218                 insert,
219                 SecurityOriginInfo,
220                 &m_securityOriginDBInterface);
221             insert->Values(row);
222             insert->Execute();
223         }
224         transaction.Commit();
225     }
226     SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to set security origin data")
227 }
228
229 void SecurityOriginDAO::removeSecurityOriginData(
230     const SecurityOriginData &securityOriginData)
231 {
232     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
233     {
234         ScopedTransaction transaction(&m_securityOriginDBInterface);
235
236         if (true == hasResult(securityOriginData)) {
237             SECURITY_ORIGIN_DB_DELETE(del, SecurityOriginInfo, &m_securityOriginDBInterface)
238             del->Where(
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)));
243             del->Execute();
244             transaction.Commit();
245         }
246     }
247     SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to set security origin data")
248 }
249
250 void SecurityOriginDAO::removeSecurityOriginData(const Result result)
251 {
252     SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
253     {
254         ScopedTransaction transaction(&m_securityOriginDBInterface);
255         SECURITY_ORIGIN_DB_DELETE(del, SecurityOriginInfo, &m_securityOriginDBInterface)
256         del->Where(Equals<SecurityOriginInfo::result>(result));
257         del->Execute();
258         transaction.Commit();
259     }
260     SQL_CONNECTION_EXCEPTION_HANDLER_END("Fail to remove data by result")
261 }
262
263 bool SecurityOriginDAO::hasResult(const SecurityOriginData &securityOriginData)
264 {
265     Result res=getResult(securityOriginData);
266     return (res != RESULT_UNKNOWN);
267 }
268
269 #undef SQL_CONNECTION_EXCEPTION_HANDLER_BEGIN
270 #undef SQL_CONNECTION_EXCEPTION_HANDLER_END
271
272 } // namespace SecurityOriginDB