2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "web-view-impl.h"
23 #include <dali/devel-api/adaptor-framework/web-engine-back-forward-list.h>
24 #include <dali/devel-api/adaptor-framework/web-engine-context.h>
25 #include <dali/devel-api/adaptor-framework/web-engine-cookie-manager.h>
26 #include <dali/devel-api/adaptor-framework/web-engine-policy-decision.h>
27 #include <dali/devel-api/adaptor-framework/web-engine-settings.h>
28 #include <dali/devel-api/scripting/enum-helper.h>
29 #include <dali/devel-api/scripting/scripting.h>
30 #include <dali/devel-api/common/stage.h>
31 #include <dali/public-api/adaptor-framework/native-image-source.h>
32 #include <dali/public-api/object/type-registry.h>
33 #include <dali/public-api/object/type-registry-helper.h>
36 #include <dali-toolkit/devel-api/controls/control-devel.h>
37 #include <dali-toolkit/devel-api/controls/web-view/web-back-forward-list.h>
38 #include <dali-toolkit/devel-api/controls/web-view/web-context.h>
39 #include <dali-toolkit/devel-api/controls/web-view/web-cookie-manager.h>
40 #include <dali-toolkit/devel-api/controls/web-view/web-settings.h>
41 #include <dali-toolkit/devel-api/image-loader/texture-manager.h>
42 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
43 #include <dali-toolkit/public-api/image-loader/image.h>
44 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
62 return Toolkit::WebView::New();
65 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::WebView, Toolkit::Control, Create )
67 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "url", STRING, URL )
68 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "userAgent", STRING, USER_AGENT )
69 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "scrollPosition", VECTOR2, SCROLL_POSITION )
70 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "scrollSize", VECTOR2, SCROLL_SIZE )
71 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "contentSize", VECTOR2, CONTENT_SIZE )
72 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "title", STRING, TITLE )
73 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "videoHoleEnabled", BOOLEAN, VIDEO_HOLE_ENABLED )
75 DALI_TYPE_REGISTRATION_END()
77 const std::string kEmptyString;
79 const char* DEFAULT_SAMPLER_TYPENAME = "sampler2D";
81 const char* FRAGMENT_SHADER_TEXTURE = DALI_COMPOSE_SHADER(
82 varying mediump vec2 vTexCoord;\n
83 uniform sampler2D sTexture;\n
84 uniform lowp vec4 uColor;\n
85 uniform lowp vec3 mixColor;\n
86 uniform lowp float preMultipliedAlpha;\n
90 gl_FragColor = texture2D(sTexture, vTexCoord) * uColor * vec4(mixColor, 1.0);\n
94 Dali::Toolkit::Visual::Base CreateNativeImageVisual(NativeImageInterfacePtr nativeImageInterface)
96 std::string fragmentShader;
97 const char* fragmentPrefix = nativeImageInterface->GetCustomFragmentPrefix();
100 fragmentShader = fragmentPrefix;
101 fragmentShader += FRAGMENT_SHADER_TEXTURE;
105 fragmentShader = FRAGMENT_SHADER_TEXTURE;
108 const char* customSamplerTypename = nativeImageInterface->GetCustomSamplerTypename();
109 if(customSamplerTypename)
111 fragmentShader.replace(fragmentShader.find(DEFAULT_SAMPLER_TYPENAME), strlen(DEFAULT_SAMPLER_TYPENAME), customSamplerTypename);
114 Texture texture = Dali::Texture::New(*nativeImageInterface);
115 const std::string nativeImageUrl = Dali::Toolkit::TextureManager::AddTexture(texture);
117 return Toolkit::VisualFactory::Get().CreateVisual(
118 { { Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE } ,
119 { Toolkit::Visual::Property::SHADER, { { Toolkit::Visual::Shader::Property::FRAGMENT_SHADER, fragmentShader } } },
120 { Toolkit::ImageVisual::Property::URL, nativeImageUrl } } );
123 } // anonymous namepsace
125 WebView::WebView( const std::string& locale, const std::string& timezoneId )
126 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
128 mWebViewSize( Stage::GetCurrent().GetSize() ),
130 mVideoHoleEnabled( true ),
131 mWebViewArea ( 0, 0, mWebViewSize.width, mWebViewSize.height )
133 mWebEngine = Dali::WebEngine::New();
135 // WebEngine is empty when it is not properly initialized.
138 mWebEngine.Create( mWebViewSize.width, mWebViewSize.height, locale, timezoneId );
142 WebView::WebView( int argc, char** argv )
143 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
145 mWebViewSize( Stage::GetCurrent().GetSize() ),
147 mVideoHoleEnabled( true ),
148 mWebViewArea ( 0, 0, mWebViewSize.width, mWebViewSize.height )
150 mWebEngine = Dali::WebEngine::New();
152 // WebEngine is empty when it is not properly initialized.
155 mWebEngine.Create( mWebViewSize.width, mWebViewSize.height, argc, argv );
168 mWebEngine.Destroy();
172 Toolkit::WebView WebView::New()
174 WebView* impl = new WebView();
175 Toolkit::WebView handle = Toolkit::WebView( *impl );
181 Toolkit::WebView WebView::New( const std::string& locale, const std::string& timezoneId )
183 WebView* impl = new WebView( locale, timezoneId );
184 Toolkit::WebView handle = Toolkit::WebView( *impl );
190 Toolkit::WebView WebView::New( int argc, char** argv )
192 WebView* impl = new WebView( argc, argv );
193 Toolkit::WebView handle = Toolkit::WebView( *impl );
199 void WebView::OnInitialize()
203 self.SetProperty(Actor::Property::KEYBOARD_FOCUSABLE, true);
204 self.TouchedSignal().Connect(this, &WebView::OnTouchEvent);
206 mPositionUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_POSITION, StepCondition( 1.0f, 1.0f ) );
207 mSizeUpdateNotification = self.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 1.0f, 1.0f ) );
208 mScaleUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
209 mPositionUpdateNotification.NotifySignal().Connect( this, &WebView::OnDisplayAreaUpdated );
210 mSizeUpdateNotification.NotifySignal().Connect( this, &WebView::OnDisplayAreaUpdated );
211 mScaleUpdateNotification.NotifySignal().Connect( this, &WebView::OnDisplayAreaUpdated );
215 mWebEngine.RegisterFrameRenderedCallback(std::bind(&WebView::OnFrameRendered, this));
216 mWebContext = std::unique_ptr<Dali::Toolkit::WebContext>(new WebContext(mWebEngine.GetContext()));
217 mWebCookieManager = std::unique_ptr<Dali::Toolkit::WebCookieManager>(new WebCookieManager(mWebEngine.GetCookieManager()));
218 mWebSettings = std::unique_ptr<Dali::Toolkit::WebSettings>(new WebSettings(mWebEngine.GetSettings()));
219 mWebBackForwardList = std::unique_ptr<Dali::Toolkit::WebBackForwardList>(new WebBackForwardList(mWebEngine.GetBackForwardList()));
223 Dali::Toolkit::WebSettings* WebView::GetSettings() const
225 return mWebSettings.get();
228 Dali::Toolkit::WebContext* WebView::GetContext() const
230 return mWebContext.get();
233 Dali::Toolkit::WebCookieManager* WebView::GetCookieManager() const
235 return mWebCookieManager.get();
238 Dali::Toolkit::WebBackForwardList* WebView::GetBackForwardList() const
240 return mWebBackForwardList.get();
243 Dali::WebEnginePlugin* WebView::GetPlugin() const
245 return mWebEngine ? mWebEngine.GetPlugin() : nullptr;
248 Dali::Toolkit::ImageView WebView::GetFavicon() const
250 Dali::Toolkit::ImageView faviconView;
253 Dali::PixelData pixelData = mWebEngine.GetFavicon();
256 std::string url = Dali::Toolkit::Image::GenerateUrl(pixelData);
257 faviconView = Dali::Toolkit::ImageView::New(url);
258 faviconView.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
264 void WebView::LoadUrl(const std::string& url)
268 mWebEngine.LoadUrl(url);
272 void WebView::LoadHtmlString(const std::string& htmlString)
276 mWebEngine.LoadHtmlString(htmlString);
280 void WebView::Reload()
288 void WebView::StopLoading()
292 mWebEngine.StopLoading();
296 void WebView::Suspend()
300 mWebEngine.Suspend();
304 void WebView::Resume()
312 void WebView::ScrollBy( int deltaX, int deltaY )
316 mWebEngine.ScrollBy( deltaX, deltaY );
320 bool WebView::CanGoForward()
322 return mWebEngine ? mWebEngine.CanGoForward() : false;
325 void WebView::GoForward()
329 mWebEngine.GoForward();
333 bool WebView::CanGoBack()
335 return mWebEngine ? mWebEngine.CanGoBack() : false;
338 void WebView::GoBack()
346 void WebView::EvaluateJavaScript( const std::string& script, Dali::WebEnginePlugin::JavaScriptMessageHandlerCallback resultHandler )
350 mWebEngine.EvaluateJavaScript( script, resultHandler );
354 void WebView::AddJavaScriptMessageHandler( const std::string& exposedObjectName, Dali::WebEnginePlugin::JavaScriptMessageHandlerCallback handler )
358 mWebEngine.AddJavaScriptMessageHandler( exposedObjectName, handler );
362 void WebView::ClearAllTilesResources()
366 mWebEngine.ClearAllTilesResources();
370 void WebView::ClearHistory()
374 mWebEngine.ClearHistory();
378 void WebView::SetTtsFocus(bool focused)
380 if(mWebEngine && !HasKeyInputFocus())
382 mWebEngine.SetFocus(focused);
386 void WebView::EnableVideoHole( bool enabled )
388 mVideoHoleEnabled = enabled;
389 EnableBlendMode(!mVideoHoleEnabled);
392 mWebEngine.EnableVideoHole(mVideoHoleEnabled);
396 void WebView::EnableBlendMode(bool blendEnabled)
399 for (uint32_t i = 0; i < self.GetRendererCount(); i++)
401 Dali::Renderer render = self.GetRendererAt(i);
402 render.SetProperty(Renderer::Property::BLEND_MODE, blendEnabled ? BlendMode::ON : BlendMode::OFF);
406 void WebView::RegisterPageLoadStartedCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback)
410 mWebEngine.RegisterPageLoadStartedCallback(callback);
414 void WebView::RegisterPageLoadFinishedCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback)
418 mWebEngine.RegisterPageLoadFinishedCallback(callback);
422 void WebView::RegisterPageLoadErrorCallback(Dali::WebEnginePlugin::WebEnginePageLoadErrorCallback callback)
426 mWebEngine.RegisterPageLoadErrorCallback(callback);
430 void WebView::RegisterScrollEdgeReachedCallback(Dali::WebEnginePlugin::WebEngineScrollEdgeReachedCallback callback)
434 mWebEngine.RegisterScrollEdgeReachedCallback(callback);
438 void WebView::RegisterUrlChangedCallback(Dali::WebEnginePlugin::WebEngineUrlChangedCallback callback)
442 mWebEngine.RegisterUrlChangedCallback(callback);
446 void WebView::RegisterNavigationPolicyDecidedCallback(Dali::WebEnginePlugin::WebEngineNavigationPolicyDecidedCallback callback)
450 mWebEngine.RegisterNavigationPolicyDecidedCallback(callback);
454 void WebView::RegisterNewWindowCreatedCallback(Dali::WebEnginePlugin::WebEngineNewWindowCreatedCallback callback)
458 mWebEngine.RegisterNewWindowCreatedCallback(callback);
462 void WebView::GetPlainTextAsynchronously(Dali::WebEnginePlugin::PlainTextReceivedCallback callback)
466 mWebEngine.GetPlainTextAsynchronously(callback);
470 Vector3 WebView::GetNaturalSize()
474 Vector2 rendererNaturalSize;
475 mVisual.GetNaturalSize( rendererNaturalSize );
476 return Vector3( rendererNaturalSize );
478 return Vector3( mWebViewSize );
481 void WebView::OnSceneConnection( int depth )
483 Control::OnSceneConnection( depth );
484 EnableBlendMode( !mVideoHoleEnabled );
487 void WebView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
489 Toolkit::WebView webView = Toolkit::WebView::DownCast( Dali::BaseHandle( object ) );
492 WebView& impl = GetImpl( webView );
495 case Toolkit::WebView::Property::URL:
498 if( value.Get( url ) )
504 case Toolkit::WebView::Property::USER_AGENT:
507 if( value.Get( input ) )
509 impl.SetUserAgent( input );
513 case Toolkit::WebView::Property::SCROLL_POSITION:
516 if ( value.Get( input ) )
518 impl.SetScrollPosition( input.x, input.y );
522 case Toolkit::WebView::Property::VIDEO_HOLE_ENABLED:
525 if( value.Get( input ) )
527 impl.EnableVideoHole( input );
535 Property::Value WebView::GetProperty( BaseObject* object, Property::Index propertyIndex )
537 Property::Value value;
538 Toolkit::WebView webView = Toolkit::WebView::DownCast( Dali::BaseHandle( object ) );
541 WebView& impl = GetImpl( webView );
542 switch( propertyIndex )
544 case Toolkit::WebView::Property::URL:
546 value = impl.GetUrl();
549 case Toolkit::WebView::Property::USER_AGENT:
551 value = impl.GetUserAgent();
554 case Toolkit::WebView::Property::SCROLL_POSITION:
557 impl.GetScrollPosition( x, y );
558 value = Vector2( x, y );
561 case Toolkit::WebView::Property::SCROLL_SIZE:
564 impl.GetScrollSize( width, height );
565 value = Vector2( width, height );
568 case Toolkit::WebView::Property::CONTENT_SIZE:
571 impl.GetContentSize( width, height );
572 value = Vector2( width, height );
575 case Toolkit::WebView::Property::TITLE:
577 value = impl.GetTitle();
580 case Toolkit::WebView::Property::VIDEO_HOLE_ENABLED:
582 value = impl.mVideoHoleEnabled;
593 bool WebView::OnTouchEvent( Actor actor, const Dali::TouchEvent& touch )
598 result = mWebEngine.SendTouchEvent( touch );
603 bool WebView::OnKeyEvent( const Dali::KeyEvent& event )
608 result = mWebEngine.SendKeyEvent( event );
613 void WebView::OnKeyInputFocusGained()
617 mWebEngine.SetFocus( true );
619 EmitKeyInputFocusSignal( true ); // Calls back into the Control hence done last.
622 void WebView::OnKeyInputFocusLost()
626 mWebEngine.SetFocus( false );
628 EmitKeyInputFocusSignal( false ); // Calls back into the Control hence done last.
631 void WebView::OnFrameRendered()
633 // Make sure that mVisual is created only once.
637 mVisual = CreateNativeImageVisual(mWebEngine.GetNativeImageSource());
640 DevelControl::RegisterVisual(*this, Toolkit::WebView::Property::URL, mVisual);
641 EnableBlendMode(!mVideoHoleEnabled);
645 void WebView::OnDisplayAreaUpdated(Dali::PropertyNotification& /*source*/)
650 Actor self( Self() );
652 bool positionUsesAnchorPoint = self.GetProperty< bool >( Actor::Property::POSITION_USES_ANCHOR_POINT );
653 Vector3 actorSize = self.GetCurrentProperty< Vector3 >( Actor::Property::SIZE ) * self.GetCurrentProperty< Vector3 >( Actor::Property::SCALE );
654 Vector3 anchorPointOffSet = actorSize * ( positionUsesAnchorPoint ? self.GetCurrentProperty< Vector3 >( Actor::Property::ANCHOR_POINT ) : AnchorPoint::TOP_LEFT );
655 Vector2 screenPosition = self.GetProperty< Vector2 >( Actor::Property::SCREEN_POSITION );
657 Dali::Rect< int > displayArea;
658 displayArea.x = screenPosition.x - anchorPointOffSet.x;
659 displayArea.y = screenPosition.y - anchorPointOffSet.y;
660 displayArea.width = actorSize.x;
661 displayArea.height = actorSize.y;
663 Size displaySize = Size( displayArea.width, displayArea.height );
664 if ( mWebViewSize != displaySize )
666 mWebViewSize = displaySize;
669 if (mWebViewArea != displayArea )
671 mWebViewArea = displayArea;
672 mWebEngine.UpdateDisplayArea( mWebViewArea );
676 void WebView::SetScrollPosition( int x, int y )
680 mWebEngine.SetScrollPosition( x, y );
684 void WebView::GetScrollPosition( int& x, int& y ) const
688 mWebEngine.GetScrollPosition( x, y );
692 void WebView::GetScrollSize( int& width, int& height ) const
696 mWebEngine.GetScrollSize( width, height );
700 void WebView::GetContentSize( int& width, int& height ) const
704 mWebEngine.GetContentSize( width, height );
708 std::string WebView::GetUrl() const
710 return mWebEngine ? mWebEngine.GetUrl() : kEmptyString;
713 std::string WebView::GetTitle() const
715 return mWebEngine ? mWebEngine.GetTitle() : kEmptyString;
718 std::string WebView::GetUserAgent() const
720 return mWebEngine ? mWebEngine.GetUserAgent() : kEmptyString;
723 void WebView::SetUserAgent( const std::string& userAgent )
727 mWebEngine.SetUserAgent( userAgent );
731 } // namespace Internal
733 } // namespace Toolkit