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/internal/system/common/environment-variables.h>
30 #include <dali/public-api/adaptor-framework/native-image-source.h>
41 namespace // unnamed namespace
44 constexpr char const * const kPluginFullNamePrefix = "libdali2-web-engine-";
45 constexpr char const * const kPluginFullNamePostfix = "-plugin.so";
46 constexpr char const * const kPluginFullNameDefault = "libdali2-web-engine-plugin.so";
48 // Note: Dali WebView policy does not allow to use multiple web engines in an application.
49 // So once pluginName is set to non-empty string, it will not change.
50 std::string pluginName;
52 std::string MakePluginName( const char* environmentName )
54 std::stringstream fullName;
55 fullName << kPluginFullNamePrefix << environmentName << kPluginFullNamePostfix;
56 return std::move( fullName.str() );
59 Dali::BaseHandle Create()
61 return Dali::WebEngine::New();
64 Dali::TypeRegistration type( typeid( Dali::WebEngine ), typeid( Dali::BaseHandle ), Create );
66 } // unnamed namespace
68 WebEnginePtr WebEngine::New()
70 WebEngine* instance = new WebEngine();
72 if( !instance->Initialize() )
81 WebEngine::WebEngine()
84 mCreateWebEnginePtr( NULL ),
85 mDestroyWebEnginePtr( NULL )
89 WebEngine::~WebEngine()
93 if( mDestroyWebEnginePtr != NULL )
96 mDestroyWebEnginePtr( mPlugin );
103 bool WebEngine::InitializePluginHandle()
105 if( pluginName.length() == 0 )
107 // pluginName is not initialized yet.
108 const char* name = EnvironmentVariable::GetEnvironmentVariable( DALI_ENV_WEB_ENGINE_NAME );
111 pluginName = MakePluginName( name );
112 mHandle = dlopen( pluginName.c_str(), RTLD_LAZY );
118 pluginName = std::string( kPluginFullNameDefault );
121 mHandle = dlopen( pluginName.c_str(), RTLD_LAZY );
124 DALI_LOG_ERROR( "Can't load %s : %s\n", pluginName.c_str(), dlerror() );
131 bool WebEngine::Initialize()
135 if( !InitializePluginHandle() )
140 mCreateWebEnginePtr = reinterpret_cast< CreateWebEngineFunction >( dlsym( mHandle, "CreateWebEnginePlugin" ) );
141 if( mCreateWebEnginePtr == NULL )
143 DALI_LOG_ERROR( "Can't load symbol CreateWebEnginePlugin(), error: %s\n", error );
147 mDestroyWebEnginePtr = reinterpret_cast< DestroyWebEngineFunction >( dlsym( mHandle, "DestroyWebEnginePlugin" ) );
149 if( mDestroyWebEnginePtr == NULL )
151 DALI_LOG_ERROR( "Can't load symbol DestroyWebEnginePlugin(), error: %s\n", error );
155 mPlugin = mCreateWebEnginePtr();
157 if( mPlugin == NULL )
159 DALI_LOG_ERROR( "Can't create the WebEnginePlugin object\n" );
166 void WebEngine::Create( int width, int height, const std::string& locale, const std::string& timezoneId )
168 mPlugin->Create( width, height, locale, timezoneId );
171 void WebEngine::Destroy()
176 Dali::NativeImageInterfacePtr WebEngine::GetNativeImageSource()
178 return mPlugin->GetNativeImageSource();
181 void WebEngine::LoadUrl( const std::string& url )
183 mPlugin->LoadUrl( url );
186 const std::string& WebEngine::GetUrl()
188 return mPlugin->GetUrl();
191 void WebEngine::LoadHTMLString( const std::string& htmlString )
193 mPlugin->LoadHTMLString( htmlString );
196 void WebEngine::Reload()
201 void WebEngine::StopLoading()
203 mPlugin->StopLoading();
206 void WebEngine::Suspend()
211 void WebEngine::Resume()
216 bool WebEngine::CanGoForward()
218 return mPlugin->CanGoForward();
221 void WebEngine::GoForward()
223 mPlugin->GoForward();
226 bool WebEngine::CanGoBack()
228 return mPlugin->CanGoBack();
231 void WebEngine::GoBack()
236 void WebEngine::EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
238 mPlugin->EvaluateJavaScript( script, resultHandler );
241 void WebEngine::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void(const std::string&) > handler )
243 mPlugin->AddJavaScriptMessageHandler( exposedObjectName, handler );
246 void WebEngine::ClearHistory()
248 mPlugin->ClearHistory();
251 void WebEngine::ClearCache()
253 mPlugin->ClearCache();
256 void WebEngine::ClearCookies()
258 mPlugin->ClearCookies();
261 Dali::WebEnginePlugin::CacheModel WebEngine::GetCacheModel() const
263 return mPlugin->GetCacheModel();
266 void WebEngine::SetCacheModel( Dali::WebEnginePlugin::CacheModel cacheModel )
268 mPlugin->SetCacheModel( cacheModel );
271 Dali::WebEnginePlugin::CookieAcceptPolicy WebEngine::GetCookieAcceptPolicy() const
273 return mPlugin->GetCookieAcceptPolicy();
276 void WebEngine::SetCookieAcceptPolicy( Dali::WebEnginePlugin::CookieAcceptPolicy policy )
278 mPlugin->SetCookieAcceptPolicy( policy );
281 const std::string& WebEngine::GetUserAgent() const
283 return mPlugin->GetUserAgent();
286 void WebEngine::SetUserAgent( const std::string& userAgent )
288 mPlugin->SetUserAgent( userAgent );
291 bool WebEngine::IsJavaScriptEnabled() const
293 return mPlugin->IsJavaScriptEnabled();
296 void WebEngine::EnableJavaScript( bool enabled )
298 mPlugin->EnableJavaScript( enabled );
301 bool WebEngine::AreImagesAutomaticallyLoaded() const
303 return mPlugin->AreImagesAutomaticallyLoaded();
306 void WebEngine::LoadImagesAutomatically( bool automatic )
308 mPlugin->LoadImagesAutomatically( automatic );
311 const std::string& WebEngine::GetDefaultTextEncodingName() const
313 return mPlugin->GetDefaultTextEncodingName();
316 void WebEngine::SetDefaultTextEncodingName( const std::string& defaultTextEncodingName )
318 mPlugin->SetDefaultTextEncodingName( defaultTextEncodingName );
321 int WebEngine::GetDefaultFontSize() const
323 return mPlugin->GetDefaultFontSize();
326 void WebEngine::SetDefaultFontSize( int defaultFontSize )
328 mPlugin->SetDefaultFontSize( defaultFontSize );
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 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadStartedSignal()
348 return mPlugin->PageLoadStartedSignal();
351 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadFinishedSignal()
353 return mPlugin->PageLoadFinishedSignal();
356 Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType& WebEngine::PageLoadErrorSignal()
358 return mPlugin->PageLoadErrorSignal();
361 } // namespace Adaptor;
362 } // namespace Internal;