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 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 )
76 if ( !gInstanceCount )
82 void LoadUrl( const std::string& url )
85 ConnectToGlobalSignal( &OnLoadUrl );
88 const std::string& GetUrl() const
93 bool CanGoForward() const
95 return mHistory.size() > mCurrentPlusOnePos;
100 ConnectToGlobalSignal( &OnGoForward );
103 bool CanGoBack() const
105 return mCurrentPlusOnePos > 1;
110 ConnectToGlobalSignal( &OnGoBack );
115 ConnectToGlobalSignal( &OnClearHistory );
118 Dali::WebEnginePlugin::WebEngineSignalType& PageLoadStartedSignal()
120 return mPageLoadStartedSignal;
123 Dali::WebEnginePlugin::WebEngineSignalType& PageLoadFinishedSignal()
125 return mPageLoadFinishedSignal;
129 std::vector< std::string > mHistory;
130 size_t mCurrentPlusOnePos;
131 Dali::WebEnginePlugin::WebEngineSignalType mPageLoadStartedSignal;
132 Dali::WebEnginePlugin::WebEngineSignalType mPageLoadFinishedSignal;
135 inline WebEngine& GetImplementation( Dali::WebEngine& webEngine )
137 DALI_ASSERT_ALWAYS( webEngine && "WebEngine handle is empty." );
138 BaseObject& handle = webEngine.GetBaseObject();
139 return static_cast< Internal::Adaptor::WebEngine& >( handle );
142 inline const WebEngine& GetImplementation( const Dali::WebEngine& webEngine )
144 DALI_ASSERT_ALWAYS( webEngine && "WebEngine handle is empty." );
145 const BaseObject& handle = webEngine.GetBaseObject();
146 return static_cast< const Internal::Adaptor::WebEngine& >( handle );
154 DisconnectFromGlobalSignal( &OnGoBack );
156 if ( gInstance && gInstance->CanGoBack() )
158 gInstance->mCurrentPlusOnePos--;
165 DisconnectFromGlobalSignal( &OnGoForward );
167 if ( gInstance && gInstance->CanGoForward() )
169 gInstance->mCurrentPlusOnePos++;
176 DisconnectFromGlobalSignal( &OnLoadUrl );
180 if ( gInstance->mHistory.size() > gInstance->mCurrentPlusOnePos )
182 gInstance->mHistory.erase( gInstance->mHistory.begin() + gInstance->mCurrentPlusOnePos, gInstance->mHistory.end() );
184 gInstance->mHistory.push_back( gInstance->mUrl );
185 gInstance->mCurrentPlusOnePos++;
186 gInstance->mPageLoadStartedSignal.Emit( gInstance->mUrl );
187 gInstance->mPageLoadFinishedSignal.Emit( gInstance->mUrl );
193 bool OnClearHistory()
195 DisconnectFromGlobalSignal( &OnClearHistory );
197 if ( gInstance && gInstance->mCurrentPlusOnePos ) {
198 std::string url = gInstance->mHistory[ gInstance->mCurrentPlusOnePos - 1 ];
199 std::vector< std::string >().swap( gInstance->mHistory );
200 gInstance->mHistory.push_back( url );
201 gInstance->mCurrentPlusOnePos = 1;
207 } // namespace Adaptor
209 } // namespace Internal
212 // Dali::WebEngine Implementation
213 WebEngine::WebEngine()
217 WebEngine::WebEngine( Internal::Adaptor::WebEngine* internal )
218 : BaseHandle( internal )
222 WebEngine::~WebEngine()
226 WebEngine WebEngine::New()
228 Internal::Adaptor::WebEngine* baseObject = new Internal::Adaptor::WebEngine();
230 return WebEngine( baseObject );
233 WebEngine::WebEngine( const WebEngine& WebEngine )
234 : BaseHandle( WebEngine )
238 WebEngine& WebEngine::operator=( const WebEngine& webEngine )
240 BaseHandle::operator=( webEngine );
244 WebEngine WebEngine::DownCast( BaseHandle handle )
246 return WebEngine( dynamic_cast< Internal::Adaptor::WebEngine* >( handle.GetObjectPtr() ) );
249 void WebEngine::Create( int width, int height, const std::string& locale, const std::string& timezoneId )
253 void WebEngine::Destroy()
257 void WebEngine::LoadUrl( const std::string& url )
259 return Internal::Adaptor::GetImplementation( *this ).LoadUrl( url );
262 const std::string& WebEngine::GetUrl()
264 return Internal::Adaptor::GetImplementation( *this ).GetUrl();
267 NativeImageInterfacePtr WebEngine::GetNativeImageSource()
270 Dali::NativeImageSourcePtr sourcePtr = Dali::NativeImageSource::New( source );
274 void WebEngine::LoadHTMLString( const std::string& htmlString )
278 void WebEngine::Reload()
282 void WebEngine::StopLoading()
286 bool WebEngine::CanGoForward()
288 return Internal::Adaptor::GetImplementation( *this ).CanGoForward();
291 void WebEngine::GoForward()
293 Internal::Adaptor::GetImplementation( *this ).GoForward();
296 bool WebEngine::CanGoBack()
298 return Internal::Adaptor::GetImplementation( *this ).CanGoBack();
301 void WebEngine::GoBack()
303 Internal::Adaptor::GetImplementation( *this ).GoBack();
306 void WebEngine::EvaluateJavaScript( const std::string& script )
310 void WebEngine::AddJavaScriptInterface( const std::string& exposedObjectName, const std::string& jsFunctionName, std::function< std::string(const std::string&) > cb )
314 void WebEngine::RemoveJavascriptInterface( const std::string& exposedObjectName, const std::string& jsFunctionName )
318 void WebEngine::ClearHistory()
320 Internal::Adaptor::GetImplementation( *this ).ClearHistory();
323 void WebEngine::ClearCache()
327 void WebEngine::SetSize( int width, int height )
331 bool WebEngine::SendTouchEvent( const TouchData& touch )
336 bool WebEngine::SendKeyEvent( const KeyEvent& event )
341 Dali::WebEnginePlugin::WebEngineSignalType& WebEngine::PageLoadStartedSignal()
343 return Internal::Adaptor::GetImplementation( *this ).PageLoadStartedSignal();
346 Dali::WebEnginePlugin::WebEngineSignalType& WebEngine::PageLoadFinishedSignal()
348 return Internal::Adaptor::GetImplementation( *this ).PageLoadFinishedSignal();