Merge "Use ModelNode / ModelPrimitive / Material instead of Actor / Renderer" into...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / web-view / web-view-impl.cpp
1 /*
2  * Copyright (c) 2022 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 "web-view-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <cstring>
23 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-back-forward-list.h>
24 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-certificate.h>
25 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-console-message.h>
26 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-context-menu-item.h>
27 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-context-menu.h>
28 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-context.h>
29 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-cookie-manager.h>
30 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-form-repost-decision.h>
31 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-hit-test.h>
32 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-http-auth-handler.h>
33 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-load-error.h>
34 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-policy-decision.h>
35 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-settings.h>
36 #include <dali/devel-api/common/stage.h>
37 #include <dali/devel-api/scripting/enum-helper.h>
38 #include <dali/devel-api/scripting/scripting.h>
39 #include <dali/public-api/adaptor-framework/native-image-source.h>
40 #include <dali/public-api/object/type-registry-helper.h>
41 #include <dali/public-api/object/type-registry.h>
42
43 // INTERNAL INCLUDES
44 #include <dali-toolkit/devel-api/controls/control-devel.h>
45 #include <dali-toolkit/devel-api/controls/web-view/web-back-forward-list.h>
46 #include <dali-toolkit/devel-api/controls/web-view/web-settings.h>
47 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
48 #include <dali-toolkit/public-api/image-loader/image.h>
49 #include <dali-toolkit/public-api/image-loader/image-url.h>
50 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
51
52 #include <functional>
53 #include <memory>
54 #include <unordered_map>
55
56 namespace Dali
57 {
58 namespace Toolkit
59 {
60 namespace Internal
61 {
62 namespace
63 {
64 BaseHandle Create()
65 {
66   return Toolkit::WebView::New();
67 }
68
69 // clang-format off
70 DALI_TYPE_REGISTRATION_BEGIN(Toolkit::WebView, Toolkit::Control, Create)
71
72 DALI_PROPERTY_REGISTRATION(Toolkit, WebView, "url",                     STRING,  URL                       )
73 DALI_PROPERTY_REGISTRATION(Toolkit, WebView, "userAgent",               STRING,  USER_AGENT                )
74 DALI_PROPERTY_REGISTRATION(Toolkit, WebView, "scrollPosition",          VECTOR2, SCROLL_POSITION           )
75 DALI_PROPERTY_REGISTRATION(Toolkit, WebView, "scrollSize",              VECTOR2, SCROLL_SIZE               )
76 DALI_PROPERTY_REGISTRATION(Toolkit, WebView, "contentSize",             VECTOR2, CONTENT_SIZE              )
77 DALI_PROPERTY_REGISTRATION(Toolkit, WebView, "title",                   STRING,  TITLE                     )
78 DALI_PROPERTY_REGISTRATION(Toolkit, WebView, "videoHoleEnabled",        BOOLEAN, VIDEO_HOLE_ENABLED        )
79 DALI_PROPERTY_REGISTRATION(Toolkit, WebView, "mouseEventsEnabled",      BOOLEAN, MOUSE_EVENTS_ENABLED      )
80 DALI_PROPERTY_REGISTRATION(Toolkit, WebView, "keyEventsEnabled",        BOOLEAN, KEY_EVENTS_ENABLED        )
81 DALI_PROPERTY_REGISTRATION(Toolkit, WebView, "documentBackgroundColor", VECTOR4, DOCUMENT_BACKGROUND_COLOR )
82 DALI_PROPERTY_REGISTRATION(Toolkit, WebView, "tilesClearedWhenHidden",  BOOLEAN, TILES_CLEARED_WHEN_HIDDEN )
83 DALI_PROPERTY_REGISTRATION(Toolkit, WebView, "tileCoverAreaMultiplier", FLOAT,   TILE_COVER_AREA_MULTIPLIER)
84 DALI_PROPERTY_REGISTRATION(Toolkit, WebView, "cursorEnabledByClient",   BOOLEAN, CURSOR_ENABLED_BY_CLIENT  )
85 DALI_PROPERTY_REGISTRATION(Toolkit, WebView, "selectedText",            STRING,  SELECTED_TEXT             )
86 DALI_PROPERTY_REGISTRATION(Toolkit, WebView, "pageZoomFactor",          FLOAT,   PAGE_ZOOM_FACTOR          )
87 DALI_PROPERTY_REGISTRATION(Toolkit, WebView, "textZoomFactor",          FLOAT,   TEXT_ZOOM_FACTOR          )
88 DALI_PROPERTY_REGISTRATION(Toolkit, WebView, "loadProgressPercentage",  FLOAT,   LOAD_PROGRESS_PERCENTAGE  )
89
90 DALI_TYPE_REGISTRATION_END()
91 // clang-format on
92
93 std::unordered_map<Dali::WebEnginePlugin*, Dali::WeakHandle<Toolkit::WebView>>& GetPluginWebViewTable()
94 {
95   static std::unordered_map<Dali::WebEnginePlugin*, Dali::WeakHandle<Toolkit::WebView>> pluginWebViewMap;
96   return pluginWebViewMap;
97 }
98 } // namespace
99
100 WebView::WebView(const std::string& locale, const std::string& timezoneId)
101 : Control(ControlBehaviour(ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS)),
102   mVisual(),
103   mWebViewSize(Stage::GetCurrent().GetSize()),
104   mWebEngine(),
105   mWebViewArea(0, 0, mWebViewSize.width, mWebViewSize.height),
106   mVideoHoleEnabled(false),
107   mMouseEventsEnabled(true),
108   mKeyEventsEnabled(true),
109   mScreenshotCapturedCallback{nullptr},
110   mFrameRenderedCallback{nullptr}
111 {
112   mWebEngine = Dali::WebEngine::New();
113
114   // WebEngine is empty when it is not properly initialized.
115   if(mWebEngine)
116   {
117     mWebEngine.Create(mWebViewSize.width, mWebViewSize.height, locale, timezoneId);
118   }
119 }
120
121 WebView::WebView(uint32_t argc, char** argv)
122 : Control(ControlBehaviour(ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS)),
123   mVisual(),
124   mWebViewSize(Stage::GetCurrent().GetSize()),
125   mWebEngine(),
126   mWebViewArea(0, 0, mWebViewSize.width, mWebViewSize.height),
127   mVideoHoleEnabled(false),
128   mMouseEventsEnabled(true),
129   mKeyEventsEnabled(true),
130   mScreenshotCapturedCallback{nullptr},
131   mFrameRenderedCallback{nullptr}
132 {
133   mWebEngine = Dali::WebEngine::New();
134
135   // WebEngine is empty when it is not properly initialized.
136   if(mWebEngine)
137   {
138     mWebEngine.Create(mWebViewSize.width, mWebViewSize.height, argc, argv);
139   }
140 }
141
142 WebView::WebView()
143 : WebView("", "")
144 {
145 }
146
147 WebView::~WebView()
148 {
149   if(mWebEngine)
150   {
151     auto iter = GetPluginWebViewTable().find(mWebEngine.GetPlugin());
152     if (iter != GetPluginWebViewTable().end())
153     {
154       GetPluginWebViewTable().erase(iter);
155     }
156     mWebEngine.Destroy();
157   }
158 }
159
160 Toolkit::WebView WebView::New()
161 {
162   WebView*         impl   = new WebView();
163   Toolkit::WebView handle = Toolkit::WebView(*impl);
164   if (impl->GetPlugin())
165   {
166     GetPluginWebViewTable()[impl->GetPlugin()] = handle;
167   }
168   impl->Initialize();
169   return handle;
170 }
171
172 Toolkit::WebView WebView::New(const std::string& locale, const std::string& timezoneId)
173 {
174   WebView*         impl   = new WebView(locale, timezoneId);
175   Toolkit::WebView handle = Toolkit::WebView(*impl);
176   if (impl->GetPlugin())
177   {
178     GetPluginWebViewTable()[impl->GetPlugin()] = handle;
179   }
180   impl->Initialize();
181   return handle;
182 }
183
184 Toolkit::WebView WebView::New(uint32_t argc, char** argv)
185 {
186   WebView*         impl   = new WebView(argc, argv);
187   Toolkit::WebView handle = Toolkit::WebView(*impl);
188   if (impl->GetPlugin())
189   {
190     GetPluginWebViewTable()[impl->GetPlugin()] = handle;
191   }
192   impl->Initialize();
193   return handle;
194 }
195
196 Toolkit::WebView WebView::FindWebView(Dali::WebEnginePlugin* plugin)
197 {
198   auto iter = GetPluginWebViewTable().find(plugin);
199   if (iter != GetPluginWebViewTable().end())
200   {
201     return iter->second.GetHandle();
202   }
203   return Toolkit::WebView();
204 }
205
206 Dali::WebEngineContext* WebView::GetContext()
207 {
208   return Dali::WebEngine::GetContext();
209 }
210
211 Dali::WebEngineCookieManager* WebView::GetCookieManager()
212 {
213   return Dali::WebEngine::GetCookieManager();
214 }
215
216 void WebView::OnInitialize()
217 {
218   Actor self = Self();
219
220   self.SetProperty(Actor::Property::KEYBOARD_FOCUSABLE, true);
221   self.SetProperty(DevelActor::Property::TOUCH_FOCUSABLE, true);
222   self.TouchedSignal().Connect(this, &WebView::OnTouchEvent);
223   self.HoveredSignal().Connect(this, &WebView::OnHoverEvent);
224   self.WheelEventSignal().Connect(this, &WebView::OnWheelEvent);
225   Dali::DevelActor::VisibilityChangedSignal(self).Connect(this, &WebView::OnVisibilityChanged);
226
227   mPositionUpdateNotification = self.AddPropertyNotification(Actor::Property::WORLD_POSITION, StepCondition(1.0f, 1.0f));
228   mSizeUpdateNotification     = self.AddPropertyNotification(Actor::Property::SIZE, StepCondition(1.0f, 1.0f));
229   mScaleUpdateNotification    = self.AddPropertyNotification(Actor::Property::WORLD_SCALE, StepCondition(0.1f, 1.0f));
230   mPositionUpdateNotification.NotifySignal().Connect(this, &WebView::OnDisplayAreaUpdated);
231   mSizeUpdateNotification.NotifySignal().Connect(this, &WebView::OnDisplayAreaUpdated);
232   mScaleUpdateNotification.NotifySignal().Connect(this, &WebView::OnDisplayAreaUpdated);
233
234   if(mWebEngine)
235   {
236     mWebEngine.RegisterFrameRenderedCallback(std::bind(&WebView::OnFrameRendered, this));
237     mWebSettings        = std::unique_ptr<Dali::Toolkit::WebSettings>(new WebSettings(mWebEngine.GetSettings()));
238     mWebBackForwardList = std::unique_ptr<Dali::Toolkit::WebBackForwardList>(new WebBackForwardList(mWebEngine.GetBackForwardList()));
239   }
240
241   self.SetProperty(DevelControl::Property::ACCESSIBILITY_ROLE, Dali::Accessibility::Role::FILLER);
242 }
243
244 DevelControl::ControlAccessible* WebView::CreateAccessibleObject()
245 {
246   return new WebViewAccessible(Self(), mWebEngine);
247 }
248
249 Dali::Toolkit::WebSettings* WebView::GetSettings() const
250 {
251   return mWebSettings.get();
252 }
253
254 Dali::Toolkit::WebBackForwardList* WebView::GetBackForwardList() const
255 {
256   return mWebBackForwardList.get();
257 }
258
259 Dali::WebEnginePlugin* WebView::GetPlugin() const
260 {
261   return mWebEngine ? mWebEngine.GetPlugin() : nullptr;
262 }
263
264 Dali::Toolkit::ImageView WebView::GetFavicon() const
265 {
266   Dali::Toolkit::ImageView faviconView;
267   if(mWebEngine)
268   {
269     Dali::PixelData pixelData = mWebEngine.GetFavicon();
270     faviconView               = CreateImageView(pixelData);
271   }
272   return faviconView;
273 }
274
275 void WebView::LoadUrl(const std::string& url)
276 {
277   if(mWebEngine)
278   {
279     mWebEngine.LoadUrl(url);
280   }
281 }
282
283 void WebView::LoadHtmlString(const std::string& htmlString)
284 {
285   if(mWebEngine)
286   {
287     mWebEngine.LoadHtmlString(htmlString);
288   }
289 }
290
291 bool WebView::LoadHtmlStringOverrideCurrentEntry(const std::string& html, const std::string& basicUri, const std::string& unreachableUrl)
292 {
293   if(!mWebEngine)
294     return false;
295
296   return mWebEngine.LoadHtmlStringOverrideCurrentEntry(html, basicUri, unreachableUrl);
297 }
298
299 bool WebView::LoadContents(const std::string& contents, uint32_t contentSize, const std::string& mimeType, const std::string& encoding, const std::string& baseUri)
300 {
301   if(!mWebEngine)
302     return false;
303
304   return mWebEngine.LoadContents(contents, contentSize, mimeType, encoding, baseUri);
305 }
306
307 void WebView::Reload()
308 {
309   if(mWebEngine)
310   {
311     mWebEngine.Reload();
312   }
313 }
314
315 bool WebView::ReloadWithoutCache()
316 {
317   return mWebEngine ? mWebEngine.ReloadWithoutCache() : false;
318 }
319
320 void WebView::StopLoading()
321 {
322   if(mWebEngine)
323   {
324     mWebEngine.StopLoading();
325   }
326 }
327
328 void WebView::Suspend()
329 {
330   if(mWebEngine)
331   {
332     mWebEngine.Suspend();
333   }
334 }
335
336 void WebView::Resume()
337 {
338   if(mWebEngine)
339   {
340     mWebEngine.Resume();
341   }
342 }
343
344 void WebView::SuspendNetworkLoading()
345 {
346   if(mWebEngine)
347   {
348     mWebEngine.SuspendNetworkLoading();
349   }
350 }
351
352 void WebView::ResumeNetworkLoading()
353 {
354   if(mWebEngine)
355   {
356     mWebEngine.ResumeNetworkLoading();
357   }
358 }
359
360 bool WebView::AddCustomHeader(const std::string& name, const std::string& value)
361 {
362   return mWebEngine ? mWebEngine.AddCustomHeader(name, value) : false;
363 }
364
365 bool WebView::RemoveCustomHeader(const std::string& name)
366 {
367   return mWebEngine ? mWebEngine.RemoveCustomHeader(name) : false;
368 }
369
370 uint32_t WebView::StartInspectorServer(uint32_t port)
371 {
372   return mWebEngine ? mWebEngine.StartInspectorServer(port) : false;
373 }
374
375 bool WebView::StopInspectorServer()
376 {
377   return mWebEngine ? mWebEngine.StopInspectorServer() : false;
378 }
379
380 void WebView::ScrollBy(int32_t deltaX, int32_t deltaY)
381 {
382   if(mWebEngine)
383   {
384     mWebEngine.ScrollBy(deltaX, deltaY);
385   }
386 }
387
388 bool WebView::ScrollEdgeBy(int32_t deltaX, int32_t deltaY)
389 {
390   return mWebEngine ? mWebEngine.ScrollEdgeBy(deltaX, deltaY) : false;
391 }
392
393 bool WebView::CanGoForward()
394 {
395   return mWebEngine ? mWebEngine.CanGoForward() : false;
396 }
397
398 void WebView::GoForward()
399 {
400   if(mWebEngine)
401   {
402     mWebEngine.GoForward();
403   }
404 }
405
406 bool WebView::CanGoBack()
407 {
408   return mWebEngine ? mWebEngine.CanGoBack() : false;
409 }
410
411 void WebView::GoBack()
412 {
413   if(mWebEngine)
414   {
415     mWebEngine.GoBack();
416   }
417 }
418
419 void WebView::EvaluateJavaScript(const std::string& script, std::function<void(const std::string&)> resultHandler)
420 {
421   if(mWebEngine)
422   {
423     mWebEngine.EvaluateJavaScript(script, resultHandler);
424   }
425 }
426
427 void WebView::AddJavaScriptMessageHandler(const std::string& exposedObjectName, std::function<void(const std::string&)> handler)
428 {
429   if(mWebEngine)
430   {
431     mWebEngine.AddJavaScriptMessageHandler(exposedObjectName, handler);
432   }
433 }
434
435 void WebView::RegisterJavaScriptAlertCallback(Dali::WebEnginePlugin::JavaScriptAlertCallback callback)
436 {
437   if(mWebEngine)
438   {
439     mWebEngine.RegisterJavaScriptAlertCallback(callback);
440   }
441 }
442
443 void WebView::JavaScriptAlertReply()
444 {
445   if(mWebEngine)
446   {
447     mWebEngine.JavaScriptAlertReply();
448   }
449 }
450
451 void WebView::RegisterJavaScriptConfirmCallback(Dali::WebEnginePlugin::JavaScriptConfirmCallback callback)
452 {
453   if(mWebEngine)
454   {
455     mWebEngine.RegisterJavaScriptConfirmCallback(callback);
456   }
457 }
458
459 void WebView::JavaScriptConfirmReply(bool confirmed)
460 {
461   if(mWebEngine)
462   {
463     mWebEngine.JavaScriptConfirmReply(confirmed);
464   }
465 }
466
467 void WebView::RegisterJavaScriptPromptCallback(Dali::WebEnginePlugin::JavaScriptPromptCallback callback)
468 {
469   if(mWebEngine)
470   {
471     mWebEngine.RegisterJavaScriptPromptCallback(callback);
472   }
473 }
474
475 void WebView::JavaScriptPromptReply(const std::string& result)
476 {
477   if(mWebEngine)
478   {
479     mWebEngine.JavaScriptPromptReply(result);
480   }
481 }
482
483 std::unique_ptr<Dali::WebEngineHitTest> WebView::CreateHitTest(int32_t x, int32_t y, Dali::WebEngineHitTest::HitTestMode mode)
484 {
485   std::unique_ptr<Dali::WebEngineHitTest> webHitTest;
486   if(!mWebEngine)
487   {
488     return webHitTest;
489   }
490
491   return mWebEngine.CreateHitTest(x, y, mode);
492 }
493
494 bool WebView::CreateHitTestAsynchronously(int32_t x, int32_t y, Dali::WebEngineHitTest::HitTestMode mode, Dali::WebEnginePlugin::WebEngineHitTestCreatedCallback callback)
495 {
496   bool result = false;
497   if(mWebEngine)
498   {
499     result = mWebEngine.CreateHitTestAsynchronously(x, y, mode, callback);
500   }
501   return result;
502 }
503
504 void WebView::ClearHistory()
505 {
506   if(mWebEngine)
507   {
508     mWebEngine.ClearHistory();
509   }
510 }
511
512 void WebView::ClearAllTilesResources()
513 {
514   if(mWebEngine)
515   {
516     mWebEngine.ClearAllTilesResources();
517   }
518 }
519
520 void WebView::SetScaleFactor(float scaleFactor, Dali::Vector2 point)
521 {
522   if(mWebEngine)
523   {
524     mWebEngine.SetScaleFactor(scaleFactor, point);
525   }
526 }
527
528 float WebView::GetScaleFactor() const
529 {
530   return mWebEngine ? mWebEngine.GetScaleFactor() : 0.0f;
531 }
532
533 void WebView::ActivateAccessibility(bool activated)
534 {
535   if(mWebEngine)
536   {
537     mWebEngine.ActivateAccessibility(activated);
538   }
539 }
540
541 bool WebView::HighlightText(const std::string& text, Dali::WebEnginePlugin::FindOption options, uint32_t maxMatchCount)
542 {
543   return mWebEngine ? mWebEngine.HighlightText(text, options, maxMatchCount) : false;
544 }
545
546 void WebView::AddDynamicCertificatePath(const std::string& host, const std::string& certPath)
547 {
548   if(mWebEngine)
549   {
550     mWebEngine.AddDynamicCertificatePath(host, certPath);
551   }
552 }
553
554 Dali::Toolkit::ImageView WebView::GetScreenshot(Dali::Rect<int32_t> viewArea, float scaleFactor)
555 {
556   Dali::Toolkit::ImageView imageView;
557   if(mWebEngine)
558   {
559     Dali::PixelData pixelData = mWebEngine.GetScreenshot(viewArea, scaleFactor);
560     imageView                 = CreateImageView(pixelData);
561   }
562   return imageView;
563 }
564
565 bool WebView::GetScreenshotAsynchronously(Dali::Rect<int32_t> viewArea, float scaleFactor, Dali::Toolkit::WebView::WebViewScreenshotCapturedCallback callback)
566 {
567   mScreenshotCapturedCallback = callback;
568   return mWebEngine ? mWebEngine.GetScreenshotAsynchronously(viewArea, scaleFactor, std::bind(&WebView::OnScreenshotCaptured, this, std::placeholders::_1)) : false;
569 }
570
571 bool WebView::CheckVideoPlayingAsynchronously(Dali::WebEnginePlugin::VideoPlayingCallback callback)
572 {
573   return mWebEngine ? mWebEngine.CheckVideoPlayingAsynchronously(callback) : false;
574 }
575
576 void WebView::RegisterGeolocationPermissionCallback(Dali::WebEnginePlugin::GeolocationPermissionCallback callback)
577 {
578   if(mWebEngine)
579   {
580     mWebEngine.RegisterGeolocationPermissionCallback(callback);
581   }
582 }
583
584 void WebView::SetTtsFocus(bool focused)
585 {
586   if(mWebEngine && !HasKeyInputFocus())
587   {
588     mWebEngine.SetFocus(focused);
589   }
590 }
591
592 void WebView::EnableVideoHole(bool enabled)
593 {
594   mVideoHoleEnabled = enabled;
595
596   EnableBlendMode(!mVideoHoleEnabled);
597
598   if(mWebEngine)
599   {
600     mWebEngine.EnableVideoHole(mVideoHoleEnabled);
601   }
602 }
603
604 void WebView::EnableBlendMode(bool blendEnabled)
605 {
606   Actor self = Self();
607   for(uint32_t i = 0; i < self.GetRendererCount(); i++)
608   {
609     Dali::Renderer render = self.GetRendererAt(i);
610     render.SetProperty(Renderer::Property::BLEND_MODE, blendEnabled ? BlendMode::ON : BlendMode::OFF);
611   }
612 }
613
614 Dali::Toolkit::ImageView WebView::CreateImageView(Dali::PixelData pixel) const
615 {
616   if(!pixel)
617   {
618     return Dali::Toolkit::ImageView();
619   }
620
621   Dali::Toolkit::ImageUrl  url       = Dali::Toolkit::Image::GenerateUrl(pixel);
622   Dali::Toolkit::ImageView imageView = Dali::Toolkit::ImageView::New(url.GetUrl());
623   imageView.SetProperty(Dali::Actor::Property::SIZE, Vector2(pixel.GetWidth(), pixel.GetHeight()));
624   return imageView;
625 }
626
627 void WebView::RegisterPageLoadStartedCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback)
628 {
629   if(mWebEngine)
630   {
631     mWebEngine.RegisterPageLoadStartedCallback(callback);
632   }
633 }
634
635 void WebView::RegisterPageLoadInProgressCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback)
636 {
637   if(mWebEngine)
638   {
639     mWebEngine.RegisterPageLoadInProgressCallback(callback);
640   }
641 }
642
643 void WebView::RegisterPageLoadFinishedCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback)
644 {
645   if(mWebEngine)
646   {
647     mWebEngine.RegisterPageLoadFinishedCallback(callback);
648   }
649 }
650
651 void WebView::RegisterPageLoadErrorCallback(Dali::WebEnginePlugin::WebEnginePageLoadErrorCallback callback)
652 {
653   if(mWebEngine)
654   {
655     mWebEngine.RegisterPageLoadErrorCallback(callback);
656   }
657 }
658
659 void WebView::RegisterScrollEdgeReachedCallback(Dali::WebEnginePlugin::WebEngineScrollEdgeReachedCallback callback)
660 {
661   if(mWebEngine)
662   {
663     mWebEngine.RegisterScrollEdgeReachedCallback(callback);
664   }
665 }
666
667 void WebView::RegisterUrlChangedCallback(Dali::WebEnginePlugin::WebEngineUrlChangedCallback callback)
668 {
669   if(mWebEngine)
670   {
671     mWebEngine.RegisterUrlChangedCallback(callback);
672   }
673 }
674
675 void WebView::RegisterFormRepostDecidedCallback(Dali::WebEnginePlugin::WebEngineFormRepostDecidedCallback callback)
676 {
677   if(mWebEngine)
678   {
679     mWebEngine.RegisterFormRepostDecidedCallback(callback);
680   }
681 }
682
683 void WebView::RegisterFrameRenderedCallback(Dali::WebEnginePlugin::WebEngineFrameRenderedCallback callback)
684 {
685   mFrameRenderedCallback = callback;
686 }
687
688 void WebView::RegisterConsoleMessageReceivedCallback(Dali::WebEnginePlugin::WebEngineConsoleMessageReceivedCallback callback)
689 {
690   if(mWebEngine)
691   {
692     mWebEngine.RegisterConsoleMessageReceivedCallback(callback);
693   }
694 }
695
696 void WebView::RegisterResponsePolicyDecidedCallback(Dali::WebEnginePlugin::WebEngineResponsePolicyDecidedCallback callback)
697 {
698   if(mWebEngine)
699   {
700     mWebEngine.RegisterResponsePolicyDecidedCallback(callback);
701   }
702 }
703
704 void WebView::RegisterNavigationPolicyDecidedCallback(Dali::WebEnginePlugin::WebEngineNavigationPolicyDecidedCallback callback)
705 {
706   if(mWebEngine)
707   {
708     mWebEngine.RegisterNavigationPolicyDecidedCallback(callback);
709   }
710 }
711
712 void WebView::RegisterNewWindowCreatedCallback(Dali::WebEnginePlugin::WebEngineNewWindowCreatedCallback callback)
713 {
714   if(mWebEngine)
715   {
716     mWebEngine.RegisterNewWindowCreatedCallback(callback);
717   }
718 }
719
720 void WebView::RegisterCertificateConfirmedCallback(Dali::WebEnginePlugin::WebEngineCertificateCallback callback)
721 {
722   if(mWebEngine)
723   {
724     mWebEngine.RegisterCertificateConfirmedCallback(callback);
725   }
726 }
727
728 void WebView::RegisterSslCertificateChangedCallback(Dali::WebEnginePlugin::WebEngineCertificateCallback callback)
729 {
730   if(mWebEngine)
731   {
732     mWebEngine.RegisterSslCertificateChangedCallback(callback);
733   }
734 }
735
736 void WebView::RegisterHttpAuthHandlerCallback(Dali::WebEnginePlugin::WebEngineHttpAuthHandlerCallback callback)
737 {
738   if(mWebEngine)
739   {
740     mWebEngine.RegisterHttpAuthHandlerCallback(callback);
741   }
742 }
743
744 void WebView::RegisterContextMenuShownCallback(Dali::WebEnginePlugin::WebEngineContextMenuShownCallback callback)
745 {
746   if(mWebEngine)
747   {
748     mWebEngine.RegisterContextMenuShownCallback(callback);
749   }
750 }
751
752 void WebView::RegisterContextMenuHiddenCallback(Dali::WebEnginePlugin::WebEngineContextMenuHiddenCallback callback)
753 {
754   if(mWebEngine)
755   {
756     mWebEngine.RegisterContextMenuHiddenCallback(callback);
757   }
758 }
759
760 void WebView::GetPlainTextAsynchronously(Dali::WebEnginePlugin::PlainTextReceivedCallback callback)
761 {
762   if(mWebEngine)
763   {
764     mWebEngine.GetPlainTextAsynchronously(callback);
765   }
766 }
767
768 void WebView::OnFrameRendered()
769 {
770   if(mFrameRenderedCallback)
771   {
772     mFrameRenderedCallback();
773   }
774
775   // Make sure that mVisual is created only once.
776   if (mVisual)
777     return;
778
779   Dali::Toolkit::ImageUrl nativeImageUrl = Dali::Toolkit::Image::GenerateUrl(mWebEngine.GetNativeImageSource());
780   mVisual                                = Toolkit::VisualFactory::Get().CreateVisual({{Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE}, {Toolkit::ImageVisual::Property::URL, nativeImageUrl.GetUrl()}});
781   if(mVisual)
782   {
783     DevelControl::RegisterVisual(*this, Toolkit::WebView::Property::URL, mVisual);
784     EnableBlendMode(!mVideoHoleEnabled);
785   }
786 }
787
788 void WebView::OnDisplayAreaUpdated(Dali::PropertyNotification& /*source*/)
789 {
790   if(!mWebEngine)
791     return;
792
793   Actor self(Self());
794
795   bool    positionUsesAnchorPoint = self.GetProperty<bool>(Actor::Property::POSITION_USES_ANCHOR_POINT);
796   Vector3 actorSize               = self.GetCurrentProperty<Vector3>(Actor::Property::SIZE) * self.GetCurrentProperty<Vector3>(Actor::Property::SCALE);
797   Vector3 anchorPointOffSet       = actorSize * (positionUsesAnchorPoint ? self.GetCurrentProperty<Vector3>(Actor::Property::ANCHOR_POINT) : AnchorPoint::TOP_LEFT);
798   Vector2 screenPosition          = self.GetProperty<Vector2>(Actor::Property::SCREEN_POSITION);
799
800   Dali::Rect<int32_t> displayArea;
801   displayArea.x      = screenPosition.x - anchorPointOffSet.x;
802   displayArea.y      = screenPosition.y - anchorPointOffSet.y;
803   displayArea.width  = actorSize.x;
804   displayArea.height = actorSize.y;
805
806   Size displaySize = Size(displayArea.width, displayArea.height);
807   if(mWebViewSize != displaySize)
808   {
809     mWebViewSize = displaySize;
810   }
811
812   if(mWebViewArea != displayArea)
813   {
814     mWebViewArea = displayArea;
815     mWebEngine.UpdateDisplayArea(mWebViewArea);
816   }
817 }
818
819 void WebView::OnVisibilityChanged(Actor actor, bool isVisible, Dali::DevelActor::VisibilityChange::Type type)
820 {
821   if(type == Dali::DevelActor::VisibilityChange::Type::SELF)
822   {
823     SetVisibility(isVisible);
824   }
825 }
826
827 void WebView::OnScreenshotCaptured(Dali::PixelData pixel)
828 {
829   if(mScreenshotCapturedCallback)
830   {
831     Dali::Toolkit::ImageView imageView = CreateImageView(pixel);
832     mScreenshotCapturedCallback(imageView);
833   }
834 }
835
836 void WebView::OnSceneConnection(int depth)
837 {
838   Control::OnSceneConnection(depth);
839   EnableBlendMode(!mVideoHoleEnabled);
840 }
841
842 bool WebView::OnTouchEvent(Actor actor, const Dali::TouchEvent& touch)
843 {
844   bool result = false;
845
846   if(mWebEngine)
847   {
848     result = mWebEngine.SendTouchEvent(touch);
849   }
850   return result;
851 }
852
853 bool WebView::OnKeyEvent(const Dali::KeyEvent& event)
854 {
855   bool result = false;
856
857   if(mWebEngine)
858   {
859     result = mWebEngine.SendKeyEvent(event);
860   }
861   return result;
862 }
863
864 bool WebView::OnHoverEvent(Actor actor, const Dali::HoverEvent& hover)
865 {
866   bool result = false;
867   if(mWebEngine && mMouseEventsEnabled)
868   {
869     result = mWebEngine.SendHoverEvent(hover);
870   }
871   return result;
872 }
873
874 bool WebView::OnWheelEvent(Actor actor, const Dali::WheelEvent& wheel)
875 {
876   bool result = false;
877   if(mWebEngine && mMouseEventsEnabled)
878   {
879     result = mWebEngine.SendWheelEvent(wheel);
880   }
881   return result;
882 }
883
884 void WebView::OnKeyInputFocusGained()
885 {
886   if(mWebEngine)
887   {
888     mWebEngine.SetFocus(true);
889   }
890
891   EmitKeyInputFocusSignal(true); // Calls back into the Control hence done last.
892 }
893
894 void WebView::OnKeyInputFocusLost()
895 {
896   if(mWebEngine)
897   {
898     mWebEngine.SetFocus(false);
899   }
900
901   EmitKeyInputFocusSignal(false); // Calls back into the Control hence done last.
902 }
903
904 Vector3 WebView::GetNaturalSize()
905 {
906   if(mVisual)
907   {
908     Vector2 rendererNaturalSize;
909     mVisual.GetNaturalSize(rendererNaturalSize);
910     return Vector3(rendererNaturalSize);
911   }
912
913   return Vector3(mWebViewSize);
914 }
915
916 void WebView::SetProperty(BaseObject* object, Property::Index index, const Property::Value& value)
917 {
918   Toolkit::WebView webView = Toolkit::WebView::DownCast(Dali::BaseHandle(object));
919
920   if(webView)
921   {
922     WebView& impl = GetImpl(webView);
923     switch(index)
924     {
925       case Toolkit::WebView::Property::URL:
926       {
927         std::string url;
928         if(value.Get(url))
929         {
930           impl.LoadUrl(url);
931         }
932         break;
933       }
934       case Toolkit::WebView::Property::USER_AGENT:
935       {
936         std::string input;
937         if(value.Get(input))
938         {
939           impl.SetUserAgent(input);
940         }
941         break;
942       }
943       case Toolkit::WebView::Property::SCROLL_POSITION:
944       {
945         Vector2 input;
946         if(value.Get(input))
947         {
948           impl.SetScrollPosition(input.x, input.y);
949         }
950         break;
951       }
952       case Toolkit::WebView::Property::VIDEO_HOLE_ENABLED:
953       {
954         bool input;
955         if(value.Get(input))
956         {
957           impl.EnableVideoHole(input);
958         }
959         break;
960       }
961       case Toolkit::WebView::Property::MOUSE_EVENTS_ENABLED:
962       {
963         bool input;
964         if(value.Get(input))
965         {
966           impl.EnableMouseEvents(input);
967         }
968         break;
969       }
970       case Toolkit::WebView::Property::KEY_EVENTS_ENABLED:
971       {
972         bool input;
973         if(value.Get(input))
974         {
975           impl.EnableKeyEvents(input);
976         }
977         break;
978       }
979       case Toolkit::WebView::Property::DOCUMENT_BACKGROUND_COLOR:
980       {
981         Vector4 input;
982         if(value.Get(input))
983         {
984           impl.SetDocumentBackgroundColor(input);
985         }
986         break;
987       }
988       case Toolkit::WebView::Property::TILES_CLEARED_WHEN_HIDDEN:
989       {
990         bool input;
991         if(value.Get(input))
992         {
993           impl.ClearTilesWhenHidden(input);
994         }
995         break;
996       }
997       case Toolkit::WebView::Property::TILE_COVER_AREA_MULTIPLIER:
998       {
999         float input;
1000         if(value.Get(input))
1001         {
1002           impl.SetTileCoverAreaMultiplier(input);
1003         }
1004         break;
1005       }
1006       case Toolkit::WebView::Property::CURSOR_ENABLED_BY_CLIENT:
1007       {
1008         bool input;
1009         if(value.Get(input))
1010         {
1011           impl.EnableCursorByClient(input);
1012         }
1013         break;
1014       }
1015       case Toolkit::WebView::Property::PAGE_ZOOM_FACTOR:
1016       {
1017         float input;
1018         if(value.Get(input))
1019         {
1020           impl.SetPageZoomFactor(input);
1021         }
1022         break;
1023       }
1024       case Toolkit::WebView::Property::TEXT_ZOOM_FACTOR:
1025       {
1026         float input;
1027         if(value.Get(input))
1028         {
1029           impl.SetTextZoomFactor(input);
1030         }
1031         break;
1032       }
1033       default:
1034         break;
1035     }
1036   }
1037 }
1038
1039 Property::Value WebView::GetProperty(BaseObject* object, Property::Index propertyIndex)
1040 {
1041   Property::Value value;
1042
1043   Toolkit::WebView webView = Toolkit::WebView::DownCast(Dali::BaseHandle(object));
1044
1045   if(webView)
1046   {
1047     WebView& impl = GetImpl(webView);
1048     switch(propertyIndex)
1049     {
1050       case Toolkit::WebView::Property::URL:
1051       {
1052         value = impl.GetUrl();
1053         break;
1054       }
1055       case Toolkit::WebView::Property::USER_AGENT:
1056       {
1057         value = impl.GetUserAgent();
1058         break;
1059       }
1060       case Toolkit::WebView::Property::SCROLL_POSITION:
1061       {
1062         value = impl.GetScrollPosition();
1063         break;
1064       }
1065       case Toolkit::WebView::Property::SCROLL_SIZE:
1066       {
1067         value = impl.GetScrollSize();
1068         break;
1069       }
1070       case Toolkit::WebView::Property::CONTENT_SIZE:
1071       {
1072         value = impl.GetContentSize();
1073         break;
1074       }
1075       case Toolkit::WebView::Property::TITLE:
1076       {
1077         value = impl.GetTitle();
1078         break;
1079       }
1080       case Toolkit::WebView::Property::VIDEO_HOLE_ENABLED:
1081       {
1082         value = impl.mVideoHoleEnabled;
1083         break;
1084       }
1085       case Toolkit::WebView::Property::MOUSE_EVENTS_ENABLED:
1086       {
1087         value = impl.mMouseEventsEnabled;
1088         break;
1089       }
1090       case Toolkit::WebView::Property::KEY_EVENTS_ENABLED:
1091       {
1092         value = impl.mKeyEventsEnabled;
1093         break;
1094       }
1095       case Toolkit::WebView::Property::SELECTED_TEXT:
1096       {
1097         value = impl.GetSelectedText();
1098         break;
1099       }
1100       case Toolkit::WebView::Property::PAGE_ZOOM_FACTOR:
1101       {
1102         value = impl.GetPageZoomFactor();
1103         break;
1104       }
1105       case Toolkit::WebView::Property::TEXT_ZOOM_FACTOR:
1106       {
1107         value = impl.GetTextZoomFactor();
1108         break;
1109       }
1110       case Toolkit::WebView::Property::LOAD_PROGRESS_PERCENTAGE:
1111       {
1112         value = impl.GetLoadProgressPercentage();
1113         break;
1114       }
1115       default:
1116         break;
1117     }
1118   }
1119
1120   return value;
1121 }
1122
1123 void WebView::SetScrollPosition(int32_t x, int32_t y)
1124 {
1125   if(mWebEngine)
1126   {
1127     mWebEngine.SetScrollPosition(x, y);
1128   }
1129 }
1130
1131 Dali::Vector2 WebView::GetScrollPosition() const
1132 {
1133   return mWebEngine ? mWebEngine.GetScrollPosition() : Dali::Vector2::ZERO;
1134 }
1135
1136 Dali::Vector2 WebView::GetScrollSize() const
1137 {
1138   return mWebEngine ? mWebEngine.GetScrollSize() : Dali::Vector2::ZERO;
1139 }
1140
1141 Dali::Vector2 WebView::GetContentSize() const
1142 {
1143   return mWebEngine ? mWebEngine.GetContentSize() : Dali::Vector2::ZERO;
1144 }
1145
1146 std::string WebView::GetTitle() const
1147 {
1148   return mWebEngine ? mWebEngine.GetTitle() : std::string();
1149 }
1150
1151 void WebView::SetDocumentBackgroundColor(Dali::Vector4 color)
1152 {
1153   if(mWebEngine)
1154   {
1155     mWebEngine.SetDocumentBackgroundColor(color);
1156   }
1157 }
1158
1159 void WebView::ClearTilesWhenHidden(bool cleared)
1160 {
1161   if(mWebEngine)
1162   {
1163     mWebEngine.ClearTilesWhenHidden(cleared);
1164   }
1165 }
1166
1167 void WebView::SetTileCoverAreaMultiplier(float multiplier)
1168 {
1169   if(mWebEngine)
1170   {
1171     mWebEngine.SetTileCoverAreaMultiplier(multiplier);
1172   }
1173 }
1174
1175 void WebView::EnableCursorByClient(bool enabled)
1176 {
1177   if(mWebEngine)
1178   {
1179     mWebEngine.EnableCursorByClient(enabled);
1180   }
1181 }
1182
1183 std::string WebView::GetSelectedText() const
1184 {
1185   return mWebEngine ? mWebEngine.GetSelectedText() : std::string();
1186 }
1187
1188 std::string WebView::GetUrl() const
1189 {
1190   return mWebEngine ? mWebEngine.GetUrl() : std::string();
1191 }
1192
1193 std::string WebView::GetUserAgent() const
1194 {
1195   return mWebEngine ? mWebEngine.GetUserAgent() : std::string();
1196 }
1197
1198 void WebView::SetUserAgent(const std::string& userAgent)
1199 {
1200   if(mWebEngine)
1201   {
1202     mWebEngine.SetUserAgent(userAgent);
1203   }
1204 }
1205
1206 void WebView::EnableMouseEvents(bool enabled)
1207 {
1208   if(mWebEngine)
1209   {
1210     mMouseEventsEnabled = enabled;
1211     mWebEngine.EnableMouseEvents(enabled);
1212   }
1213 }
1214
1215 void WebView::EnableKeyEvents(bool enabled)
1216 {
1217   if(mWebEngine)
1218   {
1219     mKeyEventsEnabled = enabled;
1220     mWebEngine.EnableKeyEvents(enabled);
1221   }
1222 }
1223
1224 void WebView::SetPageZoomFactor(float zoomFactor)
1225 {
1226   if(mWebEngine)
1227   {
1228     mWebEngine.SetPageZoomFactor(zoomFactor);
1229   }
1230 }
1231
1232 float WebView::GetPageZoomFactor() const
1233 {
1234   return mWebEngine ? mWebEngine.GetPageZoomFactor() : 0.0f;
1235 }
1236
1237 void WebView::SetTextZoomFactor(float zoomFactor)
1238 {
1239   if(mWebEngine)
1240   {
1241     mWebEngine.SetTextZoomFactor(zoomFactor);
1242   }
1243 }
1244
1245 float WebView::GetTextZoomFactor() const
1246 {
1247   return mWebEngine ? mWebEngine.GetTextZoomFactor() : 0.0f;
1248 }
1249
1250 float WebView::GetLoadProgressPercentage() const
1251 {
1252   return mWebEngine ? mWebEngine.GetLoadProgressPercentage() : 0.0f;
1253 }
1254
1255 bool WebView::SetVisibility(bool visible)
1256 {
1257   return mWebEngine ? mWebEngine.SetVisibility(visible) : false;
1258 }
1259
1260 WebView::WebViewAccessible::WebViewAccessible(Dali::Actor self, Dali::WebEngine& webEngine)
1261 : ControlAccessible(self), mRemoteChild{}, mWebEngine{webEngine}
1262 {
1263   mRemoteChild.SetParent(this);
1264
1265   Dali::Accessibility::Bridge::EnabledSignal().Connect(this, &WebViewAccessible::OnAccessibilityEnabled);
1266   Dali::Accessibility::Bridge::DisabledSignal().Connect(this, &WebViewAccessible::OnAccessibilityDisabled);
1267
1268   if(Dali::Accessibility::IsUp())
1269   {
1270     OnAccessibilityEnabled();
1271   }
1272   else
1273   {
1274     OnAccessibilityDisabled();
1275   }
1276 }
1277
1278 Dali::Accessibility::Attributes WebView::WebViewAccessible::GetAttributes() const
1279 {
1280   auto attributes = DevelControl::ControlAccessible::GetAttributes();
1281
1282   if(mRemoteChild.GetAddress())
1283   {
1284     attributes.insert_or_assign("child_bus", mRemoteChild.GetAddress().GetBus());
1285   }
1286
1287   return attributes;
1288 }
1289
1290 void WebView::WebViewAccessible::DoGetChildren(std::vector<Dali::Accessibility::Accessible*>& children)
1291 {
1292   if(mRemoteChild.GetAddress())
1293   {
1294     // DoGetChildren is called at most once per every OnChildrenChanged.
1295     // We have only one OnChildrenChanged in this case, so EmbedAtkSocket will be called only once.
1296     Accessibility::Bridge::GetCurrentBridge()->EmbedAtkSocket(GetAddress(), mRemoteChild.GetAddress());
1297     children.push_back(&mRemoteChild);
1298   }
1299 }
1300
1301 void WebView::WebViewAccessible::OnAccessibilityEnabled()
1302 {
1303   if(!mWebEngine)
1304   {
1305     return;
1306   }
1307
1308   mWebEngine.ActivateAccessibility(true);
1309   SetRemoteChildAddress(mWebEngine.GetAccessibilityAddress());
1310 }
1311
1312 void WebView::WebViewAccessible::OnAccessibilityDisabled()
1313 {
1314   if(!mWebEngine)
1315   {
1316     return;
1317   }
1318
1319   SetRemoteChildAddress({});
1320   mWebEngine.ActivateAccessibility(false);
1321 }
1322
1323 void WebView::WebViewAccessible::SetRemoteChildAddress(Dali::Accessibility::Address address)
1324 {
1325   mRemoteChild.SetAddress(address);
1326   OnChildrenChanged();
1327 }
1328
1329 } // namespace Internal
1330
1331 } // namespace Toolkit
1332
1333 } // namespace Dali