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