fix code for DCM-531 and crash 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 <FBaseColArrayList.h>
23 #include <FBaseSysLog.h>
24 #include <FBaseUtilUri.h>
25 #include <FGrpDimension.h>
26 #include <FGrpRectangle.h>
27 #include <FSecCertX509Certificate.h>
28 #include <FUiCtrlButton.h>
29 #include <FUiCtrlLabel.h>
30 #include <FUiCtrlPanel.h>
31 #include <FUiLayout.h>
32 #include <FUiVerticalBoxLayout.h>
33 #include <FUi_ControlManager.h>
34 #include <FUi_ResourceManager.h>
35 #include "FWebCtrl_CertificatePopup.h"
36 #include "FWebCtrl_UserConfirmPopup.h"
37 #include "FWebCtrl_Utility.h"
38
39
40 using namespace Tizen::Base;
41 using namespace Tizen::Base::Collection;
42 using namespace Tizen::Base::Utility;
43 using namespace Tizen::Graphics;
44 using namespace Tizen::Io;
45 using namespace Tizen::Security::Cert;
46 using namespace Tizen::Ui;
47 using namespace Tizen::Ui::Controls;
48
49
50 namespace Tizen { namespace Web { namespace Controls
51 {
52
53
54 _UserConfirmPopup::_UserConfirmPopup(void)
55         : __pUserPolicyData(null)
56         , __userConfirmMode(USER_CONFIRM_CERTIFICATE)
57         , __confirm(false)
58         , __sync(true)
59 {
60 }
61
62
63 _UserConfirmPopup::~_UserConfirmPopup(void)
64 {
65 }
66
67
68 result
69 _UserConfirmPopup::Construct(_UserConfirmMode userConfirmMode, void* pEventInfo, bool sync)
70 {
71         result r = E_SUCCESS;
72         SysTryReturnResult(NID_WEB_CTRL, pEventInfo, E_INVALID_ARG, "Invalid argument(s) is used. pPolicy is null.");
73         SysTryReturnResult(NID_WEB_CTRL, userConfirmMode >= USER_CONFIRM_CERTIFICATE && userConfirmMode <= USER_CONFIRM_USERMEDIA, E_INVALID_ARG,
74                                 "Invalid userConfirmMode is used. [%d]", userConfirmMode);
75
76         _WebPopupData* pPopupData = _WebPopup::GetPopupData();
77         SysTryReturn(NID_WEB_CTRL, pPopupData, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
78
79         __pUserPolicyData = pEventInfo;
80         __userConfirmMode = userConfirmMode;
81         __sync = sync;
82
83         int popupMaxHeight = 0;
84         popupMaxHeight =  2*pPopupData->labelDim.height + 2*pPopupData->btnDim.height + 6*pPopupData->sideMargin;
85
86         r = _WebPopup::Construct(true, Dimension(pPopupData->popupDim.width, popupMaxHeight));
87         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
88
89         SetTitleText("Security Warning");
90
91         Rectangle rect(0, 0, 0, 0);
92
93         //label
94         rect.height = 2*pPopupData->labelDim.height;
95         rect.width = pPopupData->labelDim.width;
96
97         std::unique_ptr<Label> pLabel(new (std::nothrow) Label());
98         SysTryReturnResult(NID_WEB_CTRL, pLabel.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
99
100         String message = GetMessageFromPolicy();
101         r = pLabel->Construct(rect, message);
102         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
103
104         pLabel->SetTextConfig(pPopupData->labelFontSize, LABEL_TEXT_STYLE_NORMAL);
105
106         r = AddControl(*pLabel);
107         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
108
109         Label* pInfoLabel = pLabel.release();
110
111         Panel* pButtonPanel = CreateAndAddPanel();
112         SysTryReturn(NID_WEB_CTRL, pButtonPanel, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
113
114         ArrayList idList;
115         r = idList.Construct();
116         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
117         idList.Add(*(new Integer(ID_BUTTON_USER_ALLOW)));
118         if (__userConfirmMode == USER_CONFIRM_CERTIFICATE)
119         {
120                 idList.Add(*(new Integer(ID_BUTTON_USER_CERT_VIEW)));
121         }
122         idList.Add(*(new Integer(ID_BUTTON_USER_CANCEL)));
123
124         ArrayList titleList;
125         r = titleList.Construct();
126         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
127         titleList.Add(*(new String(L"Allow")));
128         if (__userConfirmMode == USER_CONFIRM_CERTIFICATE)
129         {
130                 titleList.Add(*(new String(L"View")));
131         }
132         titleList.Add(*(new String(L"Cancel")));
133
134         r = CreateAndAddButtons(idList, titleList, pButtonPanel);
135         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
136
137         std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(GetLayoutN()));
138         SysTryReturn(NID_WEB_CTRL, pLayout.get(), r = GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
139
140         pLayout->SetHorizontalFitPolicy(*pInfoLabel, FIT_POLICY_PARENT);
141         pLayout->SetHorizontalFitPolicy(*pButtonPanel, FIT_POLICY_PARENT);
142
143         pLayout->SetHorizontalAlignment(*pInfoLabel, LAYOUT_HORIZONTAL_ALIGN_CENTER);
144         pLayout->SetHorizontalAlignment(*pButtonPanel, LAYOUT_HORIZONTAL_ALIGN_CENTER);
145
146         pLayout->SetSpacing(*pButtonPanel, 2*pPopupData->sideMargin);
147
148         return E_SUCCESS;
149 }
150
151
152 void
153 _UserConfirmPopup::OnActionPerformed(const Control& source, int actionId)
154 {
155         result r = E_SUCCESS;
156
157         switch (actionId)
158         {
159         case ID_BUTTON_USER_ALLOW:
160                 HandleUserAction(EINA_TRUE);
161                 break;
162
163         case ID_BUTTON_USER_CERT_VIEW:
164                 r = ShowCertificatePopup();
165                 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
166                 return;
167
168         case ID_BUTTON_USER_CANCEL:
169                 HandleUserAction(EINA_FALSE);
170                 break;
171
172         default:
173                 break;
174         }
175
176         r = HidePopup();
177         if (IsFailed(r))
178         {
179                 SysLogException(NID_WEB_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
180         }
181
182         if (!sync)
183         {
184                 delete this;
185         }
186 }
187
188
189 bool
190 _UserConfirmPopup::GetConfirmResult() const
191 {
192         return __confirm;
193 }
194
195
196 String
197 _UserConfirmPopup::GetMessageFromPolicy(void)
198 {
199         String message;
200
201         switch(__userConfirmMode)
202         {
203         case USER_CONFIRM_CERTIFICATE:
204         {
205                 Ewk_Certificate_Policy_Decision* pPolicy = reinterpret_cast< Ewk_Certificate_Policy_Decision* >(__pUserPolicyData);
206                 message = L"There are problems with the security certificate of this site.\n";
207                 message.Append(ewk_certificate_policy_decision_url_get(pPolicy));
208                 break;
209         }
210         case USER_CONFIRM_USERMEDIA:
211         {
212                 message = L"Do you want to allow acccess to media?\n";
213                 break;
214         }
215         default:
216                 SysAssert(false);
217         }
218
219         return message;
220 }
221
222
223 void
224 _UserConfirmPopup::HandleUserAction(Eina_Bool allow)
225 {
226         switch (__userConfirmMode)
227         {
228         case USER_CONFIRM_CERTIFICATE:
229         {
230                 Ewk_Certificate_Policy_Decision* pPolicy = reinterpret_cast< Ewk_Certificate_Policy_Decision* >(__pUserPolicyData);
231                 ewk_certificate_policy_decision_allowed_set(pPolicy, allow);
232                 break;
233         }
234         case USER_CONFIRM_USERMEDIA:
235         {
236                 Ewk_User_Media_Permission_Request* pPolicy = reinterpret_cast< Ewk_User_Media_Permission_Request* >(__pUserPolicyData);
237                 ewk_user_media_permission_request_set(pPolicy, allow);
238                 break;
239         }
240         default:
241                 SysAssert(false);
242         }
243
244         __confirm = static_cast< bool >(allow);
245 }
246
247
248 result
249 _UserConfirmPopup::ShowCertificatePopup(void)
250 {
251         //get Certificate information
252         Ewk_Certificate_Policy_Decision* pPolicy = reinterpret_cast< Ewk_Certificate_Policy_Decision* >(__pUserPolicyData);
253         SysTryReturnResult(NID_WEB_CTRL, pPolicy, E_SYSTEM, "Certificate policy is not set.");
254
255         std::unique_ptr<_CertificatePopup> pCertificatePopup(new (std::nothrow) _CertificatePopup());
256         SysTryReturnResult(NID_WEB_CTRL, pCertificatePopup.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
257
258         result r = pCertificatePopup->Construct(pPolicy);
259         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
260
261         r = pCertificatePopup->ShowPopup();
262         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
263
264         pCertificatePopup.release();
265         return E_SUCCESS;
266 }
267
268
269 }}} // Tizen::Web::Controls