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 const char* DEFAULT_SAMPLER_TYPENAME = "sampler2D";
83 const char* FRAGMENT_SHADER_TEXTURE = DALI_COMPOSE_SHADER(
84 varying mediump vec2 vTexCoord;\n
85 uniform sampler2D sTexture;\n
86 uniform lowp vec4 uColor;\n
87 uniform lowp vec3 mixColor;\n
88 uniform lowp float preMultipliedAlpha;\n
92 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * vec4( mixColor, 1.0 );\n
96 Dali::Toolkit::Visual::Base CreateNativeImageVisual( NativeImageInterfacePtr nativeImageInterface )
98 std::string fragmentShader;
100 const char* fragmentPrefix = nativeImageInterface->GetCustomFragmentPrefix();
103 fragmentShader = fragmentPrefix;
104 fragmentShader += FRAGMENT_SHADER_TEXTURE;
108 fragmentShader = FRAGMENT_SHADER_TEXTURE;
111 const char* customSamplerTypename = nativeImageInterface->GetCustomSamplerTypename();
112 if( customSamplerTypename )
114 fragmentShader.replace( fragmentShader.find( DEFAULT_SAMPLER_TYPENAME ), strlen( DEFAULT_SAMPLER_TYPENAME ), customSamplerTypename );
117 Texture texture = Dali::Texture::New( *nativeImageInterface );
118 const std::string nativeImageUrl = Dali::Toolkit::TextureManager::AddTexture( texture );
120 return Toolkit::VisualFactory::Get().CreateVisual(
121 { { Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE } ,
122 { Toolkit::Visual::Property::SHADER, { { Toolkit::Visual::Shader::Property::FRAGMENT_SHADER, fragmentShader } } },
123 { Toolkit::ImageVisual::Property::URL, nativeImageUrl } } );
126 } // anonymous namepsace
128 #define GET_ENUM_STRING( structName, inputExp ) \
129 Scripting::GetLinearEnumerationName< Toolkit::WebView::structName::Type >( static_cast< Toolkit::WebView::structName::Type >( inputExp ), structName##_TABLE, structName##_TABLE_COUNT )
131 #define GET_ENUM_VALUE( structName, inputExp, outputExp ) \
132 Scripting::GetEnumerationProperty< Toolkit::WebView::structName::Type >( inputExp, structName##_TABLE, structName##_TABLE_COUNT, outputExp )
134 WebView::WebView( const std::string& locale, const std::string& timezoneId )
135 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
138 mWebViewSize( Stage::GetCurrent().GetSize() ),
140 mPageLoadStartedSignal(),
141 mPageLoadFinishedSignal(),
142 mPageLoadErrorSignal(),
143 mVideoHoleEnabled( true ),
144 mWebViewArea ( 0, 0, mWebViewSize.width, mWebViewSize.height )
146 mWebEngine = Dali::WebEngine::New();
148 // WebEngine is empty when it is not properly initialized.
151 mWebEngine.Create( mWebViewSize.width, mWebViewSize.height, locale, timezoneId );
155 WebView::WebView( int argc, char** argv )
156 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
159 mWebViewSize( Stage::GetCurrent().GetSize() ),
161 mPageLoadStartedSignal(),
162 mPageLoadFinishedSignal(),
163 mPageLoadErrorSignal(),
164 mVideoHoleEnabled( true ),
165 mWebViewArea ( 0, 0, mWebViewSize.width, mWebViewSize.height )
167 mWebEngine = Dali::WebEngine::New();
169 // WebEngine is empty when it is not properly initialized.
172 mWebEngine.Create( mWebViewSize.width, mWebViewSize.height, argc, argv );
185 mWebEngine.Destroy();
189 Toolkit::WebView WebView::New()
191 WebView* impl = new WebView();
192 Toolkit::WebView handle = Toolkit::WebView( *impl );
198 Toolkit::WebView WebView::New( const std::string& locale, const std::string& timezoneId )
200 WebView* impl = new WebView( locale, timezoneId );
201 Toolkit::WebView handle = Toolkit::WebView( *impl );
207 Toolkit::WebView WebView::New( int argc, char** argv )
209 WebView* impl = new WebView( argc, argv );
210 Toolkit::WebView handle = Toolkit::WebView( *impl );
216 void WebView::OnInitialize()
220 self.SetProperty( Actor::Property::KEYBOARD_FOCUSABLE, true );
221 self.TouchedSignal().Connect( this, &WebView::OnTouchEvent );
223 mPositionUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_POSITION, StepCondition( 1.0f, 1.0f ) );
224 mSizeUpdateNotification = self.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 1.0f, 1.0f ) );
225 mScaleUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
226 mPositionUpdateNotification.NotifySignal().Connect( this, &WebView::UpdateDisplayArea );
227 mSizeUpdateNotification.NotifySignal().Connect( this, &WebView::UpdateDisplayArea );
228 mScaleUpdateNotification.NotifySignal().Connect( this, &WebView::UpdateDisplayArea );
232 mWebEngine.PageLoadStartedSignal().Connect( this, &WebView::OnPageLoadStarted );
233 mWebEngine.PageLoadFinishedSignal().Connect( this, &WebView::OnPageLoadFinished );
234 mWebEngine.PageLoadErrorSignal().Connect( this, &WebView::OnPageLoadError );
235 mWebEngine.ScrollEdgeReachedSignal().Connect( this, &WebView::OnScrollEdgeReached );
237 mWebContext = std::unique_ptr<Dali::Toolkit::WebContext>( new WebContext( mWebEngine.GetContext() ) );
238 mWebCookieManager = std::unique_ptr<Dali::Toolkit::WebCookieManager>( new WebCookieManager( mWebEngine.GetCookieManager() ) );
239 mWebSettings = std::unique_ptr<Dali::Toolkit::WebSettings>( new WebSettings( mWebEngine.GetSettings() ) );
240 mWebBackForwardList = std::unique_ptr<Dali::Toolkit::WebBackForwardList>( new WebBackForwardList( mWebEngine.GetBackForwardList() ) );
244 Dali::Toolkit::WebSettings* WebView::GetSettings() const
246 return mWebSettings.get();
249 Dali::Toolkit::WebContext* WebView::GetContext() const
251 return mWebContext.get();
254 Dali::Toolkit::WebCookieManager* WebView::GetCookieManager() const
256 return mWebCookieManager.get();
259 Dali::Toolkit::WebBackForwardList* WebView::GetBackForwardList() const
261 return mWebBackForwardList.get();
264 Dali::Toolkit::ImageView& WebView::GetFavicon()
268 Dali::PixelData pixelData = mWebEngine.GetFavicon();
269 std::string url = Dali::Toolkit::Image::GenerateUrl( pixelData );
270 mFaviconView = Dali::Toolkit::ImageView::New( url );
271 mFaviconView.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
272 mFaviconView.SetProperty( Dali::Actor::Property::SIZE, Vector2( pixelData.GetWidth(), pixelData.GetHeight() ) );
277 void WebView::LoadUrl( const std::string& url )
282 mVisual = CreateNativeImageVisual( mWebEngine.GetNativeImageSource() );
286 // Clean up previously registered visual and add new one.
287 DevelControl::RegisterVisual( *this, Toolkit::WebView::Property::URL, mVisual );
288 mWebEngine.LoadUrl( url );
291 if ( mVideoHoleEnabled )
293 EnableBlendMode( false );
298 void WebView::LoadHtmlString( const std::string& htmlString )
302 mVisual = CreateNativeImageVisual( mWebEngine.GetNativeImageSource() );
306 DevelControl::RegisterVisual( *this, Toolkit::WebView::Property::URL, mVisual );
307 mWebEngine.LoadHtmlString( htmlString );
310 if ( mVideoHoleEnabled )
312 EnableBlendMode( false );
317 void WebView::Reload()
325 void WebView::StopLoading()
329 mWebEngine.StopLoading();
333 void WebView::Suspend()
337 mWebEngine.Suspend();
341 void WebView::Resume()
349 void WebView::ScrollBy( int deltaX, int deltaY )
353 mWebEngine.ScrollBy( deltaX, deltaY );
357 bool WebView::CanGoForward()
359 return mWebEngine ? mWebEngine.CanGoForward() : false;
362 void WebView::GoForward()
366 mWebEngine.GoForward();
370 bool WebView::CanGoBack()
372 return mWebEngine ? mWebEngine.CanGoBack() : false;
375 void WebView::GoBack()
383 void WebView::EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
387 mWebEngine.EvaluateJavaScript( script, resultHandler );
391 void WebView::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void( const std::string& ) > handler )
395 mWebEngine.AddJavaScriptMessageHandler( exposedObjectName, handler );
399 void WebView::ClearAllTilesResources()
403 mWebEngine.ClearAllTilesResources();
407 void WebView::ClearHistory()
411 mWebEngine.ClearHistory();
415 void WebView::UpdateDisplayArea( Dali::PropertyNotification& /*source*/ )
420 Actor self( Self() );
422 bool positionUsesAnchorPoint = self.GetProperty< bool >( Actor::Property::POSITION_USES_ANCHOR_POINT );
423 Vector3 actorSize = self.GetCurrentProperty< Vector3 >( Actor::Property::SIZE ) * self.GetCurrentProperty< Vector3 >( Actor::Property::SCALE );
424 Vector3 anchorPointOffSet = actorSize * ( positionUsesAnchorPoint ? self.GetCurrentProperty< Vector3 >( Actor::Property::ANCHOR_POINT ) : AnchorPoint::TOP_LEFT );
425 Vector2 screenPosition = self.GetProperty< Vector2 >( Actor::Property::SCREEN_POSITION );
427 Dali::Rect< int > displayArea;
428 displayArea.x = screenPosition.x - anchorPointOffSet.x;
429 displayArea.y = screenPosition.y - anchorPointOffSet.y;
430 displayArea.width = actorSize.x;
431 displayArea.height = actorSize.y;
433 Size displaySize = Size( displayArea.width, displayArea.height );
434 if ( mWebViewSize != displaySize )
436 mWebViewSize = displaySize;
439 if (mWebViewArea != displayArea )
441 mWebViewArea = displayArea;
442 mWebEngine.UpdateDisplayArea( mWebViewArea );
446 void WebView::EnableVideoHole( bool enabled )
448 mVideoHoleEnabled = enabled;
450 EnableBlendMode( !mVideoHoleEnabled );
454 mWebEngine.EnableVideoHole( mVideoHoleEnabled );
458 void WebView::EnableBlendMode( bool blendEnabled )
461 for (uint32_t i = 0; i < self.GetRendererCount(); i++)
463 Dali::Renderer render = self.GetRendererAt( i );
464 render.SetProperty( Renderer::Property::BLEND_MODE, blendEnabled ? BlendMode::ON : BlendMode::OFF );
468 Dali::Toolkit::WebView::WebViewPageLoadSignalType& WebView::PageLoadStartedSignal()
470 return mPageLoadStartedSignal;
473 Dali::Toolkit::WebView::WebViewPageLoadSignalType& WebView::PageLoadFinishedSignal()
475 return mPageLoadFinishedSignal;
478 Dali::Toolkit::WebView::WebViewPageLoadErrorSignalType& WebView::PageLoadErrorSignal()
480 return mPageLoadErrorSignal;
483 Dali::Toolkit::WebView::WebViewScrollEdgeReachedSignalType& WebView::ScrollEdgeReachedSignal()
485 return mScrollEdgeReachedSignal;
488 void WebView::OnPageLoadStarted( const std::string& url )
490 if( !mPageLoadStartedSignal.Empty() )
492 Dali::Toolkit::WebView handle( GetOwner() );
493 mPageLoadStartedSignal.Emit( handle, url );
497 void WebView::OnPageLoadFinished( const std::string& url )
499 if( !mPageLoadFinishedSignal.Empty() )
501 Dali::Toolkit::WebView handle( GetOwner() );
502 mPageLoadFinishedSignal.Emit( handle, url );
506 void WebView::OnPageLoadError( const std::string& url, int errorCode )
508 if( !mPageLoadErrorSignal.Empty() )
510 Dali::Toolkit::WebView handle( GetOwner() );
511 mPageLoadErrorSignal.Emit( handle, url, static_cast< Toolkit::WebView::LoadErrorCode >( errorCode ) );
515 void WebView::OnScrollEdgeReached( Dali::WebEnginePlugin::ScrollEdge edge )
517 if( !mScrollEdgeReachedSignal.Empty() )
519 Dali::Toolkit::WebView handle( GetOwner() );
520 mScrollEdgeReachedSignal.Emit( handle, edge );
524 bool WebView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
526 Dali::BaseHandle handle( object );
528 bool connected = false;
529 Toolkit::WebView webView = Toolkit::WebView::DownCast( handle );
531 if( 0 == strcmp( signalName.c_str(), PAGE_LOAD_STARTED_SIGNAL ) )
533 webView.PageLoadStartedSignal().Connect( tracker, functor );
536 else if( 0 == strcmp( signalName.c_str(), PAGE_LOAD_FINISHED_SIGNAL ) )
538 webView.PageLoadFinishedSignal().Connect( tracker, functor );
541 else if( 0 == strcmp( signalName.c_str(), PAGE_LOAD_ERROR_SIGNAL ) )
543 webView.PageLoadErrorSignal().Connect( tracker, functor );
546 else if( 0 == strcmp( signalName.c_str(), SCROLL_EDGE_REACHED_SIGNAL ) )
548 webView.ScrollEdgeReachedSignal().Connect( tracker, functor );
555 Vector3 WebView::GetNaturalSize()
559 Vector2 rendererNaturalSize;
560 mVisual.GetNaturalSize( rendererNaturalSize );
561 return Vector3( rendererNaturalSize );
564 return Vector3( mWebViewSize );
567 void WebView::OnSceneConnection( int depth )
569 Control::OnSceneConnection( depth );
571 EnableBlendMode( !mVideoHoleEnabled );
574 void WebView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
576 Toolkit::WebView webView = Toolkit::WebView::DownCast( Dali::BaseHandle( object ) );
580 WebView& impl = GetImpl( webView );
583 case Toolkit::WebView::Property::URL:
586 if( value.Get( url ) )
592 case Toolkit::WebView::Property::USER_AGENT:
595 if( value.Get( input ) )
597 impl.SetUserAgent( input );
601 case Toolkit::WebView::Property::SCROLL_POSITION:
604 if ( value.Get( input ) )
606 impl.SetScrollPosition( input.x, input.y );
610 case Toolkit::WebView::Property::VIDEO_HOLE_ENABLED:
613 if( value.Get( input ) )
615 impl.EnableVideoHole( input );
623 Property::Value WebView::GetProperty( BaseObject* object, Property::Index propertyIndex )
625 Property::Value value;
627 Toolkit::WebView webView = Toolkit::WebView::DownCast( Dali::BaseHandle( object ) );
631 WebView& impl = GetImpl( webView );
632 switch( propertyIndex )
634 case Toolkit::WebView::Property::URL:
639 case Toolkit::WebView::Property::USER_AGENT:
641 value = impl.GetUserAgent();
644 case Toolkit::WebView::Property::SCROLL_POSITION:
647 impl.GetScrollPosition( x, y );
648 value = Vector2( x, y );
651 case Toolkit::WebView::Property::SCROLL_SIZE:
654 impl.GetScrollSize( width, height );
655 value = Vector2( width, height );
658 case Toolkit::WebView::Property::CONTENT_SIZE:
661 impl.GetContentSize( width, height );
662 value = Vector2( width, height );
665 case Toolkit::WebView::Property::TITLE:
667 value = impl.GetTitle();
670 case Toolkit::WebView::Property::VIDEO_HOLE_ENABLED:
672 value = impl.mVideoHoleEnabled;
683 bool WebView::OnTouchEvent( Actor actor, const Dali::TouchEvent& touch )
689 result = mWebEngine.SendTouchEvent( touch );
694 bool WebView::OnKeyEvent( const Dali::KeyEvent& event )
700 result = mWebEngine.SendKeyEvent( event );
705 void WebView::OnKeyInputFocusGained()
709 mWebEngine.SetFocus( true );
712 EmitKeyInputFocusSignal( true ); // Calls back into the Control hence done last.
715 void WebView::OnKeyInputFocusLost()
719 mWebEngine.SetFocus( false );
722 EmitKeyInputFocusSignal( false ); // Calls back into the Control hence done last.
725 void WebView::SetScrollPosition( int x, int y )
729 mWebEngine.SetScrollPosition( x, y );
733 void WebView::GetScrollPosition( int& x, int& y ) const
737 mWebEngine.GetScrollPosition( x, y );
741 void WebView::GetScrollSize( int& width, int& height ) const
745 mWebEngine.GetScrollSize( width, height );
749 void WebView::GetContentSize( int& width, int& height ) const
753 mWebEngine.GetContentSize( width, height );
757 std::string WebView::GetTitle() const
759 return mWebEngine ? mWebEngine.GetTitle() : kEmptyString;
762 const std::string& WebView::GetUserAgent() const
764 return mWebEngine ? mWebEngine.GetUserAgent() : kEmptyString;
767 void WebView::SetUserAgent( const std::string& userAgent )
771 mWebEngine.SetUserAgent( userAgent );
775 #undef GET_ENUM_STRING
776 #undef GET_ENUM_VALUE
778 } // namespace Internal
780 } // namespace Toolkit