Revert "[Tizen][Web] Fix WebView terminate crash"
authorWonsik Jung <sidein@samsung.com>
Tue, 15 Nov 2022 02:25:35 +0000 (11:25 +0900)
committerWonsik Jung <sidein@samsung.com>
Tue, 15 Nov 2022 02:25:35 +0000 (11:25 +0900)
This reverts commit c7025658d79551ee9f28520fbe010526cb893ab9.

dali/internal/web-engine/common/web-engine-impl.cpp [changed mode: 0644->0755]
dali/internal/web-engine/common/web-engine-impl.h

old mode 100644 (file)
new mode 100755 (executable)
index c530481..558dac7
@@ -53,6 +53,10 @@ constexpr char const* const kPluginFullNamePrefix  = "libdali2-web-engine-";
 constexpr char const* const kPluginFullNamePostfix = "-plugin.so";
 constexpr char const* const kPluginFullNameDefault = "libdali2-web-engine-plugin.so";
 
 constexpr char const* const kPluginFullNamePostfix = "-plugin.so";
 constexpr char const* const kPluginFullNameDefault = "libdali2-web-engine-plugin.so";
 
+// Note: Dali WebView policy does not allow to use multiple web engines in an application.
+// So once pluginName is set to non-empty string, it will not change.
+std::string pluginName;
+
 std::string MakePluginName(const char* environmentName)
 {
   std::stringstream fullName;
 std::string MakePluginName(const char* environmentName)
 {
   std::stringstream fullName;
@@ -67,184 +71,114 @@ Dali::BaseHandle Create()
 
 Dali::TypeRegistration type(typeid(Dali::WebEngine), typeid(Dali::BaseHandle), Create);
 
 
 Dali::TypeRegistration type(typeid(Dali::WebEngine), typeid(Dali::BaseHandle), Create);
 
-/**
- * @brief Control the WebEnginePlugin library lifecycle.
- */
-struct WebEnginePluginObject
-{
-public:
-  WebEnginePluginObject()
-  : mPluginName{},
-    mHandle{nullptr},
-    mCreateWebEnginePtr{nullptr},
-    mDestroyWebEnginePtr{nullptr},
-    mGetWebEngineContextPtr{nullptr},
-    mGetWebEngineCookieManagerPtr{nullptr}
+} // unnamed namespace
+
+void*                               WebEngine::mHandle              = nullptr;
+WebEngine::CreateWebEngineFunction  WebEngine::mCreateWebEnginePtr  = nullptr;
+WebEngine::DestroyWebEngineFunction WebEngine::mDestroyWebEnginePtr = nullptr;
+
+WebEnginePtr WebEngine::New()
+{
+  WebEngine* instance = new WebEngine();
+  if(!instance->Initialize())
   {
   {
+    delete instance;
+    return nullptr;
   }
 
   }
 
-  ~WebEnginePluginObject()
+  return instance;
+}
+
+Dali::WebEngineContext* WebEngine::GetContext()
+{
+  if(!InitializePluginHandle())
   {
   {
-    if(mHandle)
-    {
-      dlclose(mHandle);
-      mHandle = nullptr;
-    }
+    return nullptr;
   }
 
   }
 
-  bool InitializePluginHandle()
+  using GetWebEngineContext                  = Dali::WebEngineContext* (*)();
+  GetWebEngineContext getWebEngineContextPtr = reinterpret_cast<GetWebEngineContext>(dlsym(mHandle, "GetWebEngineContext"));
+  if(getWebEngineContextPtr)
   {
   {
-    if(mHandle)
-    {
-      DALI_LOG_ERROR("Plugin.so has been opened already.\n");
-      return true;
-    }
-
-    if(mPluginName.length() == 0)
-    {
-      // mPluginName is not initialized yet.
-      const char* name = EnvironmentVariable::GetEnvironmentVariable(DALI_ENV_WEB_ENGINE_NAME);
-      if(name)
-      {
-        mPluginName = MakePluginName(name);
-      }
-      else
-      {
-        mPluginName = std::string(kPluginFullNameDefault);
-      }
-    }
-
-    mHandle = dlopen(mPluginName.c_str(), RTLD_LAZY);
-    if(!mHandle)
-    {
-      DALI_LOG_ERROR("Can't load %s : %s\n", mPluginName.c_str(), dlerror());
-      return false;
-    }
-
-    mCreateWebEnginePtr = reinterpret_cast<CreateWebEngineFunction>(dlsym(mHandle, "CreateWebEnginePlugin"));
-    if(mCreateWebEnginePtr == nullptr)
-    {
-      DALI_LOG_ERROR("Can't load symbol CreateWebEnginePlugin(), error: %s\n", dlerror());
-
-      return false;
-    }
+    return getWebEngineContextPtr();
+  }
 
 
-    mDestroyWebEnginePtr = reinterpret_cast<DestroyWebEngineFunction>(dlsym(mHandle, "DestroyWebEnginePlugin"));
-    if(mDestroyWebEnginePtr == nullptr)
-    {
-      DALI_LOG_ERROR("Can't load symbol DestroyWebEnginePlugin(), error: %s\n", dlerror());
-      return false;
-    }
+  return nullptr;
+}
 
 
-    return true;
+Dali::WebEngineCookieManager* WebEngine::GetCookieManager()
+{
+  if(!InitializePluginHandle())
+  {
+    return nullptr;
   }
 
   }
 
-  bool InitializeContextHandle()
+  using GetWebEngineCookieManager                        = Dali::WebEngineCookieManager* (*)();
+  GetWebEngineCookieManager getWebEngineCookieManagerPtr = reinterpret_cast<GetWebEngineCookieManager>(dlsym(mHandle, "GetWebEngineCookieManager"));
+  if(getWebEngineCookieManagerPtr)
   {
   {
-    if(!InitializePluginHandle())
-    {
-      return false;
-    }
-
-    mGetWebEngineContextPtr = reinterpret_cast<GetWebEngineContext>(dlsym(mHandle, "GetWebEngineContext"));
+    return getWebEngineCookieManagerPtr();
+  }
 
 
-    if(!mGetWebEngineContextPtr)
-    {
-      DALI_LOG_ERROR("Can't load symbol GetWebEngineContext(), error: %s\n", dlerror());
-      return false;
-    }
+  return nullptr;
+}
 
 
+bool WebEngine::InitializePluginHandle()
+{
+  if(mHandle)
+  {
+    DALI_LOG_ERROR("Plugin.so has been opened already.\n");
     return true;
   }
 
     return true;
   }
 
-  bool InitializeCookieManagerHandle()
+  if(pluginName.length() == 0)
   {
   {
-    if(!InitializePluginHandle())
+    // pluginName is not initialized yet.
+    const char* name = EnvironmentVariable::GetEnvironmentVariable(DALI_ENV_WEB_ENGINE_NAME);
+    if(name)
     {
     {
-      return false;
+      pluginName = MakePluginName(name);
     }
     }
-
-    mGetWebEngineCookieManagerPtr = reinterpret_cast<GetWebEngineCookieManager>(dlsym(mHandle, "GetWebEngineCookieManager"));
-
-    if(!mGetWebEngineCookieManagerPtr)
+    else
     {
     {
-      DALI_LOG_ERROR("Can't load symbol GetWebEngineCookieManager(), error: %s\n", dlerror());
-      return false;
+      pluginName = std::string(kPluginFullNameDefault);
     }
     }
-
-    return true;
   }
 
   }
 
-private:
-  WebEnginePluginObject(const WebEnginePluginObject&) = delete;
-  WebEnginePluginObject(WebEnginePluginObject&&)      = delete;
-  WebEnginePluginObject& operator=(const WebEnginePluginObject&) = delete;
-  WebEnginePluginObject& operator=(WebEnginePluginObject&&) = delete;
-
-private:
-  std::string mPluginName; ///< Name of web engine plugin
-                           /// Note: Dali WebView policy does not allow to use multiple web engines in an application.
-                           /// So once pluginName is set to non-empty string, it will not change.
-
-public:
-  using CreateWebEngineFunction  = Dali::WebEnginePlugin* (*)();
-  using DestroyWebEngineFunction = void (*)(Dali::WebEnginePlugin* plugin);
-
-  using GetWebEngineContext       = Dali::WebEngineContext* (*)();
-  using GetWebEngineCookieManager = Dali::WebEngineCookieManager* (*)();
-
-  void*                    mHandle;              ///< Handle for the loaded library
-  CreateWebEngineFunction  mCreateWebEnginePtr;  ///< Function to create plugin instance
-  DestroyWebEngineFunction mDestroyWebEnginePtr; ///< Function to destroy plugin instance
-
-  GetWebEngineContext       mGetWebEngineContextPtr;       ///< Function to get WebEngineContext
-  GetWebEngineCookieManager mGetWebEngineCookieManagerPtr; ///< Function to get WebEngineCookieManager
-};
-
-static WebEnginePluginObject gPluginHandle; // Keep this object as static, so Let we assume that library closed after all WebEngines are disposed.
-
-} // unnamed namespace
-
-WebEnginePtr WebEngine::New()
-{
-  WebEngine* instance = new WebEngine();
-  if(!instance->Initialize())
+  mHandle = dlopen(pluginName.c_str(), RTLD_LAZY);
+  if(!mHandle)
   {
   {
-    delete instance;
-    return nullptr;
+    DALI_LOG_ERROR("Can't load %s : %s\n", pluginName.c_str(), dlerror());
+    return false;
   }
 
   }
 
-  return instance;
-}
+  // Make sure that mHandle would be closed.
+  Dali::LifecycleController::Get().TerminateSignal().Connect(&WebEngine::ClosePluginHandle);
 
 
-Dali::WebEngineContext* WebEngine::GetContext()
-{
-  if(!gPluginHandle.InitializeContextHandle())
+  mCreateWebEnginePtr = reinterpret_cast<CreateWebEngineFunction>(dlsym(mHandle, "CreateWebEnginePlugin"));
+  if(mCreateWebEnginePtr == nullptr)
   {
   {
-    return nullptr;
+    DALI_LOG_ERROR("Can't load symbol CreateWebEnginePlugin(), error: %s\n", dlerror());
+    return false;
   }
 
   }
 
-  if(gPluginHandle.mGetWebEngineContextPtr)
+  mDestroyWebEnginePtr = reinterpret_cast<DestroyWebEngineFunction>(dlsym(mHandle, "DestroyWebEnginePlugin"));
+  if(mDestroyWebEnginePtr == nullptr)
   {
   {
-    return gPluginHandle.mGetWebEngineContextPtr();
+    DALI_LOG_ERROR("Can't load symbol DestroyWebEnginePlugin(), error: %s\n", dlerror());
+    return false;
   }
 
   }
 
-  return nullptr;
+  return true;
 }
 
 }
 
-Dali::WebEngineCookieManager* WebEngine::GetCookieManager()
+void WebEngine::ClosePluginHandle()
 {
 {
-  if(!gPluginHandle.InitializeCookieManagerHandle())
-  {
-    return nullptr;
-  }
-
-  if(gPluginHandle.mGetWebEngineCookieManagerPtr)
+  if(mHandle)
   {
   {
-    return gPluginHandle.mGetWebEngineCookieManagerPtr();
+    dlclose(mHandle);
+    mHandle = nullptr;
   }
   }
-
-  return nullptr;
 }
 
 WebEngine::WebEngine()
 }
 
 WebEngine::WebEngine()
@@ -254,30 +188,27 @@ WebEngine::WebEngine()
 
 WebEngine::~WebEngine()
 {
 
 WebEngine::~WebEngine()
 {
-  if(mPlugin != nullptr)
+  if(mPlugin != nullptr && mDestroyWebEnginePtr != nullptr)
   {
     mPlugin->Destroy();
   {
     mPlugin->Destroy();
-    if(gPluginHandle.mDestroyWebEnginePtr != nullptr)
-    {
-      gPluginHandle.mDestroyWebEnginePtr(mPlugin);
-    }
-    mPlugin = nullptr;
+    mDestroyWebEnginePtr(mPlugin);
   }
 }
 
 bool WebEngine::Initialize()
 {
   }
 }
 
 bool WebEngine::Initialize()
 {
-  if(!gPluginHandle.InitializePluginHandle())
+  if(!InitializePluginHandle())
   {
     return false;
   }
 
   {
     return false;
   }
 
-  mPlugin = gPluginHandle.mCreateWebEnginePtr();
+  mPlugin = mCreateWebEnginePtr();
   if(mPlugin == nullptr)
   {
     DALI_LOG_ERROR("Can't create the WebEnginePlugin object\n");
     return false;
   }
   if(mPlugin == nullptr)
   {
     DALI_LOG_ERROR("Can't create the WebEnginePlugin object\n");
     return false;
   }
+
   return true;
 }
 
   return true;
 }
 
index 96e6eac..085c28a 100755 (executable)
@@ -576,8 +576,27 @@ private:
    */
   bool Initialize();
 
    */
   bool Initialize();
 
+  /**
+   * @brief Initialize library handle by loading web engine plugin.
+   *
+   * @return Whether the initialization succeed or not.
+   */
+  static bool InitializePluginHandle();
+
+  /**
+   * @brief Close library handle.
+   */
+  static void ClosePluginHandle();
+
 private:
 private:
+  using CreateWebEngineFunction  = Dali::WebEnginePlugin* (*)();
+  using DestroyWebEngineFunction = void (*)(Dali::WebEnginePlugin* plugin);
+
   Dali::WebEnginePlugin* mPlugin; ///< WebEnginePlugin instance
   Dali::WebEnginePlugin* mPlugin; ///< WebEnginePlugin instance
+
+  static void*                    mHandle;              ///< Handle for the loaded library
+  static CreateWebEngineFunction  mCreateWebEnginePtr;  ///< Function to create plugin instance
+  static DestroyWebEngineFunction mDestroyWebEnginePtr; ///< Function to destroy plugin instance
 };
 
 } // namespace Adaptor
 };
 
 } // namespace Adaptor