Revert "[Tizen] Support web engine selection using environment variable"
[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/adaptor-framework/native-image-source.h>
25 #include <dali/public-api/object/type-registry.h>
26
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32
33 namespace Adaptor
34 {
35
36 namespace // unnamed namespace
37 {
38
39 const char* WEB_ENGINE_PLUGIN_SO( "libdali-web-engine-plugin.so" );
40
41 Dali::BaseHandle Create()
42 {
43   return Dali::WebEngine::New();
44 }
45
46 Dali::TypeRegistration type( typeid( Dali::WebEngine ), typeid( Dali::BaseHandle ), Create );
47
48 } // unnamed namespace
49
50 WebEnginePtr WebEngine::New()
51 {
52   WebEnginePtr ptr;
53   WebEngine* engine = new WebEngine();
54
55   if ( !engine->Initialize() )
56   {
57     delete engine;
58     engine = nullptr;
59   }
60
61   ptr = engine;
62   return ptr;
63 }
64
65 WebEngine::WebEngine()
66 : mPlugin( NULL ),
67   mHandle( NULL ),
68   mCreateWebEnginePtr( NULL ),
69   mDestroyWebEnginePtr( NULL )
70 {
71 }
72
73 WebEngine::~WebEngine()
74 {
75   if( mHandle != NULL )
76   {
77     if( mDestroyWebEnginePtr != NULL )
78     {
79       mPlugin->Destroy();
80       mDestroyWebEnginePtr( mPlugin );
81     }
82
83     dlclose( mHandle );
84   }
85 }
86
87 bool WebEngine::Initialize()
88 {
89   char* error = NULL;
90
91   mHandle = dlopen( WEB_ENGINE_PLUGIN_SO, RTLD_LAZY );
92
93   error = dlerror();
94   if( mHandle == NULL || error != NULL )
95   {
96     DALI_LOG_ERROR( "WebEngine::Initialize(), dlopen error: %s\n", error );
97     return false;
98   }
99
100   mCreateWebEnginePtr = reinterpret_cast< CreateWebEngineFunction >( dlsym( mHandle, "CreateWebEnginePlugin" ) );
101   if( mCreateWebEnginePtr == NULL )
102   {
103     DALI_LOG_ERROR( "Can't load symbol CreateWebEnginePlugin(), error: %s\n", error );
104     return false;
105   }
106
107   mPlugin = mCreateWebEnginePtr();
108
109   if( mPlugin == NULL )
110   {
111     DALI_LOG_ERROR( "Can't create the WebEnginePlugin object\n" );
112     return false;
113   }
114
115   mDestroyWebEnginePtr = reinterpret_cast< DestroyWebEngineFunction >( dlsym( mHandle, "DestroyWebEnginePlugin" ) );
116
117   if( mDestroyWebEnginePtr == NULL )
118   {
119
120     DALI_LOG_ERROR( "Can't load symbol DestroyWebEnginePlugin(), error: %s\n", error );
121     return false;
122   }
123
124   return true;
125 }
126
127 void WebEngine::Create( int width, int height, const std::string& locale, const std::string& timezoneId )
128 {
129   if( mPlugin != NULL )
130   {
131     mPlugin->Create( width, height, locale, timezoneId );
132   }
133 }
134
135 void WebEngine::Destroy()
136 {
137   if( mPlugin != NULL )
138   {
139     mPlugin->Destroy();
140   }
141 }
142
143 Dali::NativeImageInterfacePtr WebEngine::GetNativeImageSource()
144 {
145   return mPlugin->GetNativeImageSource();
146 }
147
148 void WebEngine::LoadUrl( const std::string& url )
149 {
150   if( mPlugin != NULL )
151   {
152     mPlugin->LoadUrl( url );
153   }
154 }
155
156 const std::string& WebEngine::GetUrl()
157 {
158   static std::string emptyUrl;
159   return mPlugin ? mPlugin->GetUrl() : emptyUrl;
160 }
161
162 void WebEngine::LoadHTMLString( const std::string& htmlString )
163 {
164   if( mPlugin != NULL )
165   {
166     mPlugin->LoadHTMLString( htmlString );
167   }
168 }
169
170 void WebEngine::Reload()
171 {
172   if( mPlugin != NULL )
173   {
174     mPlugin->Reload();
175   }
176 }
177
178 void WebEngine::StopLoading()
179 {
180   if( mPlugin != NULL )
181   {
182     mPlugin->StopLoading();
183   }
184 }
185
186 bool WebEngine::CanGoForward()
187 {
188   return mPlugin ? mPlugin->CanGoForward() : false;
189 }
190
191 void WebEngine::GoForward()
192 {
193   if( mPlugin != NULL )
194   {
195     mPlugin->GoForward();
196   }
197 }
198
199 bool WebEngine::CanGoBack()
200 {
201   return mPlugin ? mPlugin->CanGoBack() : false;
202 }
203
204 void WebEngine::GoBack()
205 {
206   if( mPlugin != NULL )
207   {
208     mPlugin->GoBack();
209   }
210 }
211
212 void WebEngine::EvaluateJavaScript( const std::string& script )
213 {
214   if( mPlugin != NULL )
215   {
216     mPlugin->EvaluateJavaScript( script );
217   }
218 }
219
220 void WebEngine::AddJavaScriptInterface( const std::string& exposedObjectName, const std::string& jsFunctionName, std::function< std::string(const std::string&) > cb )
221 {
222   if( mPlugin != NULL )
223   {
224     mPlugin->AddJavaScriptInterface( exposedObjectName, jsFunctionName, cb );
225   }
226 }
227
228 void WebEngine::RemoveJavascriptInterface( const std::string& exposedObjectName, const std::string& jsFunctionName )
229 {
230   if( mPlugin != NULL )
231   {
232     mPlugin->RemoveJavascriptInterface( exposedObjectName, jsFunctionName );
233   }
234 }
235
236 void WebEngine::ClearHistory()
237 {
238   if( mPlugin != NULL )
239   {
240     mPlugin->ClearHistory();
241   }
242 }
243
244 void WebEngine::ClearCache()
245 {
246   if( mPlugin != NULL )
247   {
248     mPlugin->ClearCache();
249   }
250 }
251
252 void WebEngine::SetSize( int width, int height )
253 {
254   if( mPlugin != NULL )
255   {
256     mPlugin->SetSize( width, height );
257   }
258 }
259
260 bool WebEngine::SendTouchEvent( const Dali::TouchData& touch )
261 {
262   if( mPlugin != NULL )
263   {
264     return mPlugin->SendTouchEvent( touch );
265   }
266
267   return false;
268 }
269
270 bool WebEngine::SendKeyEvent( const Dali::KeyEvent& event )
271 {
272   if( mPlugin != NULL )
273   {
274     return mPlugin->SendKeyEvent( event );
275   }
276
277   return false;
278 }
279
280 Dali::WebEnginePlugin::WebEngineSignalType& WebEngine::PageLoadStartedSignal()
281 {
282   return mPlugin->PageLoadStartedSignal();
283 }
284
285 Dali::WebEnginePlugin::WebEngineSignalType& WebEngine::PageLoadFinishedSignal()
286 {
287   return mPlugin->PageLoadFinishedSignal();
288 }
289
290 } // namespace Adaptor;
291 } // namespace Internal;
292 } // namespace Dali;
293