Change resize policy of favicon.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / web-view / web-view-impl.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 9cc4454..1205cc2
@@ -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 <cstring>
-#include <dali/public-api/common/stage.h>
-#include <dali/public-api/images/native-image.h>
+#include <dali/devel-api/adaptor-framework/web-engine-back-forward-list.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-settings.h>
+#include <dali/devel-api/scripting/enum-helper.h>
+#include <dali/devel-api/scripting/scripting.h>
+#include <dali/devel-api/common/stage.h>
 #include <dali/public-api/adaptor-framework/native-image-source.h>
 #include <dali/public-api/object/type-registry.h>
 #include <dali/public-api/object/type-registry-helper.h>
 
 // INTERNAL INCLUDES
 #include <dali-toolkit/devel-api/controls/control-devel.h>
+#include <dali-toolkit/devel-api/controls/web-view/web-back-forward-list.h>
+#include <dali-toolkit/devel-api/controls/web-view/web-context.h>
+#include <dali-toolkit/devel-api/controls/web-view/web-cookie-manager.h>
+#include <dali-toolkit/devel-api/controls/web-view/web-settings.h>
+#include <dali-toolkit/devel-api/image-loader/texture-manager.h>
 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
+#include <dali-toolkit/public-api/image-loader/image.h>
+#include <dali-toolkit/public-api/visuals/image-visual-properties.h>
 
 namespace Dali
 {
@@ -47,17 +59,32 @@ BaseHandle Create()
   return Toolkit::WebView::New();
 }
 
-DALI_TYPE_REGISTRATION_BEGIN( Toolkit::WebView, Toolkit::Control, Create );
+DALI_TYPE_REGISTRATION_BEGIN( Toolkit::WebView, Toolkit::Control, Create )
 
-DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "url", STRING, URL );
+DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "url",                     STRING,  URL                        )
+DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "userAgent",               STRING,  USER_AGENT                 )
+DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "scrollPosition",          VECTOR2, SCROLL_POSITION            )
+DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "scrollSize",              VECTOR2, SCROLL_SIZE                )
+DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "contentSize",             VECTOR2, CONTENT_SIZE               )
+DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "title",                   STRING,  TITLE                      )
 
-DALI_SIGNAL_REGISTRATION( Toolkit, WebView, "pageLoadStarted", PAGE_LOAD_STARTED_SIGNAL );
-DALI_SIGNAL_REGISTRATION( Toolkit, WebView, "pageLoadFinished", PAGE_LOAD_FINISHED_SIGNAL );
+DALI_SIGNAL_REGISTRATION(   Toolkit, WebView, "pageLoadStarted",         PAGE_LOAD_STARTED_SIGNAL            )
+DALI_SIGNAL_REGISTRATION(   Toolkit, WebView, "pageLoadFinished",        PAGE_LOAD_FINISHED_SIGNAL           )
+DALI_SIGNAL_REGISTRATION(   Toolkit, WebView, "pageLoadError",           PAGE_LOAD_ERROR_SIGNAL              )
+DALI_SIGNAL_REGISTRATION(   Toolkit, WebView, "scrollEdgeReached",       SCROLL_EDGE_REACHED_SIGNAL          )
 
 DALI_TYPE_REGISTRATION_END()
 
+const std::string kEmptyString;
+
 } // anonymous namepsace
 
+#define GET_ENUM_STRING( structName, inputExp ) \
+  Scripting::GetLinearEnumerationName< Toolkit::WebView::structName::Type >( static_cast< Toolkit::WebView::structName::Type >( inputExp ), structName##_TABLE, structName##_TABLE_COUNT )
+
+#define GET_ENUM_VALUE( structName, inputExp, outputExp ) \
+  Scripting::GetEnumerationProperty< Toolkit::WebView::structName::Type >( inputExp, structName##_TABLE, structName##_TABLE_COUNT, outputExp )
+
 WebView::WebView( const std::string& locale, const std::string& timezoneId )
 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
   mUrl(),
@@ -65,15 +92,37 @@ WebView::WebView( const std::string& locale, const std::string& timezoneId )
   mWebViewSize( Stage::GetCurrent().GetSize() ),
   mWebEngine(),
   mPageLoadStartedSignal(),
-  mPageLoadFinishedSignal()
+  mPageLoadFinishedSignal(),
+  mPageLoadErrorSignal()
 {
   mWebEngine = Dali::WebEngine::New();
-  if ( mWebEngine )
+
+  // WebEngine is empty when it is not properly initialized.
+  if( mWebEngine )
   {
     mWebEngine.Create( mWebViewSize.width, mWebViewSize.height, locale, timezoneId );
   }
 }
 
+WebView::WebView( int argc, char** argv )
+: Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
+  mUrl(),
+  mVisual(),
+  mWebViewSize( Stage::GetCurrent().GetSize() ),
+  mWebEngine(),
+  mPageLoadStartedSignal(),
+  mPageLoadFinishedSignal(),
+  mPageLoadErrorSignal()
+{
+  mWebEngine = Dali::WebEngine::New();
+
+  // WebEngine is empty when it is not properly initialized.
+  if ( mWebEngine )
+  {
+    mWebEngine.Create( mWebViewSize.width, mWebViewSize.height, argc, argv );
+  }
+}
+
 WebView::WebView()
 : WebView( "", "" )
 {
@@ -101,27 +150,79 @@ Toolkit::WebView WebView::New( const std::string& locale, const std::string& tim
   return handle;
 }
 
+Toolkit::WebView WebView::New( int argc, char** argv )
+{
+  WebView* impl = new WebView( argc, argv );
+  Toolkit::WebView handle = Toolkit::WebView( *impl );
+
+  impl->Initialize();
+  return handle;
+}
+
 void WebView::OnInitialize()
 {
-  Self().SetKeyboardFocusable( true );
-  Self().TouchSignal().Connect( this, &WebView::OnTouchEvent );
+  Self().SetProperty( Actor::Property::KEYBOARD_FOCUSABLE, true );
+  Self().TouchedSignal().Connect( this, &WebView::OnTouchEvent );
 
-  if ( mWebEngine )
+  if( mWebEngine )
   {
     mWebEngine.PageLoadStartedSignal().Connect( this, &WebView::OnPageLoadStarted );
     mWebEngine.PageLoadFinishedSignal().Connect( this, &WebView::OnPageLoadFinished );
+    mWebEngine.PageLoadErrorSignal().Connect( this, &WebView::OnPageLoadError );
+    mWebEngine.ScrollEdgeReachedSignal().Connect( this, &WebView::OnScrollEdgeReached );
+
+    mWebContext = std::unique_ptr<Dali::Toolkit::WebContext>( new WebContext( mWebEngine.GetContext() ) );
+    mWebCookieManager = std::unique_ptr<Dali::Toolkit::WebCookieManager>( new WebCookieManager( mWebEngine.GetCookieManager() ) );
+    mWebSettings = std::unique_ptr<Dali::Toolkit::WebSettings>( new WebSettings( mWebEngine.GetSettings() ) );
+    mWebBackForwardList = std::unique_ptr<Dali::Toolkit::WebBackForwardList>( new WebBackForwardList( mWebEngine.GetBackForwardList() ) );
   }
 }
 
+Dali::Toolkit::WebSettings* WebView::GetSettings() const
+{
+  return mWebSettings.get();
+}
+
+Dali::Toolkit::WebContext* WebView::GetContext() const
+{
+  return mWebContext.get();
+}
+
+Dali::Toolkit::WebCookieManager* WebView::GetCookieManager() const
+{
+  return mWebCookieManager.get();
+}
+
+Dali::Toolkit::WebBackForwardList* WebView::GetBackForwardList() const
+{
+  return mWebBackForwardList.get();
+}
+
+Dali::Toolkit::ImageView& WebView::GetFavicon()
+{
+  if ( mWebEngine )
+  {
+    Dali::PixelData pixelData = mWebEngine.GetFavicon();
+    std::string url = Dali::Toolkit::Image::GenerateUrl( pixelData );
+    mFaviconView = Dali::Toolkit::ImageView::New( url );
+    mFaviconView.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
+    mFaviconView.SetProperty( Dali::Actor::Property::SIZE, Vector2( pixelData.GetWidth(), pixelData.GetHeight() ) );
+  }
+  return mFaviconView;
+}
+
 void WebView::LoadUrl( const std::string& url )
 {
   mUrl = url;
-  if ( mWebEngine )
+  if( mWebEngine )
   {
-    Dali::Image image = Dali::NativeImage::New( *mWebEngine.GetNativeImageSource() );
-    mVisual = Toolkit::VisualFactory::Get().CreateVisual( image );
+    Texture texture = Dali::Texture::New( *mWebEngine.GetNativeImageSource() );
+    const std::string nativeImageUrl = Dali::Toolkit::TextureManager::AddTexture( texture );
+    mVisual = Toolkit::VisualFactory::Get().CreateVisual(
+      { { Toolkit::Visual::Property::TYPE,  Toolkit::Visual::IMAGE } ,
+        { Toolkit::ImageVisual::Property::URL, nativeImageUrl } } );
 
-    if ( mVisual )
+    if( mVisual )
     {
       // Clean up previously registered visual and add new one.
       DevelControl::RegisterVisual( *this, Toolkit::WebView::Property::URL, mVisual );
@@ -130,29 +231,27 @@ void WebView::LoadUrl( const std::string& url )
   }
 }
 
-const std::string& WebView::GetUrl()
-{
-  return mWebEngine ? mWebEngine.GetUrl() : mUrl;
-}
-
-void WebView::LoadHTMLString( const std::string& htmlString )
+void WebView::LoadHtmlString( const std::string& htmlString )
 {
-  if ( mWebEngine )
+  if( mWebEngine )
   {
-    Dali::Image image = Dali::NativeImage::New( *mWebEngine.GetNativeImageSource() );
-    mVisual = Toolkit::VisualFactory::Get().CreateVisual( image );
+    Texture texture = Dali::Texture::New( *mWebEngine.GetNativeImageSource() );
+    const std::string nativeImageUrl = Dali::Toolkit::TextureManager::AddTexture( texture );
+    mVisual = Toolkit::VisualFactory::Get().CreateVisual(
+      { { Toolkit::Visual::Property::TYPE,  Toolkit::Visual::IMAGE } ,
+        { Toolkit::ImageVisual::Property::URL, nativeImageUrl } } );
 
-    if ( mVisual )
+    if( mVisual )
     {
       DevelControl::RegisterVisual( *this, Toolkit::WebView::Property::URL, mVisual );
-      mWebEngine.LoadHTMLString( htmlString );
+      mWebEngine.LoadHtmlString( htmlString );
     }
   }
 }
 
 void WebView::Reload()
 {
-  if ( mWebEngine )
+  if( mWebEngine )
   {
     mWebEngine.Reload();
   }
@@ -160,12 +259,36 @@ void WebView::Reload()
 
 void WebView::StopLoading()
 {
-  if ( mWebEngine )
+  if( mWebEngine )
   {
     mWebEngine.StopLoading();
   }
 }
 
+void WebView::Suspend()
+{
+  if( mWebEngine )
+  {
+    mWebEngine.Suspend();
+  }
+}
+
+void WebView::Resume()
+{
+  if( mWebEngine )
+  {
+    mWebEngine.Resume();
+  }
+}
+
+void WebView::ScrollBy( int deltaX, int deltaY )
+{
+  if ( mWebEngine )
+  {
+    mWebEngine.ScrollBy( deltaX, deltaY );
+  }
+}
+
 bool WebView::CanGoForward()
 {
   return mWebEngine ? mWebEngine.CanGoForward() : false;
@@ -173,7 +296,7 @@ bool WebView::CanGoForward()
 
 void WebView::GoForward()
 {
-  if ( mWebEngine )
+  if( mWebEngine )
   {
     mWebEngine.GoForward();
   }
@@ -186,60 +309,62 @@ bool WebView::CanGoBack()
 
 void WebView::GoBack()
 {
-  if ( mWebEngine )
+  if( mWebEngine )
   {
     mWebEngine.GoBack();
   }
 }
 
-void WebView::EvaluateJavaScript( const std::string& script )
+void WebView::EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
 {
-  if ( mWebEngine )
+  if( mWebEngine )
   {
-    mWebEngine.EvaluateJavaScript( script );
+    mWebEngine.EvaluateJavaScript( script, resultHandler );
   }
 }
 
-void WebView::AddJavaScriptInterface( const std::string& exposedObjectName, const std::string& jsFunctionName, std::function< std::string(const std::string&) > callback )
+void WebView::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void( const std::string& ) > handler )
 {
-  if ( mWebEngine )
+  if( mWebEngine )
   {
-    mWebEngine.AddJavaScriptInterface( exposedObjectName, jsFunctionName, callback );
+    mWebEngine.AddJavaScriptMessageHandler( exposedObjectName, handler );
   }
 }
 
-void WebView::RemoveJavascriptInterface( const std::string& exposedObjectName, const std::string& jsFunctionName )
+void WebView::ClearAllTilesResources()
 {
-  if ( mWebEngine )
+  if( mWebEngine )
   {
-    mWebEngine.RemoveJavascriptInterface( exposedObjectName, jsFunctionName );
+    mWebEngine.ClearAllTilesResources();
   }
 }
 
 void WebView::ClearHistory()
 {
-  if ( mWebEngine )
+  if( mWebEngine )
   {
     mWebEngine.ClearHistory();
   }
 }
 
-void WebView::ClearCache()
+Dali::Toolkit::WebView::WebViewPageLoadSignalType& WebView::PageLoadStartedSignal()
 {
-  if ( mWebEngine )
-  {
-    mWebEngine.ClearCache();
-  }
+  return mPageLoadStartedSignal;
 }
 
-Dali::Toolkit::WebView::WebViewSignalType& WebView::PageLoadStartedSignal()
+Dali::Toolkit::WebView::WebViewPageLoadSignalType& WebView::PageLoadFinishedSignal()
 {
-  return mPageLoadStartedSignal;
+  return mPageLoadFinishedSignal;
 }
 
-Dali::Toolkit::WebView::WebViewSignalType& WebView::PageLoadFinishedSignal()
+Dali::Toolkit::WebView::WebViewPageLoadErrorSignalType& WebView::PageLoadErrorSignal()
 {
-  return mPageLoadFinishedSignal;
+  return mPageLoadErrorSignal;
+}
+
+Dali::Toolkit::WebView::WebViewScrollEdgeReachedSignalType& WebView::ScrollEdgeReachedSignal()
+{
+  return mScrollEdgeReachedSignal;
 }
 
 void WebView::OnPageLoadStarted( const std::string& url )
@@ -260,6 +385,24 @@ void WebView::OnPageLoadFinished( const std::string& url )
   }
 }
 
+void WebView::OnPageLoadError( const std::string& url, int errorCode )
+{
+  if( !mPageLoadErrorSignal.Empty() )
+  {
+    Dali::Toolkit::WebView handle( GetOwner() );
+    mPageLoadErrorSignal.Emit( handle, url, static_cast< Toolkit::WebView::LoadErrorCode >( errorCode ) );
+  }
+}
+
+void WebView::OnScrollEdgeReached( Dali::WebEnginePlugin::ScrollEdge edge )
+{
+  if( !mScrollEdgeReachedSignal.Empty() )
+  {
+    Dali::Toolkit::WebView handle( GetOwner() );
+    mScrollEdgeReachedSignal.Emit( handle, edge );
+  }
+}
+
 bool WebView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
 {
   Dali::BaseHandle handle( object );
@@ -277,6 +420,16 @@ bool WebView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* t
     webView.PageLoadFinishedSignal().Connect( tracker, functor );
     connected = true;
   }
+  else if( 0 == strcmp( signalName.c_str(), PAGE_LOAD_ERROR_SIGNAL ) )
+  {
+    webView.PageLoadErrorSignal().Connect( tracker, functor );
+    connected = true;
+  }
+  else if( 0 == strcmp( signalName.c_str(), SCROLL_EDGE_REACHED_SIGNAL ) )
+  {
+    webView.ScrollEdgeReachedSignal().Connect( tracker, functor );
+    connected = true;
+  }
 
   return connected;
 }
@@ -326,6 +479,24 @@ void WebView::SetProperty( BaseObject* object, Property::Index index, const Prop
         }
         break;
       }
+      case Toolkit::WebView::Property::USER_AGENT:
+      {
+        std::string input;
+        if( value.Get( input ) )
+        {
+          impl.SetUserAgent( input );
+        }
+        break;
+      }
+      case Toolkit::WebView::Property::SCROLL_POSITION:
+      {
+        Vector2 input;
+        if ( value.Get( input ) )
+        {
+          impl.SetScrollPosition( input.x, input.y );
+        }
+        break;
+      }
     }
   }
 }
@@ -343,20 +514,53 @@ Property::Value WebView::GetProperty( BaseObject* object, Property::Index proper
     {
       case Toolkit::WebView::Property::URL:
       {
-        value = impl.GetUrl();
+        value = impl.mUrl;
+        break;
+      }
+      case Toolkit::WebView::Property::USER_AGENT:
+      {
+        value = impl.GetUserAgent();
+        break;
+      }
+      case Toolkit::WebView::Property::SCROLL_POSITION:
+      {
+        int x, y;
+        impl.GetScrollPosition( x, y );
+        value = Vector2( x, y );
+        break;
+      }
+      case Toolkit::WebView::Property::SCROLL_SIZE:
+      {
+        int width, height;
+        impl.GetScrollSize( width, height );
+        value = Vector2( width, height );
+        break;
+      }
+      case Toolkit::WebView::Property::CONTENT_SIZE:
+      {
+        int width, height;
+        impl.GetContentSize( width, height );
+        value = Vector2( width, height );
         break;
       }
+      case Toolkit::WebView::Property::TITLE:
+      {
+        value = impl.GetTitle();
+        break;
+      }
+      default:
+         break;
     }
   }
 
   return value;
 }
 
-bool WebView::OnTouchEvent( Actor actor, const Dali::TouchData& touch )
+bool WebView::OnTouchEvent( Actor actor, const Dali::TouchEvent& touch )
 {
   bool result = false;
 
-  if ( mWebEngine )
+  if( mWebEngine )
   {
     result = mWebEngine.SendTouchEvent( touch );
   }
@@ -367,13 +571,86 @@ bool WebView::OnKeyEvent( const Dali::KeyEvent& event )
 {
   bool result = false;
 
-  if ( mWebEngine )
+  if( mWebEngine )
   {
     result = mWebEngine.SendKeyEvent( event );
   }
   return result;
 }
 
+void WebView::OnKeyInputFocusGained()
+{
+  if( mWebEngine )
+  {
+    mWebEngine.SetFocus( true );
+  }
+
+  EmitKeyInputFocusSignal( true ); // Calls back into the Control hence done last.
+}
+
+void WebView::OnKeyInputFocusLost()
+{
+  if( mWebEngine )
+  {
+    mWebEngine.SetFocus( false );
+  }
+
+  EmitKeyInputFocusSignal( false ); // Calls back into the Control hence done last.
+}
+
+void WebView::SetScrollPosition( int x, int y )
+{
+  if( mWebEngine )
+  {
+    mWebEngine.SetScrollPosition( x, y );
+  }
+}
+
+void WebView::GetScrollPosition( int& x, int& y ) const
+{
+  if( mWebEngine )
+  {
+    mWebEngine.GetScrollPosition( x, y );
+  }
+}
+
+void WebView::GetScrollSize( int& width, int& height ) const
+{
+  if( mWebEngine )
+  {
+    mWebEngine.GetScrollSize( width, height );
+  }
+}
+
+void WebView::GetContentSize( int& width, int& height ) const
+{
+  if( mWebEngine )
+  {
+    mWebEngine.GetContentSize( width, height );
+  }
+}
+
+std::string WebView::GetTitle() const
+{
+  return mWebEngine ?  mWebEngine.GetTitle() : kEmptyString;
+}
+
+const std::string& WebView::GetUserAgent() const
+{
+  return mWebEngine ? mWebEngine.GetUserAgent() : kEmptyString;
+}
+
+void WebView::SetUserAgent( const std::string& userAgent )
+{
+  if( mWebEngine )
+  {
+    mWebEngine.SetUserAgent( userAgent );
+  }
+}
+
+#undef GET_ENUM_STRING
+#undef GET_ENUM_VALUE
+
 } // namespace Internal
 
 } // namespace Toolkit