2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FWebCtrl_CertificateConfirmPopup.cpp
20 * @brief The file contains the definition of _CertificateConfirmPopup class.
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 <FUiKeyEventInfo.h>
35 #include <FUiLayout.h>
36 #include <FUiVerticalBoxLayout.h>
37 #include <FIo_DatabaseImpl.h>
38 #include <FSys_SystemResource.h>
39 #include <FUi_ControlManager.h>
40 #include <FUi_ResourceManager.h>
41 #include "FWebCtrl_CertificateConfirmPopup.h"
42 #include "FWebCtrl_WebManager.h"
43 #include "FWebCtrl_Utility.h"
44 #include "FWebCtrl_WebImpl.h"
47 using namespace Tizen::Base;
48 using namespace Tizen::Base::Collection;
49 using namespace Tizen::Base::Utility;
50 using namespace Tizen::Graphics;
51 using namespace Tizen::Io;
52 using namespace Tizen::Security::Cert;
53 using namespace Tizen::System;
54 using namespace Tizen::Ui;
55 using namespace Tizen::Ui::Controls;
58 namespace Tizen { namespace Web { namespace Controls
62 static const int EDIT_TEXT_SIZE = 30;
65 _CertificateConfirmPopup::_CertificateConfirmPopup(void)
66 : __certPopupMode(CERTIFICATE_POPUP_MODE_USER_CONFIRM)
68 , __pCertificatePolicyData(null)
70 , __pCertificatePopup(null)
76 _CertificateConfirmPopup::~_CertificateConfirmPopup(void)
78 if (IsModalPopup() == true)
80 HandleUserAction(false);
82 if (__pCertificatePopup.get())
84 __pCertificatePopup.reset();
90 _CertificateConfirmPopup::Construct(_CertificatePopupMode certPopupMode, Ewk_Certificate_Policy_Decision* pPolicy, Tizen::Web::Controls::_CertificateConfirmPopup* pParent)
92 SysTryReturnResult(NID_WEB_CTRL, pPolicy, E_INVALID_ARG, "Certificate Policy pointer is null.");
95 _SystemResource* pSysResource = _SystemResource::GetInstance();
96 SysAssertf(pSysResource != null, "Failed to get _SystemResource instance");
98 _WebPopupData* pPopupData = _WebPopup::GetPopupData();
99 SysTryReturn(NID_WEB_CTRL, pPopupData, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
101 __pCertificatePolicyData = pPolicy;
102 __certPopupMode = certPopupMode;
103 String titleText = L"";
104 int popupMaxHeight = 0;
105 Rectangle rect(0, 0, 0, 0);
109 ArrayList idList(SingleObjectDeleter);
110 r = idList.Construct();
111 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
113 ArrayList titleList(SingleObjectDeleter);
114 r = titleList.Construct();
115 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
117 if( __certPopupMode == CERTIFICATE_POPUP_MODE_VIEW )
119 titleText = pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_TPLATFORM_HEADER_SECURITY_CERTIFICATE_ABB");
120 popupMaxHeight = 4*pPopupData->labelDim.height + pPopupData->panelHeight;
122 rect.height = 4*pPopupData->labelDim.height;
123 rect.width = pPopupData->labelDim.width;
125 idList.Add(*(new Integer(ID_BUTTON_CERTIFICATE_CLOSE)));
126 titleList.Add(*(new String(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_TPLATFORM_BUTTON_OK"))));
128 else // CERTIFICATE_POPUP_MODE_CONFIRM
130 titleText = pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_TPLATFORM_HEADER_SECURITY_WARNING_ABB");
131 popupMaxHeight = 2*pPopupData->labelDim.height + pPopupData->panelHeight;
133 rect.height = 2*pPopupData->labelDim.height;
134 rect.width = pPopupData->labelDim.width;
136 idList.Add(*(new Integer(ID_BUTTON_CERTIFICATE_CANCEL)));
137 idList.Add(*(new Integer(ID_BUTTON_CERTIFICATE_VIEW)));
138 idList.Add(*(new Integer(ID_BUTTON_CERTIFICATE_ALLOW)));
140 titleList.Add(*(new String(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_TPLATFORM_BUTTON_CANCEL_ABB"))));
141 titleList.Add(*(new String(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_TPLATFORM_BUTTON2_VIEW_CERTIFICATE"))));
142 titleList.Add(*(new String(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_TPLATFORM_BUTTON_CONTINUE_ABB"))));
145 r = _WebPopup::Construct(true, Dimension(pPopupData->popupDim.width, popupMaxHeight));
146 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
148 SetTitleText(titleText);
150 std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(GetLayoutN()));
151 SysTryReturn(NID_WEB_CTRL, pLayout.get(), r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
153 if( certPopupMode == CERTIFICATE_POPUP_MODE_VIEW )
156 result r = GenerateCertifiate(certString);
157 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
159 std::unique_ptr<TextBox> pTextBox(new (std::nothrow) TextBox());
160 SysTryReturnResult(NID_WEB_CTRL, pTextBox.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
162 r = pTextBox->Construct(rect, TEXT_BOX_BORDER_ROUNDED);
163 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
165 r = pTextBox->SetTextSize(EDIT_TEXT_SIZE);
166 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
168 r = pTextBox->SetAutoLinkMask(LINK_TYPE_NONE);
169 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
171 r = pTextBox->SetText(certString);
172 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
174 r = AddControl(*pTextBox);
175 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
177 TextBox* pCertBox = pTextBox.release();
178 pLayout->SetHorizontalAlignment(*pCertBox, LAYOUT_HORIZONTAL_ALIGN_CENTER);
180 else // CERTIFICATE_POPUP_MODE_CONFIRM
182 std::unique_ptr<Label> pLabel(new (std::nothrow) Label());
183 SysTryReturnResult(NID_WEB_CTRL, pLabel.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
185 String message = L"";
186 message = pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_TPLATFORM_BODY_THERE_ARE_PROBLEMS_WITH_THE_SECURITY_CERTIFICATE_FOR_THIS_SITE_C");
187 message.Append(L"\n");
188 message.Append(ewk_certificate_policy_decision_url_get(__pCertificatePolicyData));
190 r = pLabel->Construct(rect, message);
191 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
193 pLabel->SetTextConfig(pPopupData->labelFontSize, LABEL_TEXT_STYLE_NORMAL);
195 r = AddControl(*pLabel);
196 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
198 Label* pInfoLabel = pLabel.release();
199 pLayout->SetHorizontalFitPolicy(*pInfoLabel, FIT_POLICY_PARENT);
201 Panel* pButtonPanel = CreateAndAddPanel();
202 SysTryReturn(NID_WEB_CTRL, pButtonPanel, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
204 r = CreateAndAddButtons(idList, titleList, pButtonPanel);
205 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
207 SetPropagatedKeyEventListener(this);
214 _CertificateConfirmPopup::OnActionPerformed(const Control& source, int actionId)
216 result r = E_SUCCESS;
220 case ID_BUTTON_CERTIFICATE_ALLOW:
227 HandleUserAction(true);
231 case ID_BUTTON_CERTIFICATE_VIEW:
239 std::unique_ptr<_CertificateConfirmPopup> pCertificatePopup(new (std::nothrow) _CertificateConfirmPopup());
240 SysTryReturnVoidResult(NID_WEB_CTRL, pCertificatePopup.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
242 r = pCertificatePopup->Construct(CERTIFICATE_POPUP_MODE_VIEW, __pCertificatePolicyData, this);
243 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
245 __pCertificatePopup = std::move(pCertificatePopup);
247 r = __pCertificatePopup->ShowPopup();
248 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
252 case ID_BUTTON_CERTIFICATE_CANCEL:
259 HandleUserAction(false);
263 case ID_BUTTON_CERTIFICATE_CLOSE:
281 SysLogException(NID_WEB_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
283 if (__certPopupMode == CERTIFICATE_POPUP_MODE_VIEW)
285 __pParent->SendUserEvent(ID_CERTIFICATE_CONFIRM_POPUP_CLOSE, null);
291 _CertificateConfirmPopup::OnUserEventReceivedN(RequestId requestId, IList* pArgs)
295 case ID_CERTIFICATE_CONFIRM_POPUP_CLOSE:
297 __pCertificatePopup.reset();
314 _CertificateConfirmPopup::OnKeyPressed(Control& source, const KeyEventInfo& keyEventInfo)
320 _CertificateConfirmPopup::OnKeyReleased(Control& source, const KeyEventInfo& keyEventInfo)
322 result r = E_SUCCESS;
323 if ((keyEventInfo.GetKeyCode() == KEY_ESC || keyEventInfo.GetKeyCode() == KEY_BACK) && source.GetShowState() == true)
325 if (__certPopupMode == CERTIFICATE_POPUP_MODE_USER_CONFIRM)
327 HandleUserAction(false);
332 SysLogException(NID_WEB_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
334 if (__certPopupMode == CERTIFICATE_POPUP_MODE_VIEW)
336 __pParent->SendUserEvent(ID_CERTIFICATE_CONFIRM_POPUP_CLOSE, null);
344 _CertificateConfirmPopup::OnPreviewKeyPressed(Control& source, const KeyEventInfo& keyEventInfo)
350 _CertificateConfirmPopup::OnPreviewKeyReleased(Control& source, const KeyEventInfo& keyEventInfo)
356 _CertificateConfirmPopup::TranslateKeyEventInfo(Control& source, KeyEventInfo& keyEventInfo)
362 _CertificateConfirmPopup::GetConfirmResult() const
369 _CertificateConfirmPopup::HandleUserAction(bool allow)
373 String pem(ewk_certificate_policy_decision_certificate_pem_get(__pCertificatePolicyData));
374 ewk_certificate_policy_decision_allowed_set(__pCertificatePolicyData, static_cast< Eina_Bool >(allow));
375 AddCertificateDb(pem, allow);
380 _CertificateConfirmPopup::AddCertificateDb(const String& pem, bool allow)
383 String certificatePath(Tizen::App::App::GetInstance()->GetAppRootPath() + CUSTOM_DB_DIRECTORY_PATH + USER_CONFIRM_DB_NAME);
384 String table(CERTIFICATE_TABLE_NAME);
386 result r = db.Construct(certificatePath, "r+", null);
387 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
389 std::unique_ptr<DbStatement> pStmt(db.CreateStatementN(L"Insert Into " + table + L" (pem, allow) Values (?, ?)"));
390 SysTryReturnVoidResult(NID_WEB_CTRL, pStmt.get(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
392 SysLog(NID_WEB_CTRL, "The current value of pem is %ls, allow is %d", pem.GetPointer(), allow);
394 pStmt->BindString(0, pem);
395 pStmt->BindInt(1, static_cast < int >(allow));
397 db.BeginTransaction();
399 std::unique_ptr<DbEnumerator> pEnum(db.ExecuteStatementN(*pStmt));
401 db.CommitTransaction();
406 _CertificateConfirmPopup::GenerateCertifiate(String& certString)
408 SysTryReturnResult(NID_WEB_CTRL, __pCertificatePolicyData, E_INVALID_ARG, "Certificate Policy pointer is null.");
410 _SystemResource* pSysResource = _SystemResource::GetInstance();
411 SysAssertf(pSysResource != null, "Failed to get _SystemResource instance");
413 String pemString(ewk_certificate_policy_decision_certificate_pem_get(__pCertificatePolicyData));
414 std::unique_ptr<ByteBuffer> pByteBuf(StringUtil::StringToUtf8N(pemString));
415 SysTryReturnResult(NID_WEB_CTRL, pByteBuf.get(), E_INVALID_DATA, "Certificate pem information is Empty.");
417 X509Certificate certificate;
418 result r = certificate.Construct(*pByteBuf);
419 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
422 certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_TPLATFORM_BODY_ISSUED_TO_C"));
423 certString.Append(L"\n\n");
424 String subject = certificate.GetSubject();
426 certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_TPLATFORM_BODY_COMMON_NAME_C"));
427 certString.Append(L"\n");
428 certString.Append(GetStringOfToken(subject, L"/CN=") + L"\n");
430 certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_TPLATFORM_BODY_ORGANISATION_C"));
431 certString.Append(L"\n");
432 certString.Append(GetStringOfToken(subject, L"/O=") + L"\n");
434 certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP ,"IDS_TPLATFORM_BODY_ORGANISATIONAL_UNIT_C"));
435 certString.Append(L"\n");
436 certString.Append(GetStringOfToken(subject, L"/OU=") + L"\n");
438 certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_TPLATFORM_BODY_SERIAL_NUMBER_C"));
439 certString.Append(L"\n");
440 certString.Append(certificate.GetSerialNumber() + L"\n\n\n");
443 certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_TPLATFORM_BODY_ISSUED_BY_C"));
444 certString.Append(L"\n\n");
445 String issuer = certificate.GetIssuer();
447 certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_TPLATFORM_BODY_COMMON_NAME_C"));
448 certString.Append(L"\n");
449 certString.Append(GetStringOfToken(issuer, L"/CN=") + L"\n");
451 certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_TPLATFORM_BODY_ORGANISATION_C"));
452 certString.Append(L"\n");
453 certString.Append(GetStringOfToken(issuer, L"/O=") + L"\n");
455 certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_TPLATFORM_BODY_ORGANISATIONAL_UNIT_C"));
456 certString.Append(L"\n");
457 certString.Append(GetStringOfToken(issuer, L"/OU=") + L"\n\n");
460 certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_TPLATFORM_BODY_VALIDITY_C"));
461 certString.Append(L"\n\n");
462 certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_POP_CERTI_VALID_FROM_C"));
463 certString.Append(L"\n");
464 certString.Append(certificate.GetNotBefore() + L"\n\n");
466 certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_BODY_CERTIFICATE_VALID_TILL"));
467 certString.Append(L":\n");
468 certString.Append(certificate.GetNotAfter() + L"\n\n\n");
471 certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_TPLATFORM_BODY_FINGERPRINTS_C"));
472 certString.Append(L"\n\n");
473 certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_BODY_SIGNATURE_ALGORITHM_VODA"));
474 certString.Append(L":\n");
475 certString.Append(certificate.GetSignatureAlgorithm() + L"\n\n");
477 std::unique_ptr<ByteBuffer> pFingerPrint(certificate.GetFingerprintN());
478 if (pFingerPrint.get() && pFingerPrint->GetPointer())
481 StringUtil::Utf8ToString((const char*)pFingerPrint->GetPointer(), fingerPrint);
482 fingerPrint.SetLength(pFingerPrint->GetLimit());
483 certString.Append(fingerPrint + L"\n\n");
491 _CertificateConfirmPopup::GetStringOfToken(const String& parseString, const String& parseToken)
493 String inString(parseString);
497 outString.Append(L"\n");
499 result r = inString.IndexOf(parseToken, 0, index);
500 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, outString, r, "[%s] Propagating.", GetErrorMessage(r));
502 int prsTokLen = parseToken.GetLength();
503 r = inString.Remove(0, index + prsTokLen);
504 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, outString, r, "[%s] Propagating.", GetErrorMessage(r));
508 r = inString.IndexOf(parseToken, 0, index);
513 r = inString.Remove(index, prsTokLen);
514 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, outString, r, "[%s] Propagating.", GetErrorMessage(r));
516 inString.Insert(L"\n", index);
519 case E_OBJ_NOT_FOUND:
520 r = inString.IndexOf('=', 0, index);
524 r = inString.LastIndexOf('/', index - 3, slashIndex);
526 if (!IsFailed(r) && (slashIndex == index - 2 || slashIndex == index - 3))
528 inString.Remove(slashIndex, inString.GetLength() - slashIndex);
531 inString.Append(L"\n");
532 outString = inString;
542 }}} // Tizen::Web::Controls