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