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