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/adaptor-framework/web-engine-back-forward-list.h>
24 #include <dali/devel-api/adaptor-framework/web-engine-context.h>
25 #include <dali/devel-api/adaptor-framework/web-engine-cookie-manager.h>
26 #include <dali/devel-api/adaptor-framework/web-engine-settings.h>
27 #include <dali/devel-api/scripting/enum-helper.h>
28 #include <dali/devel-api/scripting/scripting.h>
29 #include <dali/devel-api/common/stage.h>
30 #include <dali/public-api/adaptor-framework/native-image-source.h>
31 #include <dali/public-api/object/type-registry.h>
32 #include <dali/public-api/object/type-registry-helper.h>
35 #include <dali-toolkit/devel-api/controls/control-devel.h>
36 #include <dali-toolkit/devel-api/controls/web-view/web-back-forward-list.h>
37 #include <dali-toolkit/devel-api/controls/web-view/web-context.h>
38 #include <dali-toolkit/devel-api/controls/web-view/web-cookie-manager.h>
39 #include <dali-toolkit/devel-api/controls/web-view/web-settings.h>
40 #include <dali-toolkit/devel-api/image-loader/texture-manager.h>
41 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
42 #include <dali-toolkit/public-api/image-loader/image.h>
43 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
59 return Toolkit::WebView::New();
62 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::WebView, Toolkit::Control, Create )
64 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "url", STRING, URL )
65 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "userAgent", STRING, USER_AGENT )
66 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "scrollPosition", VECTOR2, SCROLL_POSITION )
67 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "scrollSize", VECTOR2, SCROLL_SIZE )
68 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "contentSize", VECTOR2, CONTENT_SIZE )
69 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "title", STRING, TITLE )
70 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "videoHoleEnabled", BOOLEAN, VIDEO_HOLE_ENABLED )
72 DALI_SIGNAL_REGISTRATION( Toolkit, WebView, "pageLoadStarted", PAGE_LOAD_STARTED_SIGNAL )
73 DALI_SIGNAL_REGISTRATION( Toolkit, WebView, "pageLoadFinished", PAGE_LOAD_FINISHED_SIGNAL )
74 DALI_SIGNAL_REGISTRATION( Toolkit, WebView, "pageLoadError", PAGE_LOAD_ERROR_SIGNAL )
75 DALI_SIGNAL_REGISTRATION( Toolkit, WebView, "scrollEdgeReached", SCROLL_EDGE_REACHED_SIGNAL )
77 DALI_TYPE_REGISTRATION_END()
79 const std::string kEmptyString;
81 } // anonymous namepsace
83 #define GET_ENUM_STRING( structName, inputExp ) \
84 Scripting::GetLinearEnumerationName< Toolkit::WebView::structName::Type >( static_cast< Toolkit::WebView::structName::Type >( inputExp ), structName##_TABLE, structName##_TABLE_COUNT )
86 #define GET_ENUM_VALUE( structName, inputExp, outputExp ) \
87 Scripting::GetEnumerationProperty< Toolkit::WebView::structName::Type >( inputExp, structName##_TABLE, structName##_TABLE_COUNT, outputExp )
89 WebView::WebView( const std::string& locale, const std::string& timezoneId )
90 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
93 mWebViewSize( Stage::GetCurrent().GetSize() ),
95 mPageLoadStartedSignal(),
96 mPageLoadFinishedSignal(),
97 mPageLoadErrorSignal(),
98 mVideoHoleEnabled( true ),
99 mWebViewArea ( 0, 0, mWebViewSize.width, mWebViewSize.height )
101 mWebEngine = Dali::WebEngine::New();
103 // WebEngine is empty when it is not properly initialized.
106 mWebEngine.Create( mWebViewSize.width, mWebViewSize.height, locale, timezoneId );
110 WebView::WebView( int argc, char** argv )
111 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
114 mWebViewSize( Stage::GetCurrent().GetSize() ),
116 mPageLoadStartedSignal(),
117 mPageLoadFinishedSignal(),
118 mPageLoadErrorSignal(),
119 mVideoHoleEnabled( true ),
120 mWebViewArea ( 0, 0, mWebViewSize.width, mWebViewSize.height )
122 mWebEngine = Dali::WebEngine::New();
124 // WebEngine is empty when it is not properly initialized.
127 mWebEngine.Create( mWebViewSize.width, mWebViewSize.height, argc, argv );
140 mWebEngine.Destroy();
144 Toolkit::WebView WebView::New()
146 WebView* impl = new WebView();
147 Toolkit::WebView handle = Toolkit::WebView( *impl );
153 Toolkit::WebView WebView::New( const std::string& locale, const std::string& timezoneId )
155 WebView* impl = new WebView( locale, timezoneId );
156 Toolkit::WebView handle = Toolkit::WebView( *impl );
162 Toolkit::WebView WebView::New( int argc, char** argv )
164 WebView* impl = new WebView( argc, argv );
165 Toolkit::WebView handle = Toolkit::WebView( *impl );
171 void WebView::OnInitialize()
175 self.SetProperty( Actor::Property::KEYBOARD_FOCUSABLE, true );
176 self.TouchedSignal().Connect( this, &WebView::OnTouchEvent );
178 mPositionUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_POSITION, StepCondition( 1.0f, 1.0f ) );
179 mSizeUpdateNotification = self.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 1.0f, 1.0f ) );
180 mScaleUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
181 mPositionUpdateNotification.NotifySignal().Connect( this, &WebView::UpdateDisplayArea );
182 mSizeUpdateNotification.NotifySignal().Connect( this, &WebView::UpdateDisplayArea );
183 mScaleUpdateNotification.NotifySignal().Connect( this, &WebView::UpdateDisplayArea );
187 mWebEngine.PageLoadStartedSignal().Connect( this, &WebView::OnPageLoadStarted );
188 mWebEngine.PageLoadFinishedSignal().Connect( this, &WebView::OnPageLoadFinished );
189 mWebEngine.PageLoadErrorSignal().Connect( this, &WebView::OnPageLoadError );
190 mWebEngine.ScrollEdgeReachedSignal().Connect( this, &WebView::OnScrollEdgeReached );
192 mWebContext = std::unique_ptr<Dali::Toolkit::WebContext>( new WebContext( mWebEngine.GetContext() ) );
193 mWebCookieManager = std::unique_ptr<Dali::Toolkit::WebCookieManager>( new WebCookieManager( mWebEngine.GetCookieManager() ) );
194 mWebSettings = std::unique_ptr<Dali::Toolkit::WebSettings>( new WebSettings( mWebEngine.GetSettings() ) );
195 mWebBackForwardList = std::unique_ptr<Dali::Toolkit::WebBackForwardList>( new WebBackForwardList( mWebEngine.GetBackForwardList() ) );
199 Dali::Toolkit::WebSettings* WebView::GetSettings() const
201 return mWebSettings.get();
204 Dali::Toolkit::WebContext* WebView::GetContext() const
206 return mWebContext.get();
209 Dali::Toolkit::WebCookieManager* WebView::GetCookieManager() const
211 return mWebCookieManager.get();
214 Dali::Toolkit::WebBackForwardList* WebView::GetBackForwardList() const
216 return mWebBackForwardList.get();
219 Dali::Toolkit::ImageView& WebView::GetFavicon()
223 Dali::PixelData pixelData = mWebEngine.GetFavicon();
224 std::string url = Dali::Toolkit::Image::GenerateUrl( pixelData );
225 mFaviconView = Dali::Toolkit::ImageView::New( url );
226 mFaviconView.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
227 mFaviconView.SetProperty( Dali::Actor::Property::SIZE, Vector2( pixelData.GetWidth(), pixelData.GetHeight() ) );
232 void WebView::LoadUrl( const std::string& url )
237 Texture texture = Dali::Texture::New( *mWebEngine.GetNativeImageSource() );
238 const std::string nativeImageUrl = Dali::Toolkit::TextureManager::AddTexture( texture );
239 mVisual = Toolkit::VisualFactory::Get().CreateVisual(
240 { { Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE } ,
241 { Toolkit::ImageVisual::Property::URL, nativeImageUrl } } );
245 // Clean up previously registered visual and add new one.
246 DevelControl::RegisterVisual( *this, Toolkit::WebView::Property::URL, mVisual );
247 mWebEngine.LoadUrl( url );
250 if ( mVideoHoleEnabled )
252 EnableBlendMode( false );
257 void WebView::LoadHtmlString( const std::string& htmlString )
261 Texture texture = Dali::Texture::New( *mWebEngine.GetNativeImageSource() );
262 const std::string nativeImageUrl = Dali::Toolkit::TextureManager::AddTexture( texture );
263 mVisual = Toolkit::VisualFactory::Get().CreateVisual(
264 { { Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE } ,
265 { Toolkit::ImageVisual::Property::URL, nativeImageUrl } } );
269 DevelControl::RegisterVisual( *this, Toolkit::WebView::Property::URL, mVisual );
270 mWebEngine.LoadHtmlString( htmlString );
273 if ( mVideoHoleEnabled )
275 EnableBlendMode( false );
280 void WebView::Reload()
288 void WebView::StopLoading()
292 mWebEngine.StopLoading();
296 void WebView::Suspend()
300 mWebEngine.Suspend();
304 void WebView::Resume()
312 void WebView::ScrollBy( int deltaX, int deltaY )
316 mWebEngine.ScrollBy( deltaX, deltaY );
320 bool WebView::CanGoForward()
322 return mWebEngine ? mWebEngine.CanGoForward() : false;
325 void WebView::GoForward()
329 mWebEngine.GoForward();
333 bool WebView::CanGoBack()
335 return mWebEngine ? mWebEngine.CanGoBack() : false;
338 void WebView::GoBack()
346 void WebView::EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
350 mWebEngine.EvaluateJavaScript( script, resultHandler );
354 void WebView::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void( const std::string& ) > handler )
358 mWebEngine.AddJavaScriptMessageHandler( exposedObjectName, handler );
362 void WebView::ClearAllTilesResources()
366 mWebEngine.ClearAllTilesResources();
370 void WebView::ClearHistory()
374 mWebEngine.ClearHistory();
378 void WebView::UpdateDisplayArea( Dali::PropertyNotification& /*source*/ )
383 Actor self( Self() );
385 bool positionUsesAnchorPoint = self.GetProperty< bool >( Actor::Property::POSITION_USES_ANCHOR_POINT );
386 Vector3 actorSize = self.GetCurrentProperty< Vector3 >( Actor::Property::SIZE ) * self.GetCurrentProperty< Vector3 >( Actor::Property::SCALE );
387 Vector3 anchorPointOffSet = actorSize * ( positionUsesAnchorPoint ? self.GetCurrentProperty< Vector3 >( Actor::Property::ANCHOR_POINT ) : AnchorPoint::TOP_LEFT );
388 Vector2 screenPosition = self.GetProperty< Vector2 >( Actor::Property::SCREEN_POSITION );
390 Dali::Rect< int > displayArea;
391 displayArea.x = screenPosition.x - anchorPointOffSet.x;
392 displayArea.y = screenPosition.y - anchorPointOffSet.y;
393 displayArea.width = actorSize.x;
394 displayArea.height = actorSize.y;
396 Size displaySize = Size( displayArea.width, displayArea.height );
397 if ( mWebViewSize != displaySize )
399 mWebViewSize = displaySize;
402 if (mWebViewArea != displayArea )
404 mWebViewArea = displayArea;
405 mWebEngine.UpdateDisplayArea( mWebViewArea );
409 void WebView::EnableVideoHole( bool enabled )
411 mVideoHoleEnabled = enabled;
413 EnableBlendMode( !mVideoHoleEnabled );
417 mWebEngine.EnableVideoHole( mVideoHoleEnabled );
421 void WebView::EnableBlendMode( bool blendEnabled )
424 for (uint32_t i = 0; i < self.GetRendererCount(); i++)
426 Dali::Renderer render = self.GetRendererAt( i );
427 render.SetProperty( Renderer::Property::BLEND_MODE, blendEnabled ? BlendMode::ON : BlendMode::OFF );
431 Dali::Toolkit::WebView::WebViewPageLoadSignalType& WebView::PageLoadStartedSignal()
433 return mPageLoadStartedSignal;
436 Dali::Toolkit::WebView::WebViewPageLoadSignalType& WebView::PageLoadFinishedSignal()
438 return mPageLoadFinishedSignal;
441 Dali::Toolkit::WebView::WebViewPageLoadErrorSignalType& WebView::PageLoadErrorSignal()
443 return mPageLoadErrorSignal;
446 Dali::Toolkit::WebView::WebViewScrollEdgeReachedSignalType& WebView::ScrollEdgeReachedSignal()
448 return mScrollEdgeReachedSignal;
451 void WebView::OnPageLoadStarted( const std::string& url )
453 if( !mPageLoadStartedSignal.Empty() )
455 Dali::Toolkit::WebView handle( GetOwner() );
456 mPageLoadStartedSignal.Emit( handle, url );
460 void WebView::OnPageLoadFinished( const std::string& url )
462 if( !mPageLoadFinishedSignal.Empty() )
464 Dali::Toolkit::WebView handle( GetOwner() );
465 mPageLoadFinishedSignal.Emit( handle, url );
469 void WebView::OnPageLoadError( const std::string& url, int errorCode )
471 if( !mPageLoadErrorSignal.Empty() )
473 Dali::Toolkit::WebView handle( GetOwner() );
474 mPageLoadErrorSignal.Emit( handle, url, static_cast< Toolkit::WebView::LoadErrorCode >( errorCode ) );
478 void WebView::OnScrollEdgeReached( Dali::WebEnginePlugin::ScrollEdge edge )
480 if( !mScrollEdgeReachedSignal.Empty() )
482 Dali::Toolkit::WebView handle( GetOwner() );
483 mScrollEdgeReachedSignal.Emit( handle, edge );
487 bool WebView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
489 Dali::BaseHandle handle( object );
491 bool connected = false;
492 Toolkit::WebView webView = Toolkit::WebView::DownCast( handle );
494 if( 0 == strcmp( signalName.c_str(), PAGE_LOAD_STARTED_SIGNAL ) )
496 webView.PageLoadStartedSignal().Connect( tracker, functor );
499 else if( 0 == strcmp( signalName.c_str(), PAGE_LOAD_FINISHED_SIGNAL ) )
501 webView.PageLoadFinishedSignal().Connect( tracker, functor );
504 else if( 0 == strcmp( signalName.c_str(), PAGE_LOAD_ERROR_SIGNAL ) )
506 webView.PageLoadErrorSignal().Connect( tracker, functor );
509 else if( 0 == strcmp( signalName.c_str(), SCROLL_EDGE_REACHED_SIGNAL ) )
511 webView.ScrollEdgeReachedSignal().Connect( tracker, functor );
518 Vector3 WebView::GetNaturalSize()
522 Vector2 rendererNaturalSize;
523 mVisual.GetNaturalSize( rendererNaturalSize );
524 return Vector3( rendererNaturalSize );
527 return Vector3( mWebViewSize );
530 void WebView::OnSceneConnection( int depth )
532 Control::OnSceneConnection( depth );
534 EnableBlendMode( !mVideoHoleEnabled );
537 void WebView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
539 Toolkit::WebView webView = Toolkit::WebView::DownCast( Dali::BaseHandle( object ) );
543 WebView& impl = GetImpl( webView );
546 case Toolkit::WebView::Property::URL:
549 if( value.Get( url ) )
555 case Toolkit::WebView::Property::USER_AGENT:
558 if( value.Get( input ) )
560 impl.SetUserAgent( input );
564 case Toolkit::WebView::Property::SCROLL_POSITION:
567 if ( value.Get( input ) )
569 impl.SetScrollPosition( input.x, input.y );
573 case Toolkit::WebView::Property::VIDEO_HOLE_ENABLED:
576 if( value.Get( input ) )
578 impl.EnableVideoHole( input );
586 Property::Value WebView::GetProperty( BaseObject* object, Property::Index propertyIndex )
588 Property::Value value;
590 Toolkit::WebView webView = Toolkit::WebView::DownCast( Dali::BaseHandle( object ) );
594 WebView& impl = GetImpl( webView );
595 switch( propertyIndex )
597 case Toolkit::WebView::Property::URL:
602 case Toolkit::WebView::Property::USER_AGENT:
604 value = impl.GetUserAgent();
607 case Toolkit::WebView::Property::SCROLL_POSITION:
609 value = impl.GetScrollPosition();
612 case Toolkit::WebView::Property::SCROLL_SIZE:
614 value = impl.GetScrollSize();
617 case Toolkit::WebView::Property::CONTENT_SIZE:
619 value = impl.GetContentSize();
622 case Toolkit::WebView::Property::TITLE:
624 value = impl.GetTitle();
627 case Toolkit::WebView::Property::VIDEO_HOLE_ENABLED:
629 value = impl.mVideoHoleEnabled;
640 bool WebView::OnTouchEvent( Actor actor, const Dali::TouchEvent& touch )
646 result = mWebEngine.SendTouchEvent( touch );
651 bool WebView::OnKeyEvent( const Dali::KeyEvent& event )
657 result = mWebEngine.SendKeyEvent( event );
662 void WebView::OnKeyInputFocusGained()
666 mWebEngine.SetFocus( true );
669 EmitKeyInputFocusSignal( true ); // Calls back into the Control hence done last.
672 void WebView::OnKeyInputFocusLost()
676 mWebEngine.SetFocus( false );
679 EmitKeyInputFocusSignal( false ); // Calls back into the Control hence done last.
682 void WebView::SetScrollPosition( int x, int y )
686 mWebEngine.SetScrollPosition( x, y );
690 Dali::Vector2 WebView::GetScrollPosition() const
692 return mWebEngine ? mWebEngine.GetScrollPosition() : Dali::Vector2::ZERO;
695 Dali::Vector2 WebView::GetScrollSize() const
697 return mWebEngine ? mWebEngine.GetScrollSize() : Dali::Vector2::ZERO;
700 Dali::Vector2 WebView::GetContentSize() const
702 return mWebEngine ? mWebEngine.GetContentSize() : Dali::Vector2::ZERO;
705 std::string WebView::GetTitle() const
707 return mWebEngine ? mWebEngine.GetTitle() : kEmptyString;
710 const std::string& WebView::GetUserAgent() const
712 return mWebEngine ? mWebEngine.GetUserAgent() : kEmptyString;
715 void WebView::SetUserAgent( const std::string& userAgent )
719 mWebEngine.SetUserAgent( userAgent );
723 #undef GET_ENUM_STRING
724 #undef GET_ENUM_VALUE
726 } // namespace Internal
728 } // namespace Toolkit