b0d81de349deabde3ce624bc492e25c953e22569
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / web-view / web-view-impl.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "web-view-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <cstring>
23 #include <dali/devel-api/scripting/enum-helper.h>
24 #include <dali/devel-api/scripting/scripting.h>
25 #include <dali/public-api/common/stage.h>
26 #include <dali/public-api/images/native-image.h>
27 #include <dali/public-api/adaptor-framework/native-image-source.h>
28 #include <dali/public-api/object/type-registry.h>
29 #include <dali/public-api/object/type-registry-helper.h>
30
31 // INTERNAL INCLUDES
32 #include <dali-toolkit/devel-api/controls/control-devel.h>
33 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
34
35 namespace Dali
36 {
37
38 namespace Toolkit
39 {
40
41 namespace Internal
42 {
43
44 namespace
45 {
46
47 BaseHandle Create()
48 {
49   return Toolkit::WebView::New();
50 }
51
52 DALI_ENUM_TO_STRING_TABLE_BEGIN( CacheModel )
53 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::WebView::CacheModel, DOCUMENT_VIEWER )
54 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::WebView::CacheModel, DOCUMENT_BROWSER )
55 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::WebView::CacheModel, PRIMARY_WEB_BROWSER )
56 DALI_ENUM_TO_STRING_TABLE_END( CacheModel )
57
58 DALI_ENUM_TO_STRING_TABLE_BEGIN( CookieAcceptPolicy )
59 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::WebView::CookieAcceptPolicy, ALWAYS )
60 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::WebView::CookieAcceptPolicy, NEVER )
61 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::WebView::CookieAcceptPolicy, NO_THIRD_PARTY )
62 DALI_ENUM_TO_STRING_TABLE_END( CookieAcceptPolicy )
63
64 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::WebView, Toolkit::Control, Create )
65
66 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "url",                     STRING,  URL                        )
67 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "cacheModel",              STRING,  CACHE_MODEL                )
68 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "cookieAcceptPolicy",      STRING,  COOKIE_ACCEPT_POLICY       )
69 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "userAgent",               STRING,  USER_AGENT                 )
70 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "enableJavaScript",        BOOLEAN, ENABLE_JAVASCRIPT          )
71 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "loadImagesAutomatically", BOOLEAN, LOAD_IMAGES_AUTOMATICALLY  )
72 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "defaultTextEncodingName", STRING,  DEFAULT_TEXT_ENCODING_NAME )
73 DALI_PROPERTY_REGISTRATION( Toolkit, WebView, "defaultFontSize",         INTEGER, DEFAULT_FONT_SIZE          )
74
75 DALI_SIGNAL_REGISTRATION(   Toolkit, WebView, "pageLoadStarted",         PAGE_LOAD_STARTED_SIGNAL            )
76 DALI_SIGNAL_REGISTRATION(   Toolkit, WebView, "pageLoadFinished",        PAGE_LOAD_FINISHED_SIGNAL           )
77 DALI_SIGNAL_REGISTRATION(   Toolkit, WebView, "pageLoadError",           PAGE_LOAD_ERROR_SIGNAL              )
78
79 DALI_TYPE_REGISTRATION_END()
80
81 const std::string kEmptyString;
82
83 } // anonymous namepsace
84
85 #define GET_ENUM_STRING( structName, inputExp ) \
86   Scripting::GetLinearEnumerationName< Toolkit::WebView::structName::Type >( static_cast< Toolkit::WebView::structName::Type >( inputExp ), structName##_TABLE, structName##_TABLE_COUNT )
87
88 #define GET_ENUM_VALUE( structName, inputExp, outputExp ) \
89   Scripting::GetEnumerationProperty< Toolkit::WebView::structName::Type >( inputExp, structName##_TABLE, structName##_TABLE_COUNT, outputExp )
90
91 WebView::WebView( const std::string& locale, const std::string& timezoneId )
92 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
93   mUrl(),
94   mVisual(),
95   mWebViewSize( Stage::GetCurrent().GetSize() ),
96   mWebEngine(),
97   mPageLoadStartedSignal(),
98   mPageLoadFinishedSignal(),
99   mPageLoadErrorSignal()
100 {
101   mWebEngine = Dali::WebEngine::New();
102
103   // WebEngine is empty when it is not properly initialized.
104   if( mWebEngine )
105   {
106     mWebEngine.Create( mWebViewSize.width, mWebViewSize.height, locale, timezoneId );
107   }
108 }
109
110 WebView::WebView()
111 : WebView( "", "" )
112 {
113 }
114
115 WebView::~WebView()
116 {
117 }
118
119 Toolkit::WebView WebView::New()
120 {
121   WebView* impl = new WebView();
122   Toolkit::WebView handle = Toolkit::WebView( *impl );
123
124   impl->Initialize();
125   return handle;
126 }
127
128 Toolkit::WebView WebView::New( const std::string& locale, const std::string& timezoneId )
129 {
130   WebView* impl = new WebView( locale, timezoneId );
131   Toolkit::WebView handle = Toolkit::WebView( *impl );
132
133   impl->Initialize();
134   return handle;
135 }
136
137 void WebView::OnInitialize()
138 {
139   Self().SetKeyboardFocusable( true );
140   Self().TouchSignal().Connect( this, &WebView::OnTouchEvent );
141
142   if( mWebEngine )
143   {
144     mWebEngine.PageLoadStartedSignal().Connect( this, &WebView::OnPageLoadStarted );
145     mWebEngine.PageLoadFinishedSignal().Connect( this, &WebView::OnPageLoadFinished );
146     mWebEngine.PageLoadErrorSignal().Connect( this, &WebView::OnPageLoadError );
147   }
148 }
149
150 void WebView::LoadUrl( const std::string& url )
151 {
152   mUrl = url;
153   if( mWebEngine )
154   {
155     Dali::Image image = Dali::NativeImage::New( *mWebEngine.GetNativeImageSource() );
156     mVisual = Toolkit::VisualFactory::Get().CreateVisual( image );
157
158     if( mVisual )
159     {
160       // Clean up previously registered visual and add new one.
161       DevelControl::RegisterVisual( *this, Toolkit::WebView::Property::URL, mVisual );
162       mWebEngine.LoadUrl( url );
163     }
164   }
165 }
166
167 void WebView::LoadHTMLString( const std::string& htmlString )
168 {
169   if( mWebEngine )
170   {
171     Dali::Image image = Dali::NativeImage::New( *mWebEngine.GetNativeImageSource() );
172     mVisual = Toolkit::VisualFactory::Get().CreateVisual( image );
173
174     if( mVisual )
175     {
176       DevelControl::RegisterVisual( *this, Toolkit::WebView::Property::URL, mVisual );
177       mWebEngine.LoadHTMLString( htmlString );
178     }
179   }
180 }
181
182 void WebView::Reload()
183 {
184   if( mWebEngine )
185   {
186     mWebEngine.Reload();
187   }
188 }
189
190 void WebView::StopLoading()
191 {
192   if( mWebEngine )
193   {
194     mWebEngine.StopLoading();
195   }
196 }
197
198 void WebView::Suspend()
199 {
200   if( mWebEngine )
201   {
202     mWebEngine.Suspend();
203   }
204 }
205
206 void WebView::Resume()
207 {
208   if( mWebEngine )
209   {
210     mWebEngine.Resume();
211   }
212 }
213
214 bool WebView::CanGoForward()
215 {
216   return mWebEngine ? mWebEngine.CanGoForward() : false;
217 }
218
219 void WebView::GoForward()
220 {
221   if( mWebEngine )
222   {
223     mWebEngine.GoForward();
224   }
225 }
226
227 bool WebView::CanGoBack()
228 {
229   return mWebEngine ? mWebEngine.CanGoBack() : false;
230 }
231
232 void WebView::GoBack()
233 {
234   if( mWebEngine )
235   {
236     mWebEngine.GoBack();
237   }
238 }
239
240 void WebView::EvaluateJavaScript( const std::string& script, std::function< void( const std::string& ) > resultHandler )
241 {
242   if( mWebEngine )
243   {
244     mWebEngine.EvaluateJavaScript( script, resultHandler );
245   }
246 }
247
248 void WebView::AddJavaScriptMessageHandler( const std::string& exposedObjectName, std::function< void( const std::string& ) > handler )
249 {
250   if( mWebEngine )
251   {
252     mWebEngine.AddJavaScriptMessageHandler( exposedObjectName, handler );
253   }
254 }
255
256 void WebView::ClearHistory()
257 {
258   if( mWebEngine )
259   {
260     mWebEngine.ClearHistory();
261   }
262 }
263
264 void WebView::ClearCache()
265 {
266   if( mWebEngine )
267   {
268     mWebEngine.ClearCache();
269   }
270 }
271
272 void WebView::ClearCookies()
273 {
274   if( mWebEngine )
275   {
276     mWebEngine.ClearCookies();
277   }
278 }
279
280 Dali::Toolkit::WebView::WebViewPageLoadSignalType& WebView::PageLoadStartedSignal()
281 {
282   return mPageLoadStartedSignal;
283 }
284
285 Dali::Toolkit::WebView::WebViewPageLoadSignalType& WebView::PageLoadFinishedSignal()
286 {
287   return mPageLoadFinishedSignal;
288 }
289
290 Dali::Toolkit::WebView::WebViewPageLoadErrorSignalType& WebView::PageLoadErrorSignal()
291 {
292   return mPageLoadErrorSignal;
293 }
294
295 void WebView::OnPageLoadStarted( const std::string& url )
296 {
297   if( !mPageLoadStartedSignal.Empty() )
298   {
299     Dali::Toolkit::WebView handle( GetOwner() );
300     mPageLoadStartedSignal.Emit( handle, url );
301   }
302 }
303
304 void WebView::OnPageLoadFinished( const std::string& url )
305 {
306   if( !mPageLoadFinishedSignal.Empty() )
307   {
308     Dali::Toolkit::WebView handle( GetOwner() );
309     mPageLoadFinishedSignal.Emit( handle, url );
310   }
311 }
312
313 void WebView::OnPageLoadError( const std::string& url, int errorCode )
314 {
315   if( !mPageLoadErrorSignal.Empty() )
316   {
317     Dali::Toolkit::WebView handle( GetOwner() );
318     mPageLoadErrorSignal.Emit( handle, url, static_cast< Toolkit::WebView::LoadErrorCode >( errorCode ) );
319   }
320 }
321
322 bool WebView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
323 {
324   Dali::BaseHandle handle( object );
325
326   bool connected = false;
327   Toolkit::WebView webView = Toolkit::WebView::DownCast( handle );
328
329   if( 0 == strcmp( signalName.c_str(), PAGE_LOAD_STARTED_SIGNAL ) )
330   {
331     webView.PageLoadStartedSignal().Connect( tracker, functor );
332     connected = true;
333   }
334   else if( 0 == strcmp( signalName.c_str(), PAGE_LOAD_FINISHED_SIGNAL ) )
335   {
336     webView.PageLoadFinishedSignal().Connect( tracker, functor );
337     connected = true;
338   }
339   else if( 0 == strcmp( signalName.c_str(), PAGE_LOAD_ERROR_SIGNAL ) )
340   {
341     webView.PageLoadErrorSignal().Connect( tracker, functor );
342     connected = true;
343   }
344
345   return connected;
346 }
347
348 Vector3 WebView::GetNaturalSize()
349 {
350   if( mVisual )
351   {
352     Vector2 rendererNaturalSize;
353     mVisual.GetNaturalSize( rendererNaturalSize );
354     return Vector3( rendererNaturalSize );
355   }
356
357   return Vector3( mWebViewSize );
358 }
359
360 void WebView::OnRelayout( const Vector2& size, RelayoutContainer& container )
361 {
362   Control::OnRelayout( size, container );
363
364   if( size.width > 0 && size.height > 0 && mWebViewSize != size )
365   {
366     mWebViewSize = size;
367
368     if( mWebEngine )
369     {
370       mWebEngine.SetSize( size.width, size.height );
371     }
372   }
373 }
374
375 void WebView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
376 {
377   Toolkit::WebView webView = Toolkit::WebView::DownCast( Dali::BaseHandle( object ) );
378
379   if( webView )
380   {
381     WebView& impl = GetImpl( webView );
382     switch( index )
383     {
384       case Toolkit::WebView::Property::URL:
385       {
386         std::string url;
387         if( value.Get( url ) )
388         {
389           impl.LoadUrl( url );
390         }
391         break;
392       }
393       case Toolkit::WebView::Property::CACHE_MODEL:
394       {
395         Toolkit::WebView::CacheModel::Type output = impl.GetCacheModel();
396         GET_ENUM_VALUE( CacheModel, value, output );
397         impl.SetCacheModel( output );
398         break;
399       }
400       case Toolkit::WebView::Property::COOKIE_ACCEPT_POLICY:
401       {
402         Toolkit::WebView::CookieAcceptPolicy::Type output = impl.GetCookieAcceptPolicy();
403         GET_ENUM_VALUE( CookieAcceptPolicy, value, output );
404         impl.SetCookieAcceptPolicy( output );
405         break;
406       }
407       case Toolkit::WebView::Property::USER_AGENT:
408       {
409         std::string input;
410         if( value.Get( input ) )
411         {
412           impl.SetUserAgent( input );
413         }
414         break;
415       }
416       case Toolkit::WebView::Property::ENABLE_JAVASCRIPT:
417       {
418         bool input;
419         if( value.Get( input ) )
420         {
421           impl.EnableJavaScript( input );
422         }
423         break;
424       }
425       case Toolkit::WebView::Property::LOAD_IMAGES_AUTOMATICALLY:
426       {
427         bool input;
428         if( value.Get( input ) )
429         {
430           impl.LoadImagesAutomatically( input );
431         }
432         break;
433       }
434       case Toolkit::WebView::Property::DEFAULT_TEXT_ENCODING_NAME:
435       {
436         std::string input;
437         if( value.Get( input ) )
438         {
439           impl.SetDefaultTextEncodingName( input );
440         }
441         break;
442       }
443       case Toolkit::WebView::Property::DEFAULT_FONT_SIZE:
444       {
445         int input;
446         if( value.Get( input ) )
447         {
448           impl.SetDefaultFontSize( input );
449         }
450         break;
451       }
452     }
453   }
454 }
455
456 Property::Value WebView::GetProperty( BaseObject* object, Property::Index propertyIndex )
457 {
458   Property::Value value;
459
460   Toolkit::WebView webView = Toolkit::WebView::DownCast( Dali::BaseHandle( object ) );
461
462   if( webView )
463   {
464     WebView& impl = GetImpl( webView );
465     switch( propertyIndex )
466     {
467       case Toolkit::WebView::Property::URL:
468       {
469         value = impl.mUrl;
470         break;
471       }
472       case Toolkit::WebView::Property::CACHE_MODEL:
473       {
474         value = GET_ENUM_STRING( CacheModel, impl.GetCacheModel() );
475         break;
476       }
477       case Toolkit::WebView::Property::COOKIE_ACCEPT_POLICY:
478       {
479         value = GET_ENUM_STRING( CookieAcceptPolicy, impl.GetCookieAcceptPolicy() );
480         break;
481       }
482       case Toolkit::WebView::Property::USER_AGENT:
483       {
484         value = impl.GetUserAgent();
485         break;
486       }
487       case Toolkit::WebView::Property::ENABLE_JAVASCRIPT:
488       {
489         value = impl.IsJavaScriptEnabled();
490         break;
491       }
492       case Toolkit::WebView::Property::LOAD_IMAGES_AUTOMATICALLY:
493       {
494         value = impl.AreImagesAutomaticallyLoaded();
495         break;
496       }
497       case Toolkit::WebView::Property::DEFAULT_TEXT_ENCODING_NAME:
498       {
499         value = impl.GetDefaultTextEncodingName();
500         break;
501       }
502       case Toolkit::WebView::Property::DEFAULT_FONT_SIZE:
503       {
504         value = impl.GetDefaultFontSize();
505         break;
506       }
507       default:
508          break;
509     }
510   }
511
512   return value;
513 }
514
515 bool WebView::OnTouchEvent( Actor actor, const Dali::TouchData& touch )
516 {
517   bool result = false;
518
519   if( mWebEngine )
520   {
521     result = mWebEngine.SendTouchEvent( touch );
522   }
523   return result;
524 }
525
526 bool WebView::OnKeyEvent( const Dali::KeyEvent& event )
527 {
528   bool result = false;
529
530   if( mWebEngine )
531   {
532     result = mWebEngine.SendKeyEvent( event );
533   }
534   return result;
535 }
536
537 Toolkit::WebView::CacheModel::Type WebView::GetCacheModel() const
538 {
539   return mWebEngine ? static_cast< Toolkit::WebView::CacheModel::Type >( mWebEngine.GetCacheModel() ) : Toolkit::WebView::CacheModel::DOCUMENT_VIEWER;
540 }
541
542 void WebView::SetCacheModel( Toolkit::WebView::CacheModel::Type cacheModel )
543 {
544   if( mWebEngine )
545   {
546     mWebEngine.SetCacheModel( static_cast< WebEnginePlugin::CacheModel >( cacheModel ) );
547   }
548 }
549
550 Toolkit::WebView::CookieAcceptPolicy::Type WebView::GetCookieAcceptPolicy() const
551 {
552   return mWebEngine ? static_cast< Toolkit::WebView::CookieAcceptPolicy::Type >( mWebEngine.GetCookieAcceptPolicy() ) : Toolkit::WebView::CookieAcceptPolicy::NO_THIRD_PARTY;
553 }
554
555 void WebView::SetCookieAcceptPolicy( Toolkit::WebView::CookieAcceptPolicy::Type policy )
556 {
557   if( mWebEngine )
558   {
559     mWebEngine.SetCookieAcceptPolicy( static_cast< WebEnginePlugin::CookieAcceptPolicy >( policy ) );
560   }
561 }
562
563 const std::string& WebView::GetUserAgent() const
564 {
565   return mWebEngine ? mWebEngine.GetUserAgent() : kEmptyString;
566 }
567
568 void WebView::SetUserAgent( const std::string& userAgent )
569 {
570   if( mWebEngine )
571   {
572     mWebEngine.SetUserAgent( userAgent );
573   }
574 }
575
576 bool WebView::IsJavaScriptEnabled() const
577 {
578   return mWebEngine ? mWebEngine.IsJavaScriptEnabled() : true;
579 }
580
581 void WebView::EnableJavaScript( bool enabled )
582 {
583   if( mWebEngine )
584   {
585     mWebEngine.EnableJavaScript( enabled );
586   }
587 }
588
589 bool WebView::AreImagesAutomaticallyLoaded() const
590 {
591   return mWebEngine ? mWebEngine.AreImagesAutomaticallyLoaded() : true;
592 }
593
594 void WebView::LoadImagesAutomatically( bool automatic )
595 {
596   if( mWebEngine )
597   {
598     mWebEngine.LoadImagesAutomatically( automatic );
599   }
600 }
601
602 const std::string& WebView::GetDefaultTextEncodingName() const
603 {
604   return mWebEngine ? mWebEngine.GetDefaultTextEncodingName() : kEmptyString;
605 }
606
607 void WebView::SetDefaultTextEncodingName( const std::string& defaultTextEncodingName )
608 {
609   if( mWebEngine )
610   {
611     mWebEngine.SetDefaultTextEncodingName( defaultTextEncodingName );
612   }
613 }
614
615 int WebView::GetDefaultFontSize() const
616 {
617   return mWebEngine ? mWebEngine.GetDefaultFontSize() : 0;
618 }
619
620 void WebView::SetDefaultFontSize( int defaultFontSize )
621 {
622   if( mWebEngine )
623   {
624     mWebEngine.SetDefaultFontSize( defaultFontSize );
625   }
626 }
627
628 #undef GET_ENUM_STRING
629 #undef GET_ENUM_VALUE
630
631 } // namespace Internal
632
633 } // namespace Toolkit
634
635 } // namespace Dali