Make web engine context be a singleton.
[platform/core/uifw/dali-adaptor.git] / dali / internal / web-engine / common / web-engine-impl.cpp
old mode 100755 (executable)
new mode 100644 (file)
index f4753f0..1ec614a
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 // INTERNAL INCLUDES
 #include <dali/devel-api/adaptor-framework/environment-variable.h>
-#include <dali/devel-api/adaptor-framework/web-engine-back-forward-list.h>
-#include <dali/devel-api/adaptor-framework/web-engine-certificate.h>
-#include <dali/devel-api/adaptor-framework/web-engine-console-message.h>
-#include <dali/devel-api/adaptor-framework/web-engine-context-menu.h>
-#include <dali/devel-api/adaptor-framework/web-engine-context.h>
-#include <dali/devel-api/adaptor-framework/web-engine-cookie-manager.h>
-#include <dali/devel-api/adaptor-framework/web-engine-http-auth-handler.h>
-#include <dali/devel-api/adaptor-framework/web-engine-load-error.h>
-#include <dali/devel-api/adaptor-framework/web-engine-policy-decision.h>
-#include <dali/devel-api/adaptor-framework/web-engine-request-interceptor.h>
-#include <dali/devel-api/adaptor-framework/web-engine-settings.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>
+#include <dali/devel-api/adaptor-framework/web-engine/web-engine-context-menu.h>
+#include <dali/devel-api/adaptor-framework/web-engine/web-engine-context.h>
+#include <dali/devel-api/adaptor-framework/web-engine/web-engine-cookie-manager.h>
+#include <dali/devel-api/adaptor-framework/web-engine/web-engine-http-auth-handler.h>
+#include <dali/devel-api/adaptor-framework/web-engine/web-engine-load-error.h>
+#include <dali/devel-api/adaptor-framework/web-engine/web-engine-policy-decision.h>
+#include <dali/devel-api/adaptor-framework/web-engine/web-engine-settings.h>
 #include <dali/internal/system/common/environment-variables.h>
 #include <dali/public-api/adaptor-framework/native-image-source.h>
 #include <dali/public-api/images/pixel-data.h>
@@ -73,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;
@@ -86,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.
@@ -117,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);
@@ -133,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;
+}
+
+void WebEngine::ClosePluginHandle()
+{
+  if(mHandle)
+  {
+    dlclose(mHandle);
+    mHandle = nullptr;
+  }
+}
+
+WebEngine::WebEngine()
+: mPlugin(nullptr)
+{
+}
 
-  if(mDestroyWebEnginePtr == NULL)
+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;
@@ -186,7 +227,7 @@ void WebEngine::Destroy()
   mPlugin->Destroy();
 }
 
-Dali::NativeImageInterfacePtr WebEngine::GetNativeImageSource()
+Dali::NativeImageSourcePtr WebEngine::GetNativeImageSource()
 {
   return mPlugin->GetNativeImageSource();
 }
@@ -196,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();
@@ -516,6 +547,11 @@ void WebEngine::ActivateAccessibility(bool activated)
   mPlugin->ActivateAccessibility(activated);
 }
 
+Accessibility::Address WebEngine::GetAccessibilityAddress()
+{
+  return mPlugin->GetAccessibilityAddress();
+}
+
 bool WebEngine::SetVisibility(bool visible)
 {
   return mPlugin->SetVisibility(visible);
@@ -611,11 +647,6 @@ void WebEngine::RegisterFormRepostDecidedCallback(Dali::WebEnginePlugin::WebEngi
   mPlugin->RegisterFormRepostDecidedCallback(callback);
 }
 
-void WebEngine::RegisterRequestInterceptorCallback(Dali::WebEnginePlugin::WebEngineRequestInterceptorCallback callback)
-{
-  mPlugin->RegisterRequestInterceptorCallback(callback);
-}
-
 void WebEngine::RegisterConsoleMessageReceivedCallback(Dali::WebEnginePlugin::WebEngineConsoleMessageReceivedCallback callback)
 {
   mPlugin->RegisterConsoleMessageReceivedCallback(callback);
@@ -626,6 +657,11 @@ void WebEngine::RegisterResponsePolicyDecidedCallback(Dali::WebEnginePlugin::Web
   mPlugin->RegisterResponsePolicyDecidedCallback(callback);
 }
 
+void WebEngine::RegisterNavigationPolicyDecidedCallback(Dali::WebEnginePlugin::WebEngineNavigationPolicyDecidedCallback callback)
+{
+  mPlugin->RegisterNavigationPolicyDecidedCallback(callback);
+}
+
 void WebEngine::RegisterCertificateConfirmedCallback(Dali::WebEnginePlugin::WebEngineCertificateCallback callback)
 {
   mPlugin->RegisterCertificateConfirmedCallback(callback);