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 )
71 DALI_SIGNAL_REGISTRATION( Toolkit, WebView, "pageLoadStarted", PAGE_LOAD_STARTED_SIGNAL )
72 DALI_SIGNAL_REGISTRATION( Toolkit, WebView, "pageLoadFinished", PAGE_LOAD_FINISHED_SIGNAL )
73 DALI_SIGNAL_REGISTRATION( Toolkit, WebView, "pageLoadError", PAGE_LOAD_ERROR_SIGNAL )
74 DALI_SIGNAL_REGISTRATION( Toolkit, WebView, "scrollEdgeReached", SCROLL_EDGE_REACHED_SIGNAL )
76 DALI_TYPE_REGISTRATION_END()
78 const std::string kEmptyString;
80 const char* DEFAULT_SAMPLER_TYPENAME = "sampler2D";
82 const char* FRAGMENT_SHADER_TEXTURE = DALI_COMPOSE_SHADER(
83 varying mediump vec2 vTexCoord;\n
84 uniform sampler2D sTexture;\n
85 uniform lowp vec4 uColor;\n
86 uniform lowp vec3 mixColor;\n
87 uniform lowp float preMultipliedAlpha;\n
91 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * vec4( mixColor, 1.0 );\n
95 Dali::Toolkit::Visual::Base CreateNativeImageVisual( NativeImageInterfacePtr nativeImageInterface )
97 std::string fragmentShader;
99 const char* fragmentPrefix = nativeImageInterface->GetCustomFragmentPrefix();
102 fragmentShader = fragmentPrefix;
103 fragmentShader += FRAGMENT_SHADER_TEXTURE;
107 fragmentShader = FRAGMENT_SHADER_TEXTURE;
110 const char* customSamplerTypename = nativeImageInterface->GetCustomSamplerTypename();
111 if( customSamplerTypename )
113 fragmentShader.replace( fragmentShader.find( DEFAULT_SAMPLER_TYPENAME ), strlen( DEFAULT_SAMPLER_TYPENAME ), customSamplerTypename );
116 Texture texture = Dali::Texture::New( *nativeImageInterface );
117 const std::string nativeImageUrl = Dali::Toolkit::TextureManager::AddTexture( texture );
119 return Toolkit::VisualFactory::Get().CreateVisual(
120 { { Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE } ,
121 { Toolkit::Visual::Property::SHADER, { { Toolkit::Visual::Shader::Property::FRAGMENT_SHADER, fragmentShader } } },
122 { Toolkit::ImageVisual::Property::URL, nativeImageUrl } } );
125 } // anonymous namepsace
127 #define GET_ENUM_STRING( structName, inputExp ) \
128 Scripting::GetLinearEnumerationName< Toolkit::WebView::structName::Type >( static_cast< Toolkit::WebView::structName::Type >( inputExp ), structName##_TABLE, structName##_TABLE_COUNT )
130 #define GET_ENUM_VALUE( structName, inputExp, outputExp ) \
131 Scripting::GetEnumerationProperty< Toolkit::WebView::structName::Type >( inputExp, structName##_TABLE, structName##_TABLE_COUNT, outputExp )
133 WebView::WebView( const std::string& locale, const std::string& timezoneId )
134 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
137 mWebViewSize( Stage::GetCurrent().GetSize() ),
139 mPageLoadStartedSignal(),
140 mPageLoadFinishedSignal(),
141 mPageLoadErrorSignal()
143 mWebEngine = Dali::WebEngine::New();
145 // WebEngine is empty when it is not properly initialized.
148 mWebEngine.Create( mWebViewSize.width, mWebViewSize.height, locale, timezoneId );
152 WebView::WebView( int argc, char** argv )
153 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
156 mWebViewSize( Stage::GetCurrent().GetSize() ),
158 mPageLoadStartedSignal(),
159 mPageLoadFinishedSignal(),
160 mPageLoadErrorSignal()
162 mWebEngine = Dali::WebEngine::New();
164 // WebEngine is empty when it is not properly initialized.
167 mWebEngine.Create( mWebViewSize.width, mWebViewSize.height, argc, argv );
180 Toolkit::WebView WebView::New()
182 WebView* impl = new WebView();
183 Toolkit::WebView handle = Toolkit::WebView( *impl );
189 Toolkit::WebView WebView::New( const std::string& locale, const std::string& timezoneId )
191 WebView* impl = new WebView( locale, timezoneId );
192 Toolkit::WebView handle = Toolkit::WebView( *impl );
198 Toolkit::WebView WebView::New( int argc, char** argv )
200 WebView* impl = new WebView( argc, argv );
201 Toolkit::WebView handle = Toolkit::WebView( *impl );
207 void WebView::OnInitialize()
209 Self().SetProperty( Actor::Property::KEYBOARD_FOCUSABLE, true );
210 Self().TouchedSignal().Connect( this, &WebView::OnTouchEvent );
214 mWebEngine.PageLoadStartedSignal().Connect( this, &WebView::OnPageLoadStarted );
215 mWebEngine.PageLoadFinishedSignal().Connect( this, &WebView::OnPageLoadFinished );
216 mWebEngine.PageLoadErrorSignal().Connect( this, &WebView::OnPageLoadError );
217 mWebEngine.ScrollEdgeReachedSignal().Connect( this, &WebView::OnScrollEdgeReached );
219 mWebContext = std::unique_ptr<Dali::Toolkit::WebContext>( new WebContext( mWebEngine.GetContext() ) );
220 mWebCookieManager = std::unique_ptr<Dali::Toolkit::WebCookieManager>( new WebCookieManager( mWebEngine.GetCookieManager() ) );
221 mWebSettings = std::unique_ptr<Dali::Toolkit::WebSettings>( new WebSettings( mWebEngine.GetSettings() ) );
222 mWebBackForwardList = std::unique_ptr<Dali::Toolkit::WebBackForwardList>( new WebBackForwardList( mWebEngine.GetBackForwardList() ) );
226 Dali::Toolkit::WebSettings* WebView::GetSettings() const
228 return mWebSettings.get();
231 Dali::Toolkit::WebContext* WebView::GetContext() const
233 return mWebContext.get();
236 Dali::Toolkit::WebCookieManager* WebView::GetCookieManager() const
238 return mWebCookieManager.get();
241 Dali::Toolkit::WebBackForwardList* WebView::GetBackForwardList() const
243 return mWebBackForwardList.get();
246 Dali::Toolkit::ImageView& WebView::GetFavicon()
250 Dali::PixelData pixelData = mWebEngine.GetFavicon();
251 std::string url = Dali::Toolkit::Image::GenerateUrl( pixelData );
252 mFaviconView = Dali::Toolkit::ImageView::New( url );
253 mFaviconView.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
254 mFaviconView.SetProperty( Dali::Actor::Property::SIZE, Vector2( pixelData.GetWidth(), pixelData.GetHeight() ) );
259 void WebView::LoadUrl( const std::string& url )
264 mVisual = CreateNativeImageVisual( mWebEngine.GetNativeImageSource() );
268 // Clean up previously registered visual and add new one.
269 DevelControl::RegisterVisual( *this, Toolkit::WebView::Property::URL, mVisual );
270 mWebEngine.LoadUrl( url );
275 void WebView::LoadHtmlString( const std::string& htmlString )
279 mVisual = CreateNativeImageVisual( mWebEngine.GetNativeImageSource() );
283 DevelControl::RegisterVisual( *this, Toolkit::WebView::Property::URL, mVisual );
284 mWebEngine.LoadHtmlString( htmlString );
289 void WebView::Reload()
297 void WebView::StopLoading()
301 mWebEngine.StopLoading();
305 void WebView::Suspend()
309 mWebEngine.Suspend();
313 void WebView::Resume()
321 void WebView::ScrollBy( int deltaX, int deltaY )
325 mWebEngine.ScrollBy( deltaX, deltaY );
329 bool WebView::CanGoForward()
331 return mWebEngine ? mWebEngine.CanGoForward() : false;
334 void WebView::GoForward()
338 mWebEngine.GoForward();
342 bool WebView::CanGoBack()
344 return mWebEngine ? mWebEngine.CanGoBack() : false;
347 void WebView::GoBack()
355 void WebView::EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
359 mWebEngine.EvaluateJavaScript( script, resultHandler );
363 void WebView::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void( const std::string& ) > handler )
367 mWebEngine.AddJavaScriptMessageHandler( exposedObjectName, handler );
371 void WebView::ClearAllTilesResources()
375 mWebEngine.ClearAllTilesResources();
379 void WebView::ClearHistory()
383 mWebEngine.ClearHistory();
387 Dali::Toolkit::WebView::WebViewPageLoadSignalType& WebView::PageLoadStartedSignal()
389 return mPageLoadStartedSignal;
392 Dali::Toolkit::WebView::WebViewPageLoadSignalType& WebView::PageLoadFinishedSignal()
394 return mPageLoadFinishedSignal;
397 Dali::Toolkit::WebView::WebViewPageLoadErrorSignalType& WebView::PageLoadErrorSignal()
399 return mPageLoadErrorSignal;
402 Dali::Toolkit::WebView::WebViewScrollEdgeReachedSignalType& WebView::ScrollEdgeReachedSignal()
404 return mScrollEdgeReachedSignal;
407 void WebView::OnPageLoadStarted( const std::string& url )
409 if( !mPageLoadStartedSignal.Empty() )
411 Dali::Toolkit::WebView handle( GetOwner() );
412 mPageLoadStartedSignal.Emit( handle, url );
416 void WebView::OnPageLoadFinished( const std::string& url )
418 if( !mPageLoadFinishedSignal.Empty() )
420 Dali::Toolkit::WebView handle( GetOwner() );
421 mPageLoadFinishedSignal.Emit( handle, url );
425 void WebView::OnPageLoadError( const std::string& url, int errorCode )
427 if( !mPageLoadErrorSignal.Empty() )
429 Dali::Toolkit::WebView handle( GetOwner() );
430 mPageLoadErrorSignal.Emit( handle, url, static_cast< Toolkit::WebView::LoadErrorCode >( errorCode ) );
434 void WebView::OnScrollEdgeReached( Dali::WebEnginePlugin::ScrollEdge edge )
436 if( !mScrollEdgeReachedSignal.Empty() )
438 Dali::Toolkit::WebView handle( GetOwner() );
439 mScrollEdgeReachedSignal.Emit( handle, edge );
443 bool WebView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
445 Dali::BaseHandle handle( object );
447 bool connected = false;
448 Toolkit::WebView webView = Toolkit::WebView::DownCast( handle );
450 if( 0 == strcmp( signalName.c_str(), PAGE_LOAD_STARTED_SIGNAL ) )
452 webView.PageLoadStartedSignal().Connect( tracker, functor );
455 else if( 0 == strcmp( signalName.c_str(), PAGE_LOAD_FINISHED_SIGNAL ) )
457 webView.PageLoadFinishedSignal().Connect( tracker, functor );
460 else if( 0 == strcmp( signalName.c_str(), PAGE_LOAD_ERROR_SIGNAL ) )
462 webView.PageLoadErrorSignal().Connect( tracker, functor );
465 else if( 0 == strcmp( signalName.c_str(), SCROLL_EDGE_REACHED_SIGNAL ) )
467 webView.ScrollEdgeReachedSignal().Connect( tracker, functor );
474 Vector3 WebView::GetNaturalSize()
478 Vector2 rendererNaturalSize;
479 mVisual.GetNaturalSize( rendererNaturalSize );
480 return Vector3( rendererNaturalSize );
483 return Vector3( mWebViewSize );
486 void WebView::OnRelayout( const Vector2& size, RelayoutContainer& container )
488 Control::OnRelayout( size, container );
490 if( size.width > 0 && size.height > 0 && mWebViewSize != size )
496 mWebEngine.SetSize( size.width, size.height );
501 void WebView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
503 Toolkit::WebView webView = Toolkit::WebView::DownCast( Dali::BaseHandle( object ) );
507 WebView& impl = GetImpl( webView );
510 case Toolkit::WebView::Property::URL:
513 if( value.Get( url ) )
519 case Toolkit::WebView::Property::USER_AGENT:
522 if( value.Get( input ) )
524 impl.SetUserAgent( input );
528 case Toolkit::WebView::Property::SCROLL_POSITION:
531 if ( value.Get( input ) )
533 impl.SetScrollPosition( input.x, input.y );
541 Property::Value WebView::GetProperty( BaseObject* object, Property::Index propertyIndex )
543 Property::Value value;
545 Toolkit::WebView webView = Toolkit::WebView::DownCast( Dali::BaseHandle( object ) );
549 WebView& impl = GetImpl( webView );
550 switch( propertyIndex )
552 case Toolkit::WebView::Property::URL:
557 case Toolkit::WebView::Property::USER_AGENT:
559 value = impl.GetUserAgent();
562 case Toolkit::WebView::Property::SCROLL_POSITION:
565 impl.GetScrollPosition( x, y );
566 value = Vector2( x, y );
569 case Toolkit::WebView::Property::SCROLL_SIZE:
572 impl.GetScrollSize( width, height );
573 value = Vector2( width, height );
576 case Toolkit::WebView::Property::CONTENT_SIZE:
579 impl.GetContentSize( width, height );
580 value = Vector2( width, height );
583 case Toolkit::WebView::Property::TITLE:
585 value = impl.GetTitle();
596 bool WebView::OnTouchEvent( Actor actor, const Dali::TouchEvent& touch )
602 result = mWebEngine.SendTouchEvent( touch );
607 bool WebView::OnKeyEvent( const Dali::KeyEvent& event )
613 result = mWebEngine.SendKeyEvent( event );
618 void WebView::OnKeyInputFocusGained()
622 mWebEngine.SetFocus( true );
625 EmitKeyInputFocusSignal( true ); // Calls back into the Control hence done last.
628 void WebView::OnKeyInputFocusLost()
632 mWebEngine.SetFocus( false );
635 EmitKeyInputFocusSignal( false ); // Calls back into the Control hence done last.
638 void WebView::SetScrollPosition( int x, int y )
642 mWebEngine.SetScrollPosition( x, y );
646 void WebView::GetScrollPosition( int& x, int& y ) const
650 mWebEngine.GetScrollPosition( x, y );
654 void WebView::GetScrollSize( int& width, int& height ) const
658 mWebEngine.GetScrollSize( width, height );
662 void WebView::GetContentSize( int& width, int& height ) const
666 mWebEngine.GetContentSize( width, height );
670 std::string WebView::GetTitle() const
672 return mWebEngine ? mWebEngine.GetTitle() : kEmptyString;
675 const std::string& WebView::GetUserAgent() const
677 return mWebEngine ? mWebEngine.GetUserAgent() : kEmptyString;
680 void WebView::SetUserAgent( const std::string& userAgent )
684 mWebEngine.SetUserAgent( userAgent );
688 #undef GET_ENUM_STRING
689 #undef GET_ENUM_VALUE
691 } // namespace Internal
693 } // namespace Toolkit