Fix prevent issue
[platform/framework/web/wrt.git] / src / wrt-client / wrt-client.cpp
index 6cfd050..188bf04 100644 (file)
@@ -33,7 +33,6 @@
 #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"
 
@@ -42,7 +41,9 @@
 
 #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"
@@ -54,8 +55,9 @@ 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 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";
@@ -72,7 +74,6 @@ 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),
@@ -82,7 +83,8 @@ WrtClient::WrtClient(int argc, char **argv) :
     m_initialViewMode(VIEWMODE_TYPE_MAXIMIZED),
     m_currentViewMode(VIEWMODE_TYPE_MAXIMIZED),
     m_isWebkitFullscreen(false),
-    m_isFullscreenByPlatform(false)
+    m_isFullscreenByPlatform(false),
+    m_submodeSupport(new ClientModule::SubmodeSupport())
 {
     Touch();
     LogDebug("App Created");
@@ -126,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;
 }
@@ -155,9 +162,6 @@ void WrtClient::OnReset(bundle *b)
         if (m_tizenId.empty()) {
             showHelpAndQuit();
         } else {
-            m_appControlIndex =
-                ClientModule::CommandLineParser::getAppControlIndex(m_argc,
-                                                                    m_argv);
             setDebugMode(b);
             setStep();
         }
@@ -368,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) {
@@ -396,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()) {
@@ -404,7 +409,6 @@ void WrtClient::launchStep()
     }
 
     setInitialViewMode();
-    PrepareExternalStorageSingleton::Instance().Initialize(m_dao->getTizenPkgId());
 
     /* remove language change callback */
     /*
@@ -462,6 +466,21 @@ 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,
@@ -655,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;
@@ -688,6 +719,7 @@ 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");
@@ -701,6 +733,11 @@ void WrtClient::ctxpopupShare(void)
     } else {
         LogDebug("fail");
     }
+    evas_object_smart_callback_add(m_windowData->m_win,
+            "delete,request",
+            &WrtClient::ExitCallback,
+            this);
+#endif
 }
 
 void WrtClient::ctxpopupReload(void)
@@ -712,6 +749,7 @@ 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");
@@ -725,6 +763,7 @@ void WrtClient::ctxpopupLaunchBrowser(void)
     } else {
         LogDebug("fail");
     }
+#endif
 }
 
 void WrtClient::hwkeyCallback(const std::string& key)
@@ -735,7 +774,9 @@ void WrtClient::hwkeyCallback(const std::string& key)
         // windowed UX - hosted application
         if (key == KeyName::BACK) {
             if (m_isWebkitFullscreen) {
-                ewk_view_fullscreen_exit(m_widget->GetCurrentWebview());
+                // 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();
             }
@@ -747,7 +788,9 @@ void WrtClient::hwkeyCallback(const std::string& key)
         // packaged application
         if (key == KeyName::BACK) {
             if (m_isFullscreenByPlatform) {
-                ewk_view_fullscreen_exit(m_widget->GetCurrentWebview());
+                // FIXME!!! This method has not yet landed in the tizen 3.0
+                //          webkit-efl source tree
+                //ewk_view_fullscreen_exit(m_widget->GetCurrentWebview());
             }
         }
     }
@@ -779,6 +822,7 @@ void WrtClient::shutdownStep()
         m_widget->Hide();
         // 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",
@@ -788,7 +832,6 @@ void WrtClient::shutdownStep()
                                        unfocusedCallback);
         m_widget.reset();
         ewk_context_delete(s_preparedEwkContext);
-        PrepareExternalStorageSingleton::Instance().Deinitialize();
         WRT::CoreModuleSingleton::Instance().Terminate();
     }
     if (m_initialized) {
@@ -876,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))
@@ -897,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);
-
             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;
@@ -934,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
@@ -962,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");
@@ -1009,6 +1039,10 @@ int main(int argc,
             // 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);
             LogDebug("elm_init()");
             elm_init(argc, argv);
@@ -1051,6 +1085,15 @@ int main(int argc,
                 ClientModule::CommandLineParser::getTizenId(argc, argv);
 
             if (!tizenId.empty()) {
+                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");
                 // Language env setup
                 appcore_set_i18n("wrt-client", NULL);