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