Add some new APIs into web engine.
[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/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>
35 #include <dali/public-api/images/pixel-data.h>
36
37 namespace Dali
38 {
39
40 namespace Internal
41 {
42
43 namespace Adaptor
44 {
45
46 namespace // unnamed namespace
47 {
48
49 constexpr char const * const kPluginFullNamePrefix = "libdali2-web-engine-";
50 constexpr char const * const kPluginFullNamePostfix = "-plugin.so";
51 constexpr char const * const kPluginFullNameDefault = "libdali2-web-engine-plugin.so";
52
53 // Note: Dali WebView policy does not allow to use multiple web engines in an application.
54 // So once pluginName is set to non-empty string, it will not change.
55 std::string pluginName;
56
57 std::string MakePluginName( const char* environmentName )
58 {
59   std::stringstream fullName;
60   fullName << kPluginFullNamePrefix << environmentName << kPluginFullNamePostfix;
61   return std::move( fullName.str() );
62 }
63
64 Dali::BaseHandle Create()
65 {
66   return Dali::WebEngine::New();
67 }
68
69 Dali::TypeRegistration type( typeid( Dali::WebEngine ), typeid( Dali::BaseHandle ), Create );
70
71 } // unnamed namespace
72
73 WebEnginePtr WebEngine::New()
74 {
75   WebEngine* instance = new WebEngine();
76
77   if( !instance->Initialize() )
78   {
79     delete instance;
80     return nullptr;
81   }
82
83   return instance;
84 }
85
86 WebEngine::WebEngine()
87 : mPlugin( NULL ),
88   mHandle( NULL ),
89   mCreateWebEnginePtr( NULL ),
90   mDestroyWebEnginePtr( NULL )
91 {
92 }
93
94 WebEngine::~WebEngine()
95 {
96   if( mHandle != NULL )
97   {
98     if( mDestroyWebEnginePtr != NULL )
99     {
100       mPlugin->Destroy();
101       mDestroyWebEnginePtr( mPlugin );
102     }
103
104     dlclose( mHandle );
105   }
106 }
107
108 bool WebEngine::InitializePluginHandle()
109 {
110   if( pluginName.length() == 0 )
111   {
112     // pluginName is not initialized yet.
113     const char* name = EnvironmentVariable::GetEnvironmentVariable( DALI_ENV_WEB_ENGINE_NAME );
114     if( name )
115     {
116       pluginName = MakePluginName( name );
117       mHandle = dlopen( pluginName.c_str(), RTLD_LAZY );
118       if( mHandle )
119       {
120         return true;
121       }
122     }
123     pluginName = std::string( kPluginFullNameDefault );
124   }
125
126   mHandle = dlopen( pluginName.c_str(), RTLD_LAZY );
127   if( !mHandle )
128   {
129     DALI_LOG_ERROR( "Can't load %s : %s\n", pluginName.c_str(), dlerror() );
130     return false;
131   }
132
133   return true;
134 }
135
136 bool WebEngine::Initialize()
137 {
138   char* error = NULL;
139
140   if( !InitializePluginHandle() )
141   {
142     return false;
143   }
144
145   mCreateWebEnginePtr = reinterpret_cast< CreateWebEngineFunction >( dlsym( mHandle, "CreateWebEnginePlugin" ) );
146   if( mCreateWebEnginePtr == NULL )
147   {
148     DALI_LOG_ERROR( "Can't load symbol CreateWebEnginePlugin(), error: %s\n", error );
149     return false;
150   }
151
152   mDestroyWebEnginePtr = reinterpret_cast< DestroyWebEngineFunction >( dlsym( mHandle, "DestroyWebEnginePlugin" ) );
153
154   if( mDestroyWebEnginePtr == NULL )
155   {
156     DALI_LOG_ERROR( "Can't load symbol DestroyWebEnginePlugin(), error: %s\n", error );
157     return false;
158   }
159
160   mPlugin = mCreateWebEnginePtr();
161
162   if( mPlugin == NULL )
163   {
164     DALI_LOG_ERROR( "Can't create the WebEnginePlugin object\n" );
165     return false;
166   }
167
168   return true;
169 }
170
171 void WebEngine::Create( int width, int height, const std::string& locale, const std::string& timezoneId )
172 {
173   mPlugin->Create( width, height, locale, timezoneId );
174 }
175
176 void WebEngine::Create( int width, int height, int argc, char** argv )
177 {
178   mPlugin->Create( width, height, argc, argv );
179 }
180
181 void WebEngine::Destroy()
182 {
183   mPlugin->Destroy();
184 }
185
186 Dali::NativeImageInterfacePtr WebEngine::GetNativeImageSource()
187 {
188   return mPlugin->GetNativeImageSource();
189 }
190
191 Dali::WebEngineSettings& WebEngine::GetSettings() const
192 {
193   return mPlugin->GetSettings();
194 }
195
196 Dali::WebEngineContext& WebEngine::GetContext() const
197 {
198   return mPlugin->GetContext();
199 }
200
201 Dali::WebEngineCookieManager& WebEngine::GetCookieManager() const
202 {
203   return mPlugin->GetCookieManager();
204 }
205
206 Dali::WebEngineBackForwardList& WebEngine::GetBackForwardList() const
207 {
208   return mPlugin->GetBackForwardList();
209 }
210
211 void WebEngine::LoadUrl( const std::string& url )
212 {
213   mPlugin->LoadUrl( url );
214 }
215
216 std::string WebEngine::GetTitle() const
217 {
218   return mPlugin->GetTitle();
219 }
220
221 Dali::PixelData WebEngine::GetFavicon() const
222 {
223   return mPlugin->GetFavicon();
224 }
225
226 const std::string& WebEngine::GetUrl()
227 {
228   return mPlugin->GetUrl();
229 }
230
231 const std::string& WebEngine::GetUserAgent() const
232 {
233   return mPlugin->GetUserAgent();
234 }
235
236 void WebEngine::SetUserAgent( const std::string& userAgent )
237 {
238   mPlugin->SetUserAgent( userAgent );
239 }
240
241 void WebEngine::LoadHtmlString( const std::string& htmlString )
242 {
243   mPlugin->LoadHtmlString( htmlString );
244 }
245
246 void WebEngine::Reload()
247 {
248   mPlugin->Reload();
249 }
250
251 void WebEngine::StopLoading()
252 {
253   mPlugin->StopLoading();
254 }
255
256 void WebEngine::Suspend()
257 {
258   mPlugin->Suspend();
259 }
260
261 void WebEngine::Resume()
262 {
263   mPlugin->Resume();
264 }
265
266 void WebEngine::ScrollBy( int deltaX, int deltaY )
267 {
268   mPlugin->ScrollBy( deltaX, deltaY );
269 }
270
271 void WebEngine::SetScrollPosition( int x, int y )
272 {
273   mPlugin->SetScrollPosition( x, y );
274 }
275
276 void WebEngine::GetScrollPosition( int& x, int& y ) const
277 {
278   mPlugin->GetScrollPosition( x, y );
279 }
280
281 void WebEngine::GetScrollSize( int& width, int& height ) const
282 {
283   mPlugin->GetScrollSize( width, height );
284 }
285
286 void WebEngine::GetContentSize( int& width, int& height ) const
287 {
288   mPlugin->GetContentSize( width, height );
289 }
290
291 bool WebEngine::CanGoForward()
292 {
293   return mPlugin->CanGoForward();
294 }
295
296 void WebEngine::GoForward()
297 {
298   mPlugin->GoForward();
299 }
300
301 bool WebEngine::CanGoBack()
302 {
303   return mPlugin->CanGoBack();
304 }
305
306 void WebEngine::GoBack()
307 {
308   mPlugin->GoBack();
309 }
310
311 void WebEngine::EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
312 {
313   mPlugin->EvaluateJavaScript( script, resultHandler );
314 }
315
316 void WebEngine::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void(const std::string&) > handler )
317 {
318   mPlugin->AddJavaScriptMessageHandler( exposedObjectName, handler );
319 }
320
321 void WebEngine::ClearAllTilesResources()
322 {
323   mPlugin->ClearAllTilesResources();
324 }
325
326 void WebEngine::ClearHistory()
327 {
328   mPlugin->ClearHistory();
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 Dali::WebEnginePlugin::WebEngineScrollEdgeReachedSignalType& WebEngine::ScrollEdgeReachedSignal()
367 {
368   return mPlugin->ScrollEdgeReachedSignal();
369 }
370
371 } // namespace Adaptor;
372 } // namespace Internal;
373 } // namespace Dali;
374
375