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