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/visuals/image-visual-properties.h>
58 return Toolkit::WebView::New();
61 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::WebView, Toolkit::Control, Create )
63 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "url", STRING, URL )
64 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "userAgent", STRING, USER_AGENT )
65 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "scrollPosition", VECTOR2, SCROLL_POSITION )
66 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "scrollSize", VECTOR2, SCROLL_SIZE )
67 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "contentSize", VECTOR2, CONTENT_SIZE )
69 DALI_SIGNAL_REGISTRATION( Toolkit, WebView, "pageLoadStarted", PAGE_LOAD_STARTED_SIGNAL )
70 DALI_SIGNAL_REGISTRATION( Toolkit, WebView, "pageLoadFinished", PAGE_LOAD_FINISHED_SIGNAL )
71 DALI_SIGNAL_REGISTRATION( Toolkit, WebView, "pageLoadError", PAGE_LOAD_ERROR_SIGNAL )
72 DALI_SIGNAL_REGISTRATION( Toolkit, WebView, "scrollEdgeReached", SCROLL_EDGE_REACHED_SIGNAL )
74 DALI_TYPE_REGISTRATION_END()
76 const std::string kEmptyString;
78 } // anonymous namepsace
80 #define GET_ENUM_STRING( structName, inputExp ) \
81 Scripting::GetLinearEnumerationName< Toolkit::WebView::structName::Type >( static_cast< Toolkit::WebView::structName::Type >( inputExp ), structName##_TABLE, structName##_TABLE_COUNT )
83 #define GET_ENUM_VALUE( structName, inputExp, outputExp ) \
84 Scripting::GetEnumerationProperty< Toolkit::WebView::structName::Type >( inputExp, structName##_TABLE, structName##_TABLE_COUNT, outputExp )
86 WebView::WebView( const std::string& locale, const std::string& timezoneId )
87 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
90 mWebViewSize( Stage::GetCurrent().GetSize() ),
92 mPageLoadStartedSignal(),
93 mPageLoadFinishedSignal(),
94 mPageLoadErrorSignal()
96 mWebEngine = Dali::WebEngine::New();
98 // WebEngine is empty when it is not properly initialized.
101 mWebEngine.Create( mWebViewSize.width, mWebViewSize.height, locale, timezoneId );
114 Toolkit::WebView WebView::New()
116 WebView* impl = new WebView();
117 Toolkit::WebView handle = Toolkit::WebView( *impl );
123 Toolkit::WebView WebView::New( const std::string& locale, const std::string& timezoneId )
125 WebView* impl = new WebView( locale, timezoneId );
126 Toolkit::WebView handle = Toolkit::WebView( *impl );
132 void WebView::OnInitialize()
134 Self().SetProperty( Actor::Property::KEYBOARD_FOCUSABLE, true );
135 Self().TouchedSignal().Connect( this, &WebView::OnTouchEvent );
139 mWebEngine.PageLoadStartedSignal().Connect( this, &WebView::OnPageLoadStarted );
140 mWebEngine.PageLoadFinishedSignal().Connect( this, &WebView::OnPageLoadFinished );
141 mWebEngine.PageLoadErrorSignal().Connect( this, &WebView::OnPageLoadError );
142 mWebEngine.ScrollEdgeReachedSignal().Connect( this, &WebView::OnScrollEdgeReached );
144 mWebContext = std::unique_ptr<Dali::Toolkit::WebContext>( new WebContext( mWebEngine.GetContext() ) );
145 mWebCookieManager = std::unique_ptr<Dali::Toolkit::WebCookieManager>( new WebCookieManager( mWebEngine.GetCookieManager() ) );
146 mWebSettings = std::unique_ptr<Dali::Toolkit::WebSettings>( new WebSettings( mWebEngine.GetSettings() ) );
147 mWebBackForwardList = std::unique_ptr<Dali::Toolkit::WebBackForwardList>( new WebBackForwardList( mWebEngine.GetBackForwardList() ) );
151 Dali::Toolkit::WebSettings* WebView::GetSettings() const
153 return mWebSettings.get();
156 Dali::Toolkit::WebContext* WebView::GetContext() const
158 return mWebContext.get();
161 Dali::Toolkit::WebCookieManager* WebView::GetCookieManager() const
163 return mWebCookieManager.get();
166 Dali::Toolkit::WebBackForwardList* WebView::GetBackForwardList() const
168 return mWebBackForwardList.get();
171 void WebView::LoadUrl( const std::string& url )
176 Texture texture = Dali::Texture::New( *mWebEngine.GetNativeImageSource() );
177 const std::string nativeImageUrl = Dali::Toolkit::TextureManager::AddTexture( texture );
178 mVisual = Toolkit::VisualFactory::Get().CreateVisual(
179 { { Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE } ,
180 { Toolkit::ImageVisual::Property::URL, nativeImageUrl } } );
184 // Clean up previously registered visual and add new one.
185 DevelControl::RegisterVisual( *this, Toolkit::WebView::Property::URL, mVisual );
186 mWebEngine.LoadUrl( url );
191 void WebView::LoadHtmlString( const std::string& htmlString )
195 Texture texture = Dali::Texture::New( *mWebEngine.GetNativeImageSource() );
196 const std::string nativeImageUrl = Dali::Toolkit::TextureManager::AddTexture( texture );
197 mVisual = Toolkit::VisualFactory::Get().CreateVisual(
198 { { Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE } ,
199 { Toolkit::ImageVisual::Property::URL, nativeImageUrl } } );
203 DevelControl::RegisterVisual( *this, Toolkit::WebView::Property::URL, mVisual );
204 mWebEngine.LoadHtmlString( htmlString );
209 void WebView::Reload()
217 void WebView::StopLoading()
221 mWebEngine.StopLoading();
225 void WebView::Suspend()
229 mWebEngine.Suspend();
233 void WebView::Resume()
241 void WebView::ScrollBy( int deltaX, int deltaY )
245 mWebEngine.ScrollBy( deltaX, deltaY );
249 bool WebView::CanGoForward()
251 return mWebEngine ? mWebEngine.CanGoForward() : false;
254 void WebView::GoForward()
258 mWebEngine.GoForward();
262 bool WebView::CanGoBack()
264 return mWebEngine ? mWebEngine.CanGoBack() : false;
267 void WebView::GoBack()
275 void WebView::EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
279 mWebEngine.EvaluateJavaScript( script, resultHandler );
283 void WebView::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void( const std::string& ) > handler )
287 mWebEngine.AddJavaScriptMessageHandler( exposedObjectName, handler );
291 void WebView::ClearHistory()
295 mWebEngine.ClearHistory();
299 Dali::Toolkit::WebView::WebViewPageLoadSignalType& WebView::PageLoadStartedSignal()
301 return mPageLoadStartedSignal;
304 Dali::Toolkit::WebView::WebViewPageLoadSignalType& WebView::PageLoadFinishedSignal()
306 return mPageLoadFinishedSignal;
309 Dali::Toolkit::WebView::WebViewPageLoadErrorSignalType& WebView::PageLoadErrorSignal()
311 return mPageLoadErrorSignal;
314 Dali::Toolkit::WebView::WebViewScrollEdgeReachedSignalType& WebView::ScrollEdgeReachedSignal()
316 return mScrollEdgeReachedSignal;
319 void WebView::OnPageLoadStarted( const std::string& url )
321 if( !mPageLoadStartedSignal.Empty() )
323 Dali::Toolkit::WebView handle( GetOwner() );
324 mPageLoadStartedSignal.Emit( handle, url );
328 void WebView::OnPageLoadFinished( const std::string& url )
330 if( !mPageLoadFinishedSignal.Empty() )
332 Dali::Toolkit::WebView handle( GetOwner() );
333 mPageLoadFinishedSignal.Emit( handle, url );
337 void WebView::OnPageLoadError( const std::string& url, int errorCode )
339 if( !mPageLoadErrorSignal.Empty() )
341 Dali::Toolkit::WebView handle( GetOwner() );
342 mPageLoadErrorSignal.Emit( handle, url, static_cast< Toolkit::WebView::LoadErrorCode >( errorCode ) );
346 void WebView::OnScrollEdgeReached( Dali::WebEnginePlugin::ScrollEdge edge )
348 if( !mScrollEdgeReachedSignal.Empty() )
350 Dali::Toolkit::WebView handle( GetOwner() );
351 mScrollEdgeReachedSignal.Emit( handle, edge );
355 bool WebView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
357 Dali::BaseHandle handle( object );
359 bool connected = false;
360 Toolkit::WebView webView = Toolkit::WebView::DownCast( handle );
362 if( 0 == strcmp( signalName.c_str(), PAGE_LOAD_STARTED_SIGNAL ) )
364 webView.PageLoadStartedSignal().Connect( tracker, functor );
367 else if( 0 == strcmp( signalName.c_str(), PAGE_LOAD_FINISHED_SIGNAL ) )
369 webView.PageLoadFinishedSignal().Connect( tracker, functor );
372 else if( 0 == strcmp( signalName.c_str(), PAGE_LOAD_ERROR_SIGNAL ) )
374 webView.PageLoadErrorSignal().Connect( tracker, functor );
377 else if( 0 == strcmp( signalName.c_str(), SCROLL_EDGE_REACHED_SIGNAL ) )
379 webView.ScrollEdgeReachedSignal().Connect( tracker, functor );
386 Vector3 WebView::GetNaturalSize()
390 Vector2 rendererNaturalSize;
391 mVisual.GetNaturalSize( rendererNaturalSize );
392 return Vector3( rendererNaturalSize );
395 return Vector3( mWebViewSize );
398 void WebView::OnRelayout( const Vector2& size, RelayoutContainer& container )
400 Control::OnRelayout( size, container );
402 if( size.width > 0 && size.height > 0 && mWebViewSize != size )
408 mWebEngine.SetSize( size.width, size.height );
413 void WebView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
415 Toolkit::WebView webView = Toolkit::WebView::DownCast( Dali::BaseHandle( object ) );
419 WebView& impl = GetImpl( webView );
422 case Toolkit::WebView::Property::URL:
425 if( value.Get( url ) )
431 case Toolkit::WebView::Property::USER_AGENT:
434 if( value.Get( input ) )
436 impl.SetUserAgent( input );
440 case Toolkit::WebView::Property::SCROLL_POSITION:
443 if ( value.Get( input ) )
445 impl.SetScrollPosition( input.x, input.y );
453 Property::Value WebView::GetProperty( BaseObject* object, Property::Index propertyIndex )
455 Property::Value value;
457 Toolkit::WebView webView = Toolkit::WebView::DownCast( Dali::BaseHandle( object ) );
461 WebView& impl = GetImpl( webView );
462 switch( propertyIndex )
464 case Toolkit::WebView::Property::URL:
469 case Toolkit::WebView::Property::USER_AGENT:
471 value = impl.GetUserAgent();
474 case Toolkit::WebView::Property::SCROLL_POSITION:
477 impl.GetScrollPosition( x, y );
478 value = Vector2( x, y );
481 case Toolkit::WebView::Property::SCROLL_SIZE:
484 impl.GetScrollSize( width, height );
485 value = Vector2( width, height );
488 case Toolkit::WebView::Property::CONTENT_SIZE:
491 impl.GetContentSize( width, height );
492 value = Vector2( width, height );
503 bool WebView::OnTouchEvent( Actor actor, const Dali::TouchEvent& touch )
509 result = mWebEngine.SendTouchEvent( touch );
514 bool WebView::OnKeyEvent( const Dali::KeyEvent& event )
520 result = mWebEngine.SendKeyEvent( event );
525 void WebView::OnKeyInputFocusGained()
529 mWebEngine.SetFocus( true );
532 EmitKeyInputFocusSignal( true ); // Calls back into the Control hence done last.
535 void WebView::OnKeyInputFocusLost()
539 mWebEngine.SetFocus( false );
542 EmitKeyInputFocusSignal( false ); // Calls back into the Control hence done last.
545 void WebView::SetScrollPosition( int x, int y )
549 mWebEngine.SetScrollPosition( x, y );
553 void WebView::GetScrollPosition( int& x, int& y ) const
557 mWebEngine.GetScrollPosition( x, y );
561 void WebView::GetScrollSize( int& width, int& height ) const
565 mWebEngine.GetScrollSize( width, height );
569 void WebView::GetContentSize( int& width, int& height ) const
573 mWebEngine.GetContentSize( width, height );
577 const std::string& WebView::GetUserAgent() const
579 return mWebEngine ? mWebEngine.GetUserAgent() : kEmptyString;
582 void WebView::SetUserAgent( const std::string& userAgent )
586 mWebEngine.SetUserAgent( userAgent );
590 #undef GET_ENUM_STRING
591 #undef GET_ENUM_VALUE
593 } // namespace Internal
595 } // namespace Toolkit