Change WebEngine API
[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/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 = "libdali-web-engine-";
45 constexpr char const * const kPluginFullNamePostfix = "-plugin.so";
46 constexpr char const * const kPluginFullNameDefault = "libdali-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   WebEnginePtr ptr;
71   WebEngine* engine = new WebEngine();
72
73   if ( !engine->Initialize() )
74   {
75     delete engine;
76     engine = nullptr;
77   }
78
79   ptr = engine;
80   return ptr;
81 }
82
83 WebEngine::WebEngine()
84 : mPlugin( NULL ),
85   mHandle( NULL ),
86   mCreateWebEnginePtr( NULL ),
87   mDestroyWebEnginePtr( NULL )
88 {
89 }
90
91 WebEngine::~WebEngine()
92 {
93   if( mHandle != NULL )
94   {
95     if( mDestroyWebEnginePtr != NULL )
96     {
97       mPlugin->Destroy();
98       mDestroyWebEnginePtr( mPlugin );
99     }
100
101     dlclose( mHandle );
102   }
103 }
104
105 bool WebEngine::InitializePluginHandle()
106 {
107   if ( pluginName.length() == 0 )
108   {
109     // pluginName is not initialized yet.
110     const char* name = EnvironmentVariable::GetEnvironmentVariable( DALI_ENV_WEB_ENGINE_NAME );
111     if ( name )
112     {
113       pluginName = MakePluginName( name );
114       mHandle = dlopen( pluginName.c_str(), RTLD_LAZY );
115       if ( mHandle )
116       {
117         return true;
118       }
119     }
120     pluginName = std::string( kPluginFullNameDefault );
121   }
122
123   mHandle = dlopen( pluginName.c_str(), RTLD_LAZY );
124   if ( !mHandle )
125   {
126     DALI_LOG_ERROR( "Can't load %s : %s\n", pluginName.c_str(), dlerror() );
127     return false;
128   }
129
130   return true;
131 }
132
133 bool WebEngine::Initialize()
134 {
135   char* error = NULL;
136
137   if ( !InitializePluginHandle() )
138   {
139     return false;
140   }
141
142   mCreateWebEnginePtr = reinterpret_cast< CreateWebEngineFunction >( dlsym( mHandle, "CreateWebEnginePlugin" ) );
143   if( mCreateWebEnginePtr == NULL )
144   {
145     DALI_LOG_ERROR( "Can't load symbol CreateWebEnginePlugin(), error: %s\n", error );
146     return false;
147   }
148
149   mPlugin = mCreateWebEnginePtr();
150
151   if( mPlugin == NULL )
152   {
153     DALI_LOG_ERROR( "Can't create the WebEnginePlugin object\n" );
154     return false;
155   }
156
157   mDestroyWebEnginePtr = reinterpret_cast< DestroyWebEngineFunction >( dlsym( mHandle, "DestroyWebEnginePlugin" ) );
158
159   if( mDestroyWebEnginePtr == NULL )
160   {
161
162     DALI_LOG_ERROR( "Can't load symbol DestroyWebEnginePlugin(), error: %s\n", error );
163     return false;
164   }
165
166   return true;
167 }
168
169 void WebEngine::Create( int width, int height, const std::string& locale, const std::string& timezoneId )
170 {
171   if( mPlugin != NULL )
172   {
173     mPlugin->Create( width, height, locale, timezoneId );
174   }
175 }
176
177 void WebEngine::Destroy()
178 {
179   if( mPlugin != NULL )
180   {
181     mPlugin->Destroy();
182   }
183 }
184
185 Dali::NativeImageInterfacePtr WebEngine::GetNativeImageSource()
186 {
187   return mPlugin->GetNativeImageSource();
188 }
189
190 void WebEngine::LoadUrl( const std::string& url )
191 {
192   if( mPlugin != NULL )
193   {
194     mPlugin->LoadUrl( url );
195   }
196 }
197
198 const std::string& WebEngine::GetUrl()
199 {
200   static std::string emptyUrl;
201   return mPlugin ? mPlugin->GetUrl() : emptyUrl;
202 }
203
204 void WebEngine::LoadHTMLString( const std::string& htmlString )
205 {
206   if( mPlugin != NULL )
207   {
208     mPlugin->LoadHTMLString( htmlString );
209   }
210 }
211
212 void WebEngine::Reload()
213 {
214   if( mPlugin != NULL )
215   {
216     mPlugin->Reload();
217   }
218 }
219
220 void WebEngine::StopLoading()
221 {
222   if( mPlugin != NULL )
223   {
224     mPlugin->StopLoading();
225   }
226 }
227
228 bool WebEngine::CanGoForward()
229 {
230   return mPlugin ? mPlugin->CanGoForward() : false;
231 }
232
233 void WebEngine::GoForward()
234 {
235   if( mPlugin != NULL )
236   {
237     mPlugin->GoForward();
238   }
239 }
240
241 bool WebEngine::CanGoBack()
242 {
243   return mPlugin ? mPlugin->CanGoBack() : false;
244 }
245
246 void WebEngine::GoBack()
247 {
248   if( mPlugin != NULL )
249   {
250     mPlugin->GoBack();
251   }
252 }
253
254 void WebEngine::EvaluateJavaScript( const std::string& script )
255 {
256   if( mPlugin != NULL )
257   {
258     mPlugin->EvaluateJavaScript( script );
259   }
260 }
261
262 void WebEngine::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void(const std::string&) > handler )
263 {
264   if( mPlugin != NULL )
265   {
266     mPlugin->AddJavaScriptMessageHandler( exposedObjectName, handler );
267   }
268 }
269
270 void WebEngine::ClearHistory()
271 {
272   if( mPlugin != NULL )
273   {
274     mPlugin->ClearHistory();
275   }
276 }
277
278 void WebEngine::ClearCache()
279 {
280   if( mPlugin != NULL )
281   {
282     mPlugin->ClearCache();
283   }
284 }
285
286 void WebEngine::SetSize( int width, int height )
287 {
288   if( mPlugin != NULL )
289   {
290     mPlugin->SetSize( width, height );
291   }
292 }
293
294 bool WebEngine::SendTouchEvent( const Dali::TouchData& touch )
295 {
296   if( mPlugin != NULL )
297   {
298     return mPlugin->SendTouchEvent( touch );
299   }
300
301   return false;
302 }
303
304 bool WebEngine::SendKeyEvent( const Dali::KeyEvent& event )
305 {
306   if( mPlugin != NULL )
307   {
308     return mPlugin->SendKeyEvent( event );
309   }
310
311   return false;
312 }
313
314 Dali::WebEnginePlugin::WebEngineSignalType& WebEngine::PageLoadStartedSignal()
315 {
316   return mPlugin->PageLoadStartedSignal();
317 }
318
319 Dali::WebEnginePlugin::WebEngineSignalType& WebEngine::PageLoadFinishedSignal()
320 {
321   return mPlugin->PageLoadFinishedSignal();
322 }
323
324 } // namespace Adaptor;
325 } // namespace Internal;
326 } // namespace Dali;
327