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.
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 = "libdali-web-engine-";
45 constexpr char const * const kPluginFullNamePostfix = "-plugin.so";
46 constexpr char const * const kPluginFullNameDefault = "libdali-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()
71 WebEngine* engine = new WebEngine();
73 if ( !engine->Initialize() )
83 WebEngine::WebEngine()
86 mCreateWebEnginePtr( NULL ),
87 mDestroyWebEnginePtr( NULL )
91 WebEngine::~WebEngine()
95 if( mDestroyWebEnginePtr != NULL )
98 mDestroyWebEnginePtr( mPlugin );
105 bool WebEngine::InitializePluginHandle()
107 if ( pluginName.length() == 0 )
109 // pluginName is not initialized yet.
110 const char* name = EnvironmentVariable::GetEnvironmentVariable( DALI_ENV_WEB_ENGINE_NAME );
113 pluginName = MakePluginName( name );
114 mHandle = dlopen( pluginName.c_str(), RTLD_LAZY );
120 pluginName = std::string( kPluginFullNameDefault );
123 mHandle = dlopen( pluginName.c_str(), RTLD_LAZY );
126 DALI_LOG_ERROR( "Can't load %s : %s\n", pluginName.c_str(), dlerror() );
133 bool WebEngine::Initialize()
137 if ( !InitializePluginHandle() )
142 mCreateWebEnginePtr = reinterpret_cast< CreateWebEngineFunction >( dlsym( mHandle, "CreateWebEnginePlugin" ) );
143 if( mCreateWebEnginePtr == NULL )
145 DALI_LOG_ERROR( "Can't load symbol CreateWebEnginePlugin(), error: %s\n", error );
149 mPlugin = mCreateWebEnginePtr();
151 if( mPlugin == NULL )
153 DALI_LOG_ERROR( "Can't create the WebEnginePlugin object\n" );
157 mDestroyWebEnginePtr = reinterpret_cast< DestroyWebEngineFunction >( dlsym( mHandle, "DestroyWebEnginePlugin" ) );
159 if( mDestroyWebEnginePtr == NULL )
162 DALI_LOG_ERROR( "Can't load symbol DestroyWebEnginePlugin(), error: %s\n", error );
169 void WebEngine::Create( int width, int height, const std::string& locale, const std::string& timezoneId )
171 if( mPlugin != NULL )
173 mPlugin->Create( width, height, locale, timezoneId );
177 void WebEngine::Destroy()
179 if( mPlugin != NULL )
185 Dali::NativeImageInterfacePtr WebEngine::GetNativeImageSource()
187 return mPlugin->GetNativeImageSource();
190 void WebEngine::LoadUrl( const std::string& url )
192 if( mPlugin != NULL )
194 mPlugin->LoadUrl( url );
198 const std::string& WebEngine::GetUrl()
200 static std::string emptyUrl;
201 return mPlugin ? mPlugin->GetUrl() : emptyUrl;
204 void WebEngine::LoadHTMLString( const std::string& htmlString )
206 if( mPlugin != NULL )
208 mPlugin->LoadHTMLString( htmlString );
212 void WebEngine::Reload()
214 if( mPlugin != NULL )
220 void WebEngine::StopLoading()
222 if( mPlugin != NULL )
224 mPlugin->StopLoading();
228 bool WebEngine::CanGoForward()
230 return mPlugin ? mPlugin->CanGoForward() : false;
233 void WebEngine::GoForward()
235 if( mPlugin != NULL )
237 mPlugin->GoForward();
241 bool WebEngine::CanGoBack()
243 return mPlugin ? mPlugin->CanGoBack() : false;
246 void WebEngine::GoBack()
248 if( mPlugin != NULL )
254 void WebEngine::EvaluateJavaScript( const std::string& script )
256 if( mPlugin != NULL )
258 mPlugin->EvaluateJavaScript( script );
262 void WebEngine::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void(const std::string&) > handler )
264 if( mPlugin != NULL )
266 mPlugin->AddJavaScriptMessageHandler( exposedObjectName, handler );
270 void WebEngine::ClearHistory()
272 if( mPlugin != NULL )
274 mPlugin->ClearHistory();
278 void WebEngine::ClearCache()
280 if( mPlugin != NULL )
282 mPlugin->ClearCache();
286 void WebEngine::SetSize( int width, int height )
288 if( mPlugin != NULL )
290 mPlugin->SetSize( width, height );
294 bool WebEngine::SendTouchEvent( const Dali::TouchData& touch )
296 if( mPlugin != NULL )
298 return mPlugin->SendTouchEvent( touch );
304 bool WebEngine::SendKeyEvent( const Dali::KeyEvent& event )
306 if( mPlugin != NULL )
308 return mPlugin->SendKeyEvent( event );
314 Dali::WebEnginePlugin::WebEngineSignalType& WebEngine::PageLoadStartedSignal()
316 return mPlugin->PageLoadStartedSignal();
319 Dali::WebEnginePlugin::WebEngineSignalType& WebEngine::PageLoadFinishedSignal()
321 return mPlugin->PageLoadFinishedSignal();
324 } // namespace Adaptor;
325 } // namespace Internal;