add H/W backkey action
[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 <FAppApp.h>
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_EflWebkit.h"
43 #include "FWebCtrl_Utility.h"
44 #include "FWebCtrl_WebImpl.h"
45
46
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;
56
57
58 namespace Tizen { namespace Web { namespace Controls
59 {
60
61
62 static const int EDIT_TEXT_SIZE = 30;
63
64
65 _CertificateConfirmPopup::_CertificateConfirmPopup(void)
66                                                 : __certPopupMode(CERTIFICATE_POPUP_MODE_USER_CONFIRM)
67                                                 , __confirm(false)
68                                                 , __pCertificatePolicyData(null)
69 {
70 }
71
72
73 _CertificateConfirmPopup::~_CertificateConfirmPopup(void)
74 {
75 }
76
77
78 result
79 _CertificateConfirmPopup::Construct(_CertificatePopupMode certPopupMode, Ewk_Certificate_Policy_Decision* pPolicy)
80 {
81         SysTryReturnResult(NID_WEB_CTRL, pPolicy, E_INVALID_ARG, "Certificate Policy pointer is null.");
82         result r = E_SUCCESS;
83
84         _SystemResource* pSysResource = _SystemResource::GetInstance();
85         SysAssertf(pSysResource != null, "Failed to get _SystemResource instance");
86
87         _WebPopupData* pPopupData = _WebPopup::GetPopupData();
88         SysTryReturn(NID_WEB_CTRL, pPopupData, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
89
90         __pCertificatePolicyData = pPolicy;
91         __certPopupMode = certPopupMode;
92         String titleText = L"";
93         int popupMaxHeight = 0;
94         Rectangle rect(0, 0, 0, 0);
95
96         ArrayList idList;
97         r = idList.Construct();
98         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
99
100         ArrayList titleList;
101         r = titleList.Construct();
102         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
103
104         if( __certPopupMode == CERTIFICATE_POPUP_MODE_VIEW )
105         {
106                 titleText = pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_BODY_CERTIFICATES");
107                 popupMaxHeight = 4*pPopupData->labelDim.height + pPopupData->btnDim.height + 2*pPopupData->sideMargin;
108
109                 rect.height = 4*pPopupData->labelDim.height;
110                 rect.width = pPopupData->labelDim.width;
111
112                 idList.Add(*(new Integer(ID_BUTTON_CERTIFICATE_CLOSE)));
113                 titleList.Add(*(new String(pSysResource->GetString("sys_string", "IDS_COM_BODY_DONE"))));
114         }
115         else    // CERTIFICATE_POPUP_MODE_CONFIRM
116         {
117                 titleText = pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_HEADER_SECURITY_WARNING");
118                 popupMaxHeight = 2*pPopupData->labelDim.height + pPopupData->btnDim.height + 2*pPopupData->sideMargin;
119
120                 rect.height = 2*pPopupData->labelDim.height;
121                 rect.width = pPopupData->labelDim.width;
122
123                 idList.Add(*(new Integer(ID_BUTTON_CERTIFICATE_ALLOW)));
124                 idList.Add(*(new Integer(ID_BUTTON_CERTIFICATE_VIEW)));
125                 idList.Add(*(new Integer(ID_BUTTON_CERTIFICATE_CANCEL)));
126
127                 titleList.Add(*(new String(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_OPT_ALLOW"))));
128                 titleList.Add(*(new String(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_OPT_VIEW"))));
129                 titleList.Add(*(new String(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_OPT_CANCEL"))));
130         }
131
132         r = _WebPopup::Construct(true, Dimension(pPopupData->popupDim.width, popupMaxHeight));
133         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
134
135         SetTitleText(titleText);
136
137         std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(GetLayoutN()));
138         SysTryReturn(NID_WEB_CTRL, pLayout.get(), r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
139
140         if( certPopupMode == CERTIFICATE_POPUP_MODE_VIEW )
141         {
142                 String certString;
143                 result r = GenerateCertifiate(certString);
144                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
145
146                 std::unique_ptr<TextBox> pTextBox(new (std::nothrow) TextBox());
147                 SysTryReturnResult(NID_WEB_CTRL, pTextBox.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
148
149                 r = pTextBox->Construct(rect, TEXT_BOX_BORDER_ROUNDED);
150                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
151
152                 r = pTextBox->SetTextSize(EDIT_TEXT_SIZE);
153                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
154
155                 r = pTextBox->SetAutoLinkMask(LINK_TYPE_NONE);
156                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
157
158                 r = pTextBox->SetText(certString);
159                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
160
161                 r = AddControl(*pTextBox);
162                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
163
164                 TextBox* pCertBox = pTextBox.release();
165                 pLayout->SetHorizontalAlignment(*pCertBox, LAYOUT_HORIZONTAL_ALIGN_CENTER);
166         }
167         else    // CERTIFICATE_POPUP_MODE_CONFIRM
168         {
169                 std::unique_ptr<Label> pLabel(new (std::nothrow) Label());
170                 SysTryReturnResult(NID_WEB_CTRL, pLabel.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
171
172                 String message = L"";
173                 message = pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_BODY_SECURITY_CERTIFICATE_PROBLEM_MSG");
174                 message.Append(L"\n");
175                 message.Append(ewk_certificate_policy_decision_url_get(__pCertificatePolicyData));
176
177                 r = pLabel->Construct(rect, message);
178                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
179
180                 pLabel->SetTextConfig(pPopupData->labelFontSize, LABEL_TEXT_STYLE_NORMAL);
181
182                 r = AddControl(*pLabel);
183                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
184
185                 Label* pInfoLabel = pLabel.release();
186                 pLayout->SetHorizontalFitPolicy(*pInfoLabel, FIT_POLICY_PARENT);
187         }
188         Panel* pButtonPanel = CreateAndAddPanel();
189         SysTryReturn(NID_WEB_CTRL, pButtonPanel, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
190
191         r = CreateAndAddButtons(idList, titleList, pButtonPanel);
192         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
193
194         pLayout->SetHorizontalFitPolicy(*pButtonPanel, FIT_POLICY_PARENT);
195         pLayout->SetHorizontalAlignment(*pButtonPanel, LAYOUT_HORIZONTAL_ALIGN_CENTER);
196
197         pLayout->SetSpacing(*pButtonPanel, 2*pPopupData->sideMargin);
198
199         SetPropagatedKeyEventListener(this);
200
201         return E_SUCCESS;
202 }
203
204
205 void
206 _CertificateConfirmPopup::OnActionPerformed(const Control& source, int actionId)
207 {
208         result r = E_SUCCESS;
209
210         switch (actionId)
211         {
212         case ID_BUTTON_CERTIFICATE_ALLOW:
213                 HandleUserAction(true);
214                 break;
215
216         case ID_BUTTON_CERTIFICATE_VIEW:
217         {
218                 std::unique_ptr<_CertificateConfirmPopup> pCertificatePopup(new (std::nothrow) _CertificateConfirmPopup());
219                 SysTryReturnVoidResult(NID_WEB_CTRL, pCertificatePopup.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
220
221                 r = pCertificatePopup->Construct(CERTIFICATE_POPUP_MODE_VIEW, __pCertificatePolicyData);
222                 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
223
224                 pCertificatePopup->SetOwner(this);
225                 
226                 r = pCertificatePopup->ShowPopup();
227                 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
228
229                 pCertificatePopup.release();
230                 return;
231         }
232
233         case ID_BUTTON_CERTIFICATE_CANCEL:
234                 HandleUserAction(false);
235                 break;
236
237         case ID_BUTTON_CERTIFICATE_CLOSE:
238                 break;
239
240         default:
241                 SysAssert(false);
242                 break;
243         }
244
245         r = HidePopup();
246         if (IsFailed(r))
247         {
248                 SysLogException(NID_WEB_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
249         }
250         if (__certPopupMode == CERTIFICATE_POPUP_MODE_VIEW )
251         {
252                 GetOwner()->SendUserEvent(ID_CERTIFICATE_CONFIRM_POPUP_CLOSE, null);
253         }
254 }
255
256 bool
257 _CertificateConfirmPopup::OnKeyPressed(Control& source, const KeyEventInfo& keyEventInfo)
258 {
259         return true;
260 }
261
262 bool
263 _CertificateConfirmPopup::OnKeyReleased(Control& source, const KeyEventInfo& keyEventInfo)
264 {
265         result r = E_SUCCESS;
266         if ((keyEventInfo.GetKeyCode() == KEY_ESC || keyEventInfo.GetKeyCode() == KEY_BACK) && source.GetShowState() == true)
267         {
268                 HandleUserAction(false);
269                 r = HidePopup();
270                 if (IsFailed(r))
271                 {
272                         SysLogException(NID_WEB_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
273                 }
274                 if (__certPopupMode == CERTIFICATE_POPUP_MODE_VIEW )
275                 {
276                         GetOwner()->SendUserEvent(ID_CERTIFICATE_CONFIRM_POPUP_CLOSE, null);
277                 }
278         }
279
280         return true;
281 }
282
283 bool
284 _CertificateConfirmPopup::OnPreviewKeyPressed(Control& source, const KeyEventInfo& keyEventInfo)
285 {
286         return false;
287 }
288
289 bool
290 _CertificateConfirmPopup::OnPreviewKeyReleased(Control& source, const KeyEventInfo& keyEventInfo)
291 {
292         return false;
293 }
294
295 bool
296 _CertificateConfirmPopup::TranslateKeyEventInfo(Control& source, KeyEventInfo& keyEventInfo)
297 {
298         return false;
299 }
300
301 bool
302 _CertificateConfirmPopup::GetConfirmResult() const
303 {
304         return __confirm;
305 }
306
307
308 void
309 _CertificateConfirmPopup::HandleUserAction(bool allow)
310 {
311         __confirm = allow;
312
313         String pem(ewk_certificate_policy_decision_certificate_pem_get(__pCertificatePolicyData));
314         ewk_certificate_policy_decision_allowed_set(__pCertificatePolicyData, static_cast< Eina_Bool >(allow));
315         AddCertificateDb(pem, allow);
316 }
317
318
319 void
320 _CertificateConfirmPopup::AddCertificateDb(const String& pem, bool allow)
321 {
322         _DatabaseImpl db;
323         String certificatePath(Tizen::App::App::GetInstance()->GetAppRootPath() + CUSTOM_DB_DIRECTORY_PATH + USER_CONFIRM_DB_NAME);
324         String table(CERTIFICATE_TABLE_NAME);
325
326         result r = db.Construct(certificatePath, "r+", null);
327         SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
328
329         std::unique_ptr<DbStatement> pStmt(db.CreateStatementN(L"Insert Into " + table + L" (pem, allow) Values (?, ?)"));
330         SysTryReturnVoidResult(NID_WEB_CTRL, pStmt.get(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
331
332         SysLog(NID_WEB_CTRL, "The current value of pem is %ls, allow is %d", pem.GetPointer(), allow);
333
334         pStmt->BindString(0, pem);
335         pStmt->BindInt(1, static_cast < int >(allow));
336
337         db.BeginTransaction();
338
339         std::unique_ptr<DbEnumerator> pEnum(db.ExecuteStatementN(*pStmt));
340
341         db.CommitTransaction();
342 }
343
344
345 result
346 _CertificateConfirmPopup::GenerateCertifiate(String& certString)
347 {
348         SysTryReturnResult(NID_WEB_CTRL, __pCertificatePolicyData, E_INVALID_ARG, "Certificate Policy pointer is null.");
349
350         _SystemResource* pSysResource = _SystemResource::GetInstance();
351         SysAssertf(pSysResource != null, "Failed to get _SystemResource instance");
352
353         String pemString(ewk_certificate_policy_decision_certificate_pem_get(__pCertificatePolicyData));
354         std::unique_ptr<ByteBuffer> pByteBuf(StringUtil::StringToUtf8N(pemString));
355         SysTryReturnResult(NID_WEB_CTRL, pByteBuf.get(), E_INVALID_DATA, "Certificate pem information is Empty.");
356
357         X509Certificate certificate;
358         result r = certificate.Construct(*pByteBuf);
359         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
360
361         //Issued to
362         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_HEADER_ISSUED_TO_C"));
363         certString.Append(L"\n\n");
364         String subject = certificate.GetSubject();
365
366         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_HEADER_COMMON_NAME_C"));
367         certString.Append(L"\n");
368         certString.Append(GetStringOfToken(subject, L"/CN=") + L"\n");
369
370         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_HEADER_ORGANIZATION_C"));
371         certString.Append(L"\n");
372         certString.Append(GetStringOfToken(subject, L"/O=") + L"\n");
373
374         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP ,"IDS_BR_HEADER_ORGANIZATIONAL_UNIT_C"));
375         certString.Append(L"\n");
376         certString.Append(GetStringOfToken(subject, L"/OU=") + L"\n");
377
378         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_BODY_CERTIFICATE_SERIAL_NUMBER"));
379         certString.Append(L":\n");
380         certString.Append(certificate.GetSerialNumber() + L"\n\n\n");
381
382         //Issued by
383         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_HEADER_ISSUED_BY_C"));
384         certString.Append(L"\n\n");
385         String issuer = certificate.GetIssuer();
386
387         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_HEADER_COMMON_NAME_C"));
388         certString.Append(L"\n");
389         certString.Append(GetStringOfToken(issuer, L"/CN=") + L"\n");
390
391         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_HEADER_ORGANIZATION_C"));
392         certString.Append(L"\n");
393         certString.Append(GetStringOfToken(issuer, L"/O=") + L"\n");
394
395         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_HEADER_ORGANIZATIONAL_UNIT_C"));
396         certString.Append(L"\n");
397         certString.Append(GetStringOfToken(issuer, L"/OU=") + L"\n\n");
398
399         //Validity
400         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_HEADER_VALIDITY_C"));
401         certString.Append(L"\n\n");
402         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_POP_CERTI_VALID_FROM_C"));
403         certString.Append(L"\n");
404         certString.Append(certificate.GetNotBefore() + L"\n\n");
405
406         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_BODY_CERTIFICATE_VALID_TILL"));
407         certString.Append(L":\n");
408         certString.Append(certificate.GetNotAfter() + L"\n\n\n");
409
410         //FingerPrints
411         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_BODY_ROOTCERTIFICATES_FINGER_PRINT"));
412         certString.Append(L":\n\n");
413         certString.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_BODY_SIGNATURE_ALGORITHM_VODA"));
414         certString.Append(L":\n");
415         certString.Append(certificate.GetSignatureAlgorithm() + L"\n\n");
416
417         std::unique_ptr<ByteBuffer> pFingerPrint(certificate.GetFingerprintN());
418         if (pFingerPrint.get() && pFingerPrint->GetPointer())
419         {
420                 String fingerPrint;
421                 StringUtil::Utf8ToString((const char*)pFingerPrint->GetPointer(), fingerPrint);
422                 certString.Append(fingerPrint + L"\n\n");
423         }
424
425         return E_SUCCESS;
426 }
427
428
429 String
430 _CertificateConfirmPopup::GetStringOfToken(const String& parseString, const String& parseToken)
431 {
432         String inString(parseString);
433         int index = 0;
434
435         String outString;
436         outString.Append(L"\n");
437
438         result r = inString.IndexOf(parseToken, 0, index);
439         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, outString, r, "[%s] Propagating.", GetErrorMessage(r));
440
441         int prsTokLen = parseToken.GetLength();
442         r = inString.Remove(0, index + prsTokLen);
443         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, outString, r, "[%s] Propagating.", GetErrorMessage(r));
444
445         while (true)
446         {
447                 r = inString.IndexOf(parseToken, 0, index);
448
449                 switch (r)
450                 {
451                 case E_SUCCESS:
452                         r = inString.Remove(index, prsTokLen);
453                         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, outString, r, "[%s] Propagating.", GetErrorMessage(r));
454
455                         inString.Insert(L"\n", index);
456                         continue;
457
458                 case E_OBJ_NOT_FOUND:
459                         r = inString.IndexOf('=', 0, index);
460                         if (r == E_SUCCESS)
461                         {
462                                 int slashIndex = 0;
463                                 r = inString.LastIndexOf('/', index - 3, slashIndex);
464
465                                 if (!IsFailed(r) && (slashIndex == index - 2 || slashIndex == index - 3))
466                                 {
467                                         inString.Remove(slashIndex, inString.GetLength() - slashIndex);
468                                 }
469                         }
470                         inString.Append(L"\n");
471                         outString = inString;
472                         return outString;
473
474                 default:
475                         return outString;
476                 }
477         }
478 }
479
480
481 }}} // Tizen::Web::Controls