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