fix once crash issue
[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->SetHorizontalAlignment(*pCertBox, LAYOUT_HORIZONTAL_ALIGN_CENTER);
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                 pCertificatePopup->SetOwner(this);
205                 
206                 r = pCertificatePopup->ShowPopup();
207                 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
208
209                 pCertificatePopup.release();
210                 return;
211         }
212
213         case ID_BUTTON_CERTIFICATE_CANCEL:
214                 ewk_certificate_policy_decision_allowed_set(__pCertificatePolicyData, EINA_FALSE);
215                 __confirm = static_cast< bool >(EINA_FALSE);
216                 break;
217
218         case ID_BUTTON_CERTIFICATE_CLOSE:
219                 break;
220
221         default:
222                 SysAssert(false);
223                 break;
224         }
225
226         r = HidePopup();
227         if (IsFailed(r))
228         {
229                 SysLogException(NID_WEB_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
230         }
231         if (__certPopupMode == CERTIFICATE_POPUP_MODE_VIEW )
232         {
233                 delete this;
234         }
235 }
236
237
238 bool
239 _CertificateConfirmPopup::GetConfirmResult() const
240 {
241         return __confirm;
242 }
243
244
245 result
246 _CertificateConfirmPopup::GenerateCertifiate(String& certString)
247 {
248         SysTryReturnResult(NID_WEB_CTRL, __pCertificatePolicyData, E_INVALID_ARG, "Certificate Policy pointer is null.");
249
250         String pemString(ewk_certificate_policy_decision_certificate_pem_get(__pCertificatePolicyData));
251         std::unique_ptr<ByteBuffer> pByteBuf(StringUtil::StringToUtf8N(pemString));
252         SysTryReturnResult(NID_WEB_CTRL, pByteBuf.get(), E_INVALID_DATA, "Certificate pem information is Empty.");
253
254         X509Certificate certificate;
255         result r = certificate.Construct(*pByteBuf);
256         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
257
258         //Issued to
259         certString.Append(L"ISSUED TO:\n\n");
260         String subject = certificate.GetSubject();
261
262         certString.Append(L"Common Name:\n");
263         certString.Append(GetStringOfToken(subject, L"/CN=") + L"\n");
264
265         certString.Append(L"Organization:\n");
266         certString.Append(GetStringOfToken(subject, L"/O=") + L"\n");
267
268         certString.Append(L"Organizational Unit:\n");
269         certString.Append(GetStringOfToken(subject, L"/OU=") + L"\n");
270
271         certString.Append(L"Serial Number:\n");
272         certString.Append(certificate.GetSerialNumber() + L"\n\n\n");
273
274         //Issued by
275         certString.Append(L"ISSUER:\n\n");
276         String issuer = certificate.GetIssuer();
277
278         certString.Append(L"Common Name:\n");
279         certString.Append(GetStringOfToken(issuer, L"/CN=") + L"\n");
280
281         certString.Append(L"Organization:\n");
282         certString.Append(GetStringOfToken(issuer, L"/O=") + L"\n");
283
284         certString.Append(L"Organizational Unit:\n");
285         certString.Append(GetStringOfToken(issuer, L"/OU=") + L"\n\n");
286
287         //Validity
288         certString.Append(L"VALIDITY:\n\n");
289         certString.Append(L"Valid From:\n");
290         certString.Append(certificate.GetNotBefore() + L"\n\n");
291
292         certString.Append(L"Valid Till:\n");
293         certString.Append(certificate.GetNotAfter() + L"\n\n\n");
294
295         //FingerPrints
296         certString.Append(L"FINGERPRINTS:\n\n");
297         certString.Append(L"Signature Algorithm:\n");
298         certString.Append(certificate.GetSignatureAlgorithm() + L"\n\n");
299
300         std::unique_ptr<ByteBuffer> pFingerPrint(certificate.GetFingerprintN());
301         if (pFingerPrint.get() && pFingerPrint->GetPointer())
302         {
303                 String fingerPrint;
304                 StringUtil::Utf8ToString((const char*)pFingerPrint->GetPointer(), fingerPrint);
305                 certString.Append(fingerPrint + L"\n\n");
306         }
307
308         return E_SUCCESS;
309 }
310
311
312 String
313 _CertificateConfirmPopup::GetStringOfToken(const String& parseString, const String& parseToken)
314 {
315         String inString(parseString);
316         int index = 0;
317
318         String outString;
319         outString.Append(L"\n");
320
321         result r = inString.IndexOf(parseToken, 0, index);
322         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, outString, r, "[%s] Propagating.", GetErrorMessage(r));
323
324         int prsTokLen = parseToken.GetLength();
325         r = inString.Remove(0, index + prsTokLen);
326         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, outString, r, "[%s] Propagating.", GetErrorMessage(r));
327
328         while (true)
329         {
330                 r = inString.IndexOf(parseToken, 0, index);
331
332                 switch (r)
333                 {
334                 case E_SUCCESS:
335                         r = inString.Remove(index, prsTokLen);
336                         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, outString, r, "[%s] Propagating.", GetErrorMessage(r));
337
338                         inString.Insert(L"\n", index);
339                         continue;
340
341                 case E_OBJ_NOT_FOUND:
342                         r = inString.IndexOf('=', 0, index);
343                         if (r == E_SUCCESS)
344                         {
345                                 int slashIndex = 0;
346                                 r = inString.LastIndexOf('/', index - 3, slashIndex);
347
348                                 if (!IsFailed(r) && (slashIndex == index - 2 || slashIndex == index - 3))
349                                 {
350                                         inString.Remove(slashIndex, inString.GetLength() - slashIndex);
351                                 }
352                         }
353                         inString.Append(L"\n");
354                         outString = inString;
355                         return outString;
356
357                 default:
358                         return outString;
359                 }
360         }
361 }
362
363
364 }}} // Tizen::Web::Controls