Make web engine context be a singleton. 03/277703/2
authorhuayong.xu <huayong.xu@samsung.com>
Tue, 12 Jul 2022 07:57:27 +0000 (15:57 +0800)
committerhuayong.xu <huayong.xu@samsung.com>
Wed, 20 Jul 2022 05:35:38 +0000 (13:35 +0800)
Change-Id: I5654c772860afd474c564f90795b1dfa3bbc9ac4

dali/devel-api/adaptor-framework/web-engine/web-engine-plugin.h [changed mode: 0644->0755]
dali/devel-api/adaptor-framework/web-engine/web-engine.cpp
dali/devel-api/adaptor-framework/web-engine/web-engine.h
dali/internal/web-engine/common/web-engine-impl.cpp [changed mode: 0755->0644]
dali/internal/web-engine/common/web-engine-impl.h [changed mode: 0755->0644]

old mode 100644 (file)
new mode 100755 (executable)
index d4238ce..7a53042
@@ -251,16 +251,6 @@ public:
   virtual WebEngineSettings& GetSettings() const = 0;
 
   /**
-   * @brief Get context of WebEngine.
-   */
-  virtual WebEngineContext& GetContext() const = 0;
-
-  /**
-   * @brief Get cookie manager of WebEngine.
-   */
-  virtual WebEngineCookieManager& GetCookieManager() const = 0;
-
-  /**
    * @brief Get back-forward list of WebEngine.
    */
   virtual WebEngineBackForwardList& GetBackForwardList() const = 0;
index 4176021..5a39d11 100755 (executable)
@@ -57,6 +57,16 @@ WebEngine WebEngine::New()
   return WebEngine(engine.Get());
 }
 
+Dali::WebEngineContext* WebEngine::GetContext()
+{
+  return Internal::Adaptor::WebEngine::GetContext();
+}
+
+Dali::WebEngineCookieManager* WebEngine::GetCookieManager()
+{
+  return Internal::Adaptor::WebEngine::GetCookieManager();
+}
+
 WebEngine::WebEngine(const WebEngine& webEngine)
 : BaseHandle(webEngine)
 {
@@ -101,16 +111,6 @@ Dali::WebEngineSettings& WebEngine::GetSettings() const
   return GetImplementation(*this).GetSettings();
 }
 
-Dali::WebEngineContext& WebEngine::GetContext() const
-{
-  return GetImplementation(*this).GetContext();
-}
-
-Dali::WebEngineCookieManager& WebEngine::GetCookieManager() const
-{
-  return GetImplementation(*this).GetCookieManager();
-}
-
 Dali::WebEngineBackForwardList& WebEngine::GetBackForwardList() const
 {
   return GetImplementation(*this).GetBackForwardList();
index 8b73317..0af9cbe 100755 (executable)
@@ -62,6 +62,16 @@ public:
   static WebEngine New();
 
   /**
+   * @brief Get context of WebEngine.
+   */
+  static Dali::WebEngineContext* GetContext();
+
+  /**
+   * @brief Get cookie manager of WebEngine.
+   */
+  static Dali::WebEngineCookieManager* GetCookieManager();
+
+  /**
    * @brief Copy constructor.
    *
    * @param[in] WebEngine WebEngine to copy. The copied WebEngine will point at the same implementation
@@ -123,16 +133,6 @@ public:
   Dali::WebEngineSettings& GetSettings() const;
 
   /**
-   * @brief Get context of WebEngine.
-   */
-  Dali::WebEngineContext& GetContext() const;
-
-  /**
-   * @brief Get cookie manager of WebEngine.
-   */
-  Dali::WebEngineCookieManager& GetCookieManager() const;
-
-  /**
    * @brief Get back-forward list of WebEngine.
    */
   Dali::WebEngineBackForwardList& GetBackForwardList() const;
old mode 100755 (executable)
new mode 100644 (file)
index ed91a6b..1ec614a
@@ -26,6 +26,7 @@
 
 // INTERNAL INCLUDES
 #include <dali/devel-api/adaptor-framework/environment-variable.h>
+#include <dali/devel-api/adaptor-framework/lifecycle-controller.h>
 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-back-forward-list.h>
 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-certificate.h>
 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-console-message.h>
@@ -72,10 +73,13 @@ Dali::TypeRegistration type(typeid(Dali::WebEngine), typeid(Dali::BaseHandle), C
 
 } // 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;
@@ -85,30 +89,48 @@ WebEnginePtr WebEngine::New()
   return instance;
 }
 
-WebEngine::WebEngine()
-: mPlugin(NULL),
-  mHandle(NULL),
-  mCreateWebEnginePtr(NULL),
-  mDestroyWebEnginePtr(NULL)
+Dali::WebEngineContext* WebEngine::GetContext()
 {
+  if(!InitializePluginHandle())
+  {
+    return nullptr;
+  }
+
+  using GetWebEngineContext                  = Dali::WebEngineContext* (*)();
+  GetWebEngineContext getWebEngineContextPtr = reinterpret_cast<GetWebEngineContext>(dlsym(mHandle, "GetWebEngineContext"));
+  if(getWebEngineContextPtr)
+  {
+    return getWebEngineContextPtr();
+  }
+
+  return nullptr;
 }
 
-WebEngine::~WebEngine()
+Dali::WebEngineCookieManager* WebEngine::GetCookieManager()
 {
-  if(mHandle != NULL)
+  if(!InitializePluginHandle())
   {
-    if(mDestroyWebEnginePtr != NULL)
-    {
-      mPlugin->Destroy();
-      mDestroyWebEnginePtr(mPlugin);
-    }
+    return nullptr;
+  }
 
-    dlclose(mHandle);
+  using GetWebEngineCookieManager                        = Dali::WebEngineCookieManager* (*)();
+  GetWebEngineCookieManager getWebEngineCookieManagerPtr = reinterpret_cast<GetWebEngineCookieManager>(dlsym(mHandle, "GetWebEngineCookieManager"));
+  if(getWebEngineCookieManagerPtr)
+  {
+    return getWebEngineCookieManagerPtr();
   }
+
+  return nullptr;
 }
 
 bool WebEngine::InitializePluginHandle()
 {
+  if(mHandle)
+  {
+    DALI_LOG_ERROR("Plugin.so has been opened already.\n");
+    return true;
+  }
+
   if(pluginName.length() == 0)
   {
     // pluginName is not initialized yet.
@@ -116,13 +138,11 @@ bool WebEngine::InitializePluginHandle()
     if(name)
     {
       pluginName = MakePluginName(name);
-      mHandle    = dlopen(pluginName.c_str(), RTLD_LAZY);
-      if(mHandle)
-      {
-        return true;
-      }
     }
-    pluginName = std::string(kPluginFullNameDefault);
+    else
+    {
+      pluginName = std::string(kPluginFullNameDefault);
+    }
   }
 
   mHandle = dlopen(pluginName.c_str(), RTLD_LAZY);
@@ -132,36 +152,58 @@ bool WebEngine::InitializePluginHandle()
     return false;
   }
 
-  return true;
-}
-
-bool WebEngine::Initialize()
-{
-  char* error = NULL;
+  // Make sure that mHandle would be closed.
+  Dali::LifecycleController::Get().TerminateSignal().Connect(&WebEngine::ClosePluginHandle);
 
-  if(!InitializePluginHandle())
+  mCreateWebEnginePtr = reinterpret_cast<CreateWebEngineFunction>(dlsym(mHandle, "CreateWebEnginePlugin"));
+  if(mCreateWebEnginePtr == nullptr)
   {
+    DALI_LOG_ERROR("Can't load symbol CreateWebEnginePlugin(), error: %s\n", dlerror());
     return false;
   }
 
-  mCreateWebEnginePtr = reinterpret_cast<CreateWebEngineFunction>(dlsym(mHandle, "CreateWebEnginePlugin"));
-  if(mCreateWebEnginePtr == NULL)
+  mDestroyWebEnginePtr = reinterpret_cast<DestroyWebEngineFunction>(dlsym(mHandle, "DestroyWebEnginePlugin"));
+  if(mDestroyWebEnginePtr == nullptr)
   {
-    DALI_LOG_ERROR("Can't load symbol CreateWebEnginePlugin(), error: %s\n", error);
+    DALI_LOG_ERROR("Can't load symbol DestroyWebEnginePlugin(), error: %s\n", dlerror());
     return false;
   }
 
-  mDestroyWebEnginePtr = reinterpret_cast<DestroyWebEngineFunction>(dlsym(mHandle, "DestroyWebEnginePlugin"));
+  return true;
+}
 
-  if(mDestroyWebEnginePtr == NULL)
+void WebEngine::ClosePluginHandle()
+{
+  if(mHandle)
+  {
+    dlclose(mHandle);
+    mHandle = nullptr;
+  }
+}
+
+WebEngine::WebEngine()
+: mPlugin(nullptr)
+{
+}
+
+WebEngine::~WebEngine()
+{
+  if(mPlugin != nullptr && mDestroyWebEnginePtr != nullptr)
+  {
+    mPlugin->Destroy();
+    mDestroyWebEnginePtr(mPlugin);
+  }
+}
+
+bool WebEngine::Initialize()
+{
+  if(!InitializePluginHandle())
   {
-    DALI_LOG_ERROR("Can't load symbol DestroyWebEnginePlugin(), error: %s\n", error);
     return false;
   }
 
   mPlugin = mCreateWebEnginePtr();
-
-  if(mPlugin == NULL)
+  if(mPlugin == nullptr)
   {
     DALI_LOG_ERROR("Can't create the WebEnginePlugin object\n");
     return false;
@@ -195,16 +237,6 @@ Dali::WebEngineSettings& WebEngine::GetSettings() const
   return mPlugin->GetSettings();
 }
 
-Dali::WebEngineContext& WebEngine::GetContext() const
-{
-  return mPlugin->GetContext();
-}
-
-Dali::WebEngineCookieManager& WebEngine::GetCookieManager() const
-{
-  return mPlugin->GetCookieManager();
-}
-
 Dali::WebEngineBackForwardList& WebEngine::GetBackForwardList() const
 {
   return mPlugin->GetBackForwardList();
old mode 100755 (executable)
new mode 100644 (file)
index 3034d0f..7b9edac
@@ -56,6 +56,18 @@ public:
   static WebEnginePtr New();
 
   /**
+   * @brief Get context of web engine
+   *
+   */
+  static Dali::WebEngineContext* GetContext();
+
+  /**
+   * @brief Get cookie manager of web engine
+   *
+   */
+  static Dali::WebEngineCookieManager* GetCookieManager();
+
+  /**
    * @copydoc Dali::WebEngine::Create()
    */
   void Create(uint32_t width, uint32_t height, const std::string& locale, const std::string& timezoneId);
@@ -81,16 +93,6 @@ public:
   Dali::WebEngineSettings& GetSettings() const;
 
   /**
-   * @copydoc Dali::WebEngine::GetContext()
-   */
-  Dali::WebEngineContext& GetContext() const;
-
-  /**
-   * @copydoc Dali::WebEngine::GetCookieManager()
-   */
-  Dali::WebEngineCookieManager& GetCookieManager() const;
-
-  /**
    * @copydoc Dali::WebEngine::GetBackForwardList()
    */
   Dali::WebEngineBackForwardList& GetBackForwardList() const;
@@ -574,16 +576,22 @@ private:
    *
    * @return Whether the initialization succeed or not.
    */
-  bool InitializePluginHandle();
+  static bool InitializePluginHandle();
+
+  /**
+   * @brief Close library handle.
+   */
+  static void ClosePluginHandle();
 
 private:
-  typedef Dali::WebEnginePlugin* (*CreateWebEngineFunction)();
-  typedef void (*DestroyWebEngineFunction)(Dali::WebEnginePlugin* plugin);
+  using CreateWebEngineFunction  = Dali::WebEnginePlugin* (*)();
+  using DestroyWebEngineFunction = void (*)(Dali::WebEnginePlugin* plugin);
+
+  Dali::WebEnginePlugin* mPlugin; ///< WebEnginePlugin instance
 
-  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
+  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