Support scroll in 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/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 void WebEngine::ScrollBy( int deltaX, int deltaY )
217 {
218   mPlugin->ScrollBy( deltaX, deltaY );
219 }
220
221 void WebEngine::SetScrollPosition( int x, int y )
222 {
223   mPlugin->SetScrollPosition( x, y );
224 }
225
226 void WebEngine::GetScrollPosition( int& x, int& y ) const
227 {
228   mPlugin->GetScrollPosition( x, y );
229 }
230
231 void WebEngine::GetScrollSize( int& width, int& height ) const
232 {
233   mPlugin->GetScrollSize( width, height );
234 }
235
236 void WebEngine::GetContentSize( int& width, int& height ) const
237 {
238   mPlugin->GetContentSize( width, height );
239 }
240
241 bool WebEngine::CanGoForward()
242 {
243   return mPlugin->CanGoForward();
244 }
245
246 void WebEngine::GoForward()
247 {
248   mPlugin->GoForward();
249 }
250
251 bool WebEngine::CanGoBack()
252 {
253   return mPlugin->CanGoBack();
254 }
255
256 void WebEngine::GoBack()
257 {
258   mPlugin->GoBack();
259 }
260
261 void WebEngine::EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
262 {
263   mPlugin->EvaluateJavaScript( script, resultHandler );
264 }
265
266 void WebEngine::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void(const std::string&) > handler )
267 {
268   mPlugin->AddJavaScriptMessageHandler( exposedObjectName, handler );
269 }
270
271 void WebEngine::ClearHistory()
272 {
273   mPlugin->ClearHistory();
274 }
275
276 void WebEngine::ClearCache()
277 {
278   mPlugin->ClearCache();
279 }
280
281 void WebEngine::ClearCookies()
282 {
283   mPlugin->ClearCookies();
284 }
285
286 Dali::WebEnginePlugin::CacheModel WebEngine::GetCacheModel() const
287 {
288   return mPlugin->GetCacheModel();
289 }
290
291 void WebEngine::SetCacheModel( Dali::WebEnginePlugin::CacheModel cacheModel )
292 {
293   mPlugin->SetCacheModel( cacheModel );
294 }
295
296 Dali::WebEnginePlugin::CookieAcceptPolicy WebEngine::GetCookieAcceptPolicy() const
297 {
298   return mPlugin->GetCookieAcceptPolicy();
299 }
300
301 void WebEngine::SetCookieAcceptPolicy( Dali::WebEnginePlugin::CookieAcceptPolicy policy )
302 {
303   mPlugin->SetCookieAcceptPolicy( policy );
304 }
305
306 const std::string& WebEngine::GetUserAgent() const
307 {
308   return mPlugin->GetUserAgent();
309 }
310
311 void WebEngine::SetUserAgent( const std::string& userAgent )
312 {
313   mPlugin->SetUserAgent( userAgent );
314 }
315
316 bool WebEngine::IsJavaScriptEnabled() const
317 {
318   return mPlugin->IsJavaScriptEnabled();
319 }
320
321 void WebEngine::EnableJavaScript( bool enabled )
322 {
323   mPlugin->EnableJavaScript( enabled );
324 }
325
326 bool WebEngine::AreImagesAutomaticallyLoaded() const
327 {
328   return mPlugin->AreImagesAutomaticallyLoaded();
329 }
330
331 void WebEngine::LoadImagesAutomatically( bool automatic )
332 {
333   mPlugin->LoadImagesAutomatically( automatic );
334 }
335
336 const std::string& WebEngine::GetDefaultTextEncodingName() const
337 {
338   return mPlugin->GetDefaultTextEncodingName();
339 }
340
341 void WebEngine::SetDefaultTextEncodingName( const std::string& defaultTextEncodingName )
342 {
343   mPlugin->SetDefaultTextEncodingName( defaultTextEncodingName );
344 }
345
346 int WebEngine::GetDefaultFontSize() const
347 {
348   return mPlugin->GetDefaultFontSize();
349 }
350
351 void WebEngine::SetDefaultFontSize( int defaultFontSize )
352 {
353   mPlugin->SetDefaultFontSize( defaultFontSize );
354 }
355
356 void WebEngine::SetSize( int width, int height )
357 {
358   mPlugin->SetSize( width, height );
359 }
360
361 bool WebEngine::SendTouchEvent( const Dali::TouchEvent& touch )
362 {
363   return mPlugin->SendTouchEvent( touch );
364 }
365
366 bool WebEngine::SendKeyEvent( const Dali::KeyEvent& event )
367 {
368   return mPlugin->SendKeyEvent( event );
369 }
370
371 void WebEngine::SetFocus( bool focused )
372 {
373   mPlugin->SetFocus( focused );
374 }
375
376 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadStartedSignal()
377 {
378   return mPlugin->PageLoadStartedSignal();
379 }
380
381 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadFinishedSignal()
382 {
383   return mPlugin->PageLoadFinishedSignal();
384 }
385
386 Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType& WebEngine::PageLoadErrorSignal()
387 {
388   return mPlugin->PageLoadErrorSignal();
389 }
390
391 Dali::WebEnginePlugin::WebEngineScrollEdgeReachedSignalType& WebEngine::ScrollEdgeReachedSignal()
392 {
393   return mPlugin->ScrollEdgeReachedSignal();
394 }
395
396 } // namespace Adaptor;
397 } // namespace Internal;
398 } // namespace Dali;
399
400