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 ) ),
133 mWebViewSize( Stage::GetCurrent().GetSize() ),
135 mVideoHoleEnabled( true ),
136 mWebViewArea ( 0, 0, mWebViewSize.width, mWebViewSize.height )
138 mWebEngine = Dali::WebEngine::New();
140 // WebEngine is empty when it is not properly initialized.
143 mWebEngine.Create( mWebViewSize.width, mWebViewSize.height, locale, timezoneId );
147 WebView::WebView( int argc, char** argv )
148 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
150 mWebViewSize( Stage::GetCurrent().GetSize() ),
152 mVideoHoleEnabled( true ),
153 mWebViewArea ( 0, 0, mWebViewSize.width, mWebViewSize.height )
155 mWebEngine = Dali::WebEngine::New();
157 // WebEngine is empty when it is not properly initialized.
160 mWebEngine.Create( mWebViewSize.width, mWebViewSize.height, argc, argv );
173 mWebEngine.Destroy();
177 Toolkit::WebView WebView::New()
179 WebView* impl = new WebView();
180 Toolkit::WebView handle = Toolkit::WebView( *impl );
186 Toolkit::WebView WebView::New( const std::string& locale, const std::string& timezoneId )
188 WebView* impl = new WebView( locale, timezoneId );
189 Toolkit::WebView handle = Toolkit::WebView( *impl );
195 Toolkit::WebView WebView::New( int argc, char** argv )
197 WebView* impl = new WebView( argc, argv );
198 Toolkit::WebView handle = Toolkit::WebView( *impl );
204 void WebView::OnInitialize()
208 self.SetProperty( Actor::Property::KEYBOARD_FOCUSABLE, true );
209 self.TouchedSignal().Connect( this, &WebView::OnTouchEvent );
211 mPositionUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_POSITION, StepCondition( 1.0f, 1.0f ) );
212 mSizeUpdateNotification = self.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 1.0f, 1.0f ) );
213 mScaleUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
214 mPositionUpdateNotification.NotifySignal().Connect( this, &WebView::UpdateDisplayArea );
215 mSizeUpdateNotification.NotifySignal().Connect( this, &WebView::UpdateDisplayArea );
216 mScaleUpdateNotification.NotifySignal().Connect( this, &WebView::UpdateDisplayArea );
220 mWebContext = std::unique_ptr<Dali::Toolkit::WebContext>( new WebContext( mWebEngine.GetContext() ) );
221 mWebCookieManager = std::unique_ptr<Dali::Toolkit::WebCookieManager>( new WebCookieManager( mWebEngine.GetCookieManager() ) );
222 mWebSettings = std::unique_ptr<Dali::Toolkit::WebSettings>( new WebSettings( mWebEngine.GetSettings() ) );
223 mWebBackForwardList = std::unique_ptr<Dali::Toolkit::WebBackForwardList>( new WebBackForwardList( mWebEngine.GetBackForwardList() ) );
227 Dali::Toolkit::WebSettings* WebView::GetSettings() const
229 return mWebSettings.get();
232 Dali::Toolkit::WebContext* WebView::GetContext() const
234 return mWebContext.get();
237 Dali::Toolkit::WebCookieManager* WebView::GetCookieManager() const
239 return mWebCookieManager.get();
242 Dali::Toolkit::WebBackForwardList* WebView::GetBackForwardList() const
244 return mWebBackForwardList.get();
247 Dali::WebEnginePlugin* WebView::GetPlugin() const
249 return mWebEngine ? mWebEngine.GetPlugin() : nullptr;
252 Dali::Toolkit::ImageView WebView::GetFavicon() const
254 Dali::Toolkit::ImageView faviconView;
257 Dali::PixelData pixelData = mWebEngine.GetFavicon();
260 std::string url = Dali::Toolkit::Image::GenerateUrl(pixelData);
261 faviconView = Dali::Toolkit::ImageView::New(url);
262 faviconView.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
268 void WebView::LoadUrl( const std::string& url )
272 mVisual = CreateNativeImageVisual( mWebEngine.GetNativeImageSource() );
276 // Clean up previously registered visual and add new one.
277 DevelControl::RegisterVisual( *this, Toolkit::WebView::Property::URL, mVisual );
278 mWebEngine.LoadUrl( url );
281 if ( mVideoHoleEnabled )
283 EnableBlendMode( false );
288 void WebView::LoadHtmlString( const std::string& htmlString )
292 mVisual = CreateNativeImageVisual( mWebEngine.GetNativeImageSource() );
296 DevelControl::RegisterVisual( *this, Toolkit::WebView::Property::URL, mVisual );
297 mWebEngine.LoadHtmlString( htmlString );
300 if ( mVideoHoleEnabled )
302 EnableBlendMode( false );
307 void WebView::Reload()
315 void WebView::StopLoading()
319 mWebEngine.StopLoading();
323 void WebView::Suspend()
327 mWebEngine.Suspend();
331 void WebView::Resume()
339 void WebView::ScrollBy( int deltaX, int deltaY )
343 mWebEngine.ScrollBy( deltaX, deltaY );
347 bool WebView::CanGoForward()
349 return mWebEngine ? mWebEngine.CanGoForward() : false;
352 void WebView::GoForward()
356 mWebEngine.GoForward();
360 bool WebView::CanGoBack()
362 return mWebEngine ? mWebEngine.CanGoBack() : false;
365 void WebView::GoBack()
373 void WebView::EvaluateJavaScript( const std::string& script, Dali::WebEnginePlugin::JavaScriptMessageHandlerCallback resultHandler )
377 mWebEngine.EvaluateJavaScript( script, resultHandler );
381 void WebView::AddJavaScriptMessageHandler( const std::string& exposedObjectName, Dali::WebEnginePlugin::JavaScriptMessageHandlerCallback handler )
385 mWebEngine.AddJavaScriptMessageHandler( exposedObjectName, handler );
389 void WebView::ClearAllTilesResources()
393 mWebEngine.ClearAllTilesResources();
397 void WebView::ClearHistory()
401 mWebEngine.ClearHistory();
405 void WebView::SetTtsFocus(bool focused)
407 if(mWebEngine && !HasKeyInputFocus())
409 mWebEngine.SetFocus(focused);
413 void WebView::UpdateDisplayArea( Dali::PropertyNotification& /*source*/ )
418 Actor self( Self() );
420 bool positionUsesAnchorPoint = self.GetProperty< bool >( Actor::Property::POSITION_USES_ANCHOR_POINT );
421 Vector3 actorSize = self.GetCurrentProperty< Vector3 >( Actor::Property::SIZE ) * self.GetCurrentProperty< Vector3 >( Actor::Property::SCALE );
422 Vector3 anchorPointOffSet = actorSize * ( positionUsesAnchorPoint ? self.GetCurrentProperty< Vector3 >( Actor::Property::ANCHOR_POINT ) : AnchorPoint::TOP_LEFT );
423 Vector2 screenPosition = self.GetProperty< Vector2 >( Actor::Property::SCREEN_POSITION );
425 Dali::Rect< int > displayArea;
426 displayArea.x = screenPosition.x - anchorPointOffSet.x;
427 displayArea.y = screenPosition.y - anchorPointOffSet.y;
428 displayArea.width = actorSize.x;
429 displayArea.height = actorSize.y;
431 Size displaySize = Size( displayArea.width, displayArea.height );
432 if ( mWebViewSize != displaySize )
434 mWebViewSize = displaySize;
437 if (mWebViewArea != displayArea )
439 mWebViewArea = displayArea;
440 mWebEngine.UpdateDisplayArea( mWebViewArea );
444 void WebView::EnableVideoHole( bool enabled )
446 mVideoHoleEnabled = enabled;
448 EnableBlendMode( !mVideoHoleEnabled );
452 mWebEngine.EnableVideoHole( mVideoHoleEnabled );
456 void WebView::EnableBlendMode( bool blendEnabled )
459 for (uint32_t i = 0; i < self.GetRendererCount(); i++)
461 Dali::Renderer render = self.GetRendererAt( i );
462 render.SetProperty( Renderer::Property::BLEND_MODE, blendEnabled ? BlendMode::ON : BlendMode::OFF );
466 void WebView::RegisterPageLoadStartedCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback)
470 mWebEngine.RegisterPageLoadStartedCallback(callback);
474 void WebView::RegisterPageLoadFinishedCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback)
478 mWebEngine.RegisterPageLoadFinishedCallback(callback);
482 void WebView::RegisterPageLoadErrorCallback(Dali::WebEnginePlugin::WebEnginePageLoadErrorCallback callback)
486 mWebEngine.RegisterPageLoadErrorCallback(callback);
490 void WebView::RegisterScrollEdgeReachedCallback(Dali::WebEnginePlugin::WebEngineScrollEdgeReachedCallback callback)
494 mWebEngine.RegisterScrollEdgeReachedCallback(callback);
498 void WebView::RegisterUrlChangedCallback(Dali::WebEnginePlugin::WebEngineUrlChangedCallback callback)
502 mWebEngine.RegisterUrlChangedCallback(callback);
506 void WebView::RegisterNavigationPolicyDecidedCallback(Dali::WebEnginePlugin::WebEngineNavigationPolicyDecidedCallback callback)
510 mWebEngine.RegisterNavigationPolicyDecidedCallback(callback);
514 void WebView::RegisterNewWindowCreatedCallback(Dali::WebEnginePlugin::WebEngineNewWindowCreatedCallback callback)
518 mWebEngine.RegisterNewWindowCreatedCallback(callback);
522 void WebView::GetPlainTextAsynchronously(Dali::WebEnginePlugin::PlainTextReceivedCallback callback)
526 mWebEngine.GetPlainTextAsynchronously(callback);
530 Vector3 WebView::GetNaturalSize()
534 Vector2 rendererNaturalSize;
535 mVisual.GetNaturalSize( rendererNaturalSize );
536 return Vector3( rendererNaturalSize );
539 return Vector3( mWebViewSize );
542 void WebView::OnSceneConnection( int depth )
544 Control::OnSceneConnection( depth );
546 EnableBlendMode( !mVideoHoleEnabled );
549 void WebView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
551 Toolkit::WebView webView = Toolkit::WebView::DownCast( Dali::BaseHandle( object ) );
555 WebView& impl = GetImpl( webView );
558 case Toolkit::WebView::Property::URL:
561 if( value.Get( url ) )
567 case Toolkit::WebView::Property::USER_AGENT:
570 if( value.Get( input ) )
572 impl.SetUserAgent( input );
576 case Toolkit::WebView::Property::SCROLL_POSITION:
579 if ( value.Get( input ) )
581 impl.SetScrollPosition( input.x, input.y );
585 case Toolkit::WebView::Property::VIDEO_HOLE_ENABLED:
588 if( value.Get( input ) )
590 impl.EnableVideoHole( input );
598 Property::Value WebView::GetProperty( BaseObject* object, Property::Index propertyIndex )
600 Property::Value value;
602 Toolkit::WebView webView = Toolkit::WebView::DownCast( Dali::BaseHandle( object ) );
606 WebView& impl = GetImpl( webView );
607 switch( propertyIndex )
609 case Toolkit::WebView::Property::URL:
611 value = impl.GetUrl();
614 case Toolkit::WebView::Property::USER_AGENT:
616 value = impl.GetUserAgent();
619 case Toolkit::WebView::Property::SCROLL_POSITION:
622 impl.GetScrollPosition( x, y );
623 value = Vector2( x, y );
626 case Toolkit::WebView::Property::SCROLL_SIZE:
629 impl.GetScrollSize( width, height );
630 value = Vector2( width, height );
633 case Toolkit::WebView::Property::CONTENT_SIZE:
636 impl.GetContentSize( width, height );
637 value = Vector2( width, height );
640 case Toolkit::WebView::Property::TITLE:
642 value = impl.GetTitle();
645 case Toolkit::WebView::Property::VIDEO_HOLE_ENABLED:
647 value = impl.mVideoHoleEnabled;
658 bool WebView::OnTouchEvent( Actor actor, const Dali::TouchEvent& touch )
664 result = mWebEngine.SendTouchEvent( touch );
669 bool WebView::OnKeyEvent( const Dali::KeyEvent& event )
675 result = mWebEngine.SendKeyEvent( event );
680 void WebView::OnKeyInputFocusGained()
684 mWebEngine.SetFocus( true );
687 EmitKeyInputFocusSignal( true ); // Calls back into the Control hence done last.
690 void WebView::OnKeyInputFocusLost()
694 mWebEngine.SetFocus( false );
697 EmitKeyInputFocusSignal( false ); // Calls back into the Control hence done last.
700 void WebView::SetScrollPosition( int x, int y )
704 mWebEngine.SetScrollPosition( x, y );
708 void WebView::GetScrollPosition( int& x, int& y ) const
712 mWebEngine.GetScrollPosition( x, y );
716 void WebView::GetScrollSize( int& width, int& height ) const
720 mWebEngine.GetScrollSize( width, height );
724 void WebView::GetContentSize( int& width, int& height ) const
728 mWebEngine.GetContentSize( width, height );
732 std::string WebView::GetUrl() const
734 return mWebEngine ? mWebEngine.GetUrl() : kEmptyString;
737 std::string WebView::GetTitle() const
739 return mWebEngine ? mWebEngine.GetTitle() : kEmptyString;
742 std::string WebView::GetUserAgent() const
744 return mWebEngine ? mWebEngine.GetUserAgent() : kEmptyString;
747 void WebView::SetUserAgent( const std::string& userAgent )
751 mWebEngine.SetUserAgent( userAgent );
755 #undef GET_ENUM_STRING
756 #undef GET_ENUM_VALUE
758 } // namespace Internal
760 } // namespace Toolkit