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-policy-decision.h>
27 #include <dali/devel-api/adaptor-framework/web-engine-settings.h>
28 #include <dali/devel-api/scripting/enum-helper.h>
29 #include <dali/devel-api/scripting/scripting.h>
30 #include <dali/devel-api/common/stage.h>
31 #include <dali/public-api/adaptor-framework/native-image-source.h>
32 #include <dali/public-api/object/type-registry.h>
33 #include <dali/public-api/object/type-registry-helper.h>
36 #include <dali-toolkit/devel-api/controls/control-devel.h>
37 #include <dali-toolkit/devel-api/controls/web-view/web-back-forward-list.h>
38 #include <dali-toolkit/devel-api/controls/web-view/web-context.h>
39 #include <dali-toolkit/devel-api/controls/web-view/web-cookie-manager.h>
40 #include <dali-toolkit/devel-api/controls/web-view/web-settings.h>
41 #include <dali-toolkit/devel-api/image-loader/texture-manager.h>
42 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
43 #include <dali-toolkit/public-api/image-loader/image.h>
44 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
60 return Toolkit::WebView::New();
63 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::WebView, Toolkit::Control, Create )
65 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "url", STRING, URL )
66 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "userAgent", STRING, USER_AGENT )
67 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "scrollPosition", VECTOR2, SCROLL_POSITION )
68 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "scrollSize", VECTOR2, SCROLL_SIZE )
69 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "contentSize", VECTOR2, CONTENT_SIZE )
70 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "title", STRING, TITLE )
71 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "videoHoleEnabled", BOOLEAN, VIDEO_HOLE_ENABLED )
73 DALI_TYPE_REGISTRATION_END()
75 const std::string kEmptyString;
77 const char* DEFAULT_SAMPLER_TYPENAME = "sampler2D";
79 const char* FRAGMENT_SHADER_TEXTURE = DALI_COMPOSE_SHADER(
80 varying mediump vec2 vTexCoord;\n
81 uniform sampler2D sTexture;\n
82 uniform lowp vec4 uColor;\n
83 uniform lowp vec3 mixColor;\n
84 uniform lowp float preMultipliedAlpha;\n
88 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * vec4( mixColor, 1.0 );\n
92 Dali::Toolkit::Visual::Base CreateNativeImageVisual( NativeImageInterfacePtr nativeImageInterface )
94 std::string fragmentShader;
96 const char* fragmentPrefix = nativeImageInterface->GetCustomFragmentPrefix();
99 fragmentShader = fragmentPrefix;
100 fragmentShader += FRAGMENT_SHADER_TEXTURE;
104 fragmentShader = FRAGMENT_SHADER_TEXTURE;
107 const char* customSamplerTypename = nativeImageInterface->GetCustomSamplerTypename();
108 if( customSamplerTypename )
110 fragmentShader.replace( fragmentShader.find( DEFAULT_SAMPLER_TYPENAME ), strlen( DEFAULT_SAMPLER_TYPENAME ), customSamplerTypename );
113 Texture texture = Dali::Texture::New( *nativeImageInterface );
114 const std::string nativeImageUrl = Dali::Toolkit::TextureManager::AddTexture( texture );
116 return Toolkit::VisualFactory::Get().CreateVisual(
117 { { Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE } ,
118 { Toolkit::Visual::Property::SHADER, { { Toolkit::Visual::Shader::Property::FRAGMENT_SHADER, fragmentShader } } },
119 { Toolkit::ImageVisual::Property::URL, nativeImageUrl } } );
122 } // anonymous namepsace
124 #define GET_ENUM_STRING( structName, inputExp ) \
125 Scripting::GetLinearEnumerationName< Toolkit::WebView::structName::Type >( static_cast< Toolkit::WebView::structName::Type >( inputExp ), structName##_TABLE, structName##_TABLE_COUNT )
127 #define GET_ENUM_VALUE( structName, inputExp, outputExp ) \
128 Scripting::GetEnumerationProperty< Toolkit::WebView::structName::Type >( inputExp, structName##_TABLE, structName##_TABLE_COUNT, outputExp )
130 WebView::WebView( const std::string& locale, const std::string& timezoneId )
131 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
134 mWebViewSize( Stage::GetCurrent().GetSize() ),
136 mVideoHoleEnabled( true ),
137 mWebViewArea ( 0, 0, mWebViewSize.width, mWebViewSize.height )
139 mWebEngine = Dali::WebEngine::New();
141 // WebEngine is empty when it is not properly initialized.
144 mWebEngine.Create( mWebViewSize.width, mWebViewSize.height, locale, timezoneId );
148 WebView::WebView( int argc, char** argv )
149 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
152 mWebViewSize( Stage::GetCurrent().GetSize() ),
154 mVideoHoleEnabled( true ),
155 mWebViewArea ( 0, 0, mWebViewSize.width, mWebViewSize.height )
157 mWebEngine = Dali::WebEngine::New();
159 // WebEngine is empty when it is not properly initialized.
162 mWebEngine.Create( mWebViewSize.width, mWebViewSize.height, argc, argv );
175 mWebEngine.Destroy();
179 Toolkit::WebView WebView::New()
181 WebView* impl = new WebView();
182 Toolkit::WebView handle = Toolkit::WebView( *impl );
188 Toolkit::WebView WebView::New( const std::string& locale, const std::string& timezoneId )
190 WebView* impl = new WebView( locale, timezoneId );
191 Toolkit::WebView handle = Toolkit::WebView( *impl );
197 Toolkit::WebView WebView::New( int argc, char** argv )
199 WebView* impl = new WebView( argc, argv );
200 Toolkit::WebView handle = Toolkit::WebView( *impl );
206 void WebView::OnInitialize()
210 self.SetProperty( Actor::Property::KEYBOARD_FOCUSABLE, true );
211 self.TouchedSignal().Connect( this, &WebView::OnTouchEvent );
213 mPositionUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_POSITION, StepCondition( 1.0f, 1.0f ) );
214 mSizeUpdateNotification = self.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 1.0f, 1.0f ) );
215 mScaleUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
216 mPositionUpdateNotification.NotifySignal().Connect( this, &WebView::UpdateDisplayArea );
217 mSizeUpdateNotification.NotifySignal().Connect( this, &WebView::UpdateDisplayArea );
218 mScaleUpdateNotification.NotifySignal().Connect( this, &WebView::UpdateDisplayArea );
222 mWebContext = std::unique_ptr<Dali::Toolkit::WebContext>( new WebContext( mWebEngine.GetContext() ) );
223 mWebCookieManager = std::unique_ptr<Dali::Toolkit::WebCookieManager>( new WebCookieManager( mWebEngine.GetCookieManager() ) );
224 mWebSettings = std::unique_ptr<Dali::Toolkit::WebSettings>( new WebSettings( mWebEngine.GetSettings() ) );
225 mWebBackForwardList = std::unique_ptr<Dali::Toolkit::WebBackForwardList>( new WebBackForwardList( mWebEngine.GetBackForwardList() ) );
229 Dali::Toolkit::WebSettings* WebView::GetSettings() const
231 return mWebSettings.get();
234 Dali::Toolkit::WebContext* WebView::GetContext() const
236 return mWebContext.get();
239 Dali::Toolkit::WebCookieManager* WebView::GetCookieManager() const
241 return mWebCookieManager.get();
244 Dali::Toolkit::WebBackForwardList* WebView::GetBackForwardList() const
246 return mWebBackForwardList.get();
249 Dali::Toolkit::ImageView WebView::GetFavicon() const
251 Dali::Toolkit::ImageView faviconView;
254 Dali::PixelData pixelData = mWebEngine.GetFavicon();
257 std::string url = Dali::Toolkit::Image::GenerateUrl(pixelData);
258 faviconView = Dali::Toolkit::ImageView::New(url);
259 faviconView.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
265 void WebView::LoadUrl( const std::string& url )
270 mVisual = CreateNativeImageVisual( mWebEngine.GetNativeImageSource() );
274 // Clean up previously registered visual and add new one.
275 DevelControl::RegisterVisual( *this, Toolkit::WebView::Property::URL, mVisual );
276 mWebEngine.LoadUrl( url );
279 if ( mVideoHoleEnabled )
281 EnableBlendMode( false );
286 void WebView::LoadHtmlString( const std::string& htmlString )
290 mVisual = CreateNativeImageVisual( mWebEngine.GetNativeImageSource() );
294 DevelControl::RegisterVisual( *this, Toolkit::WebView::Property::URL, mVisual );
295 mWebEngine.LoadHtmlString( htmlString );
298 if ( mVideoHoleEnabled )
300 EnableBlendMode( false );
305 void WebView::Reload()
313 void WebView::StopLoading()
317 mWebEngine.StopLoading();
321 void WebView::Suspend()
325 mWebEngine.Suspend();
329 void WebView::Resume()
337 void WebView::ScrollBy( int deltaX, int deltaY )
341 mWebEngine.ScrollBy( deltaX, deltaY );
345 bool WebView::CanGoForward()
347 return mWebEngine ? mWebEngine.CanGoForward() : false;
350 void WebView::GoForward()
354 mWebEngine.GoForward();
358 bool WebView::CanGoBack()
360 return mWebEngine ? mWebEngine.CanGoBack() : false;
363 void WebView::GoBack()
371 void WebView::EvaluateJavaScript( const std::string& script, Dali::WebEnginePlugin::JavaScriptMessageHandlerCallback resultHandler )
375 mWebEngine.EvaluateJavaScript( script, resultHandler );
379 void WebView::AddJavaScriptMessageHandler( const std::string& exposedObjectName, Dali::WebEnginePlugin::JavaScriptMessageHandlerCallback handler )
383 mWebEngine.AddJavaScriptMessageHandler( exposedObjectName, handler );
387 void WebView::ClearAllTilesResources()
391 mWebEngine.ClearAllTilesResources();
395 void WebView::ClearHistory()
399 mWebEngine.ClearHistory();
403 void WebView::SetTtsFocus(bool focused)
405 if(mWebEngine && !HasKeyInputFocus())
407 mWebEngine.SetFocus(focused);
411 void WebView::UpdateDisplayArea( Dali::PropertyNotification& /*source*/ )
416 Actor self( Self() );
418 bool positionUsesAnchorPoint = self.GetProperty< bool >( Actor::Property::POSITION_USES_ANCHOR_POINT );
419 Vector3 actorSize = self.GetCurrentProperty< Vector3 >( Actor::Property::SIZE ) * self.GetCurrentProperty< Vector3 >( Actor::Property::SCALE );
420 Vector3 anchorPointOffSet = actorSize * ( positionUsesAnchorPoint ? self.GetCurrentProperty< Vector3 >( Actor::Property::ANCHOR_POINT ) : AnchorPoint::TOP_LEFT );
421 Vector2 screenPosition = self.GetProperty< Vector2 >( Actor::Property::SCREEN_POSITION );
423 Dali::Rect< int > displayArea;
424 displayArea.x = screenPosition.x - anchorPointOffSet.x;
425 displayArea.y = screenPosition.y - anchorPointOffSet.y;
426 displayArea.width = actorSize.x;
427 displayArea.height = actorSize.y;
429 Size displaySize = Size( displayArea.width, displayArea.height );
430 if ( mWebViewSize != displaySize )
432 mWebViewSize = displaySize;
435 if (mWebViewArea != displayArea )
437 mWebViewArea = displayArea;
438 mWebEngine.UpdateDisplayArea( mWebViewArea );
442 void WebView::EnableVideoHole( bool enabled )
444 mVideoHoleEnabled = enabled;
446 EnableBlendMode( !mVideoHoleEnabled );
450 mWebEngine.EnableVideoHole( mVideoHoleEnabled );
454 void WebView::EnableBlendMode( bool blendEnabled )
457 for (uint32_t i = 0; i < self.GetRendererCount(); i++)
459 Dali::Renderer render = self.GetRendererAt( i );
460 render.SetProperty( Renderer::Property::BLEND_MODE, blendEnabled ? BlendMode::ON : BlendMode::OFF );
464 void WebView::RegisterPageLoadStartedCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback)
468 mWebEngine.RegisterPageLoadStartedCallback(callback);
472 void WebView::RegisterPageLoadFinishedCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback)
476 mWebEngine.RegisterPageLoadFinishedCallback(callback);
480 void WebView::RegisterPageLoadErrorCallback(Dali::WebEnginePlugin::WebEnginePageLoadErrorCallback callback)
484 mWebEngine.RegisterPageLoadErrorCallback(callback);
488 void WebView::RegisterScrollEdgeReachedCallback(Dali::WebEnginePlugin::WebEngineScrollEdgeReachedCallback callback)
492 mWebEngine.RegisterScrollEdgeReachedCallback(callback);
496 void WebView::RegisterNavigationPolicyDecidedCallback(Dali::WebEnginePlugin::WebEngineNavigationPolicyDecidedCallback callback)
500 mWebEngine.RegisterNavigationPolicyDecidedCallback(callback);
504 void WebView::GetPlainTextAsynchronously(Dali::WebEnginePlugin::PlainTextReceivedCallback callback)
508 mWebEngine.GetPlainTextAsynchronously(callback);
512 Vector3 WebView::GetNaturalSize()
516 Vector2 rendererNaturalSize;
517 mVisual.GetNaturalSize( rendererNaturalSize );
518 return Vector3( rendererNaturalSize );
521 return Vector3( mWebViewSize );
524 void WebView::OnSceneConnection( int depth )
526 Control::OnSceneConnection( depth );
528 EnableBlendMode( !mVideoHoleEnabled );
531 void WebView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
533 Toolkit::WebView webView = Toolkit::WebView::DownCast( Dali::BaseHandle( object ) );
537 WebView& impl = GetImpl( webView );
540 case Toolkit::WebView::Property::URL:
543 if( value.Get( url ) )
549 case Toolkit::WebView::Property::USER_AGENT:
552 if( value.Get( input ) )
554 impl.SetUserAgent( input );
558 case Toolkit::WebView::Property::SCROLL_POSITION:
561 if ( value.Get( input ) )
563 impl.SetScrollPosition( input.x, input.y );
567 case Toolkit::WebView::Property::VIDEO_HOLE_ENABLED:
570 if( value.Get( input ) )
572 impl.EnableVideoHole( input );
580 Property::Value WebView::GetProperty( BaseObject* object, Property::Index propertyIndex )
582 Property::Value value;
584 Toolkit::WebView webView = Toolkit::WebView::DownCast( Dali::BaseHandle( object ) );
588 WebView& impl = GetImpl( webView );
589 switch( propertyIndex )
591 case Toolkit::WebView::Property::URL:
596 case Toolkit::WebView::Property::USER_AGENT:
598 value = impl.GetUserAgent();
601 case Toolkit::WebView::Property::SCROLL_POSITION:
604 impl.GetScrollPosition( x, y );
605 value = Vector2( x, y );
608 case Toolkit::WebView::Property::SCROLL_SIZE:
611 impl.GetScrollSize( width, height );
612 value = Vector2( width, height );
615 case Toolkit::WebView::Property::CONTENT_SIZE:
618 impl.GetContentSize( width, height );
619 value = Vector2( width, height );
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 void WebView::GetScrollPosition( int& x, int& y ) const
694 mWebEngine.GetScrollPosition( x, y );
698 void WebView::GetScrollSize( int& width, int& height ) const
702 mWebEngine.GetScrollSize( width, height );
706 void WebView::GetContentSize( int& width, int& height ) const
710 mWebEngine.GetContentSize( width, height );
714 std::string WebView::GetTitle() const
716 return mWebEngine ? mWebEngine.GetTitle() : kEmptyString;
719 const std::string& WebView::GetUserAgent() const
721 return mWebEngine ? mWebEngine.GetUserAgent() : kEmptyString;
724 void WebView::SetUserAgent( const std::string& userAgent )
728 mWebEngine.SetUserAgent( userAgent );
732 #undef GET_ENUM_STRING
733 #undef GET_ENUM_VALUE
735 } // namespace Internal
737 } // namespace Toolkit