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