2 * Copyright (c) 2018 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 <dali/public-api/images/native-image.h>
25 #include <toolkit-application.h>
40 static WebEngine* gInstance = NULL;
41 static int gInstanceCount = 0;
46 bool OnEvaluteJavaScript();
47 bool OnClearHistory();
49 static void ConnectToGlobalSignal( bool (*func)() )
51 Dali::Timer timer = Dali::Timer::New( 0 );
52 timer.TickSignal().Connect( func );
55 static void DisconnectFromGlobalSignal( bool (*func)() )
57 Dali::Timer timer = Dali::Timer::New( 0 );
58 timer.TickSignal().Disconnect( func );
62 class WebEngine: public Dali::BaseObject
68 , mCurrentPlusOnePos( 0 )
69 , mCacheModel( Dali::WebEnginePlugin::CacheModel::DOCUMENT_VIEWER )
70 , mCookieAcceptPolicy( Dali::WebEnginePlugin::CookieAcceptPolicy::NO_THIRD_PARTY )
72 , mEnableJavaScript( true )
73 , mLoadImagesAutomatically( true )
74 , mDefaultTextEncodingName()
75 , mDefaultFontSize( 16 )
76 , mEvaluating( false )
91 void LoadUrl( const std::string& url )
94 ConnectToGlobalSignal( &OnLoadUrl );
97 const std::string& GetUrl() const
102 bool CanGoForward() const
104 return mHistory.size() > mCurrentPlusOnePos;
109 ConnectToGlobalSignal( &OnGoForward );
112 bool CanGoBack() const
114 return mCurrentPlusOnePos > 1;
119 ConnectToGlobalSignal( &OnGoBack );
122 void EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
128 ConnectToGlobalSignal( &OnEvaluteJavaScript );
130 mResultCallbacks.push_back( resultHandler );
136 ConnectToGlobalSignal( &OnClearHistory );
139 Dali::WebEnginePlugin::CacheModel GetCacheModel() const
144 void SetCacheModel( Dali::WebEnginePlugin::CacheModel cacheModel )
146 mCacheModel = cacheModel;
149 Dali::WebEnginePlugin::CookieAcceptPolicy GetCookieAcceptPolicy() const
151 return mCookieAcceptPolicy;
154 void SetCookieAcceptPolicy( Dali::WebEnginePlugin::CookieAcceptPolicy policy )
156 mCookieAcceptPolicy = policy;
159 const std::string& GetUserAgent() const
164 void SetUserAgent( const std::string& userAgent )
166 mUserAgent = userAgent;
169 bool IsJavaScriptEnabled() const
171 return mEnableJavaScript;
174 void EnableJavaScript( bool enabled )
176 mEnableJavaScript = enabled;
179 bool AreImagesAutomaticallyLoaded() const
181 return mLoadImagesAutomatically;
184 void LoadImagesAutomatically( bool automatic )
186 mLoadImagesAutomatically = automatic;
189 const std::string& GetDefaultTextEncodingName() const
191 return mDefaultTextEncodingName;
194 void SetDefaultTextEncodingName( const std::string& defaultTextEncodingName )
196 mDefaultTextEncodingName = defaultTextEncodingName;
199 int GetDefaultFontSize() const
201 return mDefaultFontSize;
204 void SetDefaultFontSize( int defaultFontSize )
206 mDefaultFontSize = defaultFontSize;
209 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& PageLoadStartedSignal()
211 return mPageLoadStartedSignal;
214 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& PageLoadFinishedSignal()
216 return mPageLoadFinishedSignal;
219 Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType& PageLoadErrorSignal()
221 return mPageLoadErrorSignal;
225 std::vector< std::string > mHistory;
226 size_t mCurrentPlusOnePos;
227 Dali::WebEnginePlugin::CacheModel mCacheModel;
228 Dali::WebEnginePlugin::CookieAcceptPolicy mCookieAcceptPolicy;
229 std::string mUserAgent;
230 bool mEnableJavaScript;
231 bool mLoadImagesAutomatically;
232 std::string mDefaultTextEncodingName;
233 int mDefaultFontSize;
234 Dali::WebEnginePlugin::WebEnginePageLoadSignalType mPageLoadStartedSignal;
235 Dali::WebEnginePlugin::WebEnginePageLoadSignalType mPageLoadFinishedSignal;
236 Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType mPageLoadErrorSignal;
237 std::vector< std::function< void( const std::string& ) > > mResultCallbacks;
241 inline WebEngine& GetImplementation( Dali::WebEngine& webEngine )
243 DALI_ASSERT_ALWAYS( webEngine && "WebEngine handle is empty." );
244 BaseObject& handle = webEngine.GetBaseObject();
245 return static_cast< Internal::Adaptor::WebEngine& >( handle );
248 inline const WebEngine& GetImplementation( const Dali::WebEngine& webEngine )
250 DALI_ASSERT_ALWAYS( webEngine && "WebEngine handle is empty." );
251 const BaseObject& handle = webEngine.GetBaseObject();
252 return static_cast< const Internal::Adaptor::WebEngine& >( handle );
260 DisconnectFromGlobalSignal( &OnGoBack );
262 if( gInstance && gInstance->CanGoBack() )
264 gInstance->mCurrentPlusOnePos--;
271 DisconnectFromGlobalSignal( &OnGoForward );
273 if( gInstance && gInstance->CanGoForward() )
275 gInstance->mCurrentPlusOnePos++;
282 DisconnectFromGlobalSignal( &OnLoadUrl );
286 if( gInstance->mHistory.size() > gInstance->mCurrentPlusOnePos )
288 gInstance->mHistory.erase( gInstance->mHistory.begin() + gInstance->mCurrentPlusOnePos, gInstance->mHistory.end() );
290 gInstance->mHistory.push_back( gInstance->mUrl );
291 gInstance->mCurrentPlusOnePos++;
292 gInstance->mPageLoadStartedSignal.Emit( gInstance->mUrl );
293 gInstance->mPageLoadFinishedSignal.Emit( gInstance->mUrl );
298 bool OnEvaluteJavaScript()
300 DisconnectFromGlobalSignal( &OnEvaluteJavaScript );
304 for( auto& func : gInstance->mResultCallbacks )
308 gInstance->mResultCallbacks.clear();
313 bool OnClearHistory()
315 DisconnectFromGlobalSignal( &OnClearHistory );
317 if( gInstance && gInstance->mCurrentPlusOnePos ) {
318 std::string url = gInstance->mHistory[ gInstance->mCurrentPlusOnePos - 1 ];
319 std::vector< std::string >().swap( gInstance->mHistory );
320 gInstance->mHistory.push_back( url );
321 gInstance->mCurrentPlusOnePos = 1;
327 } // namespace Adaptor
329 } // namespace Internal
332 // Dali::WebEngine Implementation
333 WebEngine::WebEngine()
337 WebEngine::WebEngine( Internal::Adaptor::WebEngine* internal )
338 : BaseHandle( internal )
342 WebEngine::~WebEngine()
346 WebEngine WebEngine::New()
348 Internal::Adaptor::WebEngine* baseObject = new Internal::Adaptor::WebEngine();
350 return WebEngine( baseObject );
353 WebEngine::WebEngine( const WebEngine& WebEngine )
354 : BaseHandle( WebEngine )
358 WebEngine& WebEngine::operator=( const WebEngine& webEngine )
360 BaseHandle::operator=( webEngine );
364 WebEngine WebEngine::DownCast( BaseHandle handle )
366 return WebEngine( dynamic_cast< Internal::Adaptor::WebEngine* >( handle.GetObjectPtr() ) );
369 void WebEngine::Create( int width, int height, const std::string& locale, const std::string& timezoneId )
373 void WebEngine::Destroy()
377 void WebEngine::LoadUrl( const std::string& url )
379 return Internal::Adaptor::GetImplementation( *this ).LoadUrl( url );
382 const std::string& WebEngine::GetUrl()
384 return Internal::Adaptor::GetImplementation( *this ).GetUrl();
387 NativeImageInterfacePtr WebEngine::GetNativeImageSource()
390 Dali::NativeImageSourcePtr sourcePtr = Dali::NativeImageSource::New( source );
394 void WebEngine::LoadHTMLString( const std::string& htmlString )
398 void WebEngine::Reload()
402 void WebEngine::StopLoading()
406 void WebEngine::Suspend()
410 void WebEngine::Resume()
414 bool WebEngine::CanGoForward()
416 return Internal::Adaptor::GetImplementation( *this ).CanGoForward();
419 void WebEngine::GoForward()
421 Internal::Adaptor::GetImplementation( *this ).GoForward();
424 bool WebEngine::CanGoBack()
426 return Internal::Adaptor::GetImplementation( *this ).CanGoBack();
429 void WebEngine::GoBack()
431 Internal::Adaptor::GetImplementation( *this ).GoBack();
434 void WebEngine::EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
436 Internal::Adaptor::GetImplementation( *this ).EvaluateJavaScript( script, resultHandler );
439 void WebEngine::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void(const std::string&) > handler )
443 void WebEngine::ClearHistory()
445 Internal::Adaptor::GetImplementation( *this ).ClearHistory();
448 void WebEngine::ClearCache()
452 void WebEngine::ClearCookies()
456 Dali::WebEnginePlugin::CacheModel WebEngine::GetCacheModel() const
458 return Internal::Adaptor::GetImplementation( *this ).GetCacheModel();
461 void WebEngine::SetCacheModel( Dali::WebEnginePlugin::CacheModel cacheModel )
463 Internal::Adaptor::GetImplementation( *this ).SetCacheModel( cacheModel );
466 Dali::WebEnginePlugin::CookieAcceptPolicy WebEngine::GetCookieAcceptPolicy() const
468 return Internal::Adaptor::GetImplementation( *this ).GetCookieAcceptPolicy();
471 void WebEngine::SetCookieAcceptPolicy( Dali::WebEnginePlugin::CookieAcceptPolicy policy )
473 Internal::Adaptor::GetImplementation( *this ).SetCookieAcceptPolicy( policy );
476 const std::string& WebEngine::GetUserAgent() const
478 return Internal::Adaptor::GetImplementation( *this ).GetUserAgent();
481 void WebEngine::SetUserAgent( const std::string& userAgent )
483 Internal::Adaptor::GetImplementation( *this ).SetUserAgent( userAgent );
486 bool WebEngine::IsJavaScriptEnabled() const
488 return Internal::Adaptor::GetImplementation( *this ).IsJavaScriptEnabled();
491 void WebEngine::EnableJavaScript( bool enabled )
493 Internal::Adaptor::GetImplementation( *this ).EnableJavaScript( enabled );
496 bool WebEngine::AreImagesAutomaticallyLoaded() const
498 return Internal::Adaptor::GetImplementation( *this ).AreImagesAutomaticallyLoaded();
501 void WebEngine::LoadImagesAutomatically( bool automatic )
503 Internal::Adaptor::GetImplementation( *this ).LoadImagesAutomatically( automatic );
506 const std::string& WebEngine::GetDefaultTextEncodingName() const
508 return Internal::Adaptor::GetImplementation( *this ).GetDefaultTextEncodingName();
511 void WebEngine::SetDefaultTextEncodingName( const std::string& defaultTextEncodingName )
513 Internal::Adaptor::GetImplementation( *this ).SetDefaultTextEncodingName( defaultTextEncodingName );
516 int WebEngine::GetDefaultFontSize() const
518 return Internal::Adaptor::GetImplementation( *this ).GetDefaultFontSize();
521 void WebEngine::SetDefaultFontSize( int defaultFontSize )
523 Internal::Adaptor::GetImplementation( *this ).SetDefaultFontSize( defaultFontSize );
526 void WebEngine::SetSize( int width, int height )
530 bool WebEngine::SendTouchEvent( const TouchData& touch )
535 bool WebEngine::SendKeyEvent( const KeyEvent& event )
540 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadStartedSignal()
542 return Internal::Adaptor::GetImplementation( *this ).PageLoadStartedSignal();
545 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadFinishedSignal()
547 return Internal::Adaptor::GetImplementation( *this ).PageLoadFinishedSignal();
550 Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType& WebEngine::PageLoadErrorSignal()
552 return Internal::Adaptor::GetImplementation( *this ).PageLoadErrorSignal();