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