Fix prevent issue
[platform/framework/web/wrt.git] / src / wrt-client / wrt-client.cpp
index 7429412..188bf04 100644 (file)
@@ -25,7 +25,7 @@
 #include <dpl/log/log.h>
 #include <dpl/optional_typedefs.h>
 #include <dpl/exception.h>
-#include <common/application_data.h>
+#include <application_data.h>
 #include <core_module.h>
 #include <localization_setting.h>
 #include <widget_deserialize_model.h>
 #include <dpl/localization/w3c_file_localization.h>
 #include <dpl/localization/LanguageTagsProvider.h>
 #include <popup-runner/PopupInvoker.h>
-#include <prepare_external_storage.h>
 #include <vconf.h>
 #include "auto_rotation_support.h"
 
-#include "process_pool.h"
-#include "menu_db_util.h"
-#include "launchpad_util.h"
-#include <appsvc.h>
+#include <process_pool.h>
+#include <process_pool_launchpad_util.h>
 
+#include "client_command_line_parser.h"
+#include "client_ide_support.h"
+#include "client_security_support.h"
 #include "client_service_support.h"
-
+#include "client_submode_support.h"
 
 //W3C PACKAGING enviroment variable name
 #define W3C_DEBUG_ENV_VARIABLE "DEBUG_LOAD_FINISH"
 
 // window signal callback
-const char *EDJE_SHOW_BACKWARD_SIGNAL = "show,backward,signal";
 const char *EDJE_SHOW_PROGRESS_SIGNAL = "show,progress,signal";
 const char *EDJE_HIDE_PROGRESS_SIGNAL = "hide,progress,signal";
 const std::string VIEWMODE_TYPE_FULLSCREEN = "fullscreen";
 const std::string VIEWMODE_TYPE_MAXIMIZED = "maximized";
 const std::string VIEWMODE_TYPE_WINDOWED = "windowed";
 char const* const ELM_SWALLOW_CONTENT = "elm.swallow.content";
-const char* const BUNDLE_PATH = "/usr/lib/libwrt-injected-bundle.so";
-const char* const caCertPath = "/opt/usr/share/certs/ca-certificate.crt";
+const char* const BUNDLE_PATH = LIBDIR_PREFIX "/usr/lib/libwrt-injected-bundle.so";
 const char* const MESSAGE_NAME_INITIALIZE = "ToInjectedBundle::INIT";
+const unsigned int UID_ROOT = 0;
 
 // process pool
 const char* const DUMMY_PROCESS_PATH = "/usr/bin/wrt_launchpad_daemon_candidate";
@@ -75,17 +74,17 @@ const char* const ROOT_HOME_PATH = "/opt/home/root";
 WrtClient::WrtClient(int argc, char **argv) :
     Application(argc, argv, "wrt-client", false),
     DPL::TaskDecl<WrtClient>(this),
-    m_appControlIndex(DPL::OptionalUInt()),
     m_launched(false),
     m_initializing(false),
     m_initialized(false),
-    m_sdkLauncherPid(0),
     m_debugMode(false),
-    m_debuggerPort(0),
     m_returnStatus(ReturnStatus::Succeeded),
     m_widgetState(WidgetState::WidgetState_Stopped),
     m_initialViewMode(VIEWMODE_TYPE_MAXIMIZED),
-    m_currentViewMode(VIEWMODE_TYPE_MAXIMIZED)
+    m_currentViewMode(VIEWMODE_TYPE_MAXIMIZED),
+    m_isWebkitFullscreen(false),
+    m_isFullscreenByPlatform(false),
+    m_submodeSupport(new ClientModule::SubmodeSupport())
 {
     Touch();
     LogDebug("App Created");
@@ -103,12 +102,12 @@ WrtClient::ReturnStatus::Type WrtClient::getReturnStatus() const
 
 void WrtClient::OnStop()
 {
-    LogInfo("Stopping Dummy Client");
+    LogDebug("Stopping Dummy Client");
 }
 
 void WrtClient::OnCreate()
 {
-    LogInfo("On Create");
+    LogDebug("On Create");
     ADD_PROFILING_POINT("OnCreate callback", "point");
     ewk_init();
 }
@@ -129,6 +128,11 @@ void WrtClient::OnPause()
         LogWarning("Widget is not running to be suspended");
         return;
     }
+    if (m_submodeSupport->isNeedTerminateOnSuspend()) {
+        LogDebug("Current mode cannot support suspend");
+        elm_exit();
+        return;
+    }
     m_widget->Suspend();
     m_widgetState = WidgetState_Suspended;
 }
@@ -151,13 +155,15 @@ void WrtClient::OnReset(bundle *b)
             return;
         }
         m_widget->Reset();
-        m_windowData->emitSignalForUserLayout(EDJE_SHOW_BACKWARD_SIGNAL, "");
         m_widgetState = WidgetState_Running;
     } else {
-        if (true == checkArgument()) {
-            setStep();
-        } else {
+        m_tizenId =
+            ClientModule::CommandLineParser::getTizenId(m_argc, m_argv);
+        if (m_tizenId.empty()) {
             showHelpAndQuit();
+        } else {
+            setDebugMode(b);
+            setStep();
         }
     }
 
@@ -190,80 +196,11 @@ void WrtClient::showHelpAndQuit()
     Quit();
 }
 
-bool WrtClient::checkArgument()
-{
-    std::string tizenId = getTizenIdFromArgument(m_argc, m_argv);
-
-    if (tizenId.empty()) {
-        // Just show help
-        return false;
-    } else {
-        std::size_t pos =
-            tizenId.find(WrtDB::AppControlPrefix::PROCESS_PREFIX);
-        if (pos != std::string::npos) {
-            std::string id = tizenId.substr(pos);
-            id.erase(strlen(WrtDB::AppControlPrefix::PROCESS_PREFIX));
-            std::stringstream s(id);
-            unsigned int appControlIndex;
-            s >> appControlIndex;
-            m_appControlIndex = appControlIndex;
-            tizenId.erase(pos);
-            LogDebug("app-control execute-index : " << appControlIndex);
-        }
-        m_tizenId = tizenId;
-        LogDebug("Tizen id: " << m_tizenId);
-        return true;
-    }
-}
-
-std::string WrtClient::getTizenIdFromArgument(int argc, char **argv)
-{
-    LogInfo("checkArgument");
-    std::string arg = argv[0];
-
-    if (arg.empty()) {
-        return "";
-    }
-
-    if (arg.find("wrt-client") != std::string::npos) {
-        if (argc <= 1) {
-            return "";
-        }
-
-        arg = argv[1];
-
-        if (arg == "-h" || arg == "--help") {
-            return "";
-        } else if (arg == "-l" || arg == "--launch" ||
-                   arg == "-t" || arg == "--tizen")
-        {
-            if (argc != 3) {
-                return "";
-            }
-            return argv[2];
-        } else {
-            return "";
-        }
-    } else {
-        // Launch widget based on application basename
-        size_t pos = arg.find_last_of('/');
-
-        if (pos != std::string::npos) {
-            arg = arg.erase(0, pos + 1);
-        }
-
-        return arg;
-    }
-}
-
 void WrtClient::setStep()
 {
-    LogInfo("setStep");
+    LogDebug("setStep");
 
     AddStep(&WrtClient::initStep);
-
-    setSdkLauncherDebugData();
-
     AddStep(&WrtClient::launchStep);
     AddStep(&WrtClient::shutdownStep);
 
@@ -272,38 +209,10 @@ void WrtClient::setStep()
     DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(NextStepEvent());
 }
 
-void WrtClient::setSdkLauncherDebugData()
+void WrtClient::setDebugMode(bundle* b)
 {
-    LogDebug("setSdkLauncherDebugData");
-
-    /* check bundle from sdk launcher */
-    bundle *bundleFromSdkLauncher;
-    bundleFromSdkLauncher = bundle_import_from_argv(m_argc, m_argv);
-    const char *bundle_debug = bundle_get_val(bundleFromSdkLauncher, "debug");
-    const char *bundle_pid = bundle_get_val(bundleFromSdkLauncher, "pid");
-    if (bundle_debug != NULL && bundle_pid != NULL) {
-        if (strcmp(bundle_debug, "true") == 0) {
-            m_debugMode = true;
-            m_sdkLauncherPid = atoi(bundle_pid);
-        } else {
-            m_debugMode = false;
-        }
-    }
-    bundle_free(bundleFromSdkLauncher);
-}
-
-bool WrtClient::checkDebugMode(SDKDebugData* debugData)
-{
-    LogError("Checking for debug mode");
-    Assert(m_dao);
-
-    bool debugMode = debugData->debugMode;
-
-    LogInfo("[DEBUG_MODE] Widget is launched in " <<
-            (debugMode ? "DEBUG" : "RETAIL") <<
-            " mode.");
-
-    return debugMode;
+    m_debugMode = ClientModule::IDESupport::getDebugMode(b);
+    LogDebug("debug mode : " << m_debugMode);
 }
 
 void WrtClient::OnEventReceived(const NextStepEvent& /*event*/)
@@ -335,27 +244,7 @@ void WrtClient::loadFinishCallback(Evas_Object* webview)
         m_splashScreen->stopSplashScreenBuffered();
     }
 
-    SDKDebugData* debug = new SDKDebugData;
-    debug->debugMode = m_debugMode;
-    debug->pid = new unsigned long(getpid());
-
-    LogInfo("Post result of launch");
-
-    // Start inspector server, if current mode is debugger mode.
-    // In the WK2 case, ewk_view_inspector_server_start should
-    // be called after WebProcess is created.
-    if (checkDebugMode(debug)) {
-        debug->portnum =
-            ewk_view_inspector_server_start(m_widget->GetCurrentWebview(), 0);
-        if (debug->portnum == 0) {
-            LogWarning("Failed to get portnum");
-        } else {
-            LogInfo("Assigned port number for inspector : "
-                    << debug->portnum);
-        }
-    } else {
-        LogDebug("Debug mode is disabled");
-    }
+    LogDebug("Post result of launch");
 
     //w3c packaging test debug (message on 4>)
     const char * makeScreen = getenv(W3C_DEBUG_ENV_VARIABLE);
@@ -383,32 +272,28 @@ void WrtClient::loadFinishCallback(Evas_Object* webview)
             PostEvent(NextStepEvent());
     }
 
-    if (debug->debugMode) {
-        LogDebug("Send RT signal to wrt-launcher(pid: " << m_sdkLauncherPid);
-        union sigval sv;
-        /* send real time signal with result to wrt-launcher */
-        if (webview) {
-            LogDebug("userData->portnum : " << debug->portnum);
-            sv.sival_int = debug->portnum;
-        } else {
-            sv.sival_int = -1;
+    if (m_debugMode) {
+        unsigned int portNum =
+            ewk_view_inspector_server_start(m_widget->GetCurrentWebview(), 0);
+        LogDebug("Port for inspector : " << portNum);
+        bool ret = ClientModule::IDESupport::sendReply(
+                       ApplicationDataSingleton::Instance().getBundle(),
+                       portNum);
+        if (!ret) {
+            LogWarning("Fail to send reply");
         }
-
-        bundle* request = NULL;
-        appsvc_create_result_bundle(
-            ApplicationDataSingleton::Instance().getBundle(),
-            &request);
-        char port[10] = {0,};
-        sprintf(port, "%d", debug->portnum);
-        appsvc_add_data(request, "port", port);
-        appsvc_send_result(request, APPSVC_RES_OK);
     }
 
     ApplicationDataSingleton::Instance().freeBundle();
+}
 
-    LogDebug("Cleaning wrtClient launch resources...");
-    delete debug->pid;
-    delete debug;
+void WrtClient::resetCallback(bool result)
+{
+    if (!result) {
+        LogDebug("Fail to handle reset event");
+        // free bundle data
+        ApplicationDataSingleton::Instance().freeBundle();
+    }
 }
 
 void WrtClient::progressStartedCallback()
@@ -416,7 +301,9 @@ void WrtClient::progressStartedCallback()
     if (m_settingList->getProgressBarPresence() == ProgressBar_Enable ||
         m_currentViewMode == VIEWMODE_TYPE_WINDOWED)
     {
-        m_windowData->emitSignalForUserLayout(EDJE_SHOW_PROGRESS_SIGNAL, "");
+        m_windowData->signalEmit(Layer::MAIN_LAYOUT,
+                                 EDJE_SHOW_PROGRESS_SIGNAL,
+                                 "");
         m_windowData->updateProgress(0);
     }
 }
@@ -435,7 +322,9 @@ void WrtClient::progressFinishCallback()
     if (m_settingList->getProgressBarPresence() == ProgressBar_Enable ||
         m_currentViewMode == VIEWMODE_TYPE_WINDOWED)
     {
-        m_windowData->emitSignalForUserLayout(EDJE_HIDE_PROGRESS_SIGNAL, "");
+        m_windowData->signalEmit(Layer::MAIN_LAYOUT,
+                                 EDJE_HIDE_PROGRESS_SIGNAL,
+                                 "");
     }
 }
 
@@ -455,21 +344,27 @@ void WrtClient::webCrashCallback()
         NextStepEvent());
 }
 
-void WrtClient::toggleFullscreenCallback(bool fullscreen)
+void WrtClient::enterFullscreenCallback(Evas_Object* /*obj*/,
+                                        bool isFullscreenByPlatform)
 {
-    LogError("toggleFullscreen");
-
-    if (fullscreen) {
-        // enter fullscreen
-        m_windowData->toggleFullscreen(true);
-        m_currentViewMode = VIEWMODE_TYPE_FULLSCREEN;
-    } else {
-        // exit fullscreen
-        m_windowData->toggleFullscreen(false);
-        m_currentViewMode = m_initialViewMode;
+    // enter fullscreen
+    m_windowData->toggleFullscreen(true);
+    m_currentViewMode = VIEWMODE_TYPE_FULLSCREEN;
+    m_isWebkitFullscreen = true;
+    if (isFullscreenByPlatform) {
+        m_isFullscreenByPlatform = true;
     }
 }
 
+void WrtClient::exitFullscreenCallback(Evas_Object* /*obj*/)
+{
+    // exit fullscreen
+    m_windowData->toggleFullscreen(false);
+    m_currentViewMode = m_initialViewMode;
+    m_isWebkitFullscreen = false;
+    m_isFullscreenByPlatform = false;
+}
+
 void WrtClient::launchStep()
 {
     ADD_PROFILING_POINT("launchStep", "start");
@@ -477,7 +372,7 @@ void WrtClient::launchStep()
 
     ADD_PROFILING_POINT("getRunnableWidgetObject", "start");
     m_widget = WRT::CoreModuleSingleton::Instance()
-            .getRunnableWidgetObject(m_tizenId, m_appControlIndex);
+            .getRunnableWidgetObject(m_tizenId);
     ADD_PROFILING_POINT("getRunnableWidgetObject", "stop");
 
     if (!m_widget) {
@@ -505,6 +400,7 @@ void WrtClient::launchStep()
     WrtDB::WidgetSettings widgetSettings;
     m_dao->getWidgetSettings(widgetSettings);
     m_settingList.reset(new WidgetSettingList(widgetSettings));
+    m_submodeSupport->initialize(DPL::FromASCIIString(m_tizenId));
 
     DPL::Optional<DPL::String> defloc = m_dao->getDefaultlocale();
     if (!defloc.IsNull()) {
@@ -513,7 +409,6 @@ void WrtClient::launchStep()
     }
 
     setInitialViewMode();
-    PrepareExternalStorageSingleton::Instance().Initialize(m_dao->getTizenPkgId());
 
     /* remove language change callback */
     /*
@@ -529,6 +424,11 @@ void WrtClient::launchStep()
         s_preparedWindowData = NULL;
     }
     ADD_PROFILING_POINT("CreateWindow", "stop");
+    if (!m_windowData->initScreenReaderSupport(
+            m_settingList->getAccessibility() == Accessibility_Enable))
+    {
+        LogWarning("Fail to set screen reader support set");
+    }
 
     // rotate window to initial value
     setWindowInitialOrientation();
@@ -540,17 +440,21 @@ void WrtClient::launchStep()
     DPL::OptionalString splashImgSrc = m_dao->getSplashImgSrc();
     if (!splashImgSrc.IsNull())
     {
-        m_splashScreen.reset(new SplashScreenSupport(m_windowData->m_win,
-            (DPL::ToUTF8String(*splashImgSrc)).c_str(),
-             m_currentViewMode != VIEWMODE_TYPE_FULLSCREEN,
-             m_settingList->getRotationValue() == Screen_Landscape));
+        m_splashScreen.reset(
+            new SplashScreenSupport(
+                m_windowData->getEvasObject(Layer::WINDOW),
+                (DPL::ToUTF8String(*splashImgSrc)).c_str(),
+                m_currentViewMode != VIEWMODE_TYPE_FULLSCREEN,
+                m_settingList->getRotationValue() == Screen_Landscape));
         m_splashScreen->startSplashScreen();
     }
     ADD_PROFILING_POINT("Create splash screen", "stop");
 
     DPL::OptionalString startUrl = W3CFileLocalization::getStartFile(m_dao);
-    if (!m_widget->PrepareView(DPL::ToUTF8String(*startUrl),
-            m_windowData->m_win, s_preparedEwkContext))
+    if (!m_widget->PrepareView(
+            DPL::ToUTF8String(*startUrl),
+            m_windowData->getEvasObject(Layer::WINDOW),
+            s_preparedEwkContext))
     {
         DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(
             NextStepEvent());
@@ -562,6 +466,29 @@ void WrtClient::launchStep()
     //you can't show window with splash screen before PrepareView
     //ewk_view_add_with_context() in viewLogic breaks window
     m_windowData->init();
+    m_windowData->postInit();
+
+#if X11
+    // sub-mode support
+    if (m_submodeSupport->isInlineMode()) {
+        if (m_submodeSupport->transientWindow(
+                elm_win_xwindow_get(
+                    m_windowData->getEvasObject(Layer::WINDOW))))
+        {
+            LogDebug("Success to set submode");
+        } else {
+            LogWarning("Fail to set submode");
+        }
+    }
+#endif
+    m_windowData->smartCallbackAdd(Layer::FOCUS,
+                                   "focused",
+                                   focusedCallback,
+                                   this);
+    m_windowData->smartCallbackAdd(Layer::FOCUS,
+                                   "unfocused",
+                                   unfocusedCallback,
+                                   this);
 
     WrtDB::WidgetLocalizedInfo localizedInfo =
         W3CFileLocalization::getLocalizedInfo(m_dao);
@@ -569,10 +496,11 @@ void WrtClient::launchStep()
     if (!(localizedInfo.name.IsNull())) {
         name = DPL::ToUTF8String(*(localizedInfo.name));
     }
-    elm_win_title_set(m_windowData->m_win, name.c_str());
+    elm_win_title_set(m_windowData->getEvasObject(Layer::WINDOW),
+                      name.c_str());
 
     // window show
-    evas_object_show(m_windowData->m_win);
+    evas_object_show(m_windowData->getEvasObject(Layer::WINDOW));
 
     initializeWindowModes();
 
@@ -583,24 +511,25 @@ void WrtClient::launchStep()
             NextStepEvent());
         return;
     }
-    LogInfo("Widget launch accepted. Entering running state");
+    LogDebug("Widget launch accepted. Entering running state");
     m_widgetState = WidgetState_Running;
 
     cbs->progressStarted = DPL::MakeDelegate(this, &WrtClient::progressStartedCallback);
     cbs->progress = DPL::MakeDelegate(this, &WrtClient::loadProgressCallback);
     cbs->progressFinish = DPL::MakeDelegate(this, &WrtClient::progressFinishCallback);
     cbs->loadFinish = DPL::MakeDelegate(this, &WrtClient::loadFinishCallback);
+    cbs->reset = DPL::MakeDelegate(this, &WrtClient::resetCallback);
     cbs->bufferSet = DPL::MakeDelegate(this, &WrtClient::setLayout);
     cbs->bufferUnset = DPL::MakeDelegate(this, &WrtClient::unsetLayout);
     cbs->webkitExit = DPL::MakeDelegate(this, &WrtClient::webkitExitCallback);
     cbs->webCrash = DPL::MakeDelegate(this, &WrtClient::webCrashCallback);
-    cbs->toggleFullscreen = DPL::MakeDelegate(this, &WrtClient::toggleFullscreenCallback);
+    cbs->enterFullscreen = DPL::MakeDelegate(this, &WrtClient::enterFullscreenCallback);
+    cbs->exitFullscreen = DPL::MakeDelegate(this, &WrtClient::exitFullscreenCallback);
     cbs->setOrientation = DPL::MakeDelegate(this, &WrtClient::setWindowOrientation);
     cbs->hwkey = DPL::MakeDelegate(this, &WrtClient::hwkeyCallback);
 
     m_widget->SetUserDelegates(cbs);
     m_widget->Show();
-    m_windowData->emitSignalForUserLayout(EDJE_SHOW_BACKWARD_SIGNAL, "");
 
     ADD_PROFILING_POINT("launchStep", "stop");
 }
@@ -615,21 +544,10 @@ void WrtClient::initializeWindowModes()
                               backbutton);
 }
 
-void WrtClient::backButtonCallback(void* data,
-                                   Evas_Object * /*obj*/,
-                                   void * /*event_info*/)
-{
-    LogInfo("BackButtonCallback");
-    Assert(data);
-
-    WrtClient* This = static_cast<WrtClient*>(data);
-    This->m_widget->Backward();
-}
-
 Eina_Bool WrtClient::naviframeBackButtonCallback(void* data,
                                                  Elm_Object_Item* /*it*/)
 {
-    LogInfo("BackButtonCallback");
+    LogDebug("BackButtonCallback");
     Assert(data);
 
     WrtClient* This = static_cast<WrtClient*>(data);
@@ -639,12 +557,19 @@ Eina_Bool WrtClient::naviframeBackButtonCallback(void* data,
 
 int WrtClient::appcoreLowMemoryCallback(void* /*data*/)
 {
-    LogInfo("appcoreLowMemoryCallback");
+    LogDebug("appcoreLowMemoryCallback");
     //WrtClient* This = static_cast<WrtClient*>(data);
 
     // TODO call RunnableWidgetObject API regarding low memory
     // The API should be implemented
 
+    // temporary solution because we have no way to get ewk_context from runnable object.
+    if (s_preparedEwkContext)
+    {
+        ewk_context_cache_clear(s_preparedEwkContext);
+        ewk_context_notify_low_memory(s_preparedEwkContext);
+    }
+
     return 0;
 }
 
@@ -693,9 +618,10 @@ void WrtClient::setWindowInitialOrientation(void)
     } else if (rotationValue == Screen_Landscape) {
         setWindowOrientation(OrientationAngle::Window::Landscape::PRIMARY);
     } else if (rotationValue == Screen_AutoRotation) {
-        if (!AutoRotationSupport::setAutoRotation(m_windowData->m_win,
-                                                  autoRotationCallback,
-                                                  this))
+        if (!AutoRotationSupport::setAutoRotation(
+                m_windowData->getEvasObject(Layer::WINDOW),
+                autoRotationCallback,
+                this))
         {
             LogError("Fail to set auto rotation");
         }
@@ -717,7 +643,9 @@ void WrtClient::unsetWindowOrientation(void)
 
     WidgetSettingScreenLock rotationValue = m_settingList->getRotationValue();
     if (rotationValue == Screen_AutoRotation) {
-        AutoRotationSupport::unsetAutoRotation(m_windowData->m_win, autoRotationCallback);
+        AutoRotationSupport::unsetAutoRotation(
+            m_windowData->getEvasObject(Layer::WINDOW),
+            autoRotationCallback);
     }
 }
 
@@ -746,6 +674,18 @@ void WrtClient::setEwkInitialOrientation(void)
     }
 }
 
+void WrtClient::ExitCallback(void* data,
+                                   Evas_Object * /*obj*/,
+                                   void * /*event_info*/)
+{
+    LogInfo("ExitCallback");
+    Assert(data);
+
+    WrtClient* This = static_cast<WrtClient*>(data);
+
+    This->OnTerminate();
+}
+
 void WrtClient::setCtxpopupItem(void)
 {
     WindowData::CtxpopupItemDataList data;
@@ -779,19 +719,25 @@ void WrtClient::setCtxpopupItem(void)
 void WrtClient::ctxpopupShare(void)
 {
     LogDebug("share");
+#ifdef X11
     const char* url = ewk_view_url_get(m_widget->GetCurrentWebview());
     if (!url) {
         LogError("url is empty");
         return;
     }
     if (ClientModule::ServiceSupport::launchShareService(
-            elm_win_xwindow_get(m_windowData->m_win),
+            elm_win_xwindow_get(m_windowData->getEvasObject(Layer::WINDOW)),
             url))
     {
-        LogInfo("success");
+        LogDebug("success");
     } else {
-        LogInfo("fail");
+        LogDebug("fail");
     }
+    evas_object_smart_callback_add(m_windowData->m_win,
+            "delete,request",
+            &WrtClient::ExitCallback,
+            this);
+#endif
 }
 
 void WrtClient::ctxpopupReload(void)
@@ -803,19 +749,21 @@ void WrtClient::ctxpopupReload(void)
 void WrtClient::ctxpopupLaunchBrowser(void)
 {
     LogDebug("launchBrowser");
+#ifdef X11
     const char* url = ewk_view_url_get(m_widget->GetCurrentWebview());
     if (!url) {
         LogError("url is empty");
         return;
     }
     if (ClientModule::ServiceSupport::launchViewService(
-            elm_win_xwindow_get(m_windowData->m_win),
+            elm_win_xwindow_get(m_windowData->getEvasObject(Layer::WINDOW)),
             url))
     {
-        LogInfo("success");
+        LogDebug("success");
     } else {
-        LogInfo("fail");
+        LogDebug("fail");
     }
+#endif
 }
 
 void WrtClient::hwkeyCallback(const std::string& key)
@@ -823,35 +771,46 @@ void WrtClient::hwkeyCallback(const std::string& key)
     if (m_settingList->getBackButtonPresence() == BackButton_Enable
         || m_currentViewMode == VIEWMODE_TYPE_WINDOWED)
     {
-        if (key == "back") {
-            m_widget->Backward();
-        } else if (key == "menu") {
+        // windowed UX - hosted application
+        if (key == KeyName::BACK) {
+            if (m_isWebkitFullscreen) {
+                // FIXME!!! This method has not yet landed in the tizen 3.0
+                //          webkit-efl source tree
+                //ewk_view_fullscreen_exit(m_widget->GetCurrentWebview());
+            } else {
+                m_widget->Backward();
+            }
+        } else if (key == KeyName::MENU) {
             // UX isn't confirmed
             // m_windowData->showCtxpopup();
         }
+    } else {
+        // packaged application
+        if (key == KeyName::BACK) {
+            if (m_isFullscreenByPlatform) {
+                // FIXME!!! This method has not yet landed in the tizen 3.0
+                //          webkit-efl source tree
+                //ewk_view_fullscreen_exit(m_widget->GetCurrentWebview());
+            }
+        }
     }
 }
 
-void WrtClient::setLayout(Evas_Object* newBuffer)
+void WrtClient::setLayout(Evas_Object* webview)
 {
     LogDebug("add new webkit buffer to window");
-    Assert(newBuffer);
-
-    elm_object_part_content_set(m_windowData->m_user_layout,
-                                ELM_SWALLOW_CONTENT,
-                                newBuffer);
-
-    evas_object_show(newBuffer);
+    Assert(webview);
+    m_windowData->setWebview(webview);
+    evas_object_show(webview);
+    evas_object_show(m_windowData->getEvasObject(Layer::WINDOW));
 }
 
-void WrtClient::unsetLayout(Evas_Object* currentBuffer)
+void WrtClient::unsetLayout(Evas_Object* webview)
 {
     LogDebug("remove current webkit buffer from window");
-    Assert(currentBuffer);
-    evas_object_hide(currentBuffer);
-
-    elm_object_part_content_unset(m_windowData->m_user_layout,
-                                  ELM_SWALLOW_CONTENT);
+    Assert(webview);
+    evas_object_hide(webview);
+    m_windowData->unsetWebview();
 }
 
 void WrtClient::shutdownStep()
@@ -861,12 +820,18 @@ void WrtClient::shutdownStep()
     if (m_widget && m_widgetState) {
         m_widgetState = WidgetState_Stopped;
         m_widget->Hide();
-        // AutoRotation use m_widget pointer internally.
+        // AutoRotation, focusCallback use m_widget pointer internally.
         // It must be unset before m_widget is released.
+        m_submodeSupport->deinitialize();
         unsetWindowOrientation();
+        m_windowData->smartCallbackDel(Layer::FOCUS,
+                                       "focused",
+                                       focusedCallback);
+        m_windowData->smartCallbackDel(Layer::FOCUS,
+                                       "unfocused",
+                                       unfocusedCallback);
         m_widget.reset();
         ewk_context_delete(s_preparedEwkContext);
-        PrepareExternalStorageSingleton::Instance().Deinitialize();
         WRT::CoreModuleSingleton::Instance().Terminate();
     }
     if (m_initialized) {
@@ -878,19 +843,38 @@ void WrtClient::shutdownStep()
 
 void WrtClient::autoRotationCallback(void* data, Evas_Object* obj, void* /*event*/)
 {
-    LogInfo("entered");
+    LogDebug("entered");
 
     Assert(data);
     Assert(obj);
 
     WrtClient* This = static_cast<WrtClient*>(data);
-
     This->autoRotationSetOrientation(obj);
 }
 
+void WrtClient::focusedCallback(void* data,
+                                Evas_Object* /*obj*/,
+                                void* /*eventInfo*/)
+{
+    LogDebug("entered");
+    Assert(data);
+    WrtClient* This = static_cast<WrtClient*>(data);
+    elm_object_focus_set(This->m_widget->GetCurrentWebview(), EINA_TRUE);
+}
+
+void WrtClient::unfocusedCallback(void* data,
+                                Evas_Object* /*obj*/,
+                                void* /*eventInfo*/)
+{
+    LogDebug("entered");
+    Assert(data);
+    WrtClient* This = static_cast<WrtClient*>(data);
+    elm_object_focus_set(This->m_widget->GetCurrentWebview(), EINA_FALSE);
+}
+
 void WrtClient::autoRotationSetOrientation(Evas_Object* obj)
 {
-    LogInfo("entered");
+    LogDebug("entered");
     Assert(obj);
 
     AutoRotationSupport::setOrientation(obj, m_widget->GetCurrentWebview(),
@@ -935,17 +919,17 @@ static Eina_Bool proces_pool_fd_handler(void* /*data*/, Ecore_Fd_Handler *handle
 {
     int fd = ecore_main_fd_handler_fd_get(handler);
 
+    if (fd == -1)
+    {
+        LogDebug("ECORE_FD_GET");
+        exit(-1);
+    }
+
     if (ecore_main_fd_handler_active_get(handler, ECORE_FD_ERROR))
     {
         LogDebug("ECORE_FD_ERROR");
-
-        if (fd != -1)
-        {
-            close(fd);
-        }
-
+        close(fd);
         exit(-1);
-        return ECORE_CALLBACK_CANCEL;
     }
 
     if (ecore_main_fd_handler_active_get(handler, ECORE_FD_READ))
@@ -956,28 +940,21 @@ static Eina_Bool proces_pool_fd_handler(void* /*data*/, Ecore_Fd_Handler *handle
             memset(pkt, 0, AUL_SOCK_MAXBUFF);
 
             int recv_ret = recv(fd, pkt, AUL_SOCK_MAXBUFF, 0);
-
-            if (fd != -1)
-            {
-                close(fd);
-            }
+            close(fd);
 
             if (recv_ret == -1)
             {
                 LogDebug("recv error!");
+                free(pkt);
                 exit(-1);
             }
-            LogDebug("recv_ret : " << recv_ret << ", pkt->len : " << pkt->len);
 
+            LogDebug("recv_ret : " << recv_ret << ", pkt->len : " << pkt->len);
             ecore_main_fd_handler_del(handler);
-
-            __wrt_launchpad_main_loop(pkt, app_argv[0], &app_argc, &app_argv);
-
+            process_pool_launchpad_main_loop(pkt, app_argv[0], &app_argc, &app_argv);
             free(pkt);
         }
-
         ecore_main_loop_quit();
-        return ECORE_CALLBACK_CANCEL;
     }
 
     return ECORE_CALLBACK_CANCEL;
@@ -985,7 +962,7 @@ static Eina_Bool proces_pool_fd_handler(void* /*data*/, Ecore_Fd_Handler *handle
 
 static void vconf_changed_handler(keynode_t* /*key*/, void* /*data*/)
 {
-    LogInfo("VCONFKEY_LANGSET vconf-key was changed!");
+    LogDebug("VCONFKEY_LANGSET vconf-key was changed!");
 
     // When system language is changed, the candidate process will be created again.
     exit(-1);
@@ -993,19 +970,6 @@ static void vconf_changed_handler(keynode_t* /*key*/, void* /*data*/)
 
 void set_env()
 {
-    // set evas backend type
-    if (!getenv("ELM_ENGINE"))
-    {
-        if (!setenv("ELM_ENGINE", "gl", 1))
-        {
-            LogDebug("Enable backend");
-        }
-    }
-    else
-    {
-        LogDebug("ELM_ENGINE : " << getenv("ELM_ENGINE"));
-    }
-
 #ifndef TIZEN_PUBLIC
     setenv("COREGL_FASTPATH", "1", 1);
 #endif
@@ -1021,6 +985,13 @@ int main(int argc,
     app_argc = argc;
     app_argv = argv;
 
+#ifndef X11
+    // Mesa does a bad job detecting the correct EGL
+    // platform to use, and ends up assuming that the
+    // wrt-client is using X
+    setenv("EGL_PLATFORM", "wayland", 1);
+#endif
+
     UNHANDLED_EXCEPTION_HANDLER_BEGIN
     {
         ADD_PROFILING_POINT("main-entered", "point");
@@ -1033,14 +1004,14 @@ int main(int argc,
 
         if (argc > 1 && argv[1] != NULL && !strcmp(argv[1], "-d"))
         {
-            LogInfo("Entered dummy process mode");
+            LogDebug("Entered dummy process mode");
             sprintf(argv[0], "%s                                              ",
                     DUMMY_PROCESS_PATH);
 
             // Set 'root' home directory
             setenv(HOME, ROOT_HOME_PATH, 1);
 
-            LogInfo("Prepare ewk_context");
+            LogDebug("Prepare ewk_context");
             appcore_set_i18n("wrt-client", NULL);
             ewk_set_arguments(argc, argv);
             setenv("WRT_LAUNCHING_PERFORMANCE", "1", 1);
@@ -1048,7 +1019,7 @@ int main(int argc,
 
             if (s_preparedEwkContext == NULL)
             {
-                LogInfo("Creating webkit context was failed!");
+                LogDebug("Creating webkit context was failed!");
                 exit(-1);
             }
 
@@ -1056,24 +1027,28 @@ int main(int argc,
 
             if (client_fd == -1)
             {
-                LogInfo("Connecting process_pool_server was failed!");
+                LogDebug("Connecting process_pool_server was failed!");
                 exit(-1);
             }
 
             // register language changed callback
             vconf_notify_key_changed(VCONFKEY_LANGSET, vconf_changed_handler, NULL);
 
-            LogInfo("Prepare window_data");
+            LogDebug("Prepare window_data");
             // Temporarily change HOME path to app
             // This change is needed for getting elementary profile
             // /opt/home/app/.elementary/config/mobile/base.cfg
             const char* backupEnv = getenv(HOME);
+            if (!backupEnv) {
+                // If getenv return "NULL", set empty string
+                backupEnv = "";
+            }
             setenv(HOME, APP_HOME_PATH, 1);
-            LogInfo("elm_init()");
+            LogDebug("elm_init()");
             elm_init(argc, argv);
             setenv(HOME, backupEnv, 1);
 
-            LogInfo("WindowData()");
+            LogDebug("WindowData()");
             s_preparedWindowData = new WindowData(static_cast<unsigned long>(getpid()));
 
             Ecore_Fd_Handler* fd_handler = ecore_main_fd_handler_add(client_fd,
@@ -1082,7 +1057,7 @@ int main(int argc,
 
             if (fd_handler == NULL)
             {
-                LogInfo("fd_handler is NULL");
+                LogDebug("fd_handler is NULL");
                 exit(-1);
             }
 
@@ -1095,21 +1070,28 @@ int main(int argc,
             // deregister language changed callback
             vconf_ignore_key_changed(VCONFKEY_LANGSET, vconf_changed_handler);
 
-            std::string tizenId = WrtClient::getTizenIdFromArgument(argc, argv);
+            std::string tizenId =
+                ClientModule::CommandLineParser::getTizenId(argc, argv);
             ewk_context_message_post_to_injected_bundle(
                 s_preparedEwkContext,
                 MESSAGE_NAME_INITIALIZE,
                 tizenId.c_str());
+
         }
         else
         {
             // This code is to fork a web process without exec.
-            std::string tizenId = WrtClient::getTizenIdFromArgument(argc, argv);
+            std::string tizenId =
+                ClientModule::CommandLineParser::getTizenId(argc, argv);
+
             if (!tizenId.empty()) {
-                std::size_t pos =
-                    tizenId.find(WrtDB::AppControlPrefix::PROCESS_PREFIX);
-                if (pos != std::string::npos) {
-                    tizenId.erase(pos);
+                if (UID_ROOT == getuid()) {
+                    // Drop root permission
+                    // Only launch web application by console command case has root permission
+                    if (!ClientModule::SecuritySupport::setAppPrivilege(tizenId)) {
+                        LogError("Fail to set app privilege : [" << tizenId << "]");
+                        exit(-1);
+                    }
                 }
 
                 LogDebug("Launching by fork mode");
@@ -1122,7 +1104,7 @@ int main(int argc,
 
                 if (s_preparedEwkContext == NULL)
                 {
-                    LogInfo("Creating webkit context was failed!");
+                    LogDebug("Creating webkit context was failed!");
                     Wrt::Popup::PopupInvoker().showInfo("Error", "Creating webkit context was failed.", "OK");
                     exit(-1);
                 }