improve the logic to make a header
[framework/osp/web.git] / src / controls / FWebCtrl_WebImpl.cpp
index 2552153..2b78f55 100755 (executable)
@@ -37,7 +37,6 @@
 #include <FBaseRtMutexGuard.h>
 #include <FBaseSysLog.h>
 #include <FBaseUtilUri.h>
-#include <FCntDownloadRequest.h>
 #include <FGrpColor.h>
 #include <FGrpPoint.h>
 #include <FGrpRectangle.h>
@@ -70,8 +69,6 @@
 #include <FApp_AppInfo.h>
 #include <FApp_AppManagerImpl.h>
 #include <FBase_StringConverter.h>
-#include <FCnt_DownloadManagerImpl.h>
-#include <FCnt_ContentManagerImpl.h>
 #include <FGrp_CoordinateSystem.h>
 #include <FGrp_Screen.h>
 #include <FIo_DatabaseImpl.h>
@@ -153,23 +150,6 @@ static const int DEFAULT_LIST_ITEM_COUNT = 4;
 
 
 Eina_Bool
-AddHttpHeaderData(const Eina_Hash* pHash, const void* pKey, void* pValue, void* pUserData)
-{
-       result r = E_SUCCESS;
-
-       String key(reinterpret_cast< const char* >(pKey));
-       String value(reinterpret_cast< char* >(pValue));
-
-       SysLog(NID_WEB_CTRL, "The current value of key is %ls, value is %ls", key.GetPointer(), value.GetPointer());
-
-       r = reinterpret_cast< HttpHeader* >(pUserData)->AddField(key, value);
-       SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, EINA_FALSE, r, "[%s] Propagating.", GetErrorMessage(r));
-
-       return EINA_TRUE;
-}
-
-
-Eina_Bool
 AddHttpAttributeData(const Eina_Hash* pHash, const void* pKey, void* pValue, void* pUserData)
 {
        result r = E_SUCCESS;
@@ -833,12 +813,12 @@ OnFullScreenEntered(void* pUserData, Evas_Object* pView, void* pEventInfo)
 {
        _WebImpl* pImpl = reinterpret_cast<_WebImpl*>(pUserData);
        bool* pIsHandleNeeded = reinterpret_cast< bool* >(pEventInfo);
-       SysAssertf(pImpl && pIsHandleNeeded, "Failed to request");
+       SysAssertf(pImpl, "Failed to request");
 
        _Web* pWebCore = dynamic_cast< _Web* >(&(pImpl->GetCore()));
        SysAssertf(pWebCore, "Failed to get Web core object");
 
-       if (*pIsHandleNeeded)
+       if (pIsHandleNeeded && *pIsHandleNeeded)
        {
                pWebCore->SetFullScreenEntered(true);
        }
@@ -859,6 +839,28 @@ OnFullScreenExited(void* pUserData, Evas_Object* pView, void* pEventInfo)
 
 
 void
+OnFocusDownRequested(void* pUserData, Evas_Object* pView, void* pEventInfo)
+{
+       _WebImpl* pImpl = reinterpret_cast<_WebImpl*>(pUserData);
+       SysAssertf(pImpl, "Failed to request");
+
+       _Web* pWebCore = dynamic_cast< _Web* >(&(pImpl->GetCore()));
+       pWebCore->SetFocusEnd(true);
+}
+
+
+void
+OnFocusUpRequested(void* pUserData, Evas_Object* pView, void* pEventInfo)
+{
+       _WebImpl* pImpl = reinterpret_cast<_WebImpl*>(pUserData);
+       SysAssertf(pImpl, "Failed to request");
+
+       _Web* pWebCore = dynamic_cast< _Web* >(&(pImpl->GetCore()));
+       pWebCore->SetFocusEnd(true);
+}
+
+
+void
 OnVibrationRequested(uint64_t duration, void* pUserData)
 {
        result r = E_SUCCESS;
@@ -891,13 +893,7 @@ OnLoadingRequested(void* pUserData, Evas_Object* pView, void* pEventInfo)
        String url(ewk_policy_decision_url_get(pPolicy));
        SysSecureLog(NID_WEB_CTRL, "The current value of url is %ls", url.GetPointer());
 
-       if (url == L"about:blank")
-       {
-               ewk_policy_decision_use(pPolicy);
-               return;
-       }
-
-       if (pImpl->GetLoadingListener() && ewk_frame_is_main_frame(ewk_policy_decision_frame_get(pPolicy))
+       if (ewk_frame_is_main_frame(ewk_policy_decision_frame_get(pPolicy))
                && !pImpl->IsRedirectRequested())
        {
                _Web* pWebCore = dynamic_cast< _Web* >(&(pImpl->GetCore()));
@@ -911,49 +907,61 @@ OnLoadingRequested(void* pUserData, Evas_Object* pView, void* pEventInfo)
                {
                        pImpl->DisableAsyncSearch();
                }
-       
-               WebNavigationType navigationType = WEB_NAVIGATION_OTHER;
-               Ewk_Policy_Navigation_Type ewkNavigationType = ewk_policy_decision_navigation_type_get(pPolicy);
 
-               switch (ewkNavigationType)
+               ILoadingListener* pLoadingListener = pImpl->GetLoadingListener();
+               if (pLoadingListener)
                {
-               case EWK_POLICY_NAVIGATION_TYPE_LINK_CLICKED:
-                       navigationType = WEB_NAVIGATION_LINK_CLICKED;
-                       break;
-               case EWK_POLICY_NAVIGATION_TYPE_FORM_SUBMITTED:
-                       navigationType = WEB_NAVIGATION_FORM_SUBMITTED;
-                       break;
-               case EWK_POLICY_NAVIGATION_TYPE_BACK_FORWARD:
-                       navigationType = WEB_NAVIGATION_BACKFORWARD;
-                       break;
-               case EWK_POLICY_NAVIGATION_TYPE_RELOAD:
-                       navigationType = WEB_NAVIGATION_RELOAD;
-                       break;
-               case EWK_POLICY_NAVIGATION_TYPE_FORM_RESUBMITTED:
-                       navigationType = WEB_NAVIGATION_FORM_RESUBMITTED;
-                       break;
-               case EWK_POLICY_NAVIGATION_TYPE_OTHER:
-                       navigationType = WEB_NAVIGATION_OTHER;
-                       break;
-               default:
-                       SysAssert(false);
-               }
+                       WebNavigationType navigationType = WEB_NAVIGATION_OTHER;
+                       Ewk_Policy_Navigation_Type ewkNavigationType = ewk_policy_decision_navigation_type_get(pPolicy);
 
-               if (pImpl->GetLoadingListener()->OnLoadingRequested(url, navigationType))
-               {
-                       ewk_policy_decision_ignore(pPolicy);
+                       switch (ewkNavigationType)
+                       {
+                       case EWK_POLICY_NAVIGATION_TYPE_LINK_CLICKED:
+                               navigationType = WEB_NAVIGATION_LINK_CLICKED;
+                               break;
+                       case EWK_POLICY_NAVIGATION_TYPE_FORM_SUBMITTED:
+                               navigationType = WEB_NAVIGATION_FORM_SUBMITTED;
+                               break;
+                       case EWK_POLICY_NAVIGATION_TYPE_BACK_FORWARD:
+                               navigationType = WEB_NAVIGATION_BACKFORWARD;
+                               break;
+                       case EWK_POLICY_NAVIGATION_TYPE_RELOAD:
+                               navigationType = WEB_NAVIGATION_RELOAD;
+                               break;
+                       case EWK_POLICY_NAVIGATION_TYPE_FORM_RESUBMITTED:
+                               navigationType = WEB_NAVIGATION_FORM_RESUBMITTED;
+                               break;
+                       case EWK_POLICY_NAVIGATION_TYPE_OTHER:
+                               navigationType = WEB_NAVIGATION_OTHER;
+                               break;
+                       default:
+                               SysAssert(false);
+                       }
 
-                       return;
+                       if (pLoadingListener->OnLoadingRequested(url, navigationType))
+                       {
+                               ewk_policy_decision_ignore(pPolicy);
+
+                               return;
+                       }
                }
        }
 
+       if (url == L"about:blank")
+       {
+               ewk_policy_decision_use(pPolicy);
+
+               return;
+       }
+
        String currentUrl(pImpl->GetUrl());
        String redirectUrl(pImpl->GetProtocolFromUri(url,  currentUrl));
        if (redirectUrl != L"")
        {
-               pImpl->LoadUrl(redirectUrl);
                ewk_policy_decision_ignore(pPolicy);
 
+               pImpl->LoadUrl(redirectUrl);
+
                return;
        }
        
@@ -961,7 +969,7 @@ OnLoadingRequested(void* pUserData, Evas_Object* pView, void* pEventInfo)
        uri.SetUri(url);
 
        String uriScheme(uri.GetScheme());
-       SysLog(NID_WEB_CTRL, "The current value of scheme is %ls", uriScheme.GetPointer());
+       SysSecureLog(NID_WEB_CTRL, "The current value of scheme is %ls", uriScheme.GetPointer());
 
        if ((uriScheme != L"http") && (uriScheme != L"https") && (uriScheme != L"file"))
        {
@@ -1083,36 +1091,49 @@ OnWebDataReceived(void* pUserData, Evas_Object* pView, void* pEventInfo)
 
        result r = E_SUCCESS;
 
-       _SystemResource* pSysResource = _SystemResource::GetInstance();
-       SysAssertf(pSysResource != null, "Failed to get _SystemResource instance");
-
-       const char* pUrl = ewk_policy_decision_url_get(pPolicy);
        int code = ewk_policy_decision_response_status_code_get(pPolicy);
-       String mime(ewk_policy_decision_response_mime_get(pPolicy));
-       SysSecureLog(NID_WEB_CTRL, "url : %s, mime : %ls, code : %d", pUrl, mime.GetPointer(), code);
-
-       ILoadingListener* pLoadingListener = pImpl->GetLoadingListener();
-
        String url(ewk_policy_decision_url_get(pPolicy));
+       String mime(ewk_policy_decision_response_mime_get(pPolicy));
+       SysSecureLog(NID_WEB_CTRL, "code : %d, url : %ls, mime : %ls", code, url.GetPointer(), mime.GetPointer());
 
-       if (pLoadingListener && ewk_frame_is_main_frame(ewk_policy_decision_frame_get(pPolicy)))
+       if (ewk_frame_is_main_frame(ewk_policy_decision_frame_get(pPolicy)))
        {
-               const Eina_Hash* pHeader = ewk_policy_decision_response_headers_get(pPolicy);
-
-               DecisionPolicy policy;
-               HttpHeader httpHeader;
-
                if (code >= REDIRECTION_HEADER_GROUP && code < REDIRECTION_HEADER_GROUP + 100)
                {
                        pImpl->SetRedirectRequested(true);
                        mime = L"";
                }
 
-               eina_hash_foreach(pHeader, AddHttpHeaderData, &httpHeader);
-               SysTryCatch(NID_WEB_CTRL, GetLastResult() == E_SUCCESS, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
+               ILoadingListener* pLoadingListener = pImpl->GetLoadingListener();
+               if (pLoadingListener)
+               {
+                       HttpHeader httpHeader;
+
+                       Eina_Iterator* iterator = eina_hash_iterator_tuple_new(ewk_policy_decision_response_headers_get(pPolicy));
+                       if (iterator)
+                       {
+                               Eina_Hash_Tuple *tuple = null;
+
+                               EINA_ITERATOR_FOREACH(iterator, tuple)
+                               {
+                                       String key(reinterpret_cast< const char* >(tuple->key));
+                                       String value(reinterpret_cast< char* >(tuple->data));
+
+                                       SysSecureLog(NID_WEB_CTRL, "The current value of key is %ls, value is %ls", key.GetPointer(), value.GetPointer());
+
+                                       r = httpHeader.AddField(key, value);
+                                       SysTryCatch(NID_WEB_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
+                               }
 
-               policy = pLoadingListener->OnWebDataReceived(mime, httpHeader);
-               pImpl->SetPolicyDecision(policy);
+                               eina_iterator_free(iterator);
+                       }
+
+                       pImpl->SetPolicyDecision(pLoadingListener->OnWebDataReceived(mime, httpHeader));
+               }
+               else
+               {
+                       pImpl->SetPolicyDecision(WEB_DECISION_CONTINUE);
+               }
        }
 
        switch (pImpl->GetPolicyDecision())
@@ -1123,17 +1144,17 @@ OnWebDataReceived(void* pUserData, Evas_Object* pView, void* pEventInfo)
                String redirectUrl(pImpl->GetRedirectUri(url, currentUrl, mime));
                if (redirectUrl != L"")
                {
-                       pImpl->LoadUrl(redirectUrl);
                        ewk_policy_decision_ignore(pPolicy);
 
+                       pImpl->LoadUrl(redirectUrl);
+
                        return;
                }
 
-               if (pImpl->IsMimeSupported(mime) || mime == L"")
+               if (ewk_policy_decision_type_get(pPolicy) == EWK_POLICY_DECISION_USE)
                {
                        break;
                }
-               
                else
                {
                        ewk_policy_decision_ignore(pPolicy);
@@ -1142,17 +1163,11 @@ OnWebDataReceived(void* pUserData, Evas_Object* pView, void* pEventInfo)
 
                        r = _AppControlImpl::FindAndStart(operationId, &url, &mime, null, null, null);
                        SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS || r == E_OBJ_NOT_FOUND, r, "[%s] Propagating.", GetErrorMessage(r));
-       
                        if (r == E_OBJ_NOT_FOUND)
                        {
-                               _SelectBox* pSelectBox = new (std::nothrow) _SelectBox;
-                               SysTryReturnVoidResult(NID_WEB_CTRL, pSelectBox, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
-
-                               r = pSelectBox->Construct(false, L"Select application", 1, null, url);
-                               SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
+                               String operationId(L"http://tizen.org/appcontrol/operation/download");
 
-                               pSelectBox->AddListItem(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_BR_BODY_DOWNLOAD"), _SelectBox::LIST_ITEM_TYPE_NORMAL, false);
-                               r = pSelectBox->ShowPopup();
+                               r = _AppControlImpl::FindAndStart(operationId, &url, null, null, null, null);
                                SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
                        }
 
@@ -1160,6 +1175,7 @@ OnWebDataReceived(void* pUserData, Evas_Object* pView, void* pEventInfo)
                }
                break;
        }
+
        case WEB_DECISION_DOWNLOAD:
        {
                ewk_policy_decision_ignore(pPolicy);
@@ -1170,16 +1186,18 @@ OnWebDataReceived(void* pUserData, Evas_Object* pView, void* pEventInfo)
                Ewk_Context* pContext = ewk_view_context_get(pView);
                SysAssertf(pContext, "Failed to get webkit instance.");
 
-               pDownloadHandler->StartDownload(pUrl);
+               pDownloadHandler->StartDownload(url);
 
                return;
        }
+
        case WEB_DECISION_IGNORE:
        {
                ewk_policy_decision_ignore(pPolicy);
 
                return;
        }
+
        default:
        {
                SysAssert(false);
@@ -1298,6 +1316,13 @@ OnLoadingCompleted(void* pUserData, Evas_Object* pView, void* pEventInfo)
 
 
 void
+OnLoadingCommitted(void* pUserData, Evas_Object* pView, void* pEventInfo)
+{
+       evas_object_focus_set(pView, EINA_FALSE);
+}
+
+
+void
 OnFaviconReceived(void* pUserData, Evas_Object* pView, void* pEventInfo)
 {
        _WebImpl* pImpl = reinterpret_cast< _WebImpl* >(pUserData);
@@ -1537,7 +1562,6 @@ FireWebPageBlockSelectedEventF(_WebImpl* pImpl)
        SysTryReturnResult(NID_WEB_CTRL, startPoint.get() && endPoint.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
 
        pImpl->GetBlockRange(*startPoint.get(), *endPoint.get());
-
        if ((endPoint->x != 0.0f) && (endPoint->y != 0.0f))
        {
                std::unique_ptr<_WebUiEventArg> pEventArg(new (std::nothrow) _WebUiEventArg(WEB_EVENT_WEBUIEVENTLISTENER_PAGE_BLOCK_SELECTED_FLOAT));
@@ -1613,11 +1637,6 @@ OnWebKeypadStateChanged(void* pUserData, Evas_Object* pView, void* pEventInfo)
        Eina_Rectangle* pEinaRect = reinterpret_cast< Eina_Rectangle* >(pEventInfo);
        SysAssertf(pImpl && pEinaRect, "Failed to request");
 
-       if (pImpl->IsVisible() == false)
-       {
-               evas_object_focus_set(pView, EINA_FALSE);
-       }
-
        if (pImpl->GetSetting().GetInputStyle() == INPUT_STYLE_OVERLAY)
        {
                _ICoordinateSystemTransformer* pXformer = _CoordinateSystem::GetInstance()->GetInverseTransformer();
@@ -1634,12 +1653,6 @@ OnWebKeypadStateChanged(void* pUserData, Evas_Object* pView, void* pEventInfo)
                if (pFormImpl)
                {
                        pFormImpl->DeflateClientRectHeight(pXformer->TransformVertical(rect.height));
-
-                       if (pFormImpl->HasFooter())
-                       {
-                               pFormImpl->GetCore().SetKeypadShowState(true);
-                               pFormImpl->Draw();
-                       }                       
                }
 
                if (pImpl->GetWebKeypadEventListener())
@@ -1656,7 +1669,6 @@ OnWebKeypadStateChanged(void* pUserData, Evas_Object* pView, void* pEventInfo)
                                pImpl->GetWebKeypadEventListener()->OnWebKeypadWillOpen(*dynamic_cast< Web* >(&pImpl->GetPublic()));
                        }
                }
-               pImpl->SetKeypadVisibleState(true);
                pImpl->SetPreviousKeypadBounds(rect);
        }
        else
@@ -1673,6 +1685,23 @@ OnWebKeypadOpened(void* pUserData, Evas_Object* pView, void* pEventInfo)
        _WebImpl* pImpl = reinterpret_cast< _WebImpl* >(pUserData);
        SysAssertf(pImpl, "Failed to request");
 
+       pImpl->SetKeypadVisibleState(true);
+
+       _FormImpl* pFormImpl = pImpl->GetParentFormImpl(dynamic_cast< _ControlImpl* >(pImpl));
+       if (pFormImpl)
+       {
+               _ICoordinateSystemTransformer* pXformer = _CoordinateSystem::GetInstance()->GetInverseTransformer();
+               SysAssertf(pXformer, "Failed to get CoordinateTransformer");
+
+               pFormImpl->DeflateClientRectHeight(pXformer->TransformVertical(pImpl->GetPreviousKeypadBounds().height));
+
+               if (pFormImpl->HasFooter())
+               {
+                       pFormImpl->GetCore().SetKeypadShowState(true);
+                       pFormImpl->Draw();
+               }
+       }
+
        if (pImpl->GetWebKeypadEventListener())
        {
                pImpl->GetWebKeypadEventListener()->OnWebKeypadOpened(*static_cast< Web* >(&pImpl->GetPublic()));
@@ -1708,20 +1737,46 @@ OnWebKeypadClosed(void* pUserData, Evas_Object* pView, void* pEventInfo)
 
 
 void
+OnWindowObjectFocusLost(void* pUserData, Evas_Object* pWin, void* pEvent_info)
+{
+       _WebImpl* pImpl = reinterpret_cast< _WebImpl* >(pUserData);
+       SysAssertf(pImpl, "Failed to request");
+
+       pImpl->SetWinFocusLost(true);
+}
+
+
+void
 OnWindowObjectFocusGained(void* pUserData, Evas_Object* pWin, void* pEvent_info)
 {
        _WebImpl* pImpl = reinterpret_cast< _WebImpl* >(pUserData);
        SysAssertf(pImpl, "Failed to request");
 
-       if (pImpl->IsKeypadOpened() == true && pImpl->IsVisible() == true && pImpl->IsFocused() == true)
+       if (pImpl->IsVisible() == true && pImpl->IsFocused() == true)
        {
                _Web* pWebCore = dynamic_cast< _Web* >(&(pImpl->GetCore()));
                SysAssertf(pWebCore, "Failed to get Web core object");
 
-               evas_object_focus_set(pWebCore->GetWebNativeNode(), EINA_TRUE);
+               if (pImpl->IsWinFocusLost() == true)
+               {
+                       if (pImpl->IsKeypadOpened() == true)
+                       {
+                               evas_object_focus_set(pWebCore->GetWebNativeNode(), EINA_TRUE);
+                       }
+
+                       Ewk_Settings* pSettings = ewk_view_settings_get(pWebCore->GetWebNativeNode());
+                       SysAssertf(pSettings, "Failed to get settings instance.");
+
+                       if (ewk_settings_clear_text_selection_automatically_get(pSettings) == EINA_FALSE)
+                       {
+                               ewk_settings_clear_text_selection_automatically_set(pSettings, EINA_TRUE);
 
-               pImpl->SetKeypadVisibleState(true);
+                               evas_object_focus_set(pWebCore->GetWebNativeNode(), EINA_TRUE);
+                       }
+               }
        }
+
+       pImpl->SetWinFocusLost(false);
 }
 
 
@@ -1733,7 +1788,26 @@ OnWebNativeNodeFocusGained(void* pUserData, Evas* pCanvas, Evas_Object* pView, v
 
        if (pImpl->IsKeypadOpened() == true)
        {
-               pImpl->SetKeypadOpened(false);  
+               pImpl->SetKeypadOpened(false);
+       }
+}
+
+
+void
+OnWebNativeNodeFocusLost(void* pUserData, Evas* pCanvas, Evas_Object* pView, void* pEventInfo)
+{
+       _WebImpl* pImpl = reinterpret_cast< _WebImpl* >(pUserData);
+       SysAssertf(pImpl, "Failed to request");
+
+       if (pImpl->IsWinFocusLost() == true)
+       {
+               _Web* pWebCore = dynamic_cast< _Web* >(&(pImpl->GetCore()));
+               SysAssertf(pWebCore, "Failed to get Web core object");
+
+               Ewk_Settings* pSettings = ewk_view_settings_get(pWebCore->GetWebNativeNode());
+               SysAssertf(pSettings, "Failed to get settings instance.");
+
+               ewk_settings_clear_text_selection_automatically_set(pSettings, EINA_FALSE);
        }
 } 
 
@@ -2084,6 +2158,7 @@ _WebImpl::_WebImpl(Web* pWeb, Tizen::Ui::_Control* pCore)
        , __isFooterVisible(false)
        , __isKeypadVisible(false)
        , __isKeypadOpened(false)
+       , __isWinFocusLost(false)
        , __isLoadingErrorOccurred(false)
        , __isRedirectRequested(false)
        , __isCertificateRequested(false)
@@ -2139,12 +2214,13 @@ _WebImpl::_WebImpl(Web* pWeb, Tizen::Ui::_Control* pCore)
 
 _WebImpl::~_WebImpl()
 {
-       _DownloadManagerImpl* pManagerImpl = _DownloadManagerImpl::GetInstance();
-       pManagerImpl->SetDownloadListener(null);
-
        _WebManager* pWebManager = _WebManager::GetInstance();
+       SysTryCatch(NID_WEB_CTRL, pWebManager, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+
        pWebManager->RemoveWeb(reinterpret_cast< int >(this));
+       pWebManager->RemoveActiveWeb(this);
 
+CATCH:
        RemoveEventListenerCallback();
 
        ClearCertificateDb();
@@ -2233,11 +2309,6 @@ _WebImpl::Construct(void)
        r = InitWebEvent();
        SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
 
-       _DownloadManagerImpl* pManagerImpl = _DownloadManagerImpl::GetInstance();
-       SysTryReturn(NID_WEB_CTRL, pManagerImpl, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
-
-       pManagerImpl->SetDownloadListener(this);
-
        __pWebCore = dynamic_cast< _Web* >(&(GetCore()));
        SysTryReturnResult(NID_WEB_CTRL, __pWebCore, E_SYSTEM, "A system error has been occurred. Failed to get web control");
 
@@ -2453,6 +2524,8 @@ _WebImpl::ClearSelectBoxPopup(void)
 
        __pSelectBox.reset();
 
+       Resume();
+
        return E_SUCCESS;
 }
 
@@ -2554,12 +2627,12 @@ _WebImpl::EvaluateJavascriptN(const String& scriptCode)
 
        ewk_view_script_execute(pView, pScript.get(), OnScriptExecuted, pPresenter.get());
 
-       std::unique_ptr<String> pResult(new (std::nothrow) String(L""));
-       SysTryReturn(NID_WEB_CTRL, pResult.get(), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+       String* pResult = new (std::nothrow) String(L"");
+       SysTryReturn(NID_WEB_CTRL, pResult, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
 
-       pPresenter->WaitAsyncProcess(*pResult.get());
+       pPresenter->WaitAsyncProcess(*pResult);
 
-       return pResult.release();
+       return pResult;
 }
 
 
@@ -3494,10 +3567,12 @@ _WebImpl::SetEventListenerCallback(void) const
                evas_object_smart_callback_add(pWebNativeNode, "load,stop", OnLoadingCanceled, this);
                evas_object_smart_callback_add(pWebNativeNode, "load,error", OnLoadingErrorOccurred, this);
                evas_object_smart_callback_add(pWebNativeNode, "title,changed", OnPageTitleReceived, this);
+               evas_object_smart_callback_add(pWebNativeNode, "load,committed", OnLoadingCommitted, this);
 
                evas_object_smart_callback_add(pWebNativeNode, "requestToNative,json", OnHandleJavaScriptRequest, this);
 
                evas_object_smart_callback_add(pWinObject, "focus,in", OnWindowObjectFocusGained, this);
+               evas_object_smart_callback_add(pWinObject, "focus,out", OnWindowObjectFocusLost, this);
 
                evas_object_smart_callback_add(pWebNativeNode, "inputmethod,changed", OnWebKeypadStateChanged, this);
                evas_object_smart_callback_add(pWebNativeNode, "editorclient,ime,opened", OnWebKeypadOpened, this);
@@ -3534,11 +3609,14 @@ _WebImpl::SetEventListenerCallback(void) const
                evas_object_smart_callback_add(pWebNativeNode, "touchmove,handled", OnWebPreventDefaultTriggered, this);
                evas_object_smart_callback_add(pWebNativeNode, "icon,received", OnFaviconReceived, this);
 
-               evas_object_smart_callback_add(pWebNativeNode, "text,selected", OnWebPageBlockSelected, this);
+               evas_object_smart_callback_add(pWebNativeNode, "magnifier,hide", OnWebPageBlockSelected, this);
 
                evas_object_smart_callback_add(pWebNativeNode, "fullscreen,enterfullscreen", OnFullScreenEntered, this);
                evas_object_smart_callback_add(pWebNativeNode, "fullscreen,exitfullscreen", OnFullScreenExited, this);
 
+               evas_object_smart_callback_add(pWebNativeNode, "keyevent,down,not,handled", OnFocusDownRequested, this);
+               evas_object_smart_callback_add(pWebNativeNode, "keyevent,up,not,handled", OnFocusUpRequested, this);
+
                Ewk_Context* pContext = ewk_view_context_get(pWebNativeNode);
                SysAssertf(pContext, "Failed to get webkit instance.");
                ewk_context_did_start_download_callback_set(pContext, OnDidStartDownloadCallback, const_cast< _WebImpl* >(this));
@@ -3550,6 +3628,7 @@ _WebImpl::SetEventListenerCallback(void) const
                ewk_view_exceeded_local_file_system_quota_callback_set(pWebNativeNode, OnLocalFileSystemQuotaExceeded, const_cast< _WebImpl* >(this));
 
                evas_object_event_callback_add(pWebNativeNode, EVAS_CALLBACK_FOCUS_IN, OnWebNativeNodeFocusGained, this);
+               evas_object_event_callback_add(pWebNativeNode, EVAS_CALLBACK_FOCUS_OUT, OnWebNativeNodeFocusLost, this);
        }
 }
 
@@ -3577,10 +3656,12 @@ _WebImpl::RemoveEventListenerCallback(void) const
                evas_object_smart_callback_del(pWebNativeNode, "load,stop", OnLoadingCanceled);
                evas_object_smart_callback_del(pWebNativeNode, "load,error", OnLoadingErrorOccurred);
                evas_object_smart_callback_del(pWebNativeNode, "title,changed", OnPageTitleReceived);
+               evas_object_smart_callback_del(pWebNativeNode, "load,committed", OnLoadingCommitted);
 
                evas_object_smart_callback_del(pWebNativeNode, "requestToNative,json", OnHandleJavaScriptRequest);
 
                evas_object_smart_callback_del_full(pWinObject, "focus,in", OnWindowObjectFocusGained, this);
+               evas_object_smart_callback_del_full(pWinObject, "focus,out", OnWindowObjectFocusLost, this);
 
                evas_object_smart_callback_del(pWebNativeNode, "inputmethod,changed", OnWebKeypadStateChanged);
                evas_object_smart_callback_del(pWebNativeNode, "editorclient,ime,opened", OnWebKeypadOpened);
@@ -3591,9 +3672,9 @@ _WebImpl::RemoveEventListenerCallback(void) const
 
                evas_object_smart_callback_del(pWebNativeNode, "geolocation,permission,request", OnGeolocationPermissionRequested);
 
-               ewk_view_javascript_alert_callback_set(null, null, null);
-               ewk_view_javascript_prompt_callback_set(null, null, null);
-               ewk_view_javascript_confirm_callback_set(null, null, null);
+               ewk_view_javascript_alert_callback_set(pWebNativeNode, null, null);
+               ewk_view_javascript_prompt_callback_set(pWebNativeNode, null, null);
+               ewk_view_javascript_confirm_callback_set(pWebNativeNode, null, null);
 
                evas_object_smart_callback_del(pWebNativeNode, "policy,response,decide", OnWebDataReceived);
 
@@ -3617,11 +3698,14 @@ _WebImpl::RemoveEventListenerCallback(void) const
                evas_object_smart_callback_del(pWebNativeNode, "touchmove,handled", OnWebPreventDefaultTriggered);
                evas_object_smart_callback_del(pWebNativeNode, "icon,received", OnFaviconReceived);
 
-               evas_object_smart_callback_del(pWebNativeNode, "text,selected", OnWebPageBlockSelected);
+               evas_object_smart_callback_del(pWebNativeNode, "magnifier,hide", OnWebPageBlockSelected);
 
                evas_object_smart_callback_del(pWebNativeNode, "fullscreen,enterfullscreen", OnFullScreenEntered);
                evas_object_smart_callback_del(pWebNativeNode, "fullscreen,exitfullscreen", OnFullScreenExited);
 
+               evas_object_smart_callback_del(pWebNativeNode, "keyevent,down,not,handled", OnFocusDownRequested);
+               evas_object_smart_callback_del(pWebNativeNode, "keyevent,up,not,handled", OnFocusUpRequested);
+
                Ewk_Context* pContext = ewk_view_context_get(pWebNativeNode);
                SysAssertf(pContext, "Failed to get webkit instance.");
                ewk_context_vibration_client_callbacks_set(pContext, null, null, null);
@@ -3632,6 +3716,7 @@ _WebImpl::RemoveEventListenerCallback(void) const
                ewk_view_exceeded_local_file_system_quota_callback_set(pWebNativeNode, null, null);
 
                evas_object_event_callback_del(pWebNativeNode, EVAS_CALLBACK_FOCUS_IN, OnWebNativeNodeFocusGained);
+               evas_object_event_callback_del(pWebNativeNode, EVAS_CALLBACK_FOCUS_OUT, OnWebNativeNodeFocusLost);
        }
 }
 
@@ -3672,42 +3757,37 @@ _WebImpl::SetBlockSelectionPosition(const Point& startPoint)
 result
 _WebImpl::SetBlockSelectionPosition(const FloatPoint& startPoint)
 {
-       Evas_Object* pView = __pWebCore->GetWebNativeNode();
-       Ewk_View_Smart_Data* pSmartData = (Ewk_View_Smart_Data*) evas_object_smart_data_get(pView);
-       if (pSmartData && pSmartData->api)
+       Evas_Object* pWebview = __pWebCore->GetWebNativeNode();
+       if (!pWebview)
        {
-               Point absPoint( _CoordinateSystemUtils::ConvertToInteger(__pWebCore->GetAbsoluteCoordinate(startPoint)));
+               return E_SUCCESS;
+       }
 
-               Eina_Bool ret = pSmartData->api->text_selection_down(pSmartData, absPoint.x, absPoint.y);
-               if (ret)
-               {
-                       pSmartData->api->text_selection_up(pSmartData, absPoint.x, absPoint.y);
+       Ewk_View_Smart_Data* pSmartData = reinterpret_cast< Ewk_View_Smart_Data* >(evas_object_smart_data_get(pWebview));
+       SysAssertf(pSmartData && pSmartData->api, "Failed to get webkit instance.");
+       Point absPoint(_CoordinateSystemUtils::ConvertToInteger(__pWebCore->GetAbsoluteCoordinate(startPoint)));
 
-                       Ewk_Hit_Test* pEwkHitTest = ewk_view_hit_test_new(pView, absPoint.x, absPoint.y, EWK_HIT_TEST_MODE_ALL);
-                       SysTryReturnResult(NID_WEB_CTRL, pEwkHitTest, E_SYSTEM, "Failed to get hit test.");
+       Eina_Bool ret = pSmartData->api->text_selection_down(pSmartData, absPoint.x, absPoint.y);
+       if (ret)
+       {
+               pSmartData->api->text_selection_up(pSmartData, absPoint.x, absPoint.y);
 
-                       String tagName(ewk_hit_test_tag_name_get(pEwkHitTest));
+               Ewk_Hit_Test* pEwkHitTest = ewk_view_hit_test_new(pWebview, absPoint.x, absPoint.y, EWK_HIT_TEST_MODE_ALL);
+               SysTryReturnResult(NID_WEB_CTRL, pEwkHitTest, E_SYSTEM, "Failed to get hit test.");
 
-                       if (tagName.Equals(L"INPUT", false) || tagName.Equals(L"TEXTAREA", false))
-                       {
-                               ewk_view_command_execute(pView, "SelectWord", 0);
-                       }
-                       else
-                       {
-                               Eina_Hash* pAttrHash = ewk_hit_test_attribute_hash_get(pEwkHitTest);
-                               char* pValue = reinterpret_cast< char* >(eina_hash_find(pAttrHash, "contenteditable"));
-                               if(pValue)
-                               {
-                                       ewk_view_command_execute(pView, "SelectWord", 0);
-                               }
-                       }
+               String tagName(ewk_hit_test_tag_name_get(pEwkHitTest));
+
+               Eina_Hash* pAttrHash = ewk_hit_test_attribute_hash_get(pEwkHitTest);
+               char* pValue = reinterpret_cast< char* >(eina_hash_find(pAttrHash, "contenteditable"));
+               if (tagName.Equals(L"INPUT", false) || tagName.Equals(L"TEXTAREA", false) || pValue)
+               {
+                       ewk_view_command_execute(pWebview, "SelectWord", 0);
+                       evas_object_smart_callback_call(pWebview, "magnifier,hide", NULL);
                }
        }
 
        SysTryReturnResult(NID_WEB_CTRL, GetTextFromBlock().GetLength() > 0, E_INVALID_ARG, "Failed to set text selection up.");
 
-       evas_object_smart_callback_call(pView, "text,selected", NULL);
-
        return E_SUCCESS;
 }
 
@@ -4011,6 +4091,20 @@ _WebImpl::SetKeypadOpened(bool isKeypadOpened)
 }
 
 
+bool
+_WebImpl::IsWinFocusLost(void) const
+{
+       return __isWinFocusLost;
+}
+
+
+void
+_WebImpl::SetWinFocusLost(bool isWinFocusLost)
+{
+       __isWinFocusLost = isWinFocusLost;
+}
+
+
 Rectangle
 _WebImpl::GetPreviousKeypadBounds(void) const
 {
@@ -4471,7 +4565,7 @@ _WebImpl::ShowCertificateConfirmPopup(_CertificatePopupMode userConfirmMode, Ewk
        std::unique_ptr<_CertificateConfirmPopup> pCertConfirmPopup(new (std::nothrow) _CertificateConfirmPopup());
        SysTryReturnResult(NID_WEB_CTRL, pCertConfirmPopup.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
 
-       r = pCertConfirmPopup->Construct(userConfirmMode, pPolicy, this);
+       r = pCertConfirmPopup->Construct(userConfirmMode, pPolicy);
        SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
 
        __pCertConfirmPopup = std::move(pCertConfirmPopup);
@@ -4638,14 +4732,13 @@ _WebImpl::ShowUserConfirmPopupAsync(_UserConfirmMode userConfirmMode, void* pPol
        {
        case USER_CONFIRM_USERMEDIA:
        {
-        __pUserMediaConfirmPopup.reset();
+               __pUserMediaConfirmPopup.reset();
 
                __pUserMediaConfirmPopup = std::move(pUserConfirmPopup);
 
                r = __pUserMediaConfirmPopup->ShowPopup();
                SysTryCatch(NID_WEB_CTRL, r == E_SUCCESS, __pUserMediaConfirmPopup.reset(), r, "[%s] Propagating.", GetErrorMessage(r));
 
-               __pUserMediaConfirmPopup.release();
                break;
        }
        case USER_CONFIRM_GEOLOCATION:
@@ -4657,7 +4750,6 @@ _WebImpl::ShowUserConfirmPopupAsync(_UserConfirmMode userConfirmMode, void* pPol
                r = __pGeolocationConfirmPopup->ShowPopup();
                SysTryCatch(NID_WEB_CTRL, r == E_SUCCESS, __pGeolocationConfirmPopup.reset(), r, "[%s] Propagating.", GetErrorMessage(r));
 
-               __pGeolocationConfirmPopup.release();
                break;
        }
        case USER_CONFIRM_NOTIFICATION:
@@ -4669,7 +4761,6 @@ _WebImpl::ShowUserConfirmPopupAsync(_UserConfirmMode userConfirmMode, void* pPol
                r = __pNotificationConfirmPopup->ShowPopup();
                SysTryCatch(NID_WEB_CTRL, r == E_SUCCESS, __pNotificationConfirmPopup.reset(), r, "[%s] Propagating.", GetErrorMessage(r));
 
-               __pNotificationConfirmPopup.release();
                break;
        }
        case USER_SCRIPT_ALERT:
@@ -4681,7 +4772,6 @@ _WebImpl::ShowUserConfirmPopupAsync(_UserConfirmMode userConfirmMode, void* pPol
                r = __pScriptAlertConfirmPopup->ShowPopup();
                SysTryCatch(NID_WEB_CTRL, r == E_SUCCESS, __pScriptAlertConfirmPopup.reset(), r, "[%s] Propagating.", GetErrorMessage(r));
 
-               __pScriptAlertConfirmPopup.release();
                break;
        }
        case USER_SCRIPT_CONFIRM:
@@ -4693,8 +4783,6 @@ _WebImpl::ShowUserConfirmPopupAsync(_UserConfirmMode userConfirmMode, void* pPol
                r = __pScriptConfirmPopup->ShowPopup();
                SysTryCatch(NID_WEB_CTRL, r == E_SUCCESS, __pScriptConfirmPopup.reset(), r, "[%s] Propagating.", GetErrorMessage(r));
 
-               __pScriptConfirmPopup.release();
-
                break;
        }
        default:
@@ -4733,12 +4821,6 @@ _WebImpl::GetParentFormImpl(_ControlImpl* pControlImpl)
 bool 
 _WebImpl::OnFocusGained(const _ControlImpl& source)
 {
-       Ewk_Settings* pSettings = ewk_view_settings_get(__pWebCore->GetWebNativeNode());
-       SysAssertf(pSettings, "Failed to get webkit instance.");
-       ewk_settings_clear_text_selection_automatically_set(pSettings, true);
-
-       evas_object_focus_set(__pWebCore->GetWebNativeNode(), EINA_TRUE);
-
        return false;
 }
 
@@ -4746,19 +4828,17 @@ _WebImpl::OnFocusGained(const _ControlImpl& source)
 bool 
 _WebImpl::OnFocusLost(const _ControlImpl& source)
 {
+       Evas_Object* pWebview = __pWebCore->GetWebNativeNode();
+       if (!pWebview)
+       {
+               return false;
+       }
+
        if(IsKeypadVisible() == true)
        {
                SetKeypadOpened(true);
-
-               SetKeypadVisibleState(false);
        }
 
-       Ewk_Settings* pSettings = ewk_view_settings_get(__pWebCore->GetWebNativeNode());
-       SysAssertf(pSettings, "Failed to get webkit instance.");
-       ewk_settings_clear_text_selection_automatically_set(pSettings, false);
-
-       evas_object_focus_set(__pWebCore->GetWebNativeNode(), EINA_FALSE);
-
        return false;
 }
 
@@ -4786,6 +4866,9 @@ _WebImpl::OnPreAttachedToMainTree(void)
                pWebManager->SetActiveWeb(this);
        }
 
+       r = _ContainerImpl::OnPreAttachedToMainTree();
+       SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
+
        if (!HasValidNativeNode())
        {
                r = __pWebCore->InitializeWebNativeNode();
@@ -4798,9 +4881,6 @@ _WebImpl::OnPreAttachedToMainTree(void)
                r = InitializeSetting();
                SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
 
-               r = _ContainerImpl::OnPreAttachedToMainTree();
-               SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
-
                _WebManager* pWebManager = _WebManager::GetInstance();
                pWebManager->AddWeb(reinterpret_cast< int >(this));
        }
@@ -4812,8 +4892,13 @@ _WebImpl::OnPreAttachedToMainTree(void)
 result
 _WebImpl::OnDetachingFromMainTree(void)
 {
+       result r = E_SUCCESS;
+
        _WebManager* pWebManager = _WebManager::GetInstance();
-       pWebManager->RemoveActiveWeb(this);     
+       pWebManager->RemoveActiveWeb(this);
+
+       r = _ContainerImpl::OnDetachingFromMainTree();
+       SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
 
        return E_SUCCESS;
 }
@@ -4889,7 +4974,7 @@ void
 _WebImpl::OnUserEventReceivedN(RequestId requestId, Tizen::Base::Collection::IList* pArgs)
 {
        _ContainerImpl::OnUserEventReceivedN(requestId, pArgs);
-
+       
        switch (requestId)
        {
        case ID_CERTIFICATE_CONFIRM_POPUP_CLOSE:
@@ -4916,6 +5001,8 @@ _WebImpl::OnUserEventReceivedN(RequestId requestId, Tizen::Base::Collection::ILi
        default:
                break;
        }
+
+       Resume();
 }
 
 
@@ -4931,6 +5018,8 @@ _WebImpl::HideColorPicker(void)
 {
        __pColorPicker.reset();
 
+       Resume();
+
        return EINA_TRUE;
 }
 
@@ -4962,6 +5051,7 @@ CATCH:
        if (__pColorPicker)
        {
                __pColorPicker.reset();
+               Resume();
        }
 
        return r;
@@ -4993,6 +5083,7 @@ CATCH:
        if (__pDatePicker)
        {
                __pDatePicker.reset();
+               Resume();
        }
 
        return r;
@@ -5321,11 +5412,4 @@ _WebImpl::SetWebNotification(_WebNotification* pWebNotification)
 }
 
 
-void
-_WebImpl::OnDownloadCompleted(RequestId reqId, const String &path)
-{
-       _ContentManagerImpl::ScanFile(path);
-}
-
-
 }}} // Tizen::Web::Controls