8e2d55d4e9b7f1e83626000104a24820f4466347
[platform/core/uifw/dali-adaptor.git] / dali / internal / web-engine / common / web-engine-impl.cpp
1 /*
2  * Copyright (c) 2020 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/object/type-registry.h>
25 #include <sstream>
26
27 // INTERNAL INCLUDES
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>
31
32 namespace Dali
33 {
34
35 namespace Internal
36 {
37
38 namespace Adaptor
39 {
40
41 namespace // unnamed namespace
42 {
43
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";
47
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;
51
52 std::string MakePluginName( const char* environmentName )
53 {
54   std::stringstream fullName;
55   fullName << kPluginFullNamePrefix << environmentName << kPluginFullNamePostfix;
56   return std::move( fullName.str() );
57 }
58
59 Dali::BaseHandle Create()
60 {
61   return Dali::WebEngine::New();
62 }
63
64 Dali::TypeRegistration type( typeid( Dali::WebEngine ), typeid( Dali::BaseHandle ), Create );
65
66 } // unnamed namespace
67
68 WebEnginePtr WebEngine::New()
69 {
70   WebEngine* instance = new WebEngine();
71
72   if( !instance->Initialize() )
73   {
74     delete instance;
75     return nullptr;
76   }
77
78   return instance;
79 }
80
81 WebEngine::WebEngine()
82 : mPlugin( NULL ),
83   mHandle( NULL ),
84   mCreateWebEnginePtr( NULL ),
85   mDestroyWebEnginePtr( NULL )
86 {
87 }
88
89 WebEngine::~WebEngine()
90 {
91   if( mHandle != NULL )
92   {
93     if( mDestroyWebEnginePtr != NULL )
94     {
95       mPlugin->Destroy();
96       mDestroyWebEnginePtr( mPlugin );
97     }
98
99     dlclose( mHandle );
100   }
101 }
102
103 bool WebEngine::InitializePluginHandle()
104 {
105   if( pluginName.length() == 0 )
106   {
107     // pluginName is not initialized yet.
108     const char* name = EnvironmentVariable::GetEnvironmentVariable( DALI_ENV_WEB_ENGINE_NAME );
109     if( name )
110     {
111       pluginName = MakePluginName( name );
112       mHandle = dlopen( pluginName.c_str(), RTLD_LAZY );
113       if( mHandle )
114       {
115         return true;
116       }
117     }
118     pluginName = std::string( kPluginFullNameDefault );
119   }
120
121   mHandle = dlopen( pluginName.c_str(), RTLD_LAZY );
122   if( !mHandle )
123   {
124     DALI_LOG_ERROR( "Can't load %s : %s\n", pluginName.c_str(), dlerror() );
125     return false;
126   }
127
128   return true;
129 }
130
131 bool WebEngine::Initialize()
132 {
133   char* error = NULL;
134
135   if( !InitializePluginHandle() )
136   {
137     return false;
138   }
139
140   mCreateWebEnginePtr = reinterpret_cast< CreateWebEngineFunction >( dlsym( mHandle, "CreateWebEnginePlugin" ) );
141   if( mCreateWebEnginePtr == NULL )
142   {
143     DALI_LOG_ERROR( "Can't load symbol CreateWebEnginePlugin(), error: %s\n", error );
144     return false;
145   }
146
147   mDestroyWebEnginePtr = reinterpret_cast< DestroyWebEngineFunction >( dlsym( mHandle, "DestroyWebEnginePlugin" ) );
148
149   if( mDestroyWebEnginePtr == NULL )
150   {
151     DALI_LOG_ERROR( "Can't load symbol DestroyWebEnginePlugin(), error: %s\n", error );
152     return false;
153   }
154
155   mPlugin = mCreateWebEnginePtr();
156
157   if( mPlugin == NULL )
158   {
159     DALI_LOG_ERROR( "Can't create the WebEnginePlugin object\n" );
160     return false;
161   }
162
163   return true;
164 }
165
166 void WebEngine::Create( int width, int height, const std::string& locale, const std::string& timezoneId )
167 {
168   mPlugin->Create( width, height, locale, timezoneId );
169 }
170
171 void WebEngine::Destroy()
172 {
173   mPlugin->Destroy();
174 }
175
176 Dali::NativeImageInterfacePtr WebEngine::GetNativeImageSource()
177 {
178   return mPlugin->GetNativeImageSource();
179 }
180
181 void WebEngine::LoadUrl( const std::string& url )
182 {
183   mPlugin->LoadUrl( url );
184 }
185
186 const std::string& WebEngine::GetUrl()
187 {
188   return mPlugin->GetUrl();
189 }
190
191 void WebEngine::LoadHTMLString( const std::string& htmlString )
192 {
193   mPlugin->LoadHTMLString( htmlString );
194 }
195
196 void WebEngine::Reload()
197 {
198   mPlugin->Reload();
199 }
200
201 void WebEngine::StopLoading()
202 {
203   mPlugin->StopLoading();
204 }
205
206 void WebEngine::Suspend()
207 {
208   mPlugin->Suspend();
209 }
210
211 void WebEngine::Resume()
212 {
213   mPlugin->Resume();
214 }
215
216 bool WebEngine::CanGoForward()
217 {
218   return mPlugin->CanGoForward();
219 }
220
221 void WebEngine::GoForward()
222 {
223   mPlugin->GoForward();
224 }
225
226 bool WebEngine::CanGoBack()
227 {
228   return mPlugin->CanGoBack();
229 }
230
231 void WebEngine::GoBack()
232 {
233   mPlugin->GoBack();
234 }
235
236 void WebEngine::EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
237 {
238   mPlugin->EvaluateJavaScript( script, resultHandler );
239 }
240
241 void WebEngine::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void(const std::string&) > handler )
242 {
243   mPlugin->AddJavaScriptMessageHandler( exposedObjectName, handler );
244 }
245
246 void WebEngine::ClearHistory()
247 {
248   mPlugin->ClearHistory();
249 }
250
251 void WebEngine::ClearCache()
252 {
253   mPlugin->ClearCache();
254 }
255
256 void WebEngine::ClearCookies()
257 {
258   mPlugin->ClearCookies();
259 }
260
261 Dali::WebEnginePlugin::CacheModel WebEngine::GetCacheModel() const
262 {
263   return mPlugin->GetCacheModel();
264 }
265
266 void WebEngine::SetCacheModel( Dali::WebEnginePlugin::CacheModel cacheModel )
267 {
268   mPlugin->SetCacheModel( cacheModel );
269 }
270
271 Dali::WebEnginePlugin::CookieAcceptPolicy WebEngine::GetCookieAcceptPolicy() const
272 {
273   return mPlugin->GetCookieAcceptPolicy();
274 }
275
276 void WebEngine::SetCookieAcceptPolicy( Dali::WebEnginePlugin::CookieAcceptPolicy policy )
277 {
278   mPlugin->SetCookieAcceptPolicy( policy );
279 }
280
281 const std::string& WebEngine::GetUserAgent() const
282 {
283   return mPlugin->GetUserAgent();
284 }
285
286 void WebEngine::SetUserAgent( const std::string& userAgent )
287 {
288   mPlugin->SetUserAgent( userAgent );
289 }
290
291 bool WebEngine::IsJavaScriptEnabled() const
292 {
293   return mPlugin->IsJavaScriptEnabled();
294 }
295
296 void WebEngine::EnableJavaScript( bool enabled )
297 {
298   mPlugin->EnableJavaScript( enabled );
299 }
300
301 bool WebEngine::AreImagesAutomaticallyLoaded() const
302 {
303   return mPlugin->AreImagesAutomaticallyLoaded();
304 }
305
306 void WebEngine::LoadImagesAutomatically( bool automatic )
307 {
308   mPlugin->LoadImagesAutomatically( automatic );
309 }
310
311 const std::string& WebEngine::GetDefaultTextEncodingName() const
312 {
313   return mPlugin->GetDefaultTextEncodingName();
314 }
315
316 void WebEngine::SetDefaultTextEncodingName( const std::string& defaultTextEncodingName )
317 {
318   mPlugin->SetDefaultTextEncodingName( defaultTextEncodingName );
319 }
320
321 int WebEngine::GetDefaultFontSize() const
322 {
323   return mPlugin->GetDefaultFontSize();
324 }
325
326 void WebEngine::SetDefaultFontSize( int defaultFontSize )
327 {
328   mPlugin->SetDefaultFontSize( defaultFontSize );
329 }
330
331 void WebEngine::SetSize( int width, int height )
332 {
333   mPlugin->SetSize( width, height );
334 }
335
336 bool WebEngine::SendTouchEvent( const Dali::TouchEvent& touch )
337 {
338   return mPlugin->SendTouchEvent( touch );
339 }
340
341 bool WebEngine::SendKeyEvent( const Dali::KeyEvent& event )
342 {
343   return mPlugin->SendKeyEvent( event );
344 }
345
346 void WebEngine::SetFocus( bool focused )
347 {
348   mPlugin->SetFocus( focused );
349 }
350
351 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadStartedSignal()
352 {
353   return mPlugin->PageLoadStartedSignal();
354 }
355
356 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadFinishedSignal()
357 {
358   return mPlugin->PageLoadFinishedSignal();
359 }
360
361 Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType& WebEngine::PageLoadErrorSignal()
362 {
363   return mPlugin->PageLoadErrorSignal();
364 }
365
366 } // namespace Adaptor;
367 } // namespace Internal;
368 } // namespace Dali;
369
370