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 <dali/internal/web-engine/common/web-engine-impl.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/public-api/object/type-registry.h>
28 #include <dali/devel-api/adaptor-framework/environment-variable.h>
29 #include <dali/devel-api/adaptor-framework/web-engine-back-forward-list.h>
30 #include <dali/devel-api/adaptor-framework/web-engine-context.h>
31 #include <dali/devel-api/adaptor-framework/web-engine-cookie-manager.h>
32 #include <dali/devel-api/adaptor-framework/web-engine-settings.h>
33 #include <dali/internal/system/common/environment-variables.h>
34 #include <dali/public-api/adaptor-framework/native-image-source.h>
35 #include <dali/public-api/images/pixel-data.h>
46 namespace // unnamed namespace
49 constexpr char const * const kPluginFullNamePrefix = "libdali2-web-engine-";
50 constexpr char const * const kPluginFullNamePostfix = "-plugin.so";
51 constexpr char const * const kPluginFullNameDefault = "libdali2-web-engine-plugin.so";
53 // Note: Dali WebView policy does not allow to use multiple web engines in an application.
54 // So once pluginName is set to non-empty string, it will not change.
55 std::string pluginName;
57 std::string MakePluginName( const char* environmentName )
59 std::stringstream fullName;
60 fullName << kPluginFullNamePrefix << environmentName << kPluginFullNamePostfix;
61 return std::move( fullName.str() );
64 Dali::BaseHandle Create()
66 return Dali::WebEngine::New();
69 Dali::TypeRegistration type( typeid( Dali::WebEngine ), typeid( Dali::BaseHandle ), Create );
71 } // unnamed namespace
73 WebEnginePtr WebEngine::New()
75 WebEngine* instance = new WebEngine();
77 if( !instance->Initialize() )
86 WebEngine::WebEngine()
89 mCreateWebEnginePtr( NULL ),
90 mDestroyWebEnginePtr( NULL )
94 WebEngine::~WebEngine()
98 if( mDestroyWebEnginePtr != NULL )
101 mDestroyWebEnginePtr( mPlugin );
108 bool WebEngine::InitializePluginHandle()
110 if( pluginName.length() == 0 )
112 // pluginName is not initialized yet.
113 const char* name = EnvironmentVariable::GetEnvironmentVariable( DALI_ENV_WEB_ENGINE_NAME );
116 pluginName = MakePluginName( name );
117 mHandle = dlopen( pluginName.c_str(), RTLD_LAZY );
123 pluginName = std::string( kPluginFullNameDefault );
126 mHandle = dlopen( pluginName.c_str(), RTLD_LAZY );
129 DALI_LOG_ERROR( "Can't load %s : %s\n", pluginName.c_str(), dlerror() );
136 bool WebEngine::Initialize()
140 if( !InitializePluginHandle() )
145 mCreateWebEnginePtr = reinterpret_cast< CreateWebEngineFunction >( dlsym( mHandle, "CreateWebEnginePlugin" ) );
146 if( mCreateWebEnginePtr == NULL )
148 DALI_LOG_ERROR( "Can't load symbol CreateWebEnginePlugin(), error: %s\n", error );
152 mDestroyWebEnginePtr = reinterpret_cast< DestroyWebEngineFunction >( dlsym( mHandle, "DestroyWebEnginePlugin" ) );
154 if( mDestroyWebEnginePtr == NULL )
156 DALI_LOG_ERROR( "Can't load symbol DestroyWebEnginePlugin(), error: %s\n", error );
160 mPlugin = mCreateWebEnginePtr();
162 if( mPlugin == NULL )
164 DALI_LOG_ERROR( "Can't create the WebEnginePlugin object\n" );
171 void WebEngine::Create( int width, int height, const std::string& locale, const std::string& timezoneId )
173 mPlugin->Create( width, height, locale, timezoneId );
176 void WebEngine::Create( int width, int height, int argc, char** argv )
178 mPlugin->Create( width, height, argc, argv );
181 void WebEngine::Destroy()
186 Dali::NativeImageInterfacePtr WebEngine::GetNativeImageSource()
188 return mPlugin->GetNativeImageSource();
191 Dali::WebEngineSettings& WebEngine::GetSettings() const
193 return mPlugin->GetSettings();
196 Dali::WebEngineContext& WebEngine::GetContext() const
198 return mPlugin->GetContext();
201 Dali::WebEngineCookieManager& WebEngine::GetCookieManager() const
203 return mPlugin->GetCookieManager();
206 Dali::WebEngineBackForwardList& WebEngine::GetBackForwardList() const
208 return mPlugin->GetBackForwardList();
211 void WebEngine::LoadUrl( const std::string& url )
213 mPlugin->LoadUrl( url );
216 std::string WebEngine::GetTitle() const
218 return mPlugin->GetTitle();
221 Dali::PixelData WebEngine::GetFavicon() const
223 return mPlugin->GetFavicon();
226 const std::string& WebEngine::GetUrl()
228 return mPlugin->GetUrl();
231 const std::string& WebEngine::GetUserAgent() const
233 return mPlugin->GetUserAgent();
236 void WebEngine::SetUserAgent( const std::string& userAgent )
238 mPlugin->SetUserAgent( userAgent );
241 void WebEngine::LoadHtmlString( const std::string& htmlString )
243 mPlugin->LoadHtmlString( htmlString );
246 void WebEngine::Reload()
251 void WebEngine::StopLoading()
253 mPlugin->StopLoading();
256 void WebEngine::Suspend()
261 void WebEngine::Resume()
266 void WebEngine::ScrollBy( int deltaX, int deltaY )
268 mPlugin->ScrollBy( deltaX, deltaY );
271 void WebEngine::SetScrollPosition( int x, int y )
273 mPlugin->SetScrollPosition( x, y );
276 Dali::Vector2 WebEngine::GetScrollPosition() const
278 return mPlugin->GetScrollPosition();
281 Dali::Vector2 WebEngine::GetScrollSize() const
283 return mPlugin->GetScrollSize();
286 Dali::Vector2 WebEngine::GetContentSize() const
288 return mPlugin->GetContentSize();
291 bool WebEngine::CanGoForward()
293 return mPlugin->CanGoForward();
296 void WebEngine::GoForward()
298 mPlugin->GoForward();
301 bool WebEngine::CanGoBack()
303 return mPlugin->CanGoBack();
306 void WebEngine::GoBack()
311 void WebEngine::EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
313 mPlugin->EvaluateJavaScript( script, resultHandler );
316 void WebEngine::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void(const std::string&) > handler )
318 mPlugin->AddJavaScriptMessageHandler( exposedObjectName, handler );
321 void WebEngine::ClearAllTilesResources()
323 mPlugin->ClearAllTilesResources();
326 void WebEngine::ClearHistory()
328 mPlugin->ClearHistory();
331 void WebEngine::SetSize( int width, int height )
333 mPlugin->SetSize( width, height );
336 bool WebEngine::SendTouchEvent( const Dali::TouchEvent& touch )
338 return mPlugin->SendTouchEvent( touch );
341 bool WebEngine::SendKeyEvent( const Dali::KeyEvent& event )
343 return mPlugin->SendKeyEvent( event );
346 void WebEngine::SetFocus( bool focused )
348 mPlugin->SetFocus( focused );
351 void WebEngine::UpdateDisplayArea( Dali::Rect< int > displayArea )
353 mPlugin->UpdateDisplayArea( displayArea );
356 void WebEngine::EnableVideoHole( bool enabled )
358 mPlugin->EnableVideoHole( enabled );
361 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadStartedSignal()
363 return mPlugin->PageLoadStartedSignal();
366 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadFinishedSignal()
368 return mPlugin->PageLoadFinishedSignal();
371 Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType& WebEngine::PageLoadErrorSignal()
373 return mPlugin->PageLoadErrorSignal();
376 Dali::WebEnginePlugin::WebEngineScrollEdgeReachedSignalType& WebEngine::ScrollEdgeReachedSignal()
378 return mPlugin->ScrollEdgeReachedSignal();
381 } // namespace Adaptor;
382 } // namespace Internal;