2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/web-engine/common/web-engine-impl.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/public-api/object/type-registry.h>
28 #include <dali/devel-api/adaptor-framework/environment-variable.h>
29 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-back-forward-list.h>
30 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-certificate.h>
31 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-console-message.h>
32 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-context-menu.h>
33 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-context.h>
34 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-cookie-manager.h>
35 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-http-auth-handler.h>
36 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-load-error.h>
37 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-policy-decision.h>
38 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-settings.h>
39 #include <dali/internal/system/common/environment-variables.h>
40 #include <dali/public-api/adaptor-framework/native-image-source.h>
41 #include <dali/public-api/images/pixel-data.h>
49 namespace // unnamed namespace
51 constexpr char const* const kPluginFullNamePrefix = "libdali2-web-engine-";
52 constexpr char const* const kPluginFullNamePostfix = "-plugin.so";
53 constexpr char const* const kPluginFullNameDefault = "libdali2-web-engine-plugin.so";
55 std::string MakePluginName(const char* environmentName)
57 std::stringstream fullName;
58 fullName << kPluginFullNamePrefix << environmentName << kPluginFullNamePostfix;
59 return std::move(fullName.str());
62 Dali::BaseHandle Create()
64 return Dali::WebEngine::New();
67 Dali::TypeRegistration type(typeid(Dali::WebEngine), typeid(Dali::BaseHandle), Create);
70 * @brief Control the WebEnginePlugin library lifecycle.
71 * Hold the plugin library handle in static singletone.
72 * It will makes library handle alives during all WebEngine resources create & destory.
74 struct WebEnginePluginObject
77 static WebEnginePluginObject& GetInstance()
79 static WebEnginePluginObject gPluginHandle;
84 * @brief Converts an handle to a bool.
86 * This is useful for checking whether the WebEnginePluginObject succes to load library.
87 * @note We don't check mHandle because it is possible that mHandle load is success but
88 * Create/Destroy API load failed.
90 explicit operator bool() const
95 bool InitializeContextHandle()
102 if(!mGetWebEngineContextPtr)
104 mGetWebEngineContextPtr = reinterpret_cast<GetWebEngineContext>(dlsym(mHandle, "GetWebEngineContext"));
106 if(!mGetWebEngineContextPtr)
108 DALI_LOG_ERROR("Can't load symbol GetWebEngineContext(), error: %s\n", dlerror());
116 bool InitializeCookieManagerHandle()
123 if(!mGetWebEngineCookieManagerPtr)
125 mGetWebEngineCookieManagerPtr = reinterpret_cast<GetWebEngineCookieManager>(dlsym(mHandle, "GetWebEngineCookieManager"));
127 if(!mGetWebEngineCookieManagerPtr)
129 DALI_LOG_ERROR("Can't load symbol GetWebEngineCookieManager(), error: %s\n", dlerror());
138 // Private constructor / destructor
139 WebEnginePluginObject()
143 mCreateWebEnginePtr{nullptr},
144 mDestroyWebEnginePtr{nullptr},
145 mGetWebEngineContextPtr{nullptr},
146 mGetWebEngineCookieManagerPtr{nullptr}
148 if(mPluginName.length() == 0)
150 // mPluginName is not initialized yet.
151 const char* name = EnvironmentVariable::GetEnvironmentVariable(DALI_ENV_WEB_ENGINE_NAME);
154 mPluginName = MakePluginName(name);
158 mPluginName = std::string(kPluginFullNameDefault);
162 mHandle = dlopen(mPluginName.c_str(), RTLD_LAZY);
165 DALI_LOG_ERROR("Can't load %s : %s\n", mPluginName.c_str(), dlerror());
169 mCreateWebEnginePtr = reinterpret_cast<CreateWebEngineFunction>(dlsym(mHandle, "CreateWebEnginePlugin"));
170 if(mCreateWebEnginePtr == nullptr)
172 DALI_LOG_ERROR("Can't load symbol CreateWebEnginePlugin(), error: %s\n", dlerror());
176 mDestroyWebEnginePtr = reinterpret_cast<DestroyWebEngineFunction>(dlsym(mHandle, "DestroyWebEnginePlugin"));
177 if(mDestroyWebEnginePtr == nullptr)
179 DALI_LOG_ERROR("Can't load symbol DestroyWebEnginePlugin(), error: %s\n", dlerror());
186 ~WebEnginePluginObject()
192 mLoadSuccess = false;
196 WebEnginePluginObject(const WebEnginePluginObject&) = delete;
197 WebEnginePluginObject(WebEnginePluginObject&&) = delete;
198 WebEnginePluginObject& operator=(const WebEnginePluginObject&) = delete;
199 WebEnginePluginObject& operator=(WebEnginePluginObject&&) = delete;
202 std::string mPluginName; ///< Name of web engine plugin
203 /// Note: Dali WebView policy does not allow to use multiple web engines in an application.
204 /// So once pluginName is set to non-empty string, it will not change.
206 bool mLoadSuccess; ///< True if library loaded successfully. False otherwise.
209 using CreateWebEngineFunction = Dali::WebEnginePlugin* (*)();
210 using DestroyWebEngineFunction = void (*)(Dali::WebEnginePlugin* plugin);
212 using GetWebEngineContext = Dali::WebEngineContext* (*)();
213 using GetWebEngineCookieManager = Dali::WebEngineCookieManager* (*)();
215 void* mHandle; ///< Handle for the loaded library
216 CreateWebEngineFunction mCreateWebEnginePtr; ///< Function to create plugin instance
217 DestroyWebEngineFunction mDestroyWebEnginePtr; ///< Function to destroy plugin instance
219 GetWebEngineContext mGetWebEngineContextPtr; ///< Function to get WebEngineContext
220 GetWebEngineCookieManager mGetWebEngineCookieManagerPtr; ///< Function to get WebEngineCookieManager
223 } // unnamed namespace
225 WebEnginePtr WebEngine::New()
227 WebEngine* instance = new WebEngine();
228 if(!instance->Initialize())
237 Dali::WebEngineContext* WebEngine::GetContext()
239 if(!WebEnginePluginObject::GetInstance().InitializeContextHandle())
244 if(WebEnginePluginObject::GetInstance().mGetWebEngineContextPtr)
246 return WebEnginePluginObject::GetInstance().mGetWebEngineContextPtr();
252 Dali::WebEngineCookieManager* WebEngine::GetCookieManager()
254 if(!WebEnginePluginObject::GetInstance().InitializeCookieManagerHandle())
259 if(WebEnginePluginObject::GetInstance().mGetWebEngineCookieManagerPtr)
261 return WebEnginePluginObject::GetInstance().mGetWebEngineCookieManagerPtr();
267 WebEngine::WebEngine()
272 WebEngine::~WebEngine()
274 if(mPlugin != nullptr)
277 // Check whether plugin load sccess or not.
278 if(DALI_LIKELY(WebEnginePluginObject::GetInstance()))
280 WebEnginePluginObject::GetInstance().mDestroyWebEnginePtr(mPlugin);
286 bool WebEngine::Initialize()
288 // Check whether plugin load sccess or not.
289 if(!WebEnginePluginObject::GetInstance())
294 mPlugin = WebEnginePluginObject::GetInstance().mCreateWebEnginePtr();
295 if(mPlugin == nullptr)
297 DALI_LOG_ERROR("Can't create the WebEnginePlugin object\n");
303 void WebEngine::Create(uint32_t width, uint32_t height, const std::string& locale, const std::string& timezoneId)
305 mPlugin->Create(width, height, locale, timezoneId);
308 void WebEngine::Create(uint32_t width, uint32_t height, uint32_t argc, char** argv)
310 mPlugin->Create(width, height, argc, argv);
313 void WebEngine::Destroy()
318 Dali::WebEnginePlugin* WebEngine::GetPlugin() const
323 Dali::NativeImageSourcePtr WebEngine::GetNativeImageSource()
325 return mPlugin->GetNativeImageSource();
328 Dali::WebEngineSettings& WebEngine::GetSettings() const
330 return mPlugin->GetSettings();
333 Dali::WebEngineBackForwardList& WebEngine::GetBackForwardList() const
335 return mPlugin->GetBackForwardList();
338 void WebEngine::LoadUrl(const std::string& url)
340 mPlugin->LoadUrl(url);
343 std::string WebEngine::GetTitle() const
345 return mPlugin->GetTitle();
348 Dali::PixelData WebEngine::GetFavicon() const
350 return mPlugin->GetFavicon();
353 std::string WebEngine::GetUrl() const
355 return mPlugin->GetUrl();
358 std::string WebEngine::GetUserAgent() const
360 return mPlugin->GetUserAgent();
363 void WebEngine::SetUserAgent(const std::string& userAgent)
365 mPlugin->SetUserAgent(userAgent);
368 void WebEngine::LoadHtmlString(const std::string& htmlString)
370 mPlugin->LoadHtmlString(htmlString);
373 bool WebEngine::LoadHtmlStringOverrideCurrentEntry(const std::string& html, const std::string& basicUri, const std::string& unreachableUrl)
375 return mPlugin->LoadHtmlStringOverrideCurrentEntry(html, basicUri, unreachableUrl);
378 bool WebEngine::LoadContents(const std::string& contents, uint32_t contentSize, const std::string& mimeType, const std::string& encoding, const std::string& baseUri)
380 return mPlugin->LoadContents(contents, contentSize, mimeType, encoding, baseUri);
383 void WebEngine::Reload()
388 bool WebEngine::ReloadWithoutCache()
390 return mPlugin->ReloadWithoutCache();
393 void WebEngine::StopLoading()
395 mPlugin->StopLoading();
398 void WebEngine::Suspend()
403 void WebEngine::Resume()
408 void WebEngine::SuspendNetworkLoading()
410 mPlugin->SuspendNetworkLoading();
413 void WebEngine::ResumeNetworkLoading()
415 mPlugin->ResumeNetworkLoading();
418 bool WebEngine::AddCustomHeader(const std::string& name, const std::string& value)
420 return mPlugin->AddCustomHeader(name, value);
423 bool WebEngine::RemoveCustomHeader(const std::string& name)
425 return mPlugin->RemoveCustomHeader(name);
428 uint32_t WebEngine::StartInspectorServer(uint32_t port)
430 return mPlugin->StartInspectorServer(port);
433 bool WebEngine::StopInspectorServer()
435 return mPlugin->StopInspectorServer();
438 void WebEngine::ScrollBy(int32_t deltaX, int32_t deltaY)
440 mPlugin->ScrollBy(deltaX, deltaY);
443 bool WebEngine::ScrollEdgeBy(int32_t deltaX, int32_t deltaY)
445 return mPlugin->ScrollEdgeBy(deltaX, deltaY);
448 void WebEngine::SetScrollPosition(int32_t x, int32_t y)
450 mPlugin->SetScrollPosition(x, y);
453 Dali::Vector2 WebEngine::GetScrollPosition() const
455 return mPlugin->GetScrollPosition();
458 Dali::Vector2 WebEngine::GetScrollSize() const
460 return mPlugin->GetScrollSize();
463 Dali::Vector2 WebEngine::GetContentSize() const
465 return mPlugin->GetContentSize();
468 void WebEngine::RegisterJavaScriptAlertCallback(Dali::WebEnginePlugin::JavaScriptAlertCallback callback)
470 mPlugin->RegisterJavaScriptAlertCallback(callback);
473 void WebEngine::JavaScriptAlertReply()
475 mPlugin->JavaScriptAlertReply();
478 void WebEngine::RegisterJavaScriptConfirmCallback(Dali::WebEnginePlugin::JavaScriptConfirmCallback callback)
480 mPlugin->RegisterJavaScriptConfirmCallback(callback);
483 void WebEngine::JavaScriptConfirmReply(bool confirmed)
485 mPlugin->JavaScriptConfirmReply(confirmed);
488 void WebEngine::RegisterJavaScriptPromptCallback(Dali::WebEnginePlugin::JavaScriptPromptCallback callback)
490 mPlugin->RegisterJavaScriptPromptCallback(callback);
493 void WebEngine::JavaScriptPromptReply(const std::string& result)
495 mPlugin->JavaScriptPromptReply(result);
498 std::unique_ptr<Dali::WebEngineHitTest> WebEngine::CreateHitTest(int32_t x, int32_t y, Dali::WebEngineHitTest::HitTestMode mode)
500 return mPlugin->CreateHitTest(x, y, mode);
503 bool WebEngine::CreateHitTestAsynchronously(int32_t x, int32_t y, Dali::WebEngineHitTest::HitTestMode mode, Dali::WebEnginePlugin::WebEngineHitTestCreatedCallback callback)
505 return mPlugin->CreateHitTestAsynchronously(x, y, mode, callback);
508 bool WebEngine::CanGoForward()
510 return mPlugin->CanGoForward();
513 void WebEngine::GoForward()
515 mPlugin->GoForward();
518 bool WebEngine::CanGoBack()
520 return mPlugin->CanGoBack();
523 void WebEngine::GoBack()
528 void WebEngine::EvaluateJavaScript(const std::string& script, Dali::WebEnginePlugin::JavaScriptMessageHandlerCallback resultHandler)
530 mPlugin->EvaluateJavaScript(script, resultHandler);
533 void WebEngine::AddJavaScriptMessageHandler(const std::string& exposedObjectName, Dali::WebEnginePlugin::JavaScriptMessageHandlerCallback handler)
535 mPlugin->AddJavaScriptMessageHandler(exposedObjectName, handler);
538 void WebEngine::ClearAllTilesResources()
540 mPlugin->ClearAllTilesResources();
543 void WebEngine::ClearHistory()
545 mPlugin->ClearHistory();
548 void WebEngine::SetSize(uint32_t width, uint32_t height)
550 mPlugin->SetSize(width, height);
553 void WebEngine::EnableMouseEvents(bool enabled)
555 mPlugin->EnableMouseEvents(enabled);
558 void WebEngine::EnableKeyEvents(bool enabled)
560 mPlugin->EnableKeyEvents(enabled);
563 bool WebEngine::SendTouchEvent(const Dali::TouchEvent& touch)
565 return mPlugin->SendTouchEvent(touch);
568 bool WebEngine::SendKeyEvent(const Dali::KeyEvent& event)
570 return mPlugin->SendKeyEvent(event);
573 void WebEngine::SetFocus(bool focused)
575 mPlugin->SetFocus(focused);
578 void WebEngine::SetDocumentBackgroundColor(Dali::Vector4 color)
580 mPlugin->SetDocumentBackgroundColor(color);
583 void WebEngine::ClearTilesWhenHidden(bool cleared)
585 mPlugin->ClearTilesWhenHidden(cleared);
588 void WebEngine::SetTileCoverAreaMultiplier(float multiplier)
590 mPlugin->SetTileCoverAreaMultiplier(multiplier);
593 void WebEngine::EnableCursorByClient(bool enabled)
595 mPlugin->EnableCursorByClient(enabled);
598 std::string WebEngine::GetSelectedText() const
600 return mPlugin->GetSelectedText();
603 void WebEngine::SetPageZoomFactor(float zoomFactor)
605 mPlugin->SetPageZoomFactor(zoomFactor);
608 float WebEngine::GetPageZoomFactor() const
610 return mPlugin->GetPageZoomFactor();
613 void WebEngine::SetTextZoomFactor(float zoomFactor)
615 mPlugin->SetTextZoomFactor(zoomFactor);
618 float WebEngine::GetTextZoomFactor() const
620 return mPlugin->GetTextZoomFactor();
623 float WebEngine::GetLoadProgressPercentage() const
625 return mPlugin->GetLoadProgressPercentage();
628 void WebEngine::SetScaleFactor(float scaleFactor, Dali::Vector2 point)
630 mPlugin->SetScaleFactor(scaleFactor, point);
633 float WebEngine::GetScaleFactor() const
635 return mPlugin->GetScaleFactor();
638 void WebEngine::ActivateAccessibility(bool activated)
640 mPlugin->ActivateAccessibility(activated);
643 Accessibility::Address WebEngine::GetAccessibilityAddress()
645 return mPlugin->GetAccessibilityAddress();
648 bool WebEngine::SetVisibility(bool visible)
650 return mPlugin->SetVisibility(visible);
653 bool WebEngine::HighlightText(const std::string& text, Dali::WebEnginePlugin::FindOption options, uint32_t maxMatchCount)
655 return mPlugin->HighlightText(text, options, maxMatchCount);
658 void WebEngine::AddDynamicCertificatePath(const std::string& host, const std::string& certPath)
660 mPlugin->AddDynamicCertificatePath(host, certPath);
663 Dali::PixelData WebEngine::GetScreenshot(Dali::Rect<int32_t> viewArea, float scaleFactor)
665 return mPlugin->GetScreenshot(viewArea, scaleFactor);
668 bool WebEngine::GetScreenshotAsynchronously(Dali::Rect<int32_t> viewArea, float scaleFactor, Dali::WebEnginePlugin::ScreenshotCapturedCallback callback)
670 return mPlugin->GetScreenshotAsynchronously(viewArea, scaleFactor, callback);
673 bool WebEngine::CheckVideoPlayingAsynchronously(Dali::WebEnginePlugin::VideoPlayingCallback callback)
675 return mPlugin->CheckVideoPlayingAsynchronously(callback);
678 void WebEngine::RegisterGeolocationPermissionCallback(Dali::WebEnginePlugin::GeolocationPermissionCallback callback)
680 mPlugin->RegisterGeolocationPermissionCallback(callback);
683 void WebEngine::UpdateDisplayArea(Dali::Rect<int32_t> displayArea)
685 mPlugin->UpdateDisplayArea(displayArea);
688 void WebEngine::EnableVideoHole(bool enabled)
690 mPlugin->EnableVideoHole(enabled);
693 bool WebEngine::SendHoverEvent(const Dali::HoverEvent& event)
695 return mPlugin->SendHoverEvent(event);
698 bool WebEngine::SendWheelEvent(const Dali::WheelEvent& event)
700 return mPlugin->SendWheelEvent(event);
703 Dali::WebEnginePlugin::WebEngineFrameRenderedSignalType& WebEngine::FrameRenderedSignal()
705 return mPlugin->FrameRenderedSignal();
708 void WebEngine::RegisterPageLoadStartedCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback)
710 mPlugin->RegisterPageLoadStartedCallback(callback);
713 void WebEngine::RegisterPageLoadInProgressCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback)
715 mPlugin->RegisterPageLoadInProgressCallback(callback);
718 void WebEngine::RegisterPageLoadFinishedCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback)
720 mPlugin->RegisterPageLoadFinishedCallback(callback);
723 void WebEngine::RegisterPageLoadErrorCallback(Dali::WebEnginePlugin::WebEnginePageLoadErrorCallback callback)
725 mPlugin->RegisterPageLoadErrorCallback(callback);
728 void WebEngine::RegisterScrollEdgeReachedCallback(Dali::WebEnginePlugin::WebEngineScrollEdgeReachedCallback callback)
730 mPlugin->RegisterScrollEdgeReachedCallback(callback);
733 void WebEngine::RegisterUrlChangedCallback(Dali::WebEnginePlugin::WebEngineUrlChangedCallback callback)
735 mPlugin->RegisterUrlChangedCallback(callback);
738 void WebEngine::RegisterFormRepostDecidedCallback(Dali::WebEnginePlugin::WebEngineFormRepostDecidedCallback callback)
740 mPlugin->RegisterFormRepostDecidedCallback(callback);
743 void WebEngine::RegisterConsoleMessageReceivedCallback(Dali::WebEnginePlugin::WebEngineConsoleMessageReceivedCallback callback)
745 mPlugin->RegisterConsoleMessageReceivedCallback(callback);
748 void WebEngine::RegisterResponsePolicyDecidedCallback(Dali::WebEnginePlugin::WebEngineResponsePolicyDecidedCallback callback)
750 mPlugin->RegisterResponsePolicyDecidedCallback(callback);
753 void WebEngine::RegisterNavigationPolicyDecidedCallback(Dali::WebEnginePlugin::WebEngineNavigationPolicyDecidedCallback callback)
755 mPlugin->RegisterNavigationPolicyDecidedCallback(callback);
758 void WebEngine::RegisterCertificateConfirmedCallback(Dali::WebEnginePlugin::WebEngineCertificateCallback callback)
760 mPlugin->RegisterCertificateConfirmedCallback(callback);
763 void WebEngine::RegisterSslCertificateChangedCallback(Dali::WebEnginePlugin::WebEngineCertificateCallback callback)
765 mPlugin->RegisterSslCertificateChangedCallback(callback);
768 void WebEngine::RegisterHttpAuthHandlerCallback(Dali::WebEnginePlugin::WebEngineHttpAuthHandlerCallback callback)
770 mPlugin->RegisterHttpAuthHandlerCallback(callback);
773 void WebEngine::RegisterContextMenuShownCallback(Dali::WebEnginePlugin::WebEngineContextMenuShownCallback callback)
775 mPlugin->RegisterContextMenuShownCallback(callback);
778 void WebEngine::RegisterContextMenuHiddenCallback(Dali::WebEnginePlugin::WebEngineContextMenuHiddenCallback callback)
780 mPlugin->RegisterContextMenuHiddenCallback(callback);
783 void WebEngine::GetPlainTextAsynchronously(Dali::WebEnginePlugin::PlainTextReceivedCallback callback)
785 mPlugin->GetPlainTextAsynchronously(callback);
788 } // namespace Adaptor
789 } // namespace Internal