830903626c321ea36646b0f400ee925bf9ad4825
[platform/framework/native/web.git] / src / controls / FWebCtrl_UserConfirmPopup.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_UserConfirmPopup.cpp
20  * @brief               The file contains the definition of _UserConfirmPopup class.
21  */
22 #include <FAppApp.h>
23 #include <FBaseColArrayList.h>
24 #include <FBaseSysLog.h>
25 #include <FBaseUtilUri.h>
26 #include <FGrpDimension.h>
27 #include <FGrpRectangle.h>
28 #include <FIoDbEnumerator.h>
29 #include <FIoDbStatement.h>
30 #include <FSecCertX509Certificate.h>
31 #include <FUiCtrlButton.h>
32 #include <FUiCtrlLabel.h>
33 #include <FUiCtrlPanel.h>
34 #include <FUiLayout.h>
35 #include <FUiVerticalBoxLayout.h>
36 #include <FIo_DatabaseImpl.h>
37 #include <FUi_ControlManager.h>
38 #include <FUi_ResourceManager.h>
39 #include "FWebCtrl_EflWebkit.h"
40 #include "FWebCtrl_UserConfirmPopup.h"
41 #include "FWebCtrl_Utility.h"
42
43
44 using namespace Tizen::Base;
45 using namespace Tizen::Base::Collection;
46 using namespace Tizen::Base::Utility;
47 using namespace Tizen::Graphics;
48 using namespace Tizen::Io;
49 using namespace Tizen::Security::Cert;
50 using namespace Tizen::Ui;
51 using namespace Tizen::Ui::Controls;
52
53
54 namespace Tizen { namespace Web { namespace Controls
55 {
56
57
58 _UserConfirmPopup::_UserConfirmPopup(void)
59         : __pUserPolicyData(null)
60         , __userConfirmMode(USER_CONFIRM_USERMEDIA)
61         , __sync(false)
62         , __pCheckButton(null)
63 {
64 }
65
66
67 _UserConfirmPopup::~_UserConfirmPopup(void)
68 {
69 }
70
71
72 result
73 _UserConfirmPopup::Construct(_UserConfirmMode userConfirmMode, void* pEventInfo, bool sync)
74 {
75         result r = E_SUCCESS;
76         SysTryReturnResult(NID_WEB_CTRL, pEventInfo, E_INVALID_ARG, "Invalid argument(s) is used. pPolicy is null.");
77         SysTryReturnResult(NID_WEB_CTRL, userConfirmMode >= USER_CONFIRM_USERMEDIA && userConfirmMode <= USER_CONFIRM_GEOLOCATION, E_INVALID_ARG,
78                                         "Invalid userConfirmMode is used. [%d]", userConfirmMode);
79
80         _WebPopupData* pPopupData = _WebPopup::GetPopupData();
81         SysTryReturn(NID_WEB_CTRL, pPopupData, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
82
83         __pUserPolicyData = pEventInfo;
84         __userConfirmMode = userConfirmMode;
85         __sync = sync;
86
87         int popupMaxHeight = 2*pPopupData->labelDim.height + 2*pPopupData->btnDim.height + 6*pPopupData->sideMargin;
88         if (__userConfirmMode == USER_CONFIRM_GEOLOCATION)
89         {
90                 popupMaxHeight +=  pPopupData->checkDim.height;
91         }
92
93         r = _WebPopup::Construct(true, Dimension(pPopupData->popupDim.width, popupMaxHeight));
94         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
95
96         SetTitleText("Security Warning");
97
98         Rectangle rect(0, 0, 0, 0);
99
100         //label
101         rect.height = 2*pPopupData->labelDim.height;
102         rect.width = pPopupData->labelDim.width;
103
104         std::unique_ptr<Label> pLabel(new (std::nothrow) Label());
105         SysTryReturnResult(NID_WEB_CTRL, pLabel.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
106
107         String message = GetMessageFromPolicy();
108         r = pLabel->Construct(rect, message);
109         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
110
111         pLabel->SetTextConfig(pPopupData->labelFontSize, LABEL_TEXT_STYLE_NORMAL);
112
113         r = AddControl(*pLabel);
114         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
115
116         Label* pInfoLabel = pLabel.release();
117         //checkbutton
118         if (__userConfirmMode == USER_CONFIRM_GEOLOCATION)
119         {
120                 rect.y = 0;
121                 rect.height = pPopupData->checkDim.height;
122
123                 std::unique_ptr<CheckButton> pCheckButton (new (std::nothrow) CheckButton());
124                 SysTryReturnResult(NID_WEB_CTRL, pCheckButton.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
125
126                 r = pCheckButton->Construct(Rectangle(0, 0, rect.width, rect.height), CHECK_BUTTON_STYLE_MARK, BACKGROUND_STYLE_DEFAULT, false, L"Remember Preference");
127                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
128
129                 r = AddControl(*pCheckButton);
130                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
131
132                 __pCheckButton = pCheckButton.release();
133         }
134
135         Panel* pButtonPanel = CreateAndAddPanel();
136         SysTryReturn(NID_WEB_CTRL, pButtonPanel, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
137
138         ArrayList idList;
139         r = idList.Construct();
140         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
141         idList.Add(*(new Integer(ID_BUTTON_USER_ALLOW)));
142         idList.Add(*(new Integer(ID_BUTTON_USER_CANCEL)));
143
144         ArrayList titleList;
145         r = titleList.Construct();
146         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
147         titleList.Add(*(new String(L"Allow")));
148         titleList.Add(*(new String(L"Cancel")));
149
150         r = CreateAndAddButtons(idList, titleList, pButtonPanel);
151         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
152
153         std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(GetLayoutN()));
154         SysTryReturn(NID_WEB_CTRL, pLayout.get(), r = GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
155
156         pLayout->SetHorizontalFitPolicy(*pInfoLabel, FIT_POLICY_PARENT);
157         if (__userConfirmMode == USER_CONFIRM_GEOLOCATION)
158         {
159                 pLayout->SetHorizontalFitPolicy(*__pCheckButton, FIT_POLICY_PARENT);
160         }
161         pLayout->SetHorizontalFitPolicy(*pButtonPanel, FIT_POLICY_PARENT);
162
163         pLayout->SetHorizontalAlignment(*pInfoLabel, LAYOUT_HORIZONTAL_ALIGN_CENTER);
164         if (__userConfirmMode == USER_CONFIRM_GEOLOCATION)
165         {
166                 pLayout->SetHorizontalAlignment(*__pCheckButton, LAYOUT_HORIZONTAL_ALIGN_CENTER);
167         }
168         pLayout->SetHorizontalAlignment(*pButtonPanel, LAYOUT_HORIZONTAL_ALIGN_CENTER);
169
170         pLayout->SetSpacing(*pButtonPanel, 2*pPopupData->sideMargin);
171
172         return E_SUCCESS;
173 }
174
175
176 void
177 _UserConfirmPopup::OnActionPerformed(const Control& source, int actionId)
178 {
179         result r = E_SUCCESS;
180
181         switch (actionId)
182         {
183         case ID_BUTTON_USER_ALLOW:
184                 HandleUserAction(EINA_TRUE);
185                 break;
186
187         case ID_BUTTON_USER_CANCEL:
188                 HandleUserAction(EINA_FALSE);
189                 break;
190
191         default:
192                 break;
193         }
194
195         r = HidePopup();
196         if (IsFailed(r))
197         {
198                 SysLogException(NID_WEB_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
199         }
200
201         if (!__sync)
202         {
203                 delete this;
204         }
205 }
206
207
208 String
209 _UserConfirmPopup::GetMessageFromPolicy(void)
210 {
211         String message;
212
213         switch(__userConfirmMode)
214         {
215         case USER_CONFIRM_USERMEDIA:
216         {
217                 message = L"Do you want to allow acccess to media?\n";
218                 break;
219         }
220         case USER_PROTOCOL_HANDLER:
221         {
222                 Ewk_Custom_Handlers_Data* pHandlerData = reinterpret_cast< Ewk_Custom_Handlers_Data* >(__pUserPolicyData);
223                 SysAssertf(pHandlerData, "Failed to request");
224
225                 String target(ewk_custom_handlers_data_target_get(pHandlerData));
226                 String baseUri(ewk_custom_handlers_data_base_url_get(pHandlerData));
227
228                 message = baseUri + String(L" is asking to register ") + target + String(L" protocol handler.");
229                 break;
230         }
231         case USER_CONTENT_HANDLER:
232         {
233                 Ewk_Custom_Handlers_Data* pHandlerData = reinterpret_cast< Ewk_Custom_Handlers_Data* >(__pUserPolicyData);
234                 SysAssertf(pHandlerData, "Failed to request");
235
236                 String target(ewk_custom_handlers_data_target_get(pHandlerData));
237                 String baseUri(ewk_custom_handlers_data_base_url_get(pHandlerData));
238
239                 message = String(baseUri) + String(L" is asking to register ") + String(target) + String(L" content handler.");
240                 break;
241         }
242         case USER_CONFIRM_GEOLOCATION:
243         {
244                 Ewk_Geolocation_Permission_Request* pGeoLocPermReq = reinterpret_cast< Ewk_Geolocation_Permission_Request* >(__pUserPolicyData);
245                 const Ewk_Security_Origin* pSecurityOrigin = ewk_geolocation_permission_request_origin_get(pGeoLocPermReq);
246
247                 message = _Utility::CreateOrigin(pSecurityOrigin);
248                 message.Append("Requests your Location");
249                 break;
250         }
251         default:
252                 SysAssert(false);
253         }
254
255         return message;
256 }
257
258
259 void
260 _UserConfirmPopup::HandleUserAction(Eina_Bool allow)
261 {
262         switch (__userConfirmMode)
263         {
264         case USER_CONFIRM_USERMEDIA:
265         {
266                 Ewk_User_Media_Permission_Request* pPolicy = reinterpret_cast< Ewk_User_Media_Permission_Request* >(__pUserPolicyData);
267                 ewk_user_media_permission_request_set(pPolicy, allow);
268                 break;
269         }
270         case USER_PROTOCOL_HANDLER:
271         {
272                 RegisterHandler(false, allow);
273                 break;
274         }
275         case USER_CONTENT_HANDLER:
276         {
277                 RegisterHandler(true, allow);
278                 break;
279         }
280         case USER_CONFIRM_GEOLOCATION:
281         {
282                 Ewk_Geolocation_Permission_Request* pPolicy =  reinterpret_cast< Ewk_Geolocation_Permission_Request* >(__pUserPolicyData);
283                 ewk_geolocation_permission_request_set(pPolicy, allow);
284                 if (__pCheckButton->IsSelected())
285                 {
286                         result r = AddGeolocationDb(pPolicy, static_cast < bool >(allow));
287                         SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
288                 }
289                 break;
290         }
291         default:
292                 SysAssert(false);
293         }
294 }
295
296
297 result
298 _UserConfirmPopup::AddGeolocationDb(Ewk_Geolocation_Permission_Request* pPolicy, bool enable)
299 {
300         _DatabaseImpl db;
301         String geolocationPath(Tizen::App::App::GetInstance()->GetAppRootPath() + CUSTOM_DB_DIRECTORY_PATH + USER_CONFIRM_DB_NAME);
302         String table(GEOLOCATION_TABLE_NAME);
303
304         const Ewk_Security_Origin* pSecurityOrigin = ewk_geolocation_permission_request_origin_get(pPolicy);
305         String origin = _Utility::CreateOrigin(pSecurityOrigin);
306
307         result r = db.Construct(geolocationPath, "r+", null);
308         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
309
310         std::unique_ptr<DbStatement> pStmt(db.CreateStatementN(L"Insert Into " + table + L" (origin, permission) Values (?, ?)"));
311         SysTryReturn(NID_WEB_CTRL, pStmt.get(), GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
312
313         SysLog(NID_WEB_CTRL, "The current value of stmt is %u, host is %ls", pStmt.get(), origin.GetPointer());
314
315         pStmt->BindString(0, origin);
316         pStmt->BindInt(1, static_cast < int >(enable));
317
318         db.BeginTransaction();
319
320         std::unique_ptr<DbEnumerator> pEnum(db.ExecuteStatementN(*pStmt));
321
322         db.CommitTransaction();
323
324         return E_SUCCESS;
325 }
326
327 void
328 _UserConfirmPopup::RegisterHandler(bool checkHandler, Eina_Bool allow)
329 {
330         _DatabaseImpl db;
331         String handlerPath(Tizen::App::App::GetInstance()->GetAppRootPath() + CUSTOM_DB_DIRECTORY_PATH + USER_CONFIRM_DB_NAME);
332         String table;
333         int checkId = 0;
334         
335         if(checkHandler == true)
336         {
337                 table = CUSTOM_CONTENT_TABLE_NAME;
338         }
339         else
340         {
341                 table = CUSTOM_PROTOCOL_TABLE_NAME;
342         }
343         Ewk_Custom_Handlers_Data* pHandlerData = reinterpret_cast< Ewk_Custom_Handlers_Data* >(__pUserPolicyData);
344         SysAssertf(pHandlerData, "Failed to request");
345                         
346         String baseUri(ewk_custom_handlers_data_base_url_get(pHandlerData));
347         String uri = ewk_custom_handlers_data_url_get(pHandlerData);
348         String target = ewk_custom_handlers_data_target_get(pHandlerData);
349
350         result r = db.Construct(handlerPath, "r+", null);
351         SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
352
353         std::unique_ptr<DbEnumerator> pEnum(db.QueryN(L"Select id From " + table + L" Where baseUrl = '" + baseUri + L"' AND url = '" + uri + L"' AND mime = '" + target + L"'"));
354
355         if(pEnum.get())
356         {
357                 pEnum->MoveNext();
358                 pEnum->GetIntAt(0, checkId);
359                 
360                 std::unique_ptr<DbStatement> pUpdateStmt(db.CreateStatementN(L"Update " + table + L" Set allow = (?) Where id = (?)"));
361                 SysTryReturnVoidResult(NID_WEB_CTRL, pUpdateStmt.get(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
362
363                 pUpdateStmt->BindInt(0, static_cast < int >(allow));
364                 pUpdateStmt->BindInt(1, checkId);
365
366                 db.BeginTransaction();
367                 
368                 std::unique_ptr<DbEnumerator> pEnum(db.ExecuteStatementN(*pUpdateStmt));
369                 
370                 db.CommitTransaction();
371                 
372         }
373         else
374         {
375                 std::unique_ptr<DbStatement> pStmt(db.CreateStatementN(L"Insert Into " + table + L" (baseUrl, url, mime, allow) Values (?, ?, ?, ?)"));
376                 SysTryReturnVoidResult(NID_WEB_CTRL, pStmt.get(),  GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
377                                                 
378                 pStmt->BindString(0, baseUri);
379                 pStmt->BindString(1, uri);
380                 pStmt->BindString(2, target);
381                 pStmt->BindInt(3, static_cast < int >(allow));
382                 
383                 db.BeginTransaction();
384                 
385                 std::unique_ptr<DbEnumerator> pRegisterEnum(db.ExecuteStatementN(*pStmt));
386                 
387                 db.CommitTransaction();
388         }
389 }
390
391 }}} // Tizen::Web::Controls