optimize performance
[framework/osp/web.git] / src / controls / FWebCtrl_UserConfirmPopup.cpp
index 4b46e02..6f73c03 100755 (executable)
  * @file               FWebCtrl_UserConfirmPopup.cpp
  * @brief              The file contains the definition of _UserConfirmPopup class.
  */
+#include <EWebKit2.h>
 #include <FAppApp.h>
 #include <FBaseColArrayList.h>
 #include <FBaseSysLog.h>
 #include <FBaseUtilUri.h>
 #include <FGrpDimension.h>
+#include <FGrpFont.h>
 #include <FGrpRectangle.h>
 #include <FIoDbEnumerator.h>
 #include <FIoDbStatement.h>
 #include <FUiCtrlButton.h>
 #include <FUiCtrlLabel.h>
 #include <FUiCtrlPanel.h>
+#include <FUiCtrlScrollPanel.h>
+#include <FUiKeyEventInfo.h>
 #include <FUiLayout.h>
 #include <FUiVerticalBoxLayout.h>
+#include <FGrp_TextTextObject.h>
+#include <FGrp_TextTextSimple.h>
 #include <FIo_DatabaseImpl.h>
+#include <FSys_SystemResource.h>
 #include <FUi_ControlManager.h>
 #include <FUi_ResourceManager.h>
-#include "FWebCtrl_EflWebkit.h"
+#include "FWebCtrl_WebManager.h"
 #include "FWebCtrl_UserConfirmPopup.h"
 #include "FWebCtrl_Utility.h"
+#include "FWebCtrl_WebImpl.h"
 
 
 using namespace Tizen::Base;
 using namespace Tizen::Base::Collection;
 using namespace Tizen::Base::Utility;
 using namespace Tizen::Graphics;
+using namespace Tizen::Graphics::_Text;
 using namespace Tizen::Io;
 using namespace Tizen::Security::Cert;
+using namespace Tizen::System;
 using namespace Tizen::Ui;
 using namespace Tizen::Ui::Controls;
 
@@ -55,28 +65,41 @@ namespace Tizen { namespace Web { namespace Controls
 {
 
 
+static const int TEXT_SIZE_ADJUST = 1;
+
+
 _UserConfirmPopup::_UserConfirmPopup(void)
        : __pUserPolicyData(null)
        , __userConfirmMode(USER_CONFIRM_USERMEDIA)
        , __pCheckButton(null)
+       , __isUserActionNeeded(false)
        , __sync(false)
+       , __pImpl(null)
 {
 }
 
 
 _UserConfirmPopup::~_UserConfirmPopup(void)
 {
+       if (__isUserActionNeeded == true)
+       {
+               HandleUserAction(EINA_FALSE);
+               __isUserActionNeeded = false;
+       }
 }
 
 
 result
-_UserConfirmPopup::Construct(_UserConfirmMode userConfirmMode, void* pEventInfo, bool sync)
+_UserConfirmPopup::Construct(_UserConfirmMode userConfirmMode, void* pEventInfo, Tizen::Web::Controls::_WebImpl* pImpl, bool sync, String msg)
 {
        result r = E_SUCCESS;
        SysTryReturnResult(NID_WEB_CTRL, pEventInfo, E_INVALID_ARG, "Invalid argument(s) is used. pPolicy is null.");
-       SysTryReturnResult(NID_WEB_CTRL, userConfirmMode >= USER_CONFIRM_USERMEDIA && userConfirmMode <= USER_CONFIRM_GEOLOCATION, E_INVALID_ARG,
+       SysTryReturnResult(NID_WEB_CTRL, userConfirmMode >= USER_CONFIRM_USERMEDIA && userConfirmMode <= USER_CONFIRM_INDEXED_DB_QUOTA_EXCEDED, E_INVALID_ARG,
                                        "Invalid userConfirmMode is used. [%d]", userConfirmMode);
 
+       _SystemResource* pSysResource = _SystemResource::GetInstance();
+       SysAssertf(pSysResource != null, "Failed to get _SystemResource instance");
+
        _WebPopupData* pPopupData = _WebPopup::GetPopupData();
        SysTryReturn(NID_WEB_CTRL, pPopupData, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
 
@@ -84,16 +107,41 @@ _UserConfirmPopup::Construct(_UserConfirmMode userConfirmMode, void* pEventInfo,
        __userConfirmMode = userConfirmMode;
        __sync = sync;
 
-       int popupMaxHeight = 2*pPopupData->labelDim.height + 2*pPopupData->btnDim.height + 6*pPopupData->sideMargin;
+       __isUserActionNeeded = true;
+
+       bool hasTitle = true;
+       int popupMaxHeight = 2*pPopupData->labelDim.height + pPopupData->panelHeight;
+
+       __pImpl = pImpl;
+       SysAssertf(__pImpl != null, "Failed to get _WebImpl");
+
        if (__userConfirmMode == USER_CONFIRM_GEOLOCATION)
        {
                popupMaxHeight +=  pPopupData->checkDim.height;
        }
 
-       r = _WebPopup::Construct(true, Dimension(pPopupData->popupDim.width, popupMaxHeight));
+       if (__userConfirmMode == USER_SCRIPT_ALERT)
+       {
+               hasTitle = false;
+       }
+
+       r = _WebPopup::Construct(hasTitle, Dimension(pPopupData->popupDim.width, popupMaxHeight));
        SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
 
-       SetTitleText("Security Warning");
+       if (userConfirmMode != USER_SCRIPT_ALERT)
+       {
+               SetTitleText(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_TPLATFORM_HEADER_SECURITY_WARNING_ABB"));
+       }
+
+       String message = L"";
+       if (userConfirmMode >= USER_SCRIPT_ALERT && userConfirmMode <= USER_CONFIRM_INDEXED_DB_QUOTA_EXCEDED)
+       {
+               message = msg;
+       }
+       else
+       {
+               message = GetMessageFromPolicy();
+       }
 
        Rectangle rect(0, 0, 0, 0);
 
@@ -101,19 +149,82 @@ _UserConfirmPopup::Construct(_UserConfirmMode userConfirmMode, void* pEventInfo,
        rect.height = 2*pPopupData->labelDim.height;
        rect.width = pPopupData->labelDim.width;
 
+       //With the font of label and width of label, required height is calculated
+       //      using textobject. TEXT_SIZE_ADJUST is used to increase font size and get bigger height
+       //      as the accurate height is not fitting text in some cases.
+       Font font;
+       r = font.Construct(FONT_STYLE_PLAIN, pPopupData->labelFontSize + TEXT_SIZE_ADJUST);
+       SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
+
+       std::unique_ptr<TextObject> pTextObject(new (std::nothrow) TextObject());
+       SysTryReturnResult(NID_WEB_CTRL, pTextObject.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
+
+       r = pTextObject->Construct();
+       SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
+
+       std::unique_ptr<TextSimple> pSimpleText(new (std::nothrow) TextSimple(message.GetPointer(), message.GetLength(), TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL, &font));
+       SysTryReturnResult(NID_WEB_CTRL, pSimpleText.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
+
+       r = pTextObject->AppendElement(*pSimpleText);
+       SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
+
+       pSimpleText.release();
+
+       r = pTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_WORD);
+       SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
+
+       r = pTextObject->SetBounds(rect);
+       SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
+
+       r = pTextObject->SetFont(&font, 0, message.GetLength());
+       SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
+
+       r = pTextObject->Compose();
+       SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
+
+       int labelHeight = pTextObject->GetTotalHeight();
+
        std::unique_ptr<Label> pLabel(new (std::nothrow) Label());
        SysTryReturnResult(NID_WEB_CTRL, pLabel.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
 
-       String message = GetMessageFromPolicy();
        r = pLabel->Construct(rect, message);
        SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
 
        pLabel->SetTextConfig(pPopupData->labelFontSize, LABEL_TEXT_STYLE_NORMAL);
 
-       r = AddControl(*pLabel);
-       SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
+       Control* pInfoLabel = null;
+
+       if (rect.height < labelHeight)
+       {
+               //message text is bigger than label, scroll is created to accomodate it.
+               std::unique_ptr<ScrollPanel> pScrollPanel(new (std::nothrow) ScrollPanel());
+               SysTryReturnResult(NID_WEB_CTRL, pScrollPanel.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
+
+               r = pScrollPanel->Construct(rect);
+               SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
+
+               r = AddControl(*pScrollPanel);
+               SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
+
+               ScrollPanel* pSPanel = pScrollPanel.release();
+
+               r = pLabel->SetSize(Dimension(rect.width, labelHeight));
+               SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
+
+               r = pSPanel->AddControl(*pLabel);
+               SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
+
+               pLabel.release();
+               pInfoLabel = pSPanel;
+       }
+       else
+       {
+               r = AddControl(*pLabel);
+               SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
+
+               pInfoLabel = pLabel.release();
+       }
 
-       Label* pInfoLabel = pLabel.release();
        //checkbutton
        if (__userConfirmMode == USER_CONFIRM_GEOLOCATION)
        {
@@ -123,7 +234,7 @@ _UserConfirmPopup::Construct(_UserConfirmMode userConfirmMode, void* pEventInfo,
                std::unique_ptr<CheckButton> pCheckButton (new (std::nothrow) CheckButton());
                SysTryReturnResult(NID_WEB_CTRL, pCheckButton.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
 
-               r = pCheckButton->Construct(Rectangle(0, 0, rect.width, rect.height), CHECK_BUTTON_STYLE_MARK, BACKGROUND_STYLE_NONE, false, L"Remember Preference");
+               r = pCheckButton->Construct(Rectangle(0, 0, rect.width, rect.height), CHECK_BUTTON_STYLE_MARK, BACKGROUND_STYLE_NONE, false, pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_BODY_REMEMBER_PREFERENCE"));
                SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
 
                r = AddControl(*pCheckButton);
@@ -138,14 +249,30 @@ _UserConfirmPopup::Construct(_UserConfirmMode userConfirmMode, void* pEventInfo,
        ArrayList idList;
        r = idList.Construct();
        SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
-       idList.Add(*(new Integer(ID_BUTTON_USER_ALLOW)));
-       idList.Add(*(new Integer(ID_BUTTON_USER_CANCEL)));
 
        ArrayList titleList;
        r = titleList.Construct();
        SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
-       titleList.Add(*(new String(L"Allow")));
-       titleList.Add(*(new String(L"Cancel")));
+
+       if (userConfirmMode == USER_SCRIPT_ALERT)
+       {
+               idList.Add(*(new Integer(ID_BUTTON_USER_ALLOW)));
+               titleList.Add(*(new String(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_TPLATFORM_BUTTON_OK"))));
+       }
+       else if (userConfirmMode == USER_BEFORE_UNLOAD_CONFIRM)
+       {
+               idList.Add(*(new Integer(ID_BUTTON_USER_CANCEL)));
+               idList.Add(*(new Integer(ID_BUTTON_USER_ALLOW)));
+               titleList.Add(*(new String(L"Leave")));
+               titleList.Add(*(new String(L"Stay")));
+       }
+       else
+       {
+               idList.Add(*(new Integer(ID_BUTTON_USER_CANCEL)));
+               idList.Add(*(new Integer(ID_BUTTON_USER_ALLOW)));
+               titleList.Add(*(new String(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_TPLATFORM_BUTTON_CANCEL_ABB"))));
+               titleList.Add(*(new String(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_TPLATFORM_BUTTON_CONTINUE_ABB"))));
+       }
 
        r = CreateAndAddButtons(idList, titleList, pButtonPanel);
        SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
@@ -158,10 +285,8 @@ _UserConfirmPopup::Construct(_UserConfirmMode userConfirmMode, void* pEventInfo,
        {
                pLayout->SetHorizontalAlignment(*__pCheckButton, LAYOUT_HORIZONTAL_ALIGN_CENTER);
        }
-       pLayout->SetHorizontalAlignment(*pButtonPanel, LAYOUT_HORIZONTAL_ALIGN_CENTER);
-
-       pLayout->SetSpacing(*pButtonPanel, 2*pPopupData->sideMargin);
 
+       SetPropagatedKeyEventListener(this);
        return E_SUCCESS;
 }
 
@@ -193,7 +318,41 @@ _UserConfirmPopup::OnActionPerformed(const Control& source, int actionId)
 
        if (!__sync)
        {
-               delete this;
+               switch(__userConfirmMode)
+               {
+               case USER_CONFIRM_USERMEDIA:
+               {
+                       __pImpl->SendUserEvent(ID_USER_CONFIRM_USERMEDIA_CLOSE, null);
+                       break;
+               }
+               case USER_CONFIRM_GEOLOCATION:
+               {
+                       __pImpl->SendUserEvent(ID_USER_CONFIRM_GEOLOCATION_CLOSE, null);
+                       break;
+               }
+               case USER_CONFIRM_NOTIFICATION:
+               {
+                       __pImpl->SendUserEvent(ID_USER_CONFIRM_NOTIFICATION_CLOSE, null);
+                       break;
+               }
+               case USER_SCRIPT_ALERT:
+               {
+                       __pImpl->SendUserEvent(ID_USER_SCRIPT_ALERT_CLOSE, null);
+                       break;
+               }
+               case USER_SCRIPT_CONFIRM:
+               {
+                       __pImpl->SendUserEvent(ID_USER_SCRIPT_CONFIRM_CLOSE, null);
+                       break;
+               }
+               case USER_BEFORE_UNLOAD_CONFIRM:
+               {
+                       __pImpl->SendUserEvent(ID_USER_BEFORE_UNLOAD_CONFIRM_CLOSE, null);
+                       break;
+               }
+               default:
+                       SysAssert(false);
+               }
        }
 }
 
@@ -201,6 +360,9 @@ _UserConfirmPopup::OnActionPerformed(const Control& source, int actionId)
 String
 _UserConfirmPopup::GetMessageFromPolicy(void)
 {
+       _SystemResource* pSysResource = _SystemResource::GetInstance();
+       SysAssertf(pSysResource != null, "Failed to get _SystemResource instance");
+
        String message;
 
        switch(__userConfirmMode)
@@ -238,7 +400,15 @@ _UserConfirmPopup::GetMessageFromPolicy(void)
                const Ewk_Security_Origin* pSecurityOrigin = ewk_geolocation_permission_request_origin_get(pGeoLocPermReq);
 
                message = _Utility::CreateOrigin(pSecurityOrigin);
-               message.Append(" Requests your Location");
+               message.Append(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_BODY_PS_REQUESTS_YOUR_LOCATION"));
+               break;
+       }
+       case USER_CONFIRM_NOTIFICATION:
+       {
+               Ewk_Notification_Permission_Request* pPermissionRequest = reinterpret_cast< Ewk_Notification_Permission_Request* >(__pUserPolicyData);
+               const Ewk_Security_Origin* pSecurityOrigin = ewk_notification_permission_request_origin_get(pPermissionRequest);
+               message = _Utility::CreateOrigin(pSecurityOrigin);
+               message.Append(L" wants to display notifications");
                break;
        }
        default:
@@ -282,9 +452,58 @@ CATCH:
                ewk_geolocation_permission_request_set(pPolicy, allow);
                break;
        }
+       case USER_CONFIRM_NOTIFICATION:
+       {
+               Ewk_Notification_Permission_Request* pPermissionRequest = reinterpret_cast< Ewk_Notification_Permission_Request* >(__pUserPolicyData);
+               ewk_notification_permission_request_set(pPermissionRequest, allow);
+               break;
+       }
+       case USER_SCRIPT_ALERT:
+       {
+               Evas_Object* pView = reinterpret_cast< Evas_Object* >(__pUserPolicyData);
+               ewk_view_javascript_alert_reply(pView);
+               break;
+       }
+       case USER_SCRIPT_CONFIRM:
+       {
+               Evas_Object* pView = reinterpret_cast< Evas_Object* >(__pUserPolicyData);
+               ewk_view_javascript_confirm_reply(pView, allow);
+               break;
+       }
+       case USER_BEFORE_UNLOAD_CONFIRM:
+       {
+               Evas_Object* pView = reinterpret_cast< Evas_Object* >(__pUserPolicyData);
+               ewk_view_before_unload_confirm_panel_reply(pView, !allow);
+               break;
+       }
+       case USER_CONFIRM_APP_CACHE:
+       {
+               Evas_Object *pObj = reinterpret_cast< Evas_Object* >(__pUserPolicyData);
+               ewk_view_application_cache_permission_reply(pObj, allow);
+               break;
+       }
+       case USER_CONFIRM_DB_QUOTA_EXCEDED:
+       {
+               Evas_Object *pObj = reinterpret_cast< Evas_Object* >(__pUserPolicyData);
+               ewk_view_exceeded_database_quota_reply(pObj, allow);
+               break;
+       }
+       case USER_CONFIRM_LOCAL_FS_QUOTA_EXCEDED:
+       {
+               Evas_Object *pObj = reinterpret_cast< Evas_Object* >(__pUserPolicyData);
+               ewk_view_exceeded_local_file_system_quota_reply(pObj, allow);
+               break;
+       }
+       case USER_CONFIRM_INDEXED_DB_QUOTA_EXCEDED:
+       {
+               Evas_Object *pObj = reinterpret_cast< Evas_Object* >(__pUserPolicyData);
+               ewk_view_exceeded_indexed_database_quota_reply(pObj, allow);
+               break;
+       }
        default:
                SysAssert(false);
        }
+       __isUserActionNeeded = false;
 }
 
 
@@ -318,6 +537,7 @@ _UserConfirmPopup::AddGeolocationDb(Ewk_Geolocation_Permission_Request* pPolicy,
        return E_SUCCESS;
 }
 
+
 void
 _UserConfirmPopup::RegisterHandler(bool checkHandler, Eina_Bool allow)
 {
@@ -382,4 +602,89 @@ _UserConfirmPopup::RegisterHandler(bool checkHandler, Eina_Bool allow)
        }
 }
 
+bool
+_UserConfirmPopup::OnKeyPressed(Control& source, const KeyEventInfo& keyEventInfo)
+{
+       return false;
+}
+
+bool
+_UserConfirmPopup::OnKeyReleased(Control& source, const KeyEventInfo& keyEventInfo)
+{
+       result r = E_SUCCESS;
+       if ((keyEventInfo.GetKeyCode() == KEY_ESC || keyEventInfo.GetKeyCode() == KEY_BACK) && source.GetShowState() == true)
+       {
+               HandleUserAction(EINA_FALSE);
+               r = HidePopup();
+
+               if (IsFailed(r))
+               {
+                       SysLogException(NID_WEB_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
+               }
+               
+               switch (__userConfirmMode)
+               {
+               case USER_CONFIRM_USERMEDIA:
+               {
+                       __pImpl->SendUserEvent(ID_USER_CONFIRM_USERMEDIA_CLOSE, null);
+                       break;
+               }
+               case USER_CONFIRM_GEOLOCATION:
+               {
+                       __pImpl->SendUserEvent(ID_USER_CONFIRM_GEOLOCATION_CLOSE, null);
+                       break;
+               }
+               case USER_CONFIRM_NOTIFICATION:
+               {
+                       __pImpl->SendUserEvent(ID_USER_CONFIRM_NOTIFICATION_CLOSE, null);
+                       break;
+               }
+               case USER_SCRIPT_ALERT:
+               {
+                       __pImpl->SendUserEvent(ID_USER_SCRIPT_ALERT_CLOSE, null);
+                       break;
+               }
+               case USER_SCRIPT_CONFIRM:
+               {
+                       __pImpl->SendUserEvent(ID_USER_SCRIPT_CONFIRM_CLOSE, null);
+                       break;
+               }
+               case USER_BEFORE_UNLOAD_CONFIRM:
+               {
+                       __pImpl->SendUserEvent(ID_USER_BEFORE_UNLOAD_CONFIRM_CLOSE, null);
+                       break;
+               }
+               case USER_PROTOCOL_HANDLER:
+               case USER_CONTENT_HANDLER:
+               case USER_CONFIRM_APP_CACHE:
+               case USER_CONFIRM_DB_QUOTA_EXCEDED:
+               case USER_CONFIRM_LOCAL_FS_QUOTA_EXCEDED:
+               case USER_CONFIRM_INDEXED_DB_QUOTA_EXCEDED:
+                       break;
+               default:
+                       SysAssert(false);
+               }
+       }
+       return false;
+}
+
+bool
+_UserConfirmPopup::OnPreviewKeyPressed(Control& source, const KeyEventInfo& keyEventInfo)
+{
+       return false;
+}
+
+bool
+_UserConfirmPopup::OnPreviewKeyReleased(Control& source, const KeyEventInfo& keyEventInfo)
+{
+       return false;
+}
+
+bool
+_UserConfirmPopup::TranslateKeyEventInfo(Control& source, KeyEventInfo& keyEventInfo)
+{
+       return false;
+}
+
+
 }}} // Tizen::Web::Controls