Support policy decision in web engine.
[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-policy-decision.h>
36 #include <dali/devel-api/adaptor-framework/web-engine-settings.h>
37 #include <dali/internal/system/common/environment-variables.h>
38 #include <dali/public-api/adaptor-framework/native-image-source.h>
39 #include <dali/public-api/images/pixel-data.h>
40
41 namespace Dali
42 {
43 namespace Internal
44 {
45 namespace Adaptor
46 {
47 namespace // unnamed namespace
48 {
49 constexpr char const* const kPluginFullNamePrefix  = "libdali2-web-engine-";
50 constexpr char const* const kPluginFullNamePostfix = "-plugin.so";
51 constexpr char const* const kPluginFullNameDefault = "libdali2-web-engine-plugin.so";
52
53 // Note: Dali WebView policy does not allow to use multiple web engines in an application.
54 // So once pluginName is set to non-empty string, it will not change.
55 std::string pluginName;
56
57 std::string MakePluginName(const char* environmentName)
58 {
59   std::stringstream fullName;
60   fullName << kPluginFullNamePrefix << environmentName << kPluginFullNamePostfix;
61   return std::move(fullName.str());
62 }
63
64 Dali::BaseHandle Create()
65 {
66   return Dali::WebEngine::New();
67 }
68
69 Dali::TypeRegistration type(typeid(Dali::WebEngine), typeid(Dali::BaseHandle), Create);
70
71 } // unnamed namespace
72
73 WebEnginePtr WebEngine::New()
74 {
75   WebEngine* instance = new WebEngine();
76
77   if(!instance->Initialize())
78   {
79     delete instance;
80     return nullptr;
81   }
82
83   return instance;
84 }
85
86 WebEngine::WebEngine()
87 : mPlugin(NULL),
88   mHandle(NULL),
89   mCreateWebEnginePtr(NULL),
90   mDestroyWebEnginePtr(NULL)
91 {
92 }
93
94 WebEngine::~WebEngine()
95 {
96   if(mHandle != NULL)
97   {
98     if(mDestroyWebEnginePtr != NULL)
99     {
100       mPlugin->Destroy();
101       mDestroyWebEnginePtr(mPlugin);
102     }
103
104     dlclose(mHandle);
105   }
106 }
107
108 bool WebEngine::InitializePluginHandle()
109 {
110   if(pluginName.length() == 0)
111   {
112     // pluginName is not initialized yet.
113     const char* name = EnvironmentVariable::GetEnvironmentVariable(DALI_ENV_WEB_ENGINE_NAME);
114     if(name)
115     {
116       pluginName = MakePluginName(name);
117       mHandle    = dlopen(pluginName.c_str(), RTLD_LAZY);
118       if(mHandle)
119       {
120         return true;
121       }
122     }
123     pluginName = std::string(kPluginFullNameDefault);
124   }
125
126   mHandle = dlopen(pluginName.c_str(), RTLD_LAZY);
127   if(!mHandle)
128   {
129     DALI_LOG_ERROR("Can't load %s : %s\n", pluginName.c_str(), dlerror());
130     return false;
131   }
132
133   return true;
134 }
135
136 bool WebEngine::Initialize()
137 {
138   char* error = NULL;
139
140   if(!InitializePluginHandle())
141   {
142     return false;
143   }
144
145   mCreateWebEnginePtr = reinterpret_cast<CreateWebEngineFunction>(dlsym(mHandle, "CreateWebEnginePlugin"));
146   if(mCreateWebEnginePtr == NULL)
147   {
148     DALI_LOG_ERROR("Can't load symbol CreateWebEnginePlugin(), error: %s\n", error);
149     return false;
150   }
151
152   mDestroyWebEnginePtr = reinterpret_cast<DestroyWebEngineFunction>(dlsym(mHandle, "DestroyWebEnginePlugin"));
153
154   if(mDestroyWebEnginePtr == NULL)
155   {
156     DALI_LOG_ERROR("Can't load symbol DestroyWebEnginePlugin(), error: %s\n", error);
157     return false;
158   }
159
160   mPlugin = mCreateWebEnginePtr();
161
162   if(mPlugin == NULL)
163   {
164     DALI_LOG_ERROR("Can't create the WebEnginePlugin object\n");
165     return false;
166   }
167
168   return true;
169 }
170
171 void WebEngine::Create(int width, int height, const std::string& locale, const std::string& timezoneId)
172 {
173   mPlugin->Create(width, height, locale, timezoneId);
174 }
175
176 void WebEngine::Create(int width, int height, int argc, char** argv)
177 {
178   mPlugin->Create(width, height, argc, argv);
179 }
180
181 void WebEngine::Destroy()
182 {
183   mPlugin->Destroy();
184 }
185
186 Dali::NativeImageInterfacePtr WebEngine::GetNativeImageSource()
187 {
188   return mPlugin->GetNativeImageSource();
189 }
190
191 Dali::WebEngineSettings& WebEngine::GetSettings() const
192 {
193   return mPlugin->GetSettings();
194 }
195
196 Dali::WebEngineContext& WebEngine::GetContext() const
197 {
198   return mPlugin->GetContext();
199 }
200
201 Dali::WebEngineCookieManager& WebEngine::GetCookieManager() const
202 {
203   return mPlugin->GetCookieManager();
204 }
205
206 Dali::WebEngineBackForwardList& WebEngine::GetBackForwardList() const
207 {
208   return mPlugin->GetBackForwardList();
209 }
210
211 void WebEngine::LoadUrl(const std::string& url)
212 {
213   mPlugin->LoadUrl(url);
214 }
215
216 std::string WebEngine::GetTitle() const
217 {
218   return mPlugin->GetTitle();
219 }
220
221 Dali::PixelData WebEngine::GetFavicon() const
222 {
223   return mPlugin->GetFavicon();
224 }
225
226 const std::string& WebEngine::GetUrl()
227 {
228   return mPlugin->GetUrl();
229 }
230
231 const std::string& WebEngine::GetUserAgent() const
232 {
233   return mPlugin->GetUserAgent();
234 }
235
236 void WebEngine::SetUserAgent(const std::string& userAgent)
237 {
238   mPlugin->SetUserAgent(userAgent);
239 }
240
241 void WebEngine::LoadHtmlString(const std::string& htmlString)
242 {
243   mPlugin->LoadHtmlString(htmlString);
244 }
245
246 bool WebEngine::LoadHtmlStringOverrideCurrentEntry(const std::string& html, const std::string& basicUri, const std::string& unreachableUrl)
247 {
248   return mPlugin->LoadHtmlStringOverrideCurrentEntry(html, basicUri, unreachableUrl);
249 }
250
251 bool WebEngine::LoadContents(const std::string& contents, uint32_t contentSize, const std::string& mimeType, const std::string& encoding, const std::string& baseUri)
252 {
253   return mPlugin->LoadContents(contents, contentSize, mimeType, encoding, baseUri);
254 }
255
256 void WebEngine::Reload()
257 {
258   mPlugin->Reload();
259 }
260
261 bool WebEngine::ReloadWithoutCache()
262 {
263   return mPlugin->ReloadWithoutCache();
264 }
265
266 void WebEngine::StopLoading()
267 {
268   mPlugin->StopLoading();
269 }
270
271 void WebEngine::Suspend()
272 {
273   mPlugin->Suspend();
274 }
275
276 void WebEngine::Resume()
277 {
278   mPlugin->Resume();
279 }
280
281 void WebEngine::SuspendNetworkLoading()
282 {
283   mPlugin->SuspendNetworkLoading();
284 }
285
286 void WebEngine::ResumeNetworkLoading()
287 {
288   mPlugin->ResumeNetworkLoading();
289 }
290
291 bool WebEngine::AddCustomHeader(const std::string& name, const std::string& value)
292 {
293   return mPlugin->AddCustomHeader(name, value);
294 }
295
296 bool WebEngine::RemoveCustomHeader(const std::string& name)
297 {
298   return mPlugin->RemoveCustomHeader(name);
299 }
300
301 uint32_t WebEngine::StartInspectorServer(uint32_t port)
302 {
303   return mPlugin->StartInspectorServer(port);
304 }
305
306 bool WebEngine::StopInspectorServer()
307 {
308   return mPlugin->StopInspectorServer();
309 }
310
311 void WebEngine::ScrollBy(int deltaX, int deltaY)
312 {
313   mPlugin->ScrollBy(deltaX, deltaY);
314 }
315
316 bool WebEngine::ScrollEdgeBy(int deltaX, int deltaY)
317 {
318   return mPlugin->ScrollEdgeBy(deltaX, deltaY);
319 }
320
321 void WebEngine::SetScrollPosition(int x, int y)
322 {
323   mPlugin->SetScrollPosition(x, y);
324 }
325
326 Dali::Vector2 WebEngine::GetScrollPosition() const
327 {
328   return mPlugin->GetScrollPosition();
329 }
330
331 Dali::Vector2 WebEngine::GetScrollSize() const
332 {
333   return mPlugin->GetScrollSize();
334 }
335
336 Dali::Vector2 WebEngine::GetContentSize() const
337 {
338   return mPlugin->GetContentSize();
339 }
340
341 void WebEngine::RegisterJavaScriptAlertCallback(Dali::WebEnginePlugin::JavaScriptAlertCallback callback)
342 {
343   mPlugin->RegisterJavaScriptAlertCallback(callback);
344 }
345
346 void WebEngine::JavaScriptAlertReply()
347 {
348   mPlugin->JavaScriptAlertReply();
349 }
350
351 void WebEngine::RegisterJavaScriptConfirmCallback(Dali::WebEnginePlugin::JavaScriptConfirmCallback callback)
352 {
353   mPlugin->RegisterJavaScriptAlertCallback(callback);
354 }
355
356 void WebEngine::JavaScriptConfirmReply(bool confirmed)
357 {
358   mPlugin->JavaScriptConfirmReply(confirmed);
359 }
360
361 void WebEngine::RegisterJavaScriptPromptCallback(Dali::WebEnginePlugin::JavaScriptPromptCallback callback)
362 {
363   mPlugin->RegisterJavaScriptPromptCallback(callback);
364 }
365
366 void WebEngine::JavaScriptPromptReply(const std::string& result)
367 {
368   mPlugin->JavaScriptPromptReply(result);
369 }
370
371 bool WebEngine::CanGoForward()
372 {
373   return mPlugin->CanGoForward();
374 }
375
376 void WebEngine::GoForward()
377 {
378   mPlugin->GoForward();
379 }
380
381 bool WebEngine::CanGoBack()
382 {
383   return mPlugin->CanGoBack();
384 }
385
386 void WebEngine::GoBack()
387 {
388   mPlugin->GoBack();
389 }
390
391 void WebEngine::EvaluateJavaScript(const std::string& script, std::function<void(const std::string&)> resultHandler)
392 {
393   mPlugin->EvaluateJavaScript(script, resultHandler);
394 }
395
396 void WebEngine::AddJavaScriptMessageHandler(const std::string& exposedObjectName, std::function<void(const std::string&)> handler)
397 {
398   mPlugin->AddJavaScriptMessageHandler(exposedObjectName, handler);
399 }
400
401 void WebEngine::ClearAllTilesResources()
402 {
403   mPlugin->ClearAllTilesResources();
404 }
405
406 void WebEngine::ClearHistory()
407 {
408   mPlugin->ClearHistory();
409 }
410
411 void WebEngine::SetSize(int width, int height)
412 {
413   mPlugin->SetSize(width, height);
414 }
415
416 void WebEngine::EnableMouseEvents(bool enabled)
417 {
418   mPlugin->EnableMouseEvents(enabled);
419 }
420
421 void WebEngine::EnableKeyEvents(bool enabled)
422 {
423   mPlugin->EnableKeyEvents(enabled);
424 }
425
426 bool WebEngine::SendTouchEvent(const Dali::TouchEvent& touch)
427 {
428   return mPlugin->SendTouchEvent(touch);
429 }
430
431 bool WebEngine::SendKeyEvent(const Dali::KeyEvent& event)
432 {
433   return mPlugin->SendKeyEvent(event);
434 }
435
436 void WebEngine::SetFocus(bool focused)
437 {
438   mPlugin->SetFocus(focused);
439 }
440
441 void WebEngine::SetDocumentBackgroundColor(Dali::Vector4 color)
442 {
443   mPlugin->SetDocumentBackgroundColor(color);
444 }
445
446 void WebEngine::ClearTilesWhenHidden(bool cleared)
447 {
448   mPlugin->ClearTilesWhenHidden(cleared);
449 }
450
451 void WebEngine::SetTileCoverAreaMultiplier(float multiplier)
452 {
453   mPlugin->SetTileCoverAreaMultiplier(multiplier);
454 }
455
456 void WebEngine::EnableCursorByClient(bool enabled)
457 {
458   mPlugin->EnableCursorByClient(enabled);
459 }
460
461 std::string WebEngine::GetSelectedText() const
462 {
463   return mPlugin->GetSelectedText();
464 }
465
466 void WebEngine::SetPageZoomFactor(float zoomFactor)
467 {
468   mPlugin->SetPageZoomFactor(zoomFactor);
469 }
470
471 float WebEngine::GetPageZoomFactor() const
472 {
473   return mPlugin->GetPageZoomFactor();
474 }
475
476 void WebEngine::SetTextZoomFactor(float zoomFactor)
477 {
478   mPlugin->SetTextZoomFactor(zoomFactor);
479 }
480
481 float WebEngine::GetTextZoomFactor() const
482 {
483   return mPlugin->GetTextZoomFactor();
484 }
485
486 float WebEngine::GetLoadProgressPercentage() const
487 {
488   return mPlugin->GetLoadProgressPercentage();
489 }
490
491 void WebEngine::SetScaleFactor(float scaleFactor, Dali::Vector2 point)
492 {
493   mPlugin->SetScaleFactor(scaleFactor, point);
494 }
495
496 float WebEngine::GetScaleFactor() const
497 {
498   return mPlugin->GetScaleFactor();
499 }
500
501 void WebEngine::ActivateAccessibility(bool activated)
502 {
503   mPlugin->ActivateAccessibility(activated);
504 }
505
506 bool WebEngine::SetVisibility(bool visible)
507 {
508   return mPlugin->SetVisibility(visible);
509 }
510
511 bool WebEngine::HighlightText(const std::string& text, Dali::WebEnginePlugin::FindOption options, uint32_t maxMatchCount)
512 {
513   return mPlugin->HighlightText(text, options, maxMatchCount);
514 }
515
516 void WebEngine::AddDynamicCertificatePath(const std::string& host, const std::string& certPath)
517 {
518   mPlugin->AddDynamicCertificatePath(host, certPath);
519 }
520
521 Dali::PixelData WebEngine::GetScreenshot(Dali::Rect<int> viewArea, float scaleFactor)
522 {
523   return mPlugin->GetScreenshot(viewArea, scaleFactor);
524 }
525
526 bool WebEngine::GetScreenshotAsynchronously(Dali::Rect<int> viewArea, float scaleFactor, Dali::WebEnginePlugin::ScreenshotCapturedCallback callback)
527 {
528   return mPlugin->GetScreenshotAsynchronously(viewArea, scaleFactor, callback);
529 }
530
531 bool WebEngine::CheckVideoPlayingAsynchronously(Dali::WebEnginePlugin::VideoPlayingCallback callback)
532 {
533   return mPlugin->CheckVideoPlayingAsynchronously(callback);
534 }
535
536 void WebEngine::RegisterGeolocationPermissionCallback(Dali::WebEnginePlugin::GeolocationPermissionCallback callback)
537 {
538   mPlugin->RegisterGeolocationPermissionCallback(callback);
539 }
540
541 void WebEngine::UpdateDisplayArea(Dali::Rect<int> displayArea)
542 {
543   mPlugin->UpdateDisplayArea(displayArea);
544 }
545
546 void WebEngine::EnableVideoHole(bool enabled)
547 {
548   mPlugin->EnableVideoHole(enabled);
549 }
550
551 bool WebEngine::SendHoverEvent( const Dali::HoverEvent& event )
552 {
553   return mPlugin->SendHoverEvent( event );
554 }
555
556 bool WebEngine::SendWheelEvent( const Dali::WheelEvent& event )
557 {
558   return mPlugin->SendWheelEvent( event );
559 }
560
561 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadStartedSignal()
562 {
563   return mPlugin->PageLoadStartedSignal();
564 }
565
566 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadInProgressSignal()
567 {
568   return mPlugin->PageLoadInProgressSignal();
569 }
570
571 Dali::WebEnginePlugin::WebEnginePageLoadSignalType& WebEngine::PageLoadFinishedSignal()
572 {
573   return mPlugin->PageLoadFinishedSignal();
574 }
575
576 Dali::WebEnginePlugin::WebEnginePageLoadErrorSignalType& WebEngine::PageLoadErrorSignal()
577 {
578   return mPlugin->PageLoadErrorSignal();
579 }
580
581 Dali::WebEnginePlugin::WebEngineScrollEdgeReachedSignalType& WebEngine::ScrollEdgeReachedSignal()
582 {
583   return mPlugin->ScrollEdgeReachedSignal();
584 }
585
586 Dali::WebEnginePlugin::WebEngineUrlChangedSignalType& WebEngine::UrlChangedSignal()
587 {
588   return mPlugin->UrlChangedSignal();
589 }
590
591 Dali::WebEnginePlugin::WebEngineFormRepostDecisionSignalType& WebEngine::FormRepostDecisionSignal()
592 {
593   return mPlugin->FormRepostDecisionSignal();
594 }
595
596 Dali::WebEnginePlugin::WebEngineFrameRenderedSignalType& WebEngine::FrameRenderedSignal()
597 {
598   return mPlugin->FrameRenderedSignal();
599 }
600
601 Dali::WebEnginePlugin::WebEngineRequestInterceptorSignalType& WebEngine::RequestInterceptorSignal()
602 {
603   return mPlugin->RequestInterceptorSignal();
604 }
605
606 Dali::WebEnginePlugin::WebEngineConsoleMessageSignalType& WebEngine::ConsoleMessageSignal()
607 {
608   return mPlugin->ConsoleMessageSignal();
609 }
610
611 Dali::WebEnginePlugin::WebEnginePolicyDecisionSignalType& WebEngine::PolicyDecisionSignal()
612 {
613   return mPlugin->PolicyDecisionSignal();
614 }
615
616 } // namespace Adaptor
617 } // namespace Internal
618 } // namespace Dali