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.
18 #include "toolkit-timer.h"
20 #include <dali/devel-api/adaptor-framework/web-engine.h>
21 #include <dali/public-api/object/any.h>
22 #include <dali/public-api/object/base-object.h>
23 #include <dali/public-api/adaptor-framework/native-image-source.h>
24 #include <toolkit-application.h>
39 static WebEngine* gInstance = NULL;
40 static int gInstanceCount = 0;
45 bool OnEvaluteJavaScript();
46 bool OnClearHistory();
48 static void ConnectToGlobalSignal( bool (*func)() )
50 Dali::Timer timer = Dali::Timer::New( 0 );
51 timer.TickSignal().Connect( func );
54 static void DisconnectFromGlobalSignal( bool (*func)() )
56 Dali::Timer timer = Dali::Timer::New( 0 );
57 timer.TickSignal().Disconnect( func );
61 class WebEngine: public Dali::BaseObject
67 , mCurrentPlusOnePos( 0 )
68 , mCacheModel( Dali::WebEnginePlugin::CacheModel::DOCUMENT_VIEWER )
69 , mCookieAcceptPolicy( Dali::WebEnginePlugin::CookieAcceptPolicy::NO_THIRD_PARTY )
71 , mEnableJavaScript( true )
72 , mLoadImagesAutomatically( true )
73 , mDefaultTextEncodingName()
74 , mDefaultFontSize( 16 )
75 , mEvaluating( false )
76 , mScrollPosition( 0, 0 )
77 , mScrollSize( 500, 500 )
78 , mContentSize( 500, 500 )
93 void LoadUrl( const std::string& url )
96 ConnectToGlobalSignal( &OnLoadUrl );
99 const std::string& GetUrl() const
104 bool CanGoForward() const
106 return mHistory.size() > mCurrentPlusOnePos;
111 ConnectToGlobalSignal( &OnGoForward );
114 bool CanGoBack() const
116 return mCurrentPlusOnePos > 1;
121 ConnectToGlobalSignal( &OnGoBack );
124 void EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
130 ConnectToGlobalSignal( &OnEvaluteJavaScript );
132 mResultCallbacks.push_back( resultHandler );
138 ConnectToGlobalSignal( &OnClearHistory );
141 Dali::WebEnginePlugin::CacheModel GetCacheModel() const
146 void SetCacheModel( Dali::WebEnginePlugin::CacheModel cacheModel )
148 mCacheModel = cacheModel;
151 Dali::WebEnginePlugin::CookieAcceptPolicy GetCookieAcceptPolicy() const
153 return mCookieAcceptPolicy;
156 void SetCookieAcceptPolicy( Dali::WebEnginePlugin::CookieAcceptPolicy policy )
158 mCookieAcceptPolicy = policy;
161 const std::string& GetUserAgent() const
166 void SetUserAgent( const std::string& userAgent )
168 mUserAgent = userAgent;
171 bool IsJavaScriptEnabled() const
173 return mEnableJavaScript;
176 void EnableJavaScript( bool enabled )
178 mEnableJavaScript = enabled;
181 bool AreImagesAutomaticallyLoaded() const
183 return mLoadImagesAutomatically;
186 void LoadImagesAutomatically( bool automatic )
188 mLoadImagesAutomatically = automatic;
191 const std::string& GetDefaultTextEncodingName() const
193 return mDefaultTextEncodingName;
196 void SetDefaultTextEncodingName( const std::string& defaultTextEncodingName )
198 mDefaultTextEncodingName = defaultTextEncodingName;
201 int GetDefaultFontSize() const
203 return mDefaultFontSize;
206 void SetDefaultFontSize( int defaultFontSize )
208 mDefaultFontSize = defaultFontSize;
211 void ScrollBy( int dx, int dy )
213 mScrollPosition += Dali::Vector2( dx, dy );
214 if ( mScrollPosition.y + mScrollSize.height > mContentSize.height )
216 gInstance->mScrollEdgeReachedSignal.Emit( Dali::WebEnginePlugin::ScrollEdge::BOTTOM );
220 void SetScrollPosition( int x, int y )
222 mScrollPosition.x = x;
223 mScrollPosition.y = y;
226 void GetScrollPosition( int& x, int& y ) const
228 x = mScrollPosition.x;
229 y = mScrollPosition.y;
232 void GetScrollSize( int& w, int& h ) const
234 w = mScrollSize.width;
235 h = mScrollSize.height;
238 void GetContentSize( int& w, int& h ) const
240 w = mContentSize.width;
241 h = mContentSize.height;
244 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& PageLoadStartedSignal()
246 return mPageLoadStartedSignal;
249 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& PageLoadFinishedSignal()
251 return mPageLoadFinishedSignal;
254 Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType& PageLoadErrorSignal()
256 return mPageLoadErrorSignal;
259 Dali::WebEnginePlugin::WebEngineScrollEdgeReachedSignalType& ScrollEdgeReachedSignal()
261 return mScrollEdgeReachedSignal;
265 std::vector< std::string > mHistory;
266 size_t mCurrentPlusOnePos;
267 Dali::WebEnginePlugin::CacheModel mCacheModel;
268 Dali::WebEnginePlugin::CookieAcceptPolicy mCookieAcceptPolicy;
269 std::string mUserAgent;
270 bool mEnableJavaScript;
271 bool mLoadImagesAutomatically;
272 std::string mDefaultTextEncodingName;
273 int mDefaultFontSize;
274 Dali::WebEnginePlugin::WebEnginePageLoadSignalType mPageLoadStartedSignal;
275 Dali::WebEnginePlugin::WebEnginePageLoadSignalType mPageLoadFinishedSignal;
276 Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType mPageLoadErrorSignal;
277 std::vector< std::function< void( const std::string& ) > > mResultCallbacks;
280 Dali::WebEnginePlugin::WebEngineScrollEdgeReachedSignalType mScrollEdgeReachedSignal;
281 Dali::Vector2 mScrollPosition;
282 Dali::Vector2 mScrollSize;
283 Dali::Vector2 mContentSize;
286 inline WebEngine& GetImplementation( Dali::WebEngine& webEngine )
288 DALI_ASSERT_ALWAYS( webEngine && "WebEngine handle is empty." );
289 BaseObject& handle = webEngine.GetBaseObject();
290 return static_cast< Internal::Adaptor::WebEngine& >( handle );
293 inline const WebEngine& GetImplementation( const Dali::WebEngine& webEngine )
295 DALI_ASSERT_ALWAYS( webEngine && "WebEngine handle is empty." );
296 const BaseObject& handle = webEngine.GetBaseObject();
297 return static_cast< const Internal::Adaptor::WebEngine& >( handle );
305 DisconnectFromGlobalSignal( &OnGoBack );
307 if( gInstance && gInstance->CanGoBack() )
309 gInstance->mCurrentPlusOnePos--;
316 DisconnectFromGlobalSignal( &OnGoForward );
318 if( gInstance && gInstance->CanGoForward() )
320 gInstance->mCurrentPlusOnePos++;
327 DisconnectFromGlobalSignal( &OnLoadUrl );
331 if( gInstance->mHistory.size() > gInstance->mCurrentPlusOnePos )
333 gInstance->mHistory.erase( gInstance->mHistory.begin() + gInstance->mCurrentPlusOnePos, gInstance->mHistory.end() );
335 gInstance->mHistory.push_back( gInstance->mUrl );
336 gInstance->mCurrentPlusOnePos++;
337 gInstance->mPageLoadStartedSignal.Emit( gInstance->mUrl );
338 gInstance->mPageLoadFinishedSignal.Emit( gInstance->mUrl );
343 bool OnEvaluteJavaScript()
345 DisconnectFromGlobalSignal( &OnEvaluteJavaScript );
349 for( auto& func : gInstance->mResultCallbacks )
353 gInstance->mResultCallbacks.clear();
358 bool OnClearHistory()
360 DisconnectFromGlobalSignal( &OnClearHistory );
362 if( gInstance && gInstance->mCurrentPlusOnePos ) {
363 std::string url = gInstance->mHistory[ gInstance->mCurrentPlusOnePos - 1 ];
364 std::vector< std::string >().swap( gInstance->mHistory );
365 gInstance->mHistory.push_back( url );
366 gInstance->mCurrentPlusOnePos = 1;
372 } // namespace Adaptor
374 } // namespace Internal
377 // Dali::WebEngine Implementation
378 WebEngine::WebEngine()
382 WebEngine::WebEngine( Internal::Adaptor::WebEngine* internal )
383 : BaseHandle( internal )
387 WebEngine::~WebEngine()
391 WebEngine WebEngine::New()
393 Internal::Adaptor::WebEngine* baseObject = new Internal::Adaptor::WebEngine();
395 return WebEngine( baseObject );
398 WebEngine::WebEngine( const WebEngine& WebEngine )
399 : BaseHandle( WebEngine )
403 WebEngine& WebEngine::operator=( const WebEngine& webEngine )
405 BaseHandle::operator=( webEngine );
409 WebEngine WebEngine::DownCast( BaseHandle handle )
411 return WebEngine( dynamic_cast< Internal::Adaptor::WebEngine* >( handle.GetObjectPtr() ) );
414 void WebEngine::Create( int width, int height, const std::string& locale, const std::string& timezoneId )
418 void WebEngine::Destroy()
422 void WebEngine::LoadUrl( const std::string& url )
424 return Internal::Adaptor::GetImplementation( *this ).LoadUrl( url );
427 const std::string& WebEngine::GetUrl()
429 return Internal::Adaptor::GetImplementation( *this ).GetUrl();
432 NativeImageInterfacePtr WebEngine::GetNativeImageSource()
435 Dali::NativeImageSourcePtr sourcePtr = Dali::NativeImageSource::New( source );
439 void WebEngine::LoadHTMLString( const std::string& htmlString )
443 void WebEngine::Reload()
447 void WebEngine::StopLoading()
451 void WebEngine::Suspend()
455 void WebEngine::Resume()
459 bool WebEngine::CanGoForward()
461 return Internal::Adaptor::GetImplementation( *this ).CanGoForward();
464 void WebEngine::GoForward()
466 Internal::Adaptor::GetImplementation( *this ).GoForward();
469 bool WebEngine::CanGoBack()
471 return Internal::Adaptor::GetImplementation( *this ).CanGoBack();
474 void WebEngine::GoBack()
476 Internal::Adaptor::GetImplementation( *this ).GoBack();
479 void WebEngine::EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
481 Internal::Adaptor::GetImplementation( *this ).EvaluateJavaScript( script, resultHandler );
484 void WebEngine::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void(const std::string&) > handler )
488 void WebEngine::ClearHistory()
490 Internal::Adaptor::GetImplementation( *this ).ClearHistory();
493 void WebEngine::ClearCache()
497 void WebEngine::ClearCookies()
501 Dali::WebEnginePlugin::CacheModel WebEngine::GetCacheModel() const
503 return Internal::Adaptor::GetImplementation( *this ).GetCacheModel();
506 void WebEngine::SetCacheModel( Dali::WebEnginePlugin::CacheModel cacheModel )
508 Internal::Adaptor::GetImplementation( *this ).SetCacheModel( cacheModel );
511 Dali::WebEnginePlugin::CookieAcceptPolicy WebEngine::GetCookieAcceptPolicy() const
513 return Internal::Adaptor::GetImplementation( *this ).GetCookieAcceptPolicy();
516 void WebEngine::SetCookieAcceptPolicy( Dali::WebEnginePlugin::CookieAcceptPolicy policy )
518 Internal::Adaptor::GetImplementation( *this ).SetCookieAcceptPolicy( policy );
521 const std::string& WebEngine::GetUserAgent() const
523 return Internal::Adaptor::GetImplementation( *this ).GetUserAgent();
526 void WebEngine::SetUserAgent( const std::string& userAgent )
528 Internal::Adaptor::GetImplementation( *this ).SetUserAgent( userAgent );
531 bool WebEngine::IsJavaScriptEnabled() const
533 return Internal::Adaptor::GetImplementation( *this ).IsJavaScriptEnabled();
536 void WebEngine::EnableJavaScript( bool enabled )
538 Internal::Adaptor::GetImplementation( *this ).EnableJavaScript( enabled );
541 bool WebEngine::AreImagesAutomaticallyLoaded() const
543 return Internal::Adaptor::GetImplementation( *this ).AreImagesAutomaticallyLoaded();
546 void WebEngine::LoadImagesAutomatically( bool automatic )
548 Internal::Adaptor::GetImplementation( *this ).LoadImagesAutomatically( automatic );
551 const std::string& WebEngine::GetDefaultTextEncodingName() const
553 return Internal::Adaptor::GetImplementation( *this ).GetDefaultTextEncodingName();
556 void WebEngine::SetDefaultTextEncodingName( const std::string& defaultTextEncodingName )
558 Internal::Adaptor::GetImplementation( *this ).SetDefaultTextEncodingName( defaultTextEncodingName );
561 int WebEngine::GetDefaultFontSize() const
563 return Internal::Adaptor::GetImplementation( *this ).GetDefaultFontSize();
566 void WebEngine::SetDefaultFontSize( int defaultFontSize )
568 Internal::Adaptor::GetImplementation( *this ).SetDefaultFontSize( defaultFontSize );
571 void WebEngine::ScrollBy( int dx, int dy )
573 Internal::Adaptor::GetImplementation( *this ).ScrollBy( dx, dy );
576 void WebEngine::SetScrollPosition( int x, int y )
578 Internal::Adaptor::GetImplementation( *this ).SetScrollPosition( x, y );
581 void WebEngine::GetScrollPosition( int& x, int& y ) const
583 Internal::Adaptor::GetImplementation( *this ).GetScrollPosition( x, y );
586 void WebEngine::GetScrollSize( int& w, int& h ) const
588 Internal::Adaptor::GetImplementation( *this ).GetScrollSize( w, h );
591 void WebEngine::GetContentSize( int& w, int& h ) const
593 Internal::Adaptor::GetImplementation( *this ).GetContentSize( w, h );
596 void WebEngine::SetSize( int width, int height )
600 bool WebEngine::SendTouchEvent( const TouchEvent& touch )
605 bool WebEngine::SendKeyEvent( const KeyEvent& event )
610 void WebEngine::SetFocus( bool focused )
614 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadStartedSignal()
616 return Internal::Adaptor::GetImplementation( *this ).PageLoadStartedSignal();
619 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadFinishedSignal()
621 return Internal::Adaptor::GetImplementation( *this ).PageLoadFinishedSignal();
624 Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType& WebEngine::PageLoadErrorSignal()
626 return Internal::Adaptor::GetImplementation( *this ).PageLoadErrorSignal();
629 Dali::WebEnginePlugin::WebEngineScrollEdgeReachedSignalType& WebEngine::ScrollEdgeReachedSignal()
631 return Internal::Adaptor::GetImplementation( *this ).ScrollEdgeReachedSignal();