2 * Copyright (c) 2021 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-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>
43 namespace // unnamed namespace
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";
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;
53 std::string MakePluginName(const char* environmentName)
55 std::stringstream fullName;
56 fullName << kPluginFullNamePrefix << environmentName << kPluginFullNamePostfix;
57 return std::move(fullName.str());
60 Dali::BaseHandle Create()
62 return Dali::WebEngine::New();
65 Dali::TypeRegistration type(typeid(Dali::WebEngine), typeid(Dali::BaseHandle), Create);
67 } // unnamed namespace
69 WebEnginePtr WebEngine::New()
71 WebEngine* instance = new WebEngine();
73 if(!instance->Initialize())
82 WebEngine::WebEngine()
85 mCreateWebEnginePtr(NULL),
86 mDestroyWebEnginePtr(NULL)
90 WebEngine::~WebEngine()
94 if(mDestroyWebEnginePtr != NULL)
97 mDestroyWebEnginePtr(mPlugin);
104 bool WebEngine::InitializePluginHandle()
106 if(pluginName.length() == 0)
108 // pluginName is not initialized yet.
109 const char* name = EnvironmentVariable::GetEnvironmentVariable(DALI_ENV_WEB_ENGINE_NAME);
112 pluginName = MakePluginName(name);
113 mHandle = dlopen(pluginName.c_str(), RTLD_LAZY);
119 pluginName = std::string(kPluginFullNameDefault);
122 mHandle = dlopen(pluginName.c_str(), RTLD_LAZY);
125 DALI_LOG_ERROR("Can't load %s : %s\n", pluginName.c_str(), dlerror());
132 bool WebEngine::Initialize()
136 if(!InitializePluginHandle())
141 mCreateWebEnginePtr = reinterpret_cast<CreateWebEngineFunction>(dlsym(mHandle, "CreateWebEnginePlugin"));
142 if(mCreateWebEnginePtr == NULL)
144 DALI_LOG_ERROR("Can't load symbol CreateWebEnginePlugin(), error: %s\n", error);
148 mDestroyWebEnginePtr = reinterpret_cast<DestroyWebEngineFunction>(dlsym(mHandle, "DestroyWebEnginePlugin"));
150 if(mDestroyWebEnginePtr == NULL)
152 DALI_LOG_ERROR("Can't load symbol DestroyWebEnginePlugin(), error: %s\n", error);
156 mPlugin = mCreateWebEnginePtr();
160 DALI_LOG_ERROR("Can't create the WebEnginePlugin object\n");
167 void WebEngine::Create(int width, int height, const std::string& locale, const std::string& timezoneId)
169 mPlugin->Create(width, height, locale, timezoneId);
172 void WebEngine::Create(int width, int height, int argc, char** argv)
174 mPlugin->Create(width, height, argc, argv);
177 void WebEngine::Destroy()
182 Dali::NativeImageInterfacePtr WebEngine::GetNativeImageSource()
184 return mPlugin->GetNativeImageSource();
187 Dali::WebEngineSettings& WebEngine::GetSettings() const
189 return mPlugin->GetSettings();
192 Dali::WebEngineContext& WebEngine::GetContext() const
194 return mPlugin->GetContext();
197 Dali::WebEngineCookieManager& WebEngine::GetCookieManager() const
199 return mPlugin->GetCookieManager();
202 Dali::WebEngineBackForwardList& WebEngine::GetBackForwardList() const
204 return mPlugin->GetBackForwardList();
207 void WebEngine::LoadUrl(const std::string& url)
209 mPlugin->LoadUrl(url);
212 std::string WebEngine::GetTitle() const
214 return mPlugin->GetTitle();
217 Dali::PixelData WebEngine::GetFavicon() const
219 return mPlugin->GetFavicon();
222 const std::string& WebEngine::GetUrl()
224 return mPlugin->GetUrl();
227 const std::string& WebEngine::GetUserAgent() const
229 return mPlugin->GetUserAgent();
232 void WebEngine::SetUserAgent(const std::string& userAgent)
234 mPlugin->SetUserAgent(userAgent);
237 void WebEngine::LoadHtmlString(const std::string& htmlString)
239 mPlugin->LoadHtmlString(htmlString);
242 void WebEngine::Reload()
247 void WebEngine::StopLoading()
249 mPlugin->StopLoading();
252 void WebEngine::Suspend()
257 void WebEngine::Resume()
262 void WebEngine::ScrollBy(int deltaX, int deltaY)
264 mPlugin->ScrollBy(deltaX, deltaY);
267 void WebEngine::SetScrollPosition(int x, int y)
269 mPlugin->SetScrollPosition(x, y);
272 Dali::Vector2 WebEngine::GetScrollPosition() const
274 return mPlugin->GetScrollPosition();
277 Dali::Vector2 WebEngine::GetScrollSize() const
279 return mPlugin->GetScrollSize();
282 Dali::Vector2 WebEngine::GetContentSize() const
284 return mPlugin->GetContentSize();
287 void WebEngine::RegisterJavaScriptAlertCallback(Dali::WebEnginePlugin::JavaScriptAlertCallback callback)
289 mPlugin->RegisterJavaScriptAlertCallback(callback);
292 void WebEngine::JavaScriptAlertReply()
294 mPlugin->JavaScriptAlertReply();
297 void WebEngine::RegisterJavaScriptConfirmCallback(Dali::WebEnginePlugin::JavaScriptConfirmCallback callback)
299 mPlugin->RegisterJavaScriptAlertCallback(callback);
302 void WebEngine::JavaScriptConfirmReply(bool confirmed)
304 mPlugin->JavaScriptConfirmReply(confirmed);
307 void WebEngine::RegisterJavaScriptPromptCallback(Dali::WebEnginePlugin::JavaScriptPromptCallback callback)
309 mPlugin->RegisterJavaScriptPromptCallback(callback);
312 void WebEngine::JavaScriptPromptReply(const std::string& result)
314 mPlugin->JavaScriptPromptReply(result);
317 bool WebEngine::CanGoForward()
319 return mPlugin->CanGoForward();
322 void WebEngine::GoForward()
324 mPlugin->GoForward();
327 bool WebEngine::CanGoBack()
329 return mPlugin->CanGoBack();
332 void WebEngine::GoBack()
337 void WebEngine::EvaluateJavaScript(const std::string& script, std::function<void(const std::string&)> resultHandler)
339 mPlugin->EvaluateJavaScript(script, resultHandler);
342 void WebEngine::AddJavaScriptMessageHandler(const std::string& exposedObjectName, std::function<void(const std::string&)> handler)
344 mPlugin->AddJavaScriptMessageHandler(exposedObjectName, handler);
347 void WebEngine::ClearAllTilesResources()
349 mPlugin->ClearAllTilesResources();
352 void WebEngine::ClearHistory()
354 mPlugin->ClearHistory();
357 void WebEngine::SetSize(int width, int height)
359 mPlugin->SetSize(width, height);
362 void WebEngine::EnableMouseEvents(bool enabled)
364 mPlugin->EnableMouseEvents(enabled);
367 void WebEngine::EnableKeyEvents(bool enabled)
369 mPlugin->EnableKeyEvents(enabled);
372 bool WebEngine::SendTouchEvent(const Dali::TouchEvent& touch)
374 return mPlugin->SendTouchEvent(touch);
377 bool WebEngine::SendKeyEvent(const Dali::KeyEvent& event)
379 return mPlugin->SendKeyEvent(event);
382 void WebEngine::SetFocus(bool focused)
384 mPlugin->SetFocus(focused);
387 void WebEngine::SetDocumentBackgroundColor(Dali::Vector4 color)
389 mPlugin->SetDocumentBackgroundColor(color);
392 void WebEngine::ClearTilesWhenHidden(bool cleared)
394 mPlugin->ClearTilesWhenHidden(cleared);
397 void WebEngine::SetTileCoverAreaMultiplier(float multiplier)
399 mPlugin->SetTileCoverAreaMultiplier(multiplier);
402 void WebEngine::EnableCursorByClient(bool enabled)
404 mPlugin->EnableCursorByClient(enabled);
407 std::string WebEngine::GetSelectedText() const
409 return mPlugin->GetSelectedText();
412 void WebEngine::UpdateDisplayArea(Dali::Rect<int> displayArea)
414 mPlugin->UpdateDisplayArea(displayArea);
417 void WebEngine::EnableVideoHole(bool enabled)
419 mPlugin->EnableVideoHole(enabled);
422 bool WebEngine::SendHoverEvent( const Dali::HoverEvent& event )
424 return mPlugin->SendHoverEvent( event );
427 bool WebEngine::SendWheelEvent( const Dali::WheelEvent& event )
429 return mPlugin->SendWheelEvent( event );
432 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadStartedSignal()
434 return mPlugin->PageLoadStartedSignal();
437 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadInProgressSignal()
439 return mPlugin->PageLoadInProgressSignal();
442 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadFinishedSignal()
444 return mPlugin->PageLoadFinishedSignal();
447 Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType& WebEngine::PageLoadErrorSignal()
449 return mPlugin->PageLoadErrorSignal();
452 Dali::WebEnginePlugin::WebEngineScrollEdgeReachedSignalType& WebEngine::ScrollEdgeReachedSignal()
454 return mPlugin->ScrollEdgeReachedSignal();
457 Dali::WebEnginePlugin::WebEngineUrlChangedSignalType& WebEngine::UrlChangedSignal()
459 return mPlugin->UrlChangedSignal();
462 } // namespace Adaptor
463 } // namespace Internal