Make sure that web-engine lib is loaded only once. 55/286055/2 accepted/tizen/6.0/unified/20221228.044832 submit/tizen_6.0/20221227.095310
authorhuayong.xu <huayong.xu@samsung.com>
Tue, 27 Dec 2022 03:13:25 +0000 (11:13 +0800)
committerhuayong.xu <huayong.xu@samsung.com>
Tue, 27 Dec 2022 07:14:00 +0000 (15:14 +0800)
Change-Id: Idf95eba4e3689d424f13cab463202135f0b02195

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

index 1c3ed975321ce250b1380ee4f0319aa1eefff75e..54795016695c69c443fc5a5391f82e2db0d595f2 100755 (executable)
@@ -65,12 +65,107 @@ Dali::BaseHandle Create()
 
 Dali::TypeRegistration type(typeid(Dali::WebEngine), typeid(Dali::BaseHandle), Create);
 
+/**
+ * @brief Control the WebEnginePlugin library lifecycle.
+ * Hold the plugin library handle in static singletone.
+ * It will makes library handle alives during all WebEngine resources create & destory.
+ */
+struct WebEnginePluginObject
+{
+public:
+  static WebEnginePluginObject& GetInstance()
+  {
+    static WebEnginePluginObject gPluginHandle;
+    return gPluginHandle;
+  }
+
+  /**
+   * @brief Converts an handle to a bool.
+   *
+   * This is useful for checking whether the WebEnginePluginObject succes to load library.
+   * @note We don't check mHandle because it is possible that mHandle load is success but
+   * Create/Destroy API load failed.
+   */
+  explicit operator bool() const
+  {
+    return mLoadSuccess;
+  }
+
+private:
+  // Private constructor / destructor
+  WebEnginePluginObject()
+  : mLoadSuccess{false},
+    mHandle{nullptr},
+    mCreateWebEnginePtr{nullptr},
+    mDestroyWebEnginePtr{nullptr}
+  {
+    std::string pluginName;
+    const char* name = EnvironmentVariable::GetEnvironmentVariable(DALI_ENV_WEB_ENGINE_NAME);
+    if(name)
+    {
+      pluginName = MakePluginName(name);
+    }
+    else
+    {
+      pluginName = std::string(kPluginFullNameDefault);
+    }
+
+    mHandle = dlopen(pluginName.c_str(), RTLD_LAZY);
+    if(!mHandle)
+    {
+      DALI_LOG_ERROR("Can't load %s : %s\n", pluginName.c_str(), dlerror());
+      return;
+    }
+
+    mCreateWebEnginePtr = reinterpret_cast<CreateWebEngineFunction>(dlsym(mHandle, "CreateWebEnginePlugin"));
+    if(mCreateWebEnginePtr == nullptr)
+    {
+      DALI_LOG_ERROR("Can't load symbol CreateWebEnginePlugin(), error: %s\n", dlerror());
+      return;
+    }
+
+    mDestroyWebEnginePtr = reinterpret_cast<DestroyWebEngineFunction>(dlsym(mHandle, "DestroyWebEnginePlugin"));
+    if(mDestroyWebEnginePtr == nullptr)
+    {
+      DALI_LOG_ERROR("Can't load symbol DestroyWebEnginePlugin(), error: %s\n", dlerror());
+      return;
+    }
+
+    mLoadSuccess = true;
+  }
+
+  ~WebEnginePluginObject()
+  {
+    if(mHandle)
+    {
+      dlclose(mHandle);
+      mHandle      = nullptr;
+      mLoadSuccess = false;
+    }
+  }
+
+  WebEnginePluginObject(const WebEnginePluginObject&) = delete;
+  WebEnginePluginObject(WebEnginePluginObject&&)      = delete;
+  WebEnginePluginObject& operator=(const WebEnginePluginObject&) = delete;
+  WebEnginePluginObject& operator=(WebEnginePluginObject&&) = delete;
+
+private:
+  bool mLoadSuccess; ///< True if library loaded successfully. False otherwise.
+
+public:
+  using CreateWebEngineFunction  = Dali::WebEnginePlugin* (*)();
+  using DestroyWebEngineFunction = void (*)(Dali::WebEnginePlugin* plugin);
+
+  void*                    mHandle;              ///< Handle for the loaded library
+  CreateWebEngineFunction  mCreateWebEnginePtr;  ///< Function to create plugin instance
+  DestroyWebEngineFunction mDestroyWebEnginePtr; ///< Function to destroy plugin instance
+};
+
 } // unnamed namespace
 
 WebEnginePtr WebEngine::New()
 {
   WebEngine* instance = new WebEngine();
-
   if(!instance->Initialize())
   {
     delete instance;
@@ -81,87 +176,38 @@ WebEnginePtr WebEngine::New()
 }
 
 WebEngine::WebEngine()
-: mPlugin(NULL),
-  mHandle(NULL),
-  mCreateWebEnginePtr(NULL),
-  mDestroyWebEnginePtr(NULL)
+: mPlugin(nullptr)
 {
 }
 
 WebEngine::~WebEngine()
 {
-  if(mHandle != NULL)
+  if(mPlugin != nullptr)
   {
-    if(mDestroyWebEnginePtr != NULL)
+    mPlugin->Destroy();
+    // Check whether plugin load sccess or not.
+    if(DALI_LIKELY(WebEnginePluginObject::GetInstance()))
     {
-      mPlugin->Destroy();
-      mDestroyWebEnginePtr(mPlugin);
+      WebEnginePluginObject::GetInstance().mDestroyWebEnginePtr(mPlugin);
     }
-
-    dlclose(mHandle);
+    mPlugin = nullptr;
   }
 }
 
-bool WebEngine::InitializePluginHandle()
-{
-  if(pluginName.length() == 0)
-  {
-    // pluginName is not initialized yet.
-    const char* name = EnvironmentVariable::GetEnvironmentVariable(DALI_ENV_WEB_ENGINE_NAME);
-    if(name)
-    {
-      pluginName = MakePluginName(name);
-      mHandle    = dlopen(pluginName.c_str(), RTLD_LAZY);
-      if(mHandle)
-      {
-        return true;
-      }
-    }
-    pluginName = std::string(kPluginFullNameDefault);
-  }
-
-  mHandle = dlopen(pluginName.c_str(), RTLD_LAZY);
-  if(!mHandle)
-  {
-    DALI_LOG_ERROR("Can't load %s : %s\n", pluginName.c_str(), dlerror());
-    return false;
-  }
-
-  return true;
-}
-
 bool WebEngine::Initialize()
 {
-  char* error = NULL;
-
-  if(!InitializePluginHandle())
-  {
-    return false;
-  }
-
-  mCreateWebEnginePtr = reinterpret_cast<CreateWebEngineFunction>(dlsym(mHandle, "CreateWebEnginePlugin"));
-  if(mCreateWebEnginePtr == NULL)
-  {
-    DALI_LOG_ERROR("Can't load symbol CreateWebEnginePlugin(), error: %s\n", error);
-    return false;
-  }
-
-  mDestroyWebEnginePtr = reinterpret_cast<DestroyWebEngineFunction>(dlsym(mHandle, "DestroyWebEnginePlugin"));
-
-  if(mDestroyWebEnginePtr == NULL)
+  // Check whether plugin load sccess or not.
+  if(!WebEnginePluginObject::GetInstance())
   {
-    DALI_LOG_ERROR("Can't load symbol DestroyWebEnginePlugin(), error: %s\n", error);
     return false;
   }
 
-  mPlugin = mCreateWebEnginePtr();
-
-  if(mPlugin == NULL)
+  mPlugin = WebEnginePluginObject::GetInstance().mCreateWebEnginePtr();
+  if(mPlugin == nullptr)
   {
     DALI_LOG_ERROR("Can't create the WebEnginePlugin object\n");
     return false;
   }
-
   return true;
 }
 
old mode 100755 (executable)
new mode 100644 (file)
index 3540302..f8a3f6e
@@ -318,21 +318,11 @@ private:
    */
   bool Initialize();
 
-  /**
-   * @brief Initializes library handle by loading web engine plugin.
-   *
-   * @return Whether the initialization succeed or not.
-   */
-  bool InitializePluginHandle();
-
 private:
   typedef Dali::WebEnginePlugin* (*CreateWebEngineFunction)();
   typedef void (*DestroyWebEngineFunction)(Dali::WebEnginePlugin* plugin);
 
-  Dali::WebEnginePlugin*   mPlugin;              ///< WebEnginePlugin instance
-  void*                    mHandle;              ///< Handle for the loaded library
-  CreateWebEngineFunction  mCreateWebEnginePtr;  ///< Function to create plugin instance
-  DestroyWebEngineFunction mDestroyWebEnginePtr; ///< Function to destroy plugin instance
+  Dali::WebEnginePlugin* mPlugin; ///< WebEnginePlugin instance
 };
 
 } // namespace Adaptor