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 } // anonymous namepsace
82 #define GET_ENUM_STRING( structName, inputExp ) \
83 Scripting::GetLinearEnumerationName< Toolkit::WebView::structName::Type >( static_cast< Toolkit::WebView::structName::Type >( inputExp ), structName##_TABLE, structName##_TABLE_COUNT )
85 #define GET_ENUM_VALUE( structName, inputExp, outputExp ) \
86 Scripting::GetEnumerationProperty< Toolkit::WebView::structName::Type >( inputExp, structName##_TABLE, structName##_TABLE_COUNT, outputExp )
88 WebView::WebView( const std::string& locale, const std::string& timezoneId )
89 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
92 mWebViewSize( Stage::GetCurrent().GetSize() ),
94 mPageLoadStartedSignal(),
95 mPageLoadFinishedSignal(),
96 mPageLoadErrorSignal()
98 mWebEngine = Dali::WebEngine::New();
100 // WebEngine is empty when it is not properly initialized.
103 mWebEngine.Create( mWebViewSize.width, mWebViewSize.height, locale, timezoneId );
107 WebView::WebView( int argc, char** argv )
108 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
111 mWebViewSize( Stage::GetCurrent().GetSize() ),
113 mPageLoadStartedSignal(),
114 mPageLoadFinishedSignal(),
115 mPageLoadErrorSignal()
117 mWebEngine = Dali::WebEngine::New();
119 // WebEngine is empty when it is not properly initialized.
122 mWebEngine.Create( mWebViewSize.width, mWebViewSize.height, argc, argv );
135 Toolkit::WebView WebView::New()
137 WebView* impl = new WebView();
138 Toolkit::WebView handle = Toolkit::WebView( *impl );
144 Toolkit::WebView WebView::New( const std::string& locale, const std::string& timezoneId )
146 WebView* impl = new WebView( locale, timezoneId );
147 Toolkit::WebView handle = Toolkit::WebView( *impl );
153 Toolkit::WebView WebView::New( int argc, char** argv )
155 WebView* impl = new WebView( argc, argv );
156 Toolkit::WebView handle = Toolkit::WebView( *impl );
162 void WebView::OnInitialize()
164 Self().SetProperty( Actor::Property::KEYBOARD_FOCUSABLE, true );
165 Self().TouchedSignal().Connect( this, &WebView::OnTouchEvent );
169 mWebEngine.PageLoadStartedSignal().Connect( this, &WebView::OnPageLoadStarted );
170 mWebEngine.PageLoadFinishedSignal().Connect( this, &WebView::OnPageLoadFinished );
171 mWebEngine.PageLoadErrorSignal().Connect( this, &WebView::OnPageLoadError );
172 mWebEngine.ScrollEdgeReachedSignal().Connect( this, &WebView::OnScrollEdgeReached );
174 mWebContext = std::unique_ptr<Dali::Toolkit::WebContext>( new WebContext( mWebEngine.GetContext() ) );
175 mWebCookieManager = std::unique_ptr<Dali::Toolkit::WebCookieManager>( new WebCookieManager( mWebEngine.GetCookieManager() ) );
176 mWebSettings = std::unique_ptr<Dali::Toolkit::WebSettings>( new WebSettings( mWebEngine.GetSettings() ) );
177 mWebBackForwardList = std::unique_ptr<Dali::Toolkit::WebBackForwardList>( new WebBackForwardList( mWebEngine.GetBackForwardList() ) );
181 Dali::Toolkit::WebSettings* WebView::GetSettings() const
183 return mWebSettings.get();
186 Dali::Toolkit::WebContext* WebView::GetContext() const
188 return mWebContext.get();
191 Dali::Toolkit::WebCookieManager* WebView::GetCookieManager() const
193 return mWebCookieManager.get();
196 Dali::Toolkit::WebBackForwardList* WebView::GetBackForwardList() const
198 return mWebBackForwardList.get();
201 Dali::Toolkit::ImageView& WebView::GetFavicon()
205 Dali::PixelData pixelData = mWebEngine.GetFavicon();
206 std::string url = Dali::Toolkit::Image::GenerateUrl( pixelData );
207 mFaviconView = Dali::Toolkit::ImageView::New( url );
208 mFaviconView.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
209 mFaviconView.SetProperty( Dali::Actor::Property::SIZE, Vector2( pixelData.GetWidth(), pixelData.GetHeight() ) );
214 void WebView::LoadUrl( const std::string& url )
219 Texture texture = Dali::Texture::New( *mWebEngine.GetNativeImageSource() );
220 const std::string nativeImageUrl = Dali::Toolkit::TextureManager::AddTexture( texture );
221 mVisual = Toolkit::VisualFactory::Get().CreateVisual(
222 { { Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE } ,
223 { Toolkit::ImageVisual::Property::URL, nativeImageUrl } } );
227 // Clean up previously registered visual and add new one.
228 DevelControl::RegisterVisual( *this, Toolkit::WebView::Property::URL, mVisual );
229 mWebEngine.LoadUrl( url );
234 void WebView::LoadHtmlString( const std::string& htmlString )
238 Texture texture = Dali::Texture::New( *mWebEngine.GetNativeImageSource() );
239 const std::string nativeImageUrl = Dali::Toolkit::TextureManager::AddTexture( texture );
240 mVisual = Toolkit::VisualFactory::Get().CreateVisual(
241 { { Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE } ,
242 { Toolkit::ImageVisual::Property::URL, nativeImageUrl } } );
246 DevelControl::RegisterVisual( *this, Toolkit::WebView::Property::URL, mVisual );
247 mWebEngine.LoadHtmlString( htmlString );
252 void WebView::Reload()
260 void WebView::StopLoading()
264 mWebEngine.StopLoading();
268 void WebView::Suspend()
272 mWebEngine.Suspend();
276 void WebView::Resume()
284 void WebView::ScrollBy( int deltaX, int deltaY )
288 mWebEngine.ScrollBy( deltaX, deltaY );
292 bool WebView::CanGoForward()
294 return mWebEngine ? mWebEngine.CanGoForward() : false;
297 void WebView::GoForward()
301 mWebEngine.GoForward();
305 bool WebView::CanGoBack()
307 return mWebEngine ? mWebEngine.CanGoBack() : false;
310 void WebView::GoBack()
318 void WebView::EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
322 mWebEngine.EvaluateJavaScript( script, resultHandler );
326 void WebView::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void( const std::string& ) > handler )
330 mWebEngine.AddJavaScriptMessageHandler( exposedObjectName, handler );
334 void WebView::ClearAllTilesResources()
338 mWebEngine.ClearAllTilesResources();
342 void WebView::ClearHistory()
346 mWebEngine.ClearHistory();
350 Dali::Toolkit::WebView::WebViewPageLoadSignalType& WebView::PageLoadStartedSignal()
352 return mPageLoadStartedSignal;
355 Dali::Toolkit::WebView::WebViewPageLoadSignalType& WebView::PageLoadFinishedSignal()
357 return mPageLoadFinishedSignal;
360 Dali::Toolkit::WebView::WebViewPageLoadErrorSignalType& WebView::PageLoadErrorSignal()
362 return mPageLoadErrorSignal;
365 Dali::Toolkit::WebView::WebViewScrollEdgeReachedSignalType& WebView::ScrollEdgeReachedSignal()
367 return mScrollEdgeReachedSignal;
370 void WebView::OnPageLoadStarted( const std::string& url )
372 if( !mPageLoadStartedSignal.Empty() )
374 Dali::Toolkit::WebView handle( GetOwner() );
375 mPageLoadStartedSignal.Emit( handle, url );
379 void WebView::OnPageLoadFinished( const std::string& url )
381 if( !mPageLoadFinishedSignal.Empty() )
383 Dali::Toolkit::WebView handle( GetOwner() );
384 mPageLoadFinishedSignal.Emit( handle, url );
388 void WebView::OnPageLoadError( const std::string& url, int errorCode )
390 if( !mPageLoadErrorSignal.Empty() )
392 Dali::Toolkit::WebView handle( GetOwner() );
393 mPageLoadErrorSignal.Emit( handle, url, static_cast< Toolkit::WebView::LoadErrorCode >( errorCode ) );
397 void WebView::OnScrollEdgeReached( Dali::WebEnginePlugin::ScrollEdge edge )
399 if( !mScrollEdgeReachedSignal.Empty() )
401 Dali::Toolkit::WebView handle( GetOwner() );
402 mScrollEdgeReachedSignal.Emit( handle, edge );
406 bool WebView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
408 Dali::BaseHandle handle( object );
410 bool connected = false;
411 Toolkit::WebView webView = Toolkit::WebView::DownCast( handle );
413 if( 0 == strcmp( signalName.c_str(), PAGE_LOAD_STARTED_SIGNAL ) )
415 webView.PageLoadStartedSignal().Connect( tracker, functor );
418 else if( 0 == strcmp( signalName.c_str(), PAGE_LOAD_FINISHED_SIGNAL ) )
420 webView.PageLoadFinishedSignal().Connect( tracker, functor );
423 else if( 0 == strcmp( signalName.c_str(), PAGE_LOAD_ERROR_SIGNAL ) )
425 webView.PageLoadErrorSignal().Connect( tracker, functor );
428 else if( 0 == strcmp( signalName.c_str(), SCROLL_EDGE_REACHED_SIGNAL ) )
430 webView.ScrollEdgeReachedSignal().Connect( tracker, functor );
437 Vector3 WebView::GetNaturalSize()
441 Vector2 rendererNaturalSize;
442 mVisual.GetNaturalSize( rendererNaturalSize );
443 return Vector3( rendererNaturalSize );
446 return Vector3( mWebViewSize );
449 void WebView::OnRelayout( const Vector2& size, RelayoutContainer& container )
451 Control::OnRelayout( size, container );
453 if( size.width > 0 && size.height > 0 && mWebViewSize != size )
459 mWebEngine.SetSize( size.width, size.height );
464 void WebView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
466 Toolkit::WebView webView = Toolkit::WebView::DownCast( Dali::BaseHandle( object ) );
470 WebView& impl = GetImpl( webView );
473 case Toolkit::WebView::Property::URL:
476 if( value.Get( url ) )
482 case Toolkit::WebView::Property::USER_AGENT:
485 if( value.Get( input ) )
487 impl.SetUserAgent( input );
491 case Toolkit::WebView::Property::SCROLL_POSITION:
494 if ( value.Get( input ) )
496 impl.SetScrollPosition( input.x, input.y );
504 Property::Value WebView::GetProperty( BaseObject* object, Property::Index propertyIndex )
506 Property::Value value;
508 Toolkit::WebView webView = Toolkit::WebView::DownCast( Dali::BaseHandle( object ) );
512 WebView& impl = GetImpl( webView );
513 switch( propertyIndex )
515 case Toolkit::WebView::Property::URL:
520 case Toolkit::WebView::Property::USER_AGENT:
522 value = impl.GetUserAgent();
525 case Toolkit::WebView::Property::SCROLL_POSITION:
527 value = impl.GetScrollPosition();
530 case Toolkit::WebView::Property::SCROLL_SIZE:
532 value = impl.GetScrollSize();
535 case Toolkit::WebView::Property::CONTENT_SIZE:
537 value = impl.GetContentSize();
540 case Toolkit::WebView::Property::TITLE:
542 value = impl.GetTitle();
553 bool WebView::OnTouchEvent( Actor actor, const Dali::TouchEvent& touch )
559 result = mWebEngine.SendTouchEvent( touch );
564 bool WebView::OnKeyEvent( const Dali::KeyEvent& event )
570 result = mWebEngine.SendKeyEvent( event );
575 void WebView::OnKeyInputFocusGained()
579 mWebEngine.SetFocus( true );
582 EmitKeyInputFocusSignal( true ); // Calls back into the Control hence done last.
585 void WebView::OnKeyInputFocusLost()
589 mWebEngine.SetFocus( false );
592 EmitKeyInputFocusSignal( false ); // Calls back into the Control hence done last.
595 void WebView::SetScrollPosition( int x, int y )
599 mWebEngine.SetScrollPosition( x, y );
603 Dali::Vector2 WebView::GetScrollPosition() const
605 return mWebEngine ? mWebEngine.GetScrollPosition() : Dali::Vector2::ZERO;
608 Dali::Vector2 WebView::GetScrollSize() const
610 return mWebEngine ? mWebEngine.GetScrollSize() : Dali::Vector2::ZERO;
613 Dali::Vector2 WebView::GetContentSize() const
615 return mWebEngine ? mWebEngine.GetContentSize() : Dali::Vector2::ZERO;
618 std::string WebView::GetTitle() const
620 return mWebEngine ? mWebEngine.GetTitle() : kEmptyString;
623 const std::string& WebView::GetUserAgent() const
625 return mWebEngine ? mWebEngine.GetUserAgent() : kEmptyString;
628 void WebView::SetUserAgent( const std::string& userAgent )
632 mWebEngine.SetUserAgent( userAgent );
636 #undef GET_ENUM_STRING
637 #undef GET_ENUM_VALUE
639 } // namespace Internal
641 } // namespace Toolkit