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