Fix for prevent issues
[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 <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 {
63 }
64
65
66 _UserConfirmPopup::~_UserConfirmPopup(void)
67 {
68 }
69
70
71 result
72 _UserConfirmPopup::Construct(_UserConfirmMode userConfirmMode, void* pEventInfo)
73 {
74         result r = E_SUCCESS;
75         SysTryReturnResult(NID_WEB_CTRL, pEventInfo, E_INVALID_ARG, "Invalid argument(s) is used. pPolicy is null.");
76         SysTryReturnResult(NID_WEB_CTRL, userConfirmMode >= USER_CONFIRM_USERMEDIA && userConfirmMode <= USER_CONFIRM_GEOLOCATION, E_INVALID_ARG,
77                                         "Invalid userConfirmMode is used. [%d]", userConfirmMode);
78
79         _WebPopupData* pPopupData = _WebPopup::GetPopupData();
80         SysTryReturn(NID_WEB_CTRL, pPopupData, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
81
82         __pUserPolicyData = pEventInfo;
83         __userConfirmMode = userConfirmMode;
84
85         int popupMaxHeight = 2*pPopupData->labelDim.height + 2*pPopupData->btnDim.height + 6*pPopupData->sideMargin;
86         if (__userConfirmMode == USER_CONFIRM_GEOLOCATION)
87         {
88                 popupMaxHeight +=  pPopupData->checkDim.height;
89         }
90
91         r = _WebPopup::Construct(true, Dimension(pPopupData->popupDim.width, popupMaxHeight));
92         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
93
94         SetTitleText("Security Warning");
95
96         Rectangle rect(0, 0, 0, 0);
97
98         //label
99         rect.height = 2*pPopupData->labelDim.height;
100         rect.width = pPopupData->labelDim.width;
101
102         std::unique_ptr<Label> pLabel(new (std::nothrow) Label());
103         SysTryReturnResult(NID_WEB_CTRL, pLabel.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
104
105         String message = GetMessageFromPolicy();
106         r = pLabel->Construct(rect, message);
107         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
108
109         pLabel->SetTextConfig(pPopupData->labelFontSize, LABEL_TEXT_STYLE_NORMAL);
110
111         r = AddControl(*pLabel);
112         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
113
114         Label* pInfoLabel = pLabel.release();
115         //checkbutton
116         if (__userConfirmMode == USER_CONFIRM_GEOLOCATION)
117         {
118                 rect.y = 0;
119                 rect.height = pPopupData->checkDim.height;
120
121                 std::unique_ptr<CheckButton> pCheckButton (new (std::nothrow) CheckButton());
122                 SysTryReturnResult(NID_WEB_CTRL, pCheckButton.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
123
124                 r = pCheckButton->Construct(Rectangle(0, 0, rect.width, rect.height), CHECK_BUTTON_STYLE_MARK, BACKGROUND_STYLE_DEFAULT, false, L"Remember Preference");
125                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
126
127                 r = AddControl(*pCheckButton);
128                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
129
130                 __pCheckButton = pCheckButton.release();
131         }
132
133         Panel* pButtonPanel = CreateAndAddPanel();
134         SysTryReturn(NID_WEB_CTRL, pButtonPanel, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
135
136         ArrayList idList;
137         r = idList.Construct();
138         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
139         idList.Add(*(new Integer(ID_BUTTON_USER_ALLOW)));
140         idList.Add(*(new Integer(ID_BUTTON_USER_CANCEL)));
141
142         ArrayList titleList;
143         r = titleList.Construct();
144         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
145         titleList.Add(*(new String(L"Allow")));
146         titleList.Add(*(new String(L"Cancel")));
147
148         r = CreateAndAddButtons(idList, titleList, pButtonPanel);
149         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
150
151         std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(GetLayoutN()));
152         SysTryReturn(NID_WEB_CTRL, pLayout.get(), r = GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
153
154         pLayout->SetHorizontalFitPolicy(*pInfoLabel, FIT_POLICY_PARENT);
155         if (__userConfirmMode == USER_CONFIRM_GEOLOCATION)
156         {
157                 pLayout->SetHorizontalFitPolicy(*__pCheckButton, FIT_POLICY_PARENT);
158         }
159         pLayout->SetHorizontalFitPolicy(*pButtonPanel, FIT_POLICY_PARENT);
160
161         pLayout->SetHorizontalAlignment(*pInfoLabel, LAYOUT_HORIZONTAL_ALIGN_CENTER);
162         if (__userConfirmMode == USER_CONFIRM_GEOLOCATION)
163         {
164                 pLayout->SetHorizontalAlignment(*__pCheckButton, LAYOUT_HORIZONTAL_ALIGN_CENTER);
165         }
166         pLayout->SetHorizontalAlignment(*pButtonPanel, LAYOUT_HORIZONTAL_ALIGN_CENTER);
167
168         pLayout->SetSpacing(*pButtonPanel, 2*pPopupData->sideMargin);
169
170         return E_SUCCESS;
171 }
172
173
174 void
175 _UserConfirmPopup::OnActionPerformed(const Control& source, int actionId)
176 {
177         result r = E_SUCCESS;
178
179         switch (actionId)
180         {
181         case ID_BUTTON_USER_ALLOW:
182                 HandleUserAction(EINA_TRUE);
183                 break;
184
185         case ID_BUTTON_USER_CANCEL:
186                 HandleUserAction(EINA_FALSE);
187                 break;
188
189         default:
190                 break;
191         }
192
193         r = HidePopup();
194         if (IsFailed(r))
195         {
196                 SysLogException(NID_WEB_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
197         }
198
199         delete this;
200 }
201
202
203 String
204 _UserConfirmPopup::GetMessageFromPolicy(void)
205 {
206         String message;
207
208         switch(__userConfirmMode)
209         {
210         case USER_CONFIRM_USERMEDIA:
211         {
212                 message = L"Do you want to allow acccess to media?\n";
213                 break;
214         }
215         case USER_CONFIRM_GEOLOCATION:
216         {
217                 Ewk_Geolocation_Permission_Request* pGeoLocPermReq = reinterpret_cast< Ewk_Geolocation_Permission_Request* >(__pUserPolicyData);
218                 const Ewk_Security_Origin* pSecurityOrigin = ewk_geolocation_permission_request_origin_get(pGeoLocPermReq);
219
220                 message = _Utility::CreateOrigin(pSecurityOrigin);
221                 message.Append("Requests your Location");
222                 break;
223         }
224         default:
225                 SysAssert(false);
226         }
227
228         return message;
229 }
230
231
232 void
233 _UserConfirmPopup::HandleUserAction(Eina_Bool allow)
234 {
235         switch (__userConfirmMode)
236         {
237         case USER_CONFIRM_USERMEDIA:
238         {
239                 Ewk_User_Media_Permission_Request* pPolicy = reinterpret_cast< Ewk_User_Media_Permission_Request* >(__pUserPolicyData);
240                 ewk_user_media_permission_request_set(pPolicy, allow);
241                 break;
242         }
243         case USER_CONFIRM_GEOLOCATION:
244         {
245                 Ewk_Geolocation_Permission_Request* pPolicy =  reinterpret_cast< Ewk_Geolocation_Permission_Request* >(__pUserPolicyData);
246                 ewk_geolocation_permission_request_set(pPolicy, allow);
247                 if (__pCheckButton->IsSelected())
248                 {
249                         result r = AddGeolocationDb(pPolicy, static_cast < bool >(allow));
250                         SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
251                 }
252                 break;
253         }
254         default:
255                 SysAssert(false);
256         }
257 }
258
259
260 result
261 _UserConfirmPopup::AddGeolocationDb(Ewk_Geolocation_Permission_Request* pPolicy, bool enable)
262 {
263         _DatabaseImpl db;
264         String geolocationPath(Tizen::App::App::GetInstance()->GetAppRootPath() + CUSTOM_DB_DIRECTORY_PATH + USER_CONFIRM_DB_NAME);
265         String table(GEOLOCATION_TABLE_NAME);
266
267         const Ewk_Security_Origin* pSecurityOrigin = ewk_geolocation_permission_request_origin_get(pPolicy);
268         String origin = _Utility::CreateOrigin(pSecurityOrigin);
269
270         result r = db.Construct(geolocationPath, "r+", null);
271         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
272
273         std::unique_ptr<DbStatement> pStmt(db.CreateStatementN(L"Insert Into " + table + L" (origin, permission) Values (?, ?)"));
274         SysTryReturn(NID_WEB_CTRL, pStmt.get(), GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
275
276         SysLog(NID_WEB_CTRL, "The current value of stmt is %u, host is %ls", pStmt.get(), origin.GetPointer());
277
278         pStmt->BindString(0, origin);
279         pStmt->BindInt(1, static_cast < int >(enable));
280
281         db.BeginTransaction();
282
283         std::unique_ptr<DbEnumerator> pEnum(db.ExecuteStatementN(*pStmt));
284
285         db.CommitTransaction();
286
287         return E_SUCCESS;
288 }
289
290 }}} // Tizen::Web::Controls