Merge "Separated sync and async popup implementations" into tizen_2.1
[framework/osp/web.git] / src / controls / FWebCtrl_CertificateConfirmPopup.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_CertificateConfirmPopup.cpp
20  * @brief               The file contains the definition of _CertificateConfirmPopup 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_CertificateConfirmPopup.h"
36 #include "FWebCtrl_Utility.h"
37
38
39 using namespace Tizen::Base;
40 using namespace Tizen::Base::Collection;
41 using namespace Tizen::Base::Utility;
42 using namespace Tizen::Graphics;
43 using namespace Tizen::Io;
44 using namespace Tizen::Security::Cert;
45 using namespace Tizen::Ui;
46 using namespace Tizen::Ui::Controls;
47
48
49 namespace Tizen { namespace Web { namespace Controls
50 {
51
52
53 _CertificateConfirmPopup::_CertificateConfirmPopup(void)
54 {
55 }
56
57
58 _CertificateConfirmPopup::~_CertificateConfirmPopup(void)
59 {
60 }
61
62
63 result
64 _CertificateConfirmPopup::Construct(_CertificatePopupMode certPopupMode, Ewk_Certificate_Policy_Decision* pPolicy)
65 {
66         SysTryReturnResult(NID_WEB_CTRL, pPolicy, E_INVALID_ARG, "Certificate Policy pointer is null.");
67         result r = E_SUCCESS;
68
69         _WebPopupData* pPopupData = _WebPopup::GetPopupData();
70         SysTryReturn(NID_WEB_CTRL, pPopupData, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
71
72         __pCertificatePolicyData = pPolicy;
73         __certPopupMode = certPopupMode;
74         String titleText = L"";
75         int popupMaxHeight = 0;
76         Rectangle rect(0, 0, 0, 0);
77
78         ArrayList idList;
79         r = idList.Construct();
80         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
81
82         ArrayList titleList;
83         r = titleList.Construct();
84         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
85
86         if( __certPopupMode == CERTIFICATE_POPUP_MODE_VIEW )
87         {
88                 titleText = L"Certificate";
89                 popupMaxHeight = pPopupData->popupDim.height;
90
91                 rect.height = pPopupData->popupDim.height - 4*pPopupData->sideMargin - 2*pPopupData->btnDim.height;
92                 rect.width = pPopupData->labelDim.width;
93
94                 idList.Add(*(new Integer(ID_BUTTON_CERTIFICATE_CLOSE)));
95                 titleList.Add(*(new String(L"Close")));
96         }
97         else    // CERTIFICATE_POPUP_MODE_CONFIRM
98         {
99                 titleText = L"Security Warning";
100                 popupMaxHeight = 2*pPopupData->labelDim.height + 2*pPopupData->btnDim.height + 6*pPopupData->sideMargin;
101
102                 rect.height = 2*pPopupData->labelDim.height;
103                 rect.width = pPopupData->labelDim.width;
104
105                 idList.Add(*(new Integer(ID_BUTTON_CERTIFICATE_ALLOW)));
106                 idList.Add(*(new Integer(ID_BUTTON_CERTIFICATE_VIEW)));
107                 idList.Add(*(new Integer(ID_BUTTON_CERTIFICATE_CANCEL)));
108
109                 titleList.Add(*(new String(L"Allow")));
110                 titleList.Add(*(new String(L"View")));
111                 titleList.Add(*(new String(L"Cancel")));
112         }
113
114         r = _WebPopup::Construct(true, Dimension(pPopupData->popupDim.width, popupMaxHeight));
115         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
116
117         SetTitleText(titleText);
118
119         std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(GetLayoutN()));
120         SysTryReturn(NID_WEB_CTRL, pLayout.get(), r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
121
122         if( certPopupMode == CERTIFICATE_POPUP_MODE_VIEW )
123         {
124                 String certString;
125                 result r = GenerateCertifiate(certString);
126                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
127
128                 std::unique_ptr<TextBox> pTextBox(new (std::nothrow) TextBox());
129                 SysTryReturnResult(NID_WEB_CTRL, pTextBox.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
130
131                 r = pTextBox->Construct(rect, TEXT_BOX_BORDER_ROUNDED);
132                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
133
134                 r = pTextBox->SetTextSize(30);
135                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
136
137                 r = pTextBox->SetAutoLinkMask(LINK_TYPE_NONE);
138                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
139
140                 r = pTextBox->SetText(certString);
141                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
142
143                 r = AddControl(*pTextBox);
144                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
145
146                 TextBox* pCertBox = pTextBox.release();
147                 pLayout->SetHorizontalFitPolicy(*pCertBox, FIT_POLICY_PARENT);
148         }
149         else    // CERTIFICATE_POPUP_MODE_CONFIRM
150         {
151                 std::unique_ptr<Label> pLabel(new (std::nothrow) Label());
152                 SysTryReturnResult(NID_WEB_CTRL, pLabel.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
153
154                 String message = L"";
155                 message = L"There are problems with the security certificate of this site.\n";
156                 message.Append(ewk_certificate_policy_decision_url_get(__pCertificatePolicyData));
157
158                 r = pLabel->Construct(rect, message);
159                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
160
161                 pLabel->SetTextConfig(pPopupData->labelFontSize, LABEL_TEXT_STYLE_NORMAL);
162
163                 r = AddControl(*pLabel);
164                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
165
166                 Label* pInfoLabel = pLabel.release();
167                 pLayout->SetHorizontalFitPolicy(*pInfoLabel, FIT_POLICY_PARENT);
168         }
169         Panel* pButtonPanel = CreateAndAddPanel();
170         SysTryReturn(NID_WEB_CTRL, pButtonPanel, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
171
172         r = CreateAndAddButtons(idList, titleList, pButtonPanel);
173         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
174
175         pLayout->SetHorizontalFitPolicy(*pButtonPanel, FIT_POLICY_PARENT);
176         pLayout->SetHorizontalAlignment(*pButtonPanel, LAYOUT_HORIZONTAL_ALIGN_CENTER);
177
178         pLayout->SetSpacing(*pButtonPanel, 2*pPopupData->sideMargin);
179
180         return E_SUCCESS;
181 }
182
183
184 void
185 _CertificateConfirmPopup::OnActionPerformed(const Control& source, int actionId)
186 {
187         result r = E_SUCCESS;
188
189         switch (actionId)
190         {
191         case ID_BUTTON_CERTIFICATE_ALLOW:
192                 ewk_certificate_policy_decision_allowed_set(__pCertificatePolicyData, EINA_TRUE);
193                 __confirm = static_cast< bool >(EINA_TRUE);
194                 break;
195
196         case ID_BUTTON_CERTIFICATE_VIEW:
197         {
198                 std::unique_ptr<_CertificateConfirmPopup> pCertificatePopup(new (std::nothrow) _CertificateConfirmPopup());
199                 SysTryReturnVoidResult(NID_WEB_CTRL, pCertificatePopup.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
200
201                 r = pCertificatePopup->Construct(CERTIFICATE_POPUP_MODE_VIEW, __pCertificatePolicyData);
202                 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
203
204                 r = pCertificatePopup->ShowPopup();
205                 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
206
207                 pCertificatePopup.release();
208                 return;
209         }
210
211         case ID_BUTTON_CERTIFICATE_CANCEL:
212                 ewk_certificate_policy_decision_allowed_set(__pCertificatePolicyData, EINA_FALSE);
213                 __confirm = static_cast< bool >(EINA_FALSE);
214                 break;
215
216         case ID_BUTTON_CERTIFICATE_CLOSE:
217                 break;
218
219         default:
220                 SysAssert(false);
221                 break;
222         }
223
224         r = HidePopup();
225         if (IsFailed(r))
226         {
227                 SysLogException(NID_WEB_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
228         }
229         if (__certPopupMode == CERTIFICATE_POPUP_MODE_VIEW )
230         {
231                 delete this;
232         }
233 }
234
235
236 bool
237 _CertificateConfirmPopup::GetConfirmResult() const
238 {
239         return __confirm;
240 }
241
242
243 result
244 _CertificateConfirmPopup::GenerateCertifiate(String& certString)
245 {
246         SysTryReturnResult(NID_WEB_CTRL, __pCertificatePolicyData, E_INVALID_ARG, "Certificate Policy pointer is null.");
247
248         String pemString(ewk_certificate_policy_decision_certificate_pem_get(__pCertificatePolicyData));
249         std::unique_ptr<ByteBuffer> pByteBuf(StringUtil::StringToUtf8N(pemString));
250         SysTryReturnResult(NID_WEB_CTRL, pByteBuf.get(), E_INVALID_DATA, "Certificate pem information is Empty.");
251
252         X509Certificate certificate;
253         result r = certificate.Construct(*pByteBuf);
254         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
255
256         //Issued to
257         certString.Append(L"ISSUED TO:\n\n");
258         String subject = certificate.GetSubject();
259
260         certString.Append(L"Common Name:\n");
261         certString.Append(GetStringOfToken(subject, L"/CN=") + L"\n");
262
263         certString.Append(L"Organization:\n");
264         certString.Append(GetStringOfToken(subject, L"/O=") + L"\n");
265
266         certString.Append(L"Organizational Unit:\n");
267         certString.Append(GetStringOfToken(subject, L"/OU=") + L"\n");
268
269         certString.Append(L"Serial Number:\n");
270         certString.Append(certificate.GetSerialNumber() + L"\n\n\n");
271
272         //Issued by
273         certString.Append(L"ISSUER:\n\n");
274         String issuer = certificate.GetIssuer();
275
276         certString.Append(L"Common Name:\n");
277         certString.Append(GetStringOfToken(issuer, L"/CN=") + L"\n");
278
279         certString.Append(L"Organization:\n");
280         certString.Append(GetStringOfToken(issuer, L"/O=") + L"\n");
281
282         certString.Append(L"Organizational Unit:\n");
283         certString.Append(GetStringOfToken(issuer, L"/OU=") + L"\n\n");
284
285         //Validity
286         certString.Append(L"VALIDITY:\n\n");
287         certString.Append(L"Valid From:\n");
288         certString.Append(certificate.GetNotBefore() + L"\n\n");
289
290         certString.Append(L"Valid Till:\n");
291         certString.Append(certificate.GetNotAfter() + L"\n\n\n");
292
293         //FingerPrints
294         certString.Append(L"FINGERPRINTS:\n\n");
295         certString.Append(L"Signature Algorithm:\n");
296         certString.Append(certificate.GetSignatureAlgorithm() + L"\n\n");
297
298         std::unique_ptr<ByteBuffer> pFingerPrint(certificate.GetFingerprintN());
299         if (pFingerPrint.get() && pFingerPrint->GetPointer())
300         {
301                 String fingerPrint;
302                 StringUtil::Utf8ToString((const char*)pFingerPrint->GetPointer(), fingerPrint);
303                 certString.Append(fingerPrint + L"\n\n");
304         }
305
306         return E_SUCCESS;
307 }
308
309
310 String
311 _CertificateConfirmPopup::GetStringOfToken(const String& parseString, const String& parseToken)
312 {
313         String inString(parseString);
314         int index = 0;
315
316         String outString;
317         outString.Append(L"\n");
318
319         result r = inString.IndexOf(parseToken, 0, index);
320         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, outString, r, "[%s] Propagating.", GetErrorMessage(r));
321
322         int prsTokLen = parseToken.GetLength();
323         r = inString.Remove(0, index + prsTokLen);
324         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, outString, r, "[%s] Propagating.", GetErrorMessage(r));
325
326         while (true)
327         {
328                 r = inString.IndexOf(parseToken, 0, index);
329
330                 switch (r)
331                 {
332                 case E_SUCCESS:
333                         r = inString.Remove(index, prsTokLen);
334                         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, outString, r, "[%s] Propagating.", GetErrorMessage(r));
335
336                         inString.Insert(L"\n", index);
337                         continue;
338
339                 case E_OBJ_NOT_FOUND:
340                         r = inString.IndexOf('=', 0, index);
341                         if (r == E_SUCCESS)
342                         {
343                                 int slashIndex = 0;
344                                 r = inString.LastIndexOf('/', index - 3, slashIndex);
345
346                                 if (!IsFailed(r) && (slashIndex == index - 2 || slashIndex == index - 3))
347                                 {
348                                         inString.Remove(slashIndex, inString.GetLength() - slashIndex);
349                                 }
350                         }
351                         inString.Append(L"\n");
352                         outString = inString;
353                         return outString;
354
355                 default:
356                         return outString;
357                 }
358         }
359 }
360
361
362 }}} // Tizen::Web::Controls