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() const
266 Dali::Toolkit::ImageView faviconView;
269 Dali::PixelData pixelData = mWebEngine.GetFavicon();
272 std::string url = Dali::Toolkit::Image::GenerateUrl(pixelData);
273 faviconView = Dali::Toolkit::ImageView::New(url);
274 faviconView.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
280 void WebView::LoadUrl( const std::string& url )
285 mVisual = CreateNativeImageVisual( mWebEngine.GetNativeImageSource() );
289 // Clean up previously registered visual and add new one.
290 DevelControl::RegisterVisual( *this, Toolkit::WebView::Property::URL, mVisual );
291 mWebEngine.LoadUrl( url );
294 if ( mVideoHoleEnabled )
296 EnableBlendMode( false );
301 void WebView::LoadHtmlString( const std::string& htmlString )
305 mVisual = CreateNativeImageVisual( mWebEngine.GetNativeImageSource() );
309 DevelControl::RegisterVisual( *this, Toolkit::WebView::Property::URL, mVisual );
310 mWebEngine.LoadHtmlString( htmlString );
313 if ( mVideoHoleEnabled )
315 EnableBlendMode( false );
320 void WebView::Reload()
328 void WebView::StopLoading()
332 mWebEngine.StopLoading();
336 void WebView::Suspend()
340 mWebEngine.Suspend();
344 void WebView::Resume()
352 void WebView::ScrollBy( int deltaX, int deltaY )
356 mWebEngine.ScrollBy( deltaX, deltaY );
360 bool WebView::CanGoForward()
362 return mWebEngine ? mWebEngine.CanGoForward() : false;
365 void WebView::GoForward()
369 mWebEngine.GoForward();
373 bool WebView::CanGoBack()
375 return mWebEngine ? mWebEngine.CanGoBack() : false;
378 void WebView::GoBack()
386 void WebView::EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
390 mWebEngine.EvaluateJavaScript( script, resultHandler );
394 void WebView::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void( const std::string& ) > handler )
398 mWebEngine.AddJavaScriptMessageHandler( exposedObjectName, handler );
402 void WebView::ClearAllTilesResources()
406 mWebEngine.ClearAllTilesResources();
410 void WebView::ClearHistory()
414 mWebEngine.ClearHistory();
418 void WebView::UpdateDisplayArea( Dali::PropertyNotification& /*source*/ )
423 Actor self( Self() );
425 bool positionUsesAnchorPoint = self.GetProperty< bool >( Actor::Property::POSITION_USES_ANCHOR_POINT );
426 Vector3 actorSize = self.GetCurrentProperty< Vector3 >( Actor::Property::SIZE ) * self.GetCurrentProperty< Vector3 >( Actor::Property::SCALE );
427 Vector3 anchorPointOffSet = actorSize * ( positionUsesAnchorPoint ? self.GetCurrentProperty< Vector3 >( Actor::Property::ANCHOR_POINT ) : AnchorPoint::TOP_LEFT );
428 Vector2 screenPosition = self.GetProperty< Vector2 >( Actor::Property::SCREEN_POSITION );
430 Dali::Rect< int > displayArea;
431 displayArea.x = screenPosition.x - anchorPointOffSet.x;
432 displayArea.y = screenPosition.y - anchorPointOffSet.y;
433 displayArea.width = actorSize.x;
434 displayArea.height = actorSize.y;
436 Size displaySize = Size( displayArea.width, displayArea.height );
437 if ( mWebViewSize != displaySize )
439 mWebViewSize = displaySize;
442 if (mWebViewArea != displayArea )
444 mWebViewArea = displayArea;
445 mWebEngine.UpdateDisplayArea( mWebViewArea );
449 void WebView::EnableVideoHole( bool enabled )
451 mVideoHoleEnabled = enabled;
453 EnableBlendMode( !mVideoHoleEnabled );
457 mWebEngine.EnableVideoHole( mVideoHoleEnabled );
461 void WebView::EnableBlendMode( bool blendEnabled )
464 for (uint32_t i = 0; i < self.GetRendererCount(); i++)
466 Dali::Renderer render = self.GetRendererAt( i );
467 render.SetProperty( Renderer::Property::BLEND_MODE, blendEnabled ? BlendMode::ON : BlendMode::OFF );
471 Dali::Toolkit::WebView::WebViewPageLoadSignalType& WebView::PageLoadStartedSignal()
473 return mPageLoadStartedSignal;
476 Dali::Toolkit::WebView::WebViewPageLoadSignalType& WebView::PageLoadFinishedSignal()
478 return mPageLoadFinishedSignal;
481 Dali::Toolkit::WebView::WebViewPageLoadErrorSignalType& WebView::PageLoadErrorSignal()
483 return mPageLoadErrorSignal;
486 Dali::Toolkit::WebView::WebViewScrollEdgeReachedSignalType& WebView::ScrollEdgeReachedSignal()
488 return mScrollEdgeReachedSignal;
491 void WebView::OnPageLoadStarted( const std::string& url )
493 if( !mPageLoadStartedSignal.Empty() )
495 Dali::Toolkit::WebView handle( GetOwner() );
496 mPageLoadStartedSignal.Emit( handle, url );
500 void WebView::OnPageLoadFinished( const std::string& url )
502 if( !mPageLoadFinishedSignal.Empty() )
504 Dali::Toolkit::WebView handle( GetOwner() );
505 mPageLoadFinishedSignal.Emit( handle, url );
509 void WebView::OnPageLoadError( const std::string& url, int errorCode )
511 if( !mPageLoadErrorSignal.Empty() )
513 Dali::Toolkit::WebView handle( GetOwner() );
514 mPageLoadErrorSignal.Emit( handle, url, static_cast< Toolkit::WebView::LoadErrorCode >( errorCode ) );
518 void WebView::OnScrollEdgeReached( Dali::WebEnginePlugin::ScrollEdge edge )
520 if( !mScrollEdgeReachedSignal.Empty() )
522 Dali::Toolkit::WebView handle( GetOwner() );
523 mScrollEdgeReachedSignal.Emit( handle, edge );
527 bool WebView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
529 Dali::BaseHandle handle( object );
531 bool connected = false;
532 Toolkit::WebView webView = Toolkit::WebView::DownCast( handle );
534 if( 0 == strcmp( signalName.c_str(), PAGE_LOAD_STARTED_SIGNAL ) )
536 webView.PageLoadStartedSignal().Connect( tracker, functor );
539 else if( 0 == strcmp( signalName.c_str(), PAGE_LOAD_FINISHED_SIGNAL ) )
541 webView.PageLoadFinishedSignal().Connect( tracker, functor );
544 else if( 0 == strcmp( signalName.c_str(), PAGE_LOAD_ERROR_SIGNAL ) )
546 webView.PageLoadErrorSignal().Connect( tracker, functor );
549 else if( 0 == strcmp( signalName.c_str(), SCROLL_EDGE_REACHED_SIGNAL ) )
551 webView.ScrollEdgeReachedSignal().Connect( tracker, functor );
558 Vector3 WebView::GetNaturalSize()
562 Vector2 rendererNaturalSize;
563 mVisual.GetNaturalSize( rendererNaturalSize );
564 return Vector3( rendererNaturalSize );
567 return Vector3( mWebViewSize );
570 void WebView::OnSceneConnection( int depth )
572 Control::OnSceneConnection( depth );
574 EnableBlendMode( !mVideoHoleEnabled );
577 void WebView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
579 Toolkit::WebView webView = Toolkit::WebView::DownCast( Dali::BaseHandle( object ) );
583 WebView& impl = GetImpl( webView );
586 case Toolkit::WebView::Property::URL:
589 if( value.Get( url ) )
595 case Toolkit::WebView::Property::USER_AGENT:
598 if( value.Get( input ) )
600 impl.SetUserAgent( input );
604 case Toolkit::WebView::Property::SCROLL_POSITION:
607 if ( value.Get( input ) )
609 impl.SetScrollPosition( input.x, input.y );
613 case Toolkit::WebView::Property::VIDEO_HOLE_ENABLED:
616 if( value.Get( input ) )
618 impl.EnableVideoHole( input );
626 Property::Value WebView::GetProperty( BaseObject* object, Property::Index propertyIndex )
628 Property::Value value;
630 Toolkit::WebView webView = Toolkit::WebView::DownCast( Dali::BaseHandle( object ) );
634 WebView& impl = GetImpl( webView );
635 switch( propertyIndex )
637 case Toolkit::WebView::Property::URL:
642 case Toolkit::WebView::Property::USER_AGENT:
644 value = impl.GetUserAgent();
647 case Toolkit::WebView::Property::SCROLL_POSITION:
650 impl.GetScrollPosition( x, y );
651 value = Vector2( x, y );
654 case Toolkit::WebView::Property::SCROLL_SIZE:
657 impl.GetScrollSize( width, height );
658 value = Vector2( width, height );
661 case Toolkit::WebView::Property::CONTENT_SIZE:
664 impl.GetContentSize( width, height );
665 value = Vector2( width, height );
668 case Toolkit::WebView::Property::TITLE:
670 value = impl.GetTitle();
673 case Toolkit::WebView::Property::VIDEO_HOLE_ENABLED:
675 value = impl.mVideoHoleEnabled;
686 bool WebView::OnTouchEvent( Actor actor, const Dali::TouchEvent& touch )
692 result = mWebEngine.SendTouchEvent( touch );
697 bool WebView::OnKeyEvent( const Dali::KeyEvent& event )
703 result = mWebEngine.SendKeyEvent( event );
708 void WebView::OnKeyInputFocusGained()
712 mWebEngine.SetFocus( true );
715 EmitKeyInputFocusSignal( true ); // Calls back into the Control hence done last.
718 void WebView::OnKeyInputFocusLost()
722 mWebEngine.SetFocus( false );
725 EmitKeyInputFocusSignal( false ); // Calls back into the Control hence done last.
728 void WebView::SetScrollPosition( int x, int y )
732 mWebEngine.SetScrollPosition( x, y );
736 void WebView::GetScrollPosition( int& x, int& y ) const
740 mWebEngine.GetScrollPosition( x, y );
744 void WebView::GetScrollSize( int& width, int& height ) const
748 mWebEngine.GetScrollSize( width, height );
752 void WebView::GetContentSize( int& width, int& height ) const
756 mWebEngine.GetContentSize( width, height );
760 std::string WebView::GetTitle() const
762 return mWebEngine ? mWebEngine.GetTitle() : kEmptyString;
765 const std::string& WebView::GetUserAgent() const
767 return mWebEngine ? mWebEngine.GetUserAgent() : kEmptyString;
770 void WebView::SetUserAgent( const std::string& userAgent )
774 mWebEngine.SetUserAgent( userAgent );
778 #undef GET_ENUM_STRING
779 #undef GET_ENUM_VALUE
781 } // namespace Internal
783 } // namespace Toolkit