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 )
90 void LoadUrl( const std::string& url )
93 ConnectToGlobalSignal( &OnLoadUrl );
96 const std::string& GetUrl() const
101 bool CanGoForward() const
103 return mHistory.size() > mCurrentPlusOnePos;
108 ConnectToGlobalSignal( &OnGoForward );
111 bool CanGoBack() const
113 return mCurrentPlusOnePos > 1;
118 ConnectToGlobalSignal( &OnGoBack );
121 void EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
127 ConnectToGlobalSignal( &OnEvaluteJavaScript );
129 mResultCallbacks.push_back( resultHandler );
135 ConnectToGlobalSignal( &OnClearHistory );
138 Dali::WebEnginePlugin::CacheModel GetCacheModel() const
143 void SetCacheModel( Dali::WebEnginePlugin::CacheModel cacheModel )
145 mCacheModel = cacheModel;
148 Dali::WebEnginePlugin::CookieAcceptPolicy GetCookieAcceptPolicy() const
150 return mCookieAcceptPolicy;
153 void SetCookieAcceptPolicy( Dali::WebEnginePlugin::CookieAcceptPolicy policy )
155 mCookieAcceptPolicy = policy;
158 const std::string& GetUserAgent() const
163 void SetUserAgent( const std::string& userAgent )
165 mUserAgent = userAgent;
168 bool IsJavaScriptEnabled() const
170 return mEnableJavaScript;
173 void EnableJavaScript( bool enabled )
175 mEnableJavaScript = enabled;
178 bool AreImagesAutomaticallyLoaded() const
180 return mLoadImagesAutomatically;
183 void LoadImagesAutomatically( bool automatic )
185 mLoadImagesAutomatically = automatic;
188 const std::string& GetDefaultTextEncodingName() const
190 return mDefaultTextEncodingName;
193 void SetDefaultTextEncodingName( const std::string& defaultTextEncodingName )
195 mDefaultTextEncodingName = defaultTextEncodingName;
198 int GetDefaultFontSize() const
200 return mDefaultFontSize;
203 void SetDefaultFontSize( int defaultFontSize )
205 mDefaultFontSize = defaultFontSize;
208 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& PageLoadStartedSignal()
210 return mPageLoadStartedSignal;
213 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& PageLoadFinishedSignal()
215 return mPageLoadFinishedSignal;
218 Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType& PageLoadErrorSignal()
220 return mPageLoadErrorSignal;
224 std::vector< std::string > mHistory;
225 size_t mCurrentPlusOnePos;
226 Dali::WebEnginePlugin::CacheModel mCacheModel;
227 Dali::WebEnginePlugin::CookieAcceptPolicy mCookieAcceptPolicy;
228 std::string mUserAgent;
229 bool mEnableJavaScript;
230 bool mLoadImagesAutomatically;
231 std::string mDefaultTextEncodingName;
232 int mDefaultFontSize;
233 Dali::WebEnginePlugin::WebEnginePageLoadSignalType mPageLoadStartedSignal;
234 Dali::WebEnginePlugin::WebEnginePageLoadSignalType mPageLoadFinishedSignal;
235 Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType mPageLoadErrorSignal;
236 std::vector< std::function< void( const std::string& ) > > mResultCallbacks;
240 inline WebEngine& GetImplementation( Dali::WebEngine& webEngine )
242 DALI_ASSERT_ALWAYS( webEngine && "WebEngine handle is empty." );
243 BaseObject& handle = webEngine.GetBaseObject();
244 return static_cast< Internal::Adaptor::WebEngine& >( handle );
247 inline const WebEngine& GetImplementation( const Dali::WebEngine& webEngine )
249 DALI_ASSERT_ALWAYS( webEngine && "WebEngine handle is empty." );
250 const BaseObject& handle = webEngine.GetBaseObject();
251 return static_cast< const Internal::Adaptor::WebEngine& >( handle );
259 DisconnectFromGlobalSignal( &OnGoBack );
261 if( gInstance && gInstance->CanGoBack() )
263 gInstance->mCurrentPlusOnePos--;
270 DisconnectFromGlobalSignal( &OnGoForward );
272 if( gInstance && gInstance->CanGoForward() )
274 gInstance->mCurrentPlusOnePos++;
281 DisconnectFromGlobalSignal( &OnLoadUrl );
285 if( gInstance->mHistory.size() > gInstance->mCurrentPlusOnePos )
287 gInstance->mHistory.erase( gInstance->mHistory.begin() + gInstance->mCurrentPlusOnePos, gInstance->mHistory.end() );
289 gInstance->mHistory.push_back( gInstance->mUrl );
290 gInstance->mCurrentPlusOnePos++;
291 gInstance->mPageLoadStartedSignal.Emit( gInstance->mUrl );
292 gInstance->mPageLoadFinishedSignal.Emit( gInstance->mUrl );
297 bool OnEvaluteJavaScript()
299 DisconnectFromGlobalSignal( &OnEvaluteJavaScript );
303 for( auto& func : gInstance->mResultCallbacks )
307 gInstance->mResultCallbacks.clear();
312 bool OnClearHistory()
314 DisconnectFromGlobalSignal( &OnClearHistory );
316 if( gInstance && gInstance->mCurrentPlusOnePos ) {
317 std::string url = gInstance->mHistory[ gInstance->mCurrentPlusOnePos - 1 ];
318 std::vector< std::string >().swap( gInstance->mHistory );
319 gInstance->mHistory.push_back( url );
320 gInstance->mCurrentPlusOnePos = 1;
326 } // namespace Adaptor
328 } // namespace Internal
331 // Dali::WebEngine Implementation
332 WebEngine::WebEngine()
336 WebEngine::WebEngine( Internal::Adaptor::WebEngine* internal )
337 : BaseHandle( internal )
341 WebEngine::~WebEngine()
345 WebEngine WebEngine::New()
347 Internal::Adaptor::WebEngine* baseObject = new Internal::Adaptor::WebEngine();
349 return WebEngine( baseObject );
352 WebEngine::WebEngine( const WebEngine& WebEngine )
353 : BaseHandle( WebEngine )
357 WebEngine& WebEngine::operator=( const WebEngine& webEngine )
359 BaseHandle::operator=( webEngine );
363 WebEngine WebEngine::DownCast( BaseHandle handle )
365 return WebEngine( dynamic_cast< Internal::Adaptor::WebEngine* >( handle.GetObjectPtr() ) );
368 void WebEngine::Create( int width, int height, const std::string& locale, const std::string& timezoneId )
372 void WebEngine::Destroy()
376 void WebEngine::LoadUrl( const std::string& url )
378 return Internal::Adaptor::GetImplementation( *this ).LoadUrl( url );
381 const std::string& WebEngine::GetUrl()
383 return Internal::Adaptor::GetImplementation( *this ).GetUrl();
386 NativeImageInterfacePtr WebEngine::GetNativeImageSource()
389 Dali::NativeImageSourcePtr sourcePtr = Dali::NativeImageSource::New( source );
393 void WebEngine::LoadHTMLString( const std::string& htmlString )
397 void WebEngine::Reload()
401 void WebEngine::StopLoading()
405 void WebEngine::Suspend()
409 void WebEngine::Resume()
413 bool WebEngine::CanGoForward()
415 return Internal::Adaptor::GetImplementation( *this ).CanGoForward();
418 void WebEngine::GoForward()
420 Internal::Adaptor::GetImplementation( *this ).GoForward();
423 bool WebEngine::CanGoBack()
425 return Internal::Adaptor::GetImplementation( *this ).CanGoBack();
428 void WebEngine::GoBack()
430 Internal::Adaptor::GetImplementation( *this ).GoBack();
433 void WebEngine::EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
435 Internal::Adaptor::GetImplementation( *this ).EvaluateJavaScript( script, resultHandler );
438 void WebEngine::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void(const std::string&) > handler )
442 void WebEngine::ClearHistory()
444 Internal::Adaptor::GetImplementation( *this ).ClearHistory();
447 void WebEngine::ClearCache()
451 void WebEngine::ClearCookies()
455 Dali::WebEnginePlugin::CacheModel WebEngine::GetCacheModel() const
457 return Internal::Adaptor::GetImplementation( *this ).GetCacheModel();
460 void WebEngine::SetCacheModel( Dali::WebEnginePlugin::CacheModel cacheModel )
462 Internal::Adaptor::GetImplementation( *this ).SetCacheModel( cacheModel );
465 Dali::WebEnginePlugin::CookieAcceptPolicy WebEngine::GetCookieAcceptPolicy() const
467 return Internal::Adaptor::GetImplementation( *this ).GetCookieAcceptPolicy();
470 void WebEngine::SetCookieAcceptPolicy( Dali::WebEnginePlugin::CookieAcceptPolicy policy )
472 Internal::Adaptor::GetImplementation( *this ).SetCookieAcceptPolicy( policy );
475 const std::string& WebEngine::GetUserAgent() const
477 return Internal::Adaptor::GetImplementation( *this ).GetUserAgent();
480 void WebEngine::SetUserAgent( const std::string& userAgent )
482 Internal::Adaptor::GetImplementation( *this ).SetUserAgent( userAgent );
485 bool WebEngine::IsJavaScriptEnabled() const
487 return Internal::Adaptor::GetImplementation( *this ).IsJavaScriptEnabled();
490 void WebEngine::EnableJavaScript( bool enabled )
492 Internal::Adaptor::GetImplementation( *this ).EnableJavaScript( enabled );
495 bool WebEngine::AreImagesAutomaticallyLoaded() const
497 return Internal::Adaptor::GetImplementation( *this ).AreImagesAutomaticallyLoaded();
500 void WebEngine::LoadImagesAutomatically( bool automatic )
502 Internal::Adaptor::GetImplementation( *this ).LoadImagesAutomatically( automatic );
505 const std::string& WebEngine::GetDefaultTextEncodingName() const
507 return Internal::Adaptor::GetImplementation( *this ).GetDefaultTextEncodingName();
510 void WebEngine::SetDefaultTextEncodingName( const std::string& defaultTextEncodingName )
512 Internal::Adaptor::GetImplementation( *this ).SetDefaultTextEncodingName( defaultTextEncodingName );
515 int WebEngine::GetDefaultFontSize() const
517 return Internal::Adaptor::GetImplementation( *this ).GetDefaultFontSize();
520 void WebEngine::SetDefaultFontSize( int defaultFontSize )
522 Internal::Adaptor::GetImplementation( *this ).SetDefaultFontSize( defaultFontSize );
525 void WebEngine::SetSize( int width, int height )
529 bool WebEngine::SendTouchEvent( const TouchEvent& touch )
534 bool WebEngine::SendKeyEvent( const KeyEvent& event )
539 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadStartedSignal()
541 return Internal::Adaptor::GetImplementation( *this ).PageLoadStartedSignal();
544 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadFinishedSignal()
546 return Internal::Adaptor::GetImplementation( *this ).PageLoadFinishedSignal();
549 Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType& WebEngine::PageLoadErrorSignal()
551 return Internal::Adaptor::GetImplementation( *this ).PageLoadErrorSignal();