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 bool WebEngine::CanGoForward()
289 return mPlugin->CanGoForward();
292 void WebEngine::GoForward()
294 mPlugin->GoForward();
297 bool WebEngine::CanGoBack()
299 return mPlugin->CanGoBack();
302 void WebEngine::GoBack()
307 void WebEngine::EvaluateJavaScript(const std::string& script, std::function<void(const std::string&)> resultHandler)
309 mPlugin->EvaluateJavaScript(script, resultHandler);
312 void WebEngine::AddJavaScriptMessageHandler(const std::string& exposedObjectName, std::function<void(const std::string&)> handler)
314 mPlugin->AddJavaScriptMessageHandler(exposedObjectName, handler);
317 void WebEngine::ClearAllTilesResources()
319 mPlugin->ClearAllTilesResources();
322 void WebEngine::ClearHistory()
324 mPlugin->ClearHistory();
327 void WebEngine::SetSize(int width, int height)
329 mPlugin->SetSize(width, height);
332 bool WebEngine::SendTouchEvent(const Dali::TouchEvent& touch)
334 return mPlugin->SendTouchEvent(touch);
337 bool WebEngine::SendKeyEvent(const Dali::KeyEvent& event)
339 return mPlugin->SendKeyEvent(event);
342 void WebEngine::SetFocus(bool focused)
344 mPlugin->SetFocus(focused);
347 void WebEngine::UpdateDisplayArea(Dali::Rect<int> displayArea)
349 mPlugin->UpdateDisplayArea(displayArea);
352 void WebEngine::EnableVideoHole(bool enabled)
354 mPlugin->EnableVideoHole(enabled);
357 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadStartedSignal()
359 return mPlugin->PageLoadStartedSignal();
362 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadFinishedSignal()
364 return mPlugin->PageLoadFinishedSignal();
367 Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType& WebEngine::PageLoadErrorSignal()
369 return mPlugin->PageLoadErrorSignal();
372 Dali::WebEnginePlugin::WebEngineScrollEdgeReachedSignalType& WebEngine::ScrollEdgeReachedSignal()
374 return mPlugin->ScrollEdgeReachedSignal();
377 } // namespace Adaptor
378 } // namespace Internal