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>
45 namespace // unnamed namespace
48 constexpr char const * const kPluginFullNamePrefix = "libdali2-web-engine-";
49 constexpr char const * const kPluginFullNamePostfix = "-plugin.so";
50 constexpr char const * const kPluginFullNameDefault = "libdali2-web-engine-plugin.so";
52 // Note: Dali WebView policy does not allow to use multiple web engines in an application.
53 // So once pluginName is set to non-empty string, it will not change.
54 std::string pluginName;
56 std::string MakePluginName( const char* environmentName )
58 std::stringstream fullName;
59 fullName << kPluginFullNamePrefix << environmentName << kPluginFullNamePostfix;
60 return std::move( fullName.str() );
63 Dali::BaseHandle Create()
65 return Dali::WebEngine::New();
68 Dali::TypeRegistration type( typeid( Dali::WebEngine ), typeid( Dali::BaseHandle ), Create );
70 } // unnamed namespace
72 WebEnginePtr WebEngine::New()
74 WebEngine* instance = new WebEngine();
76 if( !instance->Initialize() )
85 WebEngine::WebEngine()
88 mCreateWebEnginePtr( NULL ),
89 mDestroyWebEnginePtr( NULL )
93 WebEngine::~WebEngine()
97 if( mDestroyWebEnginePtr != NULL )
100 mDestroyWebEnginePtr( mPlugin );
107 bool WebEngine::InitializePluginHandle()
109 if( pluginName.length() == 0 )
111 // pluginName is not initialized yet.
112 const char* name = EnvironmentVariable::GetEnvironmentVariable( DALI_ENV_WEB_ENGINE_NAME );
115 pluginName = MakePluginName( name );
116 mHandle = dlopen( pluginName.c_str(), RTLD_LAZY );
122 pluginName = std::string( kPluginFullNameDefault );
125 mHandle = dlopen( pluginName.c_str(), RTLD_LAZY );
128 DALI_LOG_ERROR( "Can't load %s : %s\n", pluginName.c_str(), dlerror() );
135 bool WebEngine::Initialize()
139 if( !InitializePluginHandle() )
144 mCreateWebEnginePtr = reinterpret_cast< CreateWebEngineFunction >( dlsym( mHandle, "CreateWebEnginePlugin" ) );
145 if( mCreateWebEnginePtr == NULL )
147 DALI_LOG_ERROR( "Can't load symbol CreateWebEnginePlugin(), error: %s\n", error );
151 mDestroyWebEnginePtr = reinterpret_cast< DestroyWebEngineFunction >( dlsym( mHandle, "DestroyWebEnginePlugin" ) );
153 if( mDestroyWebEnginePtr == NULL )
155 DALI_LOG_ERROR( "Can't load symbol DestroyWebEnginePlugin(), error: %s\n", error );
159 mPlugin = mCreateWebEnginePtr();
161 if( mPlugin == NULL )
163 DALI_LOG_ERROR( "Can't create the WebEnginePlugin object\n" );
170 void WebEngine::Create( int width, int height, const std::string& locale, const std::string& timezoneId )
172 mPlugin->Create( width, height, locale, timezoneId );
175 void WebEngine::Destroy()
180 Dali::NativeImageInterfacePtr WebEngine::GetNativeImageSource()
182 return mPlugin->GetNativeImageSource();
185 Dali::WebEngineSettings& WebEngine::GetSettings() const
187 return mPlugin->GetSettings();
190 Dali::WebEngineContext& WebEngine::GetContext() const
192 return mPlugin->GetContext();
195 Dali::WebEngineCookieManager& WebEngine::GetCookieManager() const
197 return mPlugin->GetCookieManager();
200 Dali::WebEngineBackForwardList& WebEngine::GetBackForwardList() const
202 return mPlugin->GetBackForwardList();
205 void WebEngine::LoadUrl( const std::string& url )
207 mPlugin->LoadUrl( url );
210 const std::string& WebEngine::GetUrl()
212 return mPlugin->GetUrl();
215 void WebEngine::LoadHtmlString( const std::string& htmlString )
217 mPlugin->LoadHtmlString( htmlString );
220 void WebEngine::Reload()
225 void WebEngine::StopLoading()
227 mPlugin->StopLoading();
230 void WebEngine::Suspend()
235 void WebEngine::Resume()
240 void WebEngine::ScrollBy( int deltaX, int deltaY )
242 mPlugin->ScrollBy( deltaX, deltaY );
245 void WebEngine::SetScrollPosition( int x, int y )
247 mPlugin->SetScrollPosition( x, y );
250 void WebEngine::GetScrollPosition( int& x, int& y ) const
252 mPlugin->GetScrollPosition( x, y );
255 void WebEngine::GetScrollSize( int& width, int& height ) const
257 mPlugin->GetScrollSize( width, height );
260 void WebEngine::GetContentSize( int& width, int& height ) const
262 mPlugin->GetContentSize( width, height );
265 bool WebEngine::CanGoForward()
267 return mPlugin->CanGoForward();
270 void WebEngine::GoForward()
272 mPlugin->GoForward();
275 bool WebEngine::CanGoBack()
277 return mPlugin->CanGoBack();
280 void WebEngine::GoBack()
285 void WebEngine::EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
287 mPlugin->EvaluateJavaScript( script, resultHandler );
290 void WebEngine::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void(const std::string&) > handler )
292 mPlugin->AddJavaScriptMessageHandler( exposedObjectName, handler );
295 const std::string& WebEngine::GetUserAgent() const
297 return mPlugin->GetUserAgent();
300 void WebEngine::SetUserAgent( const std::string& userAgent )
302 mPlugin->SetUserAgent( userAgent );
305 void WebEngine::ClearHistory()
307 mPlugin->ClearHistory();
310 void WebEngine::SetSize( int width, int height )
312 mPlugin->SetSize( width, height );
315 bool WebEngine::SendTouchEvent( const Dali::TouchEvent& touch )
317 return mPlugin->SendTouchEvent( touch );
320 bool WebEngine::SendKeyEvent( const Dali::KeyEvent& event )
322 return mPlugin->SendKeyEvent( event );
325 void WebEngine::SetFocus( bool focused )
327 mPlugin->SetFocus( focused );
330 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadStartedSignal()
332 return mPlugin->PageLoadStartedSignal();
335 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadFinishedSignal()
337 return mPlugin->PageLoadFinishedSignal();
340 Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType& WebEngine::PageLoadErrorSignal()
342 return mPlugin->PageLoadErrorSignal();
345 Dali::WebEnginePlugin::WebEngineScrollEdgeReachedSignalType& WebEngine::ScrollEdgeReachedSignal()
347 return mPlugin->ScrollEdgeReachedSignal();
350 } // namespace Adaptor;
351 } // namespace Internal;