bd2f0e2285015a8c0262dd26ae5582b7624e1496
[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-console-message.h>
31 #include <dali/devel-api/adaptor-framework/web-engine-context.h>
32 #include <dali/devel-api/adaptor-framework/web-engine-cookie-manager.h>
33 #include <dali/devel-api/adaptor-framework/web-engine-request-interceptor.h>
34 #include <dali/devel-api/adaptor-framework/web-engine-load-error.h>
35 #include <dali/devel-api/adaptor-framework/web-engine-settings.h>
36 #include <dali/internal/system/common/environment-variables.h>
37 #include <dali/public-api/adaptor-framework/native-image-source.h>
38 #include <dali/public-api/images/pixel-data.h>
39
40 namespace Dali
41 {
42 namespace Internal
43 {
44 namespace Adaptor
45 {
46 namespace // unnamed namespace
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::Create(int width, int height, int argc, char** argv)
176 {
177   mPlugin->Create(width, height, argc, argv);
178 }
179
180 void WebEngine::Destroy()
181 {
182   mPlugin->Destroy();
183 }
184
185 Dali::NativeImageInterfacePtr WebEngine::GetNativeImageSource()
186 {
187   return mPlugin->GetNativeImageSource();
188 }
189
190 Dali::WebEngineSettings& WebEngine::GetSettings() const
191 {
192   return mPlugin->GetSettings();
193 }
194
195 Dali::WebEngineContext& WebEngine::GetContext() const
196 {
197   return mPlugin->GetContext();
198 }
199
200 Dali::WebEngineCookieManager& WebEngine::GetCookieManager() const
201 {
202   return mPlugin->GetCookieManager();
203 }
204
205 Dali::WebEngineBackForwardList& WebEngine::GetBackForwardList() const
206 {
207   return mPlugin->GetBackForwardList();
208 }
209
210 void WebEngine::LoadUrl(const std::string& url)
211 {
212   mPlugin->LoadUrl(url);
213 }
214
215 std::string WebEngine::GetTitle() const
216 {
217   return mPlugin->GetTitle();
218 }
219
220 Dali::PixelData WebEngine::GetFavicon() const
221 {
222   return mPlugin->GetFavicon();
223 }
224
225 const std::string& WebEngine::GetUrl()
226 {
227   return mPlugin->GetUrl();
228 }
229
230 const std::string& WebEngine::GetUserAgent() const
231 {
232   return mPlugin->GetUserAgent();
233 }
234
235 void WebEngine::SetUserAgent(const std::string& userAgent)
236 {
237   mPlugin->SetUserAgent(userAgent);
238 }
239
240 void WebEngine::LoadHtmlString(const std::string& htmlString)
241 {
242   mPlugin->LoadHtmlString(htmlString);
243 }
244
245 bool WebEngine::LoadHtmlStringOverrideCurrentEntry(const std::string& html, const std::string& basicUri, const std::string& unreachableUrl)
246 {
247   return mPlugin->LoadHtmlStringOverrideCurrentEntry(html, basicUri, unreachableUrl);
248 }
249
250 bool WebEngine::LoadContents(const std::string& contents, uint32_t contentSize, const std::string& mimeType, const std::string& encoding, const std::string& baseUri)
251 {
252   return mPlugin->LoadContents(contents, contentSize, mimeType, encoding, baseUri);
253 }
254
255 void WebEngine::Reload()
256 {
257   mPlugin->Reload();
258 }
259
260 bool WebEngine::ReloadWithoutCache()
261 {
262   return mPlugin->ReloadWithoutCache();
263 }
264
265 void WebEngine::StopLoading()
266 {
267   mPlugin->StopLoading();
268 }
269
270 void WebEngine::Suspend()
271 {
272   mPlugin->Suspend();
273 }
274
275 void WebEngine::Resume()
276 {
277   mPlugin->Resume();
278 }
279
280 void WebEngine::SuspendNetworkLoading()
281 {
282   mPlugin->SuspendNetworkLoading();
283 }
284
285 void WebEngine::ResumeNetworkLoading()
286 {
287   mPlugin->ResumeNetworkLoading();
288 }
289
290 bool WebEngine::AddCustomHeader(const std::string& name, const std::string& value)
291 {
292   return mPlugin->AddCustomHeader(name, value);
293 }
294
295 bool WebEngine::RemoveCustomHeader(const std::string& name)
296 {
297   return mPlugin->RemoveCustomHeader(name);
298 }
299
300 uint32_t WebEngine::StartInspectorServer(uint32_t port)
301 {
302   return mPlugin->StartInspectorServer(port);
303 }
304
305 bool WebEngine::StopInspectorServer()
306 {
307   return mPlugin->StopInspectorServer();
308 }
309
310 void WebEngine::ScrollBy(int deltaX, int deltaY)
311 {
312   mPlugin->ScrollBy(deltaX, deltaY);
313 }
314
315 bool WebEngine::ScrollEdgeBy(int deltaX, int deltaY)
316 {
317   return mPlugin->ScrollEdgeBy(deltaX, deltaY);
318 }
319
320 void WebEngine::SetScrollPosition(int x, int y)
321 {
322   mPlugin->SetScrollPosition(x, y);
323 }
324
325 Dali::Vector2 WebEngine::GetScrollPosition() const
326 {
327   return mPlugin->GetScrollPosition();
328 }
329
330 Dali::Vector2 WebEngine::GetScrollSize() const
331 {
332   return mPlugin->GetScrollSize();
333 }
334
335 Dali::Vector2 WebEngine::GetContentSize() const
336 {
337   return mPlugin->GetContentSize();
338 }
339
340 void WebEngine::RegisterJavaScriptAlertCallback(Dali::WebEnginePlugin::JavaScriptAlertCallback callback)
341 {
342   mPlugin->RegisterJavaScriptAlertCallback(callback);
343 }
344
345 void WebEngine::JavaScriptAlertReply()
346 {
347   mPlugin->JavaScriptAlertReply();
348 }
349
350 void WebEngine::RegisterJavaScriptConfirmCallback(Dali::WebEnginePlugin::JavaScriptConfirmCallback callback)
351 {
352   mPlugin->RegisterJavaScriptAlertCallback(callback);
353 }
354
355 void WebEngine::JavaScriptConfirmReply(bool confirmed)
356 {
357   mPlugin->JavaScriptConfirmReply(confirmed);
358 }
359
360 void WebEngine::RegisterJavaScriptPromptCallback(Dali::WebEnginePlugin::JavaScriptPromptCallback callback)
361 {
362   mPlugin->RegisterJavaScriptPromptCallback(callback);
363 }
364
365 void WebEngine::JavaScriptPromptReply(const std::string& result)
366 {
367   mPlugin->JavaScriptPromptReply(result);
368 }
369
370 bool WebEngine::CanGoForward()
371 {
372   return mPlugin->CanGoForward();
373 }
374
375 void WebEngine::GoForward()
376 {
377   mPlugin->GoForward();
378 }
379
380 bool WebEngine::CanGoBack()
381 {
382   return mPlugin->CanGoBack();
383 }
384
385 void WebEngine::GoBack()
386 {
387   mPlugin->GoBack();
388 }
389
390 void WebEngine::EvaluateJavaScript(const std::string& script, std::function<void(const std::string&)> resultHandler)
391 {
392   mPlugin->EvaluateJavaScript(script, resultHandler);
393 }
394
395 void WebEngine::AddJavaScriptMessageHandler(const std::string& exposedObjectName, std::function<void(const std::string&)> handler)
396 {
397   mPlugin->AddJavaScriptMessageHandler(exposedObjectName, handler);
398 }
399
400 void WebEngine::ClearAllTilesResources()
401 {
402   mPlugin->ClearAllTilesResources();
403 }
404
405 void WebEngine::ClearHistory()
406 {
407   mPlugin->ClearHistory();
408 }
409
410 void WebEngine::SetSize(int width, int height)
411 {
412   mPlugin->SetSize(width, height);
413 }
414
415 void WebEngine::EnableMouseEvents(bool enabled)
416 {
417   mPlugin->EnableMouseEvents(enabled);
418 }
419
420 void WebEngine::EnableKeyEvents(bool enabled)
421 {
422   mPlugin->EnableKeyEvents(enabled);
423 }
424
425 bool WebEngine::SendTouchEvent(const Dali::TouchEvent& touch)
426 {
427   return mPlugin->SendTouchEvent(touch);
428 }
429
430 bool WebEngine::SendKeyEvent(const Dali::KeyEvent& event)
431 {
432   return mPlugin->SendKeyEvent(event);
433 }
434
435 void WebEngine::SetFocus(bool focused)
436 {
437   mPlugin->SetFocus(focused);
438 }
439
440 void WebEngine::SetDocumentBackgroundColor(Dali::Vector4 color)
441 {
442   mPlugin->SetDocumentBackgroundColor(color);
443 }
444
445 void WebEngine::ClearTilesWhenHidden(bool cleared)
446 {
447   mPlugin->ClearTilesWhenHidden(cleared);
448 }
449
450 void WebEngine::SetTileCoverAreaMultiplier(float multiplier)
451 {
452   mPlugin->SetTileCoverAreaMultiplier(multiplier);
453 }
454
455 void WebEngine::EnableCursorByClient(bool enabled)
456 {
457   mPlugin->EnableCursorByClient(enabled);
458 }
459
460 std::string WebEngine::GetSelectedText() const
461 {
462   return mPlugin->GetSelectedText();
463 }
464
465 void WebEngine::SetPageZoomFactor(float zoomFactor)
466 {
467   mPlugin->SetPageZoomFactor(zoomFactor);
468 }
469
470 float WebEngine::GetPageZoomFactor() const
471 {
472   return mPlugin->GetPageZoomFactor();
473 }
474
475 void WebEngine::SetTextZoomFactor(float zoomFactor)
476 {
477   mPlugin->SetTextZoomFactor(zoomFactor);
478 }
479
480 float WebEngine::GetTextZoomFactor() const
481 {
482   return mPlugin->GetTextZoomFactor();
483 }
484
485 float WebEngine::GetLoadProgressPercentage() const
486 {
487   return mPlugin->GetLoadProgressPercentage();
488 }
489
490 void WebEngine::SetScaleFactor(float scaleFactor, Dali::Vector2 point)
491 {
492   mPlugin->SetScaleFactor(scaleFactor, point);
493 }
494
495 float WebEngine::GetScaleFactor() const
496 {
497   return mPlugin->GetScaleFactor();
498 }
499
500 void WebEngine::ActivateAccessibility(bool activated)
501 {
502   mPlugin->ActivateAccessibility(activated);
503 }
504
505 bool WebEngine::SetVisibility(bool visible)
506 {
507   return mPlugin->SetVisibility(visible);
508 }
509
510 bool WebEngine::HighlightText(const std::string& text, Dali::WebEnginePlugin::FindOption options, uint32_t maxMatchCount)
511 {
512   return mPlugin->HighlightText(text, options, maxMatchCount);
513 }
514
515 void WebEngine::AddDynamicCertificatePath(const std::string& host, const std::string& certPath)
516 {
517   mPlugin->AddDynamicCertificatePath(host, certPath);
518 }
519
520 Dali::PixelData WebEngine::GetScreenshot(Dali::Rect<int> viewArea, float scaleFactor)
521 {
522   return mPlugin->GetScreenshot(viewArea, scaleFactor);
523 }
524
525 bool WebEngine::GetScreenshotAsynchronously(Dali::Rect<int> viewArea, float scaleFactor, Dali::WebEnginePlugin::ScreenshotCapturedCallback callback)
526 {
527   return mPlugin->GetScreenshotAsynchronously(viewArea, scaleFactor, callback);
528 }
529
530 bool WebEngine::CheckVideoPlayingAsynchronously(Dali::WebEnginePlugin::VideoPlayingCallback callback)
531 {
532   return mPlugin->CheckVideoPlayingAsynchronously(callback);
533 }
534
535 void WebEngine::RegisterGeolocationPermissionCallback(Dali::WebEnginePlugin::GeolocationPermissionCallback callback)
536 {
537   mPlugin->RegisterGeolocationPermissionCallback(callback);
538 }
539
540 void WebEngine::UpdateDisplayArea(Dali::Rect<int> displayArea)
541 {
542   mPlugin->UpdateDisplayArea(displayArea);
543 }
544
545 void WebEngine::EnableVideoHole(bool enabled)
546 {
547   mPlugin->EnableVideoHole(enabled);
548 }
549
550 bool WebEngine::SendHoverEvent( const Dali::HoverEvent& event )
551 {
552   return mPlugin->SendHoverEvent( event );
553 }
554
555 bool WebEngine::SendWheelEvent( const Dali::WheelEvent& event )
556 {
557   return mPlugin->SendWheelEvent( event );
558 }
559
560 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadStartedSignal()
561 {
562   return mPlugin->PageLoadStartedSignal();
563 }
564
565 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadInProgressSignal()
566 {
567   return mPlugin->PageLoadInProgressSignal();
568 }
569
570 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadFinishedSignal()
571 {
572   return mPlugin->PageLoadFinishedSignal();
573 }
574
575 Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType& WebEngine::PageLoadErrorSignal()
576 {
577   return mPlugin->PageLoadErrorSignal();
578 }
579
580 Dali::WebEnginePlugin::WebEngineScrollEdgeReachedSignalType& WebEngine::ScrollEdgeReachedSignal()
581 {
582   return mPlugin->ScrollEdgeReachedSignal();
583 }
584
585 Dali::WebEnginePlugin::WebEngineUrlChangedSignalType& WebEngine::UrlChangedSignal()
586 {
587   return mPlugin->UrlChangedSignal();
588 }
589
590 Dali::WebEnginePlugin::WebEngineFormRepostDecisionSignalType& WebEngine::FormRepostDecisionSignal()
591 {
592   return mPlugin->FormRepostDecisionSignal();
593 }
594
595 Dali::WebEnginePlugin::WebEngineFrameRenderedSignalType& WebEngine::FrameRenderedSignal()
596 {
597   return mPlugin->FrameRenderedSignal();
598 }
599
600 Dali::WebEnginePlugin::WebEngineRequestInterceptorSignalType& WebEngine::RequestInterceptorSignal()
601 {
602   return mPlugin->RequestInterceptorSignal();
603 }
604
605 Dali::WebEnginePlugin::WebEngineConsoleMessageSignalType& WebEngine::ConsoleMessageSignal()
606 {
607   return mPlugin->ConsoleMessageSignal();
608 }
609
610 } // namespace Adaptor
611 } // namespace Internal
612 } // namespace Dali