Fix for build error
[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         , __pCheckButton(null)
62         , __sync(false)
63 {
64 }
65
66
67 _UserConfirmPopup::~_UserConfirmPopup(void)
68 {
69 }
70
71
72 result
73 _UserConfirmPopup::Construct(_UserConfirmMode userConfirmMode, void* pEventInfo, bool sync, String msg)
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_SCRIPT_ALERT, 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         bool hasTitle = true;
88         int popupMaxHeight = 2*pPopupData->labelDim.height + 2*pPopupData->btnDim.height + 6*pPopupData->sideMargin;
89
90         if (__userConfirmMode == USER_CONFIRM_GEOLOCATION)
91         {
92                 popupMaxHeight +=  pPopupData->checkDim.height;
93         }
94
95         if (__userConfirmMode == USER_SCRIPT_ALERT)
96         {
97                 popupMaxHeight -=  pPopupData->labelDim.height;
98                 hasTitle = false;
99         }
100
101         r = _WebPopup::Construct(hasTitle, Dimension(pPopupData->popupDim.width, popupMaxHeight));
102         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
103
104         if (userConfirmMode != USER_SCRIPT_ALERT)
105         {
106                 SetTitleText("Security Warning");
107         }
108
109         Rectangle rect(0, 0, 0, 0);
110
111         //label
112         rect.height = 2*pPopupData->labelDim.height;
113         rect.width = pPopupData->labelDim.width;
114
115         std::unique_ptr<Label> pLabel(new (std::nothrow) Label());
116         SysTryReturnResult(NID_WEB_CTRL, pLabel.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
117
118         String message = L"";
119         if (userConfirmMode == USER_SCRIPT_ALERT)
120         {
121                 message = msg;
122         }
123         else
124         {
125                 message = GetMessageFromPolicy();
126         }
127         r = pLabel->Construct(rect, message);
128         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
129
130         pLabel->SetTextConfig(pPopupData->labelFontSize, LABEL_TEXT_STYLE_NORMAL);
131
132         r = AddControl(*pLabel);
133         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
134
135         Label* pInfoLabel = pLabel.release();
136         //checkbutton
137         if (__userConfirmMode == USER_CONFIRM_GEOLOCATION)
138         {
139                 rect.y = 0;
140                 rect.height = pPopupData->checkDim.height;
141
142                 std::unique_ptr<CheckButton> pCheckButton (new (std::nothrow) CheckButton());
143                 SysTryReturnResult(NID_WEB_CTRL, pCheckButton.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
144
145                 r = pCheckButton->Construct(Rectangle(0, 0, rect.width, rect.height), CHECK_BUTTON_STYLE_MARK, BACKGROUND_STYLE_NONE, false, L"Remember Preference");
146                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
147
148                 r = AddControl(*pCheckButton);
149                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
150
151                 __pCheckButton = pCheckButton.release();
152         }
153
154         Panel* pButtonPanel = CreateAndAddPanel();
155         SysTryReturn(NID_WEB_CTRL, pButtonPanel, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
156
157         ArrayList idList;
158         r = idList.Construct();
159         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
160
161         ArrayList titleList;
162         r = titleList.Construct();
163         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
164
165         idList.Add(*(new Integer(ID_BUTTON_USER_ALLOW)));
166         if (userConfirmMode == USER_SCRIPT_ALERT)
167         {
168                 titleList.Add(*(new String(L"OK")));
169         }
170         else
171         {
172                 idList.Add(*(new Integer(ID_BUTTON_USER_CANCEL)));
173                 titleList.Add(*(new String(L"Allow")));
174                 titleList.Add(*(new String(L"Cancel")));
175         }
176
177         r = CreateAndAddButtons(idList, titleList, pButtonPanel);
178         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
179
180         std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(GetLayoutN()));
181         SysTryReturn(NID_WEB_CTRL, pLayout.get(), r = GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
182
183         pLayout->SetHorizontalAlignment(*pInfoLabel, LAYOUT_HORIZONTAL_ALIGN_CENTER);
184         if (__userConfirmMode == USER_CONFIRM_GEOLOCATION)
185         {
186                 pLayout->SetHorizontalAlignment(*__pCheckButton, LAYOUT_HORIZONTAL_ALIGN_CENTER);
187         }
188         pLayout->SetHorizontalAlignment(*pButtonPanel, LAYOUT_HORIZONTAL_ALIGN_CENTER);
189
190         pLayout->SetSpacing(*pButtonPanel, 2*pPopupData->sideMargin);
191
192         return E_SUCCESS;
193 }
194
195
196 void
197 _UserConfirmPopup::OnActionPerformed(const Control& source, int actionId)
198 {
199         result r = E_SUCCESS;
200
201         switch (actionId)
202         {
203         case ID_BUTTON_USER_ALLOW:
204                 HandleUserAction(EINA_TRUE);
205                 break;
206
207         case ID_BUTTON_USER_CANCEL:
208                 HandleUserAction(EINA_FALSE);
209                 break;
210
211         default:
212                 break;
213         }
214
215         r = HidePopup();
216         if (IsFailed(r))
217         {
218                 SysLogException(NID_WEB_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
219         }
220
221         if (!__sync)
222         {
223                 delete this;
224         }
225 }
226
227
228 String
229 _UserConfirmPopup::GetMessageFromPolicy(void)
230 {
231         String message;
232
233         switch(__userConfirmMode)
234         {
235         case USER_CONFIRM_USERMEDIA:
236         {
237                 message = L"Do you want to allow acccess to media?\n";
238                 break;
239         }
240         case USER_PROTOCOL_HANDLER:
241         {
242                 Ewk_Custom_Handlers_Data* pHandlerData = reinterpret_cast< Ewk_Custom_Handlers_Data* >(__pUserPolicyData);
243                 SysAssertf(pHandlerData, "Failed to request");
244
245                 String target(ewk_custom_handlers_data_target_get(pHandlerData));
246                 String baseUri(ewk_custom_handlers_data_base_url_get(pHandlerData));
247
248                 message = baseUri + String(L" is asking to register ") + target + String(L" protocol handler.");
249                 break;
250         }
251         case USER_CONTENT_HANDLER:
252         {
253                 Ewk_Custom_Handlers_Data* pHandlerData = reinterpret_cast< Ewk_Custom_Handlers_Data* >(__pUserPolicyData);
254                 SysAssertf(pHandlerData, "Failed to request");
255
256                 String target(ewk_custom_handlers_data_target_get(pHandlerData));
257                 String baseUri(ewk_custom_handlers_data_base_url_get(pHandlerData));
258
259                 message = String(baseUri) + String(L" is asking to register ") + String(target) + String(L" content handler.");
260                 break;
261         }
262         case USER_CONFIRM_GEOLOCATION:
263         {
264                 Ewk_Geolocation_Permission_Request* pGeoLocPermReq = reinterpret_cast< Ewk_Geolocation_Permission_Request* >(__pUserPolicyData);
265                 const Ewk_Security_Origin* pSecurityOrigin = ewk_geolocation_permission_request_origin_get(pGeoLocPermReq);
266
267                 message = _Utility::CreateOrigin(pSecurityOrigin);
268                 message.Append(" Requests your Location");
269                 break;
270         }
271         case USER_CONFIRM_NOTIFICATION:
272         {
273                 Ewk_Notification_Permission_Request* pPermissionRequest = reinterpret_cast< Ewk_Notification_Permission_Request* >(__pUserPolicyData);
274                 const Ewk_Security_Origin* pSecurityOrigin = ewk_notification_permission_request_origin_get(pPermissionRequest);
275                 message = _Utility::CreateOrigin(pSecurityOrigin);
276                 message.Append(L" wants to display notifications");
277                 break;
278         }
279         default:
280                 SysAssert(false);
281         }
282
283         return message;
284 }
285
286
287 void
288 _UserConfirmPopup::HandleUserAction(Eina_Bool allow)
289 {
290         switch (__userConfirmMode)
291         {
292         case USER_CONFIRM_USERMEDIA:
293         {
294                 Ewk_User_Media_Permission_Request* pPolicy = reinterpret_cast< Ewk_User_Media_Permission_Request* >(__pUserPolicyData);
295                 ewk_user_media_permission_request_set(pPolicy, allow);
296                 break;
297         }
298         case USER_PROTOCOL_HANDLER:
299         {
300                 RegisterHandler(false, allow);
301                 break;
302         }
303         case USER_CONTENT_HANDLER:
304         {
305                 RegisterHandler(true, allow);
306                 break;
307         }
308         case USER_CONFIRM_GEOLOCATION:
309         {
310                 Ewk_Geolocation_Permission_Request* pPolicy =  reinterpret_cast< Ewk_Geolocation_Permission_Request* >(__pUserPolicyData);
311                 if (__pCheckButton->IsSelected())
312                 {
313                         result r = AddGeolocationDb(pPolicy, static_cast < bool >(allow));
314                         SysTryCatch(NID_WEB_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
315                 }
316 CATCH:
317                 ewk_geolocation_permission_request_set(pPolicy, allow);
318                 break;
319         }
320         case USER_CONFIRM_NOTIFICATION:
321         {
322                 Ewk_Notification_Permission_Request* pPermissionRequest = reinterpret_cast< Ewk_Notification_Permission_Request* >(__pUserPolicyData);
323                 ewk_notification_permission_request_set(pPermissionRequest, allow);
324                 break;
325         }
326         case USER_SCRIPT_ALERT:
327         {
328                 Evas_Object* pView = reinterpret_cast< Evas_Object* >(__pUserPolicyData);
329                 ewk_view_javascript_alert_reply(pView);
330                 break;
331         }
332         default:
333                 SysAssert(false);
334         }
335 }
336
337
338 result
339 _UserConfirmPopup::AddGeolocationDb(Ewk_Geolocation_Permission_Request* pPolicy, bool enable)
340 {
341         _DatabaseImpl db;
342         String geolocationPath(Tizen::App::App::GetInstance()->GetAppRootPath() + CUSTOM_DB_DIRECTORY_PATH + USER_CONFIRM_DB_NAME);
343         String table(GEOLOCATION_TABLE_NAME);
344
345         const Ewk_Security_Origin* pSecurityOrigin = ewk_geolocation_permission_request_origin_get(pPolicy);
346         String origin = _Utility::CreateOrigin(pSecurityOrigin);
347
348         result r = db.Construct(geolocationPath, "r+", null);
349         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
350
351         std::unique_ptr<DbStatement> pStmt(db.CreateStatementN(L"Insert Into " + table + L" (origin, permission) Values (?, ?)"));
352         SysTryReturn(NID_WEB_CTRL, pStmt.get(), GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
353
354         SysLog(NID_WEB_CTRL, "The current value of stmt is %u, host is %ls", pStmt.get(), origin.GetPointer());
355
356         pStmt->BindString(0, origin);
357         pStmt->BindInt(1, static_cast < int >(enable));
358
359         db.BeginTransaction();
360
361         std::unique_ptr<DbEnumerator> pEnum(db.ExecuteStatementN(*pStmt));
362
363         db.CommitTransaction();
364
365         return E_SUCCESS;
366 }
367
368 void
369 _UserConfirmPopup::RegisterHandler(bool checkHandler, Eina_Bool allow)
370 {
371         _DatabaseImpl db;
372         String handlerPath(Tizen::App::App::GetInstance()->GetAppRootPath() + CUSTOM_DB_DIRECTORY_PATH + USER_CONFIRM_DB_NAME);
373         String table;
374         int checkId = 0;
375
376         if(checkHandler == true)
377         {
378                 table = CUSTOM_CONTENT_TABLE_NAME;
379         }
380         else
381         {
382                 table = CUSTOM_PROTOCOL_TABLE_NAME;
383         }
384         Ewk_Custom_Handlers_Data* pHandlerData = reinterpret_cast< Ewk_Custom_Handlers_Data* >(__pUserPolicyData);
385         SysAssertf(pHandlerData, "Failed to request");
386
387         String baseUri(ewk_custom_handlers_data_base_url_get(pHandlerData));
388         String uri = ewk_custom_handlers_data_url_get(pHandlerData);
389         String target = ewk_custom_handlers_data_target_get(pHandlerData);
390
391         result r = db.Construct(handlerPath, "r+", null);
392         SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
393
394         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"'"));
395
396         if(pEnum.get())
397         {
398                 pEnum->MoveNext();
399                 pEnum->GetIntAt(0, checkId);
400
401                 std::unique_ptr<DbStatement> pUpdateStmt(db.CreateStatementN(L"Update " + table + L" Set allow = (?) Where id = (?)"));
402                 SysTryReturnVoidResult(NID_WEB_CTRL, pUpdateStmt.get(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
403
404                 pUpdateStmt->BindInt(0, static_cast < int >(allow));
405                 pUpdateStmt->BindInt(1, checkId);
406
407                 db.BeginTransaction();
408
409                 std::unique_ptr<DbEnumerator> pEnum(db.ExecuteStatementN(*pUpdateStmt));
410
411                 db.CommitTransaction();
412
413         }
414         else
415         {
416                 std::unique_ptr<DbStatement> pStmt(db.CreateStatementN(L"Insert Into " + table + L" (baseUrl, url, mime, allow) Values (?, ?, ?, ?)"));
417                 SysTryReturnVoidResult(NID_WEB_CTRL, pStmt.get(),  GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
418
419                 pStmt->BindString(0, baseUri);
420                 pStmt->BindString(1, uri);
421                 pStmt->BindString(2, target);
422                 pStmt->BindInt(3, static_cast < int >(allow));
423
424                 db.BeginTransaction();
425
426                 std::unique_ptr<DbEnumerator> pRegisterEnum(db.ExecuteStatementN(*pStmt));
427
428                 db.CommitTransaction();
429         }
430 }
431
432 }}} // Tizen::Web::Controls