Tizen 2.0 Release
[framework/osp/web.git] / src / controls / FWebCtrl_GeolocationPermissionManagerImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FWebCtrl_GeolocationPermissionManagerImpl.cpp
20  * @brief               The file contains the definition of _GeolocationPermissionManagerImp class.
21  */
22
23 #include <EWebKit2.h>
24 #include <unique_ptr.h>
25 #include <FAppApp.h>
26 #include <FBaseColIList.h>
27 #include <FBaseSysLog.h>
28 #include <FIoDbEnumerator.h>
29 #include <FWebCtrlGeolocationPermissionManager.h>
30 #include <FIo_DatabaseImpl.h>
31 #include "FWebCtrl_GeolocationPermissionManagerImpl.h"
32 #include "FWebCtrl_Utility.h"
33
34
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Collection;
37 using namespace Tizen::Io;
38
39
40 namespace Tizen { namespace Web { namespace Controls
41 {
42
43
44 _GeolocationPermissionManagerImpl::_GeolocationPermissionManagerImpl(void)
45 {
46 }
47
48
49 _GeolocationPermissionManagerImpl::~_GeolocationPermissionManagerImpl(void)
50 {
51 }
52
53
54 result
55 _GeolocationPermissionManagerImpl::Construct(void)
56 {
57         result r = E_SUCCESS;
58
59         String geolocationPath(Tizen::App::App::GetInstance()->GetAppRootPath() + GEOLOCATION_DIRECTORY_PATH + GEOLOCATION_DB_NAME);
60
61         r = __db.Construct(geolocationPath, "r+", null);
62         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
63
64         return E_SUCCESS;
65 }
66
67
68 IList*
69 _GeolocationPermissionManagerImpl::GetOriginListN(void) const
70 {
71         result r = E_SUCCESS;
72
73         String table(GEOLOCATION_TABLE_NAME);
74         std::unique_ptr<DbEnumerator> pEnum(__db.QueryN(L"Select origin From " + table));
75
76         if (pEnum.get())
77         {
78                 std::unique_ptr<ArrayList> pList(new (std::nothrow) ArrayList());
79                 SysTryReturn(NID_WEB_CTRL, pList.get(), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
80
81                 r = pList->Construct();
82                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
83         
84                 while (pEnum->MoveNext() == E_SUCCESS)
85                 {
86                         std::unique_ptr<String> origin(new (std::nothrow) String());
87                         SysTryReturn(NID_WEB_CTRL, origin.get(), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
88                         r = pEnum->GetStringAt(0, *origin.get());
89                         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
90
91                         SysLog(NID_WEB_CTRL, "The current value of origin is %ls", origin->GetPointer());
92
93                         r = pList->Add(*origin.get());
94                         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
95                         origin.release();
96                 }
97
98                 return pList.release();
99         }
100
101         return null;
102 }
103
104
105 bool
106 _GeolocationPermissionManagerImpl::IsPermissionAllowed(const String& origin) const
107 {
108         result r = E_SUCCESS;
109
110         String table(GEOLOCATION_TABLE_NAME);
111
112         std::unique_ptr<DbEnumerator> pEnum(__db.QueryN(L"Select permission From " + table + " Where origin = '" + origin + "'"));
113         if (pEnum.get())
114         {
115                 r = pEnum->MoveNext();
116                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, false, r, "[%s] Propagating.", GetErrorMessage(r));  
117
118                 int permission = 0;
119                 r = pEnum->GetIntAt(0, permission);
120                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, false, r, "[%s] Propagating.", GetErrorMessage(r));
121
122                 return static_cast <bool>(permission);
123         }
124
125         return false;
126 }
127
128
129 result
130 _GeolocationPermissionManagerImpl::Remove(const String& origin)
131 {
132         result r = E_SUCCESS;
133
134         String table(GEOLOCATION_TABLE_NAME);
135
136         __db.BeginTransaction();
137
138         r = __db.ExecuteSql(L"Delete From " + table + " Where origin = '" + origin + "'", true);
139         SysTryCatch(NID_WEB_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
140
141         __db.CommitTransaction();
142
143         return E_SUCCESS;
144
145 CATCH:
146         __db.RollbackTransaction();
147
148         return r;
149 }
150
151
152 result
153 _GeolocationPermissionManagerImpl::RemoveAll(void)
154 {
155         result r = E_SUCCESS;
156
157         String table(GEOLOCATION_TABLE_NAME);
158
159         __db.BeginTransaction();
160
161         r = __db.ExecuteSql(L"Delete From " + table, true);
162         SysTryCatch(NID_WEB_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
163
164         __db.CommitTransaction();
165
166         return E_SUCCESS;
167
168 CATCH:
169         __db.RollbackTransaction();
170
171         return r;
172 }
173
174
175 String
176 _GeolocationPermissionManagerImpl::CreateOrigin(const void* ewkSecurityOrigin)
177 {
178         const Ewk_Security_Origin* pOrigin = reinterpret_cast< const Ewk_Security_Origin* >(ewkSecurityOrigin);
179
180         String protocol(ewk_security_origin_protocol_get(pOrigin));
181         String host(ewk_security_origin_host_get(pOrigin));
182         int port = ewk_security_origin_port_get(pOrigin);
183
184         String origin = protocol + L"://" + host;
185
186         if (port)
187         {
188                 origin.Append(L":");
189                 origin.Append(port);
190         }
191
192         SysLog(NID_WEB_CTRL, "The current value of origin is %ls", origin.GetPointer());
193
194         return origin;
195 }
196
197
198 _GeolocationPermissionManagerImpl*
199 _GeolocationPermissionManagerImpl::GetInstance(GeolocationPermissionManager* pGeolocationPermissionManager)
200 {
201         SysTryReturn(NID_WEB_CTRL, pGeolocationPermissionManager, null, E_INVALID_ARG, "[%s] Invalid argument(s) is used. pGeolocationPermissionManager instance is null" , GetErrorMessage(E_INVALID_ARG));
202         return pGeolocationPermissionManager->__pGeolocationPermissionManagerImpl;
203 }
204
205
206 const _GeolocationPermissionManagerImpl*
207 _GeolocationPermissionManagerImpl::GetInstance(const GeolocationPermissionManager* pGeolocationPermissionManager)
208 {
209         SysTryReturn(NID_WEB_CTRL, pGeolocationPermissionManager, null, E_INVALID_ARG, "[%s] Invalid argument(s) is used. pGeolocationPermissionManager instance is null" , GetErrorMessage(E_INVALID_ARG));
210         return pGeolocationPermissionManager->__pGeolocationPermissionManagerImpl;
211 }
212
213
214 }}} // Tizen::Web::Controls