Fix prevent issue
[platform/framework/web/wrt.git] / src / wrt-client / wrt-client.cpp
index b18835a..188bf04 100644 (file)
 #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 <EWebKit2.h>
 #include <dpl/localization/w3c_file_localization.h>
 #include <dpl/localization/LanguageTagsProvider.h>
-#include "webkit/injected-bundle/plugin_module_support.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 <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 = LIBDIR_PREFIX "/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";
@@ -62,19 +69,22 @@ static char** app_argv = NULL;
 // env
 const char* const HOME = "HOME";
 const char* const APP_HOME_PATH = "/opt/home/app";
+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_widgetState(WidgetState::WidgetState_Stopped),
+    m_initialViewMode(VIEWMODE_TYPE_MAXIMIZED),
+    m_currentViewMode(VIEWMODE_TYPE_MAXIMIZED),
+    m_isWebkitFullscreen(false),
+    m_isFullscreenByPlatform(false),
+    m_submodeSupport(new ClientModule::SubmodeSupport())
 {
     Touch();
     LogDebug("App Created");
@@ -92,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();
 }
@@ -118,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;
 }
@@ -140,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();
         }
     }
 
@@ -179,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);
 
@@ -261,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*/)
@@ -311,38 +231,21 @@ void WrtClient::initStep()
         SwitchToStep(&WrtClient::shutdownStep);
     }
 
-    // ecore_event_jobs are processed sequentially without concession to
-    // other type events. To give a chance of execute to other events,
-    // ecore_timer_add was used.
-    DPL::Event::ControllerEventHandler<NextStepEvent>::PostTimedEvent(
-        NextStepEvent(), 0.001);
+    DPL::Event::ControllerEventHandler<NextStepEvent>::PostEvent(NextStepEvent());
 }
 
 void WrtClient::loadFinishCallback(Evas_Object* webview)
 {
     ADD_PROFILING_POINT("loadFinishCallback", "start");
-    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");
+    // Splash screen
+    if (m_splashScreen && m_splashScreen->isShowing())
+    {
+        m_splashScreen->stopSplashScreenBuffered();
     }
 
+    LogDebug("Post result of launch");
+
     //w3c packaging test debug (message on 4>)
     const char * makeScreen = getenv(W3C_DEBUG_ENV_VARIABLE);
     if (makeScreen != NULL && strcmp(makeScreen, "1") == 0) {
@@ -369,29 +272,60 @@ 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");
         }
-        sigqueue(m_sdkLauncherPid, SIGRTMIN, sv);
     }
 
     ApplicationDataSingleton::Instance().freeBundle();
+}
+
+void WrtClient::resetCallback(bool result)
+{
+    if (!result) {
+        LogDebug("Fail to handle reset event");
+        // free bundle data
+        ApplicationDataSingleton::Instance().freeBundle();
+    }
+}
+
+void WrtClient::progressStartedCallback()
+{
+    if (m_settingList->getProgressBarPresence() == ProgressBar_Enable ||
+        m_currentViewMode == VIEWMODE_TYPE_WINDOWED)
+    {
+        m_windowData->signalEmit(Layer::MAIN_LAYOUT,
+                                 EDJE_SHOW_PROGRESS_SIGNAL,
+                                 "");
+        m_windowData->updateProgress(0);
+    }
+}
 
-    LogDebug("Cleaning wrtClient launch resources...");
-    delete debug->pid;
-    delete debug;
+void WrtClient::loadProgressCallback(Evas_Object* /*webview*/, double value)
+{
+    if (m_settingList->getProgressBarPresence() == ProgressBar_Enable ||
+        m_currentViewMode == VIEWMODE_TYPE_WINDOWED)
+    {
+        m_windowData->updateProgress(value);
+    }
 }
 
 void WrtClient::progressFinishCallback()
 {
-    m_splashScreen->stopSplashScreen();
+    if (m_settingList->getProgressBarPresence() == ProgressBar_Enable ||
+        m_currentViewMode == VIEWMODE_TYPE_WINDOWED)
+    {
+        m_windowData->signalEmit(Layer::MAIN_LAYOUT,
+                                 EDJE_HIDE_PROGRESS_SIGNAL,
+                                 "");
+    }
 }
 
 void WrtClient::webkitExitCallback()
@@ -410,6 +344,27 @@ void WrtClient::webCrashCallback()
         NextStepEvent());
 }
 
+void WrtClient::enterFullscreenCallback(Evas_Object* /*obj*/,
+                                        bool isFullscreenByPlatform)
+{
+    // 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");
@@ -417,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) {
@@ -442,13 +397,18 @@ void WrtClient::launchStep()
     }
 
     m_dao.reset(new WrtDB::WidgetDAOReadOnly(DPL::FromASCIIString(m_tizenId)));
+    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()) {
         LanguageTagsProviderSingleton::Instance().addWidgetDefaultLocales(
             *defloc);
     }
 
-    PrepareExternalStorageSingleton::Instance().Initialize(m_dao->getTizenPkgId());
+    setInitialViewMode();
 
     /* remove language change callback */
     /*
@@ -464,37 +424,71 @@ void WrtClient::launchStep()
         s_preparedWindowData = NULL;
     }
     ADD_PROFILING_POINT("CreateWindow", "stop");
-    evas_object_show(m_windowData->m_win);
+    if (!m_windowData->initScreenReaderSupport(
+            m_settingList->getAccessibility() == Accessibility_Enable))
+    {
+        LogWarning("Fail to set screen reader support set");
+    }
+
     // rotate window to initial value
-    setOrientationWindow();
+    setWindowInitialOrientation();
+    setCtxpopupItem();
 
     WRT::UserDelegatesPtr cbs(new WRT::UserDelegates);
+
     ADD_PROFILING_POINT("Create splash screen", "start");
-    m_splashScreen.reset(
-        new SplashScreenSupport(m_windowData->m_win));
-    if (m_splashScreen->createSplashScreen(m_dao->getSplashImgSrc())) {
+    DPL::OptionalString splashImgSrc = m_dao->getSplashImgSrc();
+    if (!splashImgSrc.IsNull())
+    {
+        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();
-        cbs->progressFinish = DPL::MakeDelegate(
-                this,
-                &WrtClient::
-                    progressFinishCallback);
     }
     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());
         return;
     }
     // send rotate information to ewk
-    setOrientationEwk();
+    setEwkInitialOrientation();
 
     //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);
@@ -502,10 +496,13 @@ 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->getEvasObject(Layer::WINDOW));
 
     initializeWindowModes();
-    connectElmCallback();
 
     m_widgetState = WidgetState_Authorizing;
     if (!m_widget->CheckBeforeLaunch()) {
@@ -514,128 +511,150 @@ 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(
-            m_windowData.get(), &WindowData::toggleFullscreen);
+    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");
 }
 
 void WrtClient::initializeWindowModes()
 {
     Assert(m_windowData);
-    Assert(m_dao);
-    auto windowModes = m_dao->getWindowModes();
-    bool fullscreen = false;
-    bool backbutton = false;
-    if (m_dao->getWidgetType().appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
-        WrtDB::WidgetSettings widgetSettings;
-        m_dao->getWidgetSettings(widgetSettings);
-        WidgetSettingList settings(widgetSettings);
-        backbutton =
-            (settings.getBackButtonPresence() == BackButton_Enable);
-    }
-
-    FOREACH(it, windowModes)
-    {
-        std::string viewMode = DPL::ToUTF8String(*it);
-        if (viewMode == VIEWMODE_TYPE_FULLSCREEN) {
-            fullscreen = true;
-            break;
-        } else if (viewMode == VIEWMODE_TYPE_MAXIMIZED) {
-            break;
-        }
-    }
-
-    m_windowData->setViewMode(fullscreen,
+    bool backbutton =
+        (m_settingList->getBackButtonPresence() == BackButton_Enable ||
+        m_currentViewMode == VIEWMODE_TYPE_WINDOWED);
+    m_windowData->setViewMode(m_currentViewMode == VIEWMODE_TYPE_FULLSCREEN,
                               backbutton);
 }
 
-void WrtClient::backButtonCallback(void* data,
-                                   Evas_Object * /*obj*/,
-                                   void * /*event_info*/)
+Eina_Bool WrtClient::naviframeBackButtonCallback(void* data,
+                                                 Elm_Object_Item* /*it*/)
 {
-    LogInfo("BackButtonCallback");
+    LogDebug("BackButtonCallback");
     Assert(data);
 
     WrtClient* This = static_cast<WrtClient*>(data);
-
     This->m_widget->Backward();
+    return EINA_FALSE;
 }
 
 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;
 }
 
-void WrtClient::setOrientationWindow(void)
+void WrtClient::setInitialViewMode(void)
+{
+    Assert(m_dao);
+    WrtDB::WindowModeList windowModes = m_dao->getWindowModes();
+    FOREACH(it, windowModes) {
+        std::string viewMode = DPL::ToUTF8String(*it);
+        switch(viewMode[0]) {
+            case 'f':
+                if (viewMode == VIEWMODE_TYPE_FULLSCREEN) {
+                    m_initialViewMode = viewMode;
+                    m_currentViewMode = m_initialViewMode;
+                    break;
+                }
+                break;
+            case 'm':
+                if (viewMode == VIEWMODE_TYPE_MAXIMIZED) {
+                    m_initialViewMode = viewMode;
+                    m_currentViewMode = m_initialViewMode;
+                    break;
+                }
+                break;
+            case 'w':
+                if (viewMode == VIEWMODE_TYPE_WINDOWED) {
+                    m_initialViewMode = viewMode;
+                    m_currentViewMode = m_initialViewMode;
+                    break;
+                }
+                break;
+            default:
+                break;
+        }
+    }
+}
+
+void WrtClient::setWindowInitialOrientation(void)
 {
     Assert(m_windowData);
     Assert(m_dao);
 
-    WrtDB::WidgetSettings widgetSettings;
-    m_dao->getWidgetSettings(widgetSettings);
-    WidgetSettingList settings(widgetSettings);
-    WidgetSettingScreenLock rotationValue = settings.getRotationValue();
+    WidgetSettingScreenLock rotationValue = m_settingList->getRotationValue();
     if (rotationValue == Screen_Portrait) {
-        elm_win_wm_rotation_preferred_rotation_set(
-            m_windowData->m_win,
-            OrientationAngle::Window::Portrait::PRIMARY);
+        setWindowOrientation(OrientationAngle::Window::Portrait::PRIMARY);
     } else if (rotationValue == Screen_Landscape) {
-        elm_win_wm_rotation_preferred_rotation_set(
-            m_windowData->m_win,
-            OrientationAngle::Window::Landscape::PRIMARY);
+        setWindowOrientation(OrientationAngle::Window::Landscape::PRIMARY);
     } else if (rotationValue == Screen_AutoRotation) {
-        LogInfo("Nothing to do");
+        if (!AutoRotationSupport::setAutoRotation(
+                m_windowData->getEvasObject(Layer::WINDOW),
+                autoRotationCallback,
+                this))
+        {
+            LogError("Fail to set auto rotation");
+        }
     } else {
-        elm_win_wm_rotation_preferred_rotation_set(
-            m_windowData->m_win,
-            OrientationAngle::W3C::Portrait::PRIMARY);
+        setWindowOrientation(OrientationAngle::Window::Portrait::PRIMARY);
     }
 }
 
-void WrtClient::unsetOrientationWindow(void)
+void WrtClient::setWindowOrientation(int angle)
+{
+    Assert(m_windowData);
+    m_windowData->setOrientation(angle);
+}
+
+void WrtClient::unsetWindowOrientation(void)
 {
     Assert(m_windowData);
     Assert(m_dao);
 
-    WrtDB::WidgetSettings widgetSettings;
-    m_dao->getWidgetSettings(widgetSettings);
-    WidgetSettingList settings(widgetSettings);
-    WidgetSettingScreenLock rotationValue = settings.getRotationValue();
+    WidgetSettingScreenLock rotationValue = m_settingList->getRotationValue();
     if (rotationValue == Screen_AutoRotation) {
-        LogInfo("Nothing to do");
+        AutoRotationSupport::unsetAutoRotation(
+            m_windowData->getEvasObject(Layer::WINDOW),
+            autoRotationCallback);
     }
 }
 
-
-void WrtClient::setOrientationEwk(void)
+void WrtClient::setEwkInitialOrientation(void)
 {
     Assert(m_widget);
     Assert(m_dao);
 
-    WrtDB::WidgetSettings widgetSettings;
-    m_dao->getWidgetSettings(widgetSettings);
-    WidgetSettingList settings(widgetSettings);
-    WidgetSettingScreenLock rotationValue = settings.getRotationValue();
+    WidgetSettingScreenLock rotationValue = m_settingList->getRotationValue();
     if (rotationValue == Screen_Portrait) {
         ewk_view_orientation_send(
             m_widget->GetCurrentWebview(),
@@ -667,46 +686,131 @@ void WrtClient::ExitCallback(void* data,
     This->OnTerminate();
 }
 
+void WrtClient::setCtxpopupItem(void)
+{
+    WindowData::CtxpopupItemDataList data;
+
+    // 1. share
+    WindowData::CtxpopupCallbackType shareCallback =
+        DPL::MakeDelegate(this, &WrtClient::ctxpopupShare);
+    WindowData::CtxpopupItemData shareData("Share",
+                                           std::string(),
+                                           shareCallback);
+
+    // 2. reload
+    WindowData::CtxpopupCallbackType reloadCallback =
+        DPL::MakeDelegate(this, &WrtClient::ctxpopupReload);
+    WindowData::CtxpopupItemData reloadData("Reload",
+                                            std::string(),
+                                            reloadCallback);
+
+    // 3. Open in browser
+    WindowData::CtxpopupCallbackType launchBrowserCallback =
+        DPL::MakeDelegate(this, &WrtClient::ctxpopupLaunchBrowser);
+    WindowData::CtxpopupItemData launchBrowserData("Open in browser",
+                                                   std::string(),
+                                                   launchBrowserCallback);
+    data.push_back(shareData);
+    data.push_back(reloadData);
+    data.push_back(launchBrowserData);
+    m_windowData->setCtxpopupItemData(data);
+}
 
-void WrtClient::connectElmCallback()
+void WrtClient::ctxpopupShare(void)
 {
-    Assert(m_windowData);
-    Assert(m_dao);
-    WrtDB::WidgetSettings widgetSettings;
-    m_dao->getWidgetSettings(widgetSettings);
-    WidgetSettingList settings(widgetSettings);
-    if (settings.getBackButtonPresence() == BackButton_Enable) {
-        m_windowData->addFloatBackButtonCallback(
-            "clicked",
-            &WrtClient::backButtonCallback,
-            this);
+    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->getEvasObject(Layer::WINDOW)),
+            url))
+    {
+        LogDebug("success");
+    } else {
+        LogDebug("fail");
     }
     evas_object_smart_callback_add(m_windowData->m_win,
             "delete,request",
             &WrtClient::ExitCallback,
             this);
+#endif
 }
 
-void WrtClient::setLayout(Evas_Object* newBuffer)
+void WrtClient::ctxpopupReload(void)
 {
-    LogDebug("add new webkit buffer to window");
-    Assert(newBuffer);
+    LogDebug("reload");
+    ewk_view_reload(m_widget->GetCurrentWebview());
+}
 
-    elm_object_part_content_set(m_windowData->m_user_layout,
-                                ELM_SWALLOW_CONTENT,
-                                newBuffer);
+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->getEvasObject(Layer::WINDOW)),
+            url))
+    {
+        LogDebug("success");
+    } else {
+        LogDebug("fail");
+    }
+#endif
+}
 
-    evas_object_show(newBuffer);
+void WrtClient::hwkeyCallback(const std::string& key)
+{
+    if (m_settingList->getBackButtonPresence() == BackButton_Enable
+        || m_currentViewMode == VIEWMODE_TYPE_WINDOWED)
+    {
+        // 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::unsetLayout(Evas_Object* currentBuffer)
+void WrtClient::setLayout(Evas_Object* webview)
 {
-    LogDebug("remove current webkit buffer from window");
-    Assert(currentBuffer);
-    evas_object_hide(currentBuffer);
+    LogDebug("add new webkit buffer to window");
+    Assert(webview);
+    m_windowData->setWebview(webview);
+    evas_object_show(webview);
+    evas_object_show(m_windowData->getEvasObject(Layer::WINDOW));
+}
 
-    elm_object_part_content_unset(m_windowData->m_user_layout,
-                                  ELM_SWALLOW_CONTENT);
+void WrtClient::unsetLayout(Evas_Object* webview)
+{
+    LogDebug("remove current webkit buffer from window");
+    Assert(webview);
+    evas_object_hide(webview);
+    m_windowData->unsetWebview();
 }
 
 void WrtClient::shutdownStep()
@@ -716,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.
-        unsetOrientationWindow();
+        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) {
@@ -731,6 +841,46 @@ void WrtClient::shutdownStep()
     Quit();
 }
 
+void WrtClient::autoRotationCallback(void* data, Evas_Object* obj, void* /*event*/)
+{
+    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)
+{
+    LogDebug("entered");
+    Assert(obj);
+
+    AutoRotationSupport::setOrientation(obj, m_widget->GetCurrentWebview(),
+                                (m_splashScreen) ? m_splashScreen.get(): NULL);
+}
+
 int WrtClient::languageChangedCallback(void *data)
 {
     LogDebug("Language Changed");
@@ -769,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))
@@ -790,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;
@@ -819,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);
@@ -827,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
@@ -874,11 +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);
 
-            LogInfo("Prepare ewk_context");
+            // Set 'root' home directory
+            setenv(HOME, ROOT_HOME_PATH, 1);
+
+            LogDebug("Prepare ewk_context");
             appcore_set_i18n("wrt-client", NULL);
             ewk_set_arguments(argc, argv);
             setenv("WRT_LAUNCHING_PERFORMANCE", "1", 1);
@@ -886,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);
             }
 
@@ -894,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,
@@ -920,7 +1057,7 @@ int main(int argc,
 
             if (fd_handler == NULL)
             {
-                LogInfo("fd_handler is NULL");
+                LogDebug("fd_handler is NULL");
                 exit(-1);
             }
 
@@ -933,18 +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);
-            PluginModuleSupport::init(s_preparedEwkContext, tizenId);
+            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");
@@ -957,14 +1104,16 @@ 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);
                 }
 
                 // plugin init
-                PluginModuleSupport::init(s_preparedEwkContext, tizenId);
-
+                ewk_context_message_post_to_injected_bundle(
+                    s_preparedEwkContext,
+                    MESSAGE_NAME_INITIALIZE,
+                    tizenId.c_str());
             }
         }