2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FWebCtrl_UserConfirmPopup.cpp
20 * @brief The file contains the definition of _UserConfirmPopup class.
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"
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;
54 namespace Tizen { namespace Web { namespace Controls
58 _UserConfirmPopup::_UserConfirmPopup(void)
59 : __pUserPolicyData(null)
60 , __userConfirmMode(USER_CONFIRM_USERMEDIA)
61 , __pCheckButton(null)
67 _UserConfirmPopup::~_UserConfirmPopup(void)
73 _UserConfirmPopup::Construct(_UserConfirmMode userConfirmMode, void* pEventInfo, bool sync)
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);
80 _WebPopupData* pPopupData = _WebPopup::GetPopupData();
81 SysTryReturn(NID_WEB_CTRL, pPopupData, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
83 __pUserPolicyData = pEventInfo;
84 __userConfirmMode = userConfirmMode;
87 int popupMaxHeight = 2*pPopupData->labelDim.height + 2*pPopupData->btnDim.height + 6*pPopupData->sideMargin;
88 if (__userConfirmMode == USER_CONFIRM_GEOLOCATION)
90 popupMaxHeight += pPopupData->checkDim.height;
93 r = _WebPopup::Construct(true, Dimension(pPopupData->popupDim.width, popupMaxHeight));
94 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
96 SetTitleText("Security Warning");
98 Rectangle rect(0, 0, 0, 0);
101 rect.height = 2*pPopupData->labelDim.height;
102 rect.width = pPopupData->labelDim.width;
104 std::unique_ptr<Label> pLabel(new (std::nothrow) Label());
105 SysTryReturnResult(NID_WEB_CTRL, pLabel.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
107 String message = GetMessageFromPolicy();
108 r = pLabel->Construct(rect, message);
109 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
111 pLabel->SetTextConfig(pPopupData->labelFontSize, LABEL_TEXT_STYLE_NORMAL);
113 r = AddControl(*pLabel);
114 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
116 Label* pInfoLabel = pLabel.release();
118 if (__userConfirmMode == USER_CONFIRM_GEOLOCATION)
121 rect.height = pPopupData->checkDim.height;
123 std::unique_ptr<CheckButton> pCheckButton (new (std::nothrow) CheckButton());
124 SysTryReturnResult(NID_WEB_CTRL, pCheckButton.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
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));
129 r = AddControl(*pCheckButton);
130 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
132 __pCheckButton = pCheckButton.release();
135 Panel* pButtonPanel = CreateAndAddPanel();
136 SysTryReturn(NID_WEB_CTRL, pButtonPanel, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
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)));
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")));
150 r = CreateAndAddButtons(idList, titleList, pButtonPanel);
151 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
153 std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(GetLayoutN()));
154 SysTryReturn(NID_WEB_CTRL, pLayout.get(), r = GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
156 pLayout->SetHorizontalAlignment(*pInfoLabel, LAYOUT_HORIZONTAL_ALIGN_CENTER);
157 if (__userConfirmMode == USER_CONFIRM_GEOLOCATION)
159 pLayout->SetHorizontalAlignment(*__pCheckButton, LAYOUT_HORIZONTAL_ALIGN_CENTER);
161 pLayout->SetHorizontalAlignment(*pButtonPanel, LAYOUT_HORIZONTAL_ALIGN_CENTER);
163 pLayout->SetSpacing(*pButtonPanel, 2*pPopupData->sideMargin);
170 _UserConfirmPopup::OnActionPerformed(const Control& source, int actionId)
172 result r = E_SUCCESS;
176 case ID_BUTTON_USER_ALLOW:
177 HandleUserAction(EINA_TRUE);
180 case ID_BUTTON_USER_CANCEL:
181 HandleUserAction(EINA_FALSE);
191 SysLogException(NID_WEB_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
202 _UserConfirmPopup::GetMessageFromPolicy(void)
206 switch(__userConfirmMode)
208 case USER_CONFIRM_USERMEDIA:
210 message = L"Do you want to allow acccess to media?\n";
213 case USER_PROTOCOL_HANDLER:
215 Ewk_Custom_Handlers_Data* pHandlerData = reinterpret_cast< Ewk_Custom_Handlers_Data* >(__pUserPolicyData);
216 SysAssertf(pHandlerData, "Failed to request");
218 String target(ewk_custom_handlers_data_target_get(pHandlerData));
219 String baseUri(ewk_custom_handlers_data_base_url_get(pHandlerData));
221 message = baseUri + String(L" is asking to register ") + target + String(L" protocol handler.");
224 case USER_CONTENT_HANDLER:
226 Ewk_Custom_Handlers_Data* pHandlerData = reinterpret_cast< Ewk_Custom_Handlers_Data* >(__pUserPolicyData);
227 SysAssertf(pHandlerData, "Failed to request");
229 String target(ewk_custom_handlers_data_target_get(pHandlerData));
230 String baseUri(ewk_custom_handlers_data_base_url_get(pHandlerData));
232 message = String(baseUri) + String(L" is asking to register ") + String(target) + String(L" content handler.");
235 case USER_CONFIRM_GEOLOCATION:
237 Ewk_Geolocation_Permission_Request* pGeoLocPermReq = reinterpret_cast< Ewk_Geolocation_Permission_Request* >(__pUserPolicyData);
238 const Ewk_Security_Origin* pSecurityOrigin = ewk_geolocation_permission_request_origin_get(pGeoLocPermReq);
240 message = _Utility::CreateOrigin(pSecurityOrigin);
241 message.Append(" Requests your Location");
253 _UserConfirmPopup::HandleUserAction(Eina_Bool allow)
255 switch (__userConfirmMode)
257 case USER_CONFIRM_USERMEDIA:
259 Ewk_User_Media_Permission_Request* pPolicy = reinterpret_cast< Ewk_User_Media_Permission_Request* >(__pUserPolicyData);
260 ewk_user_media_permission_request_set(pPolicy, allow);
263 case USER_PROTOCOL_HANDLER:
265 RegisterHandler(false, allow);
268 case USER_CONTENT_HANDLER:
270 RegisterHandler(true, allow);
273 case USER_CONFIRM_GEOLOCATION:
275 Ewk_Geolocation_Permission_Request* pPolicy = reinterpret_cast< Ewk_Geolocation_Permission_Request* >(__pUserPolicyData);
276 if (__pCheckButton->IsSelected())
278 result r = AddGeolocationDb(pPolicy, static_cast < bool >(allow));
279 SysTryCatch(NID_WEB_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
282 ewk_geolocation_permission_request_set(pPolicy, allow);
292 _UserConfirmPopup::AddGeolocationDb(Ewk_Geolocation_Permission_Request* pPolicy, bool enable)
295 String geolocationPath(Tizen::App::App::GetInstance()->GetAppRootPath() + CUSTOM_DB_DIRECTORY_PATH + USER_CONFIRM_DB_NAME);
296 String table(GEOLOCATION_TABLE_NAME);
298 const Ewk_Security_Origin* pSecurityOrigin = ewk_geolocation_permission_request_origin_get(pPolicy);
299 String origin = _Utility::CreateOrigin(pSecurityOrigin);
301 result r = db.Construct(geolocationPath, "r+", null);
302 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
304 std::unique_ptr<DbStatement> pStmt(db.CreateStatementN(L"Insert Into " + table + L" (origin, permission) Values (?, ?)"));
305 SysTryReturn(NID_WEB_CTRL, pStmt.get(), GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
307 SysLog(NID_WEB_CTRL, "The current value of stmt is %u, host is %ls", pStmt.get(), origin.GetPointer());
309 pStmt->BindString(0, origin);
310 pStmt->BindInt(1, static_cast < int >(enable));
312 db.BeginTransaction();
314 std::unique_ptr<DbEnumerator> pEnum(db.ExecuteStatementN(*pStmt));
316 db.CommitTransaction();
322 _UserConfirmPopup::RegisterHandler(bool checkHandler, Eina_Bool allow)
325 String handlerPath(Tizen::App::App::GetInstance()->GetAppRootPath() + CUSTOM_DB_DIRECTORY_PATH + USER_CONFIRM_DB_NAME);
329 if(checkHandler == true)
331 table = CUSTOM_CONTENT_TABLE_NAME;
335 table = CUSTOM_PROTOCOL_TABLE_NAME;
337 Ewk_Custom_Handlers_Data* pHandlerData = reinterpret_cast< Ewk_Custom_Handlers_Data* >(__pUserPolicyData);
338 SysAssertf(pHandlerData, "Failed to request");
340 String baseUri(ewk_custom_handlers_data_base_url_get(pHandlerData));
341 String uri = ewk_custom_handlers_data_url_get(pHandlerData);
342 String target = ewk_custom_handlers_data_target_get(pHandlerData);
344 result r = db.Construct(handlerPath, "r+", null);
345 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
347 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"'"));
352 pEnum->GetIntAt(0, checkId);
354 std::unique_ptr<DbStatement> pUpdateStmt(db.CreateStatementN(L"Update " + table + L" Set allow = (?) Where id = (?)"));
355 SysTryReturnVoidResult(NID_WEB_CTRL, pUpdateStmt.get(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
357 pUpdateStmt->BindInt(0, static_cast < int >(allow));
358 pUpdateStmt->BindInt(1, checkId);
360 db.BeginTransaction();
362 std::unique_ptr<DbEnumerator> pEnum(db.ExecuteStatementN(*pUpdateStmt));
364 db.CommitTransaction();
369 std::unique_ptr<DbStatement> pStmt(db.CreateStatementN(L"Insert Into " + table + L" (baseUrl, url, mime, allow) Values (?, ?, ?, ?)"));
370 SysTryReturnVoidResult(NID_WEB_CTRL, pStmt.get(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
372 pStmt->BindString(0, baseUri);
373 pStmt->BindString(1, uri);
374 pStmt->BindString(2, target);
375 pStmt->BindInt(3, static_cast < int >(allow));
377 db.BeginTransaction();
379 std::unique_ptr<DbEnumerator> pRegisterEnum(db.ExecuteStatementN(*pStmt));
381 db.CommitTransaction();
385 }}} // Tizen::Web::Controls