2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "web-view-impl.h"
23 #include <dali/devel-api/scripting/enum-helper.h>
24 #include <dali/devel-api/scripting/scripting.h>
25 #include <dali/devel-api/common/stage.h>
26 #include <dali/public-api/adaptor-framework/native-image-source.h>
27 #include <dali/public-api/object/type-registry.h>
28 #include <dali/public-api/object/type-registry-helper.h>
31 #include <dali-toolkit/devel-api/controls/control-devel.h>
32 #include <dali-toolkit/devel-api/image-loader/texture-manager.h>
33 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
34 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
50 return Toolkit::WebView::New();
53 DALI_ENUM_TO_STRING_TABLE_BEGIN( CacheModel )
54 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::WebView::CacheModel, DOCUMENT_VIEWER )
55 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::WebView::CacheModel, DOCUMENT_BROWSER )
56 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::WebView::CacheModel, PRIMARY_WEB_BROWSER )
57 DALI_ENUM_TO_STRING_TABLE_END( CacheModel )
59 DALI_ENUM_TO_STRING_TABLE_BEGIN( CookieAcceptPolicy )
60 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::WebView::CookieAcceptPolicy, ALWAYS )
61 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::WebView::CookieAcceptPolicy, NEVER )
62 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::WebView::CookieAcceptPolicy, NO_THIRD_PARTY )
63 DALI_ENUM_TO_STRING_TABLE_END( CookieAcceptPolicy )
65 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::WebView, Toolkit::Control, Create )
67 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "url", STRING, URL )
68 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "cacheModel", STRING, CACHE_MODEL )
69 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "cookieAcceptPolicy", STRING, COOKIE_ACCEPT_POLICY )
70 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "userAgent", STRING, USER_AGENT )
71 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "enableJavaScript", BOOLEAN, ENABLE_JAVASCRIPT )
72 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "loadImagesAutomatically", BOOLEAN, LOAD_IMAGES_AUTOMATICALLY )
73 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "defaultTextEncodingName", STRING, DEFAULT_TEXT_ENCODING_NAME )
74 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "defaultFontSize", INTEGER, DEFAULT_FONT_SIZE )
75 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "scrollPosition", VECTOR2, SCROLL_POSITION )
76 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "scrollSize", VECTOR2, SCROLL_SIZE )
77 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "contentSize", VECTOR2, CONTENT_SIZE )
79 DALI_SIGNAL_REGISTRATION( Toolkit, WebView, "pageLoadStarted", PAGE_LOAD_STARTED_SIGNAL )
80 DALI_SIGNAL_REGISTRATION( Toolkit, WebView, "pageLoadFinished", PAGE_LOAD_FINISHED_SIGNAL )
81 DALI_SIGNAL_REGISTRATION( Toolkit, WebView, "pageLoadError", PAGE_LOAD_ERROR_SIGNAL )
82 DALI_SIGNAL_REGISTRATION( Toolkit, WebView, "scrollEdgeReached", SCROLL_EDGE_REACHED_SIGNAL )
84 DALI_TYPE_REGISTRATION_END()
86 const std::string kEmptyString;
88 } // anonymous namepsace
90 #define GET_ENUM_STRING( structName, inputExp ) \
91 Scripting::GetLinearEnumerationName< Toolkit::WebView::structName::Type >( static_cast< Toolkit::WebView::structName::Type >( inputExp ), structName##_TABLE, structName##_TABLE_COUNT )
93 #define GET_ENUM_VALUE( structName, inputExp, outputExp ) \
94 Scripting::GetEnumerationProperty< Toolkit::WebView::structName::Type >( inputExp, structName##_TABLE, structName##_TABLE_COUNT, outputExp )
96 WebView::WebView( const std::string& locale, const std::string& timezoneId )
97 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
100 mWebViewSize( Stage::GetCurrent().GetSize() ),
102 mPageLoadStartedSignal(),
103 mPageLoadFinishedSignal(),
104 mPageLoadErrorSignal()
106 mWebEngine = Dali::WebEngine::New();
108 // WebEngine is empty when it is not properly initialized.
111 mWebEngine.Create( mWebViewSize.width, mWebViewSize.height, locale, timezoneId );
124 Toolkit::WebView WebView::New()
126 WebView* impl = new WebView();
127 Toolkit::WebView handle = Toolkit::WebView( *impl );
133 Toolkit::WebView WebView::New( const std::string& locale, const std::string& timezoneId )
135 WebView* impl = new WebView( locale, timezoneId );
136 Toolkit::WebView handle = Toolkit::WebView( *impl );
142 void WebView::OnInitialize()
144 Self().SetProperty( Actor::Property::KEYBOARD_FOCUSABLE, true );
145 Self().TouchedSignal().Connect( this, &WebView::OnTouchEvent );
149 mWebEngine.PageLoadStartedSignal().Connect( this, &WebView::OnPageLoadStarted );
150 mWebEngine.PageLoadFinishedSignal().Connect( this, &WebView::OnPageLoadFinished );
151 mWebEngine.PageLoadErrorSignal().Connect( this, &WebView::OnPageLoadError );
152 mWebEngine.ScrollEdgeReachedSignal().Connect( this, &WebView::OnScrollEdgeReached );
156 void WebView::LoadUrl( const std::string& url )
161 Texture texture = Dali::Texture::New( *mWebEngine.GetNativeImageSource() );
162 const std::string nativeImageUrl = Dali::Toolkit::TextureManager::AddTexture( texture );
163 mVisual = Toolkit::VisualFactory::Get().CreateVisual(
164 { { Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE } ,
165 { Toolkit::ImageVisual::Property::URL, nativeImageUrl } } );
169 // Clean up previously registered visual and add new one.
170 DevelControl::RegisterVisual( *this, Toolkit::WebView::Property::URL, mVisual );
171 mWebEngine.LoadUrl( url );
176 void WebView::LoadHTMLString( const std::string& htmlString )
180 Texture texture = Dali::Texture::New( *mWebEngine.GetNativeImageSource() );
181 const std::string nativeImageUrl = Dali::Toolkit::TextureManager::AddTexture( texture );
182 mVisual = Toolkit::VisualFactory::Get().CreateVisual(
183 { { Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE } ,
184 { Toolkit::ImageVisual::Property::URL, nativeImageUrl } } );
188 DevelControl::RegisterVisual( *this, Toolkit::WebView::Property::URL, mVisual );
189 mWebEngine.LoadHTMLString( htmlString );
194 void WebView::Reload()
202 void WebView::StopLoading()
206 mWebEngine.StopLoading();
210 void WebView::Suspend()
214 mWebEngine.Suspend();
218 void WebView::Resume()
226 void WebView::ScrollBy( int deltaX, int deltaY )
230 mWebEngine.ScrollBy( deltaX, deltaY );
234 bool WebView::CanGoForward()
236 return mWebEngine ? mWebEngine.CanGoForward() : false;
239 void WebView::GoForward()
243 mWebEngine.GoForward();
247 bool WebView::CanGoBack()
249 return mWebEngine ? mWebEngine.CanGoBack() : false;
252 void WebView::GoBack()
260 void WebView::EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
264 mWebEngine.EvaluateJavaScript( script, resultHandler );
268 void WebView::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void( const std::string& ) > handler )
272 mWebEngine.AddJavaScriptMessageHandler( exposedObjectName, handler );
276 void WebView::ClearHistory()
280 mWebEngine.ClearHistory();
284 void WebView::ClearCache()
288 mWebEngine.ClearCache();
292 void WebView::ClearCookies()
296 mWebEngine.ClearCookies();
300 Dali::Toolkit::WebView::WebViewPageLoadSignalType& WebView::PageLoadStartedSignal()
302 return mPageLoadStartedSignal;
305 Dali::Toolkit::WebView::WebViewPageLoadSignalType& WebView::PageLoadFinishedSignal()
307 return mPageLoadFinishedSignal;
310 Dali::Toolkit::WebView::WebViewPageLoadErrorSignalType& WebView::PageLoadErrorSignal()
312 return mPageLoadErrorSignal;
315 Dali::Toolkit::WebView::WebViewScrollEdgeReachedSignalType& WebView::ScrollEdgeReachedSignal()
317 return mScrollEdgeReachedSignal;
320 void WebView::OnPageLoadStarted( const std::string& url )
322 if( !mPageLoadStartedSignal.Empty() )
324 Dali::Toolkit::WebView handle( GetOwner() );
325 mPageLoadStartedSignal.Emit( handle, url );
329 void WebView::OnPageLoadFinished( const std::string& url )
331 if( !mPageLoadFinishedSignal.Empty() )
333 Dali::Toolkit::WebView handle( GetOwner() );
334 mPageLoadFinishedSignal.Emit( handle, url );
338 void WebView::OnPageLoadError( const std::string& url, int errorCode )
340 if( !mPageLoadErrorSignal.Empty() )
342 Dali::Toolkit::WebView handle( GetOwner() );
343 mPageLoadErrorSignal.Emit( handle, url, static_cast< Toolkit::WebView::LoadErrorCode >( errorCode ) );
347 void WebView::OnScrollEdgeReached( Dali::WebEnginePlugin::ScrollEdge edge )
349 if( !mScrollEdgeReachedSignal.Empty() )
351 Dali::Toolkit::WebView handle( GetOwner() );
352 mScrollEdgeReachedSignal.Emit( handle, edge );
356 bool WebView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
358 Dali::BaseHandle handle( object );
360 bool connected = false;
361 Toolkit::WebView webView = Toolkit::WebView::DownCast( handle );
363 if( 0 == strcmp( signalName.c_str(), PAGE_LOAD_STARTED_SIGNAL ) )
365 webView.PageLoadStartedSignal().Connect( tracker, functor );
368 else if( 0 == strcmp( signalName.c_str(), PAGE_LOAD_FINISHED_SIGNAL ) )
370 webView.PageLoadFinishedSignal().Connect( tracker, functor );
373 else if( 0 == strcmp( signalName.c_str(), PAGE_LOAD_ERROR_SIGNAL ) )
375 webView.PageLoadErrorSignal().Connect( tracker, functor );
378 else if( 0 == strcmp( signalName.c_str(), SCROLL_EDGE_REACHED_SIGNAL ) )
380 webView.ScrollEdgeReachedSignal().Connect( tracker, functor );
387 Vector3 WebView::GetNaturalSize()
391 Vector2 rendererNaturalSize;
392 mVisual.GetNaturalSize( rendererNaturalSize );
393 return Vector3( rendererNaturalSize );
396 return Vector3( mWebViewSize );
399 void WebView::OnRelayout( const Vector2& size, RelayoutContainer& container )
401 Control::OnRelayout( size, container );
403 if( size.width > 0 && size.height > 0 && mWebViewSize != size )
409 mWebEngine.SetSize( size.width, size.height );
414 void WebView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
416 Toolkit::WebView webView = Toolkit::WebView::DownCast( Dali::BaseHandle( object ) );
420 WebView& impl = GetImpl( webView );
423 case Toolkit::WebView::Property::URL:
426 if( value.Get( url ) )
432 case Toolkit::WebView::Property::CACHE_MODEL:
434 Toolkit::WebView::CacheModel::Type output = impl.GetCacheModel();
435 GET_ENUM_VALUE( CacheModel, value, output );
436 impl.SetCacheModel( output );
439 case Toolkit::WebView::Property::COOKIE_ACCEPT_POLICY:
441 Toolkit::WebView::CookieAcceptPolicy::Type output = impl.GetCookieAcceptPolicy();
442 GET_ENUM_VALUE( CookieAcceptPolicy, value, output );
443 impl.SetCookieAcceptPolicy( output );
446 case Toolkit::WebView::Property::USER_AGENT:
449 if( value.Get( input ) )
451 impl.SetUserAgent( input );
455 case Toolkit::WebView::Property::ENABLE_JAVASCRIPT:
458 if( value.Get( input ) )
460 impl.EnableJavaScript( input );
464 case Toolkit::WebView::Property::LOAD_IMAGES_AUTOMATICALLY:
467 if( value.Get( input ) )
469 impl.LoadImagesAutomatically( input );
473 case Toolkit::WebView::Property::DEFAULT_TEXT_ENCODING_NAME:
476 if( value.Get( input ) )
478 impl.SetDefaultTextEncodingName( input );
482 case Toolkit::WebView::Property::DEFAULT_FONT_SIZE:
485 if( value.Get( input ) )
487 impl.SetDefaultFontSize( input );
491 case Toolkit::WebView::Property::SCROLL_POSITION:
494 if ( value.Get( input ) )
496 impl.SetScrollPosition( input.x, input.y );
504 Property::Value WebView::GetProperty( BaseObject* object, Property::Index propertyIndex )
506 Property::Value value;
508 Toolkit::WebView webView = Toolkit::WebView::DownCast( Dali::BaseHandle( object ) );
512 WebView& impl = GetImpl( webView );
513 switch( propertyIndex )
515 case Toolkit::WebView::Property::URL:
520 case Toolkit::WebView::Property::CACHE_MODEL:
522 value = GET_ENUM_STRING( CacheModel, impl.GetCacheModel() );
525 case Toolkit::WebView::Property::COOKIE_ACCEPT_POLICY:
527 value = GET_ENUM_STRING( CookieAcceptPolicy, impl.GetCookieAcceptPolicy() );
530 case Toolkit::WebView::Property::USER_AGENT:
532 value = impl.GetUserAgent();
535 case Toolkit::WebView::Property::ENABLE_JAVASCRIPT:
537 value = impl.IsJavaScriptEnabled();
540 case Toolkit::WebView::Property::LOAD_IMAGES_AUTOMATICALLY:
542 value = impl.AreImagesAutomaticallyLoaded();
545 case Toolkit::WebView::Property::DEFAULT_TEXT_ENCODING_NAME:
547 value = impl.GetDefaultTextEncodingName();
550 case Toolkit::WebView::Property::DEFAULT_FONT_SIZE:
552 value = impl.GetDefaultFontSize();
555 case Toolkit::WebView::Property::SCROLL_POSITION:
558 impl.GetScrollPosition( x, y );
559 value = Vector2( x, y );
562 case Toolkit::WebView::Property::SCROLL_SIZE:
565 impl.GetScrollSize( width, height );
566 value = Vector2( width, height );
569 case Toolkit::WebView::Property::CONTENT_SIZE:
572 impl.GetContentSize( width, height );
573 value = Vector2( width, height );
584 bool WebView::OnTouchEvent( Actor actor, const Dali::TouchEvent& touch )
590 result = mWebEngine.SendTouchEvent( touch );
595 bool WebView::OnKeyEvent( const Dali::KeyEvent& event )
601 result = mWebEngine.SendKeyEvent( event );
606 void WebView::OnKeyInputFocusGained()
610 mWebEngine.SetFocus( true );
613 EmitKeyInputFocusSignal( true ); // Calls back into the Control hence done last.
616 void WebView::OnKeyInputFocusLost()
620 mWebEngine.SetFocus( false );
623 EmitKeyInputFocusSignal( false ); // Calls back into the Control hence done last.
626 void WebView::SetScrollPosition( int x, int y )
630 mWebEngine.SetScrollPosition( x, y );
634 void WebView::GetScrollPosition( int& x, int& y ) const
638 mWebEngine.GetScrollPosition( x, y );
642 void WebView::GetScrollSize( int& width, int& height ) const
646 mWebEngine.GetScrollSize( width, height );
650 void WebView::GetContentSize( int& width, int& height ) const
654 mWebEngine.GetContentSize( width, height );
658 Toolkit::WebView::CacheModel::Type WebView::GetCacheModel() const
660 return mWebEngine ? static_cast< Toolkit::WebView::CacheModel::Type >( mWebEngine.GetCacheModel() ) : Toolkit::WebView::CacheModel::DOCUMENT_VIEWER;
663 void WebView::SetCacheModel( Toolkit::WebView::CacheModel::Type cacheModel )
667 mWebEngine.SetCacheModel( static_cast< WebEnginePlugin::CacheModel >( cacheModel ) );
671 Toolkit::WebView::CookieAcceptPolicy::Type WebView::GetCookieAcceptPolicy() const
673 return mWebEngine ? static_cast< Toolkit::WebView::CookieAcceptPolicy::Type >( mWebEngine.GetCookieAcceptPolicy() ) : Toolkit::WebView::CookieAcceptPolicy::NO_THIRD_PARTY;
676 void WebView::SetCookieAcceptPolicy( Toolkit::WebView::CookieAcceptPolicy::Type policy )
680 mWebEngine.SetCookieAcceptPolicy( static_cast< WebEnginePlugin::CookieAcceptPolicy >( policy ) );
684 const std::string& WebView::GetUserAgent() const
686 return mWebEngine ? mWebEngine.GetUserAgent() : kEmptyString;
689 void WebView::SetUserAgent( const std::string& userAgent )
693 mWebEngine.SetUserAgent( userAgent );
697 bool WebView::IsJavaScriptEnabled() const
699 return mWebEngine ? mWebEngine.IsJavaScriptEnabled() : true;
702 void WebView::EnableJavaScript( bool enabled )
706 mWebEngine.EnableJavaScript( enabled );
710 bool WebView::AreImagesAutomaticallyLoaded() const
712 return mWebEngine ? mWebEngine.AreImagesAutomaticallyLoaded() : true;
715 void WebView::LoadImagesAutomatically( bool automatic )
719 mWebEngine.LoadImagesAutomatically( automatic );
723 const std::string& WebView::GetDefaultTextEncodingName() const
725 return mWebEngine ? mWebEngine.GetDefaultTextEncodingName() : kEmptyString;
728 void WebView::SetDefaultTextEncodingName( const std::string& defaultTextEncodingName )
732 mWebEngine.SetDefaultTextEncodingName( defaultTextEncodingName );
736 int WebView::GetDefaultFontSize() const
738 return mWebEngine ? mWebEngine.GetDefaultFontSize() : 0;
741 void WebView::SetDefaultFontSize( int defaultFontSize )
745 mWebEngine.SetDefaultFontSize( defaultFontSize );
749 #undef GET_ENUM_STRING
750 #undef GET_ENUM_VALUE
752 } // namespace Internal
754 } // namespace Toolkit