Support scroll in web engine.
[platform/core/uifw/dali-adaptor.git] / dali / internal / web-engine / common / web-engine-impl.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 988462d..d3718ef
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 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.
 // EXTERNAL INCLUDES
 #include <dlfcn.h>
 #include <dali/integration-api/debug.h>
-#include <dali/public-api/adaptor-framework/native-image-source.h>
 #include <dali/public-api/object/type-registry.h>
+#include <sstream>
+
+// INTERNAL INCLUDES
+#include <dali/devel-api/adaptor-framework/environment-variable.h>
+#include <dali/internal/system/common/environment-variables.h>
+#include <dali/public-api/adaptor-framework/native-image-source.h>
 
 namespace Dali
 {
@@ -36,7 +41,20 @@ namespace Adaptor
 namespace // unnamed namespace
 {
 
-const char* WEB_ENGINE_PLUGIN_SO( "libdali-web-engine-plugin.so" );
+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";
+
+// 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;
+  fullName << kPluginFullNamePrefix << environmentName << kPluginFullNamePostfix;
+  return std::move( fullName.str() );
+}
 
 Dali::BaseHandle Create()
 {
@@ -49,17 +67,15 @@ Dali::TypeRegistration type( typeid( Dali::WebEngine ), typeid( Dali::BaseHandle
 
 WebEnginePtr WebEngine::New()
 {
-  WebEnginePtr ptr;
-  WebEngine* engine = new WebEngine();
+  WebEngine* instance = new WebEngine();
 
-  if ( !engine->Initialize() )
+  if( !instance->Initialize() )
   {
-    delete engine;
-    engine = nullptr;
+    delete instance;
+    return nullptr;
   }
 
-  ptr = engine;
-  return ptr;
+  return instance;
 }
 
 WebEngine::WebEngine()
@@ -84,16 +100,40 @@ WebEngine::~WebEngine()
   }
 }
 
+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;
 
-  mHandle = dlopen( WEB_ENGINE_PLUGIN_SO, RTLD_LAZY );
-
-  error = dlerror();
-  if( mHandle == NULL || error != NULL )
+  if( !InitializePluginHandle() )
   {
-    DALI_LOG_ERROR( "WebEngine::Initialize(), dlopen error: %s\n", error );
     return false;
   }
 
@@ -104,20 +144,19 @@ bool WebEngine::Initialize()
     return false;
   }
 
-  mPlugin = mCreateWebEnginePtr();
+  mDestroyWebEnginePtr = reinterpret_cast< DestroyWebEngineFunction >( dlsym( mHandle, "DestroyWebEnginePlugin" ) );
 
-  if( mPlugin == NULL )
+  if( mDestroyWebEnginePtr == NULL )
   {
-    DALI_LOG_ERROR( "Can't create the WebEnginePlugin object\n" );
+    DALI_LOG_ERROR( "Can't load symbol DestroyWebEnginePlugin(), error: %s\n", error );
     return false;
   }
 
-  mDestroyWebEnginePtr = reinterpret_cast< DestroyWebEngineFunction >( dlsym( mHandle, "DestroyWebEnginePlugin" ) );
+  mPlugin = mCreateWebEnginePtr();
 
-  if( mDestroyWebEnginePtr == NULL )
+  if( mPlugin == NULL )
   {
-
-    DALI_LOG_ERROR( "Can't load symbol DestroyWebEnginePlugin(), error: %s\n", error );
+    DALI_LOG_ERROR( "Can't create the WebEnginePlugin object\n" );
     return false;
   }
 
@@ -126,18 +165,12 @@ bool WebEngine::Initialize()
 
 void WebEngine::Create( int width, int height, const std::string& locale, const std::string& timezoneId )
 {
-  if( mPlugin != NULL )
-  {
-    mPlugin->Create( width, height, locale, timezoneId );
-  }
+  mPlugin->Create( width, height, locale, timezoneId );
 }
 
 void WebEngine::Destroy()
 {
-  if( mPlugin != NULL )
-  {
-    mPlugin->Destroy();
-  }
+  mPlugin->Destroy();
 }
 
 Dali::NativeImageInterfacePtr WebEngine::GetNativeImageSource()
@@ -147,147 +180,221 @@ Dali::NativeImageInterfacePtr WebEngine::GetNativeImageSource()
 
 void WebEngine::LoadUrl( const std::string& url )
 {
-  if( mPlugin != NULL )
-  {
-    mPlugin->LoadUrl( url );
-  }
+  mPlugin->LoadUrl( url );
 }
 
 const std::string& WebEngine::GetUrl()
 {
-  static std::string emptyUrl;
-  return mPlugin ? mPlugin->GetUrl() : emptyUrl;
+  return mPlugin->GetUrl();
 }
 
 void WebEngine::LoadHTMLString( const std::string& htmlString )
 {
-  if( mPlugin != NULL )
-  {
-    mPlugin->LoadHTMLString( htmlString );
-  }
+  mPlugin->LoadHTMLString( htmlString );
 }
 
 void WebEngine::Reload()
 {
-  if( mPlugin != NULL )
-  {
-    mPlugin->Reload();
-  }
+  mPlugin->Reload();
 }
 
 void WebEngine::StopLoading()
 {
-  if( mPlugin != NULL )
-  {
-    mPlugin->StopLoading();
-  }
+  mPlugin->StopLoading();
+}
+
+void WebEngine::Suspend()
+{
+  mPlugin->Suspend();
+}
+
+void WebEngine::Resume()
+{
+  mPlugin->Resume();
+}
+
+void WebEngine::ScrollBy( int deltaX, int deltaY )
+{
+  mPlugin->ScrollBy( deltaX, deltaY );
+}
+
+void WebEngine::SetScrollPosition( int x, int y )
+{
+  mPlugin->SetScrollPosition( x, y );
+}
+
+void WebEngine::GetScrollPosition( int& x, int& y ) const
+{
+  mPlugin->GetScrollPosition( x, y );
+}
+
+void WebEngine::GetScrollSize( int& width, int& height ) const
+{
+  mPlugin->GetScrollSize( width, height );
+}
+
+void WebEngine::GetContentSize( int& width, int& height ) const
+{
+  mPlugin->GetContentSize( width, height );
 }
 
 bool WebEngine::CanGoForward()
 {
-  return mPlugin ? mPlugin->CanGoForward() : false;
+  return mPlugin->CanGoForward();
 }
 
 void WebEngine::GoForward()
 {
-  if( mPlugin != NULL )
-  {
-    mPlugin->GoForward();
-  }
+  mPlugin->GoForward();
 }
 
 bool WebEngine::CanGoBack()
 {
-  return mPlugin ? mPlugin->CanGoBack() : false;
+  return mPlugin->CanGoBack();
 }
 
 void WebEngine::GoBack()
 {
-  if( mPlugin != NULL )
-  {
-    mPlugin->GoBack();
-  }
+  mPlugin->GoBack();
 }
 
-void WebEngine::EvaluateJavaScript( const std::string& script )
+void WebEngine::EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
 {
-  if( mPlugin != NULL )
-  {
-    mPlugin->EvaluateJavaScript( script );
-  }
+  mPlugin->EvaluateJavaScript( script, resultHandler );
 }
 
-void WebEngine::AddJavaScriptInterface( const std::string& exposedObjectName, const std::string& jsFunctionName, std::function< std::string(const std::string&) > cb )
+void WebEngine::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void(const std::string&) > handler )
 {
-  if( mPlugin != NULL )
-  {
-    mPlugin->AddJavaScriptInterface( exposedObjectName, jsFunctionName, cb );
-  }
+  mPlugin->AddJavaScriptMessageHandler( exposedObjectName, handler );
 }
 
-void WebEngine::RemoveJavascriptInterface( const std::string& exposedObjectName, const std::string& jsFunctionName )
+void WebEngine::ClearHistory()
 {
-  if( mPlugin != NULL )
-  {
-    mPlugin->RemoveJavascriptInterface( exposedObjectName, jsFunctionName );
-  }
+  mPlugin->ClearHistory();
 }
 
-void WebEngine::ClearHistory()
+void WebEngine::ClearCache()
 {
-  if( mPlugin != NULL )
-  {
-    mPlugin->ClearHistory();
-  }
+  mPlugin->ClearCache();
 }
 
-void WebEngine::ClearCache()
+void WebEngine::ClearCookies()
 {
-  if( mPlugin != NULL )
-  {
-    mPlugin->ClearCache();
-  }
+  mPlugin->ClearCookies();
 }
 
-void WebEngine::SetSize( int width, int height )
+Dali::WebEnginePlugin::CacheModel WebEngine::GetCacheModel() const
 {
-  if( mPlugin != NULL )
-  {
-    mPlugin->SetSize( width, height );
-  }
+  return mPlugin->GetCacheModel();
 }
 
-bool WebEngine::SendTouchEvent( const Dali::TouchData& touch )
+void WebEngine::SetCacheModel( Dali::WebEnginePlugin::CacheModel cacheModel )
 {
-  if( mPlugin != NULL )
-  {
-    return mPlugin->SendTouchEvent( touch );
-  }
+  mPlugin->SetCacheModel( cacheModel );
+}
+
+Dali::WebEnginePlugin::CookieAcceptPolicy WebEngine::GetCookieAcceptPolicy() const
+{
+  return mPlugin->GetCookieAcceptPolicy();
+}
+
+void WebEngine::SetCookieAcceptPolicy( Dali::WebEnginePlugin::CookieAcceptPolicy policy )
+{
+  mPlugin->SetCookieAcceptPolicy( policy );
+}
+
+const std::string& WebEngine::GetUserAgent() const
+{
+  return mPlugin->GetUserAgent();
+}
 
-  return false;
+void WebEngine::SetUserAgent( const std::string& userAgent )
+{
+  mPlugin->SetUserAgent( userAgent );
+}
+
+bool WebEngine::IsJavaScriptEnabled() const
+{
+  return mPlugin->IsJavaScriptEnabled();
+}
+
+void WebEngine::EnableJavaScript( bool enabled )
+{
+  mPlugin->EnableJavaScript( enabled );
+}
+
+bool WebEngine::AreImagesAutomaticallyLoaded() const
+{
+  return mPlugin->AreImagesAutomaticallyLoaded();
+}
+
+void WebEngine::LoadImagesAutomatically( bool automatic )
+{
+  mPlugin->LoadImagesAutomatically( automatic );
+}
+
+const std::string& WebEngine::GetDefaultTextEncodingName() const
+{
+  return mPlugin->GetDefaultTextEncodingName();
+}
+
+void WebEngine::SetDefaultTextEncodingName( const std::string& defaultTextEncodingName )
+{
+  mPlugin->SetDefaultTextEncodingName( defaultTextEncodingName );
+}
+
+int WebEngine::GetDefaultFontSize() const
+{
+  return mPlugin->GetDefaultFontSize();
+}
+
+void WebEngine::SetDefaultFontSize( int defaultFontSize )
+{
+  mPlugin->SetDefaultFontSize( defaultFontSize );
+}
+
+void WebEngine::SetSize( int width, int height )
+{
+  mPlugin->SetSize( width, height );
+}
+
+bool WebEngine::SendTouchEvent( const Dali::TouchEvent& touch )
+{
+  return mPlugin->SendTouchEvent( touch );
 }
 
 bool WebEngine::SendKeyEvent( const Dali::KeyEvent& event )
 {
-  if( mPlugin != NULL )
-  {
-    return mPlugin->SendKeyEvent( event );
-  }
+  return mPlugin->SendKeyEvent( event );
+}
 
-  return false;
+void WebEngine::SetFocus( bool focused )
+{
+  mPlugin->SetFocus( focused );
 }
 
-Dali::WebEnginePlugin::WebEngineSignalType& WebEngine::PageLoadStartedSignal()
+Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadStartedSignal()
 {
   return mPlugin->PageLoadStartedSignal();
 }
 
-Dali::WebEnginePlugin::WebEngineSignalType& WebEngine::PageLoadFinishedSignal()
+Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadFinishedSignal()
 {
   return mPlugin->PageLoadFinishedSignal();
 }
 
+Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType& WebEngine::PageLoadErrorSignal()
+{
+  return mPlugin->PageLoadErrorSignal();
+}
+
+Dali::WebEnginePlugin::WebEngineScrollEdgeReachedSignalType& WebEngine::ScrollEdgeReachedSignal()
+{
+  return mPlugin->ScrollEdgeReachedSignal();
+}
+
 } // namespace Adaptor;
 } // namespace Internal;
 } // namespace Dali;
 
+