[Tizen] Add WebView APIs for fullscreen, text found, etc.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / web-view / web-view-impl.cpp
1 /*
2  * Copyright (c) 2023 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 <dali/devel-api/adaptor-framework/web-engine/web-engine-back-forward-list.h>
23 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-certificate.h>
24 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-console-message.h>
25 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-context-menu-item.h>
26 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-context-menu.h>
27 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-context.h>
28 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-cookie-manager.h>
29 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-form-repost-decision.h>
30 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-hit-test.h>
31 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-http-auth-handler.h>
32 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-load-error.h>
33 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-policy-decision.h>
34 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-settings.h>
35 #include <dali/devel-api/common/stage.h>
36 #include <dali/devel-api/scripting/enum-helper.h>
37 #include <dali/devel-api/scripting/scripting.h>
38 #include <dali/public-api/adaptor-framework/native-image-source.h>
39 #include <dali/public-api/object/type-registry-helper.h>
40 #include <dali/public-api/object/type-registry.h>
41 #include <cstring>
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 int8_t* 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::ExitFullscreen()
573 {
574   if(mWebEngine)
575   {
576     mWebEngine.ExitFullscreen();
577   }
578 }
579
580 void WebView::RegisterGeolocationPermissionCallback(Dali::WebEnginePlugin::GeolocationPermissionCallback callback)
581 {
582   if(mWebEngine)
583   {
584     mWebEngine.RegisterGeolocationPermissionCallback(callback);
585   }
586 }
587
588 void WebView::SetTtsFocus(bool focused)
589 {
590   if(mWebEngine && !HasKeyInputFocus())
591   {
592     mWebEngine.SetFocus(focused);
593   }
594 }
595
596 void WebView::EnableVideoHole(bool enabled)
597 {
598   mVideoHoleEnabled = enabled;
599
600   EnableBlendMode(!mVideoHoleEnabled);
601
602   if(mWebEngine)
603   {
604     mWebEngine.EnableVideoHole(mVideoHoleEnabled);
605   }
606 }
607
608 void WebView::EnableBlendMode(bool blendEnabled)
609 {
610   Actor self = Self();
611   for(uint32_t i = 0; i < self.GetRendererCount(); i++)
612   {
613     Dali::Renderer render = self.GetRendererAt(i);
614     render.SetProperty(Renderer::Property::BLEND_MODE, blendEnabled ? BlendMode::ON : BlendMode::OFF);
615   }
616 }
617
618 Dali::Toolkit::ImageView WebView::CreateImageView(Dali::PixelData pixel) const
619 {
620   if(!pixel)
621   {
622     return Dali::Toolkit::ImageView();
623   }
624
625   Dali::Toolkit::ImageUrl  url       = Dali::Toolkit::Image::GenerateUrl(pixel);
626   Dali::Toolkit::ImageView imageView = Dali::Toolkit::ImageView::New(url.GetUrl());
627   imageView.SetProperty(Dali::Actor::Property::SIZE, Vector2(pixel.GetWidth(), pixel.GetHeight()));
628   return imageView;
629 }
630
631 void WebView::RegisterPageLoadStartedCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback)
632 {
633   if(mWebEngine)
634   {
635     mWebEngine.RegisterPageLoadStartedCallback(callback);
636   }
637 }
638
639 void WebView::RegisterPageLoadInProgressCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback)
640 {
641   if(mWebEngine)
642   {
643     mWebEngine.RegisterPageLoadInProgressCallback(callback);
644   }
645 }
646
647 void WebView::RegisterPageLoadFinishedCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback)
648 {
649   if(mWebEngine)
650   {
651     mWebEngine.RegisterPageLoadFinishedCallback(callback);
652   }
653 }
654
655 void WebView::RegisterPageLoadErrorCallback(Dali::WebEnginePlugin::WebEnginePageLoadErrorCallback callback)
656 {
657   if(mWebEngine)
658   {
659     mWebEngine.RegisterPageLoadErrorCallback(callback);
660   }
661 }
662
663 void WebView::RegisterScrollEdgeReachedCallback(Dali::WebEnginePlugin::WebEngineScrollEdgeReachedCallback callback)
664 {
665   if(mWebEngine)
666   {
667     mWebEngine.RegisterScrollEdgeReachedCallback(callback);
668   }
669 }
670
671 void WebView::RegisterUrlChangedCallback(Dali::WebEnginePlugin::WebEngineUrlChangedCallback callback)
672 {
673   if(mWebEngine)
674   {
675     mWebEngine.RegisterUrlChangedCallback(callback);
676   }
677 }
678
679 void WebView::RegisterFormRepostDecidedCallback(Dali::WebEnginePlugin::WebEngineFormRepostDecidedCallback callback)
680 {
681   if(mWebEngine)
682   {
683     mWebEngine.RegisterFormRepostDecidedCallback(callback);
684   }
685 }
686
687 void WebView::RegisterFrameRenderedCallback(Dali::WebEnginePlugin::WebEngineFrameRenderedCallback callback)
688 {
689   mFrameRenderedCallback = callback;
690 }
691
692 void WebView::RegisterConsoleMessageReceivedCallback(Dali::WebEnginePlugin::WebEngineConsoleMessageReceivedCallback callback)
693 {
694   if(mWebEngine)
695   {
696     mWebEngine.RegisterConsoleMessageReceivedCallback(callback);
697   }
698 }
699
700 void WebView::RegisterResponsePolicyDecidedCallback(Dali::WebEnginePlugin::WebEngineResponsePolicyDecidedCallback callback)
701 {
702   if(mWebEngine)
703   {
704     mWebEngine.RegisterResponsePolicyDecidedCallback(callback);
705   }
706 }
707
708 void WebView::RegisterNavigationPolicyDecidedCallback(Dali::WebEnginePlugin::WebEngineNavigationPolicyDecidedCallback callback)
709 {
710   if(mWebEngine)
711   {
712     mWebEngine.RegisterNavigationPolicyDecidedCallback(callback);
713   }
714 }
715
716 void WebView::RegisterNewWindowPolicyDecidedCallback(Dali::WebEnginePlugin::WebEngineNewWindowPolicyDecidedCallback callback)
717 {
718   if(mWebEngine)
719   {
720     mWebEngine.RegisterNewWindowPolicyDecidedCallback(callback);
721   }
722 }
723
724 void WebView::RegisterNewWindowCreatedCallback(Dali::WebEnginePlugin::WebEngineNewWindowCreatedCallback callback)
725 {
726   if(mWebEngine)
727   {
728     mWebEngine.RegisterNewWindowCreatedCallback(callback);
729   }
730 }
731
732 void WebView::RegisterCertificateConfirmedCallback(Dali::WebEnginePlugin::WebEngineCertificateCallback callback)
733 {
734   if(mWebEngine)
735   {
736     mWebEngine.RegisterCertificateConfirmedCallback(callback);
737   }
738 }
739
740 void WebView::RegisterSslCertificateChangedCallback(Dali::WebEnginePlugin::WebEngineCertificateCallback callback)
741 {
742   if(mWebEngine)
743   {
744     mWebEngine.RegisterSslCertificateChangedCallback(callback);
745   }
746 }
747
748 void WebView::RegisterHttpAuthHandlerCallback(Dali::WebEnginePlugin::WebEngineHttpAuthHandlerCallback callback)
749 {
750   if(mWebEngine)
751   {
752     mWebEngine.RegisterHttpAuthHandlerCallback(callback);
753   }
754 }
755
756 void WebView::RegisterContextMenuShownCallback(Dali::WebEnginePlugin::WebEngineContextMenuShownCallback callback)
757 {
758   if(mWebEngine)
759   {
760     mWebEngine.RegisterContextMenuShownCallback(callback);
761   }
762 }
763
764 void WebView::RegisterContextMenuHiddenCallback(Dali::WebEnginePlugin::WebEngineContextMenuHiddenCallback callback)
765 {
766   if(mWebEngine)
767   {
768     mWebEngine.RegisterContextMenuHiddenCallback(callback);
769   }
770 }
771
772 void WebView::RegisterFullscreenEnteredCallback(Dali::WebEnginePlugin::WebEngineFullscreenEnteredCallback callback)
773 {
774   if(mWebEngine)
775   {
776     mWebEngine.RegisterFullscreenEnteredCallback(callback);
777   }
778 }
779
780 void WebView::RegisterFullscreenExitedCallback(Dali::WebEnginePlugin::WebEngineFullscreenExitedCallback callback)
781 {
782   if(mWebEngine)
783   {
784     mWebEngine.RegisterFullscreenExitedCallback(callback);
785   }
786 }
787
788 void WebView::RegisterTextFoundCallback(Dali::WebEnginePlugin::WebEngineTextFoundCallback callback)
789 {
790   if(mWebEngine)
791   {
792     mWebEngine.RegisterTextFoundCallback(callback);
793   }
794 }
795
796 void WebView::GetPlainTextAsynchronously(Dali::WebEnginePlugin::PlainTextReceivedCallback callback)
797 {
798   if(mWebEngine)
799   {
800     mWebEngine.GetPlainTextAsynchronously(callback);
801   }
802 }
803
804 void WebView::OnFrameRendered()
805 {
806   if(mFrameRenderedCallback)
807   {
808     mFrameRenderedCallback();
809   }
810
811   // Make sure that mVisual is created only once.
812   if (mVisual)
813     return;
814
815   Dali::Toolkit::ImageUrl nativeImageUrl = Dali::Toolkit::Image::GenerateUrl(mWebEngine.GetNativeImageSource());
816   mVisual                                = Toolkit::VisualFactory::Get().CreateVisual({{Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE}, {Toolkit::ImageVisual::Property::URL, nativeImageUrl.GetUrl()}});
817   if(mVisual)
818   {
819     DevelControl::RegisterVisual(*this, Toolkit::WebView::Property::URL, mVisual);
820     EnableBlendMode(!mVideoHoleEnabled);
821   }
822 }
823
824 void WebView::OnDisplayAreaUpdated(Dali::PropertyNotification& /*source*/)
825 {
826   if(!mWebEngine)
827     return;
828
829   Actor self(Self());
830
831   bool    positionUsesAnchorPoint = self.GetProperty<bool>(Actor::Property::POSITION_USES_ANCHOR_POINT);
832   Vector3 actorSize               = self.GetCurrentProperty<Vector3>(Actor::Property::SIZE) * self.GetCurrentProperty<Vector3>(Actor::Property::SCALE);
833   Vector3 anchorPointOffSet       = actorSize * (positionUsesAnchorPoint ? self.GetCurrentProperty<Vector3>(Actor::Property::ANCHOR_POINT) : AnchorPoint::TOP_LEFT);
834   Vector2 screenPosition          = self.GetProperty<Vector2>(Actor::Property::SCREEN_POSITION);
835
836   Dali::Rect<int32_t> displayArea;
837   displayArea.x      = screenPosition.x - anchorPointOffSet.x;
838   displayArea.y      = screenPosition.y - anchorPointOffSet.y;
839   displayArea.width  = actorSize.x;
840   displayArea.height = actorSize.y;
841
842   Size displaySize = Size(displayArea.width, displayArea.height);
843   if(mWebViewSize != displaySize)
844   {
845     mWebViewSize = displaySize;
846   }
847
848   if(mWebViewArea != displayArea)
849   {
850     mWebViewArea = displayArea;
851     mWebEngine.UpdateDisplayArea(mWebViewArea);
852   }
853 }
854
855 void WebView::OnVisibilityChanged(Actor actor, bool isVisible, Dali::DevelActor::VisibilityChange::Type type)
856 {
857   if(type == Dali::DevelActor::VisibilityChange::Type::SELF)
858   {
859     SetVisibility(isVisible);
860   }
861 }
862
863 void WebView::OnScreenshotCaptured(Dali::PixelData pixel)
864 {
865   if(mScreenshotCapturedCallback)
866   {
867     Dali::Toolkit::ImageView imageView = CreateImageView(pixel);
868     mScreenshotCapturedCallback(imageView);
869   }
870 }
871
872 void WebView::OnSceneConnection(int depth)
873 {
874   Control::OnSceneConnection(depth);
875   EnableBlendMode(!mVideoHoleEnabled);
876 }
877
878 bool WebView::OnTouchEvent(Actor actor, const Dali::TouchEvent& touch)
879 {
880   bool result = false;
881
882   if(mWebEngine)
883   {
884     result = mWebEngine.SendTouchEvent(touch);
885   }
886   return result;
887 }
888
889 bool WebView::OnKeyEvent(const Dali::KeyEvent& event)
890 {
891   bool result = false;
892
893   if(mWebEngine)
894   {
895     result = mWebEngine.SendKeyEvent(event);
896   }
897   return result;
898 }
899
900 bool WebView::OnHoverEvent(Actor actor, const Dali::HoverEvent& hover)
901 {
902   bool result = false;
903   if(mWebEngine && mMouseEventsEnabled)
904   {
905     result = mWebEngine.SendHoverEvent(hover);
906   }
907   return result;
908 }
909
910 bool WebView::OnWheelEvent(Actor actor, const Dali::WheelEvent& wheel)
911 {
912   bool result = false;
913   if(mWebEngine && mMouseEventsEnabled)
914   {
915     result = mWebEngine.SendWheelEvent(wheel);
916   }
917   return result;
918 }
919
920 void WebView::OnKeyInputFocusGained()
921 {
922   if(mWebEngine)
923   {
924     mWebEngine.SetFocus(true);
925   }
926
927   EmitKeyInputFocusSignal(true); // Calls back into the Control hence done last.
928 }
929
930 void WebView::OnKeyInputFocusLost()
931 {
932   if(mWebEngine)
933   {
934     mWebEngine.SetFocus(false);
935   }
936
937   EmitKeyInputFocusSignal(false); // Calls back into the Control hence done last.
938 }
939
940 Vector3 WebView::GetNaturalSize()
941 {
942   if(mVisual)
943   {
944     Vector2 rendererNaturalSize;
945     mVisual.GetNaturalSize(rendererNaturalSize);
946     return Vector3(rendererNaturalSize);
947   }
948
949   return Vector3(mWebViewSize);
950 }
951
952 void WebView::SetProperty(BaseObject* object, Property::Index index, const Property::Value& value)
953 {
954   Toolkit::WebView webView = Toolkit::WebView::DownCast(Dali::BaseHandle(object));
955
956   if(webView)
957   {
958     WebView& impl = GetImpl(webView);
959     switch(index)
960     {
961       case Toolkit::WebView::Property::URL:
962       {
963         std::string url;
964         if(value.Get(url))
965         {
966           impl.LoadUrl(url);
967         }
968         break;
969       }
970       case Toolkit::WebView::Property::USER_AGENT:
971       {
972         std::string input;
973         if(value.Get(input))
974         {
975           impl.SetUserAgent(input);
976         }
977         break;
978       }
979       case Toolkit::WebView::Property::SCROLL_POSITION:
980       {
981         Vector2 input;
982         if(value.Get(input))
983         {
984           impl.SetScrollPosition(input.x, input.y);
985         }
986         break;
987       }
988       case Toolkit::WebView::Property::VIDEO_HOLE_ENABLED:
989       {
990         bool input;
991         if(value.Get(input))
992         {
993           impl.EnableVideoHole(input);
994         }
995         break;
996       }
997       case Toolkit::WebView::Property::MOUSE_EVENTS_ENABLED:
998       {
999         bool input;
1000         if(value.Get(input))
1001         {
1002           impl.EnableMouseEvents(input);
1003         }
1004         break;
1005       }
1006       case Toolkit::WebView::Property::KEY_EVENTS_ENABLED:
1007       {
1008         bool input;
1009         if(value.Get(input))
1010         {
1011           impl.EnableKeyEvents(input);
1012         }
1013         break;
1014       }
1015       case Toolkit::WebView::Property::DOCUMENT_BACKGROUND_COLOR:
1016       {
1017         Vector4 input;
1018         if(value.Get(input))
1019         {
1020           impl.SetDocumentBackgroundColor(input);
1021         }
1022         break;
1023       }
1024       case Toolkit::WebView::Property::TILES_CLEARED_WHEN_HIDDEN:
1025       {
1026         bool input;
1027         if(value.Get(input))
1028         {
1029           impl.ClearTilesWhenHidden(input);
1030         }
1031         break;
1032       }
1033       case Toolkit::WebView::Property::TILE_COVER_AREA_MULTIPLIER:
1034       {
1035         float input;
1036         if(value.Get(input))
1037         {
1038           impl.SetTileCoverAreaMultiplier(input);
1039         }
1040         break;
1041       }
1042       case Toolkit::WebView::Property::CURSOR_ENABLED_BY_CLIENT:
1043       {
1044         bool input;
1045         if(value.Get(input))
1046         {
1047           impl.EnableCursorByClient(input);
1048         }
1049         break;
1050       }
1051       case Toolkit::WebView::Property::PAGE_ZOOM_FACTOR:
1052       {
1053         float input;
1054         if(value.Get(input))
1055         {
1056           impl.SetPageZoomFactor(input);
1057         }
1058         break;
1059       }
1060       case Toolkit::WebView::Property::TEXT_ZOOM_FACTOR:
1061       {
1062         float input;
1063         if(value.Get(input))
1064         {
1065           impl.SetTextZoomFactor(input);
1066         }
1067         break;
1068       }
1069       default:
1070         break;
1071     }
1072   }
1073 }
1074
1075 Property::Value WebView::GetProperty(BaseObject* object, Property::Index propertyIndex)
1076 {
1077   Property::Value value;
1078
1079   Toolkit::WebView webView = Toolkit::WebView::DownCast(Dali::BaseHandle(object));
1080
1081   if(webView)
1082   {
1083     WebView& impl = GetImpl(webView);
1084     switch(propertyIndex)
1085     {
1086       case Toolkit::WebView::Property::URL:
1087       {
1088         value = impl.GetUrl();
1089         break;
1090       }
1091       case Toolkit::WebView::Property::USER_AGENT:
1092       {
1093         value = impl.GetUserAgent();
1094         break;
1095       }
1096       case Toolkit::WebView::Property::SCROLL_POSITION:
1097       {
1098         value = impl.GetScrollPosition();
1099         break;
1100       }
1101       case Toolkit::WebView::Property::SCROLL_SIZE:
1102       {
1103         value = impl.GetScrollSize();
1104         break;
1105       }
1106       case Toolkit::WebView::Property::CONTENT_SIZE:
1107       {
1108         value = impl.GetContentSize();
1109         break;
1110       }
1111       case Toolkit::WebView::Property::TITLE:
1112       {
1113         value = impl.GetTitle();
1114         break;
1115       }
1116       case Toolkit::WebView::Property::VIDEO_HOLE_ENABLED:
1117       {
1118         value = impl.mVideoHoleEnabled;
1119         break;
1120       }
1121       case Toolkit::WebView::Property::MOUSE_EVENTS_ENABLED:
1122       {
1123         value = impl.mMouseEventsEnabled;
1124         break;
1125       }
1126       case Toolkit::WebView::Property::KEY_EVENTS_ENABLED:
1127       {
1128         value = impl.mKeyEventsEnabled;
1129         break;
1130       }
1131       case Toolkit::WebView::Property::SELECTED_TEXT:
1132       {
1133         value = impl.GetSelectedText();
1134         break;
1135       }
1136       case Toolkit::WebView::Property::PAGE_ZOOM_FACTOR:
1137       {
1138         value = impl.GetPageZoomFactor();
1139         break;
1140       }
1141       case Toolkit::WebView::Property::TEXT_ZOOM_FACTOR:
1142       {
1143         value = impl.GetTextZoomFactor();
1144         break;
1145       }
1146       case Toolkit::WebView::Property::LOAD_PROGRESS_PERCENTAGE:
1147       {
1148         value = impl.GetLoadProgressPercentage();
1149         break;
1150       }
1151       default:
1152         break;
1153     }
1154   }
1155
1156   return value;
1157 }
1158
1159 void WebView::SetScrollPosition(int32_t x, int32_t y)
1160 {
1161   if(mWebEngine)
1162   {
1163     mWebEngine.SetScrollPosition(x, y);
1164   }
1165 }
1166
1167 Dali::Vector2 WebView::GetScrollPosition() const
1168 {
1169   return mWebEngine ? mWebEngine.GetScrollPosition() : Dali::Vector2::ZERO;
1170 }
1171
1172 Dali::Vector2 WebView::GetScrollSize() const
1173 {
1174   return mWebEngine ? mWebEngine.GetScrollSize() : Dali::Vector2::ZERO;
1175 }
1176
1177 Dali::Vector2 WebView::GetContentSize() const
1178 {
1179   return mWebEngine ? mWebEngine.GetContentSize() : Dali::Vector2::ZERO;
1180 }
1181
1182 std::string WebView::GetTitle() const
1183 {
1184   return mWebEngine ? mWebEngine.GetTitle() : std::string();
1185 }
1186
1187 void WebView::SetDocumentBackgroundColor(Dali::Vector4 color)
1188 {
1189   if(mWebEngine)
1190   {
1191     mWebEngine.SetDocumentBackgroundColor(color);
1192   }
1193 }
1194
1195 void WebView::ClearTilesWhenHidden(bool cleared)
1196 {
1197   if(mWebEngine)
1198   {
1199     mWebEngine.ClearTilesWhenHidden(cleared);
1200   }
1201 }
1202
1203 void WebView::SetTileCoverAreaMultiplier(float multiplier)
1204 {
1205   if(mWebEngine)
1206   {
1207     mWebEngine.SetTileCoverAreaMultiplier(multiplier);
1208   }
1209 }
1210
1211 void WebView::EnableCursorByClient(bool enabled)
1212 {
1213   if(mWebEngine)
1214   {
1215     mWebEngine.EnableCursorByClient(enabled);
1216   }
1217 }
1218
1219 std::string WebView::GetSelectedText() const
1220 {
1221   return mWebEngine ? mWebEngine.GetSelectedText() : std::string();
1222 }
1223
1224 std::string WebView::GetUrl() const
1225 {
1226   return mWebEngine ? mWebEngine.GetUrl() : std::string();
1227 }
1228
1229 std::string WebView::GetUserAgent() const
1230 {
1231   return mWebEngine ? mWebEngine.GetUserAgent() : std::string();
1232 }
1233
1234 void WebView::SetUserAgent(const std::string& userAgent)
1235 {
1236   if(mWebEngine)
1237   {
1238     mWebEngine.SetUserAgent(userAgent);
1239   }
1240 }
1241
1242 void WebView::EnableMouseEvents(bool enabled)
1243 {
1244   if(mWebEngine)
1245   {
1246     mMouseEventsEnabled = enabled;
1247     mWebEngine.EnableMouseEvents(enabled);
1248   }
1249 }
1250
1251 void WebView::EnableKeyEvents(bool enabled)
1252 {
1253   if(mWebEngine)
1254   {
1255     mKeyEventsEnabled = enabled;
1256     mWebEngine.EnableKeyEvents(enabled);
1257   }
1258 }
1259
1260 void WebView::SetPageZoomFactor(float zoomFactor)
1261 {
1262   if(mWebEngine)
1263   {
1264     mWebEngine.SetPageZoomFactor(zoomFactor);
1265   }
1266 }
1267
1268 float WebView::GetPageZoomFactor() const
1269 {
1270   return mWebEngine ? mWebEngine.GetPageZoomFactor() : 0.0f;
1271 }
1272
1273 void WebView::SetTextZoomFactor(float zoomFactor)
1274 {
1275   if(mWebEngine)
1276   {
1277     mWebEngine.SetTextZoomFactor(zoomFactor);
1278   }
1279 }
1280
1281 float WebView::GetTextZoomFactor() const
1282 {
1283   return mWebEngine ? mWebEngine.GetTextZoomFactor() : 0.0f;
1284 }
1285
1286 float WebView::GetLoadProgressPercentage() const
1287 {
1288   return mWebEngine ? mWebEngine.GetLoadProgressPercentage() : 0.0f;
1289 }
1290
1291 bool WebView::SetVisibility(bool visible)
1292 {
1293   return mWebEngine ? mWebEngine.SetVisibility(visible) : false;
1294 }
1295
1296 WebView::WebViewAccessible::WebViewAccessible(Dali::Actor self, Dali::WebEngine& webEngine)
1297 : ControlAccessible(self), mRemoteChild{}, mWebEngine{webEngine}
1298 {
1299   mRemoteChild.SetParent(this);
1300
1301   Dali::Accessibility::Bridge::EnabledSignal().Connect(this, &WebViewAccessible::OnAccessibilityEnabled);
1302   Dali::Accessibility::Bridge::DisabledSignal().Connect(this, &WebViewAccessible::OnAccessibilityDisabled);
1303
1304   if(Dali::Accessibility::IsUp())
1305   {
1306     OnAccessibilityEnabled();
1307   }
1308   else
1309   {
1310     OnAccessibilityDisabled();
1311   }
1312 }
1313
1314 Dali::Accessibility::Attributes WebView::WebViewAccessible::GetAttributes() const
1315 {
1316   auto attributes = DevelControl::ControlAccessible::GetAttributes();
1317
1318   if(mRemoteChild.GetAddress())
1319   {
1320     attributes.insert_or_assign("child_bus", mRemoteChild.GetAddress().GetBus());
1321   }
1322
1323   return attributes;
1324 }
1325
1326 void WebView::WebViewAccessible::DoGetChildren(std::vector<Dali::Accessibility::Accessible*>& children)
1327 {
1328   if(mRemoteChild.GetAddress())
1329   {
1330     // DoGetChildren is called at most once per every OnChildrenChanged.
1331     // We have only one OnChildrenChanged in this case, so EmbedAtkSocket will be called only once.
1332     Accessibility::Bridge::GetCurrentBridge()->EmbedAtkSocket(GetAddress(), mRemoteChild.GetAddress());
1333     children.push_back(&mRemoteChild);
1334   }
1335 }
1336
1337 void WebView::WebViewAccessible::OnAccessibilityEnabled()
1338 {
1339   if(!mWebEngine)
1340   {
1341     return;
1342   }
1343
1344   mWebEngine.ActivateAccessibility(true);
1345   SetRemoteChildAddress(mWebEngine.GetAccessibilityAddress());
1346 }
1347
1348 void WebView::WebViewAccessible::OnAccessibilityDisabled()
1349 {
1350   if(!mWebEngine)
1351   {
1352     return;
1353   }
1354
1355   SetRemoteChildAddress({});
1356   mWebEngine.ActivateAccessibility(false);
1357 }
1358
1359 void WebView::WebViewAccessible::SetRemoteChildAddress(Dali::Accessibility::Address address)
1360 {
1361   mRemoteChild.SetAddress(address);
1362   OnChildrenChanged();
1363 }
1364
1365 } // namespace Internal
1366
1367 } // namespace Toolkit
1368
1369 } // namespace Dali