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
92 return mLoadSucceeded;
95 bool InitializeContextHandle()
102 if(!mGetWebEngineContextPtr)
104 mGetWebEngineContextPtr = reinterpret_cast<GetWebEngineContext>(dlsym(mHandle, "GetWebEngineContext"));
105 if(!mGetWebEngineContextPtr)
107 DALI_LOG_ERROR("Can't load symbol GetWebEngineContext(), error: %s\n", dlerror());
115 bool InitializeCookieManagerHandle()
122 if(!mGetWebEngineCookieManagerPtr)
124 mGetWebEngineCookieManagerPtr = reinterpret_cast<GetWebEngineCookieManager>(dlsym(mHandle, "GetWebEngineCookieManager"));
125 if(!mGetWebEngineCookieManagerPtr)
127 DALI_LOG_ERROR("Can't load symbol GetWebEngineCookieManager(), error: %s\n", dlerror());
136 // Private constructor / destructor
137 WebEnginePluginObject()
138 : mLoadSucceeded{false},
140 mCreateWebEnginePtr{nullptr},
141 mDestroyWebEnginePtr{nullptr},
142 mGetWebEngineContextPtr{nullptr},
143 mGetWebEngineCookieManagerPtr{nullptr}
145 std::string pluginName;
146 const char* name = EnvironmentVariable::GetEnvironmentVariable(DALI_ENV_WEB_ENGINE_NAME);
149 pluginName = MakePluginName(name);
153 pluginName = std::string(kPluginFullNameDefault);
156 mHandle = dlopen(pluginName.c_str(), RTLD_LAZY);
159 DALI_LOG_ERROR("Can't load %s : %s\n", pluginName.c_str(), dlerror());
163 mCreateWebEnginePtr = reinterpret_cast<CreateWebEngineFunction>(dlsym(mHandle, "CreateWebEnginePlugin"));
164 if(mCreateWebEnginePtr == nullptr)
166 DALI_LOG_ERROR("Can't load symbol CreateWebEnginePlugin(), error: %s\n", dlerror());
170 mDestroyWebEnginePtr = reinterpret_cast<DestroyWebEngineFunction>(dlsym(mHandle, "DestroyWebEnginePlugin"));
171 if(mDestroyWebEnginePtr == nullptr)
173 DALI_LOG_ERROR("Can't load symbol DestroyWebEnginePlugin(), error: %s\n", dlerror());
177 mLoadSucceeded = true;
180 ~WebEnginePluginObject()
186 mLoadSucceeded = false;
190 WebEnginePluginObject(const WebEnginePluginObject&) = delete;
191 WebEnginePluginObject(WebEnginePluginObject&&) = delete;
192 WebEnginePluginObject& operator=(const WebEnginePluginObject&) = delete;
193 WebEnginePluginObject& operator=(WebEnginePluginObject&&) = delete;
196 bool mLoadSucceeded; ///< True if library loaded successfully. False otherwise.
199 using CreateWebEngineFunction = Dali::WebEnginePlugin* (*)();
200 using DestroyWebEngineFunction = void (*)(Dali::WebEnginePlugin* plugin);
202 using GetWebEngineContext = Dali::WebEngineContext* (*)();
203 using GetWebEngineCookieManager = Dali::WebEngineCookieManager* (*)();
205 void* mHandle; ///< Handle for the loaded library
206 CreateWebEngineFunction mCreateWebEnginePtr; ///< Function to create plugin instance
207 DestroyWebEngineFunction mDestroyWebEnginePtr; ///< Function to destroy plugin instance
209 GetWebEngineContext mGetWebEngineContextPtr; ///< Function to get WebEngineContext
210 GetWebEngineCookieManager mGetWebEngineCookieManagerPtr; ///< Function to get WebEngineCookieManager
213 } // unnamed namespace
215 WebEnginePtr WebEngine::New()
217 WebEngine* instance = new WebEngine();
218 if(!instance->Initialize())
227 Dali::WebEngineContext* WebEngine::GetContext()
229 if(!WebEnginePluginObject::GetInstance().InitializeContextHandle())
234 if(WebEnginePluginObject::GetInstance().mGetWebEngineContextPtr)
236 return WebEnginePluginObject::GetInstance().mGetWebEngineContextPtr();
242 Dali::WebEngineCookieManager* WebEngine::GetCookieManager()
244 if(!WebEnginePluginObject::GetInstance().InitializeCookieManagerHandle())
249 if(WebEnginePluginObject::GetInstance().mGetWebEngineCookieManagerPtr)
251 return WebEnginePluginObject::GetInstance().mGetWebEngineCookieManagerPtr();
257 WebEngine::WebEngine()
262 WebEngine::~WebEngine()
264 if(mPlugin != nullptr)
267 // Check whether plugin load sccess or not.
268 if(DALI_LIKELY(WebEnginePluginObject::GetInstance()))
270 WebEnginePluginObject::GetInstance().mDestroyWebEnginePtr(mPlugin);
276 bool WebEngine::Initialize()
278 // Check whether plugin load sccess or not.
279 if(!WebEnginePluginObject::GetInstance())
284 mPlugin = WebEnginePluginObject::GetInstance().mCreateWebEnginePtr();
285 if(mPlugin == nullptr)
287 DALI_LOG_ERROR("Can't create the WebEnginePlugin object\n");
293 void WebEngine::Create(uint32_t width, uint32_t height, const std::string& locale, const std::string& timezoneId)
295 mPlugin->Create(width, height, locale, timezoneId);
298 void WebEngine::Create(uint32_t width, uint32_t height, uint32_t argc, char** argv)
300 mPlugin->Create(width, height, argc, argv);
303 void WebEngine::Destroy()
308 Dali::WebEnginePlugin* WebEngine::GetPlugin() const
313 Dali::NativeImageSourcePtr WebEngine::GetNativeImageSource()
315 return mPlugin->GetNativeImageSource();
318 Dali::WebEngineSettings& WebEngine::GetSettings() const
320 return mPlugin->GetSettings();
323 Dali::WebEngineBackForwardList& WebEngine::GetBackForwardList() const
325 return mPlugin->GetBackForwardList();
328 void WebEngine::LoadUrl(const std::string& url)
330 mPlugin->LoadUrl(url);
333 std::string WebEngine::GetTitle() const
335 return mPlugin->GetTitle();
338 Dali::PixelData WebEngine::GetFavicon() const
340 return mPlugin->GetFavicon();
343 std::string WebEngine::GetUrl() const
345 return mPlugin->GetUrl();
348 std::string WebEngine::GetUserAgent() const
350 return mPlugin->GetUserAgent();
353 void WebEngine::SetUserAgent(const std::string& userAgent)
355 mPlugin->SetUserAgent(userAgent);
358 void WebEngine::LoadHtmlString(const std::string& htmlString)
360 mPlugin->LoadHtmlString(htmlString);
363 bool WebEngine::LoadHtmlStringOverrideCurrentEntry(const std::string& html, const std::string& basicUri, const std::string& unreachableUrl)
365 return mPlugin->LoadHtmlStringOverrideCurrentEntry(html, basicUri, unreachableUrl);
368 bool WebEngine::LoadContents(const std::string& contents, uint32_t contentSize, const std::string& mimeType, const std::string& encoding, const std::string& baseUri)
370 return mPlugin->LoadContents(contents, contentSize, mimeType, encoding, baseUri);
373 void WebEngine::Reload()
378 bool WebEngine::ReloadWithoutCache()
380 return mPlugin->ReloadWithoutCache();
383 void WebEngine::StopLoading()
385 mPlugin->StopLoading();
388 void WebEngine::Suspend()
393 void WebEngine::Resume()
398 void WebEngine::SuspendNetworkLoading()
400 mPlugin->SuspendNetworkLoading();
403 void WebEngine::ResumeNetworkLoading()
405 mPlugin->ResumeNetworkLoading();
408 bool WebEngine::AddCustomHeader(const std::string& name, const std::string& value)
410 return mPlugin->AddCustomHeader(name, value);
413 bool WebEngine::RemoveCustomHeader(const std::string& name)
415 return mPlugin->RemoveCustomHeader(name);
418 uint32_t WebEngine::StartInspectorServer(uint32_t port)
420 return mPlugin->StartInspectorServer(port);
423 bool WebEngine::StopInspectorServer()
425 return mPlugin->StopInspectorServer();
428 void WebEngine::ScrollBy(int32_t deltaX, int32_t deltaY)
430 mPlugin->ScrollBy(deltaX, deltaY);
433 bool WebEngine::ScrollEdgeBy(int32_t deltaX, int32_t deltaY)
435 return mPlugin->ScrollEdgeBy(deltaX, deltaY);
438 void WebEngine::SetScrollPosition(int32_t x, int32_t y)
440 mPlugin->SetScrollPosition(x, y);
443 Dali::Vector2 WebEngine::GetScrollPosition() const
445 return mPlugin->GetScrollPosition();
448 Dali::Vector2 WebEngine::GetScrollSize() const
450 return mPlugin->GetScrollSize();
453 Dali::Vector2 WebEngine::GetContentSize() const
455 return mPlugin->GetContentSize();
458 void WebEngine::RegisterJavaScriptAlertCallback(Dali::WebEnginePlugin::JavaScriptAlertCallback callback)
460 mPlugin->RegisterJavaScriptAlertCallback(callback);
463 void WebEngine::JavaScriptAlertReply()
465 mPlugin->JavaScriptAlertReply();
468 void WebEngine::RegisterJavaScriptConfirmCallback(Dali::WebEnginePlugin::JavaScriptConfirmCallback callback)
470 mPlugin->RegisterJavaScriptConfirmCallback(callback);
473 void WebEngine::JavaScriptConfirmReply(bool confirmed)
475 mPlugin->JavaScriptConfirmReply(confirmed);
478 void WebEngine::RegisterJavaScriptPromptCallback(Dali::WebEnginePlugin::JavaScriptPromptCallback callback)
480 mPlugin->RegisterJavaScriptPromptCallback(callback);
483 void WebEngine::JavaScriptPromptReply(const std::string& result)
485 mPlugin->JavaScriptPromptReply(result);
488 std::unique_ptr<Dali::WebEngineHitTest> WebEngine::CreateHitTest(int32_t x, int32_t y, Dali::WebEngineHitTest::HitTestMode mode)
490 return mPlugin->CreateHitTest(x, y, mode);
493 bool WebEngine::CreateHitTestAsynchronously(int32_t x, int32_t y, Dali::WebEngineHitTest::HitTestMode mode, Dali::WebEnginePlugin::WebEngineHitTestCreatedCallback callback)
495 return mPlugin->CreateHitTestAsynchronously(x, y, mode, callback);
498 bool WebEngine::CanGoForward()
500 return mPlugin->CanGoForward();
503 void WebEngine::GoForward()
505 mPlugin->GoForward();
508 bool WebEngine::CanGoBack()
510 return mPlugin->CanGoBack();
513 void WebEngine::GoBack()
518 void WebEngine::EvaluateJavaScript(const std::string& script, Dali::WebEnginePlugin::JavaScriptMessageHandlerCallback resultHandler)
520 mPlugin->EvaluateJavaScript(script, resultHandler);
523 void WebEngine::AddJavaScriptMessageHandler(const std::string& exposedObjectName, Dali::WebEnginePlugin::JavaScriptMessageHandlerCallback handler)
525 mPlugin->AddJavaScriptMessageHandler(exposedObjectName, handler);
528 void WebEngine::ClearAllTilesResources()
530 mPlugin->ClearAllTilesResources();
533 void WebEngine::ClearHistory()
535 mPlugin->ClearHistory();
538 void WebEngine::SetSize(uint32_t width, uint32_t height)
540 mPlugin->SetSize(width, height);
543 void WebEngine::EnableMouseEvents(bool enabled)
545 mPlugin->EnableMouseEvents(enabled);
548 void WebEngine::EnableKeyEvents(bool enabled)
550 mPlugin->EnableKeyEvents(enabled);
553 bool WebEngine::SendTouchEvent(const Dali::TouchEvent& touch)
555 return mPlugin->SendTouchEvent(touch);
558 bool WebEngine::SendKeyEvent(const Dali::KeyEvent& event)
560 return mPlugin->SendKeyEvent(event);
563 void WebEngine::SetFocus(bool focused)
565 mPlugin->SetFocus(focused);
568 void WebEngine::SetDocumentBackgroundColor(Dali::Vector4 color)
570 mPlugin->SetDocumentBackgroundColor(color);
573 void WebEngine::ClearTilesWhenHidden(bool cleared)
575 mPlugin->ClearTilesWhenHidden(cleared);
578 void WebEngine::SetTileCoverAreaMultiplier(float multiplier)
580 mPlugin->SetTileCoverAreaMultiplier(multiplier);
583 void WebEngine::EnableCursorByClient(bool enabled)
585 mPlugin->EnableCursorByClient(enabled);
588 std::string WebEngine::GetSelectedText() const
590 return mPlugin->GetSelectedText();
593 void WebEngine::SetPageZoomFactor(float zoomFactor)
595 mPlugin->SetPageZoomFactor(zoomFactor);
598 float WebEngine::GetPageZoomFactor() const
600 return mPlugin->GetPageZoomFactor();
603 void WebEngine::SetTextZoomFactor(float zoomFactor)
605 mPlugin->SetTextZoomFactor(zoomFactor);
608 float WebEngine::GetTextZoomFactor() const
610 return mPlugin->GetTextZoomFactor();
613 float WebEngine::GetLoadProgressPercentage() const
615 return mPlugin->GetLoadProgressPercentage();
618 void WebEngine::SetScaleFactor(float scaleFactor, Dali::Vector2 point)
620 mPlugin->SetScaleFactor(scaleFactor, point);
623 float WebEngine::GetScaleFactor() const
625 return mPlugin->GetScaleFactor();
628 void WebEngine::ActivateAccessibility(bool activated)
630 mPlugin->ActivateAccessibility(activated);
633 Accessibility::Address WebEngine::GetAccessibilityAddress()
635 return mPlugin->GetAccessibilityAddress();
638 bool WebEngine::SetVisibility(bool visible)
640 return mPlugin->SetVisibility(visible);
643 bool WebEngine::HighlightText(const std::string& text, Dali::WebEnginePlugin::FindOption options, uint32_t maxMatchCount)
645 return mPlugin->HighlightText(text, options, maxMatchCount);
648 void WebEngine::AddDynamicCertificatePath(const std::string& host, const std::string& certPath)
650 mPlugin->AddDynamicCertificatePath(host, certPath);
653 Dali::PixelData WebEngine::GetScreenshot(Dali::Rect<int32_t> viewArea, float scaleFactor)
655 return mPlugin->GetScreenshot(viewArea, scaleFactor);
658 bool WebEngine::GetScreenshotAsynchronously(Dali::Rect<int32_t> viewArea, float scaleFactor, Dali::WebEnginePlugin::ScreenshotCapturedCallback callback)
660 return mPlugin->GetScreenshotAsynchronously(viewArea, scaleFactor, callback);
663 bool WebEngine::CheckVideoPlayingAsynchronously(Dali::WebEnginePlugin::VideoPlayingCallback callback)
665 return mPlugin->CheckVideoPlayingAsynchronously(callback);
668 void WebEngine::RegisterGeolocationPermissionCallback(Dali::WebEnginePlugin::GeolocationPermissionCallback callback)
670 mPlugin->RegisterGeolocationPermissionCallback(callback);
673 void WebEngine::UpdateDisplayArea(Dali::Rect<int32_t> displayArea)
675 mPlugin->UpdateDisplayArea(displayArea);
678 void WebEngine::EnableVideoHole(bool enabled)
680 mPlugin->EnableVideoHole(enabled);
683 bool WebEngine::SendHoverEvent(const Dali::HoverEvent& event)
685 return mPlugin->SendHoverEvent(event);
688 bool WebEngine::SendWheelEvent(const Dali::WheelEvent& event)
690 return mPlugin->SendWheelEvent(event);
693 void WebEngine::RegisterFrameRenderedCallback(Dali::WebEnginePlugin::WebEngineFrameRenderedCallback callback)
695 mPlugin->RegisterFrameRenderedCallback(callback);
698 void WebEngine::RegisterPageLoadStartedCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback)
700 mPlugin->RegisterPageLoadStartedCallback(callback);
703 void WebEngine::RegisterPageLoadInProgressCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback)
705 mPlugin->RegisterPageLoadInProgressCallback(callback);
708 void WebEngine::RegisterPageLoadFinishedCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback)
710 mPlugin->RegisterPageLoadFinishedCallback(callback);
713 void WebEngine::RegisterPageLoadErrorCallback(Dali::WebEnginePlugin::WebEnginePageLoadErrorCallback callback)
715 mPlugin->RegisterPageLoadErrorCallback(callback);
718 void WebEngine::RegisterScrollEdgeReachedCallback(Dali::WebEnginePlugin::WebEngineScrollEdgeReachedCallback callback)
720 mPlugin->RegisterScrollEdgeReachedCallback(callback);
723 void WebEngine::RegisterUrlChangedCallback(Dali::WebEnginePlugin::WebEngineUrlChangedCallback callback)
725 mPlugin->RegisterUrlChangedCallback(callback);
728 void WebEngine::RegisterFormRepostDecidedCallback(Dali::WebEnginePlugin::WebEngineFormRepostDecidedCallback callback)
730 mPlugin->RegisterFormRepostDecidedCallback(callback);
733 void WebEngine::RegisterConsoleMessageReceivedCallback(Dali::WebEnginePlugin::WebEngineConsoleMessageReceivedCallback callback)
735 mPlugin->RegisterConsoleMessageReceivedCallback(callback);
738 void WebEngine::RegisterResponsePolicyDecidedCallback(Dali::WebEnginePlugin::WebEngineResponsePolicyDecidedCallback callback)
740 mPlugin->RegisterResponsePolicyDecidedCallback(callback);
743 void WebEngine::RegisterNavigationPolicyDecidedCallback(Dali::WebEnginePlugin::WebEngineNavigationPolicyDecidedCallback callback)
745 mPlugin->RegisterNavigationPolicyDecidedCallback(callback);
748 void WebEngine::RegisterNewWindowCreatedCallback(Dali::WebEnginePlugin::WebEngineNewWindowCreatedCallback callback)
750 mPlugin->RegisterNewWindowCreatedCallback(callback);
753 void WebEngine::RegisterCertificateConfirmedCallback(Dali::WebEnginePlugin::WebEngineCertificateCallback callback)
755 mPlugin->RegisterCertificateConfirmedCallback(callback);
758 void WebEngine::RegisterSslCertificateChangedCallback(Dali::WebEnginePlugin::WebEngineCertificateCallback callback)
760 mPlugin->RegisterSslCertificateChangedCallback(callback);
763 void WebEngine::RegisterHttpAuthHandlerCallback(Dali::WebEnginePlugin::WebEngineHttpAuthHandlerCallback callback)
765 mPlugin->RegisterHttpAuthHandlerCallback(callback);
768 void WebEngine::RegisterContextMenuShownCallback(Dali::WebEnginePlugin::WebEngineContextMenuShownCallback callback)
770 mPlugin->RegisterContextMenuShownCallback(callback);
773 void WebEngine::RegisterContextMenuHiddenCallback(Dali::WebEnginePlugin::WebEngineContextMenuHiddenCallback callback)
775 mPlugin->RegisterContextMenuHiddenCallback(callback);
778 void WebEngine::GetPlainTextAsynchronously(Dali::WebEnginePlugin::PlainTextReceivedCallback callback)
780 mPlugin->GetPlainTextAsynchronously(callback);
783 } // namespace Adaptor
784 } // namespace Internal