[Tizen] Support web engine selection using environment variable
[platform/core/uifw/dali-adaptor.git] / dali / internal / web-engine / common / web-engine-impl.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/web-engine/common/web-engine-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dlfcn.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/public-api/adaptor-framework/native-image-source.h>
25 #include <dali/public-api/object/type-registry.h>
26 #include <string.h>
27
28 // INTERNAL INCLUDES
29 #include <dali/devel-api/adaptor-framework/environment-variable.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 namespace Adaptor
38 {
39
40 namespace // unnamed namespace
41 {
42
43 #define DALI_ENV_WEB_ENGINE_NAME "DALI_WEB_ENGINE_NAME"
44
45 const char* WEB_ENGINE_CHROMIUM_NAME( "chromium" );
46 const char* WEB_ENGINE_LITE_NAME( "lightweight" );
47
48 const char* WEB_ENGINE_PLUGIN_SO( "libdali-web-engine-plugin.so" );
49 const char* WEB_ENGINE_CHROMIUM_PLUGIN_SO( "libdali-web-engine-chromium-plugin.so" );
50 const char* WEB_ENGINE_LITE_PLUGIN_SO( "libdali-web-engine-lite-plugin.so" );
51
52 Dali::BaseHandle Create()
53 {
54   return Dali::WebEngine::New();
55 }
56
57 Dali::TypeRegistration type( typeid( Dali::WebEngine ), typeid( Dali::BaseHandle ), Create );
58
59 } // unnamed namespace
60
61 WebEnginePtr WebEngine::New()
62 {
63   WebEnginePtr ptr;
64   WebEngine* engine = new WebEngine();
65
66   if ( !engine->Initialize() )
67   {
68     delete engine;
69     engine = nullptr;
70   }
71
72   ptr = engine;
73   return ptr;
74 }
75
76 WebEngine::WebEngine()
77 : mPlugin( NULL ),
78   mHandle( NULL ),
79   mCreateWebEnginePtr( NULL ),
80   mDestroyWebEnginePtr( NULL )
81 {
82 }
83
84 WebEngine::~WebEngine()
85 {
86   if( mHandle != NULL )
87   {
88     if( mDestroyWebEnginePtr != NULL )
89     {
90       mPlugin->Destroy();
91       mDestroyWebEnginePtr( mPlugin );
92     }
93
94     dlclose( mHandle );
95   }
96 }
97
98 bool WebEngine::Initialize()
99 {
100   char* error = NULL;
101
102   const char* engineName = WEB_ENGINE_PLUGIN_SO;
103   const char* engineEnvName = EnvironmentVariable::GetEnvironmentVariable( DALI_ENV_WEB_ENGINE_NAME );
104
105   if ( engineEnvName )
106   {
107     if( 0 == strcmp( engineEnvName, WEB_ENGINE_CHROMIUM_NAME ) )
108     {
109       engineName = WEB_ENGINE_CHROMIUM_PLUGIN_SO;
110     }
111     else if( 0 == strcmp( engineEnvName, WEB_ENGINE_LITE_NAME ) )
112     {
113       engineName = WEB_ENGINE_LITE_PLUGIN_SO;
114     }
115   }
116
117   mHandle = dlopen( engineName, RTLD_LAZY );
118
119   error = dlerror();
120   if( mHandle == NULL || error != NULL )
121   {
122     DALI_LOG_ERROR( "WebEngine::Initialize(), dlopen error: %s\n", error );
123     return false;
124   }
125
126   mCreateWebEnginePtr = reinterpret_cast< CreateWebEngineFunction >( dlsym( mHandle, "CreateWebEnginePlugin" ) );
127   if( mCreateWebEnginePtr == NULL )
128   {
129     DALI_LOG_ERROR( "Can't load symbol CreateWebEnginePlugin(), error: %s\n", error );
130     return false;
131   }
132
133   mPlugin = mCreateWebEnginePtr();
134
135   if( mPlugin == NULL )
136   {
137     DALI_LOG_ERROR( "Can't create the WebEnginePlugin object\n" );
138     return false;
139   }
140
141   mDestroyWebEnginePtr = reinterpret_cast< DestroyWebEngineFunction >( dlsym( mHandle, "DestroyWebEnginePlugin" ) );
142
143   if( mDestroyWebEnginePtr == NULL )
144   {
145
146     DALI_LOG_ERROR( "Can't load symbol DestroyWebEnginePlugin(), error: %s\n", error );
147     return false;
148   }
149
150   return true;
151 }
152
153 void WebEngine::Create( int width, int height, const std::string& locale, const std::string& timezoneId )
154 {
155   if( mPlugin != NULL )
156   {
157     mPlugin->Create( width, height, locale, timezoneId );
158   }
159 }
160
161 void WebEngine::Destroy()
162 {
163   if( mPlugin != NULL )
164   {
165     mPlugin->Destroy();
166   }
167 }
168
169 Dali::NativeImageInterfacePtr WebEngine::GetNativeImageSource()
170 {
171   return mPlugin->GetNativeImageSource();
172 }
173
174 void WebEngine::LoadUrl( const std::string& url )
175 {
176   if( mPlugin != NULL )
177   {
178     mPlugin->LoadUrl( url );
179   }
180 }
181
182 const std::string& WebEngine::GetUrl()
183 {
184   static std::string emptyUrl;
185   return mPlugin ? mPlugin->GetUrl() : emptyUrl;
186 }
187
188 void WebEngine::LoadHTMLString( const std::string& htmlString )
189 {
190   if( mPlugin != NULL )
191   {
192     mPlugin->LoadHTMLString( htmlString );
193   }
194 }
195
196 void WebEngine::Reload()
197 {
198   if( mPlugin != NULL )
199   {
200     mPlugin->Reload();
201   }
202 }
203
204 void WebEngine::StopLoading()
205 {
206   if( mPlugin != NULL )
207   {
208     mPlugin->StopLoading();
209   }
210 }
211
212 bool WebEngine::CanGoForward()
213 {
214   return mPlugin ? mPlugin->CanGoForward() : false;
215 }
216
217 void WebEngine::GoForward()
218 {
219   if( mPlugin != NULL )
220   {
221     mPlugin->GoForward();
222   }
223 }
224
225 bool WebEngine::CanGoBack()
226 {
227   return mPlugin ? mPlugin->CanGoBack() : false;
228 }
229
230 void WebEngine::GoBack()
231 {
232   if( mPlugin != NULL )
233   {
234     mPlugin->GoBack();
235   }
236 }
237
238 void WebEngine::EvaluateJavaScript( const std::string& script )
239 {
240   if( mPlugin != NULL )
241   {
242     mPlugin->EvaluateJavaScript( script );
243   }
244 }
245
246 void WebEngine::AddJavaScriptInterface( const std::string& exposedObjectName, const std::string& jsFunctionName, std::function< std::string(const std::string&) > cb )
247 {
248   if( mPlugin != NULL )
249   {
250     mPlugin->AddJavaScriptInterface( exposedObjectName, jsFunctionName, cb );
251   }
252 }
253
254 void WebEngine::RemoveJavascriptInterface( const std::string& exposedObjectName, const std::string& jsFunctionName )
255 {
256   if( mPlugin != NULL )
257   {
258     mPlugin->RemoveJavascriptInterface( exposedObjectName, jsFunctionName );
259   }
260 }
261
262 void WebEngine::ClearHistory()
263 {
264   if( mPlugin != NULL )
265   {
266     mPlugin->ClearHistory();
267   }
268 }
269
270 void WebEngine::ClearCache()
271 {
272   if( mPlugin != NULL )
273   {
274     mPlugin->ClearCache();
275   }
276 }
277
278 void WebEngine::SetSize( int width, int height )
279 {
280   if( mPlugin != NULL )
281   {
282     mPlugin->SetSize( width, height );
283   }
284 }
285
286 bool WebEngine::SendTouchEvent( const Dali::TouchData& touch )
287 {
288   if( mPlugin != NULL )
289   {
290     return mPlugin->SendTouchEvent( touch );
291   }
292
293   return false;
294 }
295
296 bool WebEngine::SendKeyEvent( const Dali::KeyEvent& event )
297 {
298   if( mPlugin != NULL )
299   {
300     return mPlugin->SendKeyEvent( event );
301   }
302
303   return false;
304 }
305
306 Dali::WebEnginePlugin::WebEngineSignalType& WebEngine::PageLoadStartedSignal()
307 {
308   return mPlugin->PageLoadStartedSignal();
309 }
310
311 Dali::WebEnginePlugin::WebEngineSignalType& WebEngine::PageLoadFinishedSignal()
312 {
313   return mPlugin->PageLoadFinishedSignal();
314 }
315
316 } // namespace Adaptor;
317 } // namespace Internal;
318 } // namespace Dali;
319